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>
359972cebbSMike Christie #include <linux/workqueue.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)
827c9e7a6fSAndy Grover 
83b6df4b79SXiubo Li /* The total size of the ring is 8M + 256K * PAGE_SIZE */
847c9e7a6fSAndy Grover #define TCMU_RING_SIZE (CMDR_SIZE + DATA_SIZE)
857c9e7a6fSAndy Grover 
86af1dd7ffSMike Christie /*
87af1dd7ffSMike Christie  * Default number of global data blocks(512K * PAGE_SIZE)
88af1dd7ffSMike Christie  * when the unmap thread will be started.
89af1dd7ffSMike Christie  */
90b6df4b79SXiubo Li #define TCMU_GLOBAL_MAX_BLOCKS (512 * 1024)
91b6df4b79SXiubo Li 
92b3af66e2SMike Christie static u8 tcmu_kern_cmd_reply_supported;
93b3af66e2SMike Christie 
947c9e7a6fSAndy Grover static struct device *tcmu_root_device;
957c9e7a6fSAndy Grover 
967c9e7a6fSAndy Grover struct tcmu_hba {
977c9e7a6fSAndy Grover 	u32 host_id;
987c9e7a6fSAndy Grover };
997c9e7a6fSAndy Grover 
1007c9e7a6fSAndy Grover #define TCMU_CONFIG_LEN 256
1017c9e7a6fSAndy Grover 
102b3af66e2SMike Christie struct tcmu_nl_cmd {
103b3af66e2SMike Christie 	/* wake up thread waiting for reply */
104b3af66e2SMike Christie 	struct completion complete;
105b3af66e2SMike Christie 	int cmd;
106b3af66e2SMike Christie 	int status;
107b3af66e2SMike Christie };
108b3af66e2SMike Christie 
1097c9e7a6fSAndy Grover struct tcmu_dev {
110b6df4b79SXiubo Li 	struct list_head node;
111f3cdbe39SMike Christie 	struct kref kref;
112af1dd7ffSMike Christie 
1137c9e7a6fSAndy Grover 	struct se_device se_dev;
1147c9e7a6fSAndy Grover 
1157c9e7a6fSAndy Grover 	char *name;
1167c9e7a6fSAndy Grover 	struct se_hba *hba;
1177c9e7a6fSAndy Grover 
1187c9e7a6fSAndy Grover #define TCMU_DEV_BIT_OPEN 0
1197c9e7a6fSAndy Grover #define TCMU_DEV_BIT_BROKEN 1
1207c9e7a6fSAndy Grover 	unsigned long flags;
1217c9e7a6fSAndy Grover 
1227c9e7a6fSAndy Grover 	struct uio_info uio_info;
1237c9e7a6fSAndy Grover 
124b6df4b79SXiubo Li 	struct inode *inode;
125b6df4b79SXiubo Li 
1267c9e7a6fSAndy Grover 	struct tcmu_mailbox *mb_addr;
1277c9e7a6fSAndy Grover 	size_t dev_size;
1287c9e7a6fSAndy Grover 	u32 cmdr_size;
1297c9e7a6fSAndy Grover 	u32 cmdr_last_cleaned;
1303d9b9555SAndy Grover 	/* Offset of data area from start of mb */
13126418649SSheng Yang 	/* Must add data_off and mb_addr to get the address */
1327c9e7a6fSAndy Grover 	size_t data_off;
1337c9e7a6fSAndy Grover 	size_t data_size;
13426418649SSheng Yang 
135b6df4b79SXiubo Li 	struct mutex cmdr_lock;
136af1dd7ffSMike Christie 	struct list_head cmdr_queue;
1377c9e7a6fSAndy Grover 
138141685a3SXiubo Li 	uint32_t dbi_max;
139b6df4b79SXiubo Li 	uint32_t dbi_thresh;
140141685a3SXiubo Li 	DECLARE_BITMAP(data_bitmap, DATA_BLOCK_BITS);
141141685a3SXiubo Li 	struct radix_tree_root data_blocks;
142141685a3SXiubo Li 
1437c9e7a6fSAndy Grover 	struct idr commands;
1447c9e7a6fSAndy Grover 
1457c9e7a6fSAndy Grover 	struct timer_list timeout;
146af980e46SMike Christie 	unsigned int cmd_time_out;
147488ebe4cSMike Christie 	struct list_head timedout_entry;
1487c9e7a6fSAndy Grover 
149b3af66e2SMike Christie 	spinlock_t nl_cmd_lock;
150b3af66e2SMike Christie 	struct tcmu_nl_cmd curr_nl_cmd;
151b3af66e2SMike Christie 	/* wake up threads waiting on curr_nl_cmd */
152b3af66e2SMike Christie 	wait_queue_head_t nl_cmd_wq;
153b3af66e2SMike Christie 
1547c9e7a6fSAndy Grover 	char dev_config[TCMU_CONFIG_LEN];
155b849b456SKenjiro Nakayama 
156b849b456SKenjiro Nakayama 	int nl_reply_supported;
1577c9e7a6fSAndy Grover };
1587c9e7a6fSAndy Grover 
1597c9e7a6fSAndy Grover #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev)
1607c9e7a6fSAndy Grover 
1617c9e7a6fSAndy Grover #define CMDR_OFF sizeof(struct tcmu_mailbox)
1627c9e7a6fSAndy Grover 
1637c9e7a6fSAndy Grover struct tcmu_cmd {
1647c9e7a6fSAndy Grover 	struct se_cmd *se_cmd;
1657c9e7a6fSAndy Grover 	struct tcmu_dev *tcmu_dev;
166af1dd7ffSMike Christie 	struct list_head cmdr_queue_entry;
1677c9e7a6fSAndy Grover 
1687c9e7a6fSAndy Grover 	uint16_t cmd_id;
1697c9e7a6fSAndy Grover 
17026418649SSheng Yang 	/* Can't use se_cmd when cleaning up expired cmds, because if
1717c9e7a6fSAndy Grover 	   cmd has been completed then accessing se_cmd is off limits */
172141685a3SXiubo Li 	uint32_t dbi_cnt;
173141685a3SXiubo Li 	uint32_t dbi_cur;
174141685a3SXiubo Li 	uint32_t *dbi;
1757c9e7a6fSAndy Grover 
1767c9e7a6fSAndy Grover 	unsigned long deadline;
1777c9e7a6fSAndy Grover 
1787c9e7a6fSAndy Grover #define TCMU_CMD_BIT_EXPIRED 0
1797c9e7a6fSAndy Grover 	unsigned long flags;
1807c9e7a6fSAndy Grover };
181af1dd7ffSMike Christie /*
182af1dd7ffSMike Christie  * To avoid dead lock the mutex lock order should always be:
183af1dd7ffSMike Christie  *
184af1dd7ffSMike Christie  * mutex_lock(&root_udev_mutex);
185af1dd7ffSMike Christie  * ...
186af1dd7ffSMike Christie  * mutex_lock(&tcmu_dev->cmdr_lock);
187af1dd7ffSMike Christie  * mutex_unlock(&tcmu_dev->cmdr_lock);
188af1dd7ffSMike Christie  * ...
189af1dd7ffSMike Christie  * mutex_unlock(&root_udev_mutex);
190af1dd7ffSMike Christie  */
191b6df4b79SXiubo Li static DEFINE_MUTEX(root_udev_mutex);
192b6df4b79SXiubo Li static LIST_HEAD(root_udev);
193b6df4b79SXiubo Li 
194488ebe4cSMike Christie static DEFINE_SPINLOCK(timed_out_udevs_lock);
195488ebe4cSMike Christie static LIST_HEAD(timed_out_udevs);
196488ebe4cSMike Christie 
197b6df4b79SXiubo Li static atomic_t global_db_count = ATOMIC_INIT(0);
198af1dd7ffSMike Christie static struct delayed_work tcmu_unmap_work;
199b6df4b79SXiubo Li 
2007c9e7a6fSAndy Grover static struct kmem_cache *tcmu_cmd_cache;
2017c9e7a6fSAndy Grover 
2027c9e7a6fSAndy Grover /* multicast group */
2037c9e7a6fSAndy Grover enum tcmu_multicast_groups {
2047c9e7a6fSAndy Grover 	TCMU_MCGRP_CONFIG,
2057c9e7a6fSAndy Grover };
2067c9e7a6fSAndy Grover 
2077c9e7a6fSAndy Grover static const struct genl_multicast_group tcmu_mcgrps[] = {
2087c9e7a6fSAndy Grover 	[TCMU_MCGRP_CONFIG] = { .name = "config", },
2097c9e7a6fSAndy Grover };
2107c9e7a6fSAndy Grover 
211b3af66e2SMike Christie static struct nla_policy tcmu_attr_policy[TCMU_ATTR_MAX+1] = {
212b3af66e2SMike Christie 	[TCMU_ATTR_DEVICE]	= { .type = NLA_STRING },
213b3af66e2SMike Christie 	[TCMU_ATTR_MINOR]	= { .type = NLA_U32 },
214b3af66e2SMike Christie 	[TCMU_ATTR_CMD_STATUS]	= { .type = NLA_S32 },
215b3af66e2SMike Christie 	[TCMU_ATTR_DEVICE_ID]	= { .type = NLA_U32 },
216b3af66e2SMike Christie 	[TCMU_ATTR_SUPP_KERN_CMD_REPLY] = { .type = NLA_U8 },
217b3af66e2SMike Christie };
218b3af66e2SMike Christie 
219b3af66e2SMike Christie static int tcmu_genl_cmd_done(struct genl_info *info, int completed_cmd)
220b3af66e2SMike Christie {
221b3af66e2SMike Christie 	struct se_device *dev;
222b3af66e2SMike Christie 	struct tcmu_dev *udev;
223b3af66e2SMike Christie 	struct tcmu_nl_cmd *nl_cmd;
224b3af66e2SMike Christie 	int dev_id, rc, ret = 0;
225b3af66e2SMike Christie 	bool is_removed = (completed_cmd == TCMU_CMD_REMOVED_DEVICE);
226b3af66e2SMike Christie 
227b3af66e2SMike Christie 	if (!info->attrs[TCMU_ATTR_CMD_STATUS] ||
228b3af66e2SMike Christie 	    !info->attrs[TCMU_ATTR_DEVICE_ID]) {
229b3af66e2SMike Christie 		printk(KERN_ERR "TCMU_ATTR_CMD_STATUS or TCMU_ATTR_DEVICE_ID not set, doing nothing\n");
230b3af66e2SMike Christie                 return -EINVAL;
231b3af66e2SMike Christie         }
232b3af66e2SMike Christie 
233b3af66e2SMike Christie 	dev_id = nla_get_u32(info->attrs[TCMU_ATTR_DEVICE_ID]);
234b3af66e2SMike Christie 	rc = nla_get_s32(info->attrs[TCMU_ATTR_CMD_STATUS]);
235b3af66e2SMike Christie 
236b3af66e2SMike Christie 	dev = target_find_device(dev_id, !is_removed);
237b3af66e2SMike Christie 	if (!dev) {
238b3af66e2SMike Christie 		printk(KERN_ERR "tcmu nl cmd %u/%u completion could not find device with dev id %u.\n",
239b3af66e2SMike Christie 		       completed_cmd, rc, dev_id);
240b3af66e2SMike Christie 		return -ENODEV;
241b3af66e2SMike Christie 	}
242b3af66e2SMike Christie 	udev = TCMU_DEV(dev);
243b3af66e2SMike Christie 
244b3af66e2SMike Christie 	spin_lock(&udev->nl_cmd_lock);
245b3af66e2SMike Christie 	nl_cmd = &udev->curr_nl_cmd;
246b3af66e2SMike Christie 
247b3af66e2SMike Christie 	pr_debug("genl cmd done got id %d curr %d done %d rc %d\n", dev_id,
248b3af66e2SMike Christie 		 nl_cmd->cmd, completed_cmd, rc);
249b3af66e2SMike Christie 
250b3af66e2SMike Christie 	if (nl_cmd->cmd != completed_cmd) {
251b3af66e2SMike Christie 		printk(KERN_ERR "Mismatched commands (Expecting reply for %d. Current %d).\n",
252b3af66e2SMike Christie 		       completed_cmd, nl_cmd->cmd);
253b3af66e2SMike Christie 		ret = -EINVAL;
254b3af66e2SMike Christie 	} else {
255b3af66e2SMike Christie 		nl_cmd->status = rc;
256b3af66e2SMike Christie 	}
257b3af66e2SMike Christie 
258b3af66e2SMike Christie 	spin_unlock(&udev->nl_cmd_lock);
259b3af66e2SMike Christie 	if (!is_removed)
260b3af66e2SMike Christie 		 target_undepend_item(&dev->dev_group.cg_item);
261b3af66e2SMike Christie 	if (!ret)
262b3af66e2SMike Christie 		complete(&nl_cmd->complete);
263b3af66e2SMike Christie 	return ret;
264b3af66e2SMike Christie }
265b3af66e2SMike Christie 
266b3af66e2SMike Christie static int tcmu_genl_rm_dev_done(struct sk_buff *skb, struct genl_info *info)
267b3af66e2SMike Christie {
268b3af66e2SMike Christie 	return tcmu_genl_cmd_done(info, TCMU_CMD_REMOVED_DEVICE);
269b3af66e2SMike Christie }
270b3af66e2SMike Christie 
271b3af66e2SMike Christie static int tcmu_genl_add_dev_done(struct sk_buff *skb, struct genl_info *info)
272b3af66e2SMike Christie {
273b3af66e2SMike Christie 	return tcmu_genl_cmd_done(info, TCMU_CMD_ADDED_DEVICE);
274b3af66e2SMike Christie }
275b3af66e2SMike Christie 
276b3af66e2SMike Christie static int tcmu_genl_reconfig_dev_done(struct sk_buff *skb,
277b3af66e2SMike Christie 				       struct genl_info *info)
278b3af66e2SMike Christie {
279b3af66e2SMike Christie 	return tcmu_genl_cmd_done(info, TCMU_CMD_RECONFIG_DEVICE);
280b3af66e2SMike Christie }
281b3af66e2SMike Christie 
282b3af66e2SMike Christie static int tcmu_genl_set_features(struct sk_buff *skb, struct genl_info *info)
283b3af66e2SMike Christie {
284b3af66e2SMike Christie 	if (info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]) {
285b3af66e2SMike Christie 		tcmu_kern_cmd_reply_supported  =
286b3af66e2SMike Christie 			nla_get_u8(info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]);
287b3af66e2SMike Christie 		printk(KERN_INFO "tcmu daemon: command reply support %u.\n",
288b3af66e2SMike Christie 		       tcmu_kern_cmd_reply_supported);
289b3af66e2SMike Christie 	}
290b3af66e2SMike Christie 
291b3af66e2SMike Christie 	return 0;
292b3af66e2SMike Christie }
293b3af66e2SMike Christie 
294b3af66e2SMike Christie static const struct genl_ops tcmu_genl_ops[] = {
295b3af66e2SMike Christie 	{
296b3af66e2SMike Christie 		.cmd	= TCMU_CMD_SET_FEATURES,
297b3af66e2SMike Christie 		.flags	= GENL_ADMIN_PERM,
298b3af66e2SMike Christie 		.policy	= tcmu_attr_policy,
299b3af66e2SMike Christie 		.doit	= tcmu_genl_set_features,
300b3af66e2SMike Christie 	},
301b3af66e2SMike Christie 	{
302b3af66e2SMike Christie 		.cmd	= TCMU_CMD_ADDED_DEVICE_DONE,
303b3af66e2SMike Christie 		.flags	= GENL_ADMIN_PERM,
304b3af66e2SMike Christie 		.policy	= tcmu_attr_policy,
305b3af66e2SMike Christie 		.doit	= tcmu_genl_add_dev_done,
306b3af66e2SMike Christie 	},
307b3af66e2SMike Christie 	{
308b3af66e2SMike Christie 		.cmd	= TCMU_CMD_REMOVED_DEVICE_DONE,
309b3af66e2SMike Christie 		.flags	= GENL_ADMIN_PERM,
310b3af66e2SMike Christie 		.policy	= tcmu_attr_policy,
311b3af66e2SMike Christie 		.doit	= tcmu_genl_rm_dev_done,
312b3af66e2SMike Christie 	},
313b3af66e2SMike Christie 	{
314b3af66e2SMike Christie 		.cmd	= TCMU_CMD_RECONFIG_DEVICE_DONE,
315b3af66e2SMike Christie 		.flags	= GENL_ADMIN_PERM,
316b3af66e2SMike Christie 		.policy	= tcmu_attr_policy,
317b3af66e2SMike Christie 		.doit	= tcmu_genl_reconfig_dev_done,
318b3af66e2SMike Christie 	},
319b3af66e2SMike Christie };
320b3af66e2SMike Christie 
3217c9e7a6fSAndy Grover /* Our generic netlink family */
32256989f6dSJohannes Berg static struct genl_family tcmu_genl_family __ro_after_init = {
323489111e5SJohannes Berg 	.module = THIS_MODULE,
3247c9e7a6fSAndy Grover 	.hdrsize = 0,
3257c9e7a6fSAndy Grover 	.name = "TCM-USER",
326b3af66e2SMike Christie 	.version = 2,
3277c9e7a6fSAndy Grover 	.maxattr = TCMU_ATTR_MAX,
3287c9e7a6fSAndy Grover 	.mcgrps = tcmu_mcgrps,
3297c9e7a6fSAndy Grover 	.n_mcgrps = ARRAY_SIZE(tcmu_mcgrps),
33020c08b36SSheng Yang 	.netnsok = true,
331b3af66e2SMike Christie 	.ops = tcmu_genl_ops,
332b3af66e2SMike Christie 	.n_ops = ARRAY_SIZE(tcmu_genl_ops),
3337c9e7a6fSAndy Grover };
3347c9e7a6fSAndy Grover 
335141685a3SXiubo Li #define tcmu_cmd_set_dbi_cur(cmd, index) ((cmd)->dbi_cur = (index))
336141685a3SXiubo Li #define tcmu_cmd_reset_dbi_cur(cmd) tcmu_cmd_set_dbi_cur(cmd, 0)
337141685a3SXiubo Li #define tcmu_cmd_set_dbi(cmd, index) ((cmd)->dbi[(cmd)->dbi_cur++] = (index))
338141685a3SXiubo Li #define tcmu_cmd_get_dbi(cmd) ((cmd)->dbi[(cmd)->dbi_cur++])
339141685a3SXiubo Li 
340b6df4b79SXiubo Li static void tcmu_cmd_free_data(struct tcmu_cmd *tcmu_cmd, uint32_t len)
341141685a3SXiubo Li {
342141685a3SXiubo Li 	struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
343141685a3SXiubo Li 	uint32_t i;
344141685a3SXiubo Li 
345b6df4b79SXiubo Li 	for (i = 0; i < len; i++)
346141685a3SXiubo Li 		clear_bit(tcmu_cmd->dbi[i], udev->data_bitmap);
347141685a3SXiubo Li }
348141685a3SXiubo Li 
349b6df4b79SXiubo Li static inline bool tcmu_get_empty_block(struct tcmu_dev *udev,
350b6df4b79SXiubo Li 					struct tcmu_cmd *tcmu_cmd)
351141685a3SXiubo Li {
352b6df4b79SXiubo Li 	struct page *page;
353b6df4b79SXiubo Li 	int ret, dbi;
354141685a3SXiubo Li 
355b6df4b79SXiubo Li 	dbi = find_first_zero_bit(udev->data_bitmap, udev->dbi_thresh);
356b6df4b79SXiubo Li 	if (dbi == udev->dbi_thresh)
357b6df4b79SXiubo Li 		return false;
358b6df4b79SXiubo Li 
359b6df4b79SXiubo Li 	page = radix_tree_lookup(&udev->data_blocks, dbi);
360b6df4b79SXiubo Li 	if (!page) {
361b6df4b79SXiubo Li 		if (atomic_add_return(1, &global_db_count) >
362af1dd7ffSMike Christie 					TCMU_GLOBAL_MAX_BLOCKS)
363af1dd7ffSMike Christie 			schedule_delayed_work(&tcmu_unmap_work, 0);
364b6df4b79SXiubo Li 
365b6df4b79SXiubo Li 		/* try to get new page from the mm */
366b6df4b79SXiubo Li 		page = alloc_page(GFP_KERNEL);
367b6df4b79SXiubo Li 		if (!page)
368daf78c30SXiubo Li 			goto err_alloc;
369b6df4b79SXiubo Li 
370b6df4b79SXiubo Li 		ret = radix_tree_insert(&udev->data_blocks, dbi, page);
371daf78c30SXiubo Li 		if (ret)
372daf78c30SXiubo Li 			goto err_insert;
373b6df4b79SXiubo Li 	}
374b6df4b79SXiubo Li 
375141685a3SXiubo Li 	if (dbi > udev->dbi_max)
376141685a3SXiubo Li 		udev->dbi_max = dbi;
377141685a3SXiubo Li 
378141685a3SXiubo Li 	set_bit(dbi, udev->data_bitmap);
379b6df4b79SXiubo Li 	tcmu_cmd_set_dbi(tcmu_cmd, dbi);
380141685a3SXiubo Li 
381b6df4b79SXiubo Li 	return true;
382daf78c30SXiubo Li err_insert:
383daf78c30SXiubo Li 	__free_page(page);
384daf78c30SXiubo Li err_alloc:
385daf78c30SXiubo Li 	atomic_dec(&global_db_count);
386daf78c30SXiubo Li 	return false;
387141685a3SXiubo Li }
388141685a3SXiubo Li 
389b6df4b79SXiubo Li static bool tcmu_get_empty_blocks(struct tcmu_dev *udev,
390b6df4b79SXiubo Li 				  struct tcmu_cmd *tcmu_cmd)
391b6df4b79SXiubo Li {
392b6df4b79SXiubo Li 	int i;
393b6df4b79SXiubo Li 
394b6df4b79SXiubo Li 	for (i = tcmu_cmd->dbi_cur; i < tcmu_cmd->dbi_cnt; i++) {
395b6df4b79SXiubo Li 		if (!tcmu_get_empty_block(udev, tcmu_cmd))
396af1dd7ffSMike Christie 			return false;
397141685a3SXiubo Li 	}
398b6df4b79SXiubo Li 	return true;
399141685a3SXiubo Li }
400141685a3SXiubo Li 
401b6df4b79SXiubo Li static inline struct page *
402b6df4b79SXiubo Li tcmu_get_block_page(struct tcmu_dev *udev, uint32_t dbi)
403141685a3SXiubo Li {
404141685a3SXiubo Li 	return radix_tree_lookup(&udev->data_blocks, dbi);
405141685a3SXiubo Li }
406141685a3SXiubo Li 
407141685a3SXiubo Li static inline void tcmu_free_cmd(struct tcmu_cmd *tcmu_cmd)
408141685a3SXiubo Li {
409141685a3SXiubo Li 	kfree(tcmu_cmd->dbi);
410141685a3SXiubo Li 	kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
411141685a3SXiubo Li }
412141685a3SXiubo Li 
413141685a3SXiubo Li static inline size_t tcmu_cmd_get_data_length(struct tcmu_cmd *tcmu_cmd)
414141685a3SXiubo Li {
415141685a3SXiubo Li 	struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
416141685a3SXiubo Li 	size_t data_length = round_up(se_cmd->data_length, DATA_BLOCK_SIZE);
417141685a3SXiubo Li 
418141685a3SXiubo Li 	if (se_cmd->se_cmd_flags & SCF_BIDI) {
419141685a3SXiubo Li 		BUG_ON(!(se_cmd->t_bidi_data_sg && se_cmd->t_bidi_data_nents));
420141685a3SXiubo Li 		data_length += round_up(se_cmd->t_bidi_data_sg->length,
421141685a3SXiubo Li 				DATA_BLOCK_SIZE);
422141685a3SXiubo Li 	}
423141685a3SXiubo Li 
424141685a3SXiubo Li 	return data_length;
425141685a3SXiubo Li }
426141685a3SXiubo Li 
427141685a3SXiubo Li static inline uint32_t tcmu_cmd_get_block_cnt(struct tcmu_cmd *tcmu_cmd)
428141685a3SXiubo Li {
429141685a3SXiubo Li 	size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd);
430141685a3SXiubo Li 
431141685a3SXiubo Li 	return data_length / DATA_BLOCK_SIZE;
432141685a3SXiubo Li }
433141685a3SXiubo Li 
4347c9e7a6fSAndy Grover static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd)
4357c9e7a6fSAndy Grover {
4367c9e7a6fSAndy Grover 	struct se_device *se_dev = se_cmd->se_dev;
4377c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
4387c9e7a6fSAndy Grover 	struct tcmu_cmd *tcmu_cmd;
4397c9e7a6fSAndy Grover 
4407c9e7a6fSAndy Grover 	tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_KERNEL);
4417c9e7a6fSAndy Grover 	if (!tcmu_cmd)
4427c9e7a6fSAndy Grover 		return NULL;
4437c9e7a6fSAndy Grover 
444af1dd7ffSMike Christie 	INIT_LIST_HEAD(&tcmu_cmd->cmdr_queue_entry);
4457c9e7a6fSAndy Grover 	tcmu_cmd->se_cmd = se_cmd;
4467c9e7a6fSAndy Grover 	tcmu_cmd->tcmu_dev = udev;
4477c9e7a6fSAndy Grover 
448141685a3SXiubo Li 	tcmu_cmd_reset_dbi_cur(tcmu_cmd);
449141685a3SXiubo Li 	tcmu_cmd->dbi_cnt = tcmu_cmd_get_block_cnt(tcmu_cmd);
450141685a3SXiubo Li 	tcmu_cmd->dbi = kcalloc(tcmu_cmd->dbi_cnt, sizeof(uint32_t),
451141685a3SXiubo Li 				GFP_KERNEL);
452141685a3SXiubo Li 	if (!tcmu_cmd->dbi) {
453141685a3SXiubo Li 		kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
454141685a3SXiubo Li 		return NULL;
455141685a3SXiubo Li 	}
456141685a3SXiubo Li 
4577c9e7a6fSAndy Grover 	return tcmu_cmd;
4587c9e7a6fSAndy Grover }
4597c9e7a6fSAndy Grover 
4607c9e7a6fSAndy Grover static inline void tcmu_flush_dcache_range(void *vaddr, size_t size)
4617c9e7a6fSAndy Grover {
462b75d8063SGeliang Tang 	unsigned long offset = offset_in_page(vaddr);
46326d2b310Stangwenji 	void *start = vaddr - offset;
4647c9e7a6fSAndy Grover 
4657c9e7a6fSAndy Grover 	size = round_up(size+offset, PAGE_SIZE);
4667c9e7a6fSAndy Grover 
4677c9e7a6fSAndy Grover 	while (size) {
46826d2b310Stangwenji 		flush_dcache_page(virt_to_page(start));
46926d2b310Stangwenji 		start += PAGE_SIZE;
4707c9e7a6fSAndy Grover 		size -= PAGE_SIZE;
4717c9e7a6fSAndy Grover 	}
4727c9e7a6fSAndy Grover }
4737c9e7a6fSAndy Grover 
4747c9e7a6fSAndy Grover /*
4757c9e7a6fSAndy Grover  * Some ring helper functions. We don't assume size is a power of 2 so
4767c9e7a6fSAndy Grover  * we can't use circ_buf.h.
4777c9e7a6fSAndy Grover  */
4787c9e7a6fSAndy Grover static inline size_t spc_used(size_t head, size_t tail, size_t size)
4797c9e7a6fSAndy Grover {
4807c9e7a6fSAndy Grover 	int diff = head - tail;
4817c9e7a6fSAndy Grover 
4827c9e7a6fSAndy Grover 	if (diff >= 0)
4837c9e7a6fSAndy Grover 		return diff;
4847c9e7a6fSAndy Grover 	else
4857c9e7a6fSAndy Grover 		return size + diff;
4867c9e7a6fSAndy Grover }
4877c9e7a6fSAndy Grover 
4887c9e7a6fSAndy Grover static inline size_t spc_free(size_t head, size_t tail, size_t size)
4897c9e7a6fSAndy Grover {
4907c9e7a6fSAndy Grover 	/* Keep 1 byte unused or we can't tell full from empty */
4917c9e7a6fSAndy Grover 	return (size - spc_used(head, tail, size) - 1);
4927c9e7a6fSAndy Grover }
4937c9e7a6fSAndy Grover 
4947c9e7a6fSAndy Grover static inline size_t head_to_end(size_t head, size_t size)
4957c9e7a6fSAndy Grover {
4967c9e7a6fSAndy Grover 	return size - head;
4977c9e7a6fSAndy Grover }
4987c9e7a6fSAndy Grover 
4993e609135SXiubo Li static inline void new_iov(struct iovec **iov, int *iov_cnt)
500f1dbd087SSheng Yang {
501f1dbd087SSheng Yang 	struct iovec *iovec;
502f1dbd087SSheng Yang 
503f1dbd087SSheng Yang 	if (*iov_cnt != 0)
504f1dbd087SSheng Yang 		(*iov)++;
505f1dbd087SSheng Yang 	(*iov_cnt)++;
506f1dbd087SSheng Yang 
507f1dbd087SSheng Yang 	iovec = *iov;
508f1dbd087SSheng Yang 	memset(iovec, 0, sizeof(struct iovec));
509f1dbd087SSheng Yang }
510f1dbd087SSheng Yang 
5117c9e7a6fSAndy Grover #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size)
5127c9e7a6fSAndy Grover 
51326418649SSheng Yang /* offset is relative to mb_addr */
514141685a3SXiubo Li static inline size_t get_block_offset_user(struct tcmu_dev *dev,
515141685a3SXiubo Li 		int dbi, int remaining)
51626418649SSheng Yang {
517141685a3SXiubo Li 	return dev->data_off + dbi * DATA_BLOCK_SIZE +
51826418649SSheng Yang 		DATA_BLOCK_SIZE - remaining;
51926418649SSheng Yang }
52026418649SSheng Yang 
521daf78c30SXiubo Li static inline size_t iov_tail(struct iovec *iov)
52226418649SSheng Yang {
52326418649SSheng Yang 	return (size_t)iov->iov_base + iov->iov_len;
52426418649SSheng Yang }
52526418649SSheng Yang 
5261a1fc0b8SMike Christie static void scatter_data_area(struct tcmu_dev *udev,
527141685a3SXiubo Li 	struct tcmu_cmd *tcmu_cmd, struct scatterlist *data_sg,
528141685a3SXiubo Li 	unsigned int data_nents, struct iovec **iov,
529141685a3SXiubo Li 	int *iov_cnt, bool copy_data)
530f97ec7dbSIlias Tsitsimpis {
531141685a3SXiubo Li 	int i, dbi;
53226418649SSheng Yang 	int block_remaining = 0;
533141685a3SXiubo Li 	void *from, *to = NULL;
534141685a3SXiubo Li 	size_t copy_bytes, to_offset, offset;
535f97ec7dbSIlias Tsitsimpis 	struct scatterlist *sg;
536b6df4b79SXiubo Li 	struct page *page;
537f97ec7dbSIlias Tsitsimpis 
538f97ec7dbSIlias Tsitsimpis 	for_each_sg(data_sg, sg, data_nents, i) {
53926418649SSheng Yang 		int sg_remaining = sg->length;
540f97ec7dbSIlias Tsitsimpis 		from = kmap_atomic(sg_page(sg)) + sg->offset;
54126418649SSheng Yang 		while (sg_remaining > 0) {
54226418649SSheng Yang 			if (block_remaining == 0) {
543b6df4b79SXiubo Li 				if (to)
544b6df4b79SXiubo Li 					kunmap_atomic(to);
545b6df4b79SXiubo Li 
54626418649SSheng Yang 				block_remaining = DATA_BLOCK_SIZE;
547b6df4b79SXiubo Li 				dbi = tcmu_cmd_get_dbi(tcmu_cmd);
548b6df4b79SXiubo Li 				page = tcmu_get_block_page(udev, dbi);
549b6df4b79SXiubo Li 				to = kmap_atomic(page);
550141685a3SXiubo Li 			}
551141685a3SXiubo Li 
5523e609135SXiubo Li 			/*
5533e609135SXiubo Li 			 * Covert to virtual offset of the ring data area.
5543e609135SXiubo Li 			 */
555141685a3SXiubo Li 			to_offset = get_block_offset_user(udev, dbi,
55626418649SSheng Yang 					block_remaining);
557141685a3SXiubo Li 
5583e609135SXiubo Li 			/*
5593e609135SXiubo Li 			 * The following code will gather and map the blocks
5603e609135SXiubo Li 			 * to the same iovec when the blocks are all next to
5613e609135SXiubo Li 			 * each other.
5623e609135SXiubo Li 			 */
5633e609135SXiubo Li 			copy_bytes = min_t(size_t, sg_remaining,
5643e609135SXiubo Li 					block_remaining);
56526418649SSheng Yang 			if (*iov_cnt != 0 &&
566daf78c30SXiubo Li 			    to_offset == iov_tail(*iov)) {
5673e609135SXiubo Li 				/*
5683e609135SXiubo Li 				 * Will append to the current iovec, because
5693e609135SXiubo Li 				 * the current block page is next to the
5703e609135SXiubo Li 				 * previous one.
5713e609135SXiubo Li 				 */
572f1dbd087SSheng Yang 				(*iov)->iov_len += copy_bytes;
57326418649SSheng Yang 			} else {
5743e609135SXiubo Li 				/*
5753e609135SXiubo Li 				 * Will allocate a new iovec because we are
5763e609135SXiubo Li 				 * first time here or the current block page
5773e609135SXiubo Li 				 * is not next to the previous one.
5783e609135SXiubo Li 				 */
5793e609135SXiubo Li 				new_iov(iov, iov_cnt);
58026418649SSheng Yang 				(*iov)->iov_base = (void __user *)to_offset;
581f97ec7dbSIlias Tsitsimpis 				(*iov)->iov_len = copy_bytes;
58226418649SSheng Yang 			}
5833e609135SXiubo Li 
584f97ec7dbSIlias Tsitsimpis 			if (copy_data) {
585c542942cSXiubo Li 				offset = DATA_BLOCK_SIZE - block_remaining;
586c542942cSXiubo Li 				memcpy(to + offset,
587c542942cSXiubo Li 				       from + sg->length - sg_remaining,
58826418649SSheng Yang 				       copy_bytes);
589f97ec7dbSIlias Tsitsimpis 				tcmu_flush_dcache_range(to, copy_bytes);
590f97ec7dbSIlias Tsitsimpis 			}
5913e609135SXiubo Li 
59226418649SSheng Yang 			sg_remaining -= copy_bytes;
59326418649SSheng Yang 			block_remaining -= copy_bytes;
594f97ec7dbSIlias Tsitsimpis 		}
595e2e21bd8SSagi Grimberg 		kunmap_atomic(from - sg->offset);
596f97ec7dbSIlias Tsitsimpis 	}
5973e609135SXiubo Li 
598b6df4b79SXiubo Li 	if (to)
599b6df4b79SXiubo Li 		kunmap_atomic(to);
6000c28481fSSheng Yang }
6010c28481fSSheng Yang 
602a5d68ba8SXiubo Li static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
603a5d68ba8SXiubo Li 			     bool bidi)
604f97ec7dbSIlias Tsitsimpis {
605a5d68ba8SXiubo Li 	struct se_cmd *se_cmd = cmd->se_cmd;
606141685a3SXiubo Li 	int i, dbi;
60726418649SSheng Yang 	int block_remaining = 0;
608b6df4b79SXiubo Li 	void *from = NULL, *to;
609141685a3SXiubo Li 	size_t copy_bytes, offset;
610a5d68ba8SXiubo Li 	struct scatterlist *sg, *data_sg;
611b6df4b79SXiubo Li 	struct page *page;
612a5d68ba8SXiubo Li 	unsigned int data_nents;
613141685a3SXiubo Li 	uint32_t count = 0;
614a5d68ba8SXiubo Li 
615a5d68ba8SXiubo Li 	if (!bidi) {
616a5d68ba8SXiubo Li 		data_sg = se_cmd->t_data_sg;
617a5d68ba8SXiubo Li 		data_nents = se_cmd->t_data_nents;
618a5d68ba8SXiubo Li 	} else {
619a5d68ba8SXiubo Li 
620a5d68ba8SXiubo Li 		/*
621a5d68ba8SXiubo Li 		 * For bidi case, the first count blocks are for Data-Out
622a5d68ba8SXiubo Li 		 * buffer blocks, and before gathering the Data-In buffer
623a5d68ba8SXiubo Li 		 * the Data-Out buffer blocks should be discarded.
624a5d68ba8SXiubo Li 		 */
625a5d68ba8SXiubo Li 		count = DIV_ROUND_UP(se_cmd->data_length, DATA_BLOCK_SIZE);
626a5d68ba8SXiubo Li 
627a5d68ba8SXiubo Li 		data_sg = se_cmd->t_bidi_data_sg;
628a5d68ba8SXiubo Li 		data_nents = se_cmd->t_bidi_data_nents;
629a5d68ba8SXiubo Li 	}
630f97ec7dbSIlias Tsitsimpis 
631141685a3SXiubo Li 	tcmu_cmd_set_dbi_cur(cmd, count);
632141685a3SXiubo Li 
633f97ec7dbSIlias Tsitsimpis 	for_each_sg(data_sg, sg, data_nents, i) {
63426418649SSheng Yang 		int sg_remaining = sg->length;
635f97ec7dbSIlias Tsitsimpis 		to = kmap_atomic(sg_page(sg)) + sg->offset;
63626418649SSheng Yang 		while (sg_remaining > 0) {
63726418649SSheng Yang 			if (block_remaining == 0) {
638b6df4b79SXiubo Li 				if (from)
639b6df4b79SXiubo Li 					kunmap_atomic(from);
640b6df4b79SXiubo Li 
64126418649SSheng Yang 				block_remaining = DATA_BLOCK_SIZE;
642141685a3SXiubo Li 				dbi = tcmu_cmd_get_dbi(cmd);
643b6df4b79SXiubo Li 				page = tcmu_get_block_page(udev, dbi);
644b6df4b79SXiubo Li 				from = kmap_atomic(page);
64526418649SSheng Yang 			}
64626418649SSheng Yang 			copy_bytes = min_t(size_t, sg_remaining,
64726418649SSheng Yang 					block_remaining);
648141685a3SXiubo Li 			offset = DATA_BLOCK_SIZE - block_remaining;
649f97ec7dbSIlias Tsitsimpis 			tcmu_flush_dcache_range(from, copy_bytes);
650c542942cSXiubo Li 			memcpy(to + sg->length - sg_remaining, from + offset,
65126418649SSheng Yang 					copy_bytes);
652f97ec7dbSIlias Tsitsimpis 
65326418649SSheng Yang 			sg_remaining -= copy_bytes;
65426418649SSheng Yang 			block_remaining -= copy_bytes;
655f97ec7dbSIlias Tsitsimpis 		}
656e2e21bd8SSagi Grimberg 		kunmap_atomic(to - sg->offset);
657f97ec7dbSIlias Tsitsimpis 	}
658b6df4b79SXiubo Li 	if (from)
659b6df4b79SXiubo Li 		kunmap_atomic(from);
660f97ec7dbSIlias Tsitsimpis }
661f97ec7dbSIlias Tsitsimpis 
662b6df4b79SXiubo Li static inline size_t spc_bitmap_free(unsigned long *bitmap, uint32_t thresh)
66326418649SSheng Yang {
6643c0f26ffSMike Christie 	return thresh - bitmap_weight(bitmap, thresh);
66526418649SSheng Yang }
66626418649SSheng Yang 
6677c9e7a6fSAndy Grover /*
668f97ec7dbSIlias Tsitsimpis  * We can't queue a command until we have space available on the cmd ring *and*
6693d9b9555SAndy Grover  * space available on the data area.
6707c9e7a6fSAndy Grover  *
6717c9e7a6fSAndy Grover  * Called with ring lock held.
6727c9e7a6fSAndy Grover  */
673b6df4b79SXiubo Li static bool is_ring_space_avail(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
674b6df4b79SXiubo Li 		size_t cmd_size, size_t data_needed)
6757c9e7a6fSAndy Grover {
6767c9e7a6fSAndy Grover 	struct tcmu_mailbox *mb = udev->mb_addr;
677b6df4b79SXiubo Li 	uint32_t blocks_needed = (data_needed + DATA_BLOCK_SIZE - 1)
678b6df4b79SXiubo Li 				/ DATA_BLOCK_SIZE;
6790241fd39SNicholas Bellinger 	size_t space, cmd_needed;
6807c9e7a6fSAndy Grover 	u32 cmd_head;
6817c9e7a6fSAndy Grover 
6827c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(mb, sizeof(*mb));
6837c9e7a6fSAndy Grover 
6847c9e7a6fSAndy Grover 	cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
6857c9e7a6fSAndy Grover 
686f56574a2SAndy Grover 	/*
687f56574a2SAndy Grover 	 * If cmd end-of-ring space is too small then we need space for a NOP plus
688f56574a2SAndy Grover 	 * original cmd - cmds are internally contiguous.
689f56574a2SAndy Grover 	 */
690f56574a2SAndy Grover 	if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size)
691f56574a2SAndy Grover 		cmd_needed = cmd_size;
692f56574a2SAndy Grover 	else
693f56574a2SAndy Grover 		cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size);
694f56574a2SAndy Grover 
6957c9e7a6fSAndy Grover 	space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size);
6967c9e7a6fSAndy Grover 	if (space < cmd_needed) {
6977c9e7a6fSAndy Grover 		pr_debug("no cmd space: %u %u %u\n", cmd_head,
6987c9e7a6fSAndy Grover 		       udev->cmdr_last_cleaned, udev->cmdr_size);
6997c9e7a6fSAndy Grover 		return false;
7007c9e7a6fSAndy Grover 	}
7017c9e7a6fSAndy Grover 
702b6df4b79SXiubo Li 	/* try to check and get the data blocks as needed */
703b6df4b79SXiubo Li 	space = spc_bitmap_free(udev->data_bitmap, udev->dbi_thresh);
7043c0f26ffSMike Christie 	if ((space * DATA_BLOCK_SIZE) < data_needed) {
7053c0f26ffSMike Christie 		unsigned long blocks_left = DATA_BLOCK_BITS - udev->dbi_thresh +
7063c0f26ffSMike Christie 						space;
707b6df4b79SXiubo Li 
708b6df4b79SXiubo Li 		if (blocks_left < blocks_needed) {
709b6df4b79SXiubo Li 			pr_debug("no data space: only %lu available, but ask for %zu\n",
710b6df4b79SXiubo Li 					blocks_left * DATA_BLOCK_SIZE,
711b6df4b79SXiubo Li 					data_needed);
7127c9e7a6fSAndy Grover 			return false;
7137c9e7a6fSAndy Grover 		}
7147c9e7a6fSAndy Grover 
715f890f579SMike Christie 		udev->dbi_thresh += blocks_needed;
716b6df4b79SXiubo Li 		if (udev->dbi_thresh > DATA_BLOCK_BITS)
717b6df4b79SXiubo Li 			udev->dbi_thresh = DATA_BLOCK_BITS;
718b6df4b79SXiubo Li 	}
719b6df4b79SXiubo Li 
720daf78c30SXiubo Li 	return tcmu_get_empty_blocks(udev, cmd);
7217c9e7a6fSAndy Grover }
7227c9e7a6fSAndy Grover 
723fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_base_cmd_size(size_t iov_cnt)
724fe25cc34SXiubo Li {
725fe25cc34SXiubo Li 	return max(offsetof(struct tcmu_cmd_entry, req.iov[iov_cnt]),
726fe25cc34SXiubo Li 			sizeof(struct tcmu_cmd_entry));
727fe25cc34SXiubo Li }
728fe25cc34SXiubo Li 
729fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_cmd_size(struct tcmu_cmd *tcmu_cmd,
730fe25cc34SXiubo Li 					   size_t base_command_size)
731fe25cc34SXiubo Li {
732fe25cc34SXiubo Li 	struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
733fe25cc34SXiubo Li 	size_t command_size;
734fe25cc34SXiubo Li 
735fe25cc34SXiubo Li 	command_size = base_command_size +
736fe25cc34SXiubo Li 		round_up(scsi_command_size(se_cmd->t_task_cdb),
737fe25cc34SXiubo Li 				TCMU_OP_ALIGN_SIZE);
738fe25cc34SXiubo Li 
739fe25cc34SXiubo Li 	WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1));
740fe25cc34SXiubo Li 
741fe25cc34SXiubo Li 	return command_size;
742fe25cc34SXiubo Li }
743fe25cc34SXiubo Li 
7440d44374cSMike Christie static int tcmu_setup_cmd_timer(struct tcmu_cmd *tcmu_cmd)
7450d44374cSMike Christie {
7460d44374cSMike Christie 	struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
7470d44374cSMike Christie 	unsigned long tmo = udev->cmd_time_out;
7480d44374cSMike Christie 	int cmd_id;
7490d44374cSMike Christie 
750af1dd7ffSMike Christie 	/*
751af1dd7ffSMike Christie 	 * If it was on the cmdr queue waiting we do not reset the timer
752af1dd7ffSMike Christie 	 * for requeues and when it is finally sent to userspace.
753af1dd7ffSMike Christie 	 */
7540d44374cSMike Christie 	if (tcmu_cmd->cmd_id)
7550d44374cSMike Christie 		return 0;
7560d44374cSMike Christie 
7570d44374cSMike Christie 	cmd_id = idr_alloc(&udev->commands, tcmu_cmd, 1, USHRT_MAX, GFP_NOWAIT);
7580d44374cSMike Christie 	if (cmd_id < 0) {
7590d44374cSMike Christie 		pr_err("tcmu: Could not allocate cmd id.\n");
7600d44374cSMike Christie 		return cmd_id;
7610d44374cSMike Christie 	}
7620d44374cSMike Christie 	tcmu_cmd->cmd_id = cmd_id;
7630d44374cSMike Christie 
7640d44374cSMike Christie 	if (!tmo)
765af1dd7ffSMike Christie 		tmo = TCMU_TIME_OUT;
766af1dd7ffSMike Christie 
767af1dd7ffSMike Christie 	pr_debug("allocated cmd %u for dev %s tmo %lu\n", tcmu_cmd->cmd_id,
768af1dd7ffSMike Christie 		 udev->name, tmo / MSEC_PER_SEC);
7690d44374cSMike Christie 
7700d44374cSMike Christie 	tcmu_cmd->deadline = round_jiffies_up(jiffies + msecs_to_jiffies(tmo));
7710d44374cSMike Christie 	mod_timer(&udev->timeout, tcmu_cmd->deadline);
7720d44374cSMike Christie 	return 0;
7730d44374cSMike Christie }
7740d44374cSMike Christie 
775af1dd7ffSMike Christie static int add_to_cmdr_queue(struct tcmu_cmd *tcmu_cmd)
776af1dd7ffSMike Christie {
777af1dd7ffSMike Christie 	struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
778af1dd7ffSMike Christie 	int ret;
779af1dd7ffSMike Christie 
780af1dd7ffSMike Christie 	ret = tcmu_setup_cmd_timer(tcmu_cmd);
781af1dd7ffSMike Christie 	if (ret)
782af1dd7ffSMike Christie 		return ret;
783af1dd7ffSMike Christie 
784af1dd7ffSMike Christie 	list_add_tail(&tcmu_cmd->cmdr_queue_entry, &udev->cmdr_queue);
785af1dd7ffSMike Christie 	pr_debug("adding cmd %u on dev %s to ring space wait queue\n",
786af1dd7ffSMike Christie 		 tcmu_cmd->cmd_id, udev->name);
787af1dd7ffSMike Christie 	return 0;
788af1dd7ffSMike Christie }
789af1dd7ffSMike Christie 
7906fd0ce79SMike Christie /**
7916fd0ce79SMike Christie  * queue_cmd_ring - queue cmd to ring or internally
7926fd0ce79SMike Christie  * @tcmu_cmd: cmd to queue
7936fd0ce79SMike Christie  * @scsi_err: TCM error code if failure (-1) returned.
7946fd0ce79SMike Christie  *
7956fd0ce79SMike Christie  * Returns:
7966fd0ce79SMike Christie  * -1 we cannot queue internally or to the ring.
7976fd0ce79SMike Christie  *  0 success
798af1dd7ffSMike Christie  *  1 internally queued to wait for ring memory to free.
7996fd0ce79SMike Christie  */
8006fd0ce79SMike Christie static sense_reason_t queue_cmd_ring(struct tcmu_cmd *tcmu_cmd, int *scsi_err)
8017c9e7a6fSAndy Grover {
8027c9e7a6fSAndy Grover 	struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
8037c9e7a6fSAndy Grover 	struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
8047c9e7a6fSAndy Grover 	size_t base_command_size, command_size;
8057c9e7a6fSAndy Grover 	struct tcmu_mailbox *mb;
8067c9e7a6fSAndy Grover 	struct tcmu_cmd_entry *entry;
8077c9e7a6fSAndy Grover 	struct iovec *iov;
808141685a3SXiubo Li 	int iov_cnt, ret;
8097c9e7a6fSAndy Grover 	uint32_t cmd_head;
8107c9e7a6fSAndy Grover 	uint64_t cdb_off;
811f97ec7dbSIlias Tsitsimpis 	bool copy_to_data_area;
812ab22d260SXiubo Li 	size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd);
8137c9e7a6fSAndy Grover 
8146fd0ce79SMike Christie 	*scsi_err = TCM_NO_SENSE;
8156fd0ce79SMike Christie 
8166fd0ce79SMike Christie 	if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) {
8176fd0ce79SMike Christie 		*scsi_err = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
8186fd0ce79SMike Christie 		return -1;
8196fd0ce79SMike Christie 	}
8207c9e7a6fSAndy Grover 
8217c9e7a6fSAndy Grover 	/*
8227c9e7a6fSAndy Grover 	 * Must be a certain minimum size for response sense info, but
8237c9e7a6fSAndy Grover 	 * also may be larger if the iov array is large.
8247c9e7a6fSAndy Grover 	 *
825fe25cc34SXiubo Li 	 * We prepare as many iovs as possbile for potential uses here,
826fe25cc34SXiubo Li 	 * because it's expensive to tell how many regions are freed in
827fe25cc34SXiubo Li 	 * the bitmap & global data pool, as the size calculated here
828fe25cc34SXiubo Li 	 * will only be used to do the checks.
829fe25cc34SXiubo Li 	 *
830fe25cc34SXiubo Li 	 * The size will be recalculated later as actually needed to save
831fe25cc34SXiubo Li 	 * cmd area memories.
8327c9e7a6fSAndy Grover 	 */
833fe25cc34SXiubo Li 	base_command_size = tcmu_cmd_get_base_cmd_size(tcmu_cmd->dbi_cnt);
834fe25cc34SXiubo Li 	command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size);
8357c9e7a6fSAndy Grover 
836af1dd7ffSMike Christie 	if (!list_empty(&udev->cmdr_queue))
837af1dd7ffSMike Christie 		goto queue;
8387c9e7a6fSAndy Grover 
8397c9e7a6fSAndy Grover 	mb = udev->mb_addr;
8407c9e7a6fSAndy Grover 	cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
841554617b2SAndy Grover 	if ((command_size > (udev->cmdr_size / 2)) ||
842554617b2SAndy Grover 	    data_length > udev->data_size) {
843554617b2SAndy Grover 		pr_warn("TCMU: Request of size %zu/%zu is too big for %u/%zu "
8443d9b9555SAndy Grover 			"cmd ring/data area\n", command_size, data_length,
8457c9e7a6fSAndy Grover 			udev->cmdr_size, udev->data_size);
8466fd0ce79SMike Christie 		*scsi_err = TCM_INVALID_CDB_FIELD;
8476fd0ce79SMike Christie 		return -1;
848554617b2SAndy Grover 	}
8497c9e7a6fSAndy Grover 
850af1dd7ffSMike Christie 	if (!is_ring_space_avail(udev, tcmu_cmd, command_size, data_length)) {
851810b8153SMike Christie 		/*
852810b8153SMike Christie 		 * Don't leave commands partially setup because the unmap
853810b8153SMike Christie 		 * thread might need the blocks to make forward progress.
854810b8153SMike Christie 		 */
855810b8153SMike Christie 		tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cur);
856810b8153SMike Christie 		tcmu_cmd_reset_dbi_cur(tcmu_cmd);
857af1dd7ffSMike Christie 		goto queue;
8587c9e7a6fSAndy Grover 	}
8597c9e7a6fSAndy Grover 
860f56574a2SAndy Grover 	/* Insert a PAD if end-of-ring space is too small */
861f56574a2SAndy Grover 	if (head_to_end(cmd_head, udev->cmdr_size) < command_size) {
862f56574a2SAndy Grover 		size_t pad_size = head_to_end(cmd_head, udev->cmdr_size);
863f56574a2SAndy Grover 
8647c9e7a6fSAndy Grover 		entry = (void *) mb + CMDR_OFF + cmd_head;
8650ad46af8SAndy Grover 		tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_PAD);
8660ad46af8SAndy Grover 		tcmu_hdr_set_len(&entry->hdr.len_op, pad_size);
8670ad46af8SAndy Grover 		entry->hdr.cmd_id = 0; /* not used for PAD */
8680ad46af8SAndy Grover 		entry->hdr.kflags = 0;
8690ad46af8SAndy Grover 		entry->hdr.uflags = 0;
8709d62bc0eSXiubo Li 		tcmu_flush_dcache_range(entry, sizeof(*entry));
8717c9e7a6fSAndy Grover 
8727c9e7a6fSAndy Grover 		UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size);
8739d62bc0eSXiubo Li 		tcmu_flush_dcache_range(mb, sizeof(*mb));
8747c9e7a6fSAndy Grover 
8757c9e7a6fSAndy Grover 		cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
8767c9e7a6fSAndy Grover 		WARN_ON(cmd_head != 0);
8777c9e7a6fSAndy Grover 	}
8787c9e7a6fSAndy Grover 
8797c9e7a6fSAndy Grover 	entry = (void *) mb + CMDR_OFF + cmd_head;
880b3743c71SXiubo Li 	memset(entry, 0, command_size);
8810ad46af8SAndy Grover 	tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD);
8827c9e7a6fSAndy Grover 
8833d9b9555SAndy Grover 	/* Handle allocating space from the data area */
884b6df4b79SXiubo Li 	tcmu_cmd_reset_dbi_cur(tcmu_cmd);
8857c9e7a6fSAndy Grover 	iov = &entry->req.iov[0];
886f97ec7dbSIlias Tsitsimpis 	iov_cnt = 0;
887e4648b01SIlias Tsitsimpis 	copy_to_data_area = (se_cmd->data_direction == DMA_TO_DEVICE
888e4648b01SIlias Tsitsimpis 		|| se_cmd->se_cmd_flags & SCF_BIDI);
8891a1fc0b8SMike Christie 	scatter_data_area(udev, tcmu_cmd, se_cmd->t_data_sg,
890b6df4b79SXiubo Li 			  se_cmd->t_data_nents, &iov, &iov_cnt,
891b6df4b79SXiubo Li 			  copy_to_data_area);
8927c9e7a6fSAndy Grover 	entry->req.iov_cnt = iov_cnt;
8937c9e7a6fSAndy Grover 
894e4648b01SIlias Tsitsimpis 	/* Handle BIDI commands */
895e4648b01SIlias Tsitsimpis 	iov_cnt = 0;
896b3743c71SXiubo Li 	if (se_cmd->se_cmd_flags & SCF_BIDI) {
897ab22d260SXiubo Li 		iov++;
8981a1fc0b8SMike Christie 		scatter_data_area(udev, tcmu_cmd, se_cmd->t_bidi_data_sg,
8991a1fc0b8SMike Christie 				  se_cmd->t_bidi_data_nents, &iov, &iov_cnt,
9001a1fc0b8SMike Christie 				  false);
901ab22d260SXiubo Li 	}
902b3743c71SXiubo Li 	entry->req.iov_bidi_cnt = iov_cnt;
90326418649SSheng Yang 
9040d44374cSMike Christie 	ret = tcmu_setup_cmd_timer(tcmu_cmd);
9050d44374cSMike Christie 	if (ret) {
9060d44374cSMike Christie 		tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt);
90797488c73SDan Carpenter 		mutex_unlock(&udev->cmdr_lock);
9086fd0ce79SMike Christie 
9096fd0ce79SMike Christie 		*scsi_err = TCM_OUT_OF_RESOURCES;
9106fd0ce79SMike Christie 		return -1;
9110d44374cSMike Christie 	}
9120d44374cSMike Christie 	entry->hdr.cmd_id = tcmu_cmd->cmd_id;
9130d44374cSMike Christie 
914fe25cc34SXiubo Li 	/*
915fe25cc34SXiubo Li 	 * Recalaulate the command's base size and size according
916fe25cc34SXiubo Li 	 * to the actual needs
917fe25cc34SXiubo Li 	 */
918fe25cc34SXiubo Li 	base_command_size = tcmu_cmd_get_base_cmd_size(entry->req.iov_cnt +
919fe25cc34SXiubo Li 						       entry->req.iov_bidi_cnt);
920fe25cc34SXiubo Li 	command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size);
921fe25cc34SXiubo Li 
922fe25cc34SXiubo Li 	tcmu_hdr_set_len(&entry->hdr.len_op, command_size);
923fe25cc34SXiubo Li 
9247c9e7a6fSAndy Grover 	/* All offsets relative to mb_addr, not start of entry! */
9257c9e7a6fSAndy Grover 	cdb_off = CMDR_OFF + cmd_head + base_command_size;
9267c9e7a6fSAndy Grover 	memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb));
9277c9e7a6fSAndy Grover 	entry->req.cdb_off = cdb_off;
9287c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(entry, sizeof(*entry));
9297c9e7a6fSAndy Grover 
9307c9e7a6fSAndy Grover 	UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size);
9317c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(mb, sizeof(*mb));
9327c9e7a6fSAndy Grover 
9337c9e7a6fSAndy Grover 	/* TODO: only if FLUSH and FUA? */
9347c9e7a6fSAndy Grover 	uio_event_notify(&udev->uio_info);
9357c9e7a6fSAndy Grover 
9366fd0ce79SMike Christie 	return 0;
937af1dd7ffSMike Christie 
938af1dd7ffSMike Christie queue:
939af1dd7ffSMike Christie 	if (add_to_cmdr_queue(tcmu_cmd)) {
940af1dd7ffSMike Christie 		*scsi_err = TCM_OUT_OF_RESOURCES;
941af1dd7ffSMike Christie 		return -1;
942af1dd7ffSMike Christie 	}
943af1dd7ffSMike Christie 
944af1dd7ffSMike Christie 	return 1;
9457c9e7a6fSAndy Grover }
9467c9e7a6fSAndy Grover 
94702eb924fSAndy Grover static sense_reason_t
94802eb924fSAndy Grover tcmu_queue_cmd(struct se_cmd *se_cmd)
9497c9e7a6fSAndy Grover {
950af1dd7ffSMike Christie 	struct se_device *se_dev = se_cmd->se_dev;
951af1dd7ffSMike Christie 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
9527c9e7a6fSAndy Grover 	struct tcmu_cmd *tcmu_cmd;
9536fd0ce79SMike Christie 	sense_reason_t scsi_ret;
954af1dd7ffSMike Christie 	int ret;
9557c9e7a6fSAndy Grover 
9567c9e7a6fSAndy Grover 	tcmu_cmd = tcmu_alloc_cmd(se_cmd);
9577c9e7a6fSAndy Grover 	if (!tcmu_cmd)
95802eb924fSAndy Grover 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
9597c9e7a6fSAndy Grover 
960af1dd7ffSMike Christie 	mutex_lock(&udev->cmdr_lock);
961af1dd7ffSMike Christie 	ret = queue_cmd_ring(tcmu_cmd, &scsi_ret);
962af1dd7ffSMike Christie 	mutex_unlock(&udev->cmdr_lock);
963af1dd7ffSMike Christie 	if (ret < 0)
964141685a3SXiubo Li 		tcmu_free_cmd(tcmu_cmd);
9656fd0ce79SMike Christie 	return scsi_ret;
9667c9e7a6fSAndy Grover }
9677c9e7a6fSAndy Grover 
9687c9e7a6fSAndy Grover static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *entry)
9697c9e7a6fSAndy Grover {
9707c9e7a6fSAndy Grover 	struct se_cmd *se_cmd = cmd->se_cmd;
9717c9e7a6fSAndy Grover 	struct tcmu_dev *udev = cmd->tcmu_dev;
9727c9e7a6fSAndy Grover 
973b25c7863SSheng Yang 	/*
974b25c7863SSheng Yang 	 * cmd has been completed already from timeout, just reclaim
9753d9b9555SAndy Grover 	 * data area space and free cmd
976b25c7863SSheng Yang 	 */
977141685a3SXiubo Li 	if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
978141685a3SXiubo Li 		goto out;
979b25c7863SSheng Yang 
980141685a3SXiubo Li 	tcmu_cmd_reset_dbi_cur(cmd);
9817c9e7a6fSAndy Grover 
9820ad46af8SAndy Grover 	if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) {
9830ad46af8SAndy Grover 		pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n",
9840ad46af8SAndy Grover 			cmd->se_cmd);
985ed97d0cdSAndy Grover 		entry->rsp.scsi_status = SAM_STAT_CHECK_CONDITION;
986ed97d0cdSAndy Grover 	} else if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) {
987406f74c2SMike Christie 		transport_copy_sense_to_cmd(se_cmd, entry->rsp.sense_buffer);
988e4648b01SIlias Tsitsimpis 	} else if (se_cmd->se_cmd_flags & SCF_BIDI) {
98926418649SSheng Yang 		/* Get Data-In buffer before clean up */
990a5d68ba8SXiubo Li 		gather_data_area(udev, cmd, true);
991e4648b01SIlias Tsitsimpis 	} else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
992a5d68ba8SXiubo Li 		gather_data_area(udev, cmd, false);
9937c9e7a6fSAndy Grover 	} else if (se_cmd->data_direction == DMA_TO_DEVICE) {
994141685a3SXiubo Li 		/* TODO: */
9952bc396a2SIlias Tsitsimpis 	} else if (se_cmd->data_direction != DMA_NONE) {
9962bc396a2SIlias Tsitsimpis 		pr_warn("TCMU: data direction was %d!\n",
9972bc396a2SIlias Tsitsimpis 			se_cmd->data_direction);
9987c9e7a6fSAndy Grover 	}
9997c9e7a6fSAndy Grover 
10007c9e7a6fSAndy Grover 	target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status);
10017c9e7a6fSAndy Grover 
1002141685a3SXiubo Li out:
1003141685a3SXiubo Li 	cmd->se_cmd = NULL;
1004b6df4b79SXiubo Li 	tcmu_cmd_free_data(cmd, cmd->dbi_cnt);
1005141685a3SXiubo Li 	tcmu_free_cmd(cmd);
10067c9e7a6fSAndy Grover }
10077c9e7a6fSAndy Grover 
10087c9e7a6fSAndy Grover static unsigned int tcmu_handle_completions(struct tcmu_dev *udev)
10097c9e7a6fSAndy Grover {
10107c9e7a6fSAndy Grover 	struct tcmu_mailbox *mb;
10117c9e7a6fSAndy Grover 	int handled = 0;
10127c9e7a6fSAndy Grover 
10137c9e7a6fSAndy Grover 	if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) {
10147c9e7a6fSAndy Grover 		pr_err("ring broken, not handling completions\n");
10157c9e7a6fSAndy Grover 		return 0;
10167c9e7a6fSAndy Grover 	}
10177c9e7a6fSAndy Grover 
10187c9e7a6fSAndy Grover 	mb = udev->mb_addr;
10197c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(mb, sizeof(*mb));
10207c9e7a6fSAndy Grover 
10216aa7de05SMark Rutland 	while (udev->cmdr_last_cleaned != READ_ONCE(mb->cmd_tail)) {
10227c9e7a6fSAndy Grover 
10237c9e7a6fSAndy Grover 		struct tcmu_cmd_entry *entry = (void *) mb + CMDR_OFF + udev->cmdr_last_cleaned;
10247c9e7a6fSAndy Grover 		struct tcmu_cmd *cmd;
10257c9e7a6fSAndy Grover 
10267c9e7a6fSAndy Grover 		tcmu_flush_dcache_range(entry, sizeof(*entry));
10277c9e7a6fSAndy Grover 
10280ad46af8SAndy Grover 		if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD) {
10290ad46af8SAndy Grover 			UPDATE_HEAD(udev->cmdr_last_cleaned,
10300ad46af8SAndy Grover 				    tcmu_hdr_get_len(entry->hdr.len_op),
10310ad46af8SAndy Grover 				    udev->cmdr_size);
10327c9e7a6fSAndy Grover 			continue;
10337c9e7a6fSAndy Grover 		}
10340ad46af8SAndy Grover 		WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD);
10357c9e7a6fSAndy Grover 
1036d3e709e6SMatthew Wilcox 		cmd = idr_remove(&udev->commands, entry->hdr.cmd_id);
10377c9e7a6fSAndy Grover 		if (!cmd) {
10387c9e7a6fSAndy Grover 			pr_err("cmd_id not found, ring is broken\n");
10397c9e7a6fSAndy Grover 			set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags);
10407c9e7a6fSAndy Grover 			break;
10417c9e7a6fSAndy Grover 		}
10427c9e7a6fSAndy Grover 
10437c9e7a6fSAndy Grover 		tcmu_handle_completion(cmd, entry);
10447c9e7a6fSAndy Grover 
10450ad46af8SAndy Grover 		UPDATE_HEAD(udev->cmdr_last_cleaned,
10460ad46af8SAndy Grover 			    tcmu_hdr_get_len(entry->hdr.len_op),
10470ad46af8SAndy Grover 			    udev->cmdr_size);
10487c9e7a6fSAndy Grover 
10497c9e7a6fSAndy Grover 		handled++;
10507c9e7a6fSAndy Grover 	}
10517c9e7a6fSAndy Grover 
1052af1dd7ffSMike Christie 	if (mb->cmd_tail == mb->cmd_head && list_empty(&udev->cmdr_queue)) {
1053af1dd7ffSMike Christie 		del_timer(&udev->timeout);
1054af1dd7ffSMike Christie 		/*
1055af1dd7ffSMike Christie 		 * not more pending or waiting commands so try to reclaim
1056af1dd7ffSMike Christie 		 * blocks if needed.
1057af1dd7ffSMike Christie 		 */
1058af1dd7ffSMike Christie 		if (atomic_read(&global_db_count) > TCMU_GLOBAL_MAX_BLOCKS)
1059af1dd7ffSMike Christie 			schedule_delayed_work(&tcmu_unmap_work, 0);
1060af1dd7ffSMike Christie 	}
10617c9e7a6fSAndy Grover 
10627c9e7a6fSAndy Grover 	return handled;
10637c9e7a6fSAndy Grover }
10647c9e7a6fSAndy Grover 
10657c9e7a6fSAndy Grover static int tcmu_check_expired_cmd(int id, void *p, void *data)
10667c9e7a6fSAndy Grover {
10677c9e7a6fSAndy Grover 	struct tcmu_cmd *cmd = p;
1068af1dd7ffSMike Christie 	struct tcmu_dev *udev = cmd->tcmu_dev;
1069af1dd7ffSMike Christie 	u8 scsi_status;
1070af1dd7ffSMike Christie 	struct se_cmd *se_cmd;
1071af1dd7ffSMike Christie 	bool is_running;
10727c9e7a6fSAndy Grover 
10737c9e7a6fSAndy Grover 	if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
10747c9e7a6fSAndy Grover 		return 0;
10757c9e7a6fSAndy Grover 
1076611e2267SAndy Grover 	if (!time_after(jiffies, cmd->deadline))
10777c9e7a6fSAndy Grover 		return 0;
10787c9e7a6fSAndy Grover 
1079af1dd7ffSMike Christie 	is_running = list_empty(&cmd->cmdr_queue_entry);
1080af1dd7ffSMike Christie 	pr_debug("Timing out cmd %u on dev %s that is %s.\n",
1081af1dd7ffSMike Christie 		 id, udev->name, is_running ? "inflight" : "queued");
1082af1dd7ffSMike Christie 
1083af1dd7ffSMike Christie 	se_cmd = cmd->se_cmd;
10847c9e7a6fSAndy Grover 	cmd->se_cmd = NULL;
10857c9e7a6fSAndy Grover 
1086af1dd7ffSMike Christie 	if (is_running) {
1087af1dd7ffSMike Christie 		set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags);
1088af1dd7ffSMike Christie 		/*
1089af1dd7ffSMike Christie 		 * target_complete_cmd will translate this to LUN COMM FAILURE
1090af1dd7ffSMike Christie 		 */
1091af1dd7ffSMike Christie 		scsi_status = SAM_STAT_CHECK_CONDITION;
1092af1dd7ffSMike Christie 	} else {
1093af1dd7ffSMike Christie 		list_del_init(&cmd->cmdr_queue_entry);
1094af1dd7ffSMike Christie 
1095af1dd7ffSMike Christie 		idr_remove(&udev->commands, id);
1096af1dd7ffSMike Christie 		tcmu_free_cmd(cmd);
1097af1dd7ffSMike Christie 		scsi_status = SAM_STAT_TASK_SET_FULL;
1098af1dd7ffSMike Christie 	}
1099af1dd7ffSMike Christie 	target_complete_cmd(se_cmd, scsi_status);
11007c9e7a6fSAndy Grover 	return 0;
11017c9e7a6fSAndy Grover }
11027c9e7a6fSAndy Grover 
1103e99e88a9SKees Cook static void tcmu_device_timedout(struct timer_list *t)
11047c9e7a6fSAndy Grover {
1105e99e88a9SKees Cook 	struct tcmu_dev *udev = from_timer(udev, t, timeout);
11067c9e7a6fSAndy Grover 
1107488ebe4cSMike Christie 	pr_debug("%s cmd timeout has expired\n", udev->name);
1108488ebe4cSMike Christie 
1109488ebe4cSMike Christie 	spin_lock(&timed_out_udevs_lock);
1110488ebe4cSMike Christie 	if (list_empty(&udev->timedout_entry))
1111488ebe4cSMike Christie 		list_add_tail(&udev->timedout_entry, &timed_out_udevs);
1112488ebe4cSMike Christie 	spin_unlock(&timed_out_udevs_lock);
11137c9e7a6fSAndy Grover 
1114af1dd7ffSMike Christie 	schedule_delayed_work(&tcmu_unmap_work, 0);
11157c9e7a6fSAndy Grover }
11167c9e7a6fSAndy Grover 
11177c9e7a6fSAndy Grover static int tcmu_attach_hba(struct se_hba *hba, u32 host_id)
11187c9e7a6fSAndy Grover {
11197c9e7a6fSAndy Grover 	struct tcmu_hba *tcmu_hba;
11207c9e7a6fSAndy Grover 
11217c9e7a6fSAndy Grover 	tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL);
11227c9e7a6fSAndy Grover 	if (!tcmu_hba)
11237c9e7a6fSAndy Grover 		return -ENOMEM;
11247c9e7a6fSAndy Grover 
11257c9e7a6fSAndy Grover 	tcmu_hba->host_id = host_id;
11267c9e7a6fSAndy Grover 	hba->hba_ptr = tcmu_hba;
11277c9e7a6fSAndy Grover 
11287c9e7a6fSAndy Grover 	return 0;
11297c9e7a6fSAndy Grover }
11307c9e7a6fSAndy Grover 
11317c9e7a6fSAndy Grover static void tcmu_detach_hba(struct se_hba *hba)
11327c9e7a6fSAndy Grover {
11337c9e7a6fSAndy Grover 	kfree(hba->hba_ptr);
11347c9e7a6fSAndy Grover 	hba->hba_ptr = NULL;
11357c9e7a6fSAndy Grover }
11367c9e7a6fSAndy Grover 
11377c9e7a6fSAndy Grover static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
11387c9e7a6fSAndy Grover {
11397c9e7a6fSAndy Grover 	struct tcmu_dev *udev;
11407c9e7a6fSAndy Grover 
11417c9e7a6fSAndy Grover 	udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL);
11427c9e7a6fSAndy Grover 	if (!udev)
11437c9e7a6fSAndy Grover 		return NULL;
1144f3cdbe39SMike Christie 	kref_init(&udev->kref);
11457c9e7a6fSAndy Grover 
11467c9e7a6fSAndy Grover 	udev->name = kstrdup(name, GFP_KERNEL);
11477c9e7a6fSAndy Grover 	if (!udev->name) {
11487c9e7a6fSAndy Grover 		kfree(udev);
11497c9e7a6fSAndy Grover 		return NULL;
11507c9e7a6fSAndy Grover 	}
11517c9e7a6fSAndy Grover 
11527c9e7a6fSAndy Grover 	udev->hba = hba;
1153af980e46SMike Christie 	udev->cmd_time_out = TCMU_TIME_OUT;
11547c9e7a6fSAndy Grover 
1155b6df4b79SXiubo Li 	mutex_init(&udev->cmdr_lock);
11567c9e7a6fSAndy Grover 
1157488ebe4cSMike Christie 	INIT_LIST_HEAD(&udev->timedout_entry);
1158af1dd7ffSMike Christie 	INIT_LIST_HEAD(&udev->cmdr_queue);
11597c9e7a6fSAndy Grover 	idr_init(&udev->commands);
11607c9e7a6fSAndy Grover 
1161e99e88a9SKees Cook 	timer_setup(&udev->timeout, tcmu_device_timedout, 0);
11627c9e7a6fSAndy Grover 
1163b3af66e2SMike Christie 	init_waitqueue_head(&udev->nl_cmd_wq);
1164b3af66e2SMike Christie 	spin_lock_init(&udev->nl_cmd_lock);
1165b3af66e2SMike Christie 
1166c22adc0bSXiubo Li 	INIT_RADIX_TREE(&udev->data_blocks, GFP_KERNEL);
1167c22adc0bSXiubo Li 
11687c9e7a6fSAndy Grover 	return &udev->se_dev;
11697c9e7a6fSAndy Grover }
11707c9e7a6fSAndy Grover 
1171af1dd7ffSMike Christie static bool run_cmdr_queue(struct tcmu_dev *udev)
1172af1dd7ffSMike Christie {
1173af1dd7ffSMike Christie 	struct tcmu_cmd *tcmu_cmd, *tmp_cmd;
1174af1dd7ffSMike Christie 	LIST_HEAD(cmds);
1175af1dd7ffSMike Christie 	bool drained = true;
1176af1dd7ffSMike Christie 	sense_reason_t scsi_ret;
1177af1dd7ffSMike Christie 	int ret;
1178af1dd7ffSMike Christie 
1179af1dd7ffSMike Christie 	if (list_empty(&udev->cmdr_queue))
1180af1dd7ffSMike Christie 		return true;
1181af1dd7ffSMike Christie 
1182af1dd7ffSMike Christie 	pr_debug("running %s's cmdr queue\n", udev->name);
1183af1dd7ffSMike Christie 
1184af1dd7ffSMike Christie 	list_splice_init(&udev->cmdr_queue, &cmds);
1185af1dd7ffSMike Christie 
1186af1dd7ffSMike Christie 	list_for_each_entry_safe(tcmu_cmd, tmp_cmd, &cmds, cmdr_queue_entry) {
1187af1dd7ffSMike Christie 		list_del_init(&tcmu_cmd->cmdr_queue_entry);
1188af1dd7ffSMike Christie 
1189af1dd7ffSMike Christie 	        pr_debug("removing cmd %u on dev %s from queue\n",
1190af1dd7ffSMike Christie 		         tcmu_cmd->cmd_id, udev->name);
1191af1dd7ffSMike Christie 
1192af1dd7ffSMike Christie 		ret = queue_cmd_ring(tcmu_cmd, &scsi_ret);
1193af1dd7ffSMike Christie 		if (ret < 0) {
1194af1dd7ffSMike Christie 		        pr_debug("cmd %u on dev %s failed with %u\n",
1195af1dd7ffSMike Christie 			         tcmu_cmd->cmd_id, udev->name, scsi_ret);
1196af1dd7ffSMike Christie 
1197af1dd7ffSMike Christie 			idr_remove(&udev->commands, tcmu_cmd->cmd_id);
1198af1dd7ffSMike Christie 			/*
1199af1dd7ffSMike Christie 			 * Ignore scsi_ret for now. target_complete_cmd
1200af1dd7ffSMike Christie 			 * drops it.
1201af1dd7ffSMike Christie 			 */
1202af1dd7ffSMike Christie 			target_complete_cmd(tcmu_cmd->se_cmd,
1203af1dd7ffSMike Christie 					    SAM_STAT_CHECK_CONDITION);
1204af1dd7ffSMike Christie 			tcmu_free_cmd(tcmu_cmd);
1205af1dd7ffSMike Christie 		} else if (ret > 0) {
1206af1dd7ffSMike Christie 			pr_debug("ran out of space during cmdr queue run\n");
1207af1dd7ffSMike Christie 			/*
1208af1dd7ffSMike Christie 			 * cmd was requeued, so just put all cmds back in
1209af1dd7ffSMike Christie 			 * the queue
1210af1dd7ffSMike Christie 			 */
1211af1dd7ffSMike Christie 			list_splice_tail(&cmds, &udev->cmdr_queue);
1212af1dd7ffSMike Christie 			drained = false;
1213af1dd7ffSMike Christie 			goto done;
1214af1dd7ffSMike Christie 		}
1215af1dd7ffSMike Christie 	}
1216af1dd7ffSMike Christie done:
1217af1dd7ffSMike Christie 	return drained;
1218af1dd7ffSMike Christie }
1219af1dd7ffSMike Christie 
12207c9e7a6fSAndy Grover static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on)
12217c9e7a6fSAndy Grover {
1222af1dd7ffSMike Christie 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
12237c9e7a6fSAndy Grover 
1224af1dd7ffSMike Christie 	mutex_lock(&udev->cmdr_lock);
1225af1dd7ffSMike Christie 	tcmu_handle_completions(udev);
1226af1dd7ffSMike Christie 	run_cmdr_queue(udev);
1227af1dd7ffSMike Christie 	mutex_unlock(&udev->cmdr_lock);
12287c9e7a6fSAndy Grover 
12297c9e7a6fSAndy Grover 	return 0;
12307c9e7a6fSAndy Grover }
12317c9e7a6fSAndy Grover 
12327c9e7a6fSAndy Grover /*
12337c9e7a6fSAndy Grover  * mmap code from uio.c. Copied here because we want to hook mmap()
12347c9e7a6fSAndy Grover  * and this stuff must come along.
12357c9e7a6fSAndy Grover  */
12367c9e7a6fSAndy Grover static int tcmu_find_mem_index(struct vm_area_struct *vma)
12377c9e7a6fSAndy Grover {
12387c9e7a6fSAndy Grover 	struct tcmu_dev *udev = vma->vm_private_data;
12397c9e7a6fSAndy Grover 	struct uio_info *info = &udev->uio_info;
12407c9e7a6fSAndy Grover 
12417c9e7a6fSAndy Grover 	if (vma->vm_pgoff < MAX_UIO_MAPS) {
12427c9e7a6fSAndy Grover 		if (info->mem[vma->vm_pgoff].size == 0)
12437c9e7a6fSAndy Grover 			return -1;
12447c9e7a6fSAndy Grover 		return (int)vma->vm_pgoff;
12457c9e7a6fSAndy Grover 	}
12467c9e7a6fSAndy Grover 	return -1;
12477c9e7a6fSAndy Grover }
12487c9e7a6fSAndy Grover 
1249b6df4b79SXiubo Li static struct page *tcmu_try_get_block_page(struct tcmu_dev *udev, uint32_t dbi)
1250b6df4b79SXiubo Li {
1251b6df4b79SXiubo Li 	struct page *page;
1252b6df4b79SXiubo Li 	int ret;
1253b6df4b79SXiubo Li 
1254b6df4b79SXiubo Li 	mutex_lock(&udev->cmdr_lock);
1255b6df4b79SXiubo Li 	page = tcmu_get_block_page(udev, dbi);
1256b6df4b79SXiubo Li 	if (likely(page)) {
1257b6df4b79SXiubo Li 		mutex_unlock(&udev->cmdr_lock);
1258b6df4b79SXiubo Li 		return page;
1259b6df4b79SXiubo Li 	}
1260b6df4b79SXiubo Li 
1261b6df4b79SXiubo Li 	/*
1262b6df4b79SXiubo Li 	 * Normally it shouldn't be here:
1263b6df4b79SXiubo Li 	 * Only when the userspace has touched the blocks which
1264b6df4b79SXiubo Li 	 * are out of the tcmu_cmd's data iov[], and will return
1265b6df4b79SXiubo Li 	 * one zeroed page.
1266b6df4b79SXiubo Li 	 */
1267b6df4b79SXiubo Li 	pr_warn("Block(%u) out of cmd's iov[] has been touched!\n", dbi);
1268b6df4b79SXiubo Li 	pr_warn("Mostly it will be a bug of userspace, please have a check!\n");
1269b6df4b79SXiubo Li 
1270b6df4b79SXiubo Li 	if (dbi >= udev->dbi_thresh) {
1271b6df4b79SXiubo Li 		/* Extern the udev->dbi_thresh to dbi + 1 */
1272b6df4b79SXiubo Li 		udev->dbi_thresh = dbi + 1;
1273b6df4b79SXiubo Li 		udev->dbi_max = dbi;
1274b6df4b79SXiubo Li 	}
1275b6df4b79SXiubo Li 
1276b6df4b79SXiubo Li 	page = radix_tree_lookup(&udev->data_blocks, dbi);
1277b6df4b79SXiubo Li 	if (!page) {
1278b6df4b79SXiubo Li 		page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1279b6df4b79SXiubo Li 		if (!page) {
1280b6df4b79SXiubo Li 			mutex_unlock(&udev->cmdr_lock);
1281b6df4b79SXiubo Li 			return NULL;
1282b6df4b79SXiubo Li 		}
1283b6df4b79SXiubo Li 
1284b6df4b79SXiubo Li 		ret = radix_tree_insert(&udev->data_blocks, dbi, page);
1285b6df4b79SXiubo Li 		if (ret) {
1286b6df4b79SXiubo Li 			mutex_unlock(&udev->cmdr_lock);
1287b6df4b79SXiubo Li 			__free_page(page);
1288b6df4b79SXiubo Li 			return NULL;
1289b6df4b79SXiubo Li 		}
1290b6df4b79SXiubo Li 
1291b6df4b79SXiubo Li 		/*
1292b6df4b79SXiubo Li 		 * Since this case is rare in page fault routine, here we
1293b6df4b79SXiubo Li 		 * will allow the global_db_count >= TCMU_GLOBAL_MAX_BLOCKS
1294b6df4b79SXiubo Li 		 * to reduce possible page fault call trace.
1295b6df4b79SXiubo Li 		 */
1296b6df4b79SXiubo Li 		atomic_inc(&global_db_count);
1297b6df4b79SXiubo Li 	}
1298b6df4b79SXiubo Li 	mutex_unlock(&udev->cmdr_lock);
1299b6df4b79SXiubo Li 
1300b6df4b79SXiubo Li 	return page;
1301b6df4b79SXiubo Li }
1302b6df4b79SXiubo Li 
130311bac800SDave Jiang static int tcmu_vma_fault(struct vm_fault *vmf)
13047c9e7a6fSAndy Grover {
130511bac800SDave Jiang 	struct tcmu_dev *udev = vmf->vma->vm_private_data;
13067c9e7a6fSAndy Grover 	struct uio_info *info = &udev->uio_info;
13077c9e7a6fSAndy Grover 	struct page *page;
13087c9e7a6fSAndy Grover 	unsigned long offset;
13097c9e7a6fSAndy Grover 	void *addr;
13107c9e7a6fSAndy Grover 
131111bac800SDave Jiang 	int mi = tcmu_find_mem_index(vmf->vma);
13127c9e7a6fSAndy Grover 	if (mi < 0)
13137c9e7a6fSAndy Grover 		return VM_FAULT_SIGBUS;
13147c9e7a6fSAndy Grover 
13157c9e7a6fSAndy Grover 	/*
13167c9e7a6fSAndy Grover 	 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
13177c9e7a6fSAndy Grover 	 * to use mem[N].
13187c9e7a6fSAndy Grover 	 */
13197c9e7a6fSAndy Grover 	offset = (vmf->pgoff - mi) << PAGE_SHIFT;
13207c9e7a6fSAndy Grover 
1321141685a3SXiubo Li 	if (offset < udev->data_off) {
1322141685a3SXiubo Li 		/* For the vmalloc()ed cmd area pages */
13237c9e7a6fSAndy Grover 		addr = (void *)(unsigned long)info->mem[mi].addr + offset;
13247c9e7a6fSAndy Grover 		page = vmalloc_to_page(addr);
1325141685a3SXiubo Li 	} else {
1326141685a3SXiubo Li 		uint32_t dbi;
1327141685a3SXiubo Li 
1328b6df4b79SXiubo Li 		/* For the dynamically growing data area pages */
1329141685a3SXiubo Li 		dbi = (offset - udev->data_off) / DATA_BLOCK_SIZE;
1330b6df4b79SXiubo Li 		page = tcmu_try_get_block_page(udev, dbi);
1331b6df4b79SXiubo Li 		if (!page)
1332141685a3SXiubo Li 			return VM_FAULT_NOPAGE;
1333141685a3SXiubo Li 	}
1334141685a3SXiubo Li 
13357c9e7a6fSAndy Grover 	get_page(page);
13367c9e7a6fSAndy Grover 	vmf->page = page;
13377c9e7a6fSAndy Grover 	return 0;
13387c9e7a6fSAndy Grover }
13397c9e7a6fSAndy Grover 
13407c9e7a6fSAndy Grover static const struct vm_operations_struct tcmu_vm_ops = {
13417c9e7a6fSAndy Grover 	.fault = tcmu_vma_fault,
13427c9e7a6fSAndy Grover };
13437c9e7a6fSAndy Grover 
13447c9e7a6fSAndy Grover static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma)
13457c9e7a6fSAndy Grover {
13467c9e7a6fSAndy Grover 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
13477c9e7a6fSAndy Grover 
13487c9e7a6fSAndy Grover 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
13497c9e7a6fSAndy Grover 	vma->vm_ops = &tcmu_vm_ops;
13507c9e7a6fSAndy Grover 
13517c9e7a6fSAndy Grover 	vma->vm_private_data = udev;
13527c9e7a6fSAndy Grover 
13537c9e7a6fSAndy Grover 	/* Ensure the mmap is exactly the right size */
13547c9e7a6fSAndy Grover 	if (vma_pages(vma) != (TCMU_RING_SIZE >> PAGE_SHIFT))
13557c9e7a6fSAndy Grover 		return -EINVAL;
13567c9e7a6fSAndy Grover 
13577c9e7a6fSAndy Grover 	return 0;
13587c9e7a6fSAndy Grover }
13597c9e7a6fSAndy Grover 
13607c9e7a6fSAndy Grover static int tcmu_open(struct uio_info *info, struct inode *inode)
13617c9e7a6fSAndy Grover {
13627c9e7a6fSAndy Grover 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
13637c9e7a6fSAndy Grover 
13647c9e7a6fSAndy Grover 	/* O_EXCL not supported for char devs, so fake it? */
13657c9e7a6fSAndy Grover 	if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags))
13667c9e7a6fSAndy Grover 		return -EBUSY;
13677c9e7a6fSAndy Grover 
1368b6df4b79SXiubo Li 	udev->inode = inode;
13699260695dSMike Christie 	kref_get(&udev->kref);
1370b6df4b79SXiubo Li 
13717c9e7a6fSAndy Grover 	pr_debug("open\n");
13727c9e7a6fSAndy Grover 
13737c9e7a6fSAndy Grover 	return 0;
13747c9e7a6fSAndy Grover }
13757c9e7a6fSAndy Grover 
1376f3cdbe39SMike Christie static void tcmu_dev_call_rcu(struct rcu_head *p)
1377f3cdbe39SMike Christie {
1378f3cdbe39SMike Christie 	struct se_device *dev = container_of(p, struct se_device, rcu_head);
1379f3cdbe39SMike Christie 	struct tcmu_dev *udev = TCMU_DEV(dev);
1380f3cdbe39SMike Christie 
1381f3cdbe39SMike Christie 	kfree(udev->uio_info.name);
1382f3cdbe39SMike Christie 	kfree(udev->name);
1383f3cdbe39SMike Christie 	kfree(udev);
1384f3cdbe39SMike Christie }
1385f3cdbe39SMike Christie 
1386c22adc0bSXiubo Li static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd)
1387c22adc0bSXiubo Li {
1388c22adc0bSXiubo Li 	if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
1389c22adc0bSXiubo Li 		kmem_cache_free(tcmu_cmd_cache, cmd);
1390c22adc0bSXiubo Li 		return 0;
1391c22adc0bSXiubo Li 	}
1392c22adc0bSXiubo Li 	return -EINVAL;
1393c22adc0bSXiubo Li }
1394c22adc0bSXiubo Li 
1395bf99ec13SMike Christie static void tcmu_blocks_release(struct radix_tree_root *blocks,
1396bf99ec13SMike Christie 				int start, int end)
1397c22adc0bSXiubo Li {
1398c22adc0bSXiubo Li 	int i;
1399c22adc0bSXiubo Li 	struct page *page;
1400c22adc0bSXiubo Li 
1401bf99ec13SMike Christie 	for (i = start; i < end; i++) {
1402bf99ec13SMike Christie 		page = radix_tree_delete(blocks, i);
1403c22adc0bSXiubo Li 		if (page) {
1404c22adc0bSXiubo Li 			__free_page(page);
1405c22adc0bSXiubo Li 			atomic_dec(&global_db_count);
1406c22adc0bSXiubo Li 		}
1407c22adc0bSXiubo Li 	}
1408c22adc0bSXiubo Li }
1409c22adc0bSXiubo Li 
1410f3cdbe39SMike Christie static void tcmu_dev_kref_release(struct kref *kref)
1411f3cdbe39SMike Christie {
1412f3cdbe39SMike Christie 	struct tcmu_dev *udev = container_of(kref, struct tcmu_dev, kref);
1413f3cdbe39SMike Christie 	struct se_device *dev = &udev->se_dev;
1414c22adc0bSXiubo Li 	struct tcmu_cmd *cmd;
1415c22adc0bSXiubo Li 	bool all_expired = true;
1416c22adc0bSXiubo Li 	int i;
1417c22adc0bSXiubo Li 
1418c22adc0bSXiubo Li 	vfree(udev->mb_addr);
1419c22adc0bSXiubo Li 	udev->mb_addr = NULL;
1420c22adc0bSXiubo Li 
1421488ebe4cSMike Christie 	spin_lock_bh(&timed_out_udevs_lock);
1422488ebe4cSMike Christie 	if (!list_empty(&udev->timedout_entry))
1423488ebe4cSMike Christie 		list_del(&udev->timedout_entry);
1424488ebe4cSMike Christie 	spin_unlock_bh(&timed_out_udevs_lock);
1425488ebe4cSMike Christie 
1426c22adc0bSXiubo Li 	/* Upper layer should drain all requests before calling this */
14276fddcb77SMike Christie 	mutex_lock(&udev->cmdr_lock);
1428c22adc0bSXiubo Li 	idr_for_each_entry(&udev->commands, cmd, i) {
1429c22adc0bSXiubo Li 		if (tcmu_check_and_free_pending_cmd(cmd) != 0)
1430c22adc0bSXiubo Li 			all_expired = false;
1431c22adc0bSXiubo Li 	}
1432c22adc0bSXiubo Li 	idr_destroy(&udev->commands);
1433c22adc0bSXiubo Li 	WARN_ON(!all_expired);
1434c22adc0bSXiubo Li 
1435bf99ec13SMike Christie 	tcmu_blocks_release(&udev->data_blocks, 0, udev->dbi_max + 1);
1436bf99ec13SMike Christie 	mutex_unlock(&udev->cmdr_lock);
1437f3cdbe39SMike Christie 
1438f3cdbe39SMike Christie 	call_rcu(&dev->rcu_head, tcmu_dev_call_rcu);
1439f3cdbe39SMike Christie }
1440f3cdbe39SMike Christie 
14417c9e7a6fSAndy Grover static int tcmu_release(struct uio_info *info, struct inode *inode)
14427c9e7a6fSAndy Grover {
14437c9e7a6fSAndy Grover 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
14447c9e7a6fSAndy Grover 
14457c9e7a6fSAndy Grover 	clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags);
14467c9e7a6fSAndy Grover 
14477c9e7a6fSAndy Grover 	pr_debug("close\n");
14489260695dSMike Christie 	/* release ref from open */
1449f3cdbe39SMike Christie 	kref_put(&udev->kref, tcmu_dev_kref_release);
14507c9e7a6fSAndy Grover 	return 0;
14517c9e7a6fSAndy Grover }
14527c9e7a6fSAndy Grover 
1453b3af66e2SMike Christie static void tcmu_init_genl_cmd_reply(struct tcmu_dev *udev, int cmd)
1454b3af66e2SMike Christie {
1455b3af66e2SMike Christie 	struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd;
1456b3af66e2SMike Christie 
1457b3af66e2SMike Christie 	if (!tcmu_kern_cmd_reply_supported)
1458b3af66e2SMike Christie 		return;
1459b849b456SKenjiro Nakayama 
1460b849b456SKenjiro Nakayama 	if (udev->nl_reply_supported <= 0)
1461b849b456SKenjiro Nakayama 		return;
1462b849b456SKenjiro Nakayama 
1463b3af66e2SMike Christie relock:
1464b3af66e2SMike Christie 	spin_lock(&udev->nl_cmd_lock);
1465b3af66e2SMike Christie 
1466b3af66e2SMike Christie 	if (nl_cmd->cmd != TCMU_CMD_UNSPEC) {
1467b3af66e2SMike Christie 		spin_unlock(&udev->nl_cmd_lock);
1468b3af66e2SMike Christie 		pr_debug("sleeping for open nl cmd\n");
1469b3af66e2SMike Christie 		wait_event(udev->nl_cmd_wq, (nl_cmd->cmd == TCMU_CMD_UNSPEC));
1470b3af66e2SMike Christie 		goto relock;
1471b3af66e2SMike Christie 	}
1472b3af66e2SMike Christie 
1473b3af66e2SMike Christie 	memset(nl_cmd, 0, sizeof(*nl_cmd));
1474b3af66e2SMike Christie 	nl_cmd->cmd = cmd;
1475b3af66e2SMike Christie 	init_completion(&nl_cmd->complete);
1476b3af66e2SMike Christie 
1477b3af66e2SMike Christie 	spin_unlock(&udev->nl_cmd_lock);
1478b3af66e2SMike Christie }
1479b3af66e2SMike Christie 
1480b3af66e2SMike Christie static int tcmu_wait_genl_cmd_reply(struct tcmu_dev *udev)
1481b3af66e2SMike Christie {
1482b3af66e2SMike Christie 	struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd;
1483b3af66e2SMike Christie 	int ret;
1484b3af66e2SMike Christie 	DEFINE_WAIT(__wait);
1485b3af66e2SMike Christie 
1486b3af66e2SMike Christie 	if (!tcmu_kern_cmd_reply_supported)
1487b3af66e2SMike Christie 		return 0;
1488b3af66e2SMike Christie 
1489b849b456SKenjiro Nakayama 	if (udev->nl_reply_supported <= 0)
1490b849b456SKenjiro Nakayama 		return 0;
1491b849b456SKenjiro Nakayama 
1492b3af66e2SMike Christie 	pr_debug("sleeping for nl reply\n");
1493b3af66e2SMike Christie 	wait_for_completion(&nl_cmd->complete);
1494b3af66e2SMike Christie 
1495b3af66e2SMike Christie 	spin_lock(&udev->nl_cmd_lock);
1496b3af66e2SMike Christie 	nl_cmd->cmd = TCMU_CMD_UNSPEC;
1497b3af66e2SMike Christie 	ret = nl_cmd->status;
1498b3af66e2SMike Christie 	nl_cmd->status = 0;
1499b3af66e2SMike Christie 	spin_unlock(&udev->nl_cmd_lock);
1500b3af66e2SMike Christie 
1501b3af66e2SMike Christie 	wake_up_all(&udev->nl_cmd_wq);
1502b3af66e2SMike Christie 
1503b3af66e2SMike Christie 	return ret;;
1504b3af66e2SMike Christie }
1505b3af66e2SMike Christie 
1506b3af66e2SMike Christie static int tcmu_netlink_event(struct tcmu_dev *udev, enum tcmu_genl_cmd cmd,
1507b3af66e2SMike Christie 			      int reconfig_attr, const void *reconfig_data)
15087c9e7a6fSAndy Grover {
15097c9e7a6fSAndy Grover 	struct sk_buff *skb;
15107c9e7a6fSAndy Grover 	void *msg_header;
15116e14eab9SNicholas Bellinger 	int ret = -ENOMEM;
15127c9e7a6fSAndy Grover 
15137c9e7a6fSAndy Grover 	skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
15147c9e7a6fSAndy Grover 	if (!skb)
15156e14eab9SNicholas Bellinger 		return ret;
15167c9e7a6fSAndy Grover 
15177c9e7a6fSAndy Grover 	msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd);
15186e14eab9SNicholas Bellinger 	if (!msg_header)
15196e14eab9SNicholas Bellinger 		goto free_skb;
15207c9e7a6fSAndy Grover 
1521b3af66e2SMike Christie 	ret = nla_put_string(skb, TCMU_ATTR_DEVICE, udev->uio_info.name);
15226e14eab9SNicholas Bellinger 	if (ret < 0)
15236e14eab9SNicholas Bellinger 		goto free_skb;
15247c9e7a6fSAndy Grover 
1525b3af66e2SMike Christie 	ret = nla_put_u32(skb, TCMU_ATTR_MINOR, udev->uio_info.uio_dev->minor);
1526b3af66e2SMike Christie 	if (ret < 0)
1527b3af66e2SMike Christie 		goto free_skb;
1528b3af66e2SMike Christie 
1529b3af66e2SMike Christie 	ret = nla_put_u32(skb, TCMU_ATTR_DEVICE_ID, udev->se_dev.dev_index);
15306e14eab9SNicholas Bellinger 	if (ret < 0)
15316e14eab9SNicholas Bellinger 		goto free_skb;
15327c9e7a6fSAndy Grover 
15332d76443eSMike Christie 	if (cmd == TCMU_CMD_RECONFIG_DEVICE) {
15342d76443eSMike Christie 		switch (reconfig_attr) {
15352d76443eSMike Christie 		case TCMU_ATTR_DEV_CFG:
15362d76443eSMike Christie 			ret = nla_put_string(skb, reconfig_attr, reconfig_data);
15372d76443eSMike Christie 			break;
15382d76443eSMike Christie 		case TCMU_ATTR_DEV_SIZE:
15392d76443eSMike Christie 			ret = nla_put_u64_64bit(skb, reconfig_attr,
15402d76443eSMike Christie 						*((u64 *)reconfig_data),
15412d76443eSMike Christie 						TCMU_ATTR_PAD);
15422d76443eSMike Christie 			break;
15432d76443eSMike Christie 		case TCMU_ATTR_WRITECACHE:
15442d76443eSMike Christie 			ret = nla_put_u8(skb, reconfig_attr,
15452d76443eSMike Christie 					  *((u8 *)reconfig_data));
15462d76443eSMike Christie 			break;
15472d76443eSMike Christie 		default:
15482d76443eSMike Christie 			BUG();
15492d76443eSMike Christie 		}
15502d76443eSMike Christie 
15518a45885cSBryant G. Ly 		if (ret < 0)
15528a45885cSBryant G. Ly 			goto free_skb;
15532d76443eSMike Christie 	}
15548a45885cSBryant G. Ly 
1555053c095aSJohannes Berg 	genlmsg_end(skb, msg_header);
15567c9e7a6fSAndy Grover 
1557b3af66e2SMike Christie 	tcmu_init_genl_cmd_reply(udev, cmd);
1558b3af66e2SMike Christie 
155920c08b36SSheng Yang 	ret = genlmsg_multicast_allns(&tcmu_genl_family, skb, 0,
15607c9e7a6fSAndy Grover 				TCMU_MCGRP_CONFIG, GFP_KERNEL);
15617c9e7a6fSAndy Grover 	/* We don't care if no one is listening */
15627c9e7a6fSAndy Grover 	if (ret == -ESRCH)
15637c9e7a6fSAndy Grover 		ret = 0;
1564b3af66e2SMike Christie 	if (!ret)
1565b3af66e2SMike Christie 		ret = tcmu_wait_genl_cmd_reply(udev);
15667c9e7a6fSAndy Grover 
15677c9e7a6fSAndy Grover 	return ret;
15686e14eab9SNicholas Bellinger free_skb:
15696e14eab9SNicholas Bellinger 	nlmsg_free(skb);
15706e14eab9SNicholas Bellinger 	return ret;
15717c9e7a6fSAndy Grover }
15727c9e7a6fSAndy Grover 
1573de8c5221SBryant G. Ly static int tcmu_update_uio_info(struct tcmu_dev *udev)
15747c9e7a6fSAndy Grover {
15757c9e7a6fSAndy Grover 	struct tcmu_hba *hba = udev->hba->hba_ptr;
15767c9e7a6fSAndy Grover 	struct uio_info *info;
1577de8c5221SBryant G. Ly 	size_t size, used;
15787c9e7a6fSAndy Grover 	char *str;
15797c9e7a6fSAndy Grover 
15807c9e7a6fSAndy Grover 	info = &udev->uio_info;
15817c9e7a6fSAndy Grover 	size = snprintf(NULL, 0, "tcm-user/%u/%s/%s", hba->host_id, udev->name,
15827c9e7a6fSAndy Grover 			udev->dev_config);
15837c9e7a6fSAndy Grover 	size += 1; /* for \0 */
15847c9e7a6fSAndy Grover 	str = kmalloc(size, GFP_KERNEL);
15857c9e7a6fSAndy Grover 	if (!str)
15867c9e7a6fSAndy Grover 		return -ENOMEM;
15877c9e7a6fSAndy Grover 
15887c9e7a6fSAndy Grover 	used = snprintf(str, size, "tcm-user/%u/%s", hba->host_id, udev->name);
15897c9e7a6fSAndy Grover 	if (udev->dev_config[0])
15907c9e7a6fSAndy Grover 		snprintf(str + used, size - used, "/%s", udev->dev_config);
15917c9e7a6fSAndy Grover 
1592ededd039SBryant G. Ly 	/* If the old string exists, free it */
1593ededd039SBryant G. Ly 	kfree(info->name);
15947c9e7a6fSAndy Grover 	info->name = str;
15957c9e7a6fSAndy Grover 
1596de8c5221SBryant G. Ly 	return 0;
1597de8c5221SBryant G. Ly }
1598de8c5221SBryant G. Ly 
1599de8c5221SBryant G. Ly static int tcmu_configure_device(struct se_device *dev)
1600de8c5221SBryant G. Ly {
1601de8c5221SBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(dev);
1602de8c5221SBryant G. Ly 	struct uio_info *info;
1603de8c5221SBryant G. Ly 	struct tcmu_mailbox *mb;
1604de8c5221SBryant G. Ly 	int ret = 0;
1605de8c5221SBryant G. Ly 
1606de8c5221SBryant G. Ly 	ret = tcmu_update_uio_info(udev);
1607de8c5221SBryant G. Ly 	if (ret)
1608de8c5221SBryant G. Ly 		return ret;
1609de8c5221SBryant G. Ly 
1610de8c5221SBryant G. Ly 	info = &udev->uio_info;
1611de8c5221SBryant G. Ly 
1612141685a3SXiubo Li 	udev->mb_addr = vzalloc(CMDR_SIZE);
16137c9e7a6fSAndy Grover 	if (!udev->mb_addr) {
16147c9e7a6fSAndy Grover 		ret = -ENOMEM;
16157c9e7a6fSAndy Grover 		goto err_vzalloc;
16167c9e7a6fSAndy Grover 	}
16177c9e7a6fSAndy Grover 
16187c9e7a6fSAndy Grover 	/* mailbox fits in first part of CMDR space */
16197c9e7a6fSAndy Grover 	udev->cmdr_size = CMDR_SIZE - CMDR_OFF;
16207c9e7a6fSAndy Grover 	udev->data_off = CMDR_SIZE;
1621141685a3SXiubo Li 	udev->data_size = DATA_SIZE;
1622b6df4b79SXiubo Li 	udev->dbi_thresh = 0; /* Default in Idle state */
16237c9e7a6fSAndy Grover 
1624141685a3SXiubo Li 	/* Initialise the mailbox of the ring buffer */
16257c9e7a6fSAndy Grover 	mb = udev->mb_addr;
16260ad46af8SAndy Grover 	mb->version = TCMU_MAILBOX_VERSION;
162732c76de3SSheng Yang 	mb->flags = TCMU_MAILBOX_FLAG_CAP_OOOC;
16287c9e7a6fSAndy Grover 	mb->cmdr_off = CMDR_OFF;
16297c9e7a6fSAndy Grover 	mb->cmdr_size = udev->cmdr_size;
16307c9e7a6fSAndy Grover 
16317c9e7a6fSAndy Grover 	WARN_ON(!PAGE_ALIGNED(udev->data_off));
16327c9e7a6fSAndy Grover 	WARN_ON(udev->data_size % PAGE_SIZE);
163326418649SSheng Yang 	WARN_ON(udev->data_size % DATA_BLOCK_SIZE);
16347c9e7a6fSAndy Grover 
1635ac64a2ceSDavid Disseldorp 	info->version = __stringify(TCMU_MAILBOX_VERSION);
16367c9e7a6fSAndy Grover 
16377c9e7a6fSAndy Grover 	info->mem[0].name = "tcm-user command & data buffer";
16380633e123SArnd Bergmann 	info->mem[0].addr = (phys_addr_t)(uintptr_t)udev->mb_addr;
16397c9e7a6fSAndy Grover 	info->mem[0].size = TCMU_RING_SIZE;
1640141685a3SXiubo Li 	info->mem[0].memtype = UIO_MEM_NONE;
16417c9e7a6fSAndy Grover 
16427c9e7a6fSAndy Grover 	info->irqcontrol = tcmu_irqcontrol;
16437c9e7a6fSAndy Grover 	info->irq = UIO_IRQ_CUSTOM;
16447c9e7a6fSAndy Grover 
16457c9e7a6fSAndy Grover 	info->mmap = tcmu_mmap;
16467c9e7a6fSAndy Grover 	info->open = tcmu_open;
16477c9e7a6fSAndy Grover 	info->release = tcmu_release;
16487c9e7a6fSAndy Grover 
16497c9e7a6fSAndy Grover 	ret = uio_register_device(tcmu_root_device, info);
16507c9e7a6fSAndy Grover 	if (ret)
16517c9e7a6fSAndy Grover 		goto err_register;
16527c9e7a6fSAndy Grover 
165381ee28deSSheng Yang 	/* User can set hw_block_size before enable the device */
165481ee28deSSheng Yang 	if (dev->dev_attrib.hw_block_size == 0)
16557c9e7a6fSAndy Grover 		dev->dev_attrib.hw_block_size = 512;
165681ee28deSSheng Yang 	/* Other attributes can be configured in userspace */
16573abaa2bfSMike Christie 	if (!dev->dev_attrib.hw_max_sectors)
16587c9e7a6fSAndy Grover 		dev->dev_attrib.hw_max_sectors = 128;
16599a8bb606SBryant G. Ly 	if (!dev->dev_attrib.emulate_write_cache)
16609a8bb606SBryant G. Ly 		dev->dev_attrib.emulate_write_cache = 0;
16617c9e7a6fSAndy Grover 	dev->dev_attrib.hw_queue_depth = 128;
16627c9e7a6fSAndy Grover 
1663b849b456SKenjiro Nakayama 	/* If user didn't explicitly disable netlink reply support, use
1664b849b456SKenjiro Nakayama 	 * module scope setting.
1665b849b456SKenjiro Nakayama 	 */
1666b849b456SKenjiro Nakayama 	if (udev->nl_reply_supported >= 0)
1667b849b456SKenjiro Nakayama 		udev->nl_reply_supported = tcmu_kern_cmd_reply_supported;
1668b849b456SKenjiro Nakayama 
1669f3cdbe39SMike Christie 	/*
1670f3cdbe39SMike Christie 	 * Get a ref incase userspace does a close on the uio device before
1671f3cdbe39SMike Christie 	 * LIO has initiated tcmu_free_device.
1672f3cdbe39SMike Christie 	 */
1673f3cdbe39SMike Christie 	kref_get(&udev->kref);
1674f3cdbe39SMike Christie 
1675b3af66e2SMike Christie 	ret = tcmu_netlink_event(udev, TCMU_CMD_ADDED_DEVICE, 0, NULL);
16767c9e7a6fSAndy Grover 	if (ret)
16777c9e7a6fSAndy Grover 		goto err_netlink;
16787c9e7a6fSAndy Grover 
1679b6df4b79SXiubo Li 	mutex_lock(&root_udev_mutex);
1680b6df4b79SXiubo Li 	list_add(&udev->node, &root_udev);
1681b6df4b79SXiubo Li 	mutex_unlock(&root_udev_mutex);
1682b6df4b79SXiubo Li 
16837c9e7a6fSAndy Grover 	return 0;
16847c9e7a6fSAndy Grover 
16857c9e7a6fSAndy Grover err_netlink:
1686f3cdbe39SMike Christie 	kref_put(&udev->kref, tcmu_dev_kref_release);
16877c9e7a6fSAndy Grover 	uio_unregister_device(&udev->uio_info);
16887c9e7a6fSAndy Grover err_register:
16897c9e7a6fSAndy Grover 	vfree(udev->mb_addr);
1690c22adc0bSXiubo Li 	udev->mb_addr = NULL;
16917c9e7a6fSAndy Grover err_vzalloc:
16927c9e7a6fSAndy Grover 	kfree(info->name);
1693f3cdbe39SMike Christie 	info->name = NULL;
16947c9e7a6fSAndy Grover 
16957c9e7a6fSAndy Grover 	return ret;
16967c9e7a6fSAndy Grover }
16977c9e7a6fSAndy Grover 
1698972c7f16SMike Christie static bool tcmu_dev_configured(struct tcmu_dev *udev)
1699972c7f16SMike Christie {
1700972c7f16SMike Christie 	return udev->uio_info.uio_dev ? true : false;
1701972c7f16SMike Christie }
1702972c7f16SMike Christie 
17037c9e7a6fSAndy Grover static void tcmu_free_device(struct se_device *dev)
17047c9e7a6fSAndy Grover {
17057c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
170692634706SMike Christie 
170792634706SMike Christie 	/* release ref from init */
170892634706SMike Christie 	kref_put(&udev->kref, tcmu_dev_kref_release);
170992634706SMike Christie }
171092634706SMike Christie 
171192634706SMike Christie static void tcmu_destroy_device(struct se_device *dev)
171292634706SMike Christie {
171392634706SMike Christie 	struct tcmu_dev *udev = TCMU_DEV(dev);
17147c9e7a6fSAndy Grover 
17157c9e7a6fSAndy Grover 	del_timer_sync(&udev->timeout);
17167c9e7a6fSAndy Grover 
1717b6df4b79SXiubo Li 	mutex_lock(&root_udev_mutex);
1718b6df4b79SXiubo Li 	list_del(&udev->node);
1719b6df4b79SXiubo Li 	mutex_unlock(&root_udev_mutex);
1720b6df4b79SXiubo Li 
1721b3af66e2SMike Christie 	tcmu_netlink_event(udev, TCMU_CMD_REMOVED_DEVICE, 0, NULL);
17227c9e7a6fSAndy Grover 
17237c9e7a6fSAndy Grover 	uio_unregister_device(&udev->uio_info);
17249260695dSMike Christie 
17259260695dSMike Christie 	/* release ref from configure */
17269260695dSMike Christie 	kref_put(&udev->kref, tcmu_dev_kref_release);
17277c9e7a6fSAndy Grover }
17287c9e7a6fSAndy Grover 
17297c9e7a6fSAndy Grover enum {
17303abaa2bfSMike Christie 	Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_hw_max_sectors,
1731b849b456SKenjiro Nakayama 	Opt_nl_reply_supported, Opt_err,
17327c9e7a6fSAndy Grover };
17337c9e7a6fSAndy Grover 
17347c9e7a6fSAndy Grover static match_table_t tokens = {
17357c9e7a6fSAndy Grover 	{Opt_dev_config, "dev_config=%s"},
17367c9e7a6fSAndy Grover 	{Opt_dev_size, "dev_size=%u"},
17379c1cd1b6SAndy Grover 	{Opt_hw_block_size, "hw_block_size=%u"},
17383abaa2bfSMike Christie 	{Opt_hw_max_sectors, "hw_max_sectors=%u"},
1739b849b456SKenjiro Nakayama 	{Opt_nl_reply_supported, "nl_reply_supported=%d"},
17407c9e7a6fSAndy Grover 	{Opt_err, NULL}
17417c9e7a6fSAndy Grover };
17427c9e7a6fSAndy Grover 
17433abaa2bfSMike Christie static int tcmu_set_dev_attrib(substring_t *arg, u32 *dev_attrib)
17443abaa2bfSMike Christie {
17453abaa2bfSMike Christie 	unsigned long tmp_ul;
17463abaa2bfSMike Christie 	char *arg_p;
17473abaa2bfSMike Christie 	int ret;
17483abaa2bfSMike Christie 
17493abaa2bfSMike Christie 	arg_p = match_strdup(arg);
17503abaa2bfSMike Christie 	if (!arg_p)
17513abaa2bfSMike Christie 		return -ENOMEM;
17523abaa2bfSMike Christie 
17533abaa2bfSMike Christie 	ret = kstrtoul(arg_p, 0, &tmp_ul);
17543abaa2bfSMike Christie 	kfree(arg_p);
17553abaa2bfSMike Christie 	if (ret < 0) {
17563abaa2bfSMike Christie 		pr_err("kstrtoul() failed for dev attrib\n");
17573abaa2bfSMike Christie 		return ret;
17583abaa2bfSMike Christie 	}
17593abaa2bfSMike Christie 	if (!tmp_ul) {
17603abaa2bfSMike Christie 		pr_err("dev attrib must be nonzero\n");
17613abaa2bfSMike Christie 		return -EINVAL;
17623abaa2bfSMike Christie 	}
17633abaa2bfSMike Christie 	*dev_attrib = tmp_ul;
17643abaa2bfSMike Christie 	return 0;
17653abaa2bfSMike Christie }
17663abaa2bfSMike Christie 
17677c9e7a6fSAndy Grover static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev,
17687c9e7a6fSAndy Grover 		const char *page, ssize_t count)
17697c9e7a6fSAndy Grover {
17707c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
17717c9e7a6fSAndy Grover 	char *orig, *ptr, *opts, *arg_p;
17727c9e7a6fSAndy Grover 	substring_t args[MAX_OPT_ARGS];
17737c9e7a6fSAndy Grover 	int ret = 0, token;
17747c9e7a6fSAndy Grover 
17757c9e7a6fSAndy Grover 	opts = kstrdup(page, GFP_KERNEL);
17767c9e7a6fSAndy Grover 	if (!opts)
17777c9e7a6fSAndy Grover 		return -ENOMEM;
17787c9e7a6fSAndy Grover 
17797c9e7a6fSAndy Grover 	orig = opts;
17807c9e7a6fSAndy Grover 
17817c9e7a6fSAndy Grover 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
17827c9e7a6fSAndy Grover 		if (!*ptr)
17837c9e7a6fSAndy Grover 			continue;
17847c9e7a6fSAndy Grover 
17857c9e7a6fSAndy Grover 		token = match_token(ptr, tokens, args);
17867c9e7a6fSAndy Grover 		switch (token) {
17877c9e7a6fSAndy Grover 		case Opt_dev_config:
17887c9e7a6fSAndy Grover 			if (match_strlcpy(udev->dev_config, &args[0],
17897c9e7a6fSAndy Grover 					  TCMU_CONFIG_LEN) == 0) {
17907c9e7a6fSAndy Grover 				ret = -EINVAL;
17917c9e7a6fSAndy Grover 				break;
17927c9e7a6fSAndy Grover 			}
17937c9e7a6fSAndy Grover 			pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config);
17947c9e7a6fSAndy Grover 			break;
17957c9e7a6fSAndy Grover 		case Opt_dev_size:
17967c9e7a6fSAndy Grover 			arg_p = match_strdup(&args[0]);
17977c9e7a6fSAndy Grover 			if (!arg_p) {
17987c9e7a6fSAndy Grover 				ret = -ENOMEM;
17997c9e7a6fSAndy Grover 				break;
18007c9e7a6fSAndy Grover 			}
18017c9e7a6fSAndy Grover 			ret = kstrtoul(arg_p, 0, (unsigned long *) &udev->dev_size);
18027c9e7a6fSAndy Grover 			kfree(arg_p);
18037c9e7a6fSAndy Grover 			if (ret < 0)
18047c9e7a6fSAndy Grover 				pr_err("kstrtoul() failed for dev_size=\n");
18057c9e7a6fSAndy Grover 			break;
18069c1cd1b6SAndy Grover 		case Opt_hw_block_size:
18073abaa2bfSMike Christie 			ret = tcmu_set_dev_attrib(&args[0],
18083abaa2bfSMike Christie 					&(dev->dev_attrib.hw_block_size));
18099c1cd1b6SAndy Grover 			break;
18103abaa2bfSMike Christie 		case Opt_hw_max_sectors:
18113abaa2bfSMike Christie 			ret = tcmu_set_dev_attrib(&args[0],
18123abaa2bfSMike Christie 					&(dev->dev_attrib.hw_max_sectors));
18139c1cd1b6SAndy Grover 			break;
1814b849b456SKenjiro Nakayama 		case Opt_nl_reply_supported:
1815b849b456SKenjiro Nakayama 			arg_p = match_strdup(&args[0]);
1816b849b456SKenjiro Nakayama 			if (!arg_p) {
1817b849b456SKenjiro Nakayama 				ret = -ENOMEM;
1818b849b456SKenjiro Nakayama 				break;
1819b849b456SKenjiro Nakayama 			}
182016b93277SDan Carpenter 			ret = kstrtoint(arg_p, 0, &udev->nl_reply_supported);
1821b849b456SKenjiro Nakayama 			kfree(arg_p);
1822b849b456SKenjiro Nakayama 			if (ret < 0)
182316b93277SDan Carpenter 				pr_err("kstrtoint() failed for nl_reply_supported=\n");
1824b849b456SKenjiro Nakayama 			break;
18257c9e7a6fSAndy Grover 		default:
18267c9e7a6fSAndy Grover 			break;
18277c9e7a6fSAndy Grover 		}
18282579325cSMike Christie 
18292579325cSMike Christie 		if (ret)
18302579325cSMike Christie 			break;
18317c9e7a6fSAndy Grover 	}
18327c9e7a6fSAndy Grover 
18337c9e7a6fSAndy Grover 	kfree(orig);
18347c9e7a6fSAndy Grover 	return (!ret) ? count : ret;
18357c9e7a6fSAndy Grover }
18367c9e7a6fSAndy Grover 
18377c9e7a6fSAndy Grover static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b)
18387c9e7a6fSAndy Grover {
18397c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
18407c9e7a6fSAndy Grover 	ssize_t bl = 0;
18417c9e7a6fSAndy Grover 
18427c9e7a6fSAndy Grover 	bl = sprintf(b + bl, "Config: %s ",
18437c9e7a6fSAndy Grover 		     udev->dev_config[0] ? udev->dev_config : "NULL");
18447d7a7435SNicholas Bellinger 	bl += sprintf(b + bl, "Size: %zu\n", udev->dev_size);
18457c9e7a6fSAndy Grover 
18467c9e7a6fSAndy Grover 	return bl;
18477c9e7a6fSAndy Grover }
18487c9e7a6fSAndy Grover 
18497c9e7a6fSAndy Grover static sector_t tcmu_get_blocks(struct se_device *dev)
18507c9e7a6fSAndy Grover {
18517c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
18527c9e7a6fSAndy Grover 
18537c9e7a6fSAndy Grover 	return div_u64(udev->dev_size - dev->dev_attrib.block_size,
18547c9e7a6fSAndy Grover 		       dev->dev_attrib.block_size);
18557c9e7a6fSAndy Grover }
18567c9e7a6fSAndy Grover 
18577c9e7a6fSAndy Grover static sense_reason_t
18587c9e7a6fSAndy Grover tcmu_parse_cdb(struct se_cmd *cmd)
18597c9e7a6fSAndy Grover {
186002eb924fSAndy Grover 	return passthrough_parse_cdb(cmd, tcmu_queue_cmd);
18619c1cd1b6SAndy Grover }
18629c1cd1b6SAndy Grover 
18637d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_show(struct config_item *item, char *page)
18647d7a7435SNicholas Bellinger {
18657d7a7435SNicholas Bellinger 	struct se_dev_attrib *da = container_of(to_config_group(item),
18667d7a7435SNicholas Bellinger 					struct se_dev_attrib, da_group);
1867b5ab697cSKenjiro Nakayama 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
18687d7a7435SNicholas Bellinger 
18697d7a7435SNicholas Bellinger 	return snprintf(page, PAGE_SIZE, "%lu\n", udev->cmd_time_out / MSEC_PER_SEC);
18707d7a7435SNicholas Bellinger }
18717d7a7435SNicholas Bellinger 
18727d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_store(struct config_item *item, const char *page,
18737d7a7435SNicholas Bellinger 				       size_t count)
18747d7a7435SNicholas Bellinger {
18757d7a7435SNicholas Bellinger 	struct se_dev_attrib *da = container_of(to_config_group(item),
18767d7a7435SNicholas Bellinger 					struct se_dev_attrib, da_group);
18777d7a7435SNicholas Bellinger 	struct tcmu_dev *udev = container_of(da->da_dev,
18787d7a7435SNicholas Bellinger 					struct tcmu_dev, se_dev);
18797d7a7435SNicholas Bellinger 	u32 val;
18807d7a7435SNicholas Bellinger 	int ret;
18817d7a7435SNicholas Bellinger 
18827d7a7435SNicholas Bellinger 	if (da->da_dev->export_count) {
18837d7a7435SNicholas Bellinger 		pr_err("Unable to set tcmu cmd_time_out while exports exist\n");
18847d7a7435SNicholas Bellinger 		return -EINVAL;
18857d7a7435SNicholas Bellinger 	}
18867d7a7435SNicholas Bellinger 
18877d7a7435SNicholas Bellinger 	ret = kstrtou32(page, 0, &val);
18887d7a7435SNicholas Bellinger 	if (ret < 0)
18897d7a7435SNicholas Bellinger 		return ret;
18907d7a7435SNicholas Bellinger 
18917d7a7435SNicholas Bellinger 	udev->cmd_time_out = val * MSEC_PER_SEC;
18927d7a7435SNicholas Bellinger 	return count;
18937d7a7435SNicholas Bellinger }
18947d7a7435SNicholas Bellinger CONFIGFS_ATTR(tcmu_, cmd_time_out);
18957d7a7435SNicholas Bellinger 
18962d76443eSMike Christie static ssize_t tcmu_dev_config_show(struct config_item *item, char *page)
1897ee018252SBryant G. Ly {
1898ee018252SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
1899ee018252SBryant G. Ly 						struct se_dev_attrib, da_group);
1900ee018252SBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
1901ee018252SBryant G. Ly 
1902ee018252SBryant G. Ly 	return snprintf(page, PAGE_SIZE, "%s\n", udev->dev_config);
1903ee018252SBryant G. Ly }
1904ee018252SBryant G. Ly 
19052d76443eSMike Christie static ssize_t tcmu_dev_config_store(struct config_item *item, const char *page,
1906ee018252SBryant G. Ly 				     size_t count)
1907ee018252SBryant G. Ly {
1908ee018252SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
1909ee018252SBryant G. Ly 						struct se_dev_attrib, da_group);
1910ee018252SBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
19112d76443eSMike Christie 	int ret, len;
1912ee018252SBryant G. Ly 
19132d76443eSMike Christie 	len = strlen(page);
19142d76443eSMike Christie 	if (!len || len > TCMU_CONFIG_LEN - 1)
1915ee018252SBryant G. Ly 		return -EINVAL;
1916ee018252SBryant G. Ly 
1917ee018252SBryant G. Ly 	/* Check if device has been configured before */
1918ee018252SBryant G. Ly 	if (tcmu_dev_configured(udev)) {
1919b3af66e2SMike Christie 		ret = tcmu_netlink_event(udev, TCMU_CMD_RECONFIG_DEVICE,
19202d76443eSMike Christie 					 TCMU_ATTR_DEV_CFG, page);
1921ee018252SBryant G. Ly 		if (ret) {
1922ee018252SBryant G. Ly 			pr_err("Unable to reconfigure device\n");
1923ee018252SBryant G. Ly 			return ret;
1924ee018252SBryant G. Ly 		}
1925de8c5221SBryant G. Ly 		strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN);
1926de8c5221SBryant G. Ly 
1927de8c5221SBryant G. Ly 		ret = tcmu_update_uio_info(udev);
1928de8c5221SBryant G. Ly 		if (ret)
1929de8c5221SBryant G. Ly 			return ret;
1930de8c5221SBryant G. Ly 		return count;
1931ee018252SBryant G. Ly 	}
19322d76443eSMike Christie 	strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN);
1933ee018252SBryant G. Ly 
1934ee018252SBryant G. Ly 	return count;
1935ee018252SBryant G. Ly }
19362d76443eSMike Christie CONFIGFS_ATTR(tcmu_, dev_config);
1937ee018252SBryant G. Ly 
1938801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_show(struct config_item *item, char *page)
1939801fc54dSBryant G. Ly {
1940801fc54dSBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
1941801fc54dSBryant G. Ly 						struct se_dev_attrib, da_group);
1942801fc54dSBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
1943801fc54dSBryant G. Ly 
1944801fc54dSBryant G. Ly 	return snprintf(page, PAGE_SIZE, "%zu\n", udev->dev_size);
1945801fc54dSBryant G. Ly }
1946801fc54dSBryant G. Ly 
1947801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_store(struct config_item *item, const char *page,
1948801fc54dSBryant G. Ly 				   size_t count)
1949801fc54dSBryant G. Ly {
1950801fc54dSBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
1951801fc54dSBryant G. Ly 						struct se_dev_attrib, da_group);
1952801fc54dSBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
19532d76443eSMike Christie 	u64 val;
1954801fc54dSBryant G. Ly 	int ret;
1955801fc54dSBryant G. Ly 
19562d76443eSMike Christie 	ret = kstrtou64(page, 0, &val);
1957801fc54dSBryant G. Ly 	if (ret < 0)
1958801fc54dSBryant G. Ly 		return ret;
1959801fc54dSBryant G. Ly 
1960801fc54dSBryant G. Ly 	/* Check if device has been configured before */
1961801fc54dSBryant G. Ly 	if (tcmu_dev_configured(udev)) {
1962b3af66e2SMike Christie 		ret = tcmu_netlink_event(udev, TCMU_CMD_RECONFIG_DEVICE,
19632d76443eSMike Christie 					 TCMU_ATTR_DEV_SIZE, &val);
1964801fc54dSBryant G. Ly 		if (ret) {
1965801fc54dSBryant G. Ly 			pr_err("Unable to reconfigure device\n");
1966801fc54dSBryant G. Ly 			return ret;
1967801fc54dSBryant G. Ly 		}
1968801fc54dSBryant G. Ly 	}
19692d76443eSMike Christie 	udev->dev_size = val;
1970801fc54dSBryant G. Ly 	return count;
1971801fc54dSBryant G. Ly }
1972801fc54dSBryant G. Ly CONFIGFS_ATTR(tcmu_, dev_size);
1973801fc54dSBryant G. Ly 
1974b849b456SKenjiro Nakayama static ssize_t tcmu_nl_reply_supported_show(struct config_item *item,
1975b849b456SKenjiro Nakayama 		char *page)
1976b849b456SKenjiro Nakayama {
1977b849b456SKenjiro Nakayama 	struct se_dev_attrib *da = container_of(to_config_group(item),
1978b849b456SKenjiro Nakayama 						struct se_dev_attrib, da_group);
1979b849b456SKenjiro Nakayama 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
1980b849b456SKenjiro Nakayama 
1981b849b456SKenjiro Nakayama 	return snprintf(page, PAGE_SIZE, "%d\n", udev->nl_reply_supported);
1982b849b456SKenjiro Nakayama }
1983b849b456SKenjiro Nakayama 
1984b849b456SKenjiro Nakayama static ssize_t tcmu_nl_reply_supported_store(struct config_item *item,
1985b849b456SKenjiro Nakayama 		const char *page, size_t count)
1986b849b456SKenjiro Nakayama {
1987b849b456SKenjiro Nakayama 	struct se_dev_attrib *da = container_of(to_config_group(item),
1988b849b456SKenjiro Nakayama 						struct se_dev_attrib, da_group);
1989b849b456SKenjiro Nakayama 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
1990b849b456SKenjiro Nakayama 	s8 val;
1991b849b456SKenjiro Nakayama 	int ret;
1992b849b456SKenjiro Nakayama 
1993b849b456SKenjiro Nakayama 	ret = kstrtos8(page, 0, &val);
1994b849b456SKenjiro Nakayama 	if (ret < 0)
1995b849b456SKenjiro Nakayama 		return ret;
1996b849b456SKenjiro Nakayama 
1997b849b456SKenjiro Nakayama 	udev->nl_reply_supported = val;
1998b849b456SKenjiro Nakayama 	return count;
1999b849b456SKenjiro Nakayama }
2000b849b456SKenjiro Nakayama CONFIGFS_ATTR(tcmu_, nl_reply_supported);
2001b849b456SKenjiro Nakayama 
20029a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_show(struct config_item *item,
20039a8bb606SBryant G. Ly 					     char *page)
20049a8bb606SBryant G. Ly {
20059a8bb606SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
20069a8bb606SBryant G. Ly 					struct se_dev_attrib, da_group);
20079a8bb606SBryant G. Ly 
20089a8bb606SBryant G. Ly 	return snprintf(page, PAGE_SIZE, "%i\n", da->emulate_write_cache);
20099a8bb606SBryant G. Ly }
20109a8bb606SBryant G. Ly 
20119a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_store(struct config_item *item,
20129a8bb606SBryant G. Ly 					      const char *page, size_t count)
20139a8bb606SBryant G. Ly {
20149a8bb606SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
20159a8bb606SBryant G. Ly 					struct se_dev_attrib, da_group);
20161068be7bSBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
20172d76443eSMike Christie 	u8 val;
20189a8bb606SBryant G. Ly 	int ret;
20199a8bb606SBryant G. Ly 
20202d76443eSMike Christie 	ret = kstrtou8(page, 0, &val);
20219a8bb606SBryant G. Ly 	if (ret < 0)
20229a8bb606SBryant G. Ly 		return ret;
20239a8bb606SBryant G. Ly 
20241068be7bSBryant G. Ly 	/* Check if device has been configured before */
20251068be7bSBryant G. Ly 	if (tcmu_dev_configured(udev)) {
2026b3af66e2SMike Christie 		ret = tcmu_netlink_event(udev, TCMU_CMD_RECONFIG_DEVICE,
20272d76443eSMike Christie 					 TCMU_ATTR_WRITECACHE, &val);
20281068be7bSBryant G. Ly 		if (ret) {
20291068be7bSBryant G. Ly 			pr_err("Unable to reconfigure device\n");
20301068be7bSBryant G. Ly 			return ret;
20311068be7bSBryant G. Ly 		}
20321068be7bSBryant G. Ly 	}
20332d76443eSMike Christie 
20342d76443eSMike Christie 	da->emulate_write_cache = val;
20359a8bb606SBryant G. Ly 	return count;
20369a8bb606SBryant G. Ly }
20379a8bb606SBryant G. Ly CONFIGFS_ATTR(tcmu_, emulate_write_cache);
20389a8bb606SBryant G. Ly 
20395821783bSColin Ian King static struct configfs_attribute *tcmu_attrib_attrs[] = {
2040801fc54dSBryant G. Ly 	&tcmu_attr_cmd_time_out,
20412d76443eSMike Christie 	&tcmu_attr_dev_config,
2042801fc54dSBryant G. Ly 	&tcmu_attr_dev_size,
2043801fc54dSBryant G. Ly 	&tcmu_attr_emulate_write_cache,
2044b849b456SKenjiro Nakayama 	&tcmu_attr_nl_reply_supported,
2045801fc54dSBryant G. Ly 	NULL,
2046801fc54dSBryant G. Ly };
2047801fc54dSBryant G. Ly 
20487d7a7435SNicholas Bellinger static struct configfs_attribute **tcmu_attrs;
20497d7a7435SNicholas Bellinger 
20507d7a7435SNicholas Bellinger static struct target_backend_ops tcmu_ops = {
20517c9e7a6fSAndy Grover 	.name			= "user",
20527c9e7a6fSAndy Grover 	.owner			= THIS_MODULE,
2053a3541703SAndy Grover 	.transport_flags	= TRANSPORT_FLAG_PASSTHROUGH,
20547c9e7a6fSAndy Grover 	.attach_hba		= tcmu_attach_hba,
20557c9e7a6fSAndy Grover 	.detach_hba		= tcmu_detach_hba,
20567c9e7a6fSAndy Grover 	.alloc_device		= tcmu_alloc_device,
20577c9e7a6fSAndy Grover 	.configure_device	= tcmu_configure_device,
205892634706SMike Christie 	.destroy_device		= tcmu_destroy_device,
20597c9e7a6fSAndy Grover 	.free_device		= tcmu_free_device,
20607c9e7a6fSAndy Grover 	.parse_cdb		= tcmu_parse_cdb,
20617c9e7a6fSAndy Grover 	.set_configfs_dev_params = tcmu_set_configfs_dev_params,
20627c9e7a6fSAndy Grover 	.show_configfs_dev_params = tcmu_show_configfs_dev_params,
20637c9e7a6fSAndy Grover 	.get_device_type	= sbc_get_device_type,
20647c9e7a6fSAndy Grover 	.get_blocks		= tcmu_get_blocks,
20657d7a7435SNicholas Bellinger 	.tb_dev_attrib_attrs	= NULL,
20667c9e7a6fSAndy Grover };
20677c9e7a6fSAndy Grover 
206889ec9cfdSMike Christie static void find_free_blocks(void)
2069b6df4b79SXiubo Li {
2070b6df4b79SXiubo Li 	struct tcmu_dev *udev;
2071b6df4b79SXiubo Li 	loff_t off;
2072af1dd7ffSMike Christie 	u32 start, end, block, total_freed = 0;
2073af1dd7ffSMike Christie 
2074af1dd7ffSMike Christie 	if (atomic_read(&global_db_count) <= TCMU_GLOBAL_MAX_BLOCKS)
2075af1dd7ffSMike Christie 		return;
2076b6df4b79SXiubo Li 
2077b6df4b79SXiubo Li 	mutex_lock(&root_udev_mutex);
2078b6df4b79SXiubo Li 	list_for_each_entry(udev, &root_udev, node) {
2079b6df4b79SXiubo Li 		mutex_lock(&udev->cmdr_lock);
2080b6df4b79SXiubo Li 
2081b6df4b79SXiubo Li 		/* Try to complete the finished commands first */
2082b6df4b79SXiubo Li 		tcmu_handle_completions(udev);
2083b6df4b79SXiubo Li 
2084af1dd7ffSMike Christie 		/* Skip the udevs in idle */
2085af1dd7ffSMike Christie 		if (!udev->dbi_thresh) {
2086b6df4b79SXiubo Li 			mutex_unlock(&udev->cmdr_lock);
2087b6df4b79SXiubo Li 			continue;
2088b6df4b79SXiubo Li 		}
2089b6df4b79SXiubo Li 
2090b6df4b79SXiubo Li 		end = udev->dbi_max + 1;
2091b6df4b79SXiubo Li 		block = find_last_bit(udev->data_bitmap, end);
2092b6df4b79SXiubo Li 		if (block == udev->dbi_max) {
2093b6df4b79SXiubo Li 			/*
2094af1dd7ffSMike Christie 			 * The last bit is dbi_max, so it is not possible
2095af1dd7ffSMike Christie 			 * reclaim any blocks.
2096b6df4b79SXiubo Li 			 */
2097b6df4b79SXiubo Li 			mutex_unlock(&udev->cmdr_lock);
2098b6df4b79SXiubo Li 			continue;
2099b6df4b79SXiubo Li 		} else if (block == end) {
2100b6df4b79SXiubo Li 			/* The current udev will goto idle state */
2101b6df4b79SXiubo Li 			udev->dbi_thresh = start = 0;
2102b6df4b79SXiubo Li 			udev->dbi_max = 0;
2103b6df4b79SXiubo Li 		} else {
2104b6df4b79SXiubo Li 			udev->dbi_thresh = start = block + 1;
2105b6df4b79SXiubo Li 			udev->dbi_max = block;
2106b6df4b79SXiubo Li 		}
2107b6df4b79SXiubo Li 
2108b6df4b79SXiubo Li 		/* Here will truncate the data area from off */
2109b6df4b79SXiubo Li 		off = udev->data_off + start * DATA_BLOCK_SIZE;
2110b6df4b79SXiubo Li 		unmap_mapping_range(udev->inode->i_mapping, off, 0, 1);
2111b6df4b79SXiubo Li 
2112b6df4b79SXiubo Li 		/* Release the block pages */
2113bf99ec13SMike Christie 		tcmu_blocks_release(&udev->data_blocks, start, end);
2114b6df4b79SXiubo Li 		mutex_unlock(&udev->cmdr_lock);
2115af1dd7ffSMike Christie 
2116af1dd7ffSMike Christie 		total_freed += end - start;
2117af1dd7ffSMike Christie 		pr_debug("Freed %u blocks (total %u) from %s.\n", end - start,
2118af1dd7ffSMike Christie 			 total_freed, udev->name);
2119b6df4b79SXiubo Li 	}
212089ec9cfdSMike Christie 	mutex_unlock(&root_udev_mutex);
212189ec9cfdSMike Christie 
2122af1dd7ffSMike Christie 	if (atomic_read(&global_db_count) > TCMU_GLOBAL_MAX_BLOCKS)
2123af1dd7ffSMike Christie 		schedule_delayed_work(&tcmu_unmap_work, msecs_to_jiffies(5000));
2124b6df4b79SXiubo Li }
2125b6df4b79SXiubo Li 
2126488ebe4cSMike Christie static void check_timedout_devices(void)
2127488ebe4cSMike Christie {
2128488ebe4cSMike Christie 	struct tcmu_dev *udev, *tmp_dev;
2129488ebe4cSMike Christie 	LIST_HEAD(devs);
2130488ebe4cSMike Christie 
2131488ebe4cSMike Christie 	spin_lock_bh(&timed_out_udevs_lock);
2132488ebe4cSMike Christie 	list_splice_init(&timed_out_udevs, &devs);
2133488ebe4cSMike Christie 
2134488ebe4cSMike Christie 	list_for_each_entry_safe(udev, tmp_dev, &devs, timedout_entry) {
2135488ebe4cSMike Christie 		list_del_init(&udev->timedout_entry);
2136488ebe4cSMike Christie 		spin_unlock_bh(&timed_out_udevs_lock);
2137488ebe4cSMike Christie 
21386fddcb77SMike Christie 		mutex_lock(&udev->cmdr_lock);
2139488ebe4cSMike Christie 		idr_for_each(&udev->commands, tcmu_check_expired_cmd, NULL);
21406fddcb77SMike Christie 		mutex_unlock(&udev->cmdr_lock);
2141488ebe4cSMike Christie 
2142488ebe4cSMike Christie 		spin_lock_bh(&timed_out_udevs_lock);
2143488ebe4cSMike Christie 	}
2144488ebe4cSMike Christie 
2145488ebe4cSMike Christie 	spin_unlock_bh(&timed_out_udevs_lock);
2146488ebe4cSMike Christie }
2147488ebe4cSMike Christie 
21489972cebbSMike Christie static void tcmu_unmap_work_fn(struct work_struct *work)
214989ec9cfdSMike Christie {
2150488ebe4cSMike Christie 	check_timedout_devices();
215189ec9cfdSMike Christie 	find_free_blocks();
215289ec9cfdSMike Christie }
215389ec9cfdSMike Christie 
21547c9e7a6fSAndy Grover static int __init tcmu_module_init(void)
21557c9e7a6fSAndy Grover {
2156801fc54dSBryant G. Ly 	int ret, i, k, len = 0;
21577c9e7a6fSAndy Grover 
21587c9e7a6fSAndy Grover 	BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0);
21597c9e7a6fSAndy Grover 
2160af1dd7ffSMike Christie 	INIT_DELAYED_WORK(&tcmu_unmap_work, tcmu_unmap_work_fn);
21619972cebbSMike Christie 
21627c9e7a6fSAndy Grover 	tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache",
21637c9e7a6fSAndy Grover 				sizeof(struct tcmu_cmd),
21647c9e7a6fSAndy Grover 				__alignof__(struct tcmu_cmd),
21657c9e7a6fSAndy Grover 				0, NULL);
21667c9e7a6fSAndy Grover 	if (!tcmu_cmd_cache)
21677c9e7a6fSAndy Grover 		return -ENOMEM;
21687c9e7a6fSAndy Grover 
21697c9e7a6fSAndy Grover 	tcmu_root_device = root_device_register("tcm_user");
21707c9e7a6fSAndy Grover 	if (IS_ERR(tcmu_root_device)) {
21717c9e7a6fSAndy Grover 		ret = PTR_ERR(tcmu_root_device);
21727c9e7a6fSAndy Grover 		goto out_free_cache;
21737c9e7a6fSAndy Grover 	}
21747c9e7a6fSAndy Grover 
21757c9e7a6fSAndy Grover 	ret = genl_register_family(&tcmu_genl_family);
21767c9e7a6fSAndy Grover 	if (ret < 0) {
21777c9e7a6fSAndy Grover 		goto out_unreg_device;
21787c9e7a6fSAndy Grover 	}
21797c9e7a6fSAndy Grover 
21807d7a7435SNicholas Bellinger 	for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) {
21817d7a7435SNicholas Bellinger 		len += sizeof(struct configfs_attribute *);
21827d7a7435SNicholas Bellinger 	}
2183801fc54dSBryant G. Ly 	for (i = 0; tcmu_attrib_attrs[i] != NULL; i++) {
2184801fc54dSBryant G. Ly 		len += sizeof(struct configfs_attribute *);
2185801fc54dSBryant G. Ly 	}
2186801fc54dSBryant G. Ly 	len += sizeof(struct configfs_attribute *);
21877d7a7435SNicholas Bellinger 
21887d7a7435SNicholas Bellinger 	tcmu_attrs = kzalloc(len, GFP_KERNEL);
21897d7a7435SNicholas Bellinger 	if (!tcmu_attrs) {
21907d7a7435SNicholas Bellinger 		ret = -ENOMEM;
21917d7a7435SNicholas Bellinger 		goto out_unreg_genl;
21927d7a7435SNicholas Bellinger 	}
21937d7a7435SNicholas Bellinger 
21947d7a7435SNicholas Bellinger 	for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) {
21957d7a7435SNicholas Bellinger 		tcmu_attrs[i] = passthrough_attrib_attrs[i];
21967d7a7435SNicholas Bellinger 	}
2197801fc54dSBryant G. Ly 	for (k = 0; tcmu_attrib_attrs[k] != NULL; k++) {
2198801fc54dSBryant G. Ly 		tcmu_attrs[i] = tcmu_attrib_attrs[k];
21999a8bb606SBryant G. Ly 		i++;
2200801fc54dSBryant G. Ly 	}
22017d7a7435SNicholas Bellinger 	tcmu_ops.tb_dev_attrib_attrs = tcmu_attrs;
22027d7a7435SNicholas Bellinger 
22030a06d430SChristoph Hellwig 	ret = transport_backend_register(&tcmu_ops);
22047c9e7a6fSAndy Grover 	if (ret)
22057d7a7435SNicholas Bellinger 		goto out_attrs;
22067c9e7a6fSAndy Grover 
22077c9e7a6fSAndy Grover 	return 0;
22087c9e7a6fSAndy Grover 
22097d7a7435SNicholas Bellinger out_attrs:
22107d7a7435SNicholas Bellinger 	kfree(tcmu_attrs);
22117c9e7a6fSAndy Grover out_unreg_genl:
22127c9e7a6fSAndy Grover 	genl_unregister_family(&tcmu_genl_family);
22137c9e7a6fSAndy Grover out_unreg_device:
22147c9e7a6fSAndy Grover 	root_device_unregister(tcmu_root_device);
22157c9e7a6fSAndy Grover out_free_cache:
22167c9e7a6fSAndy Grover 	kmem_cache_destroy(tcmu_cmd_cache);
22177c9e7a6fSAndy Grover 
22187c9e7a6fSAndy Grover 	return ret;
22197c9e7a6fSAndy Grover }
22207c9e7a6fSAndy Grover 
22217c9e7a6fSAndy Grover static void __exit tcmu_module_exit(void)
22227c9e7a6fSAndy Grover {
2223af1dd7ffSMike Christie 	cancel_delayed_work_sync(&tcmu_unmap_work);
22240a06d430SChristoph Hellwig 	target_backend_unregister(&tcmu_ops);
22257d7a7435SNicholas Bellinger 	kfree(tcmu_attrs);
22267c9e7a6fSAndy Grover 	genl_unregister_family(&tcmu_genl_family);
22277c9e7a6fSAndy Grover 	root_device_unregister(tcmu_root_device);
22287c9e7a6fSAndy Grover 	kmem_cache_destroy(tcmu_cmd_cache);
22297c9e7a6fSAndy Grover }
22307c9e7a6fSAndy Grover 
22317c9e7a6fSAndy Grover MODULE_DESCRIPTION("TCM USER subsystem plugin");
22327c9e7a6fSAndy Grover MODULE_AUTHOR("Shaohua Li <shli@kernel.org>");
22337c9e7a6fSAndy Grover MODULE_AUTHOR("Andy Grover <agrover@redhat.com>");
22347c9e7a6fSAndy Grover MODULE_LICENSE("GPL");
22357c9e7a6fSAndy Grover 
22367c9e7a6fSAndy Grover module_init(tcmu_module_init);
22377c9e7a6fSAndy Grover module_exit(tcmu_module_exit);
2238