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];
153b849b456SKenjiro Nakayama 
154b849b456SKenjiro Nakayama 	int nl_reply_supported;
1557c9e7a6fSAndy Grover };
1567c9e7a6fSAndy Grover 
1577c9e7a6fSAndy Grover #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev)
1587c9e7a6fSAndy Grover 
1597c9e7a6fSAndy Grover #define CMDR_OFF sizeof(struct tcmu_mailbox)
1607c9e7a6fSAndy Grover 
1617c9e7a6fSAndy Grover struct tcmu_cmd {
1627c9e7a6fSAndy Grover 	struct se_cmd *se_cmd;
1637c9e7a6fSAndy Grover 	struct tcmu_dev *tcmu_dev;
1647c9e7a6fSAndy Grover 
1657c9e7a6fSAndy Grover 	uint16_t cmd_id;
1667c9e7a6fSAndy Grover 
16726418649SSheng Yang 	/* Can't use se_cmd when cleaning up expired cmds, because if
1687c9e7a6fSAndy Grover 	   cmd has been completed then accessing se_cmd is off limits */
169141685a3SXiubo Li 	uint32_t dbi_cnt;
170141685a3SXiubo Li 	uint32_t dbi_cur;
171141685a3SXiubo Li 	uint32_t *dbi;
1727c9e7a6fSAndy Grover 
1737c9e7a6fSAndy Grover 	unsigned long deadline;
1747c9e7a6fSAndy Grover 
1757c9e7a6fSAndy Grover #define TCMU_CMD_BIT_EXPIRED 0
1767c9e7a6fSAndy Grover 	unsigned long flags;
1777c9e7a6fSAndy Grover };
1787c9e7a6fSAndy Grover 
179b6df4b79SXiubo Li static struct task_struct *unmap_thread;
180b6df4b79SXiubo Li static wait_queue_head_t unmap_wait;
181b6df4b79SXiubo Li static DEFINE_MUTEX(root_udev_mutex);
182b6df4b79SXiubo Li static LIST_HEAD(root_udev);
183b6df4b79SXiubo Li 
184b6df4b79SXiubo Li static atomic_t global_db_count = ATOMIC_INIT(0);
185b6df4b79SXiubo Li 
1867c9e7a6fSAndy Grover static struct kmem_cache *tcmu_cmd_cache;
1877c9e7a6fSAndy Grover 
1887c9e7a6fSAndy Grover /* multicast group */
1897c9e7a6fSAndy Grover enum tcmu_multicast_groups {
1907c9e7a6fSAndy Grover 	TCMU_MCGRP_CONFIG,
1917c9e7a6fSAndy Grover };
1927c9e7a6fSAndy Grover 
1937c9e7a6fSAndy Grover static const struct genl_multicast_group tcmu_mcgrps[] = {
1947c9e7a6fSAndy Grover 	[TCMU_MCGRP_CONFIG] = { .name = "config", },
1957c9e7a6fSAndy Grover };
1967c9e7a6fSAndy Grover 
197b3af66e2SMike Christie static struct nla_policy tcmu_attr_policy[TCMU_ATTR_MAX+1] = {
198b3af66e2SMike Christie 	[TCMU_ATTR_DEVICE]	= { .type = NLA_STRING },
199b3af66e2SMike Christie 	[TCMU_ATTR_MINOR]	= { .type = NLA_U32 },
200b3af66e2SMike Christie 	[TCMU_ATTR_CMD_STATUS]	= { .type = NLA_S32 },
201b3af66e2SMike Christie 	[TCMU_ATTR_DEVICE_ID]	= { .type = NLA_U32 },
202b3af66e2SMike Christie 	[TCMU_ATTR_SUPP_KERN_CMD_REPLY] = { .type = NLA_U8 },
203b3af66e2SMike Christie };
204b3af66e2SMike Christie 
205b3af66e2SMike Christie static int tcmu_genl_cmd_done(struct genl_info *info, int completed_cmd)
206b3af66e2SMike Christie {
207b3af66e2SMike Christie 	struct se_device *dev;
208b3af66e2SMike Christie 	struct tcmu_dev *udev;
209b3af66e2SMike Christie 	struct tcmu_nl_cmd *nl_cmd;
210b3af66e2SMike Christie 	int dev_id, rc, ret = 0;
211b3af66e2SMike Christie 	bool is_removed = (completed_cmd == TCMU_CMD_REMOVED_DEVICE);
212b3af66e2SMike Christie 
213b3af66e2SMike Christie 	if (!info->attrs[TCMU_ATTR_CMD_STATUS] ||
214b3af66e2SMike Christie 	    !info->attrs[TCMU_ATTR_DEVICE_ID]) {
215b3af66e2SMike Christie 		printk(KERN_ERR "TCMU_ATTR_CMD_STATUS or TCMU_ATTR_DEVICE_ID not set, doing nothing\n");
216b3af66e2SMike Christie                 return -EINVAL;
217b3af66e2SMike Christie         }
218b3af66e2SMike Christie 
219b3af66e2SMike Christie 	dev_id = nla_get_u32(info->attrs[TCMU_ATTR_DEVICE_ID]);
220b3af66e2SMike Christie 	rc = nla_get_s32(info->attrs[TCMU_ATTR_CMD_STATUS]);
221b3af66e2SMike Christie 
222b3af66e2SMike Christie 	dev = target_find_device(dev_id, !is_removed);
223b3af66e2SMike Christie 	if (!dev) {
224b3af66e2SMike Christie 		printk(KERN_ERR "tcmu nl cmd %u/%u completion could not find device with dev id %u.\n",
225b3af66e2SMike Christie 		       completed_cmd, rc, dev_id);
226b3af66e2SMike Christie 		return -ENODEV;
227b3af66e2SMike Christie 	}
228b3af66e2SMike Christie 	udev = TCMU_DEV(dev);
229b3af66e2SMike Christie 
230b3af66e2SMike Christie 	spin_lock(&udev->nl_cmd_lock);
231b3af66e2SMike Christie 	nl_cmd = &udev->curr_nl_cmd;
232b3af66e2SMike Christie 
233b3af66e2SMike Christie 	pr_debug("genl cmd done got id %d curr %d done %d rc %d\n", dev_id,
234b3af66e2SMike Christie 		 nl_cmd->cmd, completed_cmd, rc);
235b3af66e2SMike Christie 
236b3af66e2SMike Christie 	if (nl_cmd->cmd != completed_cmd) {
237b3af66e2SMike Christie 		printk(KERN_ERR "Mismatched commands (Expecting reply for %d. Current %d).\n",
238b3af66e2SMike Christie 		       completed_cmd, nl_cmd->cmd);
239b3af66e2SMike Christie 		ret = -EINVAL;
240b3af66e2SMike Christie 	} else {
241b3af66e2SMike Christie 		nl_cmd->status = rc;
242b3af66e2SMike Christie 	}
243b3af66e2SMike Christie 
244b3af66e2SMike Christie 	spin_unlock(&udev->nl_cmd_lock);
245b3af66e2SMike Christie 	if (!is_removed)
246b3af66e2SMike Christie 		 target_undepend_item(&dev->dev_group.cg_item);
247b3af66e2SMike Christie 	if (!ret)
248b3af66e2SMike Christie 		complete(&nl_cmd->complete);
249b3af66e2SMike Christie 	return ret;
250b3af66e2SMike Christie }
251b3af66e2SMike Christie 
252b3af66e2SMike Christie static int tcmu_genl_rm_dev_done(struct sk_buff *skb, struct genl_info *info)
253b3af66e2SMike Christie {
254b3af66e2SMike Christie 	return tcmu_genl_cmd_done(info, TCMU_CMD_REMOVED_DEVICE);
255b3af66e2SMike Christie }
256b3af66e2SMike Christie 
257b3af66e2SMike Christie static int tcmu_genl_add_dev_done(struct sk_buff *skb, struct genl_info *info)
258b3af66e2SMike Christie {
259b3af66e2SMike Christie 	return tcmu_genl_cmd_done(info, TCMU_CMD_ADDED_DEVICE);
260b3af66e2SMike Christie }
261b3af66e2SMike Christie 
262b3af66e2SMike Christie static int tcmu_genl_reconfig_dev_done(struct sk_buff *skb,
263b3af66e2SMike Christie 				       struct genl_info *info)
264b3af66e2SMike Christie {
265b3af66e2SMike Christie 	return tcmu_genl_cmd_done(info, TCMU_CMD_RECONFIG_DEVICE);
266b3af66e2SMike Christie }
267b3af66e2SMike Christie 
268b3af66e2SMike Christie static int tcmu_genl_set_features(struct sk_buff *skb, struct genl_info *info)
269b3af66e2SMike Christie {
270b3af66e2SMike Christie 	if (info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]) {
271b3af66e2SMike Christie 		tcmu_kern_cmd_reply_supported  =
272b3af66e2SMike Christie 			nla_get_u8(info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]);
273b3af66e2SMike Christie 		printk(KERN_INFO "tcmu daemon: command reply support %u.\n",
274b3af66e2SMike Christie 		       tcmu_kern_cmd_reply_supported);
275b3af66e2SMike Christie 	}
276b3af66e2SMike Christie 
277b3af66e2SMike Christie 	return 0;
278b3af66e2SMike Christie }
279b3af66e2SMike Christie 
280b3af66e2SMike Christie static const struct genl_ops tcmu_genl_ops[] = {
281b3af66e2SMike Christie 	{
282b3af66e2SMike Christie 		.cmd	= TCMU_CMD_SET_FEATURES,
283b3af66e2SMike Christie 		.flags	= GENL_ADMIN_PERM,
284b3af66e2SMike Christie 		.policy	= tcmu_attr_policy,
285b3af66e2SMike Christie 		.doit	= tcmu_genl_set_features,
286b3af66e2SMike Christie 	},
287b3af66e2SMike Christie 	{
288b3af66e2SMike Christie 		.cmd	= TCMU_CMD_ADDED_DEVICE_DONE,
289b3af66e2SMike Christie 		.flags	= GENL_ADMIN_PERM,
290b3af66e2SMike Christie 		.policy	= tcmu_attr_policy,
291b3af66e2SMike Christie 		.doit	= tcmu_genl_add_dev_done,
292b3af66e2SMike Christie 	},
293b3af66e2SMike Christie 	{
294b3af66e2SMike Christie 		.cmd	= TCMU_CMD_REMOVED_DEVICE_DONE,
295b3af66e2SMike Christie 		.flags	= GENL_ADMIN_PERM,
296b3af66e2SMike Christie 		.policy	= tcmu_attr_policy,
297b3af66e2SMike Christie 		.doit	= tcmu_genl_rm_dev_done,
298b3af66e2SMike Christie 	},
299b3af66e2SMike Christie 	{
300b3af66e2SMike Christie 		.cmd	= TCMU_CMD_RECONFIG_DEVICE_DONE,
301b3af66e2SMike Christie 		.flags	= GENL_ADMIN_PERM,
302b3af66e2SMike Christie 		.policy	= tcmu_attr_policy,
303b3af66e2SMike Christie 		.doit	= tcmu_genl_reconfig_dev_done,
304b3af66e2SMike Christie 	},
305b3af66e2SMike Christie };
306b3af66e2SMike Christie 
3077c9e7a6fSAndy Grover /* Our generic netlink family */
30856989f6dSJohannes Berg static struct genl_family tcmu_genl_family __ro_after_init = {
309489111e5SJohannes Berg 	.module = THIS_MODULE,
3107c9e7a6fSAndy Grover 	.hdrsize = 0,
3117c9e7a6fSAndy Grover 	.name = "TCM-USER",
312b3af66e2SMike Christie 	.version = 2,
3137c9e7a6fSAndy Grover 	.maxattr = TCMU_ATTR_MAX,
3147c9e7a6fSAndy Grover 	.mcgrps = tcmu_mcgrps,
3157c9e7a6fSAndy Grover 	.n_mcgrps = ARRAY_SIZE(tcmu_mcgrps),
31620c08b36SSheng Yang 	.netnsok = true,
317b3af66e2SMike Christie 	.ops = tcmu_genl_ops,
318b3af66e2SMike Christie 	.n_ops = ARRAY_SIZE(tcmu_genl_ops),
3197c9e7a6fSAndy Grover };
3207c9e7a6fSAndy Grover 
321141685a3SXiubo Li #define tcmu_cmd_set_dbi_cur(cmd, index) ((cmd)->dbi_cur = (index))
322141685a3SXiubo Li #define tcmu_cmd_reset_dbi_cur(cmd) tcmu_cmd_set_dbi_cur(cmd, 0)
323141685a3SXiubo Li #define tcmu_cmd_set_dbi(cmd, index) ((cmd)->dbi[(cmd)->dbi_cur++] = (index))
324141685a3SXiubo Li #define tcmu_cmd_get_dbi(cmd) ((cmd)->dbi[(cmd)->dbi_cur++])
325141685a3SXiubo Li 
326b6df4b79SXiubo Li static void tcmu_cmd_free_data(struct tcmu_cmd *tcmu_cmd, uint32_t len)
327141685a3SXiubo Li {
328141685a3SXiubo Li 	struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
329141685a3SXiubo Li 	uint32_t i;
330141685a3SXiubo Li 
331b6df4b79SXiubo Li 	for (i = 0; i < len; i++)
332141685a3SXiubo Li 		clear_bit(tcmu_cmd->dbi[i], udev->data_bitmap);
333141685a3SXiubo Li }
334141685a3SXiubo Li 
335b6df4b79SXiubo Li static inline bool tcmu_get_empty_block(struct tcmu_dev *udev,
336b6df4b79SXiubo Li 					struct tcmu_cmd *tcmu_cmd)
337141685a3SXiubo Li {
338b6df4b79SXiubo Li 	struct page *page;
339b6df4b79SXiubo Li 	int ret, dbi;
340141685a3SXiubo Li 
341b6df4b79SXiubo Li 	dbi = find_first_zero_bit(udev->data_bitmap, udev->dbi_thresh);
342b6df4b79SXiubo Li 	if (dbi == udev->dbi_thresh)
343b6df4b79SXiubo Li 		return false;
344b6df4b79SXiubo Li 
345b6df4b79SXiubo Li 	page = radix_tree_lookup(&udev->data_blocks, dbi);
346b6df4b79SXiubo Li 	if (!page) {
347b6df4b79SXiubo Li 		if (atomic_add_return(1, &global_db_count) >
348b6df4b79SXiubo Li 					TCMU_GLOBAL_MAX_BLOCKS) {
349b6df4b79SXiubo Li 			atomic_dec(&global_db_count);
350b6df4b79SXiubo Li 			return false;
351b6df4b79SXiubo Li 		}
352b6df4b79SXiubo Li 
353b6df4b79SXiubo Li 		/* try to get new page from the mm */
354b6df4b79SXiubo Li 		page = alloc_page(GFP_KERNEL);
355b6df4b79SXiubo Li 		if (!page)
356daf78c30SXiubo Li 			goto err_alloc;
357b6df4b79SXiubo Li 
358b6df4b79SXiubo Li 		ret = radix_tree_insert(&udev->data_blocks, dbi, page);
359daf78c30SXiubo Li 		if (ret)
360daf78c30SXiubo Li 			goto err_insert;
361b6df4b79SXiubo Li 	}
362b6df4b79SXiubo Li 
363141685a3SXiubo Li 	if (dbi > udev->dbi_max)
364141685a3SXiubo Li 		udev->dbi_max = dbi;
365141685a3SXiubo Li 
366141685a3SXiubo Li 	set_bit(dbi, udev->data_bitmap);
367b6df4b79SXiubo Li 	tcmu_cmd_set_dbi(tcmu_cmd, dbi);
368141685a3SXiubo Li 
369b6df4b79SXiubo Li 	return true;
370daf78c30SXiubo Li err_insert:
371daf78c30SXiubo Li 	__free_page(page);
372daf78c30SXiubo Li err_alloc:
373daf78c30SXiubo Li 	atomic_dec(&global_db_count);
374daf78c30SXiubo Li 	return false;
375141685a3SXiubo Li }
376141685a3SXiubo Li 
377b6df4b79SXiubo Li static bool tcmu_get_empty_blocks(struct tcmu_dev *udev,
378b6df4b79SXiubo Li 				  struct tcmu_cmd *tcmu_cmd)
379b6df4b79SXiubo Li {
380b6df4b79SXiubo Li 	int i;
381b6df4b79SXiubo Li 
382b6df4b79SXiubo Li 	udev->waiting_global = false;
383b6df4b79SXiubo Li 
384b6df4b79SXiubo Li 	for (i = tcmu_cmd->dbi_cur; i < tcmu_cmd->dbi_cnt; i++) {
385b6df4b79SXiubo Li 		if (!tcmu_get_empty_block(udev, tcmu_cmd))
386b6df4b79SXiubo Li 			goto err;
387141685a3SXiubo Li 	}
388b6df4b79SXiubo Li 	return true;
389b6df4b79SXiubo Li 
390b6df4b79SXiubo Li err:
391b6df4b79SXiubo Li 	udev->waiting_global = true;
392b6df4b79SXiubo Li 	/* Try to wake up the unmap thread */
393b6df4b79SXiubo Li 	wake_up(&unmap_wait);
394b6df4b79SXiubo Li 	return false;
395141685a3SXiubo Li }
396141685a3SXiubo Li 
397b6df4b79SXiubo Li static inline struct page *
398b6df4b79SXiubo Li tcmu_get_block_page(struct tcmu_dev *udev, uint32_t dbi)
399141685a3SXiubo Li {
400141685a3SXiubo Li 	return radix_tree_lookup(&udev->data_blocks, dbi);
401141685a3SXiubo Li }
402141685a3SXiubo Li 
403141685a3SXiubo Li static inline void tcmu_free_cmd(struct tcmu_cmd *tcmu_cmd)
404141685a3SXiubo Li {
405141685a3SXiubo Li 	kfree(tcmu_cmd->dbi);
406141685a3SXiubo Li 	kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
407141685a3SXiubo Li }
408141685a3SXiubo Li 
409141685a3SXiubo Li static inline size_t tcmu_cmd_get_data_length(struct tcmu_cmd *tcmu_cmd)
410141685a3SXiubo Li {
411141685a3SXiubo Li 	struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
412141685a3SXiubo Li 	size_t data_length = round_up(se_cmd->data_length, DATA_BLOCK_SIZE);
413141685a3SXiubo Li 
414141685a3SXiubo Li 	if (se_cmd->se_cmd_flags & SCF_BIDI) {
415141685a3SXiubo Li 		BUG_ON(!(se_cmd->t_bidi_data_sg && se_cmd->t_bidi_data_nents));
416141685a3SXiubo Li 		data_length += round_up(se_cmd->t_bidi_data_sg->length,
417141685a3SXiubo Li 				DATA_BLOCK_SIZE);
418141685a3SXiubo Li 	}
419141685a3SXiubo Li 
420141685a3SXiubo Li 	return data_length;
421141685a3SXiubo Li }
422141685a3SXiubo Li 
423141685a3SXiubo Li static inline uint32_t tcmu_cmd_get_block_cnt(struct tcmu_cmd *tcmu_cmd)
424141685a3SXiubo Li {
425141685a3SXiubo Li 	size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd);
426141685a3SXiubo Li 
427141685a3SXiubo Li 	return data_length / DATA_BLOCK_SIZE;
428141685a3SXiubo Li }
429141685a3SXiubo Li 
4307c9e7a6fSAndy Grover static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd)
4317c9e7a6fSAndy Grover {
4327c9e7a6fSAndy Grover 	struct se_device *se_dev = se_cmd->se_dev;
4337c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
4347c9e7a6fSAndy Grover 	struct tcmu_cmd *tcmu_cmd;
4357c9e7a6fSAndy Grover 	int cmd_id;
4367c9e7a6fSAndy Grover 
4377c9e7a6fSAndy Grover 	tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_KERNEL);
4387c9e7a6fSAndy Grover 	if (!tcmu_cmd)
4397c9e7a6fSAndy Grover 		return NULL;
4407c9e7a6fSAndy Grover 
4417c9e7a6fSAndy Grover 	tcmu_cmd->se_cmd = se_cmd;
4427c9e7a6fSAndy Grover 	tcmu_cmd->tcmu_dev = udev;
443af980e46SMike Christie 	if (udev->cmd_time_out)
444af980e46SMike Christie 		tcmu_cmd->deadline = jiffies +
445af980e46SMike Christie 					msecs_to_jiffies(udev->cmd_time_out);
4467c9e7a6fSAndy Grover 
447141685a3SXiubo Li 	tcmu_cmd_reset_dbi_cur(tcmu_cmd);
448141685a3SXiubo Li 	tcmu_cmd->dbi_cnt = tcmu_cmd_get_block_cnt(tcmu_cmd);
449141685a3SXiubo Li 	tcmu_cmd->dbi = kcalloc(tcmu_cmd->dbi_cnt, sizeof(uint32_t),
450141685a3SXiubo Li 				GFP_KERNEL);
451141685a3SXiubo Li 	if (!tcmu_cmd->dbi) {
452141685a3SXiubo Li 		kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
453141685a3SXiubo Li 		return NULL;
454141685a3SXiubo Li 	}
455141685a3SXiubo Li 
4567c9e7a6fSAndy Grover 	idr_preload(GFP_KERNEL);
4577c9e7a6fSAndy Grover 	spin_lock_irq(&udev->commands_lock);
4587c9e7a6fSAndy Grover 	cmd_id = idr_alloc(&udev->commands, tcmu_cmd, 0,
4597c9e7a6fSAndy Grover 		USHRT_MAX, GFP_NOWAIT);
4607c9e7a6fSAndy Grover 	spin_unlock_irq(&udev->commands_lock);
4617c9e7a6fSAndy Grover 	idr_preload_end();
4627c9e7a6fSAndy Grover 
4637c9e7a6fSAndy Grover 	if (cmd_id < 0) {
464141685a3SXiubo Li 		tcmu_free_cmd(tcmu_cmd);
4657c9e7a6fSAndy Grover 		return NULL;
4667c9e7a6fSAndy Grover 	}
4677c9e7a6fSAndy Grover 	tcmu_cmd->cmd_id = cmd_id;
4687c9e7a6fSAndy Grover 
4697c9e7a6fSAndy Grover 	return tcmu_cmd;
4707c9e7a6fSAndy Grover }
4717c9e7a6fSAndy Grover 
4727c9e7a6fSAndy Grover static inline void tcmu_flush_dcache_range(void *vaddr, size_t size)
4737c9e7a6fSAndy Grover {
474b75d8063SGeliang Tang 	unsigned long offset = offset_in_page(vaddr);
4757c9e7a6fSAndy Grover 
4767c9e7a6fSAndy Grover 	size = round_up(size+offset, PAGE_SIZE);
4777c9e7a6fSAndy Grover 	vaddr -= offset;
4787c9e7a6fSAndy Grover 
4797c9e7a6fSAndy Grover 	while (size) {
4807c9e7a6fSAndy Grover 		flush_dcache_page(virt_to_page(vaddr));
4817c9e7a6fSAndy Grover 		size -= PAGE_SIZE;
4827c9e7a6fSAndy Grover 	}
4837c9e7a6fSAndy Grover }
4847c9e7a6fSAndy Grover 
4857c9e7a6fSAndy Grover /*
4867c9e7a6fSAndy Grover  * Some ring helper functions. We don't assume size is a power of 2 so
4877c9e7a6fSAndy Grover  * we can't use circ_buf.h.
4887c9e7a6fSAndy Grover  */
4897c9e7a6fSAndy Grover static inline size_t spc_used(size_t head, size_t tail, size_t size)
4907c9e7a6fSAndy Grover {
4917c9e7a6fSAndy Grover 	int diff = head - tail;
4927c9e7a6fSAndy Grover 
4937c9e7a6fSAndy Grover 	if (diff >= 0)
4947c9e7a6fSAndy Grover 		return diff;
4957c9e7a6fSAndy Grover 	else
4967c9e7a6fSAndy Grover 		return size + diff;
4977c9e7a6fSAndy Grover }
4987c9e7a6fSAndy Grover 
4997c9e7a6fSAndy Grover static inline size_t spc_free(size_t head, size_t tail, size_t size)
5007c9e7a6fSAndy Grover {
5017c9e7a6fSAndy Grover 	/* Keep 1 byte unused or we can't tell full from empty */
5027c9e7a6fSAndy Grover 	return (size - spc_used(head, tail, size) - 1);
5037c9e7a6fSAndy Grover }
5047c9e7a6fSAndy Grover 
5057c9e7a6fSAndy Grover static inline size_t head_to_end(size_t head, size_t size)
5067c9e7a6fSAndy Grover {
5077c9e7a6fSAndy Grover 	return size - head;
5087c9e7a6fSAndy Grover }
5097c9e7a6fSAndy Grover 
510f1dbd087SSheng Yang static inline void new_iov(struct iovec **iov, int *iov_cnt,
511f1dbd087SSheng Yang 			   struct tcmu_dev *udev)
512f1dbd087SSheng Yang {
513f1dbd087SSheng Yang 	struct iovec *iovec;
514f1dbd087SSheng Yang 
515f1dbd087SSheng Yang 	if (*iov_cnt != 0)
516f1dbd087SSheng Yang 		(*iov)++;
517f1dbd087SSheng Yang 	(*iov_cnt)++;
518f1dbd087SSheng Yang 
519f1dbd087SSheng Yang 	iovec = *iov;
520f1dbd087SSheng Yang 	memset(iovec, 0, sizeof(struct iovec));
521f1dbd087SSheng Yang }
522f1dbd087SSheng Yang 
5237c9e7a6fSAndy Grover #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size)
5247c9e7a6fSAndy Grover 
52526418649SSheng Yang /* offset is relative to mb_addr */
526141685a3SXiubo Li static inline size_t get_block_offset_user(struct tcmu_dev *dev,
527141685a3SXiubo Li 		int dbi, int remaining)
52826418649SSheng Yang {
529141685a3SXiubo Li 	return dev->data_off + dbi * DATA_BLOCK_SIZE +
53026418649SSheng Yang 		DATA_BLOCK_SIZE - remaining;
53126418649SSheng Yang }
53226418649SSheng Yang 
533daf78c30SXiubo Li static inline size_t iov_tail(struct iovec *iov)
53426418649SSheng Yang {
53526418649SSheng Yang 	return (size_t)iov->iov_base + iov->iov_len;
53626418649SSheng Yang }
53726418649SSheng Yang 
538b6df4b79SXiubo Li static int scatter_data_area(struct tcmu_dev *udev,
539141685a3SXiubo Li 	struct tcmu_cmd *tcmu_cmd, struct scatterlist *data_sg,
540141685a3SXiubo Li 	unsigned int data_nents, struct iovec **iov,
541141685a3SXiubo Li 	int *iov_cnt, bool copy_data)
542f97ec7dbSIlias Tsitsimpis {
543141685a3SXiubo Li 	int i, dbi;
54426418649SSheng Yang 	int block_remaining = 0;
545141685a3SXiubo Li 	void *from, *to = NULL;
546141685a3SXiubo Li 	size_t copy_bytes, to_offset, offset;
547f97ec7dbSIlias Tsitsimpis 	struct scatterlist *sg;
548b6df4b79SXiubo Li 	struct page *page;
549f97ec7dbSIlias Tsitsimpis 
550f97ec7dbSIlias Tsitsimpis 	for_each_sg(data_sg, sg, data_nents, i) {
55126418649SSheng Yang 		int sg_remaining = sg->length;
552f97ec7dbSIlias Tsitsimpis 		from = kmap_atomic(sg_page(sg)) + sg->offset;
55326418649SSheng Yang 		while (sg_remaining > 0) {
55426418649SSheng Yang 			if (block_remaining == 0) {
555b6df4b79SXiubo Li 				if (to)
556b6df4b79SXiubo Li 					kunmap_atomic(to);
557b6df4b79SXiubo Li 
55826418649SSheng Yang 				block_remaining = DATA_BLOCK_SIZE;
559b6df4b79SXiubo Li 				dbi = tcmu_cmd_get_dbi(tcmu_cmd);
560b6df4b79SXiubo Li 				page = tcmu_get_block_page(udev, dbi);
561b6df4b79SXiubo Li 				to = kmap_atomic(page);
562141685a3SXiubo Li 			}
563141685a3SXiubo Li 
56426418649SSheng Yang 			copy_bytes = min_t(size_t, sg_remaining,
56526418649SSheng Yang 					block_remaining);
566141685a3SXiubo Li 			to_offset = get_block_offset_user(udev, dbi,
56726418649SSheng Yang 					block_remaining);
568141685a3SXiubo Li 
56926418649SSheng Yang 			if (*iov_cnt != 0 &&
570daf78c30SXiubo Li 			    to_offset == iov_tail(*iov)) {
571f1dbd087SSheng Yang 				(*iov)->iov_len += copy_bytes;
57226418649SSheng Yang 			} else {
573f1dbd087SSheng Yang 				new_iov(iov, iov_cnt, udev);
57426418649SSheng Yang 				(*iov)->iov_base = (void __user *)to_offset;
575f97ec7dbSIlias Tsitsimpis 				(*iov)->iov_len = copy_bytes;
57626418649SSheng Yang 			}
577f97ec7dbSIlias Tsitsimpis 			if (copy_data) {
578c542942cSXiubo Li 				offset = DATA_BLOCK_SIZE - block_remaining;
579c542942cSXiubo Li 				memcpy(to + offset,
580c542942cSXiubo Li 				       from + sg->length - sg_remaining,
58126418649SSheng Yang 				       copy_bytes);
582f97ec7dbSIlias Tsitsimpis 				tcmu_flush_dcache_range(to, copy_bytes);
583f97ec7dbSIlias Tsitsimpis 			}
58426418649SSheng Yang 			sg_remaining -= copy_bytes;
58526418649SSheng Yang 			block_remaining -= copy_bytes;
586f97ec7dbSIlias Tsitsimpis 		}
587e2e21bd8SSagi Grimberg 		kunmap_atomic(from - sg->offset);
588f97ec7dbSIlias Tsitsimpis 	}
589b6df4b79SXiubo Li 	if (to)
590b6df4b79SXiubo Li 		kunmap_atomic(to);
591f97ec7dbSIlias Tsitsimpis 
592141685a3SXiubo Li 	return 0;
5930c28481fSSheng Yang }
5940c28481fSSheng Yang 
595a5d68ba8SXiubo Li static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
596a5d68ba8SXiubo Li 			     bool bidi)
597f97ec7dbSIlias Tsitsimpis {
598a5d68ba8SXiubo Li 	struct se_cmd *se_cmd = cmd->se_cmd;
599141685a3SXiubo Li 	int i, dbi;
60026418649SSheng Yang 	int block_remaining = 0;
601b6df4b79SXiubo Li 	void *from = NULL, *to;
602141685a3SXiubo Li 	size_t copy_bytes, offset;
603a5d68ba8SXiubo Li 	struct scatterlist *sg, *data_sg;
604b6df4b79SXiubo Li 	struct page *page;
605a5d68ba8SXiubo Li 	unsigned int data_nents;
606141685a3SXiubo Li 	uint32_t count = 0;
607a5d68ba8SXiubo Li 
608a5d68ba8SXiubo Li 	if (!bidi) {
609a5d68ba8SXiubo Li 		data_sg = se_cmd->t_data_sg;
610a5d68ba8SXiubo Li 		data_nents = se_cmd->t_data_nents;
611a5d68ba8SXiubo Li 	} else {
612a5d68ba8SXiubo Li 
613a5d68ba8SXiubo Li 		/*
614a5d68ba8SXiubo Li 		 * For bidi case, the first count blocks are for Data-Out
615a5d68ba8SXiubo Li 		 * buffer blocks, and before gathering the Data-In buffer
616a5d68ba8SXiubo Li 		 * the Data-Out buffer blocks should be discarded.
617a5d68ba8SXiubo Li 		 */
618a5d68ba8SXiubo Li 		count = DIV_ROUND_UP(se_cmd->data_length, DATA_BLOCK_SIZE);
619a5d68ba8SXiubo Li 
620a5d68ba8SXiubo Li 		data_sg = se_cmd->t_bidi_data_sg;
621a5d68ba8SXiubo Li 		data_nents = se_cmd->t_bidi_data_nents;
622a5d68ba8SXiubo Li 	}
623f97ec7dbSIlias Tsitsimpis 
624141685a3SXiubo Li 	tcmu_cmd_set_dbi_cur(cmd, count);
625141685a3SXiubo Li 
626f97ec7dbSIlias Tsitsimpis 	for_each_sg(data_sg, sg, data_nents, i) {
62726418649SSheng Yang 		int sg_remaining = sg->length;
628f97ec7dbSIlias Tsitsimpis 		to = kmap_atomic(sg_page(sg)) + sg->offset;
62926418649SSheng Yang 		while (sg_remaining > 0) {
63026418649SSheng Yang 			if (block_remaining == 0) {
631b6df4b79SXiubo Li 				if (from)
632b6df4b79SXiubo Li 					kunmap_atomic(from);
633b6df4b79SXiubo Li 
63426418649SSheng Yang 				block_remaining = DATA_BLOCK_SIZE;
635141685a3SXiubo Li 				dbi = tcmu_cmd_get_dbi(cmd);
636b6df4b79SXiubo Li 				page = tcmu_get_block_page(udev, dbi);
637b6df4b79SXiubo Li 				from = kmap_atomic(page);
63826418649SSheng Yang 			}
63926418649SSheng Yang 			copy_bytes = min_t(size_t, sg_remaining,
64026418649SSheng Yang 					block_remaining);
641141685a3SXiubo Li 			offset = DATA_BLOCK_SIZE - block_remaining;
642f97ec7dbSIlias Tsitsimpis 			tcmu_flush_dcache_range(from, copy_bytes);
643c542942cSXiubo Li 			memcpy(to + sg->length - sg_remaining, from + offset,
64426418649SSheng Yang 					copy_bytes);
645f97ec7dbSIlias Tsitsimpis 
64626418649SSheng Yang 			sg_remaining -= copy_bytes;
64726418649SSheng Yang 			block_remaining -= copy_bytes;
648f97ec7dbSIlias Tsitsimpis 		}
649e2e21bd8SSagi Grimberg 		kunmap_atomic(to - sg->offset);
650f97ec7dbSIlias Tsitsimpis 	}
651b6df4b79SXiubo Li 	if (from)
652b6df4b79SXiubo Li 		kunmap_atomic(from);
653f97ec7dbSIlias Tsitsimpis }
654f97ec7dbSIlias Tsitsimpis 
655b6df4b79SXiubo Li static inline size_t spc_bitmap_free(unsigned long *bitmap, uint32_t thresh)
65626418649SSheng Yang {
657b6df4b79SXiubo Li 	return DATA_BLOCK_SIZE * (thresh - bitmap_weight(bitmap, thresh));
65826418649SSheng Yang }
65926418649SSheng Yang 
6607c9e7a6fSAndy Grover /*
661f97ec7dbSIlias Tsitsimpis  * We can't queue a command until we have space available on the cmd ring *and*
6623d9b9555SAndy Grover  * space available on the data area.
6637c9e7a6fSAndy Grover  *
6647c9e7a6fSAndy Grover  * Called with ring lock held.
6657c9e7a6fSAndy Grover  */
666b6df4b79SXiubo Li static bool is_ring_space_avail(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
667b6df4b79SXiubo Li 		size_t cmd_size, size_t data_needed)
6687c9e7a6fSAndy Grover {
6697c9e7a6fSAndy Grover 	struct tcmu_mailbox *mb = udev->mb_addr;
670b6df4b79SXiubo Li 	uint32_t blocks_needed = (data_needed + DATA_BLOCK_SIZE - 1)
671b6df4b79SXiubo Li 				/ DATA_BLOCK_SIZE;
6720241fd39SNicholas Bellinger 	size_t space, cmd_needed;
6737c9e7a6fSAndy Grover 	u32 cmd_head;
6747c9e7a6fSAndy Grover 
6757c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(mb, sizeof(*mb));
6767c9e7a6fSAndy Grover 
6777c9e7a6fSAndy Grover 	cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
6787c9e7a6fSAndy Grover 
679f56574a2SAndy Grover 	/*
680f56574a2SAndy Grover 	 * If cmd end-of-ring space is too small then we need space for a NOP plus
681f56574a2SAndy Grover 	 * original cmd - cmds are internally contiguous.
682f56574a2SAndy Grover 	 */
683f56574a2SAndy Grover 	if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size)
684f56574a2SAndy Grover 		cmd_needed = cmd_size;
685f56574a2SAndy Grover 	else
686f56574a2SAndy Grover 		cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size);
687f56574a2SAndy Grover 
6887c9e7a6fSAndy Grover 	space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size);
6897c9e7a6fSAndy Grover 	if (space < cmd_needed) {
6907c9e7a6fSAndy Grover 		pr_debug("no cmd space: %u %u %u\n", cmd_head,
6917c9e7a6fSAndy Grover 		       udev->cmdr_last_cleaned, udev->cmdr_size);
6927c9e7a6fSAndy Grover 		return false;
6937c9e7a6fSAndy Grover 	}
6947c9e7a6fSAndy Grover 
695b6df4b79SXiubo Li 	/* try to check and get the data blocks as needed */
696b6df4b79SXiubo Li 	space = spc_bitmap_free(udev->data_bitmap, udev->dbi_thresh);
6977c9e7a6fSAndy Grover 	if (space < data_needed) {
698b6df4b79SXiubo Li 		unsigned long blocks_left = DATA_BLOCK_BITS - udev->dbi_thresh;
699b6df4b79SXiubo Li 		unsigned long grow;
700b6df4b79SXiubo Li 
701b6df4b79SXiubo Li 		if (blocks_left < blocks_needed) {
702b6df4b79SXiubo Li 			pr_debug("no data space: only %lu available, but ask for %zu\n",
703b6df4b79SXiubo Li 					blocks_left * DATA_BLOCK_SIZE,
704b6df4b79SXiubo Li 					data_needed);
7057c9e7a6fSAndy Grover 			return false;
7067c9e7a6fSAndy Grover 		}
7077c9e7a6fSAndy Grover 
708b6df4b79SXiubo Li 		/* Try to expand the thresh */
709b6df4b79SXiubo Li 		if (!udev->dbi_thresh) {
710b6df4b79SXiubo Li 			/* From idle state */
711b6df4b79SXiubo Li 			uint32_t init_thresh = DATA_BLOCK_INIT_BITS;
712b6df4b79SXiubo Li 
713b6df4b79SXiubo Li 			udev->dbi_thresh = max(blocks_needed, init_thresh);
714b6df4b79SXiubo Li 		} else {
715b6df4b79SXiubo Li 			/*
716b6df4b79SXiubo Li 			 * Grow the data area by max(blocks needed,
717b6df4b79SXiubo Li 			 * dbi_thresh / 2), but limited to the max
718b6df4b79SXiubo Li 			 * DATA_BLOCK_BITS size.
719b6df4b79SXiubo Li 			 */
720b6df4b79SXiubo Li 			grow = max(blocks_needed, udev->dbi_thresh / 2);
721b6df4b79SXiubo Li 			udev->dbi_thresh += grow;
722b6df4b79SXiubo Li 			if (udev->dbi_thresh > DATA_BLOCK_BITS)
723b6df4b79SXiubo Li 				udev->dbi_thresh = DATA_BLOCK_BITS;
724b6df4b79SXiubo Li 		}
725b6df4b79SXiubo Li 	}
726b6df4b79SXiubo Li 
727daf78c30SXiubo Li 	return tcmu_get_empty_blocks(udev, cmd);
7287c9e7a6fSAndy Grover }
7297c9e7a6fSAndy Grover 
730fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_base_cmd_size(size_t iov_cnt)
731fe25cc34SXiubo Li {
732fe25cc34SXiubo Li 	return max(offsetof(struct tcmu_cmd_entry, req.iov[iov_cnt]),
733fe25cc34SXiubo Li 			sizeof(struct tcmu_cmd_entry));
734fe25cc34SXiubo Li }
735fe25cc34SXiubo Li 
736fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_cmd_size(struct tcmu_cmd *tcmu_cmd,
737fe25cc34SXiubo Li 					   size_t base_command_size)
738fe25cc34SXiubo Li {
739fe25cc34SXiubo Li 	struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
740fe25cc34SXiubo Li 	size_t command_size;
741fe25cc34SXiubo Li 
742fe25cc34SXiubo Li 	command_size = base_command_size +
743fe25cc34SXiubo Li 		round_up(scsi_command_size(se_cmd->t_task_cdb),
744fe25cc34SXiubo Li 				TCMU_OP_ALIGN_SIZE);
745fe25cc34SXiubo Li 
746fe25cc34SXiubo Li 	WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1));
747fe25cc34SXiubo Li 
748fe25cc34SXiubo Li 	return command_size;
749fe25cc34SXiubo Li }
750fe25cc34SXiubo Li 
75102eb924fSAndy Grover static sense_reason_t
75202eb924fSAndy Grover tcmu_queue_cmd_ring(struct tcmu_cmd *tcmu_cmd)
7537c9e7a6fSAndy Grover {
7547c9e7a6fSAndy Grover 	struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
7557c9e7a6fSAndy Grover 	struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
7567c9e7a6fSAndy Grover 	size_t base_command_size, command_size;
7577c9e7a6fSAndy Grover 	struct tcmu_mailbox *mb;
7587c9e7a6fSAndy Grover 	struct tcmu_cmd_entry *entry;
7597c9e7a6fSAndy Grover 	struct iovec *iov;
760141685a3SXiubo Li 	int iov_cnt, ret;
7617c9e7a6fSAndy Grover 	uint32_t cmd_head;
7627c9e7a6fSAndy Grover 	uint64_t cdb_off;
763f97ec7dbSIlias Tsitsimpis 	bool copy_to_data_area;
764ab22d260SXiubo Li 	size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd);
7657c9e7a6fSAndy Grover 
7667c9e7a6fSAndy Grover 	if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags))
76702eb924fSAndy Grover 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
7687c9e7a6fSAndy Grover 
7697c9e7a6fSAndy Grover 	/*
7707c9e7a6fSAndy Grover 	 * Must be a certain minimum size for response sense info, but
7717c9e7a6fSAndy Grover 	 * also may be larger if the iov array is large.
7727c9e7a6fSAndy Grover 	 *
773fe25cc34SXiubo Li 	 * We prepare as many iovs as possbile for potential uses here,
774fe25cc34SXiubo Li 	 * because it's expensive to tell how many regions are freed in
775fe25cc34SXiubo Li 	 * the bitmap & global data pool, as the size calculated here
776fe25cc34SXiubo Li 	 * will only be used to do the checks.
777fe25cc34SXiubo Li 	 *
778fe25cc34SXiubo Li 	 * The size will be recalculated later as actually needed to save
779fe25cc34SXiubo Li 	 * cmd area memories.
7807c9e7a6fSAndy Grover 	 */
781fe25cc34SXiubo Li 	base_command_size = tcmu_cmd_get_base_cmd_size(tcmu_cmd->dbi_cnt);
782fe25cc34SXiubo Li 	command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size);
7837c9e7a6fSAndy Grover 
784b6df4b79SXiubo Li 	mutex_lock(&udev->cmdr_lock);
7857c9e7a6fSAndy Grover 
7867c9e7a6fSAndy Grover 	mb = udev->mb_addr;
7877c9e7a6fSAndy Grover 	cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
788554617b2SAndy Grover 	if ((command_size > (udev->cmdr_size / 2)) ||
789554617b2SAndy Grover 	    data_length > udev->data_size) {
790554617b2SAndy Grover 		pr_warn("TCMU: Request of size %zu/%zu is too big for %u/%zu "
7913d9b9555SAndy Grover 			"cmd ring/data area\n", command_size, data_length,
7927c9e7a6fSAndy Grover 			udev->cmdr_size, udev->data_size);
793b6df4b79SXiubo Li 		mutex_unlock(&udev->cmdr_lock);
794554617b2SAndy Grover 		return TCM_INVALID_CDB_FIELD;
795554617b2SAndy Grover 	}
7967c9e7a6fSAndy Grover 
797b6df4b79SXiubo Li 	while (!is_ring_space_avail(udev, tcmu_cmd, command_size, data_length)) {
7987c9e7a6fSAndy Grover 		int ret;
7997c9e7a6fSAndy Grover 		DEFINE_WAIT(__wait);
8007c9e7a6fSAndy Grover 
8017c9e7a6fSAndy Grover 		prepare_to_wait(&udev->wait_cmdr, &__wait, TASK_INTERRUPTIBLE);
8027c9e7a6fSAndy Grover 
8037c9e7a6fSAndy Grover 		pr_debug("sleeping for ring space\n");
804b6df4b79SXiubo Li 		mutex_unlock(&udev->cmdr_lock);
805af980e46SMike Christie 		if (udev->cmd_time_out)
806af980e46SMike Christie 			ret = schedule_timeout(
807af980e46SMike Christie 					msecs_to_jiffies(udev->cmd_time_out));
808af980e46SMike Christie 		else
8097c9e7a6fSAndy Grover 			ret = schedule_timeout(msecs_to_jiffies(TCMU_TIME_OUT));
8107c9e7a6fSAndy Grover 		finish_wait(&udev->wait_cmdr, &__wait);
8117c9e7a6fSAndy Grover 		if (!ret) {
8127c9e7a6fSAndy Grover 			pr_warn("tcmu: command timed out\n");
81302eb924fSAndy Grover 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
8147c9e7a6fSAndy Grover 		}
8157c9e7a6fSAndy Grover 
816b6df4b79SXiubo Li 		mutex_lock(&udev->cmdr_lock);
8177c9e7a6fSAndy Grover 
8187c9e7a6fSAndy Grover 		/* We dropped cmdr_lock, cmd_head is stale */
8197c9e7a6fSAndy Grover 		cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
8207c9e7a6fSAndy Grover 	}
8217c9e7a6fSAndy Grover 
822f56574a2SAndy Grover 	/* Insert a PAD if end-of-ring space is too small */
823f56574a2SAndy Grover 	if (head_to_end(cmd_head, udev->cmdr_size) < command_size) {
824f56574a2SAndy Grover 		size_t pad_size = head_to_end(cmd_head, udev->cmdr_size);
825f56574a2SAndy Grover 
8267c9e7a6fSAndy Grover 		entry = (void *) mb + CMDR_OFF + cmd_head;
8270ad46af8SAndy Grover 		tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_PAD);
8280ad46af8SAndy Grover 		tcmu_hdr_set_len(&entry->hdr.len_op, pad_size);
8290ad46af8SAndy Grover 		entry->hdr.cmd_id = 0; /* not used for PAD */
8300ad46af8SAndy Grover 		entry->hdr.kflags = 0;
8310ad46af8SAndy Grover 		entry->hdr.uflags = 0;
8329d62bc0eSXiubo Li 		tcmu_flush_dcache_range(entry, sizeof(*entry));
8337c9e7a6fSAndy Grover 
8347c9e7a6fSAndy Grover 		UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size);
8359d62bc0eSXiubo Li 		tcmu_flush_dcache_range(mb, sizeof(*mb));
8367c9e7a6fSAndy Grover 
8377c9e7a6fSAndy Grover 		cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
8387c9e7a6fSAndy Grover 		WARN_ON(cmd_head != 0);
8397c9e7a6fSAndy Grover 	}
8407c9e7a6fSAndy Grover 
8417c9e7a6fSAndy Grover 	entry = (void *) mb + CMDR_OFF + cmd_head;
842b3743c71SXiubo Li 	memset(entry, 0, command_size);
8430ad46af8SAndy Grover 	tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD);
8440ad46af8SAndy Grover 	entry->hdr.cmd_id = tcmu_cmd->cmd_id;
8457c9e7a6fSAndy Grover 
8463d9b9555SAndy Grover 	/* Handle allocating space from the data area */
847b6df4b79SXiubo Li 	tcmu_cmd_reset_dbi_cur(tcmu_cmd);
8487c9e7a6fSAndy Grover 	iov = &entry->req.iov[0];
849f97ec7dbSIlias Tsitsimpis 	iov_cnt = 0;
850e4648b01SIlias Tsitsimpis 	copy_to_data_area = (se_cmd->data_direction == DMA_TO_DEVICE
851e4648b01SIlias Tsitsimpis 		|| se_cmd->se_cmd_flags & SCF_BIDI);
852b6df4b79SXiubo Li 	ret = scatter_data_area(udev, tcmu_cmd, se_cmd->t_data_sg,
853b6df4b79SXiubo Li 				se_cmd->t_data_nents, &iov, &iov_cnt,
854b6df4b79SXiubo Li 				copy_to_data_area);
855141685a3SXiubo Li 	if (ret) {
856b6df4b79SXiubo Li 		tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt);
857b6df4b79SXiubo Li 		mutex_unlock(&udev->cmdr_lock);
858b6df4b79SXiubo Li 
859141685a3SXiubo Li 		pr_err("tcmu: alloc and scatter data failed\n");
860141685a3SXiubo Li 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
861141685a3SXiubo Li 	}
8627c9e7a6fSAndy Grover 	entry->req.iov_cnt = iov_cnt;
8637c9e7a6fSAndy Grover 
864e4648b01SIlias Tsitsimpis 	/* Handle BIDI commands */
865e4648b01SIlias Tsitsimpis 	iov_cnt = 0;
866b3743c71SXiubo Li 	if (se_cmd->se_cmd_flags & SCF_BIDI) {
867ab22d260SXiubo Li 		iov++;
868b6df4b79SXiubo Li 		ret = scatter_data_area(udev, tcmu_cmd,
869141685a3SXiubo Li 					se_cmd->t_bidi_data_sg,
870141685a3SXiubo Li 					se_cmd->t_bidi_data_nents,
871141685a3SXiubo Li 					&iov, &iov_cnt, false);
872141685a3SXiubo Li 		if (ret) {
873b6df4b79SXiubo Li 			tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt);
874b6df4b79SXiubo Li 			mutex_unlock(&udev->cmdr_lock);
875b6df4b79SXiubo Li 
876141685a3SXiubo Li 			pr_err("tcmu: alloc and scatter bidi data failed\n");
877141685a3SXiubo Li 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
878141685a3SXiubo Li 		}
879ab22d260SXiubo Li 	}
880b3743c71SXiubo Li 	entry->req.iov_bidi_cnt = iov_cnt;
88126418649SSheng Yang 
882fe25cc34SXiubo Li 	/*
883fe25cc34SXiubo Li 	 * Recalaulate the command's base size and size according
884fe25cc34SXiubo Li 	 * to the actual needs
885fe25cc34SXiubo Li 	 */
886fe25cc34SXiubo Li 	base_command_size = tcmu_cmd_get_base_cmd_size(entry->req.iov_cnt +
887fe25cc34SXiubo Li 						       entry->req.iov_bidi_cnt);
888fe25cc34SXiubo Li 	command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size);
889fe25cc34SXiubo Li 
890fe25cc34SXiubo Li 	tcmu_hdr_set_len(&entry->hdr.len_op, command_size);
891fe25cc34SXiubo Li 
8927c9e7a6fSAndy Grover 	/* All offsets relative to mb_addr, not start of entry! */
8937c9e7a6fSAndy Grover 	cdb_off = CMDR_OFF + cmd_head + base_command_size;
8947c9e7a6fSAndy Grover 	memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb));
8957c9e7a6fSAndy Grover 	entry->req.cdb_off = cdb_off;
8967c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(entry, sizeof(*entry));
8977c9e7a6fSAndy Grover 
8987c9e7a6fSAndy Grover 	UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size);
8997c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(mb, sizeof(*mb));
900b6df4b79SXiubo Li 	mutex_unlock(&udev->cmdr_lock);
9017c9e7a6fSAndy Grover 
9027c9e7a6fSAndy Grover 	/* TODO: only if FLUSH and FUA? */
9037c9e7a6fSAndy Grover 	uio_event_notify(&udev->uio_info);
9047c9e7a6fSAndy Grover 
905af980e46SMike Christie 	if (udev->cmd_time_out)
906af980e46SMike Christie 		mod_timer(&udev->timeout, round_jiffies_up(jiffies +
907af980e46SMike Christie 			  msecs_to_jiffies(udev->cmd_time_out)));
9087c9e7a6fSAndy Grover 
90902eb924fSAndy Grover 	return TCM_NO_SENSE;
9107c9e7a6fSAndy Grover }
9117c9e7a6fSAndy Grover 
91202eb924fSAndy Grover static sense_reason_t
91302eb924fSAndy Grover tcmu_queue_cmd(struct se_cmd *se_cmd)
9147c9e7a6fSAndy Grover {
9157c9e7a6fSAndy Grover 	struct se_device *se_dev = se_cmd->se_dev;
9167c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
9177c9e7a6fSAndy Grover 	struct tcmu_cmd *tcmu_cmd;
918ecaf597bSBart Van Assche 	sense_reason_t ret;
9197c9e7a6fSAndy Grover 
9207c9e7a6fSAndy Grover 	tcmu_cmd = tcmu_alloc_cmd(se_cmd);
9217c9e7a6fSAndy Grover 	if (!tcmu_cmd)
92202eb924fSAndy Grover 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
9237c9e7a6fSAndy Grover 
9247c9e7a6fSAndy Grover 	ret = tcmu_queue_cmd_ring(tcmu_cmd);
92502eb924fSAndy Grover 	if (ret != TCM_NO_SENSE) {
9267c9e7a6fSAndy Grover 		pr_err("TCMU: Could not queue command\n");
9277c9e7a6fSAndy Grover 		spin_lock_irq(&udev->commands_lock);
9287c9e7a6fSAndy Grover 		idr_remove(&udev->commands, tcmu_cmd->cmd_id);
9297c9e7a6fSAndy Grover 		spin_unlock_irq(&udev->commands_lock);
9307c9e7a6fSAndy Grover 
931141685a3SXiubo Li 		tcmu_free_cmd(tcmu_cmd);
9327c9e7a6fSAndy Grover 	}
9337c9e7a6fSAndy Grover 
9347c9e7a6fSAndy Grover 	return ret;
9357c9e7a6fSAndy Grover }
9367c9e7a6fSAndy Grover 
9377c9e7a6fSAndy Grover static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *entry)
9387c9e7a6fSAndy Grover {
9397c9e7a6fSAndy Grover 	struct se_cmd *se_cmd = cmd->se_cmd;
9407c9e7a6fSAndy Grover 	struct tcmu_dev *udev = cmd->tcmu_dev;
9417c9e7a6fSAndy Grover 
942b25c7863SSheng Yang 	/*
943b25c7863SSheng Yang 	 * cmd has been completed already from timeout, just reclaim
9443d9b9555SAndy Grover 	 * data area space and free cmd
945b25c7863SSheng Yang 	 */
946141685a3SXiubo Li 	if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
947141685a3SXiubo Li 		goto out;
948b25c7863SSheng Yang 
949141685a3SXiubo Li 	tcmu_cmd_reset_dbi_cur(cmd);
9507c9e7a6fSAndy Grover 
9510ad46af8SAndy Grover 	if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) {
9520ad46af8SAndy Grover 		pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n",
9530ad46af8SAndy Grover 			cmd->se_cmd);
954ed97d0cdSAndy Grover 		entry->rsp.scsi_status = SAM_STAT_CHECK_CONDITION;
955ed97d0cdSAndy Grover 	} else if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) {
956406f74c2SMike Christie 		transport_copy_sense_to_cmd(se_cmd, entry->rsp.sense_buffer);
957e4648b01SIlias Tsitsimpis 	} else if (se_cmd->se_cmd_flags & SCF_BIDI) {
95826418649SSheng Yang 		/* Get Data-In buffer before clean up */
959a5d68ba8SXiubo Li 		gather_data_area(udev, cmd, true);
960e4648b01SIlias Tsitsimpis 	} else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
961a5d68ba8SXiubo Li 		gather_data_area(udev, cmd, false);
9627c9e7a6fSAndy Grover 	} else if (se_cmd->data_direction == DMA_TO_DEVICE) {
963141685a3SXiubo Li 		/* TODO: */
9642bc396a2SIlias Tsitsimpis 	} else if (se_cmd->data_direction != DMA_NONE) {
9652bc396a2SIlias Tsitsimpis 		pr_warn("TCMU: data direction was %d!\n",
9662bc396a2SIlias Tsitsimpis 			se_cmd->data_direction);
9677c9e7a6fSAndy Grover 	}
9687c9e7a6fSAndy Grover 
9697c9e7a6fSAndy Grover 	target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status);
9707c9e7a6fSAndy Grover 
971141685a3SXiubo Li out:
972141685a3SXiubo Li 	cmd->se_cmd = NULL;
973b6df4b79SXiubo Li 	tcmu_cmd_free_data(cmd, cmd->dbi_cnt);
974141685a3SXiubo Li 	tcmu_free_cmd(cmd);
9757c9e7a6fSAndy Grover }
9767c9e7a6fSAndy Grover 
9777c9e7a6fSAndy Grover static unsigned int tcmu_handle_completions(struct tcmu_dev *udev)
9787c9e7a6fSAndy Grover {
9797c9e7a6fSAndy Grover 	struct tcmu_mailbox *mb;
9807c9e7a6fSAndy Grover 	int handled = 0;
9817c9e7a6fSAndy Grover 
9827c9e7a6fSAndy Grover 	if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) {
9837c9e7a6fSAndy Grover 		pr_err("ring broken, not handling completions\n");
9847c9e7a6fSAndy Grover 		return 0;
9857c9e7a6fSAndy Grover 	}
9867c9e7a6fSAndy Grover 
9877c9e7a6fSAndy Grover 	mb = udev->mb_addr;
9887c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(mb, sizeof(*mb));
9897c9e7a6fSAndy Grover 
9907c9e7a6fSAndy Grover 	while (udev->cmdr_last_cleaned != ACCESS_ONCE(mb->cmd_tail)) {
9917c9e7a6fSAndy Grover 
9927c9e7a6fSAndy Grover 		struct tcmu_cmd_entry *entry = (void *) mb + CMDR_OFF + udev->cmdr_last_cleaned;
9937c9e7a6fSAndy Grover 		struct tcmu_cmd *cmd;
9947c9e7a6fSAndy Grover 
9957c9e7a6fSAndy Grover 		tcmu_flush_dcache_range(entry, sizeof(*entry));
9967c9e7a6fSAndy Grover 
9970ad46af8SAndy Grover 		if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD) {
9980ad46af8SAndy Grover 			UPDATE_HEAD(udev->cmdr_last_cleaned,
9990ad46af8SAndy Grover 				    tcmu_hdr_get_len(entry->hdr.len_op),
10000ad46af8SAndy Grover 				    udev->cmdr_size);
10017c9e7a6fSAndy Grover 			continue;
10027c9e7a6fSAndy Grover 		}
10030ad46af8SAndy Grover 		WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD);
10047c9e7a6fSAndy Grover 
10057c9e7a6fSAndy Grover 		spin_lock(&udev->commands_lock);
1006d3e709e6SMatthew Wilcox 		cmd = idr_remove(&udev->commands, entry->hdr.cmd_id);
10077c9e7a6fSAndy Grover 		spin_unlock(&udev->commands_lock);
10087c9e7a6fSAndy Grover 
10097c9e7a6fSAndy Grover 		if (!cmd) {
10107c9e7a6fSAndy Grover 			pr_err("cmd_id not found, ring is broken\n");
10117c9e7a6fSAndy Grover 			set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags);
10127c9e7a6fSAndy Grover 			break;
10137c9e7a6fSAndy Grover 		}
10147c9e7a6fSAndy Grover 
10157c9e7a6fSAndy Grover 		tcmu_handle_completion(cmd, entry);
10167c9e7a6fSAndy Grover 
10170ad46af8SAndy Grover 		UPDATE_HEAD(udev->cmdr_last_cleaned,
10180ad46af8SAndy Grover 			    tcmu_hdr_get_len(entry->hdr.len_op),
10190ad46af8SAndy Grover 			    udev->cmdr_size);
10207c9e7a6fSAndy Grover 
10217c9e7a6fSAndy Grover 		handled++;
10227c9e7a6fSAndy Grover 	}
10237c9e7a6fSAndy Grover 
10247c9e7a6fSAndy Grover 	if (mb->cmd_tail == mb->cmd_head)
10257c9e7a6fSAndy Grover 		del_timer(&udev->timeout); /* no more pending cmds */
10267c9e7a6fSAndy Grover 
10277c9e7a6fSAndy Grover 	wake_up(&udev->wait_cmdr);
10287c9e7a6fSAndy Grover 
10297c9e7a6fSAndy Grover 	return handled;
10307c9e7a6fSAndy Grover }
10317c9e7a6fSAndy Grover 
10327c9e7a6fSAndy Grover static int tcmu_check_expired_cmd(int id, void *p, void *data)
10337c9e7a6fSAndy Grover {
10347c9e7a6fSAndy Grover 	struct tcmu_cmd *cmd = p;
10357c9e7a6fSAndy Grover 
10367c9e7a6fSAndy Grover 	if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
10377c9e7a6fSAndy Grover 		return 0;
10387c9e7a6fSAndy Grover 
1039611e2267SAndy Grover 	if (!time_after(jiffies, cmd->deadline))
10407c9e7a6fSAndy Grover 		return 0;
10417c9e7a6fSAndy Grover 
10427c9e7a6fSAndy Grover 	set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags);
10437c9e7a6fSAndy Grover 	target_complete_cmd(cmd->se_cmd, SAM_STAT_CHECK_CONDITION);
10447c9e7a6fSAndy Grover 	cmd->se_cmd = NULL;
10457c9e7a6fSAndy Grover 
10467c9e7a6fSAndy Grover 	return 0;
10477c9e7a6fSAndy Grover }
10487c9e7a6fSAndy Grover 
10497c9e7a6fSAndy Grover static void tcmu_device_timedout(unsigned long data)
10507c9e7a6fSAndy Grover {
10517c9e7a6fSAndy Grover 	struct tcmu_dev *udev = (struct tcmu_dev *)data;
10527c9e7a6fSAndy Grover 	unsigned long flags;
10537c9e7a6fSAndy Grover 
10547c9e7a6fSAndy Grover 	spin_lock_irqsave(&udev->commands_lock, flags);
10557c9e7a6fSAndy Grover 	idr_for_each(&udev->commands, tcmu_check_expired_cmd, NULL);
10567c9e7a6fSAndy Grover 	spin_unlock_irqrestore(&udev->commands_lock, flags);
10577c9e7a6fSAndy Grover 
1058b6df4b79SXiubo Li 	/* Try to wake up the ummap thread */
1059b6df4b79SXiubo Li 	wake_up(&unmap_wait);
1060b6df4b79SXiubo Li 
10617c9e7a6fSAndy Grover 	/*
10627c9e7a6fSAndy Grover 	 * We don't need to wakeup threads on wait_cmdr since they have their
10637c9e7a6fSAndy Grover 	 * own timeout.
10647c9e7a6fSAndy Grover 	 */
10657c9e7a6fSAndy Grover }
10667c9e7a6fSAndy Grover 
10677c9e7a6fSAndy Grover static int tcmu_attach_hba(struct se_hba *hba, u32 host_id)
10687c9e7a6fSAndy Grover {
10697c9e7a6fSAndy Grover 	struct tcmu_hba *tcmu_hba;
10707c9e7a6fSAndy Grover 
10717c9e7a6fSAndy Grover 	tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL);
10727c9e7a6fSAndy Grover 	if (!tcmu_hba)
10737c9e7a6fSAndy Grover 		return -ENOMEM;
10747c9e7a6fSAndy Grover 
10757c9e7a6fSAndy Grover 	tcmu_hba->host_id = host_id;
10767c9e7a6fSAndy Grover 	hba->hba_ptr = tcmu_hba;
10777c9e7a6fSAndy Grover 
10787c9e7a6fSAndy Grover 	return 0;
10797c9e7a6fSAndy Grover }
10807c9e7a6fSAndy Grover 
10817c9e7a6fSAndy Grover static void tcmu_detach_hba(struct se_hba *hba)
10827c9e7a6fSAndy Grover {
10837c9e7a6fSAndy Grover 	kfree(hba->hba_ptr);
10847c9e7a6fSAndy Grover 	hba->hba_ptr = NULL;
10857c9e7a6fSAndy Grover }
10867c9e7a6fSAndy Grover 
10877c9e7a6fSAndy Grover static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
10887c9e7a6fSAndy Grover {
10897c9e7a6fSAndy Grover 	struct tcmu_dev *udev;
10907c9e7a6fSAndy Grover 
10917c9e7a6fSAndy Grover 	udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL);
10927c9e7a6fSAndy Grover 	if (!udev)
10937c9e7a6fSAndy Grover 		return NULL;
1094f3cdbe39SMike Christie 	kref_init(&udev->kref);
10957c9e7a6fSAndy Grover 
10967c9e7a6fSAndy Grover 	udev->name = kstrdup(name, GFP_KERNEL);
10977c9e7a6fSAndy Grover 	if (!udev->name) {
10987c9e7a6fSAndy Grover 		kfree(udev);
10997c9e7a6fSAndy Grover 		return NULL;
11007c9e7a6fSAndy Grover 	}
11017c9e7a6fSAndy Grover 
11027c9e7a6fSAndy Grover 	udev->hba = hba;
1103af980e46SMike Christie 	udev->cmd_time_out = TCMU_TIME_OUT;
11047c9e7a6fSAndy Grover 
11057c9e7a6fSAndy Grover 	init_waitqueue_head(&udev->wait_cmdr);
1106b6df4b79SXiubo Li 	mutex_init(&udev->cmdr_lock);
11077c9e7a6fSAndy Grover 
11087c9e7a6fSAndy Grover 	idr_init(&udev->commands);
11097c9e7a6fSAndy Grover 	spin_lock_init(&udev->commands_lock);
11107c9e7a6fSAndy Grover 
11117c9e7a6fSAndy Grover 	setup_timer(&udev->timeout, tcmu_device_timedout,
11127c9e7a6fSAndy Grover 		(unsigned long)udev);
11137c9e7a6fSAndy Grover 
1114b3af66e2SMike Christie 	init_waitqueue_head(&udev->nl_cmd_wq);
1115b3af66e2SMike Christie 	spin_lock_init(&udev->nl_cmd_lock);
1116b3af66e2SMike Christie 
1117c22adc0bSXiubo Li 	INIT_RADIX_TREE(&udev->data_blocks, GFP_KERNEL);
1118c22adc0bSXiubo Li 
11197c9e7a6fSAndy Grover 	return &udev->se_dev;
11207c9e7a6fSAndy Grover }
11217c9e7a6fSAndy Grover 
11227c9e7a6fSAndy Grover static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on)
11237c9e7a6fSAndy Grover {
11247c9e7a6fSAndy Grover 	struct tcmu_dev *tcmu_dev = container_of(info, struct tcmu_dev, uio_info);
11257c9e7a6fSAndy Grover 
1126b6df4b79SXiubo Li 	mutex_lock(&tcmu_dev->cmdr_lock);
11277c9e7a6fSAndy Grover 	tcmu_handle_completions(tcmu_dev);
1128b6df4b79SXiubo Li 	mutex_unlock(&tcmu_dev->cmdr_lock);
11297c9e7a6fSAndy Grover 
11307c9e7a6fSAndy Grover 	return 0;
11317c9e7a6fSAndy Grover }
11327c9e7a6fSAndy Grover 
11337c9e7a6fSAndy Grover /*
11347c9e7a6fSAndy Grover  * mmap code from uio.c. Copied here because we want to hook mmap()
11357c9e7a6fSAndy Grover  * and this stuff must come along.
11367c9e7a6fSAndy Grover  */
11377c9e7a6fSAndy Grover static int tcmu_find_mem_index(struct vm_area_struct *vma)
11387c9e7a6fSAndy Grover {
11397c9e7a6fSAndy Grover 	struct tcmu_dev *udev = vma->vm_private_data;
11407c9e7a6fSAndy Grover 	struct uio_info *info = &udev->uio_info;
11417c9e7a6fSAndy Grover 
11427c9e7a6fSAndy Grover 	if (vma->vm_pgoff < MAX_UIO_MAPS) {
11437c9e7a6fSAndy Grover 		if (info->mem[vma->vm_pgoff].size == 0)
11447c9e7a6fSAndy Grover 			return -1;
11457c9e7a6fSAndy Grover 		return (int)vma->vm_pgoff;
11467c9e7a6fSAndy Grover 	}
11477c9e7a6fSAndy Grover 	return -1;
11487c9e7a6fSAndy Grover }
11497c9e7a6fSAndy Grover 
1150b6df4b79SXiubo Li static struct page *tcmu_try_get_block_page(struct tcmu_dev *udev, uint32_t dbi)
1151b6df4b79SXiubo Li {
1152b6df4b79SXiubo Li 	struct page *page;
1153b6df4b79SXiubo Li 	int ret;
1154b6df4b79SXiubo Li 
1155b6df4b79SXiubo Li 	mutex_lock(&udev->cmdr_lock);
1156b6df4b79SXiubo Li 	page = tcmu_get_block_page(udev, dbi);
1157b6df4b79SXiubo Li 	if (likely(page)) {
1158b6df4b79SXiubo Li 		mutex_unlock(&udev->cmdr_lock);
1159b6df4b79SXiubo Li 		return page;
1160b6df4b79SXiubo Li 	}
1161b6df4b79SXiubo Li 
1162b6df4b79SXiubo Li 	/*
1163b6df4b79SXiubo Li 	 * Normally it shouldn't be here:
1164b6df4b79SXiubo Li 	 * Only when the userspace has touched the blocks which
1165b6df4b79SXiubo Li 	 * are out of the tcmu_cmd's data iov[], and will return
1166b6df4b79SXiubo Li 	 * one zeroed page.
1167b6df4b79SXiubo Li 	 */
1168b6df4b79SXiubo Li 	pr_warn("Block(%u) out of cmd's iov[] has been touched!\n", dbi);
1169b6df4b79SXiubo Li 	pr_warn("Mostly it will be a bug of userspace, please have a check!\n");
1170b6df4b79SXiubo Li 
1171b6df4b79SXiubo Li 	if (dbi >= udev->dbi_thresh) {
1172b6df4b79SXiubo Li 		/* Extern the udev->dbi_thresh to dbi + 1 */
1173b6df4b79SXiubo Li 		udev->dbi_thresh = dbi + 1;
1174b6df4b79SXiubo Li 		udev->dbi_max = dbi;
1175b6df4b79SXiubo Li 	}
1176b6df4b79SXiubo Li 
1177b6df4b79SXiubo Li 	page = radix_tree_lookup(&udev->data_blocks, dbi);
1178b6df4b79SXiubo Li 	if (!page) {
1179b6df4b79SXiubo Li 		page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1180b6df4b79SXiubo Li 		if (!page) {
1181b6df4b79SXiubo Li 			mutex_unlock(&udev->cmdr_lock);
1182b6df4b79SXiubo Li 			return NULL;
1183b6df4b79SXiubo Li 		}
1184b6df4b79SXiubo Li 
1185b6df4b79SXiubo Li 		ret = radix_tree_insert(&udev->data_blocks, dbi, page);
1186b6df4b79SXiubo Li 		if (ret) {
1187b6df4b79SXiubo Li 			mutex_unlock(&udev->cmdr_lock);
1188b6df4b79SXiubo Li 			__free_page(page);
1189b6df4b79SXiubo Li 			return NULL;
1190b6df4b79SXiubo Li 		}
1191b6df4b79SXiubo Li 
1192b6df4b79SXiubo Li 		/*
1193b6df4b79SXiubo Li 		 * Since this case is rare in page fault routine, here we
1194b6df4b79SXiubo Li 		 * will allow the global_db_count >= TCMU_GLOBAL_MAX_BLOCKS
1195b6df4b79SXiubo Li 		 * to reduce possible page fault call trace.
1196b6df4b79SXiubo Li 		 */
1197b6df4b79SXiubo Li 		atomic_inc(&global_db_count);
1198b6df4b79SXiubo Li 	}
1199b6df4b79SXiubo Li 	mutex_unlock(&udev->cmdr_lock);
1200b6df4b79SXiubo Li 
1201b6df4b79SXiubo Li 	return page;
1202b6df4b79SXiubo Li }
1203b6df4b79SXiubo Li 
120411bac800SDave Jiang static int tcmu_vma_fault(struct vm_fault *vmf)
12057c9e7a6fSAndy Grover {
120611bac800SDave Jiang 	struct tcmu_dev *udev = vmf->vma->vm_private_data;
12077c9e7a6fSAndy Grover 	struct uio_info *info = &udev->uio_info;
12087c9e7a6fSAndy Grover 	struct page *page;
12097c9e7a6fSAndy Grover 	unsigned long offset;
12107c9e7a6fSAndy Grover 	void *addr;
12117c9e7a6fSAndy Grover 
121211bac800SDave Jiang 	int mi = tcmu_find_mem_index(vmf->vma);
12137c9e7a6fSAndy Grover 	if (mi < 0)
12147c9e7a6fSAndy Grover 		return VM_FAULT_SIGBUS;
12157c9e7a6fSAndy Grover 
12167c9e7a6fSAndy Grover 	/*
12177c9e7a6fSAndy Grover 	 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
12187c9e7a6fSAndy Grover 	 * to use mem[N].
12197c9e7a6fSAndy Grover 	 */
12207c9e7a6fSAndy Grover 	offset = (vmf->pgoff - mi) << PAGE_SHIFT;
12217c9e7a6fSAndy Grover 
1222141685a3SXiubo Li 	if (offset < udev->data_off) {
1223141685a3SXiubo Li 		/* For the vmalloc()ed cmd area pages */
12247c9e7a6fSAndy Grover 		addr = (void *)(unsigned long)info->mem[mi].addr + offset;
12257c9e7a6fSAndy Grover 		page = vmalloc_to_page(addr);
1226141685a3SXiubo Li 	} else {
1227141685a3SXiubo Li 		uint32_t dbi;
1228141685a3SXiubo Li 
1229b6df4b79SXiubo Li 		/* For the dynamically growing data area pages */
1230141685a3SXiubo Li 		dbi = (offset - udev->data_off) / DATA_BLOCK_SIZE;
1231b6df4b79SXiubo Li 		page = tcmu_try_get_block_page(udev, dbi);
1232b6df4b79SXiubo Li 		if (!page)
1233141685a3SXiubo Li 			return VM_FAULT_NOPAGE;
1234141685a3SXiubo Li 	}
1235141685a3SXiubo Li 
12367c9e7a6fSAndy Grover 	get_page(page);
12377c9e7a6fSAndy Grover 	vmf->page = page;
12387c9e7a6fSAndy Grover 	return 0;
12397c9e7a6fSAndy Grover }
12407c9e7a6fSAndy Grover 
12417c9e7a6fSAndy Grover static const struct vm_operations_struct tcmu_vm_ops = {
12427c9e7a6fSAndy Grover 	.fault = tcmu_vma_fault,
12437c9e7a6fSAndy Grover };
12447c9e7a6fSAndy Grover 
12457c9e7a6fSAndy Grover static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma)
12467c9e7a6fSAndy Grover {
12477c9e7a6fSAndy Grover 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
12487c9e7a6fSAndy Grover 
12497c9e7a6fSAndy Grover 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
12507c9e7a6fSAndy Grover 	vma->vm_ops = &tcmu_vm_ops;
12517c9e7a6fSAndy Grover 
12527c9e7a6fSAndy Grover 	vma->vm_private_data = udev;
12537c9e7a6fSAndy Grover 
12547c9e7a6fSAndy Grover 	/* Ensure the mmap is exactly the right size */
12557c9e7a6fSAndy Grover 	if (vma_pages(vma) != (TCMU_RING_SIZE >> PAGE_SHIFT))
12567c9e7a6fSAndy Grover 		return -EINVAL;
12577c9e7a6fSAndy Grover 
12587c9e7a6fSAndy Grover 	return 0;
12597c9e7a6fSAndy Grover }
12607c9e7a6fSAndy Grover 
12617c9e7a6fSAndy Grover static int tcmu_open(struct uio_info *info, struct inode *inode)
12627c9e7a6fSAndy Grover {
12637c9e7a6fSAndy Grover 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
12647c9e7a6fSAndy Grover 
12657c9e7a6fSAndy Grover 	/* O_EXCL not supported for char devs, so fake it? */
12667c9e7a6fSAndy Grover 	if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags))
12677c9e7a6fSAndy Grover 		return -EBUSY;
12687c9e7a6fSAndy Grover 
1269b6df4b79SXiubo Li 	udev->inode = inode;
12709260695dSMike Christie 	kref_get(&udev->kref);
1271b6df4b79SXiubo Li 
12727c9e7a6fSAndy Grover 	pr_debug("open\n");
12737c9e7a6fSAndy Grover 
12747c9e7a6fSAndy Grover 	return 0;
12757c9e7a6fSAndy Grover }
12767c9e7a6fSAndy Grover 
1277f3cdbe39SMike Christie static void tcmu_dev_call_rcu(struct rcu_head *p)
1278f3cdbe39SMike Christie {
1279f3cdbe39SMike Christie 	struct se_device *dev = container_of(p, struct se_device, rcu_head);
1280f3cdbe39SMike Christie 	struct tcmu_dev *udev = TCMU_DEV(dev);
1281f3cdbe39SMike Christie 
1282f3cdbe39SMike Christie 	kfree(udev->uio_info.name);
1283f3cdbe39SMike Christie 	kfree(udev->name);
1284f3cdbe39SMike Christie 	kfree(udev);
1285f3cdbe39SMike Christie }
1286f3cdbe39SMike Christie 
1287c22adc0bSXiubo Li static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd)
1288c22adc0bSXiubo Li {
1289c22adc0bSXiubo Li 	if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
1290c22adc0bSXiubo Li 		kmem_cache_free(tcmu_cmd_cache, cmd);
1291c22adc0bSXiubo Li 		return 0;
1292c22adc0bSXiubo Li 	}
1293c22adc0bSXiubo Li 	return -EINVAL;
1294c22adc0bSXiubo Li }
1295c22adc0bSXiubo Li 
1296c22adc0bSXiubo Li static void tcmu_blocks_release(struct tcmu_dev *udev)
1297c22adc0bSXiubo Li {
1298c22adc0bSXiubo Li 	int i;
1299c22adc0bSXiubo Li 	struct page *page;
1300c22adc0bSXiubo Li 
1301c22adc0bSXiubo Li 	/* Try to release all block pages */
1302c22adc0bSXiubo Li 	mutex_lock(&udev->cmdr_lock);
1303c22adc0bSXiubo Li 	for (i = 0; i <= udev->dbi_max; i++) {
1304c22adc0bSXiubo Li 		page = radix_tree_delete(&udev->data_blocks, i);
1305c22adc0bSXiubo Li 		if (page) {
1306c22adc0bSXiubo Li 			__free_page(page);
1307c22adc0bSXiubo Li 			atomic_dec(&global_db_count);
1308c22adc0bSXiubo Li 		}
1309c22adc0bSXiubo Li 	}
1310c22adc0bSXiubo Li 	mutex_unlock(&udev->cmdr_lock);
1311c22adc0bSXiubo Li }
1312c22adc0bSXiubo Li 
1313f3cdbe39SMike Christie static void tcmu_dev_kref_release(struct kref *kref)
1314f3cdbe39SMike Christie {
1315f3cdbe39SMike Christie 	struct tcmu_dev *udev = container_of(kref, struct tcmu_dev, kref);
1316f3cdbe39SMike Christie 	struct se_device *dev = &udev->se_dev;
1317c22adc0bSXiubo Li 	struct tcmu_cmd *cmd;
1318c22adc0bSXiubo Li 	bool all_expired = true;
1319c22adc0bSXiubo Li 	int i;
1320c22adc0bSXiubo Li 
1321c22adc0bSXiubo Li 	vfree(udev->mb_addr);
1322c22adc0bSXiubo Li 	udev->mb_addr = NULL;
1323c22adc0bSXiubo Li 
1324c22adc0bSXiubo Li 	/* Upper layer should drain all requests before calling this */
1325c22adc0bSXiubo Li 	spin_lock_irq(&udev->commands_lock);
1326c22adc0bSXiubo Li 	idr_for_each_entry(&udev->commands, cmd, i) {
1327c22adc0bSXiubo Li 		if (tcmu_check_and_free_pending_cmd(cmd) != 0)
1328c22adc0bSXiubo Li 			all_expired = false;
1329c22adc0bSXiubo Li 	}
1330c22adc0bSXiubo Li 	idr_destroy(&udev->commands);
1331c22adc0bSXiubo Li 	spin_unlock_irq(&udev->commands_lock);
1332c22adc0bSXiubo Li 	WARN_ON(!all_expired);
1333c22adc0bSXiubo Li 
1334c22adc0bSXiubo Li 	tcmu_blocks_release(udev);
1335f3cdbe39SMike Christie 
1336f3cdbe39SMike Christie 	call_rcu(&dev->rcu_head, tcmu_dev_call_rcu);
1337f3cdbe39SMike Christie }
1338f3cdbe39SMike Christie 
13397c9e7a6fSAndy Grover static int tcmu_release(struct uio_info *info, struct inode *inode)
13407c9e7a6fSAndy Grover {
13417c9e7a6fSAndy Grover 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
13427c9e7a6fSAndy Grover 
13437c9e7a6fSAndy Grover 	clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags);
13447c9e7a6fSAndy Grover 
13457c9e7a6fSAndy Grover 	pr_debug("close\n");
13469260695dSMike Christie 	/* release ref from open */
1347f3cdbe39SMike Christie 	kref_put(&udev->kref, tcmu_dev_kref_release);
13487c9e7a6fSAndy Grover 	return 0;
13497c9e7a6fSAndy Grover }
13507c9e7a6fSAndy Grover 
1351b3af66e2SMike Christie static void tcmu_init_genl_cmd_reply(struct tcmu_dev *udev, int cmd)
1352b3af66e2SMike Christie {
1353b3af66e2SMike Christie 	struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd;
1354b3af66e2SMike Christie 
1355b3af66e2SMike Christie 	if (!tcmu_kern_cmd_reply_supported)
1356b3af66e2SMike Christie 		return;
1357b849b456SKenjiro Nakayama 
1358b849b456SKenjiro Nakayama 	if (udev->nl_reply_supported <= 0)
1359b849b456SKenjiro Nakayama 		return;
1360b849b456SKenjiro Nakayama 
1361b3af66e2SMike Christie relock:
1362b3af66e2SMike Christie 	spin_lock(&udev->nl_cmd_lock);
1363b3af66e2SMike Christie 
1364b3af66e2SMike Christie 	if (nl_cmd->cmd != TCMU_CMD_UNSPEC) {
1365b3af66e2SMike Christie 		spin_unlock(&udev->nl_cmd_lock);
1366b3af66e2SMike Christie 		pr_debug("sleeping for open nl cmd\n");
1367b3af66e2SMike Christie 		wait_event(udev->nl_cmd_wq, (nl_cmd->cmd == TCMU_CMD_UNSPEC));
1368b3af66e2SMike Christie 		goto relock;
1369b3af66e2SMike Christie 	}
1370b3af66e2SMike Christie 
1371b3af66e2SMike Christie 	memset(nl_cmd, 0, sizeof(*nl_cmd));
1372b3af66e2SMike Christie 	nl_cmd->cmd = cmd;
1373b3af66e2SMike Christie 	init_completion(&nl_cmd->complete);
1374b3af66e2SMike Christie 
1375b3af66e2SMike Christie 	spin_unlock(&udev->nl_cmd_lock);
1376b3af66e2SMike Christie }
1377b3af66e2SMike Christie 
1378b3af66e2SMike Christie static int tcmu_wait_genl_cmd_reply(struct tcmu_dev *udev)
1379b3af66e2SMike Christie {
1380b3af66e2SMike Christie 	struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd;
1381b3af66e2SMike Christie 	int ret;
1382b3af66e2SMike Christie 	DEFINE_WAIT(__wait);
1383b3af66e2SMike Christie 
1384b3af66e2SMike Christie 	if (!tcmu_kern_cmd_reply_supported)
1385b3af66e2SMike Christie 		return 0;
1386b3af66e2SMike Christie 
1387b849b456SKenjiro Nakayama 	if (udev->nl_reply_supported <= 0)
1388b849b456SKenjiro Nakayama 		return 0;
1389b849b456SKenjiro Nakayama 
1390b3af66e2SMike Christie 	pr_debug("sleeping for nl reply\n");
1391b3af66e2SMike Christie 	wait_for_completion(&nl_cmd->complete);
1392b3af66e2SMike Christie 
1393b3af66e2SMike Christie 	spin_lock(&udev->nl_cmd_lock);
1394b3af66e2SMike Christie 	nl_cmd->cmd = TCMU_CMD_UNSPEC;
1395b3af66e2SMike Christie 	ret = nl_cmd->status;
1396b3af66e2SMike Christie 	nl_cmd->status = 0;
1397b3af66e2SMike Christie 	spin_unlock(&udev->nl_cmd_lock);
1398b3af66e2SMike Christie 
1399b3af66e2SMike Christie 	wake_up_all(&udev->nl_cmd_wq);
1400b3af66e2SMike Christie 
1401b3af66e2SMike Christie 	return ret;;
1402b3af66e2SMike Christie }
1403b3af66e2SMike Christie 
1404b3af66e2SMike Christie static int tcmu_netlink_event(struct tcmu_dev *udev, enum tcmu_genl_cmd cmd,
1405b3af66e2SMike Christie 			      int reconfig_attr, const void *reconfig_data)
14067c9e7a6fSAndy Grover {
14077c9e7a6fSAndy Grover 	struct sk_buff *skb;
14087c9e7a6fSAndy Grover 	void *msg_header;
14096e14eab9SNicholas Bellinger 	int ret = -ENOMEM;
14107c9e7a6fSAndy Grover 
14117c9e7a6fSAndy Grover 	skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
14127c9e7a6fSAndy Grover 	if (!skb)
14136e14eab9SNicholas Bellinger 		return ret;
14147c9e7a6fSAndy Grover 
14157c9e7a6fSAndy Grover 	msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd);
14166e14eab9SNicholas Bellinger 	if (!msg_header)
14176e14eab9SNicholas Bellinger 		goto free_skb;
14187c9e7a6fSAndy Grover 
1419b3af66e2SMike Christie 	ret = nla_put_string(skb, TCMU_ATTR_DEVICE, udev->uio_info.name);
14206e14eab9SNicholas Bellinger 	if (ret < 0)
14216e14eab9SNicholas Bellinger 		goto free_skb;
14227c9e7a6fSAndy Grover 
1423b3af66e2SMike Christie 	ret = nla_put_u32(skb, TCMU_ATTR_MINOR, udev->uio_info.uio_dev->minor);
1424b3af66e2SMike Christie 	if (ret < 0)
1425b3af66e2SMike Christie 		goto free_skb;
1426b3af66e2SMike Christie 
1427b3af66e2SMike Christie 	ret = nla_put_u32(skb, TCMU_ATTR_DEVICE_ID, udev->se_dev.dev_index);
14286e14eab9SNicholas Bellinger 	if (ret < 0)
14296e14eab9SNicholas Bellinger 		goto free_skb;
14307c9e7a6fSAndy Grover 
14312d76443eSMike Christie 	if (cmd == TCMU_CMD_RECONFIG_DEVICE) {
14322d76443eSMike Christie 		switch (reconfig_attr) {
14332d76443eSMike Christie 		case TCMU_ATTR_DEV_CFG:
14342d76443eSMike Christie 			ret = nla_put_string(skb, reconfig_attr, reconfig_data);
14352d76443eSMike Christie 			break;
14362d76443eSMike Christie 		case TCMU_ATTR_DEV_SIZE:
14372d76443eSMike Christie 			ret = nla_put_u64_64bit(skb, reconfig_attr,
14382d76443eSMike Christie 						*((u64 *)reconfig_data),
14392d76443eSMike Christie 						TCMU_ATTR_PAD);
14402d76443eSMike Christie 			break;
14412d76443eSMike Christie 		case TCMU_ATTR_WRITECACHE:
14422d76443eSMike Christie 			ret = nla_put_u8(skb, reconfig_attr,
14432d76443eSMike Christie 					  *((u8 *)reconfig_data));
14442d76443eSMike Christie 			break;
14452d76443eSMike Christie 		default:
14462d76443eSMike Christie 			BUG();
14472d76443eSMike Christie 		}
14482d76443eSMike Christie 
14498a45885cSBryant G. Ly 		if (ret < 0)
14508a45885cSBryant G. Ly 			goto free_skb;
14512d76443eSMike Christie 	}
14528a45885cSBryant G. Ly 
1453053c095aSJohannes Berg 	genlmsg_end(skb, msg_header);
14547c9e7a6fSAndy Grover 
1455b3af66e2SMike Christie 	tcmu_init_genl_cmd_reply(udev, cmd);
1456b3af66e2SMike Christie 
145720c08b36SSheng Yang 	ret = genlmsg_multicast_allns(&tcmu_genl_family, skb, 0,
14587c9e7a6fSAndy Grover 				TCMU_MCGRP_CONFIG, GFP_KERNEL);
14597c9e7a6fSAndy Grover 	/* We don't care if no one is listening */
14607c9e7a6fSAndy Grover 	if (ret == -ESRCH)
14617c9e7a6fSAndy Grover 		ret = 0;
1462b3af66e2SMike Christie 	if (!ret)
1463b3af66e2SMike Christie 		ret = tcmu_wait_genl_cmd_reply(udev);
14647c9e7a6fSAndy Grover 
14657c9e7a6fSAndy Grover 	return ret;
14666e14eab9SNicholas Bellinger free_skb:
14676e14eab9SNicholas Bellinger 	nlmsg_free(skb);
14686e14eab9SNicholas Bellinger 	return ret;
14697c9e7a6fSAndy Grover }
14707c9e7a6fSAndy Grover 
1471de8c5221SBryant G. Ly static int tcmu_update_uio_info(struct tcmu_dev *udev)
14727c9e7a6fSAndy Grover {
14737c9e7a6fSAndy Grover 	struct tcmu_hba *hba = udev->hba->hba_ptr;
14747c9e7a6fSAndy Grover 	struct uio_info *info;
1475de8c5221SBryant G. Ly 	size_t size, used;
14767c9e7a6fSAndy Grover 	char *str;
14777c9e7a6fSAndy Grover 
14787c9e7a6fSAndy Grover 	info = &udev->uio_info;
14797c9e7a6fSAndy Grover 	size = snprintf(NULL, 0, "tcm-user/%u/%s/%s", hba->host_id, udev->name,
14807c9e7a6fSAndy Grover 			udev->dev_config);
14817c9e7a6fSAndy Grover 	size += 1; /* for \0 */
14827c9e7a6fSAndy Grover 	str = kmalloc(size, GFP_KERNEL);
14837c9e7a6fSAndy Grover 	if (!str)
14847c9e7a6fSAndy Grover 		return -ENOMEM;
14857c9e7a6fSAndy Grover 
14867c9e7a6fSAndy Grover 	used = snprintf(str, size, "tcm-user/%u/%s", hba->host_id, udev->name);
14877c9e7a6fSAndy Grover 	if (udev->dev_config[0])
14887c9e7a6fSAndy Grover 		snprintf(str + used, size - used, "/%s", udev->dev_config);
14897c9e7a6fSAndy Grover 
1490ededd039SBryant G. Ly 	/* If the old string exists, free it */
1491ededd039SBryant G. Ly 	kfree(info->name);
14927c9e7a6fSAndy Grover 	info->name = str;
14937c9e7a6fSAndy Grover 
1494de8c5221SBryant G. Ly 	return 0;
1495de8c5221SBryant G. Ly }
1496de8c5221SBryant G. Ly 
1497de8c5221SBryant G. Ly static int tcmu_configure_device(struct se_device *dev)
1498de8c5221SBryant G. Ly {
1499de8c5221SBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(dev);
1500de8c5221SBryant G. Ly 	struct uio_info *info;
1501de8c5221SBryant G. Ly 	struct tcmu_mailbox *mb;
1502de8c5221SBryant G. Ly 	int ret = 0;
1503de8c5221SBryant G. Ly 
1504de8c5221SBryant G. Ly 	ret = tcmu_update_uio_info(udev);
1505de8c5221SBryant G. Ly 	if (ret)
1506de8c5221SBryant G. Ly 		return ret;
1507de8c5221SBryant G. Ly 
1508de8c5221SBryant G. Ly 	info = &udev->uio_info;
1509de8c5221SBryant G. Ly 
1510141685a3SXiubo Li 	udev->mb_addr = vzalloc(CMDR_SIZE);
15117c9e7a6fSAndy Grover 	if (!udev->mb_addr) {
15127c9e7a6fSAndy Grover 		ret = -ENOMEM;
15137c9e7a6fSAndy Grover 		goto err_vzalloc;
15147c9e7a6fSAndy Grover 	}
15157c9e7a6fSAndy Grover 
15167c9e7a6fSAndy Grover 	/* mailbox fits in first part of CMDR space */
15177c9e7a6fSAndy Grover 	udev->cmdr_size = CMDR_SIZE - CMDR_OFF;
15187c9e7a6fSAndy Grover 	udev->data_off = CMDR_SIZE;
1519141685a3SXiubo Li 	udev->data_size = DATA_SIZE;
1520b6df4b79SXiubo Li 	udev->dbi_thresh = 0; /* Default in Idle state */
1521b6df4b79SXiubo Li 	udev->waiting_global = false;
15227c9e7a6fSAndy Grover 
1523141685a3SXiubo Li 	/* Initialise the mailbox of the ring buffer */
15247c9e7a6fSAndy Grover 	mb = udev->mb_addr;
15250ad46af8SAndy Grover 	mb->version = TCMU_MAILBOX_VERSION;
152632c76de3SSheng Yang 	mb->flags = TCMU_MAILBOX_FLAG_CAP_OOOC;
15277c9e7a6fSAndy Grover 	mb->cmdr_off = CMDR_OFF;
15287c9e7a6fSAndy Grover 	mb->cmdr_size = udev->cmdr_size;
15297c9e7a6fSAndy Grover 
15307c9e7a6fSAndy Grover 	WARN_ON(!PAGE_ALIGNED(udev->data_off));
15317c9e7a6fSAndy Grover 	WARN_ON(udev->data_size % PAGE_SIZE);
153226418649SSheng Yang 	WARN_ON(udev->data_size % DATA_BLOCK_SIZE);
15337c9e7a6fSAndy Grover 
1534ac64a2ceSDavid Disseldorp 	info->version = __stringify(TCMU_MAILBOX_VERSION);
15357c9e7a6fSAndy Grover 
15367c9e7a6fSAndy Grover 	info->mem[0].name = "tcm-user command & data buffer";
15370633e123SArnd Bergmann 	info->mem[0].addr = (phys_addr_t)(uintptr_t)udev->mb_addr;
15387c9e7a6fSAndy Grover 	info->mem[0].size = TCMU_RING_SIZE;
1539141685a3SXiubo Li 	info->mem[0].memtype = UIO_MEM_NONE;
15407c9e7a6fSAndy Grover 
15417c9e7a6fSAndy Grover 	info->irqcontrol = tcmu_irqcontrol;
15427c9e7a6fSAndy Grover 	info->irq = UIO_IRQ_CUSTOM;
15437c9e7a6fSAndy Grover 
15447c9e7a6fSAndy Grover 	info->mmap = tcmu_mmap;
15457c9e7a6fSAndy Grover 	info->open = tcmu_open;
15467c9e7a6fSAndy Grover 	info->release = tcmu_release;
15477c9e7a6fSAndy Grover 
15487c9e7a6fSAndy Grover 	ret = uio_register_device(tcmu_root_device, info);
15497c9e7a6fSAndy Grover 	if (ret)
15507c9e7a6fSAndy Grover 		goto err_register;
15517c9e7a6fSAndy Grover 
155281ee28deSSheng Yang 	/* User can set hw_block_size before enable the device */
155381ee28deSSheng Yang 	if (dev->dev_attrib.hw_block_size == 0)
15547c9e7a6fSAndy Grover 		dev->dev_attrib.hw_block_size = 512;
155581ee28deSSheng Yang 	/* Other attributes can be configured in userspace */
15563abaa2bfSMike Christie 	if (!dev->dev_attrib.hw_max_sectors)
15577c9e7a6fSAndy Grover 		dev->dev_attrib.hw_max_sectors = 128;
15589a8bb606SBryant G. Ly 	if (!dev->dev_attrib.emulate_write_cache)
15599a8bb606SBryant G. Ly 		dev->dev_attrib.emulate_write_cache = 0;
15607c9e7a6fSAndy Grover 	dev->dev_attrib.hw_queue_depth = 128;
15617c9e7a6fSAndy Grover 
1562b849b456SKenjiro Nakayama 	/* If user didn't explicitly disable netlink reply support, use
1563b849b456SKenjiro Nakayama 	 * module scope setting.
1564b849b456SKenjiro Nakayama 	 */
1565b849b456SKenjiro Nakayama 	if (udev->nl_reply_supported >= 0)
1566b849b456SKenjiro Nakayama 		udev->nl_reply_supported = tcmu_kern_cmd_reply_supported;
1567b849b456SKenjiro Nakayama 
1568f3cdbe39SMike Christie 	/*
1569f3cdbe39SMike Christie 	 * Get a ref incase userspace does a close on the uio device before
1570f3cdbe39SMike Christie 	 * LIO has initiated tcmu_free_device.
1571f3cdbe39SMike Christie 	 */
1572f3cdbe39SMike Christie 	kref_get(&udev->kref);
1573f3cdbe39SMike Christie 
1574b3af66e2SMike Christie 	ret = tcmu_netlink_event(udev, TCMU_CMD_ADDED_DEVICE, 0, NULL);
15757c9e7a6fSAndy Grover 	if (ret)
15767c9e7a6fSAndy Grover 		goto err_netlink;
15777c9e7a6fSAndy Grover 
1578b6df4b79SXiubo Li 	mutex_lock(&root_udev_mutex);
1579b6df4b79SXiubo Li 	list_add(&udev->node, &root_udev);
1580b6df4b79SXiubo Li 	mutex_unlock(&root_udev_mutex);
1581b6df4b79SXiubo Li 
15827c9e7a6fSAndy Grover 	return 0;
15837c9e7a6fSAndy Grover 
15847c9e7a6fSAndy Grover err_netlink:
1585f3cdbe39SMike Christie 	kref_put(&udev->kref, tcmu_dev_kref_release);
15867c9e7a6fSAndy Grover 	uio_unregister_device(&udev->uio_info);
15877c9e7a6fSAndy Grover err_register:
15887c9e7a6fSAndy Grover 	vfree(udev->mb_addr);
1589c22adc0bSXiubo Li 	udev->mb_addr = NULL;
15907c9e7a6fSAndy Grover err_vzalloc:
15917c9e7a6fSAndy Grover 	kfree(info->name);
1592f3cdbe39SMike Christie 	info->name = NULL;
15937c9e7a6fSAndy Grover 
15947c9e7a6fSAndy Grover 	return ret;
15957c9e7a6fSAndy Grover }
15967c9e7a6fSAndy Grover 
1597972c7f16SMike Christie static bool tcmu_dev_configured(struct tcmu_dev *udev)
1598972c7f16SMike Christie {
1599972c7f16SMike Christie 	return udev->uio_info.uio_dev ? true : false;
1600972c7f16SMike Christie }
1601972c7f16SMike Christie 
16027c9e7a6fSAndy Grover static void tcmu_free_device(struct se_device *dev)
16037c9e7a6fSAndy Grover {
16047c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
160592634706SMike Christie 
160692634706SMike Christie 	/* release ref from init */
160792634706SMike Christie 	kref_put(&udev->kref, tcmu_dev_kref_release);
160892634706SMike Christie }
160992634706SMike Christie 
161092634706SMike Christie static void tcmu_destroy_device(struct se_device *dev)
161192634706SMike Christie {
161292634706SMike Christie 	struct tcmu_dev *udev = TCMU_DEV(dev);
16137c9e7a6fSAndy Grover 
16147c9e7a6fSAndy Grover 	del_timer_sync(&udev->timeout);
16157c9e7a6fSAndy Grover 
1616b6df4b79SXiubo Li 	mutex_lock(&root_udev_mutex);
1617b6df4b79SXiubo Li 	list_del(&udev->node);
1618b6df4b79SXiubo Li 	mutex_unlock(&root_udev_mutex);
1619b6df4b79SXiubo Li 
1620b3af66e2SMike Christie 	tcmu_netlink_event(udev, TCMU_CMD_REMOVED_DEVICE, 0, NULL);
16217c9e7a6fSAndy Grover 
16227c9e7a6fSAndy Grover 	uio_unregister_device(&udev->uio_info);
16239260695dSMike Christie 
16249260695dSMike Christie 	/* release ref from configure */
16259260695dSMike Christie 	kref_put(&udev->kref, tcmu_dev_kref_release);
16267c9e7a6fSAndy Grover }
16277c9e7a6fSAndy Grover 
16287c9e7a6fSAndy Grover enum {
16293abaa2bfSMike Christie 	Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_hw_max_sectors,
1630b849b456SKenjiro Nakayama 	Opt_nl_reply_supported, Opt_err,
16317c9e7a6fSAndy Grover };
16327c9e7a6fSAndy Grover 
16337c9e7a6fSAndy Grover static match_table_t tokens = {
16347c9e7a6fSAndy Grover 	{Opt_dev_config, "dev_config=%s"},
16357c9e7a6fSAndy Grover 	{Opt_dev_size, "dev_size=%u"},
16369c1cd1b6SAndy Grover 	{Opt_hw_block_size, "hw_block_size=%u"},
16373abaa2bfSMike Christie 	{Opt_hw_max_sectors, "hw_max_sectors=%u"},
1638b849b456SKenjiro Nakayama 	{Opt_nl_reply_supported, "nl_reply_supported=%d"},
16397c9e7a6fSAndy Grover 	{Opt_err, NULL}
16407c9e7a6fSAndy Grover };
16417c9e7a6fSAndy Grover 
16423abaa2bfSMike Christie static int tcmu_set_dev_attrib(substring_t *arg, u32 *dev_attrib)
16433abaa2bfSMike Christie {
16443abaa2bfSMike Christie 	unsigned long tmp_ul;
16453abaa2bfSMike Christie 	char *arg_p;
16463abaa2bfSMike Christie 	int ret;
16473abaa2bfSMike Christie 
16483abaa2bfSMike Christie 	arg_p = match_strdup(arg);
16493abaa2bfSMike Christie 	if (!arg_p)
16503abaa2bfSMike Christie 		return -ENOMEM;
16513abaa2bfSMike Christie 
16523abaa2bfSMike Christie 	ret = kstrtoul(arg_p, 0, &tmp_ul);
16533abaa2bfSMike Christie 	kfree(arg_p);
16543abaa2bfSMike Christie 	if (ret < 0) {
16553abaa2bfSMike Christie 		pr_err("kstrtoul() failed for dev attrib\n");
16563abaa2bfSMike Christie 		return ret;
16573abaa2bfSMike Christie 	}
16583abaa2bfSMike Christie 	if (!tmp_ul) {
16593abaa2bfSMike Christie 		pr_err("dev attrib must be nonzero\n");
16603abaa2bfSMike Christie 		return -EINVAL;
16613abaa2bfSMike Christie 	}
16623abaa2bfSMike Christie 	*dev_attrib = tmp_ul;
16633abaa2bfSMike Christie 	return 0;
16643abaa2bfSMike Christie }
16653abaa2bfSMike Christie 
16667c9e7a6fSAndy Grover static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev,
16677c9e7a6fSAndy Grover 		const char *page, ssize_t count)
16687c9e7a6fSAndy Grover {
16697c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
16707c9e7a6fSAndy Grover 	char *orig, *ptr, *opts, *arg_p;
16717c9e7a6fSAndy Grover 	substring_t args[MAX_OPT_ARGS];
16727c9e7a6fSAndy Grover 	int ret = 0, token;
16737c9e7a6fSAndy Grover 
16747c9e7a6fSAndy Grover 	opts = kstrdup(page, GFP_KERNEL);
16757c9e7a6fSAndy Grover 	if (!opts)
16767c9e7a6fSAndy Grover 		return -ENOMEM;
16777c9e7a6fSAndy Grover 
16787c9e7a6fSAndy Grover 	orig = opts;
16797c9e7a6fSAndy Grover 
16807c9e7a6fSAndy Grover 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
16817c9e7a6fSAndy Grover 		if (!*ptr)
16827c9e7a6fSAndy Grover 			continue;
16837c9e7a6fSAndy Grover 
16847c9e7a6fSAndy Grover 		token = match_token(ptr, tokens, args);
16857c9e7a6fSAndy Grover 		switch (token) {
16867c9e7a6fSAndy Grover 		case Opt_dev_config:
16877c9e7a6fSAndy Grover 			if (match_strlcpy(udev->dev_config, &args[0],
16887c9e7a6fSAndy Grover 					  TCMU_CONFIG_LEN) == 0) {
16897c9e7a6fSAndy Grover 				ret = -EINVAL;
16907c9e7a6fSAndy Grover 				break;
16917c9e7a6fSAndy Grover 			}
16927c9e7a6fSAndy Grover 			pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config);
16937c9e7a6fSAndy Grover 			break;
16947c9e7a6fSAndy Grover 		case Opt_dev_size:
16957c9e7a6fSAndy Grover 			arg_p = match_strdup(&args[0]);
16967c9e7a6fSAndy Grover 			if (!arg_p) {
16977c9e7a6fSAndy Grover 				ret = -ENOMEM;
16987c9e7a6fSAndy Grover 				break;
16997c9e7a6fSAndy Grover 			}
17007c9e7a6fSAndy Grover 			ret = kstrtoul(arg_p, 0, (unsigned long *) &udev->dev_size);
17017c9e7a6fSAndy Grover 			kfree(arg_p);
17027c9e7a6fSAndy Grover 			if (ret < 0)
17037c9e7a6fSAndy Grover 				pr_err("kstrtoul() failed for dev_size=\n");
17047c9e7a6fSAndy Grover 			break;
17059c1cd1b6SAndy Grover 		case Opt_hw_block_size:
17063abaa2bfSMike Christie 			ret = tcmu_set_dev_attrib(&args[0],
17073abaa2bfSMike Christie 					&(dev->dev_attrib.hw_block_size));
17089c1cd1b6SAndy Grover 			break;
17093abaa2bfSMike Christie 		case Opt_hw_max_sectors:
17103abaa2bfSMike Christie 			ret = tcmu_set_dev_attrib(&args[0],
17113abaa2bfSMike Christie 					&(dev->dev_attrib.hw_max_sectors));
17129c1cd1b6SAndy Grover 			break;
1713b849b456SKenjiro Nakayama 		case Opt_nl_reply_supported:
1714b849b456SKenjiro Nakayama 			arg_p = match_strdup(&args[0]);
1715b849b456SKenjiro Nakayama 			if (!arg_p) {
1716b849b456SKenjiro Nakayama 				ret = -ENOMEM;
1717b849b456SKenjiro Nakayama 				break;
1718b849b456SKenjiro Nakayama 			}
1719b849b456SKenjiro Nakayama 			ret = kstrtol(arg_p, 0,
1720b849b456SKenjiro Nakayama 					(long int *) &udev->nl_reply_supported);
1721b849b456SKenjiro Nakayama 			kfree(arg_p);
1722b849b456SKenjiro Nakayama 			if (ret < 0)
1723b849b456SKenjiro Nakayama 				pr_err("kstrtoul() failed for nl_reply_supported=\n");
1724b849b456SKenjiro Nakayama 			break;
17257c9e7a6fSAndy Grover 		default:
17267c9e7a6fSAndy Grover 			break;
17277c9e7a6fSAndy Grover 		}
17282579325cSMike Christie 
17292579325cSMike Christie 		if (ret)
17302579325cSMike Christie 			break;
17317c9e7a6fSAndy Grover 	}
17327c9e7a6fSAndy Grover 
17337c9e7a6fSAndy Grover 	kfree(orig);
17347c9e7a6fSAndy Grover 	return (!ret) ? count : ret;
17357c9e7a6fSAndy Grover }
17367c9e7a6fSAndy Grover 
17377c9e7a6fSAndy Grover static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b)
17387c9e7a6fSAndy Grover {
17397c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
17407c9e7a6fSAndy Grover 	ssize_t bl = 0;
17417c9e7a6fSAndy Grover 
17427c9e7a6fSAndy Grover 	bl = sprintf(b + bl, "Config: %s ",
17437c9e7a6fSAndy Grover 		     udev->dev_config[0] ? udev->dev_config : "NULL");
17447d7a7435SNicholas Bellinger 	bl += sprintf(b + bl, "Size: %zu\n", udev->dev_size);
17457c9e7a6fSAndy Grover 
17467c9e7a6fSAndy Grover 	return bl;
17477c9e7a6fSAndy Grover }
17487c9e7a6fSAndy Grover 
17497c9e7a6fSAndy Grover static sector_t tcmu_get_blocks(struct se_device *dev)
17507c9e7a6fSAndy Grover {
17517c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
17527c9e7a6fSAndy Grover 
17537c9e7a6fSAndy Grover 	return div_u64(udev->dev_size - dev->dev_attrib.block_size,
17547c9e7a6fSAndy Grover 		       dev->dev_attrib.block_size);
17557c9e7a6fSAndy Grover }
17567c9e7a6fSAndy Grover 
17577c9e7a6fSAndy Grover static sense_reason_t
17587c9e7a6fSAndy Grover tcmu_parse_cdb(struct se_cmd *cmd)
17597c9e7a6fSAndy Grover {
176002eb924fSAndy Grover 	return passthrough_parse_cdb(cmd, tcmu_queue_cmd);
17619c1cd1b6SAndy Grover }
17629c1cd1b6SAndy Grover 
17637d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_show(struct config_item *item, char *page)
17647d7a7435SNicholas Bellinger {
17657d7a7435SNicholas Bellinger 	struct se_dev_attrib *da = container_of(to_config_group(item),
17667d7a7435SNicholas Bellinger 					struct se_dev_attrib, da_group);
1767b5ab697cSKenjiro Nakayama 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
17687d7a7435SNicholas Bellinger 
17697d7a7435SNicholas Bellinger 	return snprintf(page, PAGE_SIZE, "%lu\n", udev->cmd_time_out / MSEC_PER_SEC);
17707d7a7435SNicholas Bellinger }
17717d7a7435SNicholas Bellinger 
17727d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_store(struct config_item *item, const char *page,
17737d7a7435SNicholas Bellinger 				       size_t count)
17747d7a7435SNicholas Bellinger {
17757d7a7435SNicholas Bellinger 	struct se_dev_attrib *da = container_of(to_config_group(item),
17767d7a7435SNicholas Bellinger 					struct se_dev_attrib, da_group);
17777d7a7435SNicholas Bellinger 	struct tcmu_dev *udev = container_of(da->da_dev,
17787d7a7435SNicholas Bellinger 					struct tcmu_dev, se_dev);
17797d7a7435SNicholas Bellinger 	u32 val;
17807d7a7435SNicholas Bellinger 	int ret;
17817d7a7435SNicholas Bellinger 
17827d7a7435SNicholas Bellinger 	if (da->da_dev->export_count) {
17837d7a7435SNicholas Bellinger 		pr_err("Unable to set tcmu cmd_time_out while exports exist\n");
17847d7a7435SNicholas Bellinger 		return -EINVAL;
17857d7a7435SNicholas Bellinger 	}
17867d7a7435SNicholas Bellinger 
17877d7a7435SNicholas Bellinger 	ret = kstrtou32(page, 0, &val);
17887d7a7435SNicholas Bellinger 	if (ret < 0)
17897d7a7435SNicholas Bellinger 		return ret;
17907d7a7435SNicholas Bellinger 
17917d7a7435SNicholas Bellinger 	udev->cmd_time_out = val * MSEC_PER_SEC;
17927d7a7435SNicholas Bellinger 	return count;
17937d7a7435SNicholas Bellinger }
17947d7a7435SNicholas Bellinger CONFIGFS_ATTR(tcmu_, cmd_time_out);
17957d7a7435SNicholas Bellinger 
17962d76443eSMike Christie static ssize_t tcmu_dev_config_show(struct config_item *item, char *page)
1797ee018252SBryant G. Ly {
1798ee018252SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
1799ee018252SBryant G. Ly 						struct se_dev_attrib, da_group);
1800ee018252SBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
1801ee018252SBryant G. Ly 
1802ee018252SBryant G. Ly 	return snprintf(page, PAGE_SIZE, "%s\n", udev->dev_config);
1803ee018252SBryant G. Ly }
1804ee018252SBryant G. Ly 
18052d76443eSMike Christie static ssize_t tcmu_dev_config_store(struct config_item *item, const char *page,
1806ee018252SBryant G. Ly 				     size_t count)
1807ee018252SBryant G. Ly {
1808ee018252SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
1809ee018252SBryant G. Ly 						struct se_dev_attrib, da_group);
1810ee018252SBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
18112d76443eSMike Christie 	int ret, len;
1812ee018252SBryant G. Ly 
18132d76443eSMike Christie 	len = strlen(page);
18142d76443eSMike Christie 	if (!len || len > TCMU_CONFIG_LEN - 1)
1815ee018252SBryant G. Ly 		return -EINVAL;
1816ee018252SBryant G. Ly 
1817ee018252SBryant G. Ly 	/* Check if device has been configured before */
1818ee018252SBryant G. Ly 	if (tcmu_dev_configured(udev)) {
1819b3af66e2SMike Christie 		ret = tcmu_netlink_event(udev, TCMU_CMD_RECONFIG_DEVICE,
18202d76443eSMike Christie 					 TCMU_ATTR_DEV_CFG, page);
1821ee018252SBryant G. Ly 		if (ret) {
1822ee018252SBryant G. Ly 			pr_err("Unable to reconfigure device\n");
1823ee018252SBryant G. Ly 			return ret;
1824ee018252SBryant G. Ly 		}
1825de8c5221SBryant G. Ly 		strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN);
1826de8c5221SBryant G. Ly 
1827de8c5221SBryant G. Ly 		ret = tcmu_update_uio_info(udev);
1828de8c5221SBryant G. Ly 		if (ret)
1829de8c5221SBryant G. Ly 			return ret;
1830de8c5221SBryant G. Ly 		return count;
1831ee018252SBryant G. Ly 	}
18322d76443eSMike Christie 	strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN);
1833ee018252SBryant G. Ly 
1834ee018252SBryant G. Ly 	return count;
1835ee018252SBryant G. Ly }
18362d76443eSMike Christie CONFIGFS_ATTR(tcmu_, dev_config);
1837ee018252SBryant G. Ly 
1838801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_show(struct config_item *item, char *page)
1839801fc54dSBryant G. Ly {
1840801fc54dSBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
1841801fc54dSBryant G. Ly 						struct se_dev_attrib, da_group);
1842801fc54dSBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
1843801fc54dSBryant G. Ly 
1844801fc54dSBryant G. Ly 	return snprintf(page, PAGE_SIZE, "%zu\n", udev->dev_size);
1845801fc54dSBryant G. Ly }
1846801fc54dSBryant G. Ly 
1847801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_store(struct config_item *item, const char *page,
1848801fc54dSBryant G. Ly 				   size_t count)
1849801fc54dSBryant G. Ly {
1850801fc54dSBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
1851801fc54dSBryant G. Ly 						struct se_dev_attrib, da_group);
1852801fc54dSBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
18532d76443eSMike Christie 	u64 val;
1854801fc54dSBryant G. Ly 	int ret;
1855801fc54dSBryant G. Ly 
18562d76443eSMike Christie 	ret = kstrtou64(page, 0, &val);
1857801fc54dSBryant G. Ly 	if (ret < 0)
1858801fc54dSBryant G. Ly 		return ret;
1859801fc54dSBryant G. Ly 
1860801fc54dSBryant G. Ly 	/* Check if device has been configured before */
1861801fc54dSBryant G. Ly 	if (tcmu_dev_configured(udev)) {
1862b3af66e2SMike Christie 		ret = tcmu_netlink_event(udev, TCMU_CMD_RECONFIG_DEVICE,
18632d76443eSMike Christie 					 TCMU_ATTR_DEV_SIZE, &val);
1864801fc54dSBryant G. Ly 		if (ret) {
1865801fc54dSBryant G. Ly 			pr_err("Unable to reconfigure device\n");
1866801fc54dSBryant G. Ly 			return ret;
1867801fc54dSBryant G. Ly 		}
1868801fc54dSBryant G. Ly 	}
18692d76443eSMike Christie 	udev->dev_size = val;
1870801fc54dSBryant G. Ly 	return count;
1871801fc54dSBryant G. Ly }
1872801fc54dSBryant G. Ly CONFIGFS_ATTR(tcmu_, dev_size);
1873801fc54dSBryant G. Ly 
1874b849b456SKenjiro Nakayama static ssize_t tcmu_nl_reply_supported_show(struct config_item *item,
1875b849b456SKenjiro Nakayama 		char *page)
1876b849b456SKenjiro Nakayama {
1877b849b456SKenjiro Nakayama 	struct se_dev_attrib *da = container_of(to_config_group(item),
1878b849b456SKenjiro Nakayama 						struct se_dev_attrib, da_group);
1879b849b456SKenjiro Nakayama 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
1880b849b456SKenjiro Nakayama 
1881b849b456SKenjiro Nakayama 	return snprintf(page, PAGE_SIZE, "%d\n", udev->nl_reply_supported);
1882b849b456SKenjiro Nakayama }
1883b849b456SKenjiro Nakayama 
1884b849b456SKenjiro Nakayama static ssize_t tcmu_nl_reply_supported_store(struct config_item *item,
1885b849b456SKenjiro Nakayama 		const char *page, size_t count)
1886b849b456SKenjiro Nakayama {
1887b849b456SKenjiro Nakayama 	struct se_dev_attrib *da = container_of(to_config_group(item),
1888b849b456SKenjiro Nakayama 						struct se_dev_attrib, da_group);
1889b849b456SKenjiro Nakayama 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
1890b849b456SKenjiro Nakayama 	s8 val;
1891b849b456SKenjiro Nakayama 	int ret;
1892b849b456SKenjiro Nakayama 
1893b849b456SKenjiro Nakayama 	ret = kstrtos8(page, 0, &val);
1894b849b456SKenjiro Nakayama 	if (ret < 0)
1895b849b456SKenjiro Nakayama 		return ret;
1896b849b456SKenjiro Nakayama 
1897b849b456SKenjiro Nakayama 	udev->nl_reply_supported = val;
1898b849b456SKenjiro Nakayama 	return count;
1899b849b456SKenjiro Nakayama }
1900b849b456SKenjiro Nakayama CONFIGFS_ATTR(tcmu_, nl_reply_supported);
1901b849b456SKenjiro Nakayama 
19029a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_show(struct config_item *item,
19039a8bb606SBryant G. Ly 					     char *page)
19049a8bb606SBryant G. Ly {
19059a8bb606SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
19069a8bb606SBryant G. Ly 					struct se_dev_attrib, da_group);
19079a8bb606SBryant G. Ly 
19089a8bb606SBryant G. Ly 	return snprintf(page, PAGE_SIZE, "%i\n", da->emulate_write_cache);
19099a8bb606SBryant G. Ly }
19109a8bb606SBryant G. Ly 
19119a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_store(struct config_item *item,
19129a8bb606SBryant G. Ly 					      const char *page, size_t count)
19139a8bb606SBryant G. Ly {
19149a8bb606SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
19159a8bb606SBryant G. Ly 					struct se_dev_attrib, da_group);
19161068be7bSBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
19172d76443eSMike Christie 	u8 val;
19189a8bb606SBryant G. Ly 	int ret;
19199a8bb606SBryant G. Ly 
19202d76443eSMike Christie 	ret = kstrtou8(page, 0, &val);
19219a8bb606SBryant G. Ly 	if (ret < 0)
19229a8bb606SBryant G. Ly 		return ret;
19239a8bb606SBryant G. Ly 
19241068be7bSBryant G. Ly 	/* Check if device has been configured before */
19251068be7bSBryant G. Ly 	if (tcmu_dev_configured(udev)) {
1926b3af66e2SMike Christie 		ret = tcmu_netlink_event(udev, TCMU_CMD_RECONFIG_DEVICE,
19272d76443eSMike Christie 					 TCMU_ATTR_WRITECACHE, &val);
19281068be7bSBryant G. Ly 		if (ret) {
19291068be7bSBryant G. Ly 			pr_err("Unable to reconfigure device\n");
19301068be7bSBryant G. Ly 			return ret;
19311068be7bSBryant G. Ly 		}
19321068be7bSBryant G. Ly 	}
19332d76443eSMike Christie 
19342d76443eSMike Christie 	da->emulate_write_cache = val;
19359a8bb606SBryant G. Ly 	return count;
19369a8bb606SBryant G. Ly }
19379a8bb606SBryant G. Ly CONFIGFS_ATTR(tcmu_, emulate_write_cache);
19389a8bb606SBryant G. Ly 
19395821783bSColin Ian King static struct configfs_attribute *tcmu_attrib_attrs[] = {
1940801fc54dSBryant G. Ly 	&tcmu_attr_cmd_time_out,
19412d76443eSMike Christie 	&tcmu_attr_dev_config,
1942801fc54dSBryant G. Ly 	&tcmu_attr_dev_size,
1943801fc54dSBryant G. Ly 	&tcmu_attr_emulate_write_cache,
1944b849b456SKenjiro Nakayama 	&tcmu_attr_nl_reply_supported,
1945801fc54dSBryant G. Ly 	NULL,
1946801fc54dSBryant G. Ly };
1947801fc54dSBryant G. Ly 
19487d7a7435SNicholas Bellinger static struct configfs_attribute **tcmu_attrs;
19497d7a7435SNicholas Bellinger 
19507d7a7435SNicholas Bellinger static struct target_backend_ops tcmu_ops = {
19517c9e7a6fSAndy Grover 	.name			= "user",
19527c9e7a6fSAndy Grover 	.owner			= THIS_MODULE,
1953a3541703SAndy Grover 	.transport_flags	= TRANSPORT_FLAG_PASSTHROUGH,
19547c9e7a6fSAndy Grover 	.attach_hba		= tcmu_attach_hba,
19557c9e7a6fSAndy Grover 	.detach_hba		= tcmu_detach_hba,
19567c9e7a6fSAndy Grover 	.alloc_device		= tcmu_alloc_device,
19577c9e7a6fSAndy Grover 	.configure_device	= tcmu_configure_device,
195892634706SMike Christie 	.destroy_device		= tcmu_destroy_device,
19597c9e7a6fSAndy Grover 	.free_device		= tcmu_free_device,
19607c9e7a6fSAndy Grover 	.parse_cdb		= tcmu_parse_cdb,
19617c9e7a6fSAndy Grover 	.set_configfs_dev_params = tcmu_set_configfs_dev_params,
19627c9e7a6fSAndy Grover 	.show_configfs_dev_params = tcmu_show_configfs_dev_params,
19637c9e7a6fSAndy Grover 	.get_device_type	= sbc_get_device_type,
19647c9e7a6fSAndy Grover 	.get_blocks		= tcmu_get_blocks,
19657d7a7435SNicholas Bellinger 	.tb_dev_attrib_attrs	= NULL,
19667c9e7a6fSAndy Grover };
19677c9e7a6fSAndy Grover 
1968b6df4b79SXiubo Li static int unmap_thread_fn(void *data)
1969b6df4b79SXiubo Li {
1970b6df4b79SXiubo Li 	struct tcmu_dev *udev;
1971b6df4b79SXiubo Li 	loff_t off;
1972b6df4b79SXiubo Li 	uint32_t start, end, block;
1973b6df4b79SXiubo Li 	struct page *page;
1974b6df4b79SXiubo Li 	int i;
1975b6df4b79SXiubo Li 
197607932a02SXiubo Li 	while (!kthread_should_stop()) {
1977b6df4b79SXiubo Li 		DEFINE_WAIT(__wait);
1978b6df4b79SXiubo Li 
1979b6df4b79SXiubo Li 		prepare_to_wait(&unmap_wait, &__wait, TASK_INTERRUPTIBLE);
1980b6df4b79SXiubo Li 		schedule();
1981b6df4b79SXiubo Li 		finish_wait(&unmap_wait, &__wait);
1982b6df4b79SXiubo Li 
1983d906d8afSMike Christie 		if (kthread_should_stop())
1984d906d8afSMike Christie 			break;
1985d906d8afSMike Christie 
1986b6df4b79SXiubo Li 		mutex_lock(&root_udev_mutex);
1987b6df4b79SXiubo Li 		list_for_each_entry(udev, &root_udev, node) {
1988b6df4b79SXiubo Li 			mutex_lock(&udev->cmdr_lock);
1989b6df4b79SXiubo Li 
1990b6df4b79SXiubo Li 			/* Try to complete the finished commands first */
1991b6df4b79SXiubo Li 			tcmu_handle_completions(udev);
1992b6df4b79SXiubo Li 
1993b6df4b79SXiubo Li 			/* Skip the udevs waiting the global pool or in idle */
1994b6df4b79SXiubo Li 			if (udev->waiting_global || !udev->dbi_thresh) {
1995b6df4b79SXiubo Li 				mutex_unlock(&udev->cmdr_lock);
1996b6df4b79SXiubo Li 				continue;
1997b6df4b79SXiubo Li 			}
1998b6df4b79SXiubo Li 
1999b6df4b79SXiubo Li 			end = udev->dbi_max + 1;
2000b6df4b79SXiubo Li 			block = find_last_bit(udev->data_bitmap, end);
2001b6df4b79SXiubo Li 			if (block == udev->dbi_max) {
2002b6df4b79SXiubo Li 				/*
2003b6df4b79SXiubo Li 				 * The last bit is dbi_max, so there is
2004b6df4b79SXiubo Li 				 * no need to shrink any blocks.
2005b6df4b79SXiubo Li 				 */
2006b6df4b79SXiubo Li 				mutex_unlock(&udev->cmdr_lock);
2007b6df4b79SXiubo Li 				continue;
2008b6df4b79SXiubo Li 			} else if (block == end) {
2009b6df4b79SXiubo Li 				/* The current udev will goto idle state */
2010b6df4b79SXiubo Li 				udev->dbi_thresh = start = 0;
2011b6df4b79SXiubo Li 				udev->dbi_max = 0;
2012b6df4b79SXiubo Li 			} else {
2013b6df4b79SXiubo Li 				udev->dbi_thresh = start = block + 1;
2014b6df4b79SXiubo Li 				udev->dbi_max = block;
2015b6df4b79SXiubo Li 			}
2016b6df4b79SXiubo Li 
2017b6df4b79SXiubo Li 			/* Here will truncate the data area from off */
2018b6df4b79SXiubo Li 			off = udev->data_off + start * DATA_BLOCK_SIZE;
2019b6df4b79SXiubo Li 			unmap_mapping_range(udev->inode->i_mapping, off, 0, 1);
2020b6df4b79SXiubo Li 
2021b6df4b79SXiubo Li 			/* Release the block pages */
2022b6df4b79SXiubo Li 			for (i = start; i < end; i++) {
2023b6df4b79SXiubo Li 				page = radix_tree_delete(&udev->data_blocks, i);
2024b6df4b79SXiubo Li 				if (page) {
2025b6df4b79SXiubo Li 					__free_page(page);
2026b6df4b79SXiubo Li 					atomic_dec(&global_db_count);
2027b6df4b79SXiubo Li 				}
2028b6df4b79SXiubo Li 			}
2029b6df4b79SXiubo Li 			mutex_unlock(&udev->cmdr_lock);
2030b6df4b79SXiubo Li 		}
2031b6df4b79SXiubo Li 
2032b6df4b79SXiubo Li 		/*
2033b6df4b79SXiubo Li 		 * Try to wake up the udevs who are waiting
2034b6df4b79SXiubo Li 		 * for the global data pool.
2035b6df4b79SXiubo Li 		 */
2036b6df4b79SXiubo Li 		list_for_each_entry(udev, &root_udev, node) {
2037b6df4b79SXiubo Li 			if (udev->waiting_global)
2038b6df4b79SXiubo Li 				wake_up(&udev->wait_cmdr);
2039b6df4b79SXiubo Li 		}
2040b6df4b79SXiubo Li 		mutex_unlock(&root_udev_mutex);
2041b6df4b79SXiubo Li 	}
2042b6df4b79SXiubo Li 
2043b6df4b79SXiubo Li 	return 0;
2044b6df4b79SXiubo Li }
2045b6df4b79SXiubo Li 
20467c9e7a6fSAndy Grover static int __init tcmu_module_init(void)
20477c9e7a6fSAndy Grover {
2048801fc54dSBryant G. Ly 	int ret, i, k, len = 0;
20497c9e7a6fSAndy Grover 
20507c9e7a6fSAndy Grover 	BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0);
20517c9e7a6fSAndy Grover 
20527c9e7a6fSAndy Grover 	tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache",
20537c9e7a6fSAndy Grover 				sizeof(struct tcmu_cmd),
20547c9e7a6fSAndy Grover 				__alignof__(struct tcmu_cmd),
20557c9e7a6fSAndy Grover 				0, NULL);
20567c9e7a6fSAndy Grover 	if (!tcmu_cmd_cache)
20577c9e7a6fSAndy Grover 		return -ENOMEM;
20587c9e7a6fSAndy Grover 
20597c9e7a6fSAndy Grover 	tcmu_root_device = root_device_register("tcm_user");
20607c9e7a6fSAndy Grover 	if (IS_ERR(tcmu_root_device)) {
20617c9e7a6fSAndy Grover 		ret = PTR_ERR(tcmu_root_device);
20627c9e7a6fSAndy Grover 		goto out_free_cache;
20637c9e7a6fSAndy Grover 	}
20647c9e7a6fSAndy Grover 
20657c9e7a6fSAndy Grover 	ret = genl_register_family(&tcmu_genl_family);
20667c9e7a6fSAndy Grover 	if (ret < 0) {
20677c9e7a6fSAndy Grover 		goto out_unreg_device;
20687c9e7a6fSAndy Grover 	}
20697c9e7a6fSAndy Grover 
20707d7a7435SNicholas Bellinger 	for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) {
20717d7a7435SNicholas Bellinger 		len += sizeof(struct configfs_attribute *);
20727d7a7435SNicholas Bellinger 	}
2073801fc54dSBryant G. Ly 	for (i = 0; tcmu_attrib_attrs[i] != NULL; i++) {
2074801fc54dSBryant G. Ly 		len += sizeof(struct configfs_attribute *);
2075801fc54dSBryant G. Ly 	}
2076801fc54dSBryant G. Ly 	len += sizeof(struct configfs_attribute *);
20777d7a7435SNicholas Bellinger 
20787d7a7435SNicholas Bellinger 	tcmu_attrs = kzalloc(len, GFP_KERNEL);
20797d7a7435SNicholas Bellinger 	if (!tcmu_attrs) {
20807d7a7435SNicholas Bellinger 		ret = -ENOMEM;
20817d7a7435SNicholas Bellinger 		goto out_unreg_genl;
20827d7a7435SNicholas Bellinger 	}
20837d7a7435SNicholas Bellinger 
20847d7a7435SNicholas Bellinger 	for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) {
20857d7a7435SNicholas Bellinger 		tcmu_attrs[i] = passthrough_attrib_attrs[i];
20867d7a7435SNicholas Bellinger 	}
2087801fc54dSBryant G. Ly 	for (k = 0; tcmu_attrib_attrs[k] != NULL; k++) {
2088801fc54dSBryant G. Ly 		tcmu_attrs[i] = tcmu_attrib_attrs[k];
20899a8bb606SBryant G. Ly 		i++;
2090801fc54dSBryant G. Ly 	}
20917d7a7435SNicholas Bellinger 	tcmu_ops.tb_dev_attrib_attrs = tcmu_attrs;
20927d7a7435SNicholas Bellinger 
20930a06d430SChristoph Hellwig 	ret = transport_backend_register(&tcmu_ops);
20947c9e7a6fSAndy Grover 	if (ret)
20957d7a7435SNicholas Bellinger 		goto out_attrs;
20967c9e7a6fSAndy Grover 
2097b6df4b79SXiubo Li 	init_waitqueue_head(&unmap_wait);
2098b6df4b79SXiubo Li 	unmap_thread = kthread_run(unmap_thread_fn, NULL, "tcmu_unmap");
2099b6df4b79SXiubo Li 	if (IS_ERR(unmap_thread)) {
2100b6df4b79SXiubo Li 		ret = PTR_ERR(unmap_thread);
2101b6df4b79SXiubo Li 		goto out_unreg_transport;
2102b6df4b79SXiubo Li 	}
2103b6df4b79SXiubo Li 
21047c9e7a6fSAndy Grover 	return 0;
21057c9e7a6fSAndy Grover 
2106b6df4b79SXiubo Li out_unreg_transport:
2107b6df4b79SXiubo Li 	target_backend_unregister(&tcmu_ops);
21087d7a7435SNicholas Bellinger out_attrs:
21097d7a7435SNicholas Bellinger 	kfree(tcmu_attrs);
21107c9e7a6fSAndy Grover out_unreg_genl:
21117c9e7a6fSAndy Grover 	genl_unregister_family(&tcmu_genl_family);
21127c9e7a6fSAndy Grover out_unreg_device:
21137c9e7a6fSAndy Grover 	root_device_unregister(tcmu_root_device);
21147c9e7a6fSAndy Grover out_free_cache:
21157c9e7a6fSAndy Grover 	kmem_cache_destroy(tcmu_cmd_cache);
21167c9e7a6fSAndy Grover 
21177c9e7a6fSAndy Grover 	return ret;
21187c9e7a6fSAndy Grover }
21197c9e7a6fSAndy Grover 
21207c9e7a6fSAndy Grover static void __exit tcmu_module_exit(void)
21217c9e7a6fSAndy Grover {
2122b6df4b79SXiubo Li 	kthread_stop(unmap_thread);
21230a06d430SChristoph Hellwig 	target_backend_unregister(&tcmu_ops);
21247d7a7435SNicholas Bellinger 	kfree(tcmu_attrs);
21257c9e7a6fSAndy Grover 	genl_unregister_family(&tcmu_genl_family);
21267c9e7a6fSAndy Grover 	root_device_unregister(tcmu_root_device);
21277c9e7a6fSAndy Grover 	kmem_cache_destroy(tcmu_cmd_cache);
21287c9e7a6fSAndy Grover }
21297c9e7a6fSAndy Grover 
21307c9e7a6fSAndy Grover MODULE_DESCRIPTION("TCM USER subsystem plugin");
21317c9e7a6fSAndy Grover MODULE_AUTHOR("Shaohua Li <shli@kernel.org>");
21327c9e7a6fSAndy Grover MODULE_AUTHOR("Andy Grover <agrover@redhat.com>");
21337c9e7a6fSAndy Grover MODULE_LICENSE("GPL");
21347c9e7a6fSAndy Grover 
21357c9e7a6fSAndy Grover module_init(tcmu_module_init);
21367c9e7a6fSAndy Grover module_exit(tcmu_module_exit);
2137