1a61127c2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27c9e7a6fSAndy Grover /*
37c9e7a6fSAndy Grover  * Copyright (C) 2013 Shaohua Li <shli@kernel.org>
47c9e7a6fSAndy Grover  * Copyright (C) 2014 Red Hat, Inc.
5f97ec7dbSIlias Tsitsimpis  * Copyright (C) 2015 Arrikto, Inc.
6141685a3SXiubo Li  * Copyright (C) 2017 Chinamobile, Inc.
77c9e7a6fSAndy Grover  */
87c9e7a6fSAndy Grover 
97c9e7a6fSAndy Grover #include <linux/spinlock.h>
107c9e7a6fSAndy Grover #include <linux/module.h>
11ba929992SBart Van Assche #include <linux/kernel.h>
127c9e7a6fSAndy Grover #include <linux/timer.h>
137c9e7a6fSAndy Grover #include <linux/parser.h>
145538d294SDavid S. Miller #include <linux/vmalloc.h>
157c9e7a6fSAndy Grover #include <linux/uio_driver.h>
16d3cbb743SBodo Stroesser #include <linux/xarray.h>
17ac64a2ceSDavid Disseldorp #include <linux/stringify.h>
1826418649SSheng Yang #include <linux/bitops.h>
19f5045724SBart Van Assche #include <linux/highmem.h>
207d7a7435SNicholas Bellinger #include <linux/configfs.h>
21b6df4b79SXiubo Li #include <linux/mutex.h>
229972cebbSMike Christie #include <linux/workqueue.h>
237c9e7a6fSAndy Grover #include <net/genetlink.h>
24ba929992SBart Van Assche #include <scsi/scsi_common.h>
25ba929992SBart Van Assche #include <scsi/scsi_proto.h>
267c9e7a6fSAndy Grover #include <target/target_core_base.h>
277c9e7a6fSAndy Grover #include <target/target_core_fabric.h>
287c9e7a6fSAndy Grover #include <target/target_core_backend.h>
29e9f720d6SNicholas Bellinger 
307c9e7a6fSAndy Grover #include <linux/target_core_user.h>
317c9e7a6fSAndy Grover 
32572ccdabSRandy Dunlap /**
33572ccdabSRandy Dunlap  * DOC: Userspace I/O
34572ccdabSRandy Dunlap  * Userspace I/O
35572ccdabSRandy Dunlap  * -------------
36572ccdabSRandy Dunlap  *
377c9e7a6fSAndy Grover  * Define a shared-memory interface for LIO to pass SCSI commands and
387c9e7a6fSAndy Grover  * data to userspace for processing. This is to allow backends that
397c9e7a6fSAndy Grover  * are too complex for in-kernel support to be possible.
407c9e7a6fSAndy Grover  *
417c9e7a6fSAndy Grover  * It uses the UIO framework to do a lot of the device-creation and
427c9e7a6fSAndy Grover  * introspection work for us.
437c9e7a6fSAndy Grover  *
447c9e7a6fSAndy Grover  * See the .h file for how the ring is laid out. Note that while the
457c9e7a6fSAndy Grover  * command ring is defined, the particulars of the data area are
467c9e7a6fSAndy Grover  * not. Offset values in the command entry point to other locations
47572ccdabSRandy Dunlap  * internal to the mmap-ed area. There is separate space outside the
487c9e7a6fSAndy Grover  * command ring for data buffers. This leaves maximum flexibility for
497c9e7a6fSAndy Grover  * moving buffer allocations, or even page flipping or other
507c9e7a6fSAndy Grover  * allocation techniques, without altering the command ring layout.
517c9e7a6fSAndy Grover  *
527c9e7a6fSAndy Grover  * SECURITY:
537c9e7a6fSAndy Grover  * The user process must be assumed to be malicious. There's no way to
547c9e7a6fSAndy Grover  * prevent it breaking the command ring protocol if it wants, but in
557c9e7a6fSAndy Grover  * order to prevent other issues we must only ever read *data* from
567c9e7a6fSAndy Grover  * the shared memory area, not offsets or sizes. This applies to
577c9e7a6fSAndy Grover  * command ring entries as well as the mailbox. Extra code needed for
587c9e7a6fSAndy Grover  * this may have a 'UAM' comment.
597c9e7a6fSAndy Grover  */
607c9e7a6fSAndy Grover 
617c9e7a6fSAndy Grover #define TCMU_TIME_OUT (30 * MSEC_PER_SEC)
627c9e7a6fSAndy Grover 
63ecddbb7eSBodo Stroesser /* For mailbox plus cmd ring, the size is fixed 8MB */
64ecddbb7eSBodo Stroesser #define MB_CMDR_SIZE (8 * 1024 * 1024)
65ecddbb7eSBodo Stroesser /* Offset of cmd ring is size of mailbox */
66ecddbb7eSBodo Stroesser #define CMDR_OFF sizeof(struct tcmu_mailbox)
67ecddbb7eSBodo Stroesser #define CMDR_SIZE (MB_CMDR_SIZE - CMDR_OFF)
6826418649SSheng Yang 
69b6df4b79SXiubo Li /*
70f5ce815fSBodo Stroesser  * For data area, the default block size is PAGE_SIZE and
71f5ce815fSBodo Stroesser  * the default total size is 256K * PAGE_SIZE.
72b6df4b79SXiubo Li  */
73e719afdcSBodo Stroesser #define DATA_PAGES_PER_BLK_DEF 1
74f5ce815fSBodo Stroesser #define DATA_AREA_PAGES_DEF (256 * 1024)
757c9e7a6fSAndy Grover 
76f5ce815fSBodo Stroesser #define TCMU_MBS_TO_PAGES(_mbs) ((size_t)_mbs << (20 - PAGE_SHIFT))
778b084d9dSBodo Stroesser #define TCMU_PAGES_TO_MBS(_pages) (_pages >> (20 - PAGE_SHIFT))
7880eb8761SMike Christie 
79af1dd7ffSMike Christie /*
80af1dd7ffSMike Christie  * Default number of global data blocks(512K * PAGE_SIZE)
81af1dd7ffSMike Christie  * when the unmap thread will be started.
82af1dd7ffSMike Christie  */
838b084d9dSBodo Stroesser #define TCMU_GLOBAL_MAX_PAGES_DEF (512 * 1024)
84b6df4b79SXiubo Li 
85b3af66e2SMike Christie static u8 tcmu_kern_cmd_reply_supported;
86bdaeedc1SMike Christie static u8 tcmu_netlink_blocked;
87b3af66e2SMike Christie 
887c9e7a6fSAndy Grover static struct device *tcmu_root_device;
897c9e7a6fSAndy Grover 
907c9e7a6fSAndy Grover struct tcmu_hba {
917c9e7a6fSAndy Grover 	u32 host_id;
927c9e7a6fSAndy Grover };
937c9e7a6fSAndy Grover 
947c9e7a6fSAndy Grover #define TCMU_CONFIG_LEN 256
957c9e7a6fSAndy Grover 
963228691fSMike Christie static DEFINE_MUTEX(tcmu_nl_cmd_mutex);
973228691fSMike Christie static LIST_HEAD(tcmu_nl_cmd_list);
983228691fSMike Christie 
993228691fSMike Christie struct tcmu_dev;
1003228691fSMike Christie 
101b3af66e2SMike Christie struct tcmu_nl_cmd {
102b3af66e2SMike Christie 	/* wake up thread waiting for reply */
103b3af66e2SMike Christie 	struct completion complete;
1043228691fSMike Christie 	struct list_head nl_list;
1053228691fSMike Christie 	struct tcmu_dev *udev;
106b3af66e2SMike Christie 	int cmd;
107b3af66e2SMike Christie 	int status;
108b3af66e2SMike Christie };
109b3af66e2SMike Christie 
1107c9e7a6fSAndy Grover struct tcmu_dev {
111b6df4b79SXiubo Li 	struct list_head node;
112f3cdbe39SMike Christie 	struct kref kref;
113af1dd7ffSMike Christie 
1147c9e7a6fSAndy Grover 	struct se_device se_dev;
1156888da81SMike Christie 	struct se_dev_plug se_plug;
1167c9e7a6fSAndy Grover 
1177c9e7a6fSAndy Grover 	char *name;
1187c9e7a6fSAndy Grover 	struct se_hba *hba;
1197c9e7a6fSAndy Grover 
1207c9e7a6fSAndy Grover #define TCMU_DEV_BIT_OPEN 0
1217c9e7a6fSAndy Grover #define TCMU_DEV_BIT_BROKEN 1
122892782caSMike Christie #define TCMU_DEV_BIT_BLOCKED 2
12359526d7aSBodo Stroesser #define TCMU_DEV_BIT_TMR_NOTIFY 3
1246888da81SMike Christie #define TCM_DEV_BIT_PLUGGED 4
1257c9e7a6fSAndy Grover 	unsigned long flags;
1267c9e7a6fSAndy Grover 
1277c9e7a6fSAndy Grover 	struct uio_info uio_info;
1287c9e7a6fSAndy Grover 
129b6df4b79SXiubo Li 	struct inode *inode;
130b6df4b79SXiubo Li 
1310e0d7526SMike Christie 	uint64_t dev_size;
132ecddbb7eSBodo Stroesser 
133ecddbb7eSBodo Stroesser 	struct tcmu_mailbox *mb_addr;
134ecddbb7eSBodo Stroesser 	void *cmdr;
1357c9e7a6fSAndy Grover 	u32 cmdr_size;
1367c9e7a6fSAndy Grover 	u32 cmdr_last_cleaned;
1373d9b9555SAndy Grover 	/* Offset of data area from start of mb */
13826418649SSheng Yang 	/* Must add data_off and mb_addr to get the address */
1397c9e7a6fSAndy Grover 	size_t data_off;
140f5ce815fSBodo Stroesser 	int data_area_mb;
14180eb8761SMike Christie 	uint32_t max_blocks;
142ecddbb7eSBodo Stroesser 	size_t mmap_pages;
14326418649SSheng Yang 
144b6df4b79SXiubo Li 	struct mutex cmdr_lock;
145a94a2572SXiubo Li 	struct list_head qfull_queue;
146bc2d214aSBodo Stroesser 	struct list_head tmr_queue;
1477c9e7a6fSAndy Grover 
148141685a3SXiubo Li 	uint32_t dbi_max;
149b6df4b79SXiubo Li 	uint32_t dbi_thresh;
15080eb8761SMike Christie 	unsigned long *data_bitmap;
1518b084d9dSBodo Stroesser 	struct xarray data_pages;
152e719afdcSBodo Stroesser 	uint32_t data_pages_per_blk;
153e719afdcSBodo Stroesser 	uint32_t data_blk_size;
154141685a3SXiubo Li 
155d3cbb743SBodo Stroesser 	struct xarray commands;
1567c9e7a6fSAndy Grover 
1579103575aSMike Christie 	struct timer_list cmd_timer;
158af980e46SMike Christie 	unsigned int cmd_time_out;
159a94a2572SXiubo Li 	struct list_head inflight_queue;
1609103575aSMike Christie 
1619103575aSMike Christie 	struct timer_list qfull_timer;
1629103575aSMike Christie 	int qfull_time_out;
1639103575aSMike Christie 
164488ebe4cSMike Christie 	struct list_head timedout_entry;
1657c9e7a6fSAndy Grover 
166b3af66e2SMike Christie 	struct tcmu_nl_cmd curr_nl_cmd;
167b3af66e2SMike Christie 
1687c9e7a6fSAndy Grover 	char dev_config[TCMU_CONFIG_LEN];
169b849b456SKenjiro Nakayama 
170b849b456SKenjiro Nakayama 	int nl_reply_supported;
1717c9e7a6fSAndy Grover };
1727c9e7a6fSAndy Grover 
1737c9e7a6fSAndy Grover #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev)
1747c9e7a6fSAndy Grover 
1757c9e7a6fSAndy Grover struct tcmu_cmd {
1767c9e7a6fSAndy Grover 	struct se_cmd *se_cmd;
1777c9e7a6fSAndy Grover 	struct tcmu_dev *tcmu_dev;
178a94a2572SXiubo Li 	struct list_head queue_entry;
1797c9e7a6fSAndy Grover 
1807c9e7a6fSAndy Grover 	uint16_t cmd_id;
1817c9e7a6fSAndy Grover 
18226418649SSheng Yang 	/* Can't use se_cmd when cleaning up expired cmds, because if
1837c9e7a6fSAndy Grover 	   cmd has been completed then accessing se_cmd is off limits */
184141685a3SXiubo Li 	uint32_t dbi_cnt;
18552ef2743SBodo Stroesser 	uint32_t dbi_bidi_cnt;
186141685a3SXiubo Li 	uint32_t dbi_cur;
187141685a3SXiubo Li 	uint32_t *dbi;
1887c9e7a6fSAndy Grover 
1893c9a7c58SBodo Stroesser 	uint32_t data_len_bidi;
1903c9a7c58SBodo Stroesser 
1917c9e7a6fSAndy Grover 	unsigned long deadline;
1927c9e7a6fSAndy Grover 
1937c9e7a6fSAndy Grover #define TCMU_CMD_BIT_EXPIRED 0
1947c9e7a6fSAndy Grover 	unsigned long flags;
1957c9e7a6fSAndy Grover };
196bc2d214aSBodo Stroesser 
197bc2d214aSBodo Stroesser struct tcmu_tmr {
198bc2d214aSBodo Stroesser 	struct list_head queue_entry;
199bc2d214aSBodo Stroesser 
200bc2d214aSBodo Stroesser 	uint8_t tmr_type;
201bc2d214aSBodo Stroesser 	uint32_t tmr_cmd_cnt;
2028fdaabe1SGustavo A. R. Silva 	int16_t tmr_cmd_ids[];
203bc2d214aSBodo Stroesser };
204bc2d214aSBodo Stroesser 
205af1dd7ffSMike Christie /*
206af1dd7ffSMike Christie  * To avoid dead lock the mutex lock order should always be:
207af1dd7ffSMike Christie  *
208af1dd7ffSMike Christie  * mutex_lock(&root_udev_mutex);
209af1dd7ffSMike Christie  * ...
210af1dd7ffSMike Christie  * mutex_lock(&tcmu_dev->cmdr_lock);
211af1dd7ffSMike Christie  * mutex_unlock(&tcmu_dev->cmdr_lock);
212af1dd7ffSMike Christie  * ...
213af1dd7ffSMike Christie  * mutex_unlock(&root_udev_mutex);
214af1dd7ffSMike Christie  */
215b6df4b79SXiubo Li static DEFINE_MUTEX(root_udev_mutex);
216b6df4b79SXiubo Li static LIST_HEAD(root_udev);
217b6df4b79SXiubo Li 
218488ebe4cSMike Christie static DEFINE_SPINLOCK(timed_out_udevs_lock);
219488ebe4cSMike Christie static LIST_HEAD(timed_out_udevs);
220488ebe4cSMike Christie 
22180eb8761SMike Christie static struct kmem_cache *tcmu_cmd_cache;
22280eb8761SMike Christie 
2238b084d9dSBodo Stroesser static atomic_t global_page_count = ATOMIC_INIT(0);
224af1dd7ffSMike Christie static struct delayed_work tcmu_unmap_work;
2258b084d9dSBodo Stroesser static int tcmu_global_max_pages = TCMU_GLOBAL_MAX_PAGES_DEF;
226b6df4b79SXiubo Li 
22780eb8761SMike Christie static int tcmu_set_global_max_data_area(const char *str,
22880eb8761SMike Christie 					 const struct kernel_param *kp)
22980eb8761SMike Christie {
23080eb8761SMike Christie 	int ret, max_area_mb;
23180eb8761SMike Christie 
23280eb8761SMike Christie 	ret = kstrtoint(str, 10, &max_area_mb);
23380eb8761SMike Christie 	if (ret)
23480eb8761SMike Christie 		return -EINVAL;
23580eb8761SMike Christie 
23680eb8761SMike Christie 	if (max_area_mb <= 0) {
23780eb8761SMike Christie 		pr_err("global_max_data_area must be larger than 0.\n");
23880eb8761SMike Christie 		return -EINVAL;
23980eb8761SMike Christie 	}
24080eb8761SMike Christie 
2418b084d9dSBodo Stroesser 	tcmu_global_max_pages = TCMU_MBS_TO_PAGES(max_area_mb);
2428b084d9dSBodo Stroesser 	if (atomic_read(&global_page_count) > tcmu_global_max_pages)
24380eb8761SMike Christie 		schedule_delayed_work(&tcmu_unmap_work, 0);
24480eb8761SMike Christie 	else
24580eb8761SMike Christie 		cancel_delayed_work_sync(&tcmu_unmap_work);
24680eb8761SMike Christie 
24780eb8761SMike Christie 	return 0;
24880eb8761SMike Christie }
24980eb8761SMike Christie 
25080eb8761SMike Christie static int tcmu_get_global_max_data_area(char *buffer,
25180eb8761SMike Christie 					 const struct kernel_param *kp)
25280eb8761SMike Christie {
2538b084d9dSBodo Stroesser 	return sprintf(buffer, "%d\n", TCMU_PAGES_TO_MBS(tcmu_global_max_pages));
25480eb8761SMike Christie }
25580eb8761SMike Christie 
25680eb8761SMike Christie static const struct kernel_param_ops tcmu_global_max_data_area_op = {
25780eb8761SMike Christie 	.set = tcmu_set_global_max_data_area,
25880eb8761SMike Christie 	.get = tcmu_get_global_max_data_area,
25980eb8761SMike Christie };
26080eb8761SMike Christie 
26180eb8761SMike Christie module_param_cb(global_max_data_area_mb, &tcmu_global_max_data_area_op, NULL,
26280eb8761SMike Christie 		S_IWUSR | S_IRUGO);
26380eb8761SMike Christie MODULE_PARM_DESC(global_max_data_area_mb,
26480eb8761SMike Christie 		 "Max MBs allowed to be allocated to all the tcmu device's "
26580eb8761SMike Christie 		 "data areas.");
2667c9e7a6fSAndy Grover 
267bdaeedc1SMike Christie static int tcmu_get_block_netlink(char *buffer,
268bdaeedc1SMike Christie 				  const struct kernel_param *kp)
269bdaeedc1SMike Christie {
270bdaeedc1SMike Christie 	return sprintf(buffer, "%s\n", tcmu_netlink_blocked ?
271bdaeedc1SMike Christie 		       "blocked" : "unblocked");
272bdaeedc1SMike Christie }
273bdaeedc1SMike Christie 
274bdaeedc1SMike Christie static int tcmu_set_block_netlink(const char *str,
275bdaeedc1SMike Christie 				  const struct kernel_param *kp)
276bdaeedc1SMike Christie {
277bdaeedc1SMike Christie 	int ret;
278bdaeedc1SMike Christie 	u8 val;
279bdaeedc1SMike Christie 
280bdaeedc1SMike Christie 	ret = kstrtou8(str, 0, &val);
281bdaeedc1SMike Christie 	if (ret < 0)
282bdaeedc1SMike Christie 		return ret;
283bdaeedc1SMike Christie 
284bdaeedc1SMike Christie 	if (val > 1) {
285bdaeedc1SMike Christie 		pr_err("Invalid block netlink value %u\n", val);
286bdaeedc1SMike Christie 		return -EINVAL;
287bdaeedc1SMike Christie 	}
288bdaeedc1SMike Christie 
289bdaeedc1SMike Christie 	tcmu_netlink_blocked = val;
290bdaeedc1SMike Christie 	return 0;
291bdaeedc1SMike Christie }
292bdaeedc1SMike Christie 
293bdaeedc1SMike Christie static const struct kernel_param_ops tcmu_block_netlink_op = {
294bdaeedc1SMike Christie 	.set = tcmu_set_block_netlink,
295bdaeedc1SMike Christie 	.get = tcmu_get_block_netlink,
296bdaeedc1SMike Christie };
297bdaeedc1SMike Christie 
298bdaeedc1SMike Christie module_param_cb(block_netlink, &tcmu_block_netlink_op, NULL, S_IWUSR | S_IRUGO);
299bdaeedc1SMike Christie MODULE_PARM_DESC(block_netlink, "Block new netlink commands.");
300bdaeedc1SMike Christie 
301bdaeedc1SMike Christie static int tcmu_fail_netlink_cmd(struct tcmu_nl_cmd *nl_cmd)
302bdaeedc1SMike Christie {
303bdaeedc1SMike Christie 	struct tcmu_dev *udev = nl_cmd->udev;
304bdaeedc1SMike Christie 
305bdaeedc1SMike Christie 	if (!tcmu_netlink_blocked) {
306bdaeedc1SMike Christie 		pr_err("Could not reset device's netlink interface. Netlink is not blocked.\n");
307bdaeedc1SMike Christie 		return -EBUSY;
308bdaeedc1SMike Christie 	}
309bdaeedc1SMike Christie 
310bdaeedc1SMike Christie 	if (nl_cmd->cmd != TCMU_CMD_UNSPEC) {
311bdaeedc1SMike Christie 		pr_debug("Aborting nl cmd %d on %s\n", nl_cmd->cmd, udev->name);
312bdaeedc1SMike Christie 		nl_cmd->status = -EINTR;
313bdaeedc1SMike Christie 		list_del(&nl_cmd->nl_list);
314bdaeedc1SMike Christie 		complete(&nl_cmd->complete);
315bdaeedc1SMike Christie 	}
316bdaeedc1SMike Christie 	return 0;
317bdaeedc1SMike Christie }
318bdaeedc1SMike Christie 
319bdaeedc1SMike Christie static int tcmu_set_reset_netlink(const char *str,
320bdaeedc1SMike Christie 				  const struct kernel_param *kp)
321bdaeedc1SMike Christie {
322bdaeedc1SMike Christie 	struct tcmu_nl_cmd *nl_cmd, *tmp_cmd;
323bdaeedc1SMike Christie 	int ret;
324bdaeedc1SMike Christie 	u8 val;
325bdaeedc1SMike Christie 
326bdaeedc1SMike Christie 	ret = kstrtou8(str, 0, &val);
327bdaeedc1SMike Christie 	if (ret < 0)
328bdaeedc1SMike Christie 		return ret;
329bdaeedc1SMike Christie 
330bdaeedc1SMike Christie 	if (val != 1) {
331bdaeedc1SMike Christie 		pr_err("Invalid reset netlink value %u\n", val);
332bdaeedc1SMike Christie 		return -EINVAL;
333bdaeedc1SMike Christie 	}
334bdaeedc1SMike Christie 
335bdaeedc1SMike Christie 	mutex_lock(&tcmu_nl_cmd_mutex);
336bdaeedc1SMike Christie 	list_for_each_entry_safe(nl_cmd, tmp_cmd, &tcmu_nl_cmd_list, nl_list) {
337bdaeedc1SMike Christie 		ret = tcmu_fail_netlink_cmd(nl_cmd);
338bdaeedc1SMike Christie 		if (ret)
339bdaeedc1SMike Christie 			break;
340bdaeedc1SMike Christie 	}
341bdaeedc1SMike Christie 	mutex_unlock(&tcmu_nl_cmd_mutex);
342bdaeedc1SMike Christie 
343bdaeedc1SMike Christie 	return ret;
344bdaeedc1SMike Christie }
345bdaeedc1SMike Christie 
346bdaeedc1SMike Christie static const struct kernel_param_ops tcmu_reset_netlink_op = {
347bdaeedc1SMike Christie 	.set = tcmu_set_reset_netlink,
348bdaeedc1SMike Christie };
349bdaeedc1SMike Christie 
350bdaeedc1SMike Christie module_param_cb(reset_netlink, &tcmu_reset_netlink_op, NULL, S_IWUSR);
351bdaeedc1SMike Christie MODULE_PARM_DESC(reset_netlink, "Reset netlink commands.");
352bdaeedc1SMike Christie 
3537c9e7a6fSAndy Grover /* multicast group */
3547c9e7a6fSAndy Grover enum tcmu_multicast_groups {
3557c9e7a6fSAndy Grover 	TCMU_MCGRP_CONFIG,
3567c9e7a6fSAndy Grover };
3577c9e7a6fSAndy Grover 
3587c9e7a6fSAndy Grover static const struct genl_multicast_group tcmu_mcgrps[] = {
3597c9e7a6fSAndy Grover 	[TCMU_MCGRP_CONFIG] = { .name = "config", },
3607c9e7a6fSAndy Grover };
3617c9e7a6fSAndy Grover 
362b3af66e2SMike Christie static struct nla_policy tcmu_attr_policy[TCMU_ATTR_MAX+1] = {
363b3af66e2SMike Christie 	[TCMU_ATTR_DEVICE]	= { .type = NLA_STRING },
364b3af66e2SMike Christie 	[TCMU_ATTR_MINOR]	= { .type = NLA_U32 },
365b3af66e2SMike Christie 	[TCMU_ATTR_CMD_STATUS]	= { .type = NLA_S32 },
366b3af66e2SMike Christie 	[TCMU_ATTR_DEVICE_ID]	= { .type = NLA_U32 },
367b3af66e2SMike Christie 	[TCMU_ATTR_SUPP_KERN_CMD_REPLY] = { .type = NLA_U8 },
368b3af66e2SMike Christie };
369b3af66e2SMike Christie 
370b3af66e2SMike Christie static int tcmu_genl_cmd_done(struct genl_info *info, int completed_cmd)
371b3af66e2SMike Christie {
3723228691fSMike Christie 	struct tcmu_dev *udev = NULL;
373b3af66e2SMike Christie 	struct tcmu_nl_cmd *nl_cmd;
374b3af66e2SMike Christie 	int dev_id, rc, ret = 0;
375b3af66e2SMike Christie 
376b3af66e2SMike Christie 	if (!info->attrs[TCMU_ATTR_CMD_STATUS] ||
377b3af66e2SMike Christie 	    !info->attrs[TCMU_ATTR_DEVICE_ID]) {
378b3af66e2SMike Christie 		printk(KERN_ERR "TCMU_ATTR_CMD_STATUS or TCMU_ATTR_DEVICE_ID not set, doing nothing\n");
379b3af66e2SMike Christie 		return -EINVAL;
380b3af66e2SMike Christie         }
381b3af66e2SMike Christie 
382b3af66e2SMike Christie 	dev_id = nla_get_u32(info->attrs[TCMU_ATTR_DEVICE_ID]);
383b3af66e2SMike Christie 	rc = nla_get_s32(info->attrs[TCMU_ATTR_CMD_STATUS]);
384b3af66e2SMike Christie 
3853228691fSMike Christie 	mutex_lock(&tcmu_nl_cmd_mutex);
3863228691fSMike Christie 	list_for_each_entry(nl_cmd, &tcmu_nl_cmd_list, nl_list) {
3873228691fSMike Christie 		if (nl_cmd->udev->se_dev.dev_index == dev_id) {
3883228691fSMike Christie 			udev = nl_cmd->udev;
3893228691fSMike Christie 			break;
390b3af66e2SMike Christie 		}
3913228691fSMike Christie 	}
392b3af66e2SMike Christie 
3933228691fSMike Christie 	if (!udev) {
3940c218e16SMike Christie 		pr_err("tcmu nl cmd %u/%d completion could not find device with dev id %u.\n",
3953228691fSMike Christie 		       completed_cmd, rc, dev_id);
3963228691fSMike Christie 		ret = -ENODEV;
3973228691fSMike Christie 		goto unlock;
3983228691fSMike Christie 	}
3993228691fSMike Christie 	list_del(&nl_cmd->nl_list);
400b3af66e2SMike Christie 
401bdaeedc1SMike Christie 	pr_debug("%s genl cmd done got id %d curr %d done %d rc %d stat %d\n",
402bdaeedc1SMike Christie 		 udev->name, dev_id, nl_cmd->cmd, completed_cmd, rc,
403bdaeedc1SMike Christie 		 nl_cmd->status);
404b3af66e2SMike Christie 
405b3af66e2SMike Christie 	if (nl_cmd->cmd != completed_cmd) {
4063228691fSMike Christie 		pr_err("Mismatched commands on %s (Expecting reply for %d. Current %d).\n",
4073228691fSMike Christie 		       udev->name, completed_cmd, nl_cmd->cmd);
408b3af66e2SMike Christie 		ret = -EINVAL;
4093228691fSMike Christie 		goto unlock;
410b3af66e2SMike Christie 	}
411b3af66e2SMike Christie 
4123228691fSMike Christie 	nl_cmd->status = rc;
413b3af66e2SMike Christie 	complete(&nl_cmd->complete);
4143228691fSMike Christie unlock:
4153228691fSMike Christie 	mutex_unlock(&tcmu_nl_cmd_mutex);
416b3af66e2SMike Christie 	return ret;
417b3af66e2SMike Christie }
418b3af66e2SMike Christie 
419b3af66e2SMike Christie static int tcmu_genl_rm_dev_done(struct sk_buff *skb, struct genl_info *info)
420b3af66e2SMike Christie {
421b3af66e2SMike Christie 	return tcmu_genl_cmd_done(info, TCMU_CMD_REMOVED_DEVICE);
422b3af66e2SMike Christie }
423b3af66e2SMike Christie 
424b3af66e2SMike Christie static int tcmu_genl_add_dev_done(struct sk_buff *skb, struct genl_info *info)
425b3af66e2SMike Christie {
426b3af66e2SMike Christie 	return tcmu_genl_cmd_done(info, TCMU_CMD_ADDED_DEVICE);
427b3af66e2SMike Christie }
428b3af66e2SMike Christie 
429b3af66e2SMike Christie static int tcmu_genl_reconfig_dev_done(struct sk_buff *skb,
430b3af66e2SMike Christie 				       struct genl_info *info)
431b3af66e2SMike Christie {
432b3af66e2SMike Christie 	return tcmu_genl_cmd_done(info, TCMU_CMD_RECONFIG_DEVICE);
433b3af66e2SMike Christie }
434b3af66e2SMike Christie 
435b3af66e2SMike Christie static int tcmu_genl_set_features(struct sk_buff *skb, struct genl_info *info)
436b3af66e2SMike Christie {
437b3af66e2SMike Christie 	if (info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]) {
438b3af66e2SMike Christie 		tcmu_kern_cmd_reply_supported  =
439b3af66e2SMike Christie 			nla_get_u8(info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]);
440b3af66e2SMike Christie 		printk(KERN_INFO "tcmu daemon: command reply support %u.\n",
441b3af66e2SMike Christie 		       tcmu_kern_cmd_reply_supported);
442b3af66e2SMike Christie 	}
443b3af66e2SMike Christie 
444b3af66e2SMike Christie 	return 0;
445b3af66e2SMike Christie }
446b3af66e2SMike Christie 
44766a9b928SJakub Kicinski static const struct genl_small_ops tcmu_genl_ops[] = {
448b3af66e2SMike Christie 	{
449b3af66e2SMike Christie 		.cmd	= TCMU_CMD_SET_FEATURES,
450ef6243acSJohannes Berg 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
451b3af66e2SMike Christie 		.flags	= GENL_ADMIN_PERM,
452b3af66e2SMike Christie 		.doit	= tcmu_genl_set_features,
453b3af66e2SMike Christie 	},
454b3af66e2SMike Christie 	{
455b3af66e2SMike Christie 		.cmd	= TCMU_CMD_ADDED_DEVICE_DONE,
456ef6243acSJohannes Berg 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
457b3af66e2SMike Christie 		.flags	= GENL_ADMIN_PERM,
458b3af66e2SMike Christie 		.doit	= tcmu_genl_add_dev_done,
459b3af66e2SMike Christie 	},
460b3af66e2SMike Christie 	{
461b3af66e2SMike Christie 		.cmd	= TCMU_CMD_REMOVED_DEVICE_DONE,
462ef6243acSJohannes Berg 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
463b3af66e2SMike Christie 		.flags	= GENL_ADMIN_PERM,
464b3af66e2SMike Christie 		.doit	= tcmu_genl_rm_dev_done,
465b3af66e2SMike Christie 	},
466b3af66e2SMike Christie 	{
467b3af66e2SMike Christie 		.cmd	= TCMU_CMD_RECONFIG_DEVICE_DONE,
468ef6243acSJohannes Berg 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
469b3af66e2SMike Christie 		.flags	= GENL_ADMIN_PERM,
470b3af66e2SMike Christie 		.doit	= tcmu_genl_reconfig_dev_done,
471b3af66e2SMike Christie 	},
472b3af66e2SMike Christie };
473b3af66e2SMike Christie 
4747c9e7a6fSAndy Grover /* Our generic netlink family */
47556989f6dSJohannes Berg static struct genl_family tcmu_genl_family __ro_after_init = {
476489111e5SJohannes Berg 	.module = THIS_MODULE,
4777c9e7a6fSAndy Grover 	.hdrsize = 0,
4787c9e7a6fSAndy Grover 	.name = "TCM-USER",
479b3af66e2SMike Christie 	.version = 2,
4807c9e7a6fSAndy Grover 	.maxattr = TCMU_ATTR_MAX,
4813b0f31f2SJohannes Berg 	.policy = tcmu_attr_policy,
4827c9e7a6fSAndy Grover 	.mcgrps = tcmu_mcgrps,
4837c9e7a6fSAndy Grover 	.n_mcgrps = ARRAY_SIZE(tcmu_mcgrps),
48420c08b36SSheng Yang 	.netnsok = true,
48566a9b928SJakub Kicinski 	.small_ops = tcmu_genl_ops,
48666a9b928SJakub Kicinski 	.n_small_ops = ARRAY_SIZE(tcmu_genl_ops),
4877c9e7a6fSAndy Grover };
4887c9e7a6fSAndy Grover 
489141685a3SXiubo Li #define tcmu_cmd_set_dbi_cur(cmd, index) ((cmd)->dbi_cur = (index))
490141685a3SXiubo Li #define tcmu_cmd_reset_dbi_cur(cmd) tcmu_cmd_set_dbi_cur(cmd, 0)
491141685a3SXiubo Li #define tcmu_cmd_set_dbi(cmd, index) ((cmd)->dbi[(cmd)->dbi_cur++] = (index))
492141685a3SXiubo Li #define tcmu_cmd_get_dbi(cmd) ((cmd)->dbi[(cmd)->dbi_cur++])
493141685a3SXiubo Li 
494b6df4b79SXiubo Li static void tcmu_cmd_free_data(struct tcmu_cmd *tcmu_cmd, uint32_t len)
495141685a3SXiubo Li {
496141685a3SXiubo Li 	struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
497141685a3SXiubo Li 	uint32_t i;
498141685a3SXiubo Li 
499b6df4b79SXiubo Li 	for (i = 0; i < len; i++)
500141685a3SXiubo Li 		clear_bit(tcmu_cmd->dbi[i], udev->data_bitmap);
501141685a3SXiubo Li }
502141685a3SXiubo Li 
5037e98905eSBodo Stroesser static inline int tcmu_get_empty_block(struct tcmu_dev *udev,
5047e98905eSBodo Stroesser 				       struct tcmu_cmd *tcmu_cmd,
505f5ce815fSBodo Stroesser 				       int prev_dbi, int length, int *iov_cnt)
506141685a3SXiubo Li {
507f5ce815fSBodo Stroesser 	XA_STATE(xas, &udev->data_pages, 0);
508b6df4b79SXiubo Li 	struct page *page;
509e719afdcSBodo Stroesser 	int i, cnt, dbi, dpi;
510f5ce815fSBodo Stroesser 	int page_cnt = DIV_ROUND_UP(length, PAGE_SIZE);
511141685a3SXiubo Li 
512b6df4b79SXiubo Li 	dbi = find_first_zero_bit(udev->data_bitmap, udev->dbi_thresh);
513b6df4b79SXiubo Li 	if (dbi == udev->dbi_thresh)
5147e98905eSBodo Stroesser 		return -1;
515b6df4b79SXiubo Li 
516e719afdcSBodo Stroesser 	dpi = dbi * udev->data_pages_per_blk;
517f5ce815fSBodo Stroesser 	/* Count the number of already allocated pages */
518e719afdcSBodo Stroesser 	xas_set(&xas, dpi);
519f5ce815fSBodo Stroesser 	for (cnt = 0; xas_next(&xas) && cnt < page_cnt;)
520f5ce815fSBodo Stroesser 		cnt++;
521b6df4b79SXiubo Li 
522f5ce815fSBodo Stroesser 	for (i = cnt; i < page_cnt; i++) {
523b6df4b79SXiubo Li 		/* try to get new page from the mm */
5240eccce86SDamien Le Moal 		page = alloc_page(GFP_NOIO);
525b6df4b79SXiubo Li 		if (!page)
526f5ce815fSBodo Stroesser 			break;
527b6df4b79SXiubo Li 
528e719afdcSBodo Stroesser 		if (xa_store(&udev->data_pages, dpi + i, page, GFP_NOIO)) {
529f5ce815fSBodo Stroesser 			__free_page(page);
530f5ce815fSBodo Stroesser 			break;
531b6df4b79SXiubo Li 		}
532f5ce815fSBodo Stroesser 	}
533f5ce815fSBodo Stroesser 	if (atomic_add_return(i - cnt, &global_page_count) >
534f5ce815fSBodo Stroesser 			      tcmu_global_max_pages)
535f5ce815fSBodo Stroesser 		schedule_delayed_work(&tcmu_unmap_work, 0);
536b6df4b79SXiubo Li 
537f5ce815fSBodo Stroesser 	if (i && dbi > udev->dbi_max)
538141685a3SXiubo Li 		udev->dbi_max = dbi;
539141685a3SXiubo Li 
540141685a3SXiubo Li 	set_bit(dbi, udev->data_bitmap);
541b6df4b79SXiubo Li 	tcmu_cmd_set_dbi(tcmu_cmd, dbi);
542141685a3SXiubo Li 
5437e98905eSBodo Stroesser 	if (dbi != prev_dbi + 1)
5447e98905eSBodo Stroesser 		*iov_cnt += 1;
5457e98905eSBodo Stroesser 
546f5ce815fSBodo Stroesser 	return i == page_cnt ? dbi : -1;
547141685a3SXiubo Li }
548141685a3SXiubo Li 
5497e98905eSBodo Stroesser static int tcmu_get_empty_blocks(struct tcmu_dev *udev,
550f5ce815fSBodo Stroesser 				 struct tcmu_cmd *tcmu_cmd, int length)
551b6df4b79SXiubo Li {
5527e98905eSBodo Stroesser 	/* start value of dbi + 1 must not be a valid dbi */
5537e98905eSBodo Stroesser 	int dbi = -2;
554e719afdcSBodo Stroesser 	int blk_data_len, iov_cnt = 0;
555e719afdcSBodo Stroesser 	uint32_t blk_size = udev->data_blk_size;
556b6df4b79SXiubo Li 
557e719afdcSBodo Stroesser 	for (; length > 0; length -= blk_size) {
558e719afdcSBodo Stroesser 		blk_data_len = min_t(uint32_t, length, blk_size);
559e719afdcSBodo Stroesser 		dbi = tcmu_get_empty_block(udev, tcmu_cmd, dbi, blk_data_len,
560e719afdcSBodo Stroesser 					   &iov_cnt);
5617e98905eSBodo Stroesser 		if (dbi < 0)
5627e98905eSBodo Stroesser 			return -1;
563141685a3SXiubo Li 	}
5647e98905eSBodo Stroesser 	return iov_cnt;
565141685a3SXiubo Li }
566141685a3SXiubo Li 
567141685a3SXiubo Li static inline void tcmu_free_cmd(struct tcmu_cmd *tcmu_cmd)
568141685a3SXiubo Li {
569141685a3SXiubo Li 	kfree(tcmu_cmd->dbi);
570141685a3SXiubo Li 	kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
571141685a3SXiubo Li }
572141685a3SXiubo Li 
57352ef2743SBodo Stroesser static inline void tcmu_cmd_set_block_cnts(struct tcmu_cmd *cmd)
574141685a3SXiubo Li {
57552ef2743SBodo Stroesser 	int i, len;
57652ef2743SBodo Stroesser 	struct se_cmd *se_cmd = cmd->se_cmd;
577e719afdcSBodo Stroesser 	uint32_t blk_size = cmd->tcmu_dev->data_blk_size;
57852ef2743SBodo Stroesser 
579e719afdcSBodo Stroesser 	cmd->dbi_cnt = DIV_ROUND_UP(se_cmd->data_length, blk_size);
580141685a3SXiubo Li 
581141685a3SXiubo Li 	if (se_cmd->se_cmd_flags & SCF_BIDI) {
582141685a3SXiubo Li 		BUG_ON(!(se_cmd->t_bidi_data_sg && se_cmd->t_bidi_data_nents));
58352ef2743SBodo Stroesser 		for (i = 0, len = 0; i < se_cmd->t_bidi_data_nents; i++)
58452ef2743SBodo Stroesser 			len += se_cmd->t_bidi_data_sg[i].length;
585e719afdcSBodo Stroesser 		cmd->dbi_bidi_cnt = DIV_ROUND_UP(len, blk_size);
58652ef2743SBodo Stroesser 		cmd->dbi_cnt += cmd->dbi_bidi_cnt;
5873c9a7c58SBodo Stroesser 		cmd->data_len_bidi = len;
588141685a3SXiubo Li 	}
589141685a3SXiubo Li }
590141685a3SXiubo Li 
5913c9a7c58SBodo Stroesser static int new_block_to_iov(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
592c8ed1ff8SBodo Stroesser 			    struct iovec **iov, int prev_dbi, int len)
5933c9a7c58SBodo Stroesser {
5943c9a7c58SBodo Stroesser 	/* Get the next dbi */
5953c9a7c58SBodo Stroesser 	int dbi = tcmu_cmd_get_dbi(cmd);
5963c9a7c58SBodo Stroesser 
597e719afdcSBodo Stroesser 	/* Do not add more than udev->data_blk_size to iov */
598e719afdcSBodo Stroesser 	len = min_t(int,  len, udev->data_blk_size);
599c8ed1ff8SBodo Stroesser 
6003c9a7c58SBodo Stroesser 	/*
6013c9a7c58SBodo Stroesser 	 * The following code will gather and map the blocks to the same iovec
6023c9a7c58SBodo Stroesser 	 * when the blocks are all next to each other.
6033c9a7c58SBodo Stroesser 	 */
6043c9a7c58SBodo Stroesser 	if (dbi != prev_dbi + 1) {
6053c9a7c58SBodo Stroesser 		/* dbi is not next to previous dbi, so start new iov */
6063c9a7c58SBodo Stroesser 		if (prev_dbi >= 0)
6073c9a7c58SBodo Stroesser 			(*iov)++;
6083c9a7c58SBodo Stroesser 		/* write offset relative to mb_addr */
6093c9a7c58SBodo Stroesser 		(*iov)->iov_base = (void __user *)
610e719afdcSBodo Stroesser 				   (udev->data_off + dbi * udev->data_blk_size);
6113c9a7c58SBodo Stroesser 	}
6123c9a7c58SBodo Stroesser 	(*iov)->iov_len += len;
6133c9a7c58SBodo Stroesser 
6143c9a7c58SBodo Stroesser 	return dbi;
6153c9a7c58SBodo Stroesser }
6163c9a7c58SBodo Stroesser 
6173c9a7c58SBodo Stroesser static void tcmu_setup_iovs(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
6183c9a7c58SBodo Stroesser 			    struct iovec **iov, int data_length)
6193c9a7c58SBodo Stroesser {
6203c9a7c58SBodo Stroesser 	/* start value of dbi + 1 must not be a valid dbi */
6213c9a7c58SBodo Stroesser 	int dbi = -2;
6223c9a7c58SBodo Stroesser 
6233c9a7c58SBodo Stroesser 	/* We prepare the IOVs for DMA_FROM_DEVICE transfer direction */
624e719afdcSBodo Stroesser 	for (; data_length > 0; data_length -= udev->data_blk_size)
625c8ed1ff8SBodo Stroesser 		dbi = new_block_to_iov(udev, cmd, iov, dbi, data_length);
6263c9a7c58SBodo Stroesser }
6273c9a7c58SBodo Stroesser 
6287c9e7a6fSAndy Grover static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd)
6297c9e7a6fSAndy Grover {
6307c9e7a6fSAndy Grover 	struct se_device *se_dev = se_cmd->se_dev;
6317c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
6327c9e7a6fSAndy Grover 	struct tcmu_cmd *tcmu_cmd;
6337c9e7a6fSAndy Grover 
6340eccce86SDamien Le Moal 	tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_NOIO);
6357c9e7a6fSAndy Grover 	if (!tcmu_cmd)
6367c9e7a6fSAndy Grover 		return NULL;
6377c9e7a6fSAndy Grover 
638a94a2572SXiubo Li 	INIT_LIST_HEAD(&tcmu_cmd->queue_entry);
6397c9e7a6fSAndy Grover 	tcmu_cmd->se_cmd = se_cmd;
6407c9e7a6fSAndy Grover 	tcmu_cmd->tcmu_dev = udev;
6417c9e7a6fSAndy Grover 
64252ef2743SBodo Stroesser 	tcmu_cmd_set_block_cnts(tcmu_cmd);
643141685a3SXiubo Li 	tcmu_cmd->dbi = kcalloc(tcmu_cmd->dbi_cnt, sizeof(uint32_t),
6440eccce86SDamien Le Moal 				GFP_NOIO);
645141685a3SXiubo Li 	if (!tcmu_cmd->dbi) {
646141685a3SXiubo Li 		kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
647141685a3SXiubo Li 		return NULL;
648141685a3SXiubo Li 	}
649141685a3SXiubo Li 
6507c9e7a6fSAndy Grover 	return tcmu_cmd;
6517c9e7a6fSAndy Grover }
6527c9e7a6fSAndy Grover 
6537c9e7a6fSAndy Grover static inline void tcmu_flush_dcache_range(void *vaddr, size_t size)
6547c9e7a6fSAndy Grover {
655b75d8063SGeliang Tang 	unsigned long offset = offset_in_page(vaddr);
65626d2b310Stangwenji 	void *start = vaddr - offset;
6577c9e7a6fSAndy Grover 
6587c9e7a6fSAndy Grover 	size = round_up(size+offset, PAGE_SIZE);
6597c9e7a6fSAndy Grover 
6607c9e7a6fSAndy Grover 	while (size) {
6613145550aSBodo Stroesser 		flush_dcache_page(vmalloc_to_page(start));
66226d2b310Stangwenji 		start += PAGE_SIZE;
6637c9e7a6fSAndy Grover 		size -= PAGE_SIZE;
6647c9e7a6fSAndy Grover 	}
6657c9e7a6fSAndy Grover }
6667c9e7a6fSAndy Grover 
6677c9e7a6fSAndy Grover /*
6687c9e7a6fSAndy Grover  * Some ring helper functions. We don't assume size is a power of 2 so
6697c9e7a6fSAndy Grover  * we can't use circ_buf.h.
6707c9e7a6fSAndy Grover  */
6717c9e7a6fSAndy Grover static inline size_t spc_used(size_t head, size_t tail, size_t size)
6727c9e7a6fSAndy Grover {
6737c9e7a6fSAndy Grover 	int diff = head - tail;
6747c9e7a6fSAndy Grover 
6757c9e7a6fSAndy Grover 	if (diff >= 0)
6767c9e7a6fSAndy Grover 		return diff;
6777c9e7a6fSAndy Grover 	else
6787c9e7a6fSAndy Grover 		return size + diff;
6797c9e7a6fSAndy Grover }
6807c9e7a6fSAndy Grover 
6817c9e7a6fSAndy Grover static inline size_t spc_free(size_t head, size_t tail, size_t size)
6827c9e7a6fSAndy Grover {
6837c9e7a6fSAndy Grover 	/* Keep 1 byte unused or we can't tell full from empty */
6847c9e7a6fSAndy Grover 	return (size - spc_used(head, tail, size) - 1);
6857c9e7a6fSAndy Grover }
6867c9e7a6fSAndy Grover 
6877c9e7a6fSAndy Grover static inline size_t head_to_end(size_t head, size_t size)
6887c9e7a6fSAndy Grover {
6897c9e7a6fSAndy Grover 	return size - head;
6907c9e7a6fSAndy Grover }
6917c9e7a6fSAndy Grover 
6927c9e7a6fSAndy Grover #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size)
6937c9e7a6fSAndy Grover 
694c8ed1ff8SBodo Stroesser #define TCMU_SG_TO_DATA_AREA 1
695c8ed1ff8SBodo Stroesser #define TCMU_DATA_AREA_TO_SG 2
696c8ed1ff8SBodo Stroesser 
697c8ed1ff8SBodo Stroesser static inline void tcmu_copy_data(struct tcmu_dev *udev,
698c8ed1ff8SBodo Stroesser 				  struct tcmu_cmd *tcmu_cmd, uint32_t direction,
699c8ed1ff8SBodo Stroesser 				  struct scatterlist *sg, unsigned int sg_nents,
700c8ed1ff8SBodo Stroesser 				  struct iovec **iov, size_t data_len)
701c8ed1ff8SBodo Stroesser {
702f5ce815fSBodo Stroesser 	XA_STATE(xas, &udev->data_pages, 0);
703c8ed1ff8SBodo Stroesser 	/* start value of dbi + 1 must not be a valid dbi */
704c8ed1ff8SBodo Stroesser 	int dbi = -2;
705f5ce815fSBodo Stroesser 	size_t page_remaining, cp_len;
706f5ce815fSBodo Stroesser 	int page_cnt, page_inx;
707c8ed1ff8SBodo Stroesser 	struct sg_mapping_iter sg_iter;
708c8ed1ff8SBodo Stroesser 	unsigned int sg_flags;
709c8ed1ff8SBodo Stroesser 	struct page *page;
710c8ed1ff8SBodo Stroesser 	void *data_page_start, *data_addr;
711c8ed1ff8SBodo Stroesser 
712c8ed1ff8SBodo Stroesser 	if (direction == TCMU_SG_TO_DATA_AREA)
713c8ed1ff8SBodo Stroesser 		sg_flags = SG_MITER_ATOMIC | SG_MITER_FROM_SG;
714c8ed1ff8SBodo Stroesser 	else
715c8ed1ff8SBodo Stroesser 		sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG;
716c8ed1ff8SBodo Stroesser 	sg_miter_start(&sg_iter, sg, sg_nents, sg_flags);
717c8ed1ff8SBodo Stroesser 
718c8ed1ff8SBodo Stroesser 	while (data_len) {
719c8ed1ff8SBodo Stroesser 		if (direction == TCMU_SG_TO_DATA_AREA)
720c8ed1ff8SBodo Stroesser 			dbi = new_block_to_iov(udev, tcmu_cmd, iov, dbi,
721c8ed1ff8SBodo Stroesser 					       data_len);
722c8ed1ff8SBodo Stroesser 		else
723c8ed1ff8SBodo Stroesser 			dbi = tcmu_cmd_get_dbi(tcmu_cmd);
724f5ce815fSBodo Stroesser 
725f5ce815fSBodo Stroesser 		page_cnt = DIV_ROUND_UP(data_len, PAGE_SIZE);
726e719afdcSBodo Stroesser 		if (page_cnt > udev->data_pages_per_blk)
727e719afdcSBodo Stroesser 			page_cnt = udev->data_pages_per_blk;
728f5ce815fSBodo Stroesser 
729e719afdcSBodo Stroesser 		xas_set(&xas, dbi * udev->data_pages_per_blk);
730f5ce815fSBodo Stroesser 		for (page_inx = 0; page_inx < page_cnt && data_len; page_inx++) {
731f5ce815fSBodo Stroesser 			page = xas_next(&xas);
732f5ce815fSBodo Stroesser 
733c8ed1ff8SBodo Stroesser 			if (direction == TCMU_DATA_AREA_TO_SG)
734c8ed1ff8SBodo Stroesser 				flush_dcache_page(page);
735c8ed1ff8SBodo Stroesser 			data_page_start = kmap_atomic(page);
736f5ce815fSBodo Stroesser 			page_remaining = PAGE_SIZE;
737c8ed1ff8SBodo Stroesser 
738f5ce815fSBodo Stroesser 			while (page_remaining && data_len) {
739c8ed1ff8SBodo Stroesser 				if (!sg_miter_next(&sg_iter)) {
740c8ed1ff8SBodo Stroesser 					/* set length to 0 to abort outer loop */
741c8ed1ff8SBodo Stroesser 					data_len = 0;
742f5ce815fSBodo Stroesser 					pr_debug("%s: aborting data copy due to exhausted sg_list\n",
743f5ce815fSBodo Stroesser 						 __func__);
744c8ed1ff8SBodo Stroesser 					break;
745c8ed1ff8SBodo Stroesser 				}
746f5ce815fSBodo Stroesser 				cp_len = min3(sg_iter.length, page_remaining,
747f5ce815fSBodo Stroesser 					      data_len);
748c8ed1ff8SBodo Stroesser 
749c8ed1ff8SBodo Stroesser 				data_addr = data_page_start +
750f5ce815fSBodo Stroesser 					    PAGE_SIZE - page_remaining;
751c8ed1ff8SBodo Stroesser 				if (direction == TCMU_SG_TO_DATA_AREA)
752c8ed1ff8SBodo Stroesser 					memcpy(data_addr, sg_iter.addr, cp_len);
753c8ed1ff8SBodo Stroesser 				else
754c8ed1ff8SBodo Stroesser 					memcpy(sg_iter.addr, data_addr, cp_len);
755c8ed1ff8SBodo Stroesser 
756c8ed1ff8SBodo Stroesser 				data_len -= cp_len;
757f5ce815fSBodo Stroesser 				page_remaining -= cp_len;
758c8ed1ff8SBodo Stroesser 				sg_iter.consumed = cp_len;
759c8ed1ff8SBodo Stroesser 			}
760c8ed1ff8SBodo Stroesser 			sg_miter_stop(&sg_iter);
761c8ed1ff8SBodo Stroesser 
762c8ed1ff8SBodo Stroesser 			kunmap_atomic(data_page_start);
763c8ed1ff8SBodo Stroesser 			if (direction == TCMU_SG_TO_DATA_AREA)
764c8ed1ff8SBodo Stroesser 				flush_dcache_page(page);
765c8ed1ff8SBodo Stroesser 		}
766c8ed1ff8SBodo Stroesser 	}
767f5ce815fSBodo Stroesser }
768c8ed1ff8SBodo Stroesser 
7693c9a7c58SBodo Stroesser static void scatter_data_area(struct tcmu_dev *udev, struct tcmu_cmd *tcmu_cmd,
7703c9a7c58SBodo Stroesser 			      struct iovec **iov)
77126418649SSheng Yang {
7723c9a7c58SBodo Stroesser 	struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
773f97ec7dbSIlias Tsitsimpis 
774c8ed1ff8SBodo Stroesser 	tcmu_copy_data(udev, tcmu_cmd, TCMU_SG_TO_DATA_AREA, se_cmd->t_data_sg,
775c8ed1ff8SBodo Stroesser 		       se_cmd->t_data_nents, iov, se_cmd->data_length);
7763c58f737SBodo Stroesser }
777b6df4b79SXiubo Li 
778c8ed1ff8SBodo Stroesser static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *tcmu_cmd,
7796c3796d1Sbstroesser@ts.fujitsu.com 			     bool bidi, uint32_t read_len)
780f97ec7dbSIlias Tsitsimpis {
781c8ed1ff8SBodo Stroesser 	struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
782c8ed1ff8SBodo Stroesser 	struct scatterlist *data_sg;
783a5d68ba8SXiubo Li 	unsigned int data_nents;
784a5d68ba8SXiubo Li 
785a5d68ba8SXiubo Li 	if (!bidi) {
786a5d68ba8SXiubo Li 		data_sg = se_cmd->t_data_sg;
787a5d68ba8SXiubo Li 		data_nents = se_cmd->t_data_nents;
788a5d68ba8SXiubo Li 	} else {
789a5d68ba8SXiubo Li 		/*
790a5d68ba8SXiubo Li 		 * For bidi case, the first count blocks are for Data-Out
791a5d68ba8SXiubo Li 		 * buffer blocks, and before gathering the Data-In buffer
79252ef2743SBodo Stroesser 		 * the Data-Out buffer blocks should be skipped.
793a5d68ba8SXiubo Li 		 */
794c8ed1ff8SBodo Stroesser 		tcmu_cmd_set_dbi_cur(tcmu_cmd,
795c8ed1ff8SBodo Stroesser 				     tcmu_cmd->dbi_cnt - tcmu_cmd->dbi_bidi_cnt);
796a5d68ba8SXiubo Li 
797a5d68ba8SXiubo Li 		data_sg = se_cmd->t_bidi_data_sg;
798a5d68ba8SXiubo Li 		data_nents = se_cmd->t_bidi_data_nents;
799a5d68ba8SXiubo Li 	}
800f97ec7dbSIlias Tsitsimpis 
801c8ed1ff8SBodo Stroesser 	tcmu_copy_data(udev, tcmu_cmd, TCMU_DATA_AREA_TO_SG, data_sg,
802c8ed1ff8SBodo Stroesser 		       data_nents, NULL, read_len);
803f97ec7dbSIlias Tsitsimpis }
804f97ec7dbSIlias Tsitsimpis 
805b6df4b79SXiubo Li static inline size_t spc_bitmap_free(unsigned long *bitmap, uint32_t thresh)
80626418649SSheng Yang {
8073c0f26ffSMike Christie 	return thresh - bitmap_weight(bitmap, thresh);
80826418649SSheng Yang }
80926418649SSheng Yang 
8107c9e7a6fSAndy Grover /*
8117e98905eSBodo Stroesser  * We can't queue a command until we have space available on the cmd ring.
8127c9e7a6fSAndy Grover  *
8137c9e7a6fSAndy Grover  * Called with ring lock held.
8147c9e7a6fSAndy Grover  */
8157e98905eSBodo Stroesser static bool is_ring_space_avail(struct tcmu_dev *udev, size_t cmd_size)
8167c9e7a6fSAndy Grover {
8177c9e7a6fSAndy Grover 	struct tcmu_mailbox *mb = udev->mb_addr;
8180241fd39SNicholas Bellinger 	size_t space, cmd_needed;
8197c9e7a6fSAndy Grover 	u32 cmd_head;
8207c9e7a6fSAndy Grover 
8217c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(mb, sizeof(*mb));
8227c9e7a6fSAndy Grover 
8237c9e7a6fSAndy Grover 	cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
8247c9e7a6fSAndy Grover 
825f56574a2SAndy Grover 	/*
826f56574a2SAndy Grover 	 * If cmd end-of-ring space is too small then we need space for a NOP plus
827f56574a2SAndy Grover 	 * original cmd - cmds are internally contiguous.
828f56574a2SAndy Grover 	 */
829f56574a2SAndy Grover 	if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size)
830f56574a2SAndy Grover 		cmd_needed = cmd_size;
831f56574a2SAndy Grover 	else
832f56574a2SAndy Grover 		cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size);
833f56574a2SAndy Grover 
8347c9e7a6fSAndy Grover 	space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size);
8357c9e7a6fSAndy Grover 	if (space < cmd_needed) {
8367c9e7a6fSAndy Grover 		pr_debug("no cmd space: %u %u %u\n", cmd_head,
8377c9e7a6fSAndy Grover 		       udev->cmdr_last_cleaned, udev->cmdr_size);
8387c9e7a6fSAndy Grover 		return false;
8397c9e7a6fSAndy Grover 	}
840bc2d214aSBodo Stroesser 	return true;
8417e98905eSBodo Stroesser }
8427e98905eSBodo Stroesser 
8437e98905eSBodo Stroesser /*
8447e98905eSBodo Stroesser  * We have to allocate data buffers before we can queue a command.
8457e98905eSBodo Stroesser  * Returns -1 on error (not enough space) or number of needed iovs on success
8467e98905eSBodo Stroesser  *
8477e98905eSBodo Stroesser  * Called with ring lock held.
8487e98905eSBodo Stroesser  */
8497e98905eSBodo Stroesser static int tcmu_alloc_data_space(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
8507e98905eSBodo Stroesser 				  int *iov_bidi_cnt)
8517e98905eSBodo Stroesser {
8527e98905eSBodo Stroesser 	int space, iov_cnt = 0, ret = 0;
8537e98905eSBodo Stroesser 
8547e98905eSBodo Stroesser 	if (!cmd->dbi_cnt)
8557e98905eSBodo Stroesser 		goto wr_iov_cnts;
856bc2d214aSBodo Stroesser 
857b6df4b79SXiubo Li 	/* try to check and get the data blocks as needed */
858b6df4b79SXiubo Li 	space = spc_bitmap_free(udev->data_bitmap, udev->dbi_thresh);
85952ef2743SBodo Stroesser 	if (space < cmd->dbi_cnt) {
86080eb8761SMike Christie 		unsigned long blocks_left =
86180eb8761SMike Christie 				(udev->max_blocks - udev->dbi_thresh) + space;
862b6df4b79SXiubo Li 
86352ef2743SBodo Stroesser 		if (blocks_left < cmd->dbi_cnt) {
864e719afdcSBodo Stroesser 			pr_debug("no data space: only %lu available, but ask for %u\n",
865e719afdcSBodo Stroesser 					blocks_left * udev->data_blk_size,
866e719afdcSBodo Stroesser 					cmd->dbi_cnt * udev->data_blk_size);
8677e98905eSBodo Stroesser 			return -1;
8687c9e7a6fSAndy Grover 		}
8697c9e7a6fSAndy Grover 
87052ef2743SBodo Stroesser 		udev->dbi_thresh += cmd->dbi_cnt;
87180eb8761SMike Christie 		if (udev->dbi_thresh > udev->max_blocks)
87280eb8761SMike Christie 			udev->dbi_thresh = udev->max_blocks;
873b6df4b79SXiubo Li 	}
874b6df4b79SXiubo Li 
875f5ce815fSBodo Stroesser 	iov_cnt = tcmu_get_empty_blocks(udev, cmd, cmd->se_cmd->data_length);
8767e98905eSBodo Stroesser 	if (iov_cnt < 0)
8777e98905eSBodo Stroesser 		return -1;
8787e98905eSBodo Stroesser 
8797e98905eSBodo Stroesser 	if (cmd->dbi_bidi_cnt) {
880f5ce815fSBodo Stroesser 		ret = tcmu_get_empty_blocks(udev, cmd, cmd->data_len_bidi);
8817e98905eSBodo Stroesser 		if (ret < 0)
8827e98905eSBodo Stroesser 			return -1;
8837e98905eSBodo Stroesser 	}
8847e98905eSBodo Stroesser wr_iov_cnts:
8857e98905eSBodo Stroesser 	*iov_bidi_cnt = ret;
8867e98905eSBodo Stroesser 	return iov_cnt + ret;
8877c9e7a6fSAndy Grover }
8887c9e7a6fSAndy Grover 
889fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_base_cmd_size(size_t iov_cnt)
890fe25cc34SXiubo Li {
891fe25cc34SXiubo Li 	return max(offsetof(struct tcmu_cmd_entry, req.iov[iov_cnt]),
892fe25cc34SXiubo Li 			sizeof(struct tcmu_cmd_entry));
893fe25cc34SXiubo Li }
894fe25cc34SXiubo Li 
895fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_cmd_size(struct tcmu_cmd *tcmu_cmd,
896fe25cc34SXiubo Li 					   size_t base_command_size)
897fe25cc34SXiubo Li {
898fe25cc34SXiubo Li 	struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
899fe25cc34SXiubo Li 	size_t command_size;
900fe25cc34SXiubo Li 
901fe25cc34SXiubo Li 	command_size = base_command_size +
902fe25cc34SXiubo Li 		round_up(scsi_command_size(se_cmd->t_task_cdb),
903fe25cc34SXiubo Li 				TCMU_OP_ALIGN_SIZE);
904fe25cc34SXiubo Li 
905fe25cc34SXiubo Li 	WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1));
906fe25cc34SXiubo Li 
907fe25cc34SXiubo Li 	return command_size;
908fe25cc34SXiubo Li }
909fe25cc34SXiubo Li 
91061fb2482SBodo Stroesser static void tcmu_setup_cmd_timer(struct tcmu_cmd *tcmu_cmd, unsigned int tmo,
9119103575aSMike Christie 				 struct timer_list *timer)
9120d44374cSMike Christie {
9139103575aSMike Christie 	if (!tmo)
91461fb2482SBodo Stroesser 		return;
9159103575aSMike Christie 
9160d44374cSMike Christie 	tcmu_cmd->deadline = round_jiffies_up(jiffies + msecs_to_jiffies(tmo));
917a94a2572SXiubo Li 	if (!timer_pending(timer))
9189103575aSMike Christie 		mod_timer(timer, tcmu_cmd->deadline);
919a94a2572SXiubo Li 
92061fb2482SBodo Stroesser 	pr_debug("Timeout set up for cmd %p, dev = %s, tmo = %lu\n", tcmu_cmd,
92161fb2482SBodo Stroesser 		 tcmu_cmd->tcmu_dev->name, tmo / MSEC_PER_SEC);
9220d44374cSMike Christie }
9230d44374cSMike Christie 
924a94a2572SXiubo Li static int add_to_qfull_queue(struct tcmu_cmd *tcmu_cmd)
925af1dd7ffSMike Christie {
926af1dd7ffSMike Christie 	struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
9279103575aSMike Christie 	unsigned int tmo;
928af1dd7ffSMike Christie 
9299103575aSMike Christie 	/*
9309103575aSMike Christie 	 * For backwards compat if qfull_time_out is not set use
9319103575aSMike Christie 	 * cmd_time_out and if that's not set use the default time out.
9329103575aSMike Christie 	 */
9339103575aSMike Christie 	if (!udev->qfull_time_out)
9349103575aSMike Christie 		return -ETIMEDOUT;
9359103575aSMike Christie 	else if (udev->qfull_time_out > 0)
9369103575aSMike Christie 		tmo = udev->qfull_time_out;
9379103575aSMike Christie 	else if (udev->cmd_time_out)
9389103575aSMike Christie 		tmo = udev->cmd_time_out;
9399103575aSMike Christie 	else
9409103575aSMike Christie 		tmo = TCMU_TIME_OUT;
9419103575aSMike Christie 
94261fb2482SBodo Stroesser 	tcmu_setup_cmd_timer(tcmu_cmd, tmo, &udev->qfull_timer);
943af1dd7ffSMike Christie 
944a94a2572SXiubo Li 	list_add_tail(&tcmu_cmd->queue_entry, &udev->qfull_queue);
94561fb2482SBodo Stroesser 	pr_debug("adding cmd %p on dev %s to ring space wait queue\n",
94661fb2482SBodo Stroesser 		 tcmu_cmd, udev->name);
947af1dd7ffSMike Christie 	return 0;
948af1dd7ffSMike Christie }
949af1dd7ffSMike Christie 
9503d3f9d56SBodo Stroesser static uint32_t ring_insert_padding(struct tcmu_dev *udev, size_t cmd_size)
9513d3f9d56SBodo Stroesser {
9523d3f9d56SBodo Stroesser 	struct tcmu_cmd_entry_hdr *hdr;
9533d3f9d56SBodo Stroesser 	struct tcmu_mailbox *mb = udev->mb_addr;
9543d3f9d56SBodo Stroesser 	uint32_t cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
9553d3f9d56SBodo Stroesser 
9563d3f9d56SBodo Stroesser 	/* Insert a PAD if end-of-ring space is too small */
9573d3f9d56SBodo Stroesser 	if (head_to_end(cmd_head, udev->cmdr_size) < cmd_size) {
9583d3f9d56SBodo Stroesser 		size_t pad_size = head_to_end(cmd_head, udev->cmdr_size);
9593d3f9d56SBodo Stroesser 
960ecddbb7eSBodo Stroesser 		hdr = udev->cmdr + cmd_head;
9613d3f9d56SBodo Stroesser 		tcmu_hdr_set_op(&hdr->len_op, TCMU_OP_PAD);
9623d3f9d56SBodo Stroesser 		tcmu_hdr_set_len(&hdr->len_op, pad_size);
9633d3f9d56SBodo Stroesser 		hdr->cmd_id = 0; /* not used for PAD */
9643d3f9d56SBodo Stroesser 		hdr->kflags = 0;
9653d3f9d56SBodo Stroesser 		hdr->uflags = 0;
9663d3f9d56SBodo Stroesser 		tcmu_flush_dcache_range(hdr, sizeof(*hdr));
9673d3f9d56SBodo Stroesser 
9683d3f9d56SBodo Stroesser 		UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size);
9693d3f9d56SBodo Stroesser 		tcmu_flush_dcache_range(mb, sizeof(*mb));
9703d3f9d56SBodo Stroesser 
9713d3f9d56SBodo Stroesser 		cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
9723d3f9d56SBodo Stroesser 		WARN_ON(cmd_head != 0);
9733d3f9d56SBodo Stroesser 	}
9743d3f9d56SBodo Stroesser 
9753d3f9d56SBodo Stroesser 	return cmd_head;
9763d3f9d56SBodo Stroesser }
9773d3f9d56SBodo Stroesser 
9786888da81SMike Christie static void tcmu_unplug_device(struct se_dev_plug *se_plug)
9796888da81SMike Christie {
9806888da81SMike Christie 	struct se_device *se_dev = se_plug->se_dev;
9816888da81SMike Christie 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
9826888da81SMike Christie 
9836888da81SMike Christie 	clear_bit(TCM_DEV_BIT_PLUGGED, &udev->flags);
9846888da81SMike Christie 	uio_event_notify(&udev->uio_info);
9856888da81SMike Christie }
9866888da81SMike Christie 
9876888da81SMike Christie static struct se_dev_plug *tcmu_plug_device(struct se_device *se_dev)
9886888da81SMike Christie {
9896888da81SMike Christie 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
9906888da81SMike Christie 
9916888da81SMike Christie 	if (!test_and_set_bit(TCM_DEV_BIT_PLUGGED, &udev->flags))
9926888da81SMike Christie 		return &udev->se_plug;
9936888da81SMike Christie 
9946888da81SMike Christie 	return NULL;
9956888da81SMike Christie }
9966888da81SMike Christie 
9976fd0ce79SMike Christie /**
9986fd0ce79SMike Christie  * queue_cmd_ring - queue cmd to ring or internally
9996fd0ce79SMike Christie  * @tcmu_cmd: cmd to queue
10006fd0ce79SMike Christie  * @scsi_err: TCM error code if failure (-1) returned.
10016fd0ce79SMike Christie  *
10026fd0ce79SMike Christie  * Returns:
10036fd0ce79SMike Christie  * -1 we cannot queue internally or to the ring.
10046fd0ce79SMike Christie  *  0 success
1005af1dd7ffSMike Christie  *  1 internally queued to wait for ring memory to free.
10066fd0ce79SMike Christie  */
1007e7f41104SBart Van Assche static int queue_cmd_ring(struct tcmu_cmd *tcmu_cmd, sense_reason_t *scsi_err)
10087c9e7a6fSAndy Grover {
10097c9e7a6fSAndy Grover 	struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
10107c9e7a6fSAndy Grover 	struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
10117c9e7a6fSAndy Grover 	size_t base_command_size, command_size;
10123d3f9d56SBodo Stroesser 	struct tcmu_mailbox *mb = udev->mb_addr;
10137c9e7a6fSAndy Grover 	struct tcmu_cmd_entry *entry;
10147c9e7a6fSAndy Grover 	struct iovec *iov;
1015d3cbb743SBodo Stroesser 	int iov_cnt, iov_bidi_cnt;
1016d3cbb743SBodo Stroesser 	uint32_t cmd_id, cmd_head;
10177c9e7a6fSAndy Grover 	uint64_t cdb_off;
1018e719afdcSBodo Stroesser 	uint32_t blk_size = udev->data_blk_size;
101952ef2743SBodo Stroesser 	/* size of data buffer needed */
1020e719afdcSBodo Stroesser 	size_t data_length = (size_t)tcmu_cmd->dbi_cnt * blk_size;
10217c9e7a6fSAndy Grover 
10226fd0ce79SMike Christie 	*scsi_err = TCM_NO_SENSE;
10236fd0ce79SMike Christie 
1024892782caSMike Christie 	if (test_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags)) {
1025892782caSMike Christie 		*scsi_err = TCM_LUN_BUSY;
1026892782caSMike Christie 		return -1;
1027892782caSMike Christie 	}
1028892782caSMike Christie 
10296fd0ce79SMike Christie 	if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) {
10306fd0ce79SMike Christie 		*scsi_err = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
10316fd0ce79SMike Christie 		return -1;
10326fd0ce79SMike Christie 	}
10337c9e7a6fSAndy Grover 
1034a94a2572SXiubo Li 	if (!list_empty(&udev->qfull_queue))
1035af1dd7ffSMike Christie 		goto queue;
10367c9e7a6fSAndy Grover 
1037e719afdcSBodo Stroesser 	if (data_length > (size_t)udev->max_blocks * blk_size) {
10387e98905eSBodo Stroesser 		pr_warn("TCMU: Request of size %zu is too big for %zu data area\n",
1039e719afdcSBodo Stroesser 			data_length, (size_t)udev->max_blocks * blk_size);
10406fd0ce79SMike Christie 		*scsi_err = TCM_INVALID_CDB_FIELD;
10416fd0ce79SMike Christie 		return -1;
1042554617b2SAndy Grover 	}
10437c9e7a6fSAndy Grover 
10447e98905eSBodo Stroesser 	iov_cnt = tcmu_alloc_data_space(udev, tcmu_cmd, &iov_bidi_cnt);
10457e98905eSBodo Stroesser 	if (iov_cnt < 0)
10467e98905eSBodo Stroesser 		goto free_and_queue;
10477e98905eSBodo Stroesser 
10487e98905eSBodo Stroesser 	/*
10497e98905eSBodo Stroesser 	 * Must be a certain minimum size for response sense info, but
10507e98905eSBodo Stroesser 	 * also may be larger if the iov array is large.
10517e98905eSBodo Stroesser 	 */
10527e98905eSBodo Stroesser 	base_command_size = tcmu_cmd_get_base_cmd_size(iov_cnt);
10537e98905eSBodo Stroesser 	command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size);
10547e98905eSBodo Stroesser 
10557e98905eSBodo Stroesser 	if (command_size > (udev->cmdr_size / 2)) {
10567e98905eSBodo Stroesser 		pr_warn("TCMU: Request of size %zu is too big for %u cmd ring\n",
10577e98905eSBodo Stroesser 			command_size, udev->cmdr_size);
10587e98905eSBodo Stroesser 		tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cur);
10597e98905eSBodo Stroesser 		*scsi_err = TCM_INVALID_CDB_FIELD;
10607e98905eSBodo Stroesser 		return -1;
10617e98905eSBodo Stroesser 	}
10627e98905eSBodo Stroesser 
10637e98905eSBodo Stroesser 	if (!is_ring_space_avail(udev, command_size))
1064810b8153SMike Christie 		/*
1065810b8153SMike Christie 		 * Don't leave commands partially setup because the unmap
1066810b8153SMike Christie 		 * thread might need the blocks to make forward progress.
1067810b8153SMike Christie 		 */
10687e98905eSBodo Stroesser 		goto free_and_queue;
106926418649SSheng Yang 
1070d3cbb743SBodo Stroesser 	if (xa_alloc(&udev->commands, &cmd_id, tcmu_cmd, XA_LIMIT(1, 0xffff),
1071d3cbb743SBodo Stroesser 		     GFP_NOWAIT) < 0) {
107261fb2482SBodo Stroesser 		pr_err("tcmu: Could not allocate cmd id.\n");
10736fd0ce79SMike Christie 
107461fb2482SBodo Stroesser 		tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt);
10756fd0ce79SMike Christie 		*scsi_err = TCM_OUT_OF_RESOURCES;
10766fd0ce79SMike Christie 		return -1;
10770d44374cSMike Christie 	}
107861fb2482SBodo Stroesser 	tcmu_cmd->cmd_id = cmd_id;
107961fb2482SBodo Stroesser 
108061fb2482SBodo Stroesser 	pr_debug("allocated cmd id %u for cmd %p dev %s\n", tcmu_cmd->cmd_id,
108161fb2482SBodo Stroesser 		 tcmu_cmd, udev->name);
108261fb2482SBodo Stroesser 
10837e98905eSBodo Stroesser 	cmd_head = ring_insert_padding(udev, command_size);
10847e98905eSBodo Stroesser 
1085ecddbb7eSBodo Stroesser 	entry = udev->cmdr + cmd_head;
10867e98905eSBodo Stroesser 	memset(entry, 0, command_size);
10877e98905eSBodo Stroesser 	tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD);
10887e98905eSBodo Stroesser 
10897e98905eSBodo Stroesser 	/* prepare iov list and copy data to data area if necessary */
10907e98905eSBodo Stroesser 	tcmu_cmd_reset_dbi_cur(tcmu_cmd);
10917e98905eSBodo Stroesser 	iov = &entry->req.iov[0];
10923c9a7c58SBodo Stroesser 
10933c9a7c58SBodo Stroesser 	if (se_cmd->data_direction == DMA_TO_DEVICE ||
10943c9a7c58SBodo Stroesser 	    se_cmd->se_cmd_flags & SCF_BIDI)
10953c9a7c58SBodo Stroesser 		scatter_data_area(udev, tcmu_cmd, &iov);
10963c9a7c58SBodo Stroesser 	else
10973c9a7c58SBodo Stroesser 		tcmu_setup_iovs(udev, tcmu_cmd, &iov, se_cmd->data_length);
10983c9a7c58SBodo Stroesser 
10997e98905eSBodo Stroesser 	entry->req.iov_cnt = iov_cnt - iov_bidi_cnt;
11007e98905eSBodo Stroesser 
11017e98905eSBodo Stroesser 	/* Handle BIDI commands */
11027e98905eSBodo Stroesser 	if (se_cmd->se_cmd_flags & SCF_BIDI) {
11037e98905eSBodo Stroesser 		iov++;
11043c9a7c58SBodo Stroesser 		tcmu_setup_iovs(udev, tcmu_cmd, &iov, tcmu_cmd->data_len_bidi);
11057e98905eSBodo Stroesser 		entry->req.iov_bidi_cnt = iov_bidi_cnt;
11067e98905eSBodo Stroesser 	}
11077e98905eSBodo Stroesser 
110861fb2482SBodo Stroesser 	tcmu_setup_cmd_timer(tcmu_cmd, udev->cmd_time_out, &udev->cmd_timer);
110961fb2482SBodo Stroesser 
11100d44374cSMike Christie 	entry->hdr.cmd_id = tcmu_cmd->cmd_id;
11110d44374cSMike Christie 
1112fe25cc34SXiubo Li 	tcmu_hdr_set_len(&entry->hdr.len_op, command_size);
1113fe25cc34SXiubo Li 
11147c9e7a6fSAndy Grover 	/* All offsets relative to mb_addr, not start of entry! */
11157c9e7a6fSAndy Grover 	cdb_off = CMDR_OFF + cmd_head + base_command_size;
11167c9e7a6fSAndy Grover 	memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb));
11177c9e7a6fSAndy Grover 	entry->req.cdb_off = cdb_off;
11188c4e0f21SBodo Stroesser 	tcmu_flush_dcache_range(entry, command_size);
11197c9e7a6fSAndy Grover 
11207c9e7a6fSAndy Grover 	UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size);
11217c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(mb, sizeof(*mb));
11227c9e7a6fSAndy Grover 
1123a94a2572SXiubo Li 	list_add_tail(&tcmu_cmd->queue_entry, &udev->inflight_queue);
1124a94a2572SXiubo Li 
11256888da81SMike Christie 	if (!test_bit(TCM_DEV_BIT_PLUGGED, &udev->flags))
11267c9e7a6fSAndy Grover 		uio_event_notify(&udev->uio_info);
11277c9e7a6fSAndy Grover 
11286fd0ce79SMike Christie 	return 0;
1129af1dd7ffSMike Christie 
11307e98905eSBodo Stroesser free_and_queue:
11317e98905eSBodo Stroesser 	tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cur);
11327e98905eSBodo Stroesser 	tcmu_cmd_reset_dbi_cur(tcmu_cmd);
11337e98905eSBodo Stroesser 
1134af1dd7ffSMike Christie queue:
1135a94a2572SXiubo Li 	if (add_to_qfull_queue(tcmu_cmd)) {
1136af1dd7ffSMike Christie 		*scsi_err = TCM_OUT_OF_RESOURCES;
1137af1dd7ffSMike Christie 		return -1;
1138af1dd7ffSMike Christie 	}
1139af1dd7ffSMike Christie 
1140af1dd7ffSMike Christie 	return 1;
11417c9e7a6fSAndy Grover }
11427c9e7a6fSAndy Grover 
1143bc2d214aSBodo Stroesser /**
1144bc2d214aSBodo Stroesser  * queue_tmr_ring - queue tmr info to ring or internally
1145bc2d214aSBodo Stroesser  * @udev: related tcmu_dev
1146bc2d214aSBodo Stroesser  * @tmr: tcmu_tmr containing tmr info to queue
1147bc2d214aSBodo Stroesser  *
1148bc2d214aSBodo Stroesser  * Returns:
1149bc2d214aSBodo Stroesser  *  0 success
1150bc2d214aSBodo Stroesser  *  1 internally queued to wait for ring memory to free.
1151bc2d214aSBodo Stroesser  */
1152bc2d214aSBodo Stroesser static int
1153bc2d214aSBodo Stroesser queue_tmr_ring(struct tcmu_dev *udev, struct tcmu_tmr *tmr)
1154bc2d214aSBodo Stroesser {
1155bc2d214aSBodo Stroesser 	struct tcmu_tmr_entry *entry;
1156bc2d214aSBodo Stroesser 	int cmd_size;
1157bc2d214aSBodo Stroesser 	int id_list_sz;
1158bc2d214aSBodo Stroesser 	struct tcmu_mailbox *mb = udev->mb_addr;
1159bc2d214aSBodo Stroesser 	uint32_t cmd_head;
1160bc2d214aSBodo Stroesser 
1161bc2d214aSBodo Stroesser 	if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags))
1162bc2d214aSBodo Stroesser 		goto out_free;
1163bc2d214aSBodo Stroesser 
1164bc2d214aSBodo Stroesser 	id_list_sz = sizeof(tmr->tmr_cmd_ids[0]) * tmr->tmr_cmd_cnt;
1165bc2d214aSBodo Stroesser 	cmd_size = round_up(sizeof(*entry) + id_list_sz, TCMU_OP_ALIGN_SIZE);
1166bc2d214aSBodo Stroesser 
1167bc2d214aSBodo Stroesser 	if (!list_empty(&udev->tmr_queue) ||
11687e98905eSBodo Stroesser 	    !is_ring_space_avail(udev, cmd_size)) {
1169bc2d214aSBodo Stroesser 		list_add_tail(&tmr->queue_entry, &udev->tmr_queue);
1170bc2d214aSBodo Stroesser 		pr_debug("adding tmr %p on dev %s to TMR ring space wait queue\n",
1171bc2d214aSBodo Stroesser 			 tmr, udev->name);
1172bc2d214aSBodo Stroesser 		return 1;
1173bc2d214aSBodo Stroesser 	}
1174bc2d214aSBodo Stroesser 
1175bc2d214aSBodo Stroesser 	cmd_head = ring_insert_padding(udev, cmd_size);
1176bc2d214aSBodo Stroesser 
1177ecddbb7eSBodo Stroesser 	entry = udev->cmdr + cmd_head;
1178bc2d214aSBodo Stroesser 	memset(entry, 0, cmd_size);
1179bc2d214aSBodo Stroesser 	tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_TMR);
1180bc2d214aSBodo Stroesser 	tcmu_hdr_set_len(&entry->hdr.len_op, cmd_size);
1181bc2d214aSBodo Stroesser 	entry->tmr_type = tmr->tmr_type;
1182bc2d214aSBodo Stroesser 	entry->cmd_cnt = tmr->tmr_cmd_cnt;
1183bc2d214aSBodo Stroesser 	memcpy(&entry->cmd_ids[0], &tmr->tmr_cmd_ids[0], id_list_sz);
1184bc2d214aSBodo Stroesser 	tcmu_flush_dcache_range(entry, cmd_size);
1185bc2d214aSBodo Stroesser 
1186bc2d214aSBodo Stroesser 	UPDATE_HEAD(mb->cmd_head, cmd_size, udev->cmdr_size);
1187bc2d214aSBodo Stroesser 	tcmu_flush_dcache_range(mb, sizeof(*mb));
1188bc2d214aSBodo Stroesser 
1189bc2d214aSBodo Stroesser 	uio_event_notify(&udev->uio_info);
1190bc2d214aSBodo Stroesser 
1191bc2d214aSBodo Stroesser out_free:
1192bc2d214aSBodo Stroesser 	kfree(tmr);
1193bc2d214aSBodo Stroesser 
1194bc2d214aSBodo Stroesser 	return 0;
1195bc2d214aSBodo Stroesser }
1196bc2d214aSBodo Stroesser 
119702eb924fSAndy Grover static sense_reason_t
119802eb924fSAndy Grover tcmu_queue_cmd(struct se_cmd *se_cmd)
11997c9e7a6fSAndy Grover {
1200af1dd7ffSMike Christie 	struct se_device *se_dev = se_cmd->se_dev;
1201af1dd7ffSMike Christie 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
12027c9e7a6fSAndy Grover 	struct tcmu_cmd *tcmu_cmd;
1203c9684927SBodo Stroesser 	sense_reason_t scsi_ret = TCM_CHECK_CONDITION_ABORT_CMD;
1204c9684927SBodo Stroesser 	int ret = -1;
12057c9e7a6fSAndy Grover 
12067c9e7a6fSAndy Grover 	tcmu_cmd = tcmu_alloc_cmd(se_cmd);
12077c9e7a6fSAndy Grover 	if (!tcmu_cmd)
120802eb924fSAndy Grover 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
12097c9e7a6fSAndy Grover 
1210af1dd7ffSMike Christie 	mutex_lock(&udev->cmdr_lock);
1211c9684927SBodo Stroesser 	if (!(se_cmd->transport_state & CMD_T_ABORTED))
1212af1dd7ffSMike Christie 		ret = queue_cmd_ring(tcmu_cmd, &scsi_ret);
1213af1dd7ffSMike Christie 	if (ret < 0)
1214141685a3SXiubo Li 		tcmu_free_cmd(tcmu_cmd);
1215780e1384SShin'ichiro Kawasaki 	else
1216780e1384SShin'ichiro Kawasaki 		se_cmd->priv = tcmu_cmd;
1217a3512902SBodo Stroesser 	mutex_unlock(&udev->cmdr_lock);
12186fd0ce79SMike Christie 	return scsi_ret;
12197c9e7a6fSAndy Grover }
12207c9e7a6fSAndy Grover 
1221ed212ca8SBodo Stroesser static void tcmu_set_next_deadline(struct list_head *queue,
1222ed212ca8SBodo Stroesser 				   struct timer_list *timer)
1223ed212ca8SBodo Stroesser {
1224ed212ca8SBodo Stroesser 	struct tcmu_cmd *cmd;
1225ed212ca8SBodo Stroesser 
1226ed212ca8SBodo Stroesser 	if (!list_empty(queue)) {
1227ed212ca8SBodo Stroesser 		cmd = list_first_entry(queue, struct tcmu_cmd, queue_entry);
1228ed212ca8SBodo Stroesser 		mod_timer(timer, cmd->deadline);
1229ed212ca8SBodo Stroesser 	} else
1230ed212ca8SBodo Stroesser 		del_timer(timer);
1231ed212ca8SBodo Stroesser }
1232ed212ca8SBodo Stroesser 
1233bc2d214aSBodo Stroesser static int
1234bc2d214aSBodo Stroesser tcmu_tmr_type(enum tcm_tmreq_table tmf)
1235bc2d214aSBodo Stroesser {
1236bc2d214aSBodo Stroesser 	switch (tmf) {
1237bc2d214aSBodo Stroesser 	case TMR_ABORT_TASK:		return TCMU_TMR_ABORT_TASK;
1238bc2d214aSBodo Stroesser 	case TMR_ABORT_TASK_SET:	return TCMU_TMR_ABORT_TASK_SET;
1239bc2d214aSBodo Stroesser 	case TMR_CLEAR_ACA:		return TCMU_TMR_CLEAR_ACA;
1240bc2d214aSBodo Stroesser 	case TMR_CLEAR_TASK_SET:	return TCMU_TMR_CLEAR_TASK_SET;
1241bc2d214aSBodo Stroesser 	case TMR_LUN_RESET:		return TCMU_TMR_LUN_RESET;
1242bc2d214aSBodo Stroesser 	case TMR_TARGET_WARM_RESET:	return TCMU_TMR_TARGET_WARM_RESET;
1243bc2d214aSBodo Stroesser 	case TMR_TARGET_COLD_RESET:	return TCMU_TMR_TARGET_COLD_RESET;
1244bc2d214aSBodo Stroesser 	case TMR_LUN_RESET_PRO:		return TCMU_TMR_LUN_RESET_PRO;
1245bc2d214aSBodo Stroesser 	default:			return TCMU_TMR_UNKNOWN;
1246bc2d214aSBodo Stroesser 	}
1247bc2d214aSBodo Stroesser }
1248bc2d214aSBodo Stroesser 
1249bc2d214aSBodo Stroesser static void
1250bc2d214aSBodo Stroesser tcmu_tmr_notify(struct se_device *se_dev, enum tcm_tmreq_table tmf,
1251bc2d214aSBodo Stroesser 		struct list_head *cmd_list)
1252bc2d214aSBodo Stroesser {
1253bc2d214aSBodo Stroesser 	int i = 0, cmd_cnt = 0;
1254bc2d214aSBodo Stroesser 	bool unqueued = false;
1255bc2d214aSBodo Stroesser 	uint16_t *cmd_ids = NULL;
1256bc2d214aSBodo Stroesser 	struct tcmu_cmd *cmd;
1257bc2d214aSBodo Stroesser 	struct se_cmd *se_cmd;
1258bc2d214aSBodo Stroesser 	struct tcmu_tmr *tmr;
1259bc2d214aSBodo Stroesser 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
1260bc2d214aSBodo Stroesser 
1261bc2d214aSBodo Stroesser 	mutex_lock(&udev->cmdr_lock);
1262bc2d214aSBodo Stroesser 
1263bc2d214aSBodo Stroesser 	/* First we check for aborted commands in qfull_queue */
1264bc2d214aSBodo Stroesser 	list_for_each_entry(se_cmd, cmd_list, state_list) {
1265bc2d214aSBodo Stroesser 		i++;
1266bc2d214aSBodo Stroesser 		if (!se_cmd->priv)
1267bc2d214aSBodo Stroesser 			continue;
1268bc2d214aSBodo Stroesser 		cmd = se_cmd->priv;
1269bc2d214aSBodo Stroesser 		/* Commands on qfull queue have no id yet */
1270bc2d214aSBodo Stroesser 		if (cmd->cmd_id) {
1271bc2d214aSBodo Stroesser 			cmd_cnt++;
1272bc2d214aSBodo Stroesser 			continue;
1273bc2d214aSBodo Stroesser 		}
1274bc2d214aSBodo Stroesser 		pr_debug("Removing aborted command %p from queue on dev %s.\n",
1275bc2d214aSBodo Stroesser 			 cmd, udev->name);
1276bc2d214aSBodo Stroesser 
1277bc2d214aSBodo Stroesser 		list_del_init(&cmd->queue_entry);
1278bc2d214aSBodo Stroesser 		tcmu_free_cmd(cmd);
1279780e1384SShin'ichiro Kawasaki 		se_cmd->priv = NULL;
1280bc2d214aSBodo Stroesser 		target_complete_cmd(se_cmd, SAM_STAT_TASK_ABORTED);
1281bc2d214aSBodo Stroesser 		unqueued = true;
1282bc2d214aSBodo Stroesser 	}
1283bc2d214aSBodo Stroesser 	if (unqueued)
1284bc2d214aSBodo Stroesser 		tcmu_set_next_deadline(&udev->qfull_queue, &udev->qfull_timer);
1285bc2d214aSBodo Stroesser 
128659526d7aSBodo Stroesser 	if (!test_bit(TCMU_DEV_BIT_TMR_NOTIFY, &udev->flags))
128759526d7aSBodo Stroesser 		goto unlock;
128859526d7aSBodo Stroesser 
1289bc2d214aSBodo Stroesser 	pr_debug("TMR event %d on dev %s, aborted cmds %d, afflicted cmd_ids %d\n",
1290bc2d214aSBodo Stroesser 		 tcmu_tmr_type(tmf), udev->name, i, cmd_cnt);
1291bc2d214aSBodo Stroesser 
12921080782fSBodo Stroesser 	tmr = kmalloc(sizeof(*tmr) + cmd_cnt * sizeof(*cmd_ids), GFP_NOIO);
1293bc2d214aSBodo Stroesser 	if (!tmr)
1294bc2d214aSBodo Stroesser 		goto unlock;
1295bc2d214aSBodo Stroesser 
1296bc2d214aSBodo Stroesser 	tmr->tmr_type = tcmu_tmr_type(tmf);
1297bc2d214aSBodo Stroesser 	tmr->tmr_cmd_cnt = cmd_cnt;
1298bc2d214aSBodo Stroesser 
1299bc2d214aSBodo Stroesser 	if (cmd_cnt != 0) {
1300bc2d214aSBodo Stroesser 		cmd_cnt = 0;
1301bc2d214aSBodo Stroesser 		list_for_each_entry(se_cmd, cmd_list, state_list) {
1302bc2d214aSBodo Stroesser 			if (!se_cmd->priv)
1303bc2d214aSBodo Stroesser 				continue;
1304bc2d214aSBodo Stroesser 			cmd = se_cmd->priv;
1305bc2d214aSBodo Stroesser 			if (cmd->cmd_id)
1306bc2d214aSBodo Stroesser 				tmr->tmr_cmd_ids[cmd_cnt++] = cmd->cmd_id;
1307bc2d214aSBodo Stroesser 		}
1308bc2d214aSBodo Stroesser 	}
1309bc2d214aSBodo Stroesser 
1310bc2d214aSBodo Stroesser 	queue_tmr_ring(udev, tmr);
1311bc2d214aSBodo Stroesser 
1312bc2d214aSBodo Stroesser unlock:
1313bc2d214aSBodo Stroesser 	mutex_unlock(&udev->cmdr_lock);
1314bc2d214aSBodo Stroesser }
1315bc2d214aSBodo Stroesser 
13167c9e7a6fSAndy Grover static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *entry)
13177c9e7a6fSAndy Grover {
13187c9e7a6fSAndy Grover 	struct se_cmd *se_cmd = cmd->se_cmd;
13197c9e7a6fSAndy Grover 	struct tcmu_dev *udev = cmd->tcmu_dev;
13206c3796d1Sbstroesser@ts.fujitsu.com 	bool read_len_valid = false;
1321a86a7586SDmitry Fomichev 	uint32_t read_len;
13227c9e7a6fSAndy Grover 
1323b25c7863SSheng Yang 	/*
1324b25c7863SSheng Yang 	 * cmd has been completed already from timeout, just reclaim
13253d9b9555SAndy Grover 	 * data area space and free cmd
1326b25c7863SSheng Yang 	 */
1327a86a7586SDmitry Fomichev 	if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
1328a86a7586SDmitry Fomichev 		WARN_ON_ONCE(se_cmd);
1329141685a3SXiubo Li 		goto out;
1330a86a7586SDmitry Fomichev 	}
1331b25c7863SSheng Yang 
1332a94a2572SXiubo Li 	list_del_init(&cmd->queue_entry);
1333a94a2572SXiubo Li 
1334141685a3SXiubo Li 	tcmu_cmd_reset_dbi_cur(cmd);
13357c9e7a6fSAndy Grover 
13360ad46af8SAndy Grover 	if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) {
13370ad46af8SAndy Grover 		pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n",
13380ad46af8SAndy Grover 			cmd->se_cmd);
1339ed97d0cdSAndy Grover 		entry->rsp.scsi_status = SAM_STAT_CHECK_CONDITION;
13406c3796d1Sbstroesser@ts.fujitsu.com 		goto done;
13416c3796d1Sbstroesser@ts.fujitsu.com 	}
13426c3796d1Sbstroesser@ts.fujitsu.com 
1343a86a7586SDmitry Fomichev 	read_len = se_cmd->data_length;
13446c3796d1Sbstroesser@ts.fujitsu.com 	if (se_cmd->data_direction == DMA_FROM_DEVICE &&
13456c3796d1Sbstroesser@ts.fujitsu.com 	    (entry->hdr.uflags & TCMU_UFLAG_READ_LEN) && entry->rsp.read_len) {
13466c3796d1Sbstroesser@ts.fujitsu.com 		read_len_valid = true;
13476c3796d1Sbstroesser@ts.fujitsu.com 		if (entry->rsp.read_len < read_len)
13486c3796d1Sbstroesser@ts.fujitsu.com 			read_len = entry->rsp.read_len;
13496c3796d1Sbstroesser@ts.fujitsu.com 	}
13506c3796d1Sbstroesser@ts.fujitsu.com 
13516c3796d1Sbstroesser@ts.fujitsu.com 	if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) {
1352406f74c2SMike Christie 		transport_copy_sense_to_cmd(se_cmd, entry->rsp.sense_buffer);
13536c3796d1Sbstroesser@ts.fujitsu.com 		if (!read_len_valid )
13546c3796d1Sbstroesser@ts.fujitsu.com 			goto done;
13556c3796d1Sbstroesser@ts.fujitsu.com 		else
13566c3796d1Sbstroesser@ts.fujitsu.com 			se_cmd->se_cmd_flags |= SCF_TREAT_READ_AS_NORMAL;
13576c3796d1Sbstroesser@ts.fujitsu.com 	}
13586c3796d1Sbstroesser@ts.fujitsu.com 	if (se_cmd->se_cmd_flags & SCF_BIDI) {
135926418649SSheng Yang 		/* Get Data-In buffer before clean up */
13606c3796d1Sbstroesser@ts.fujitsu.com 		gather_data_area(udev, cmd, true, read_len);
1361e4648b01SIlias Tsitsimpis 	} else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
13626c3796d1Sbstroesser@ts.fujitsu.com 		gather_data_area(udev, cmd, false, read_len);
13637c9e7a6fSAndy Grover 	} else if (se_cmd->data_direction == DMA_TO_DEVICE) {
1364141685a3SXiubo Li 		/* TODO: */
13652bc396a2SIlias Tsitsimpis 	} else if (se_cmd->data_direction != DMA_NONE) {
13662bc396a2SIlias Tsitsimpis 		pr_warn("TCMU: data direction was %d!\n",
13672bc396a2SIlias Tsitsimpis 			se_cmd->data_direction);
13687c9e7a6fSAndy Grover 	}
13697c9e7a6fSAndy Grover 
13706c3796d1Sbstroesser@ts.fujitsu.com done:
1371780e1384SShin'ichiro Kawasaki 	se_cmd->priv = NULL;
13726c3796d1Sbstroesser@ts.fujitsu.com 	if (read_len_valid) {
13736c3796d1Sbstroesser@ts.fujitsu.com 		pr_debug("read_len = %d\n", read_len);
13746c3796d1Sbstroesser@ts.fujitsu.com 		target_complete_cmd_with_length(cmd->se_cmd,
13756c3796d1Sbstroesser@ts.fujitsu.com 					entry->rsp.scsi_status, read_len);
13766c3796d1Sbstroesser@ts.fujitsu.com 	} else
13777c9e7a6fSAndy Grover 		target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status);
13787c9e7a6fSAndy Grover 
1379141685a3SXiubo Li out:
1380b6df4b79SXiubo Li 	tcmu_cmd_free_data(cmd, cmd->dbi_cnt);
1381141685a3SXiubo Li 	tcmu_free_cmd(cmd);
13827c9e7a6fSAndy Grover }
13837c9e7a6fSAndy Grover 
1384bc2d214aSBodo Stroesser static int tcmu_run_tmr_queue(struct tcmu_dev *udev)
1385bc2d214aSBodo Stroesser {
1386bc2d214aSBodo Stroesser 	struct tcmu_tmr *tmr, *tmp;
1387bc2d214aSBodo Stroesser 	LIST_HEAD(tmrs);
1388bc2d214aSBodo Stroesser 
1389bc2d214aSBodo Stroesser 	if (list_empty(&udev->tmr_queue))
1390bc2d214aSBodo Stroesser 		return 1;
1391bc2d214aSBodo Stroesser 
1392bc2d214aSBodo Stroesser 	pr_debug("running %s's tmr queue\n", udev->name);
1393bc2d214aSBodo Stroesser 
1394bc2d214aSBodo Stroesser 	list_splice_init(&udev->tmr_queue, &tmrs);
1395bc2d214aSBodo Stroesser 
1396bc2d214aSBodo Stroesser 	list_for_each_entry_safe(tmr, tmp, &tmrs, queue_entry) {
1397bc2d214aSBodo Stroesser 		list_del_init(&tmr->queue_entry);
1398bc2d214aSBodo Stroesser 
1399bc2d214aSBodo Stroesser 		pr_debug("removing tmr %p on dev %s from queue\n",
1400bc2d214aSBodo Stroesser 			 tmr, udev->name);
1401bc2d214aSBodo Stroesser 
1402bc2d214aSBodo Stroesser 		if (queue_tmr_ring(udev, tmr)) {
1403bc2d214aSBodo Stroesser 			pr_debug("ran out of space during tmr queue run\n");
1404bc2d214aSBodo Stroesser 			/*
1405bc2d214aSBodo Stroesser 			 * tmr was requeued, so just put all tmrs back in
1406bc2d214aSBodo Stroesser 			 * the queue
1407bc2d214aSBodo Stroesser 			 */
1408bc2d214aSBodo Stroesser 			list_splice_tail(&tmrs, &udev->tmr_queue);
1409bc2d214aSBodo Stroesser 			return 0;
1410bc2d214aSBodo Stroesser 		}
1411bc2d214aSBodo Stroesser 	}
1412bc2d214aSBodo Stroesser 
1413bc2d214aSBodo Stroesser 	return 1;
1414bc2d214aSBodo Stroesser }
1415bc2d214aSBodo Stroesser 
1416*9814b55cSBodo Stroesser static bool tcmu_handle_completions(struct tcmu_dev *udev)
14177c9e7a6fSAndy Grover {
14187c9e7a6fSAndy Grover 	struct tcmu_mailbox *mb;
1419a94a2572SXiubo Li 	struct tcmu_cmd *cmd;
1420bc2d214aSBodo Stroesser 	bool free_space = false;
14217c9e7a6fSAndy Grover 
14227c9e7a6fSAndy Grover 	if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) {
14237c9e7a6fSAndy Grover 		pr_err("ring broken, not handling completions\n");
14247c9e7a6fSAndy Grover 		return 0;
14257c9e7a6fSAndy Grover 	}
14267c9e7a6fSAndy Grover 
14277c9e7a6fSAndy Grover 	mb = udev->mb_addr;
14287c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(mb, sizeof(*mb));
14297c9e7a6fSAndy Grover 
14306aa7de05SMark Rutland 	while (udev->cmdr_last_cleaned != READ_ONCE(mb->cmd_tail)) {
14317c9e7a6fSAndy Grover 
1432ecddbb7eSBodo Stroesser 		struct tcmu_cmd_entry *entry = udev->cmdr + udev->cmdr_last_cleaned;
14337c9e7a6fSAndy Grover 
14345a0c256dSBodo Stroesser 		/*
14355a0c256dSBodo Stroesser 		 * Flush max. up to end of cmd ring since current entry might
14365a0c256dSBodo Stroesser 		 * be a padding that is shorter than sizeof(*entry)
14375a0c256dSBodo Stroesser 		 */
14385a0c256dSBodo Stroesser 		size_t ring_left = head_to_end(udev->cmdr_last_cleaned,
14395a0c256dSBodo Stroesser 					       udev->cmdr_size);
14405a0c256dSBodo Stroesser 		tcmu_flush_dcache_range(entry, ring_left < sizeof(*entry) ?
14415a0c256dSBodo Stroesser 					ring_left : sizeof(*entry));
14427c9e7a6fSAndy Grover 
1443bc2d214aSBodo Stroesser 		free_space = true;
1444bc2d214aSBodo Stroesser 
1445bc2d214aSBodo Stroesser 		if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD ||
1446bc2d214aSBodo Stroesser 		    tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_TMR) {
14470ad46af8SAndy Grover 			UPDATE_HEAD(udev->cmdr_last_cleaned,
14480ad46af8SAndy Grover 				    tcmu_hdr_get_len(entry->hdr.len_op),
14490ad46af8SAndy Grover 				    udev->cmdr_size);
14507c9e7a6fSAndy Grover 			continue;
14517c9e7a6fSAndy Grover 		}
14520ad46af8SAndy Grover 		WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD);
14537c9e7a6fSAndy Grover 
1454d3cbb743SBodo Stroesser 		cmd = xa_erase(&udev->commands, entry->hdr.cmd_id);
14557c9e7a6fSAndy Grover 		if (!cmd) {
145688cf1073SMike Christie 			pr_err("cmd_id %u not found, ring is broken\n",
145788cf1073SMike Christie 			       entry->hdr.cmd_id);
14587c9e7a6fSAndy Grover 			set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags);
1459*9814b55cSBodo Stroesser 			return false;
14607c9e7a6fSAndy Grover 		}
14617c9e7a6fSAndy Grover 
14627c9e7a6fSAndy Grover 		tcmu_handle_completion(cmd, entry);
14637c9e7a6fSAndy Grover 
14640ad46af8SAndy Grover 		UPDATE_HEAD(udev->cmdr_last_cleaned,
14650ad46af8SAndy Grover 			    tcmu_hdr_get_len(entry->hdr.len_op),
14660ad46af8SAndy Grover 			    udev->cmdr_size);
14677c9e7a6fSAndy Grover 	}
1468bc2d214aSBodo Stroesser 	if (free_space)
1469bc2d214aSBodo Stroesser 		free_space = tcmu_run_tmr_queue(udev);
14707c9e7a6fSAndy Grover 
14718b084d9dSBodo Stroesser 	if (atomic_read(&global_page_count) > tcmu_global_max_pages &&
1472d3cbb743SBodo Stroesser 	    xa_empty(&udev->commands) && list_empty(&udev->qfull_queue)) {
1473af1dd7ffSMike Christie 		/*
1474ed212ca8SBodo Stroesser 		 * Allocated blocks exceeded global block limit, currently no
1475ed212ca8SBodo Stroesser 		 * more pending or waiting commands so try to reclaim blocks.
1476af1dd7ffSMike Christie 		 */
1477af1dd7ffSMike Christie 		schedule_delayed_work(&tcmu_unmap_work, 0);
1478af1dd7ffSMike Christie 	}
1479ed212ca8SBodo Stroesser 	if (udev->cmd_time_out)
1480a94a2572SXiubo Li 		tcmu_set_next_deadline(&udev->inflight_queue, &udev->cmd_timer);
14817c9e7a6fSAndy Grover 
1482bc2d214aSBodo Stroesser 	return free_space;
14837c9e7a6fSAndy Grover }
14847c9e7a6fSAndy Grover 
148561fb2482SBodo Stroesser static void tcmu_check_expired_ring_cmd(struct tcmu_cmd *cmd)
14867c9e7a6fSAndy Grover {
1487af1dd7ffSMike Christie 	struct se_cmd *se_cmd;
14887c9e7a6fSAndy Grover 
1489ed212ca8SBodo Stroesser 	if (!time_after_eq(jiffies, cmd->deadline))
149061fb2482SBodo Stroesser 		return;
14919103575aSMike Christie 
1492af1dd7ffSMike Christie 	set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags);
149340d883b0SXiubo Li 	list_del_init(&cmd->queue_entry);
149461fb2482SBodo Stroesser 	se_cmd = cmd->se_cmd;
1495a3512902SBodo Stroesser 	se_cmd->priv = NULL;
1496a86a7586SDmitry Fomichev 	cmd->se_cmd = NULL;
149761fb2482SBodo Stroesser 
149861fb2482SBodo Stroesser 	pr_debug("Timing out inflight cmd %u on dev %s.\n",
149961fb2482SBodo Stroesser 		 cmd->cmd_id, cmd->tcmu_dev->name);
150061fb2482SBodo Stroesser 
150161fb2482SBodo Stroesser 	target_complete_cmd(se_cmd, SAM_STAT_CHECK_CONDITION);
1502af1dd7ffSMike Christie }
15039103575aSMike Christie 
150461fb2482SBodo Stroesser static void tcmu_check_expired_queue_cmd(struct tcmu_cmd *cmd)
150561fb2482SBodo Stroesser {
150661fb2482SBodo Stroesser 	struct se_cmd *se_cmd;
15079103575aSMike Christie 
1508ed212ca8SBodo Stroesser 	if (!time_after_eq(jiffies, cmd->deadline))
150961fb2482SBodo Stroesser 		return;
151061fb2482SBodo Stroesser 
15119d7464b1SDan Carpenter 	pr_debug("Timing out queued cmd %p on dev %s.\n",
15129d7464b1SDan Carpenter 		  cmd, cmd->tcmu_dev->name);
15139d7464b1SDan Carpenter 
151461fb2482SBodo Stroesser 	list_del_init(&cmd->queue_entry);
151561fb2482SBodo Stroesser 	se_cmd = cmd->se_cmd;
151661fb2482SBodo Stroesser 	tcmu_free_cmd(cmd);
151761fb2482SBodo Stroesser 
1518780e1384SShin'ichiro Kawasaki 	se_cmd->priv = NULL;
151961fb2482SBodo Stroesser 	target_complete_cmd(se_cmd, SAM_STAT_TASK_SET_FULL);
15207c9e7a6fSAndy Grover }
15217c9e7a6fSAndy Grover 
15229103575aSMike Christie static void tcmu_device_timedout(struct tcmu_dev *udev)
15237c9e7a6fSAndy Grover {
1524488ebe4cSMike Christie 	spin_lock(&timed_out_udevs_lock);
1525488ebe4cSMike Christie 	if (list_empty(&udev->timedout_entry))
1526488ebe4cSMike Christie 		list_add_tail(&udev->timedout_entry, &timed_out_udevs);
1527488ebe4cSMike Christie 	spin_unlock(&timed_out_udevs_lock);
15287c9e7a6fSAndy Grover 
1529af1dd7ffSMike Christie 	schedule_delayed_work(&tcmu_unmap_work, 0);
15307c9e7a6fSAndy Grover }
15317c9e7a6fSAndy Grover 
15329103575aSMike Christie static void tcmu_cmd_timedout(struct timer_list *t)
15339103575aSMike Christie {
15349103575aSMike Christie 	struct tcmu_dev *udev = from_timer(udev, t, cmd_timer);
15359103575aSMike Christie 
15369103575aSMike Christie 	pr_debug("%s cmd timeout has expired\n", udev->name);
15379103575aSMike Christie 	tcmu_device_timedout(udev);
15389103575aSMike Christie }
15399103575aSMike Christie 
15409103575aSMike Christie static void tcmu_qfull_timedout(struct timer_list *t)
15419103575aSMike Christie {
15429103575aSMike Christie 	struct tcmu_dev *udev = from_timer(udev, t, qfull_timer);
15439103575aSMike Christie 
15449103575aSMike Christie 	pr_debug("%s qfull timeout has expired\n", udev->name);
15459103575aSMike Christie 	tcmu_device_timedout(udev);
15469103575aSMike Christie }
15479103575aSMike Christie 
15487c9e7a6fSAndy Grover static int tcmu_attach_hba(struct se_hba *hba, u32 host_id)
15497c9e7a6fSAndy Grover {
15507c9e7a6fSAndy Grover 	struct tcmu_hba *tcmu_hba;
15517c9e7a6fSAndy Grover 
15527c9e7a6fSAndy Grover 	tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL);
15537c9e7a6fSAndy Grover 	if (!tcmu_hba)
15547c9e7a6fSAndy Grover 		return -ENOMEM;
15557c9e7a6fSAndy Grover 
15567c9e7a6fSAndy Grover 	tcmu_hba->host_id = host_id;
15577c9e7a6fSAndy Grover 	hba->hba_ptr = tcmu_hba;
15587c9e7a6fSAndy Grover 
15597c9e7a6fSAndy Grover 	return 0;
15607c9e7a6fSAndy Grover }
15617c9e7a6fSAndy Grover 
15627c9e7a6fSAndy Grover static void tcmu_detach_hba(struct se_hba *hba)
15637c9e7a6fSAndy Grover {
15647c9e7a6fSAndy Grover 	kfree(hba->hba_ptr);
15657c9e7a6fSAndy Grover 	hba->hba_ptr = NULL;
15667c9e7a6fSAndy Grover }
15677c9e7a6fSAndy Grover 
15687c9e7a6fSAndy Grover static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
15697c9e7a6fSAndy Grover {
15707c9e7a6fSAndy Grover 	struct tcmu_dev *udev;
15717c9e7a6fSAndy Grover 
15727c9e7a6fSAndy Grover 	udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL);
15737c9e7a6fSAndy Grover 	if (!udev)
15747c9e7a6fSAndy Grover 		return NULL;
1575f3cdbe39SMike Christie 	kref_init(&udev->kref);
15767c9e7a6fSAndy Grover 
15777c9e7a6fSAndy Grover 	udev->name = kstrdup(name, GFP_KERNEL);
15787c9e7a6fSAndy Grover 	if (!udev->name) {
15797c9e7a6fSAndy Grover 		kfree(udev);
15807c9e7a6fSAndy Grover 		return NULL;
15817c9e7a6fSAndy Grover 	}
15827c9e7a6fSAndy Grover 
15837c9e7a6fSAndy Grover 	udev->hba = hba;
1584af980e46SMike Christie 	udev->cmd_time_out = TCMU_TIME_OUT;
15859103575aSMike Christie 	udev->qfull_time_out = -1;
15867c9e7a6fSAndy Grover 
1587e719afdcSBodo Stroesser 	udev->data_pages_per_blk = DATA_PAGES_PER_BLK_DEF;
1588e719afdcSBodo Stroesser 	udev->max_blocks = DATA_AREA_PAGES_DEF / udev->data_pages_per_blk;
1589f5ce815fSBodo Stroesser 	udev->data_area_mb = TCMU_PAGES_TO_MBS(DATA_AREA_PAGES_DEF);
1590e719afdcSBodo Stroesser 
1591b6df4b79SXiubo Li 	mutex_init(&udev->cmdr_lock);
15927c9e7a6fSAndy Grover 
1593ff07e4a4SMike Christie 	INIT_LIST_HEAD(&udev->node);
1594488ebe4cSMike Christie 	INIT_LIST_HEAD(&udev->timedout_entry);
1595a94a2572SXiubo Li 	INIT_LIST_HEAD(&udev->qfull_queue);
1596bc2d214aSBodo Stroesser 	INIT_LIST_HEAD(&udev->tmr_queue);
1597a94a2572SXiubo Li 	INIT_LIST_HEAD(&udev->inflight_queue);
1598d3cbb743SBodo Stroesser 	xa_init_flags(&udev->commands, XA_FLAGS_ALLOC1);
15997c9e7a6fSAndy Grover 
16009103575aSMike Christie 	timer_setup(&udev->qfull_timer, tcmu_qfull_timedout, 0);
16019103575aSMike Christie 	timer_setup(&udev->cmd_timer, tcmu_cmd_timedout, 0);
16027c9e7a6fSAndy Grover 
16038b084d9dSBodo Stroesser 	xa_init(&udev->data_pages);
1604c22adc0bSXiubo Li 
16057c9e7a6fSAndy Grover 	return &udev->se_dev;
16067c9e7a6fSAndy Grover }
16077c9e7a6fSAndy Grover 
160843bf922cSBodo Stroesser static void tcmu_dev_call_rcu(struct rcu_head *p)
160943bf922cSBodo Stroesser {
161043bf922cSBodo Stroesser 	struct se_device *dev = container_of(p, struct se_device, rcu_head);
161143bf922cSBodo Stroesser 	struct tcmu_dev *udev = TCMU_DEV(dev);
161243bf922cSBodo Stroesser 
161343bf922cSBodo Stroesser 	kfree(udev->uio_info.name);
161443bf922cSBodo Stroesser 	kfree(udev->name);
161543bf922cSBodo Stroesser 	kfree(udev);
161643bf922cSBodo Stroesser }
161743bf922cSBodo Stroesser 
161843bf922cSBodo Stroesser static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd)
161943bf922cSBodo Stroesser {
162043bf922cSBodo Stroesser 	if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
162143bf922cSBodo Stroesser 		kmem_cache_free(tcmu_cmd_cache, cmd);
162243bf922cSBodo Stroesser 		return 0;
162343bf922cSBodo Stroesser 	}
162443bf922cSBodo Stroesser 	return -EINVAL;
162543bf922cSBodo Stroesser }
162643bf922cSBodo Stroesser 
1627e719afdcSBodo Stroesser static u32 tcmu_blocks_release(struct tcmu_dev *udev, unsigned long first,
1628f7c89771SBodo Stroesser 				unsigned long last)
162943bf922cSBodo Stroesser {
1630e719afdcSBodo Stroesser 	XA_STATE(xas, &udev->data_pages, first * udev->data_pages_per_blk);
163143bf922cSBodo Stroesser 	struct page *page;
1632f5ce815fSBodo Stroesser 	u32 pages_freed = 0;
163343bf922cSBodo Stroesser 
1634f7c89771SBodo Stroesser 	xas_lock(&xas);
1635e719afdcSBodo Stroesser 	xas_for_each(&xas, page, (last + 1) * udev->data_pages_per_blk - 1) {
1636f7c89771SBodo Stroesser 		xas_store(&xas, NULL);
163743bf922cSBodo Stroesser 		__free_page(page);
1638f5ce815fSBodo Stroesser 		pages_freed++;
163943bf922cSBodo Stroesser 	}
1640f7c89771SBodo Stroesser 	xas_unlock(&xas);
1641f5ce815fSBodo Stroesser 
1642f5ce815fSBodo Stroesser 	atomic_sub(pages_freed, &global_page_count);
1643f5ce815fSBodo Stroesser 
1644f5ce815fSBodo Stroesser 	return pages_freed;
164543bf922cSBodo Stroesser }
164643bf922cSBodo Stroesser 
164743bf922cSBodo Stroesser static void tcmu_remove_all_queued_tmr(struct tcmu_dev *udev)
164843bf922cSBodo Stroesser {
164943bf922cSBodo Stroesser 	struct tcmu_tmr *tmr, *tmp;
165043bf922cSBodo Stroesser 
165143bf922cSBodo Stroesser 	list_for_each_entry_safe(tmr, tmp, &udev->tmr_queue, queue_entry) {
165243bf922cSBodo Stroesser 		list_del_init(&tmr->queue_entry);
165343bf922cSBodo Stroesser 		kfree(tmr);
165443bf922cSBodo Stroesser 	}
165543bf922cSBodo Stroesser }
165643bf922cSBodo Stroesser 
165743bf922cSBodo Stroesser static void tcmu_dev_kref_release(struct kref *kref)
165843bf922cSBodo Stroesser {
165943bf922cSBodo Stroesser 	struct tcmu_dev *udev = container_of(kref, struct tcmu_dev, kref);
166043bf922cSBodo Stroesser 	struct se_device *dev = &udev->se_dev;
166143bf922cSBodo Stroesser 	struct tcmu_cmd *cmd;
166243bf922cSBodo Stroesser 	bool all_expired = true;
1663d3cbb743SBodo Stroesser 	unsigned long i;
166443bf922cSBodo Stroesser 
166543bf922cSBodo Stroesser 	vfree(udev->mb_addr);
166643bf922cSBodo Stroesser 	udev->mb_addr = NULL;
166743bf922cSBodo Stroesser 
166843bf922cSBodo Stroesser 	spin_lock_bh(&timed_out_udevs_lock);
166943bf922cSBodo Stroesser 	if (!list_empty(&udev->timedout_entry))
167043bf922cSBodo Stroesser 		list_del(&udev->timedout_entry);
167143bf922cSBodo Stroesser 	spin_unlock_bh(&timed_out_udevs_lock);
167243bf922cSBodo Stroesser 
167343bf922cSBodo Stroesser 	/* Upper layer should drain all requests before calling this */
167443bf922cSBodo Stroesser 	mutex_lock(&udev->cmdr_lock);
1675d3cbb743SBodo Stroesser 	xa_for_each(&udev->commands, i, cmd) {
167643bf922cSBodo Stroesser 		if (tcmu_check_and_free_pending_cmd(cmd) != 0)
167743bf922cSBodo Stroesser 			all_expired = false;
167843bf922cSBodo Stroesser 	}
167943bf922cSBodo Stroesser 	/* There can be left over TMR cmds. Remove them. */
168043bf922cSBodo Stroesser 	tcmu_remove_all_queued_tmr(udev);
168143bf922cSBodo Stroesser 	if (!list_empty(&udev->qfull_queue))
168243bf922cSBodo Stroesser 		all_expired = false;
1683d3cbb743SBodo Stroesser 	xa_destroy(&udev->commands);
168443bf922cSBodo Stroesser 	WARN_ON(!all_expired);
168543bf922cSBodo Stroesser 
1686e719afdcSBodo Stroesser 	tcmu_blocks_release(udev, 0, udev->dbi_max);
168743bf922cSBodo Stroesser 	bitmap_free(udev->data_bitmap);
168843bf922cSBodo Stroesser 	mutex_unlock(&udev->cmdr_lock);
168943bf922cSBodo Stroesser 
16908f33bb24SBodo Stroesser 	pr_debug("dev_kref_release\n");
16918f33bb24SBodo Stroesser 
169243bf922cSBodo Stroesser 	call_rcu(&dev->rcu_head, tcmu_dev_call_rcu);
169343bf922cSBodo Stroesser }
169443bf922cSBodo Stroesser 
169561fb2482SBodo Stroesser static void run_qfull_queue(struct tcmu_dev *udev, bool fail)
1696af1dd7ffSMike Christie {
1697af1dd7ffSMike Christie 	struct tcmu_cmd *tcmu_cmd, *tmp_cmd;
1698af1dd7ffSMike Christie 	LIST_HEAD(cmds);
1699af1dd7ffSMike Christie 	sense_reason_t scsi_ret;
1700af1dd7ffSMike Christie 	int ret;
1701af1dd7ffSMike Christie 
1702a94a2572SXiubo Li 	if (list_empty(&udev->qfull_queue))
170361fb2482SBodo Stroesser 		return;
1704af1dd7ffSMike Christie 
1705892782caSMike Christie 	pr_debug("running %s's cmdr queue forcefail %d\n", udev->name, fail);
1706af1dd7ffSMike Christie 
1707a94a2572SXiubo Li 	list_splice_init(&udev->qfull_queue, &cmds);
1708af1dd7ffSMike Christie 
1709a94a2572SXiubo Li 	list_for_each_entry_safe(tcmu_cmd, tmp_cmd, &cmds, queue_entry) {
1710a94a2572SXiubo Li 		list_del_init(&tcmu_cmd->queue_entry);
1711af1dd7ffSMike Christie 
171261fb2482SBodo Stroesser 		pr_debug("removing cmd %p on dev %s from queue\n",
171361fb2482SBodo Stroesser 			 tcmu_cmd, udev->name);
1714af1dd7ffSMike Christie 
1715892782caSMike Christie 		if (fail) {
1716892782caSMike Christie 			/*
1717892782caSMike Christie 			 * We were not able to even start the command, so
1718892782caSMike Christie 			 * fail with busy to allow a retry in case runner
1719892782caSMike Christie 			 * was only temporarily down. If the device is being
1720892782caSMike Christie 			 * removed then LIO core will do the right thing and
1721892782caSMike Christie 			 * fail the retry.
1722892782caSMike Christie 			 */
1723780e1384SShin'ichiro Kawasaki 			tcmu_cmd->se_cmd->priv = NULL;
1724892782caSMike Christie 			target_complete_cmd(tcmu_cmd->se_cmd, SAM_STAT_BUSY);
1725892782caSMike Christie 			tcmu_free_cmd(tcmu_cmd);
1726892782caSMike Christie 			continue;
1727892782caSMike Christie 		}
1728892782caSMike Christie 
1729af1dd7ffSMike Christie 		ret = queue_cmd_ring(tcmu_cmd, &scsi_ret);
1730af1dd7ffSMike Christie 		if (ret < 0) {
173161fb2482SBodo Stroesser 			pr_debug("cmd %p on dev %s failed with %u\n",
173261fb2482SBodo Stroesser 				 tcmu_cmd, udev->name, scsi_ret);
1733af1dd7ffSMike Christie 			/*
1734af1dd7ffSMike Christie 			 * Ignore scsi_ret for now. target_complete_cmd
1735af1dd7ffSMike Christie 			 * drops it.
1736af1dd7ffSMike Christie 			 */
1737780e1384SShin'ichiro Kawasaki 			tcmu_cmd->se_cmd->priv = NULL;
1738af1dd7ffSMike Christie 			target_complete_cmd(tcmu_cmd->se_cmd,
1739af1dd7ffSMike Christie 					    SAM_STAT_CHECK_CONDITION);
1740af1dd7ffSMike Christie 			tcmu_free_cmd(tcmu_cmd);
1741af1dd7ffSMike Christie 		} else if (ret > 0) {
1742af1dd7ffSMike Christie 			pr_debug("ran out of space during cmdr queue run\n");
1743af1dd7ffSMike Christie 			/*
1744af1dd7ffSMike Christie 			 * cmd was requeued, so just put all cmds back in
1745af1dd7ffSMike Christie 			 * the queue
1746af1dd7ffSMike Christie 			 */
1747a94a2572SXiubo Li 			list_splice_tail(&cmds, &udev->qfull_queue);
1748a94a2572SXiubo Li 			break;
1749af1dd7ffSMike Christie 		}
1750af1dd7ffSMike Christie 	}
1751a94a2572SXiubo Li 
1752a94a2572SXiubo Li 	tcmu_set_next_deadline(&udev->qfull_queue, &udev->qfull_timer);
1753af1dd7ffSMike Christie }
1754af1dd7ffSMike Christie 
17557c9e7a6fSAndy Grover static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on)
17567c9e7a6fSAndy Grover {
1757af1dd7ffSMike Christie 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
17587c9e7a6fSAndy Grover 
1759af1dd7ffSMike Christie 	mutex_lock(&udev->cmdr_lock);
1760bc2d214aSBodo Stroesser 	if (tcmu_handle_completions(udev))
1761a94a2572SXiubo Li 		run_qfull_queue(udev, false);
1762af1dd7ffSMike Christie 	mutex_unlock(&udev->cmdr_lock);
17637c9e7a6fSAndy Grover 
17647c9e7a6fSAndy Grover 	return 0;
17657c9e7a6fSAndy Grover }
17667c9e7a6fSAndy Grover 
17677c9e7a6fSAndy Grover /*
17687c9e7a6fSAndy Grover  * mmap code from uio.c. Copied here because we want to hook mmap()
17697c9e7a6fSAndy Grover  * and this stuff must come along.
17707c9e7a6fSAndy Grover  */
17717c9e7a6fSAndy Grover static int tcmu_find_mem_index(struct vm_area_struct *vma)
17727c9e7a6fSAndy Grover {
17737c9e7a6fSAndy Grover 	struct tcmu_dev *udev = vma->vm_private_data;
17747c9e7a6fSAndy Grover 	struct uio_info *info = &udev->uio_info;
17757c9e7a6fSAndy Grover 
17767c9e7a6fSAndy Grover 	if (vma->vm_pgoff < MAX_UIO_MAPS) {
17777c9e7a6fSAndy Grover 		if (info->mem[vma->vm_pgoff].size == 0)
17787c9e7a6fSAndy Grover 			return -1;
17797c9e7a6fSAndy Grover 		return (int)vma->vm_pgoff;
17807c9e7a6fSAndy Grover 	}
17817c9e7a6fSAndy Grover 	return -1;
17827c9e7a6fSAndy Grover }
17837c9e7a6fSAndy Grover 
17848b084d9dSBodo Stroesser static struct page *tcmu_try_get_data_page(struct tcmu_dev *udev, uint32_t dpi)
1785b6df4b79SXiubo Li {
1786b6df4b79SXiubo Li 	struct page *page;
1787b6df4b79SXiubo Li 
1788b6df4b79SXiubo Li 	mutex_lock(&udev->cmdr_lock);
17893722e36cSBodo Stroesser 	page = xa_load(&udev->data_pages, dpi);
1790b6df4b79SXiubo Li 	if (likely(page)) {
1791b6df4b79SXiubo Li 		mutex_unlock(&udev->cmdr_lock);
1792b6df4b79SXiubo Li 		return page;
1793b6df4b79SXiubo Li 	}
1794b6df4b79SXiubo Li 
1795b6df4b79SXiubo Li 	/*
1796c1c390baSMike Christie 	 * Userspace messed up and passed in a address not in the
1797c1c390baSMike Christie 	 * data iov passed to it.
1798b6df4b79SXiubo Li 	 */
17998b084d9dSBodo Stroesser 	pr_err("Invalid addr to data page mapping (dpi %u) on device %s\n",
18008b084d9dSBodo Stroesser 	       dpi, udev->name);
1801b6df4b79SXiubo Li 	mutex_unlock(&udev->cmdr_lock);
1802b6df4b79SXiubo Li 
18038b084d9dSBodo Stroesser 	return NULL;
1804b6df4b79SXiubo Li }
1805b6df4b79SXiubo Li 
18068f33bb24SBodo Stroesser static void tcmu_vma_open(struct vm_area_struct *vma)
18078f33bb24SBodo Stroesser {
18088f33bb24SBodo Stroesser 	struct tcmu_dev *udev = vma->vm_private_data;
18098f33bb24SBodo Stroesser 
18108f33bb24SBodo Stroesser 	pr_debug("vma_open\n");
18118f33bb24SBodo Stroesser 
18128f33bb24SBodo Stroesser 	kref_get(&udev->kref);
18138f33bb24SBodo Stroesser }
18148f33bb24SBodo Stroesser 
18158f33bb24SBodo Stroesser static void tcmu_vma_close(struct vm_area_struct *vma)
18168f33bb24SBodo Stroesser {
18178f33bb24SBodo Stroesser 	struct tcmu_dev *udev = vma->vm_private_data;
18188f33bb24SBodo Stroesser 
18198f33bb24SBodo Stroesser 	pr_debug("vma_close\n");
18208f33bb24SBodo Stroesser 
18218f33bb24SBodo Stroesser 	/* release ref from tcmu_vma_open */
18228f33bb24SBodo Stroesser 	kref_put(&udev->kref, tcmu_dev_kref_release);
18238f33bb24SBodo Stroesser }
18248f33bb24SBodo Stroesser 
182569589c9bSSouptick Joarder static vm_fault_t tcmu_vma_fault(struct vm_fault *vmf)
18267c9e7a6fSAndy Grover {
182711bac800SDave Jiang 	struct tcmu_dev *udev = vmf->vma->vm_private_data;
18287c9e7a6fSAndy Grover 	struct uio_info *info = &udev->uio_info;
18297c9e7a6fSAndy Grover 	struct page *page;
18307c9e7a6fSAndy Grover 	unsigned long offset;
18317c9e7a6fSAndy Grover 	void *addr;
18327c9e7a6fSAndy Grover 
183311bac800SDave Jiang 	int mi = tcmu_find_mem_index(vmf->vma);
18347c9e7a6fSAndy Grover 	if (mi < 0)
18357c9e7a6fSAndy Grover 		return VM_FAULT_SIGBUS;
18367c9e7a6fSAndy Grover 
18377c9e7a6fSAndy Grover 	/*
18387c9e7a6fSAndy Grover 	 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
18397c9e7a6fSAndy Grover 	 * to use mem[N].
18407c9e7a6fSAndy Grover 	 */
18417c9e7a6fSAndy Grover 	offset = (vmf->pgoff - mi) << PAGE_SHIFT;
18427c9e7a6fSAndy Grover 
1843141685a3SXiubo Li 	if (offset < udev->data_off) {
1844141685a3SXiubo Li 		/* For the vmalloc()ed cmd area pages */
18457c9e7a6fSAndy Grover 		addr = (void *)(unsigned long)info->mem[mi].addr + offset;
18467c9e7a6fSAndy Grover 		page = vmalloc_to_page(addr);
1847141685a3SXiubo Li 	} else {
18488b084d9dSBodo Stroesser 		uint32_t dpi;
1849141685a3SXiubo Li 
1850b6df4b79SXiubo Li 		/* For the dynamically growing data area pages */
18518b084d9dSBodo Stroesser 		dpi = (offset - udev->data_off) / PAGE_SIZE;
18528b084d9dSBodo Stroesser 		page = tcmu_try_get_data_page(udev, dpi);
1853b6df4b79SXiubo Li 		if (!page)
1854c1c390baSMike Christie 			return VM_FAULT_SIGBUS;
1855141685a3SXiubo Li 	}
1856141685a3SXiubo Li 
18577c9e7a6fSAndy Grover 	get_page(page);
18587c9e7a6fSAndy Grover 	vmf->page = page;
18597c9e7a6fSAndy Grover 	return 0;
18607c9e7a6fSAndy Grover }
18617c9e7a6fSAndy Grover 
18627c9e7a6fSAndy Grover static const struct vm_operations_struct tcmu_vm_ops = {
18638f33bb24SBodo Stroesser 	.open = tcmu_vma_open,
18648f33bb24SBodo Stroesser 	.close = tcmu_vma_close,
18657c9e7a6fSAndy Grover 	.fault = tcmu_vma_fault,
18667c9e7a6fSAndy Grover };
18677c9e7a6fSAndy Grover 
18687c9e7a6fSAndy Grover static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma)
18697c9e7a6fSAndy Grover {
18707c9e7a6fSAndy Grover 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
18717c9e7a6fSAndy Grover 
18727c9e7a6fSAndy Grover 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
18737c9e7a6fSAndy Grover 	vma->vm_ops = &tcmu_vm_ops;
18747c9e7a6fSAndy Grover 
18757c9e7a6fSAndy Grover 	vma->vm_private_data = udev;
18767c9e7a6fSAndy Grover 
18777c9e7a6fSAndy Grover 	/* Ensure the mmap is exactly the right size */
1878ecddbb7eSBodo Stroesser 	if (vma_pages(vma) != udev->mmap_pages)
18797c9e7a6fSAndy Grover 		return -EINVAL;
18807c9e7a6fSAndy Grover 
18818f33bb24SBodo Stroesser 	tcmu_vma_open(vma);
18828f33bb24SBodo Stroesser 
18837c9e7a6fSAndy Grover 	return 0;
18847c9e7a6fSAndy Grover }
18857c9e7a6fSAndy Grover 
18867c9e7a6fSAndy Grover static int tcmu_open(struct uio_info *info, struct inode *inode)
18877c9e7a6fSAndy Grover {
18887c9e7a6fSAndy Grover 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
18897c9e7a6fSAndy Grover 
18907c9e7a6fSAndy Grover 	/* O_EXCL not supported for char devs, so fake it? */
18917c9e7a6fSAndy Grover 	if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags))
18927c9e7a6fSAndy Grover 		return -EBUSY;
18937c9e7a6fSAndy Grover 
1894b6df4b79SXiubo Li 	udev->inode = inode;
1895b6df4b79SXiubo Li 
18967c9e7a6fSAndy Grover 	pr_debug("open\n");
18977c9e7a6fSAndy Grover 
18987c9e7a6fSAndy Grover 	return 0;
18997c9e7a6fSAndy Grover }
19007c9e7a6fSAndy Grover 
19017c9e7a6fSAndy Grover static int tcmu_release(struct uio_info *info, struct inode *inode)
19027c9e7a6fSAndy Grover {
19037c9e7a6fSAndy Grover 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
19047c9e7a6fSAndy Grover 
19057c9e7a6fSAndy Grover 	clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags);
19067c9e7a6fSAndy Grover 
19077c9e7a6fSAndy Grover 	pr_debug("close\n");
19088f33bb24SBodo Stroesser 
19097c9e7a6fSAndy Grover 	return 0;
19107c9e7a6fSAndy Grover }
19117c9e7a6fSAndy Grover 
19129de3a1efSMike Christie static int tcmu_init_genl_cmd_reply(struct tcmu_dev *udev, int cmd)
1913b3af66e2SMike Christie {
1914b3af66e2SMike Christie 	struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd;
1915b3af66e2SMike Christie 
1916b3af66e2SMike Christie 	if (!tcmu_kern_cmd_reply_supported)
19179de3a1efSMike Christie 		return 0;
1918b849b456SKenjiro Nakayama 
1919b849b456SKenjiro Nakayama 	if (udev->nl_reply_supported <= 0)
19209de3a1efSMike Christie 		return 0;
1921b849b456SKenjiro Nakayama 
19223228691fSMike Christie 	mutex_lock(&tcmu_nl_cmd_mutex);
1923b3af66e2SMike Christie 
1924bdaeedc1SMike Christie 	if (tcmu_netlink_blocked) {
1925bdaeedc1SMike Christie 		mutex_unlock(&tcmu_nl_cmd_mutex);
1926bdaeedc1SMike Christie 		pr_warn("Failing nl cmd %d on %s. Interface is blocked.\n", cmd,
1927bdaeedc1SMike Christie 			udev->name);
1928bdaeedc1SMike Christie 		return -EAGAIN;
1929bdaeedc1SMike Christie 	}
1930b3af66e2SMike Christie 
1931b3af66e2SMike Christie 	if (nl_cmd->cmd != TCMU_CMD_UNSPEC) {
19323228691fSMike Christie 		mutex_unlock(&tcmu_nl_cmd_mutex);
19339de3a1efSMike Christie 		pr_warn("netlink cmd %d already executing on %s\n",
19349de3a1efSMike Christie 			 nl_cmd->cmd, udev->name);
19359de3a1efSMike Christie 		return -EBUSY;
1936b3af66e2SMike Christie 	}
1937b3af66e2SMike Christie 
1938b3af66e2SMike Christie 	memset(nl_cmd, 0, sizeof(*nl_cmd));
1939b3af66e2SMike Christie 	nl_cmd->cmd = cmd;
19403228691fSMike Christie 	nl_cmd->udev = udev;
1941b3af66e2SMike Christie 	init_completion(&nl_cmd->complete);
19423228691fSMike Christie 	INIT_LIST_HEAD(&nl_cmd->nl_list);
1943b3af66e2SMike Christie 
19443228691fSMike Christie 	list_add_tail(&nl_cmd->nl_list, &tcmu_nl_cmd_list);
19453228691fSMike Christie 
19463228691fSMike Christie 	mutex_unlock(&tcmu_nl_cmd_mutex);
19479de3a1efSMike Christie 	return 0;
1948b3af66e2SMike Christie }
1949b3af66e2SMike Christie 
19507d894862SLi Zhong static void tcmu_destroy_genl_cmd_reply(struct tcmu_dev *udev)
19517d894862SLi Zhong {
19527d894862SLi Zhong 	struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd;
19537d894862SLi Zhong 
19547d894862SLi Zhong 	if (!tcmu_kern_cmd_reply_supported)
19557d894862SLi Zhong 		return;
19567d894862SLi Zhong 
19577d894862SLi Zhong 	if (udev->nl_reply_supported <= 0)
19587d894862SLi Zhong 		return;
19597d894862SLi Zhong 
19607d894862SLi Zhong 	mutex_lock(&tcmu_nl_cmd_mutex);
19617d894862SLi Zhong 
19627d894862SLi Zhong 	list_del(&nl_cmd->nl_list);
19637d894862SLi Zhong 	memset(nl_cmd, 0, sizeof(*nl_cmd));
19647d894862SLi Zhong 
19657d894862SLi Zhong 	mutex_unlock(&tcmu_nl_cmd_mutex);
19667d894862SLi Zhong }
19677d894862SLi Zhong 
1968b3af66e2SMike Christie static int tcmu_wait_genl_cmd_reply(struct tcmu_dev *udev)
1969b3af66e2SMike Christie {
1970b3af66e2SMike Christie 	struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd;
1971b3af66e2SMike Christie 	int ret;
1972b3af66e2SMike Christie 
1973b3af66e2SMike Christie 	if (!tcmu_kern_cmd_reply_supported)
1974b3af66e2SMike Christie 		return 0;
1975b3af66e2SMike Christie 
1976b849b456SKenjiro Nakayama 	if (udev->nl_reply_supported <= 0)
1977b849b456SKenjiro Nakayama 		return 0;
1978b849b456SKenjiro Nakayama 
1979b3af66e2SMike Christie 	pr_debug("sleeping for nl reply\n");
1980b3af66e2SMike Christie 	wait_for_completion(&nl_cmd->complete);
1981b3af66e2SMike Christie 
19823228691fSMike Christie 	mutex_lock(&tcmu_nl_cmd_mutex);
1983b3af66e2SMike Christie 	nl_cmd->cmd = TCMU_CMD_UNSPEC;
1984b3af66e2SMike Christie 	ret = nl_cmd->status;
19853228691fSMike Christie 	mutex_unlock(&tcmu_nl_cmd_mutex);
1986b3af66e2SMike Christie 
198785fae482SLuis de Bethencourt 	return ret;
1988b3af66e2SMike Christie }
1989b3af66e2SMike Christie 
19900e5aee39SZhu Lingshan static int tcmu_netlink_event_init(struct tcmu_dev *udev,
19910e5aee39SZhu Lingshan 				   enum tcmu_genl_cmd cmd,
19920e5aee39SZhu Lingshan 				   struct sk_buff **buf, void **hdr)
19937c9e7a6fSAndy Grover {
19947c9e7a6fSAndy Grover 	struct sk_buff *skb;
19957c9e7a6fSAndy Grover 	void *msg_header;
19966e14eab9SNicholas Bellinger 	int ret = -ENOMEM;
19977c9e7a6fSAndy Grover 
19987c9e7a6fSAndy Grover 	skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
19997c9e7a6fSAndy Grover 	if (!skb)
20006e14eab9SNicholas Bellinger 		return ret;
20017c9e7a6fSAndy Grover 
20027c9e7a6fSAndy Grover 	msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd);
20036e14eab9SNicholas Bellinger 	if (!msg_header)
20046e14eab9SNicholas Bellinger 		goto free_skb;
20057c9e7a6fSAndy Grover 
2006b3af66e2SMike Christie 	ret = nla_put_string(skb, TCMU_ATTR_DEVICE, udev->uio_info.name);
20076e14eab9SNicholas Bellinger 	if (ret < 0)
20086e14eab9SNicholas Bellinger 		goto free_skb;
20097c9e7a6fSAndy Grover 
2010b3af66e2SMike Christie 	ret = nla_put_u32(skb, TCMU_ATTR_MINOR, udev->uio_info.uio_dev->minor);
2011b3af66e2SMike Christie 	if (ret < 0)
2012b3af66e2SMike Christie 		goto free_skb;
2013b3af66e2SMike Christie 
2014b3af66e2SMike Christie 	ret = nla_put_u32(skb, TCMU_ATTR_DEVICE_ID, udev->se_dev.dev_index);
20156e14eab9SNicholas Bellinger 	if (ret < 0)
20166e14eab9SNicholas Bellinger 		goto free_skb;
20177c9e7a6fSAndy Grover 
20180e5aee39SZhu Lingshan 	*buf = skb;
20190e5aee39SZhu Lingshan 	*hdr = msg_header;
20200e5aee39SZhu Lingshan 	return ret;
20210e5aee39SZhu Lingshan 
20220e5aee39SZhu Lingshan free_skb:
20230e5aee39SZhu Lingshan 	nlmsg_free(skb);
20240e5aee39SZhu Lingshan 	return ret;
20252d76443eSMike Christie }
20262d76443eSMike Christie 
20270e5aee39SZhu Lingshan static int tcmu_netlink_event_send(struct tcmu_dev *udev,
20280e5aee39SZhu Lingshan 				   enum tcmu_genl_cmd cmd,
202906add777SMike Christie 				   struct sk_buff *skb, void *msg_header)
20300e5aee39SZhu Lingshan {
203106add777SMike Christie 	int ret;
20328a45885cSBryant G. Ly 
2033053c095aSJohannes Berg 	genlmsg_end(skb, msg_header);
20347c9e7a6fSAndy Grover 
20359de3a1efSMike Christie 	ret = tcmu_init_genl_cmd_reply(udev, cmd);
20369de3a1efSMike Christie 	if (ret) {
20379de3a1efSMike Christie 		nlmsg_free(skb);
20389de3a1efSMike Christie 		return ret;
20399de3a1efSMike Christie 	}
2040b3af66e2SMike Christie 
204120c08b36SSheng Yang 	ret = genlmsg_multicast_allns(&tcmu_genl_family, skb, 0,
20427c9e7a6fSAndy Grover 				      TCMU_MCGRP_CONFIG, GFP_KERNEL);
20432ff717cdSCathy Avery 
20442ff717cdSCathy Avery 	/* Wait during an add as the listener may not be up yet */
20452ff717cdSCathy Avery 	if (ret == 0 ||
20462ff717cdSCathy Avery 	   (ret == -ESRCH && cmd == TCMU_CMD_ADDED_DEVICE))
20472ff717cdSCathy Avery 		return tcmu_wait_genl_cmd_reply(udev);
20487d894862SLi Zhong 	else
20497d894862SLi Zhong 		tcmu_destroy_genl_cmd_reply(udev);
20502ff717cdSCathy Avery 
20510e5aee39SZhu Lingshan 	return ret;
20520e5aee39SZhu Lingshan }
20537c9e7a6fSAndy Grover 
2054e0c240acSZhu Lingshan static int tcmu_send_dev_add_event(struct tcmu_dev *udev)
2055e0c240acSZhu Lingshan {
2056e0c240acSZhu Lingshan 	struct sk_buff *skb = NULL;
2057e0c240acSZhu Lingshan 	void *msg_header = NULL;
2058e0c240acSZhu Lingshan 	int ret = 0;
2059e0c240acSZhu Lingshan 
2060e0c240acSZhu Lingshan 	ret = tcmu_netlink_event_init(udev, TCMU_CMD_ADDED_DEVICE, &skb,
2061e0c240acSZhu Lingshan 				      &msg_header);
2062e0c240acSZhu Lingshan 	if (ret < 0)
20637c9e7a6fSAndy Grover 		return ret;
206406add777SMike Christie 	return tcmu_netlink_event_send(udev, TCMU_CMD_ADDED_DEVICE, skb,
206506add777SMike Christie 				       msg_header);
2066e0c240acSZhu Lingshan }
2067e0c240acSZhu Lingshan 
2068f892bd8eSZhu Lingshan static int tcmu_send_dev_remove_event(struct tcmu_dev *udev)
2069f892bd8eSZhu Lingshan {
2070f892bd8eSZhu Lingshan 	struct sk_buff *skb = NULL;
2071f892bd8eSZhu Lingshan 	void *msg_header = NULL;
2072f892bd8eSZhu Lingshan 	int ret = 0;
2073f892bd8eSZhu Lingshan 
2074f892bd8eSZhu Lingshan 	ret = tcmu_netlink_event_init(udev, TCMU_CMD_REMOVED_DEVICE,
2075f892bd8eSZhu Lingshan 				      &skb, &msg_header);
2076f892bd8eSZhu Lingshan 	if (ret < 0)
20776e14eab9SNicholas Bellinger 		return ret;
2078f892bd8eSZhu Lingshan 	return tcmu_netlink_event_send(udev, TCMU_CMD_REMOVED_DEVICE,
207906add777SMike Christie 				       skb, msg_header);
20807c9e7a6fSAndy Grover }
20817c9e7a6fSAndy Grover 
2082de8c5221SBryant G. Ly static int tcmu_update_uio_info(struct tcmu_dev *udev)
20837c9e7a6fSAndy Grover {
20847c9e7a6fSAndy Grover 	struct tcmu_hba *hba = udev->hba->hba_ptr;
20857c9e7a6fSAndy Grover 	struct uio_info *info;
20867c9e7a6fSAndy Grover 	char *str;
20877c9e7a6fSAndy Grover 
20887c9e7a6fSAndy Grover 	info = &udev->uio_info;
208922c2f35fSChristophe JAILLET 
209022c2f35fSChristophe JAILLET 	if (udev->dev_config[0])
209122c2f35fSChristophe JAILLET 		str = kasprintf(GFP_KERNEL, "tcm-user/%u/%s/%s", hba->host_id,
209222c2f35fSChristophe JAILLET 				udev->name, udev->dev_config);
209322c2f35fSChristophe JAILLET 	else
209422c2f35fSChristophe JAILLET 		str = kasprintf(GFP_KERNEL, "tcm-user/%u/%s", hba->host_id,
209522c2f35fSChristophe JAILLET 				udev->name);
20967c9e7a6fSAndy Grover 	if (!str)
20977c9e7a6fSAndy Grover 		return -ENOMEM;
20987c9e7a6fSAndy Grover 
2099ededd039SBryant G. Ly 	/* If the old string exists, free it */
2100ededd039SBryant G. Ly 	kfree(info->name);
21017c9e7a6fSAndy Grover 	info->name = str;
21027c9e7a6fSAndy Grover 
2103de8c5221SBryant G. Ly 	return 0;
2104de8c5221SBryant G. Ly }
2105de8c5221SBryant G. Ly 
2106de8c5221SBryant G. Ly static int tcmu_configure_device(struct se_device *dev)
2107de8c5221SBryant G. Ly {
2108de8c5221SBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(dev);
2109de8c5221SBryant G. Ly 	struct uio_info *info;
2110de8c5221SBryant G. Ly 	struct tcmu_mailbox *mb;
2111f5ce815fSBodo Stroesser 	size_t data_size;
2112de8c5221SBryant G. Ly 	int ret = 0;
2113de8c5221SBryant G. Ly 
2114de8c5221SBryant G. Ly 	ret = tcmu_update_uio_info(udev);
2115de8c5221SBryant G. Ly 	if (ret)
2116de8c5221SBryant G. Ly 		return ret;
2117de8c5221SBryant G. Ly 
2118de8c5221SBryant G. Ly 	info = &udev->uio_info;
2119de8c5221SBryant G. Ly 
2120c97840c8SMike Christie 	mutex_lock(&udev->cmdr_lock);
212198effe47SAndy Shevchenko 	udev->data_bitmap = bitmap_zalloc(udev->max_blocks, GFP_KERNEL);
2122c97840c8SMike Christie 	mutex_unlock(&udev->cmdr_lock);
2123a24e7917SWei Yongjun 	if (!udev->data_bitmap) {
2124a24e7917SWei Yongjun 		ret = -ENOMEM;
212580eb8761SMike Christie 		goto err_bitmap_alloc;
2126a24e7917SWei Yongjun 	}
212780eb8761SMike Christie 
2128ecddbb7eSBodo Stroesser 	mb = vzalloc(MB_CMDR_SIZE);
2129ecddbb7eSBodo Stroesser 	if (!mb) {
21307c9e7a6fSAndy Grover 		ret = -ENOMEM;
21317c9e7a6fSAndy Grover 		goto err_vzalloc;
21327c9e7a6fSAndy Grover 	}
21337c9e7a6fSAndy Grover 
21347c9e7a6fSAndy Grover 	/* mailbox fits in first part of CMDR space */
2135ecddbb7eSBodo Stroesser 	udev->mb_addr = mb;
2136ecddbb7eSBodo Stroesser 	udev->cmdr = (void *)mb + CMDR_OFF;
2137ecddbb7eSBodo Stroesser 	udev->cmdr_size = CMDR_SIZE;
2138ecddbb7eSBodo Stroesser 	udev->data_off = MB_CMDR_SIZE;
2139f5ce815fSBodo Stroesser 	data_size = TCMU_MBS_TO_PAGES(udev->data_area_mb) << PAGE_SHIFT;
2140f5ce815fSBodo Stroesser 	udev->mmap_pages = (data_size + MB_CMDR_SIZE) >> PAGE_SHIFT;
2141e719afdcSBodo Stroesser 	udev->data_blk_size = udev->data_pages_per_blk * PAGE_SIZE;
2142b6df4b79SXiubo Li 	udev->dbi_thresh = 0; /* Default in Idle state */
21437c9e7a6fSAndy Grover 
2144141685a3SXiubo Li 	/* Initialise the mailbox of the ring buffer */
21450ad46af8SAndy Grover 	mb->version = TCMU_MAILBOX_VERSION;
2146bc2d214aSBodo Stroesser 	mb->flags = TCMU_MAILBOX_FLAG_CAP_OOOC |
2147bc2d214aSBodo Stroesser 		    TCMU_MAILBOX_FLAG_CAP_READ_LEN |
2148bc2d214aSBodo Stroesser 		    TCMU_MAILBOX_FLAG_CAP_TMR;
21497c9e7a6fSAndy Grover 	mb->cmdr_off = CMDR_OFF;
21507c9e7a6fSAndy Grover 	mb->cmdr_size = udev->cmdr_size;
21517c9e7a6fSAndy Grover 
21527c9e7a6fSAndy Grover 	WARN_ON(!PAGE_ALIGNED(udev->data_off));
2153f5ce815fSBodo Stroesser 	WARN_ON(data_size % PAGE_SIZE);
21547c9e7a6fSAndy Grover 
2155ac64a2ceSDavid Disseldorp 	info->version = __stringify(TCMU_MAILBOX_VERSION);
21567c9e7a6fSAndy Grover 
21577c9e7a6fSAndy Grover 	info->mem[0].name = "tcm-user command & data buffer";
21580633e123SArnd Bergmann 	info->mem[0].addr = (phys_addr_t)(uintptr_t)udev->mb_addr;
2159f5ce815fSBodo Stroesser 	info->mem[0].size = data_size + MB_CMDR_SIZE;
2160141685a3SXiubo Li 	info->mem[0].memtype = UIO_MEM_NONE;
21617c9e7a6fSAndy Grover 
21627c9e7a6fSAndy Grover 	info->irqcontrol = tcmu_irqcontrol;
21637c9e7a6fSAndy Grover 	info->irq = UIO_IRQ_CUSTOM;
21647c9e7a6fSAndy Grover 
21657c9e7a6fSAndy Grover 	info->mmap = tcmu_mmap;
21667c9e7a6fSAndy Grover 	info->open = tcmu_open;
21677c9e7a6fSAndy Grover 	info->release = tcmu_release;
21687c9e7a6fSAndy Grover 
21697c9e7a6fSAndy Grover 	ret = uio_register_device(tcmu_root_device, info);
21707c9e7a6fSAndy Grover 	if (ret)
21717c9e7a6fSAndy Grover 		goto err_register;
21727c9e7a6fSAndy Grover 
217381ee28deSSheng Yang 	/* User can set hw_block_size before enable the device */
217481ee28deSSheng Yang 	if (dev->dev_attrib.hw_block_size == 0)
21757c9e7a6fSAndy Grover 		dev->dev_attrib.hw_block_size = 512;
217681ee28deSSheng Yang 	/* Other attributes can be configured in userspace */
21773abaa2bfSMike Christie 	if (!dev->dev_attrib.hw_max_sectors)
21787c9e7a6fSAndy Grover 		dev->dev_attrib.hw_max_sectors = 128;
21799a8bb606SBryant G. Ly 	if (!dev->dev_attrib.emulate_write_cache)
21809a8bb606SBryant G. Ly 		dev->dev_attrib.emulate_write_cache = 0;
21817c9e7a6fSAndy Grover 	dev->dev_attrib.hw_queue_depth = 128;
21827c9e7a6fSAndy Grover 
2183b849b456SKenjiro Nakayama 	/* If user didn't explicitly disable netlink reply support, use
2184b849b456SKenjiro Nakayama 	 * module scope setting.
2185b849b456SKenjiro Nakayama 	 */
2186b849b456SKenjiro Nakayama 	if (udev->nl_reply_supported >= 0)
2187b849b456SKenjiro Nakayama 		udev->nl_reply_supported = tcmu_kern_cmd_reply_supported;
2188b849b456SKenjiro Nakayama 
2189f3cdbe39SMike Christie 	/*
2190f3cdbe39SMike Christie 	 * Get a ref incase userspace does a close on the uio device before
2191f3cdbe39SMike Christie 	 * LIO has initiated tcmu_free_device.
2192f3cdbe39SMike Christie 	 */
2193f3cdbe39SMike Christie 	kref_get(&udev->kref);
2194f3cdbe39SMike Christie 
2195e0c240acSZhu Lingshan 	ret = tcmu_send_dev_add_event(udev);
21967c9e7a6fSAndy Grover 	if (ret)
21977c9e7a6fSAndy Grover 		goto err_netlink;
21987c9e7a6fSAndy Grover 
2199b6df4b79SXiubo Li 	mutex_lock(&root_udev_mutex);
2200b6df4b79SXiubo Li 	list_add(&udev->node, &root_udev);
2201b6df4b79SXiubo Li 	mutex_unlock(&root_udev_mutex);
2202b6df4b79SXiubo Li 
22037c9e7a6fSAndy Grover 	return 0;
22047c9e7a6fSAndy Grover 
22057c9e7a6fSAndy Grover err_netlink:
2206f3cdbe39SMike Christie 	kref_put(&udev->kref, tcmu_dev_kref_release);
22077c9e7a6fSAndy Grover 	uio_unregister_device(&udev->uio_info);
22087c9e7a6fSAndy Grover err_register:
22097c9e7a6fSAndy Grover 	vfree(udev->mb_addr);
2210c22adc0bSXiubo Li 	udev->mb_addr = NULL;
22117c9e7a6fSAndy Grover err_vzalloc:
221298effe47SAndy Shevchenko 	bitmap_free(udev->data_bitmap);
221380eb8761SMike Christie 	udev->data_bitmap = NULL;
221480eb8761SMike Christie err_bitmap_alloc:
22157c9e7a6fSAndy Grover 	kfree(info->name);
2216f3cdbe39SMike Christie 	info->name = NULL;
22177c9e7a6fSAndy Grover 
22187c9e7a6fSAndy Grover 	return ret;
22197c9e7a6fSAndy Grover }
22207c9e7a6fSAndy Grover 
22217c9e7a6fSAndy Grover static void tcmu_free_device(struct se_device *dev)
22227c9e7a6fSAndy Grover {
22237c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
222492634706SMike Christie 
222592634706SMike Christie 	/* release ref from init */
222692634706SMike Christie 	kref_put(&udev->kref, tcmu_dev_kref_release);
222792634706SMike Christie }
222892634706SMike Christie 
222992634706SMike Christie static void tcmu_destroy_device(struct se_device *dev)
223092634706SMike Christie {
223192634706SMike Christie 	struct tcmu_dev *udev = TCMU_DEV(dev);
22327c9e7a6fSAndy Grover 
22339103575aSMike Christie 	del_timer_sync(&udev->cmd_timer);
22349103575aSMike Christie 	del_timer_sync(&udev->qfull_timer);
22357c9e7a6fSAndy Grover 
2236b6df4b79SXiubo Li 	mutex_lock(&root_udev_mutex);
2237b6df4b79SXiubo Li 	list_del(&udev->node);
2238b6df4b79SXiubo Li 	mutex_unlock(&root_udev_mutex);
2239b6df4b79SXiubo Li 
2240f892bd8eSZhu Lingshan 	tcmu_send_dev_remove_event(udev);
22417c9e7a6fSAndy Grover 
22427c9e7a6fSAndy Grover 	uio_unregister_device(&udev->uio_info);
22439260695dSMike Christie 
22449260695dSMike Christie 	/* release ref from configure */
22459260695dSMike Christie 	kref_put(&udev->kref, tcmu_dev_kref_release);
22467c9e7a6fSAndy Grover }
22477c9e7a6fSAndy Grover 
2248892782caSMike Christie static void tcmu_unblock_dev(struct tcmu_dev *udev)
2249892782caSMike Christie {
2250892782caSMike Christie 	mutex_lock(&udev->cmdr_lock);
2251892782caSMike Christie 	clear_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags);
2252892782caSMike Christie 	mutex_unlock(&udev->cmdr_lock);
2253892782caSMike Christie }
2254892782caSMike Christie 
2255892782caSMike Christie static void tcmu_block_dev(struct tcmu_dev *udev)
2256892782caSMike Christie {
2257892782caSMike Christie 	mutex_lock(&udev->cmdr_lock);
2258892782caSMike Christie 
2259892782caSMike Christie 	if (test_and_set_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags))
2260892782caSMike Christie 		goto unlock;
2261892782caSMike Christie 
2262892782caSMike Christie 	/* complete IO that has executed successfully */
2263892782caSMike Christie 	tcmu_handle_completions(udev);
2264892782caSMike Christie 	/* fail IO waiting to be queued */
2265a94a2572SXiubo Li 	run_qfull_queue(udev, true);
2266892782caSMike Christie 
2267892782caSMike Christie unlock:
2268892782caSMike Christie 	mutex_unlock(&udev->cmdr_lock);
2269892782caSMike Christie }
2270892782caSMike Christie 
2271892782caSMike Christie static void tcmu_reset_ring(struct tcmu_dev *udev, u8 err_level)
2272892782caSMike Christie {
2273892782caSMike Christie 	struct tcmu_mailbox *mb;
2274892782caSMike Christie 	struct tcmu_cmd *cmd;
2275d3cbb743SBodo Stroesser 	unsigned long i;
2276892782caSMike Christie 
2277892782caSMike Christie 	mutex_lock(&udev->cmdr_lock);
2278892782caSMike Christie 
2279d3cbb743SBodo Stroesser 	xa_for_each(&udev->commands, i, cmd) {
2280892782caSMike Christie 		pr_debug("removing cmd %u on dev %s from ring (is expired %d)\n",
2281892782caSMike Christie 			  cmd->cmd_id, udev->name,
2282892782caSMike Christie 			  test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags));
2283892782caSMike Christie 
2284d3cbb743SBodo Stroesser 		xa_erase(&udev->commands, i);
2285892782caSMike Christie 		if (!test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
2286a86a7586SDmitry Fomichev 			WARN_ON(!cmd->se_cmd);
2287a94a2572SXiubo Li 			list_del_init(&cmd->queue_entry);
2288780e1384SShin'ichiro Kawasaki 			cmd->se_cmd->priv = NULL;
2289892782caSMike Christie 			if (err_level == 1) {
2290892782caSMike Christie 				/*
2291892782caSMike Christie 				 * Userspace was not able to start the
2292892782caSMike Christie 				 * command or it is retryable.
2293892782caSMike Christie 				 */
2294892782caSMike Christie 				target_complete_cmd(cmd->se_cmd, SAM_STAT_BUSY);
2295892782caSMike Christie 			} else {
2296892782caSMike Christie 				/* hard failure */
2297892782caSMike Christie 				target_complete_cmd(cmd->se_cmd,
2298892782caSMike Christie 						    SAM_STAT_CHECK_CONDITION);
2299892782caSMike Christie 			}
2300892782caSMike Christie 		}
2301892782caSMike Christie 		tcmu_cmd_free_data(cmd, cmd->dbi_cnt);
2302892782caSMike Christie 		tcmu_free_cmd(cmd);
2303892782caSMike Christie 	}
2304892782caSMike Christie 
2305892782caSMike Christie 	mb = udev->mb_addr;
2306892782caSMike Christie 	tcmu_flush_dcache_range(mb, sizeof(*mb));
2307892782caSMike Christie 	pr_debug("mb last %u head %u tail %u\n", udev->cmdr_last_cleaned,
2308892782caSMike Christie 		 mb->cmd_tail, mb->cmd_head);
2309892782caSMike Christie 
2310892782caSMike Christie 	udev->cmdr_last_cleaned = 0;
2311892782caSMike Christie 	mb->cmd_tail = 0;
2312892782caSMike Christie 	mb->cmd_head = 0;
2313892782caSMike Christie 	tcmu_flush_dcache_range(mb, sizeof(*mb));
2314066f79a5SBodo Stroesser 	clear_bit(TCMU_DEV_BIT_BROKEN, &udev->flags);
2315892782caSMike Christie 
2316892782caSMike Christie 	del_timer(&udev->cmd_timer);
2317892782caSMike Christie 
2318bc2d214aSBodo Stroesser 	/*
2319bc2d214aSBodo Stroesser 	 * ring is empty and qfull queue never contains aborted commands.
2320bc2d214aSBodo Stroesser 	 * So TMRs in tmr queue do not contain relevant cmd_ids.
2321bc2d214aSBodo Stroesser 	 * After a ring reset userspace should do a fresh start, so
2322bc2d214aSBodo Stroesser 	 * even LUN RESET message is no longer relevant.
2323bc2d214aSBodo Stroesser 	 * Therefore remove all TMRs from qfull queue
2324bc2d214aSBodo Stroesser 	 */
2325bc2d214aSBodo Stroesser 	tcmu_remove_all_queued_tmr(udev);
2326bc2d214aSBodo Stroesser 
232761fb2482SBodo Stroesser 	run_qfull_queue(udev, false);
232861fb2482SBodo Stroesser 
2329892782caSMike Christie 	mutex_unlock(&udev->cmdr_lock);
2330892782caSMike Christie }
2331892782caSMike Christie 
23327c9e7a6fSAndy Grover enum {
23333abaa2bfSMike Christie 	Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_hw_max_sectors,
233408976cb5SBodo Stroesser 	Opt_nl_reply_supported, Opt_max_data_area_mb, Opt_data_pages_per_blk,
233508976cb5SBodo Stroesser 	Opt_err,
23367c9e7a6fSAndy Grover };
23377c9e7a6fSAndy Grover 
23387c9e7a6fSAndy Grover static match_table_t tokens = {
23397c9e7a6fSAndy Grover 	{Opt_dev_config, "dev_config=%s"},
23400e0d7526SMike Christie 	{Opt_dev_size, "dev_size=%s"},
2341b60cb1f8SMike Christie 	{Opt_hw_block_size, "hw_block_size=%d"},
2342b60cb1f8SMike Christie 	{Opt_hw_max_sectors, "hw_max_sectors=%d"},
2343b849b456SKenjiro Nakayama 	{Opt_nl_reply_supported, "nl_reply_supported=%d"},
2344c97840c8SMike Christie 	{Opt_max_data_area_mb, "max_data_area_mb=%d"},
234508976cb5SBodo Stroesser 	{Opt_data_pages_per_blk, "data_pages_per_blk=%d"},
23467c9e7a6fSAndy Grover 	{Opt_err, NULL}
23477c9e7a6fSAndy Grover };
23487c9e7a6fSAndy Grover 
23493abaa2bfSMike Christie static int tcmu_set_dev_attrib(substring_t *arg, u32 *dev_attrib)
23503abaa2bfSMike Christie {
2351b60cb1f8SMike Christie 	int val, ret;
23523abaa2bfSMike Christie 
2353b60cb1f8SMike Christie 	ret = match_int(arg, &val);
23543abaa2bfSMike Christie 	if (ret < 0) {
2355b60cb1f8SMike Christie 		pr_err("match_int() failed for dev attrib. Error %d.\n",
2356b60cb1f8SMike Christie 		       ret);
23573abaa2bfSMike Christie 		return ret;
23583abaa2bfSMike Christie 	}
2359b60cb1f8SMike Christie 
2360b60cb1f8SMike Christie 	if (val <= 0) {
2361b60cb1f8SMike Christie 		pr_err("Invalid dev attrib value %d. Must be greater than zero.\n",
2362b60cb1f8SMike Christie 		       val);
23633abaa2bfSMike Christie 		return -EINVAL;
23643abaa2bfSMike Christie 	}
2365b60cb1f8SMike Christie 	*dev_attrib = val;
23663abaa2bfSMike Christie 	return 0;
23673abaa2bfSMike Christie }
23683abaa2bfSMike Christie 
2369c97840c8SMike Christie static int tcmu_set_max_blocks_param(struct tcmu_dev *udev, substring_t *arg)
2370c97840c8SMike Christie {
2371f5ce815fSBodo Stroesser 	int val, ret;
2372e719afdcSBodo Stroesser 	uint32_t pages_per_blk = udev->data_pages_per_blk;
2373c97840c8SMike Christie 
2374c97840c8SMike Christie 	ret = match_int(arg, &val);
2375c97840c8SMike Christie 	if (ret < 0) {
2376c97840c8SMike Christie 		pr_err("match_int() failed for max_data_area_mb=. Error %d.\n",
2377c97840c8SMike Christie 		       ret);
2378c97840c8SMike Christie 		return ret;
2379c97840c8SMike Christie 	}
2380f5ce815fSBodo Stroesser 	if (val <= 0) {
2381c97840c8SMike Christie 		pr_err("Invalid max_data_area %d.\n", val);
2382c97840c8SMike Christie 		return -EINVAL;
2383c97840c8SMike Christie 	}
2384f5ce815fSBodo Stroesser 	if (val > TCMU_PAGES_TO_MBS(tcmu_global_max_pages)) {
2385f5ce815fSBodo Stroesser 		pr_err("%d is too large. Adjusting max_data_area_mb to global limit of %u\n",
2386f5ce815fSBodo Stroesser 		       val, TCMU_PAGES_TO_MBS(tcmu_global_max_pages));
2387f5ce815fSBodo Stroesser 		val = TCMU_PAGES_TO_MBS(tcmu_global_max_pages);
2388f5ce815fSBodo Stroesser 	}
2389e719afdcSBodo Stroesser 	if (TCMU_MBS_TO_PAGES(val) < pages_per_blk) {
2390e719afdcSBodo Stroesser 		pr_err("Invalid max_data_area %d (%zu pages): smaller than data_pages_per_blk (%u pages).\n",
2391e719afdcSBodo Stroesser 		       val, TCMU_MBS_TO_PAGES(val), pages_per_blk);
2392f5ce815fSBodo Stroesser 		return -EINVAL;
2393f5ce815fSBodo Stroesser 	}
2394c97840c8SMike Christie 
2395c97840c8SMike Christie 	mutex_lock(&udev->cmdr_lock);
2396c97840c8SMike Christie 	if (udev->data_bitmap) {
2397c97840c8SMike Christie 		pr_err("Cannot set max_data_area_mb after it has been enabled.\n");
2398c97840c8SMike Christie 		ret = -EINVAL;
2399c97840c8SMike Christie 		goto unlock;
2400c97840c8SMike Christie 	}
2401c97840c8SMike Christie 
2402f5ce815fSBodo Stroesser 	udev->data_area_mb = val;
2403e719afdcSBodo Stroesser 	udev->max_blocks = TCMU_MBS_TO_PAGES(val) / pages_per_blk;
2404c97840c8SMike Christie 
2405c97840c8SMike Christie unlock:
2406c97840c8SMike Christie 	mutex_unlock(&udev->cmdr_lock);
2407c97840c8SMike Christie 	return ret;
2408c97840c8SMike Christie }
2409c97840c8SMike Christie 
241008976cb5SBodo Stroesser static int tcmu_set_data_pages_per_blk(struct tcmu_dev *udev, substring_t *arg)
241108976cb5SBodo Stroesser {
241208976cb5SBodo Stroesser 	int val, ret;
241308976cb5SBodo Stroesser 
241408976cb5SBodo Stroesser 	ret = match_int(arg, &val);
241508976cb5SBodo Stroesser 	if (ret < 0) {
241608976cb5SBodo Stroesser 		pr_err("match_int() failed for data_pages_per_blk=. Error %d.\n",
241708976cb5SBodo Stroesser 		       ret);
241808976cb5SBodo Stroesser 		return ret;
241908976cb5SBodo Stroesser 	}
242008976cb5SBodo Stroesser 
242108976cb5SBodo Stroesser 	if (val > TCMU_MBS_TO_PAGES(udev->data_area_mb)) {
242208976cb5SBodo Stroesser 		pr_err("Invalid data_pages_per_blk %d: greater than max_data_area_mb %d -> %zd pages).\n",
242308976cb5SBodo Stroesser 		       val, udev->data_area_mb,
242408976cb5SBodo Stroesser 		       TCMU_MBS_TO_PAGES(udev->data_area_mb));
242508976cb5SBodo Stroesser 		return -EINVAL;
242608976cb5SBodo Stroesser 	}
242708976cb5SBodo Stroesser 
242808976cb5SBodo Stroesser 	mutex_lock(&udev->cmdr_lock);
242908976cb5SBodo Stroesser 	if (udev->data_bitmap) {
243008976cb5SBodo Stroesser 		pr_err("Cannot set data_pages_per_blk after it has been enabled.\n");
243108976cb5SBodo Stroesser 		ret = -EINVAL;
243208976cb5SBodo Stroesser 		goto unlock;
243308976cb5SBodo Stroesser 	}
243408976cb5SBodo Stroesser 
243508976cb5SBodo Stroesser 	udev->data_pages_per_blk = val;
243608976cb5SBodo Stroesser 	udev->max_blocks = TCMU_MBS_TO_PAGES(udev->data_area_mb) / val;
243708976cb5SBodo Stroesser 
243808976cb5SBodo Stroesser unlock:
243908976cb5SBodo Stroesser 	mutex_unlock(&udev->cmdr_lock);
244008976cb5SBodo Stroesser 	return ret;
244108976cb5SBodo Stroesser }
244208976cb5SBodo Stroesser 
24437c9e7a6fSAndy Grover static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev,
24447c9e7a6fSAndy Grover 		const char *page, ssize_t count)
24457c9e7a6fSAndy Grover {
24467c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
24470e0d7526SMike Christie 	char *orig, *ptr, *opts;
24487c9e7a6fSAndy Grover 	substring_t args[MAX_OPT_ARGS];
2449c97840c8SMike Christie 	int ret = 0, token;
24507c9e7a6fSAndy Grover 
24517c9e7a6fSAndy Grover 	opts = kstrdup(page, GFP_KERNEL);
24527c9e7a6fSAndy Grover 	if (!opts)
24537c9e7a6fSAndy Grover 		return -ENOMEM;
24547c9e7a6fSAndy Grover 
24557c9e7a6fSAndy Grover 	orig = opts;
24567c9e7a6fSAndy Grover 
24577c9e7a6fSAndy Grover 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
24587c9e7a6fSAndy Grover 		if (!*ptr)
24597c9e7a6fSAndy Grover 			continue;
24607c9e7a6fSAndy Grover 
24617c9e7a6fSAndy Grover 		token = match_token(ptr, tokens, args);
24627c9e7a6fSAndy Grover 		switch (token) {
24637c9e7a6fSAndy Grover 		case Opt_dev_config:
24647c9e7a6fSAndy Grover 			if (match_strlcpy(udev->dev_config, &args[0],
24657c9e7a6fSAndy Grover 					  TCMU_CONFIG_LEN) == 0) {
24667c9e7a6fSAndy Grover 				ret = -EINVAL;
24677c9e7a6fSAndy Grover 				break;
24687c9e7a6fSAndy Grover 			}
24697c9e7a6fSAndy Grover 			pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config);
24707c9e7a6fSAndy Grover 			break;
24717c9e7a6fSAndy Grover 		case Opt_dev_size:
24720e0d7526SMike Christie 			ret = match_u64(&args[0], &udev->dev_size);
24737c9e7a6fSAndy Grover 			if (ret < 0)
24740e0d7526SMike Christie 				pr_err("match_u64() failed for dev_size=. Error %d.\n",
24750e0d7526SMike Christie 				       ret);
24767c9e7a6fSAndy Grover 			break;
24779c1cd1b6SAndy Grover 		case Opt_hw_block_size:
24783abaa2bfSMike Christie 			ret = tcmu_set_dev_attrib(&args[0],
24793abaa2bfSMike Christie 					&(dev->dev_attrib.hw_block_size));
24809c1cd1b6SAndy Grover 			break;
24813abaa2bfSMike Christie 		case Opt_hw_max_sectors:
24823abaa2bfSMike Christie 			ret = tcmu_set_dev_attrib(&args[0],
24833abaa2bfSMike Christie 					&(dev->dev_attrib.hw_max_sectors));
24849c1cd1b6SAndy Grover 			break;
2485b849b456SKenjiro Nakayama 		case Opt_nl_reply_supported:
2486b60cb1f8SMike Christie 			ret = match_int(&args[0], &udev->nl_reply_supported);
2487b849b456SKenjiro Nakayama 			if (ret < 0)
2488b60cb1f8SMike Christie 				pr_err("match_int() failed for nl_reply_supported=. Error %d.\n",
2489b60cb1f8SMike Christie 				       ret);
2490b849b456SKenjiro Nakayama 			break;
249180eb8761SMike Christie 		case Opt_max_data_area_mb:
2492c97840c8SMike Christie 			ret = tcmu_set_max_blocks_param(udev, &args[0]);
249380eb8761SMike Christie 			break;
249408976cb5SBodo Stroesser 		case Opt_data_pages_per_blk:
249508976cb5SBodo Stroesser 			ret = tcmu_set_data_pages_per_blk(udev, &args[0]);
249608976cb5SBodo Stroesser 			break;
24977c9e7a6fSAndy Grover 		default:
24987c9e7a6fSAndy Grover 			break;
24997c9e7a6fSAndy Grover 		}
25002579325cSMike Christie 
25012579325cSMike Christie 		if (ret)
25022579325cSMike Christie 			break;
25037c9e7a6fSAndy Grover 	}
25047c9e7a6fSAndy Grover 
25057c9e7a6fSAndy Grover 	kfree(orig);
25067c9e7a6fSAndy Grover 	return (!ret) ? count : ret;
25077c9e7a6fSAndy Grover }
25087c9e7a6fSAndy Grover 
25097c9e7a6fSAndy Grover static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b)
25107c9e7a6fSAndy Grover {
25117c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
25127c9e7a6fSAndy Grover 	ssize_t bl = 0;
25137c9e7a6fSAndy Grover 
25147c9e7a6fSAndy Grover 	bl = sprintf(b + bl, "Config: %s ",
25157c9e7a6fSAndy Grover 		     udev->dev_config[0] ? udev->dev_config : "NULL");
25160e0d7526SMike Christie 	bl += sprintf(b + bl, "Size: %llu ", udev->dev_size);
251708976cb5SBodo Stroesser 	bl += sprintf(b + bl, "MaxDataAreaMB: %u ", udev->data_area_mb);
251808976cb5SBodo Stroesser 	bl += sprintf(b + bl, "DataPagesPerBlk: %u\n", udev->data_pages_per_blk);
25197c9e7a6fSAndy Grover 
25207c9e7a6fSAndy Grover 	return bl;
25217c9e7a6fSAndy Grover }
25227c9e7a6fSAndy Grover 
25237c9e7a6fSAndy Grover static sector_t tcmu_get_blocks(struct se_device *dev)
25247c9e7a6fSAndy Grover {
25257c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
25267c9e7a6fSAndy Grover 
25277c9e7a6fSAndy Grover 	return div_u64(udev->dev_size - dev->dev_attrib.block_size,
25287c9e7a6fSAndy Grover 		       dev->dev_attrib.block_size);
25297c9e7a6fSAndy Grover }
25307c9e7a6fSAndy Grover 
25317c9e7a6fSAndy Grover static sense_reason_t
25327c9e7a6fSAndy Grover tcmu_parse_cdb(struct se_cmd *cmd)
25337c9e7a6fSAndy Grover {
253402eb924fSAndy Grover 	return passthrough_parse_cdb(cmd, tcmu_queue_cmd);
25359c1cd1b6SAndy Grover }
25369c1cd1b6SAndy Grover 
25377d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_show(struct config_item *item, char *page)
25387d7a7435SNicholas Bellinger {
25397d7a7435SNicholas Bellinger 	struct se_dev_attrib *da = container_of(to_config_group(item),
25407d7a7435SNicholas Bellinger 					struct se_dev_attrib, da_group);
2541b5ab697cSKenjiro Nakayama 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
25427d7a7435SNicholas Bellinger 
25437d7a7435SNicholas Bellinger 	return snprintf(page, PAGE_SIZE, "%lu\n", udev->cmd_time_out / MSEC_PER_SEC);
25447d7a7435SNicholas Bellinger }
25457d7a7435SNicholas Bellinger 
25467d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_store(struct config_item *item, const char *page,
25477d7a7435SNicholas Bellinger 				       size_t count)
25487d7a7435SNicholas Bellinger {
25497d7a7435SNicholas Bellinger 	struct se_dev_attrib *da = container_of(to_config_group(item),
25507d7a7435SNicholas Bellinger 					struct se_dev_attrib, da_group);
25517d7a7435SNicholas Bellinger 	struct tcmu_dev *udev = container_of(da->da_dev,
25527d7a7435SNicholas Bellinger 					struct tcmu_dev, se_dev);
25537d7a7435SNicholas Bellinger 	u32 val;
25547d7a7435SNicholas Bellinger 	int ret;
25557d7a7435SNicholas Bellinger 
25567d7a7435SNicholas Bellinger 	if (da->da_dev->export_count) {
25577d7a7435SNicholas Bellinger 		pr_err("Unable to set tcmu cmd_time_out while exports exist\n");
25587d7a7435SNicholas Bellinger 		return -EINVAL;
25597d7a7435SNicholas Bellinger 	}
25607d7a7435SNicholas Bellinger 
25617d7a7435SNicholas Bellinger 	ret = kstrtou32(page, 0, &val);
25627d7a7435SNicholas Bellinger 	if (ret < 0)
25637d7a7435SNicholas Bellinger 		return ret;
25647d7a7435SNicholas Bellinger 
25657d7a7435SNicholas Bellinger 	udev->cmd_time_out = val * MSEC_PER_SEC;
25667d7a7435SNicholas Bellinger 	return count;
25677d7a7435SNicholas Bellinger }
25687d7a7435SNicholas Bellinger CONFIGFS_ATTR(tcmu_, cmd_time_out);
25697d7a7435SNicholas Bellinger 
25709103575aSMike Christie static ssize_t tcmu_qfull_time_out_show(struct config_item *item, char *page)
25719103575aSMike Christie {
25729103575aSMike Christie 	struct se_dev_attrib *da = container_of(to_config_group(item),
25739103575aSMike Christie 						struct se_dev_attrib, da_group);
25749103575aSMike Christie 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
25759103575aSMike Christie 
25769103575aSMike Christie 	return snprintf(page, PAGE_SIZE, "%ld\n", udev->qfull_time_out <= 0 ?
25779103575aSMike Christie 			udev->qfull_time_out :
25789103575aSMike Christie 			udev->qfull_time_out / MSEC_PER_SEC);
25799103575aSMike Christie }
25809103575aSMike Christie 
25819103575aSMike Christie static ssize_t tcmu_qfull_time_out_store(struct config_item *item,
25829103575aSMike Christie 					 const char *page, size_t count)
25839103575aSMike Christie {
25849103575aSMike Christie 	struct se_dev_attrib *da = container_of(to_config_group(item),
25859103575aSMike Christie 					struct se_dev_attrib, da_group);
25869103575aSMike Christie 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
25879103575aSMike Christie 	s32 val;
25889103575aSMike Christie 	int ret;
25899103575aSMike Christie 
25909103575aSMike Christie 	ret = kstrtos32(page, 0, &val);
25919103575aSMike Christie 	if (ret < 0)
25929103575aSMike Christie 		return ret;
25939103575aSMike Christie 
25949103575aSMike Christie 	if (val >= 0) {
25959103575aSMike Christie 		udev->qfull_time_out = val * MSEC_PER_SEC;
2596125966dbSPrasanna Kumar Kalever 	} else if (val == -1) {
2597125966dbSPrasanna Kumar Kalever 		udev->qfull_time_out = val;
25989103575aSMike Christie 	} else {
25999103575aSMike Christie 		printk(KERN_ERR "Invalid qfull timeout value %d\n", val);
26009103575aSMike Christie 		return -EINVAL;
26019103575aSMike Christie 	}
26029103575aSMike Christie 	return count;
26039103575aSMike Christie }
26049103575aSMike Christie CONFIGFS_ATTR(tcmu_, qfull_time_out);
26059103575aSMike Christie 
260680eb8761SMike Christie static ssize_t tcmu_max_data_area_mb_show(struct config_item *item, char *page)
260780eb8761SMike Christie {
260880eb8761SMike Christie 	struct se_dev_attrib *da = container_of(to_config_group(item),
260980eb8761SMike Christie 						struct se_dev_attrib, da_group);
261080eb8761SMike Christie 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
261180eb8761SMike Christie 
2612f5ce815fSBodo Stroesser 	return snprintf(page, PAGE_SIZE, "%u\n", udev->data_area_mb);
261380eb8761SMike Christie }
261480eb8761SMike Christie CONFIGFS_ATTR_RO(tcmu_, max_data_area_mb);
261580eb8761SMike Christie 
261608976cb5SBodo Stroesser static ssize_t tcmu_data_pages_per_blk_show(struct config_item *item,
261708976cb5SBodo Stroesser 					    char *page)
261808976cb5SBodo Stroesser {
261908976cb5SBodo Stroesser 	struct se_dev_attrib *da = container_of(to_config_group(item),
262008976cb5SBodo Stroesser 						struct se_dev_attrib, da_group);
262108976cb5SBodo Stroesser 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
262208976cb5SBodo Stroesser 
262308976cb5SBodo Stroesser 	return snprintf(page, PAGE_SIZE, "%u\n", udev->data_pages_per_blk);
262408976cb5SBodo Stroesser }
262508976cb5SBodo Stroesser CONFIGFS_ATTR_RO(tcmu_, data_pages_per_blk);
262608976cb5SBodo Stroesser 
26272d76443eSMike Christie static ssize_t tcmu_dev_config_show(struct config_item *item, char *page)
2628ee018252SBryant G. Ly {
2629ee018252SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
2630ee018252SBryant G. Ly 						struct se_dev_attrib, da_group);
2631ee018252SBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2632ee018252SBryant G. Ly 
2633ee018252SBryant G. Ly 	return snprintf(page, PAGE_SIZE, "%s\n", udev->dev_config);
2634ee018252SBryant G. Ly }
2635ee018252SBryant G. Ly 
263602ccfb54SZhu Lingshan static int tcmu_send_dev_config_event(struct tcmu_dev *udev,
263702ccfb54SZhu Lingshan 				      const char *reconfig_data)
263802ccfb54SZhu Lingshan {
263902ccfb54SZhu Lingshan 	struct sk_buff *skb = NULL;
264002ccfb54SZhu Lingshan 	void *msg_header = NULL;
264102ccfb54SZhu Lingshan 	int ret = 0;
264202ccfb54SZhu Lingshan 
264302ccfb54SZhu Lingshan 	ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE,
264402ccfb54SZhu Lingshan 				      &skb, &msg_header);
264502ccfb54SZhu Lingshan 	if (ret < 0)
264602ccfb54SZhu Lingshan 		return ret;
264702ccfb54SZhu Lingshan 	ret = nla_put_string(skb, TCMU_ATTR_DEV_CFG, reconfig_data);
264802ccfb54SZhu Lingshan 	if (ret < 0) {
264902ccfb54SZhu Lingshan 		nlmsg_free(skb);
265002ccfb54SZhu Lingshan 		return ret;
265102ccfb54SZhu Lingshan 	}
265202ccfb54SZhu Lingshan 	return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE,
265306add777SMike Christie 				       skb, msg_header);
265402ccfb54SZhu Lingshan }
265502ccfb54SZhu Lingshan 
265602ccfb54SZhu Lingshan 
26572d76443eSMike Christie static ssize_t tcmu_dev_config_store(struct config_item *item, const char *page,
2658ee018252SBryant G. Ly 				     size_t count)
2659ee018252SBryant G. Ly {
2660ee018252SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
2661ee018252SBryant G. Ly 						struct se_dev_attrib, da_group);
2662ee018252SBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
26632d76443eSMike Christie 	int ret, len;
2664ee018252SBryant G. Ly 
26652d76443eSMike Christie 	len = strlen(page);
26662d76443eSMike Christie 	if (!len || len > TCMU_CONFIG_LEN - 1)
2667ee018252SBryant G. Ly 		return -EINVAL;
2668ee018252SBryant G. Ly 
2669ee018252SBryant G. Ly 	/* Check if device has been configured before */
267063d5be0fSMike Christie 	if (target_dev_configured(&udev->se_dev)) {
267102ccfb54SZhu Lingshan 		ret = tcmu_send_dev_config_event(udev, page);
2672ee018252SBryant G. Ly 		if (ret) {
2673ee018252SBryant G. Ly 			pr_err("Unable to reconfigure device\n");
2674ee018252SBryant G. Ly 			return ret;
2675ee018252SBryant G. Ly 		}
2676de8c5221SBryant G. Ly 		strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN);
2677de8c5221SBryant G. Ly 
2678de8c5221SBryant G. Ly 		ret = tcmu_update_uio_info(udev);
2679de8c5221SBryant G. Ly 		if (ret)
2680de8c5221SBryant G. Ly 			return ret;
2681de8c5221SBryant G. Ly 		return count;
2682ee018252SBryant G. Ly 	}
26832d76443eSMike Christie 	strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN);
2684ee018252SBryant G. Ly 
2685ee018252SBryant G. Ly 	return count;
2686ee018252SBryant G. Ly }
26872d76443eSMike Christie CONFIGFS_ATTR(tcmu_, dev_config);
2688ee018252SBryant G. Ly 
2689801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_show(struct config_item *item, char *page)
2690801fc54dSBryant G. Ly {
2691801fc54dSBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
2692801fc54dSBryant G. Ly 						struct se_dev_attrib, da_group);
2693801fc54dSBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2694801fc54dSBryant G. Ly 
26950e0d7526SMike Christie 	return snprintf(page, PAGE_SIZE, "%llu\n", udev->dev_size);
2696801fc54dSBryant G. Ly }
2697801fc54dSBryant G. Ly 
269884e28506SZhu Lingshan static int tcmu_send_dev_size_event(struct tcmu_dev *udev, u64 size)
269984e28506SZhu Lingshan {
270084e28506SZhu Lingshan 	struct sk_buff *skb = NULL;
270184e28506SZhu Lingshan 	void *msg_header = NULL;
270284e28506SZhu Lingshan 	int ret = 0;
270384e28506SZhu Lingshan 
270484e28506SZhu Lingshan 	ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE,
270584e28506SZhu Lingshan 				      &skb, &msg_header);
270684e28506SZhu Lingshan 	if (ret < 0)
270784e28506SZhu Lingshan 		return ret;
270884e28506SZhu Lingshan 	ret = nla_put_u64_64bit(skb, TCMU_ATTR_DEV_SIZE,
270984e28506SZhu Lingshan 				size, TCMU_ATTR_PAD);
271084e28506SZhu Lingshan 	if (ret < 0) {
271184e28506SZhu Lingshan 		nlmsg_free(skb);
271284e28506SZhu Lingshan 		return ret;
271384e28506SZhu Lingshan 	}
271484e28506SZhu Lingshan 	return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE,
271506add777SMike Christie 				       skb, msg_header);
271684e28506SZhu Lingshan }
271784e28506SZhu Lingshan 
2718801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_store(struct config_item *item, const char *page,
2719801fc54dSBryant G. Ly 				   size_t count)
2720801fc54dSBryant G. Ly {
2721801fc54dSBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
2722801fc54dSBryant G. Ly 						struct se_dev_attrib, da_group);
2723801fc54dSBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
27242d76443eSMike Christie 	u64 val;
2725801fc54dSBryant G. Ly 	int ret;
2726801fc54dSBryant G. Ly 
27272d76443eSMike Christie 	ret = kstrtou64(page, 0, &val);
2728801fc54dSBryant G. Ly 	if (ret < 0)
2729801fc54dSBryant G. Ly 		return ret;
2730801fc54dSBryant G. Ly 
2731801fc54dSBryant G. Ly 	/* Check if device has been configured before */
273263d5be0fSMike Christie 	if (target_dev_configured(&udev->se_dev)) {
273384e28506SZhu Lingshan 		ret = tcmu_send_dev_size_event(udev, val);
2734801fc54dSBryant G. Ly 		if (ret) {
2735801fc54dSBryant G. Ly 			pr_err("Unable to reconfigure device\n");
2736801fc54dSBryant G. Ly 			return ret;
2737801fc54dSBryant G. Ly 		}
2738801fc54dSBryant G. Ly 	}
27392d76443eSMike Christie 	udev->dev_size = val;
2740801fc54dSBryant G. Ly 	return count;
2741801fc54dSBryant G. Ly }
2742801fc54dSBryant G. Ly CONFIGFS_ATTR(tcmu_, dev_size);
2743801fc54dSBryant G. Ly 
2744b849b456SKenjiro Nakayama static ssize_t tcmu_nl_reply_supported_show(struct config_item *item,
2745b849b456SKenjiro Nakayama 		char *page)
2746b849b456SKenjiro Nakayama {
2747b849b456SKenjiro Nakayama 	struct se_dev_attrib *da = container_of(to_config_group(item),
2748b849b456SKenjiro Nakayama 						struct se_dev_attrib, da_group);
2749b849b456SKenjiro Nakayama 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2750b849b456SKenjiro Nakayama 
2751b849b456SKenjiro Nakayama 	return snprintf(page, PAGE_SIZE, "%d\n", udev->nl_reply_supported);
2752b849b456SKenjiro Nakayama }
2753b849b456SKenjiro Nakayama 
2754b849b456SKenjiro Nakayama static ssize_t tcmu_nl_reply_supported_store(struct config_item *item,
2755b849b456SKenjiro Nakayama 		const char *page, size_t count)
2756b849b456SKenjiro Nakayama {
2757b849b456SKenjiro Nakayama 	struct se_dev_attrib *da = container_of(to_config_group(item),
2758b849b456SKenjiro Nakayama 						struct se_dev_attrib, da_group);
2759b849b456SKenjiro Nakayama 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2760b849b456SKenjiro Nakayama 	s8 val;
2761b849b456SKenjiro Nakayama 	int ret;
2762b849b456SKenjiro Nakayama 
2763b849b456SKenjiro Nakayama 	ret = kstrtos8(page, 0, &val);
2764b849b456SKenjiro Nakayama 	if (ret < 0)
2765b849b456SKenjiro Nakayama 		return ret;
2766b849b456SKenjiro Nakayama 
2767b849b456SKenjiro Nakayama 	udev->nl_reply_supported = val;
2768b849b456SKenjiro Nakayama 	return count;
2769b849b456SKenjiro Nakayama }
2770b849b456SKenjiro Nakayama CONFIGFS_ATTR(tcmu_, nl_reply_supported);
2771b849b456SKenjiro Nakayama 
27729a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_show(struct config_item *item,
27739a8bb606SBryant G. Ly 					     char *page)
27749a8bb606SBryant G. Ly {
27759a8bb606SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
27769a8bb606SBryant G. Ly 					struct se_dev_attrib, da_group);
27779a8bb606SBryant G. Ly 
27789a8bb606SBryant G. Ly 	return snprintf(page, PAGE_SIZE, "%i\n", da->emulate_write_cache);
27799a8bb606SBryant G. Ly }
27809a8bb606SBryant G. Ly 
278133d065ccSZhu Lingshan static int tcmu_send_emulate_write_cache(struct tcmu_dev *udev, u8 val)
278233d065ccSZhu Lingshan {
278333d065ccSZhu Lingshan 	struct sk_buff *skb = NULL;
278433d065ccSZhu Lingshan 	void *msg_header = NULL;
278533d065ccSZhu Lingshan 	int ret = 0;
278633d065ccSZhu Lingshan 
278733d065ccSZhu Lingshan 	ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE,
278833d065ccSZhu Lingshan 				      &skb, &msg_header);
278933d065ccSZhu Lingshan 	if (ret < 0)
279033d065ccSZhu Lingshan 		return ret;
279133d065ccSZhu Lingshan 	ret = nla_put_u8(skb, TCMU_ATTR_WRITECACHE, val);
279233d065ccSZhu Lingshan 	if (ret < 0) {
279333d065ccSZhu Lingshan 		nlmsg_free(skb);
279433d065ccSZhu Lingshan 		return ret;
279533d065ccSZhu Lingshan 	}
279633d065ccSZhu Lingshan 	return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE,
279706add777SMike Christie 				       skb, msg_header);
279833d065ccSZhu Lingshan }
279933d065ccSZhu Lingshan 
28009a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_store(struct config_item *item,
28019a8bb606SBryant G. Ly 					      const char *page, size_t count)
28029a8bb606SBryant G. Ly {
28039a8bb606SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
28049a8bb606SBryant G. Ly 					struct se_dev_attrib, da_group);
28051068be7bSBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
28062d76443eSMike Christie 	u8 val;
28079a8bb606SBryant G. Ly 	int ret;
28089a8bb606SBryant G. Ly 
28092d76443eSMike Christie 	ret = kstrtou8(page, 0, &val);
28109a8bb606SBryant G. Ly 	if (ret < 0)
28119a8bb606SBryant G. Ly 		return ret;
28129a8bb606SBryant G. Ly 
28131068be7bSBryant G. Ly 	/* Check if device has been configured before */
281463d5be0fSMike Christie 	if (target_dev_configured(&udev->se_dev)) {
281533d065ccSZhu Lingshan 		ret = tcmu_send_emulate_write_cache(udev, val);
28161068be7bSBryant G. Ly 		if (ret) {
28171068be7bSBryant G. Ly 			pr_err("Unable to reconfigure device\n");
28181068be7bSBryant G. Ly 			return ret;
28191068be7bSBryant G. Ly 		}
28201068be7bSBryant G. Ly 	}
28212d76443eSMike Christie 
28222d76443eSMike Christie 	da->emulate_write_cache = val;
28239a8bb606SBryant G. Ly 	return count;
28249a8bb606SBryant G. Ly }
28259a8bb606SBryant G. Ly CONFIGFS_ATTR(tcmu_, emulate_write_cache);
28269a8bb606SBryant G. Ly 
282759526d7aSBodo Stroesser static ssize_t tcmu_tmr_notification_show(struct config_item *item, char *page)
282859526d7aSBodo Stroesser {
282959526d7aSBodo Stroesser 	struct se_dev_attrib *da = container_of(to_config_group(item),
283059526d7aSBodo Stroesser 					struct se_dev_attrib, da_group);
283159526d7aSBodo Stroesser 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
283259526d7aSBodo Stroesser 
283359526d7aSBodo Stroesser 	return snprintf(page, PAGE_SIZE, "%i\n",
283459526d7aSBodo Stroesser 			test_bit(TCMU_DEV_BIT_TMR_NOTIFY, &udev->flags));
283559526d7aSBodo Stroesser }
283659526d7aSBodo Stroesser 
283759526d7aSBodo Stroesser static ssize_t tcmu_tmr_notification_store(struct config_item *item,
283859526d7aSBodo Stroesser 					   const char *page, size_t count)
283959526d7aSBodo Stroesser {
284059526d7aSBodo Stroesser 	struct se_dev_attrib *da = container_of(to_config_group(item),
284159526d7aSBodo Stroesser 					struct se_dev_attrib, da_group);
284259526d7aSBodo Stroesser 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
284359526d7aSBodo Stroesser 	u8 val;
284459526d7aSBodo Stroesser 	int ret;
284559526d7aSBodo Stroesser 
284659526d7aSBodo Stroesser 	ret = kstrtou8(page, 0, &val);
284759526d7aSBodo Stroesser 	if (ret < 0)
284859526d7aSBodo Stroesser 		return ret;
284959526d7aSBodo Stroesser 	if (val > 1)
285059526d7aSBodo Stroesser 		return -EINVAL;
285159526d7aSBodo Stroesser 
285259526d7aSBodo Stroesser 	if (val)
285359526d7aSBodo Stroesser 		set_bit(TCMU_DEV_BIT_TMR_NOTIFY, &udev->flags);
285459526d7aSBodo Stroesser 	else
285559526d7aSBodo Stroesser 		clear_bit(TCMU_DEV_BIT_TMR_NOTIFY, &udev->flags);
285659526d7aSBodo Stroesser 	return count;
285759526d7aSBodo Stroesser }
285859526d7aSBodo Stroesser CONFIGFS_ATTR(tcmu_, tmr_notification);
285959526d7aSBodo Stroesser 
2860892782caSMike Christie static ssize_t tcmu_block_dev_show(struct config_item *item, char *page)
2861892782caSMike Christie {
2862892782caSMike Christie 	struct se_device *se_dev = container_of(to_config_group(item),
2863892782caSMike Christie 						struct se_device,
2864892782caSMike Christie 						dev_action_group);
2865892782caSMike Christie 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
2866892782caSMike Christie 
2867892782caSMike Christie 	if (test_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags))
2868892782caSMike Christie 		return snprintf(page, PAGE_SIZE, "%s\n", "blocked");
2869892782caSMike Christie 	else
2870892782caSMike Christie 		return snprintf(page, PAGE_SIZE, "%s\n", "unblocked");
2871892782caSMike Christie }
2872892782caSMike Christie 
2873892782caSMike Christie static ssize_t tcmu_block_dev_store(struct config_item *item, const char *page,
2874892782caSMike Christie 				    size_t count)
2875892782caSMike Christie {
2876892782caSMike Christie 	struct se_device *se_dev = container_of(to_config_group(item),
2877892782caSMike Christie 						struct se_device,
2878892782caSMike Christie 						dev_action_group);
2879892782caSMike Christie 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
2880892782caSMike Christie 	u8 val;
2881892782caSMike Christie 	int ret;
2882892782caSMike Christie 
2883a30b0473SMike Christie 	if (!target_dev_configured(&udev->se_dev)) {
2884a30b0473SMike Christie 		pr_err("Device is not configured.\n");
2885a30b0473SMike Christie 		return -EINVAL;
2886a30b0473SMike Christie 	}
2887a30b0473SMike Christie 
2888892782caSMike Christie 	ret = kstrtou8(page, 0, &val);
2889892782caSMike Christie 	if (ret < 0)
2890892782caSMike Christie 		return ret;
2891892782caSMike Christie 
2892892782caSMike Christie 	if (val > 1) {
2893892782caSMike Christie 		pr_err("Invalid block value %d\n", val);
2894892782caSMike Christie 		return -EINVAL;
2895892782caSMike Christie 	}
2896892782caSMike Christie 
2897892782caSMike Christie 	if (!val)
2898892782caSMike Christie 		tcmu_unblock_dev(udev);
2899892782caSMike Christie 	else
2900892782caSMike Christie 		tcmu_block_dev(udev);
2901892782caSMike Christie 	return count;
2902892782caSMike Christie }
2903892782caSMike Christie CONFIGFS_ATTR(tcmu_, block_dev);
2904892782caSMike Christie 
2905892782caSMike Christie static ssize_t tcmu_reset_ring_store(struct config_item *item, const char *page,
2906892782caSMike Christie 				     size_t count)
2907892782caSMike Christie {
2908892782caSMike Christie 	struct se_device *se_dev = container_of(to_config_group(item),
2909892782caSMike Christie 						struct se_device,
2910892782caSMike Christie 						dev_action_group);
2911892782caSMike Christie 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
2912892782caSMike Christie 	u8 val;
2913892782caSMike Christie 	int ret;
2914892782caSMike Christie 
2915a30b0473SMike Christie 	if (!target_dev_configured(&udev->se_dev)) {
2916a30b0473SMike Christie 		pr_err("Device is not configured.\n");
2917a30b0473SMike Christie 		return -EINVAL;
2918a30b0473SMike Christie 	}
2919a30b0473SMike Christie 
2920892782caSMike Christie 	ret = kstrtou8(page, 0, &val);
2921892782caSMike Christie 	if (ret < 0)
2922892782caSMike Christie 		return ret;
2923892782caSMike Christie 
2924892782caSMike Christie 	if (val != 1 && val != 2) {
2925892782caSMike Christie 		pr_err("Invalid reset ring value %d\n", val);
2926892782caSMike Christie 		return -EINVAL;
2927892782caSMike Christie 	}
2928892782caSMike Christie 
2929892782caSMike Christie 	tcmu_reset_ring(udev, val);
2930892782caSMike Christie 	return count;
2931892782caSMike Christie }
2932892782caSMike Christie CONFIGFS_ATTR_WO(tcmu_, reset_ring);
2933892782caSMike Christie 
29345821783bSColin Ian King static struct configfs_attribute *tcmu_attrib_attrs[] = {
2935801fc54dSBryant G. Ly 	&tcmu_attr_cmd_time_out,
29369103575aSMike Christie 	&tcmu_attr_qfull_time_out,
293780eb8761SMike Christie 	&tcmu_attr_max_data_area_mb,
293808976cb5SBodo Stroesser 	&tcmu_attr_data_pages_per_blk,
29392d76443eSMike Christie 	&tcmu_attr_dev_config,
2940801fc54dSBryant G. Ly 	&tcmu_attr_dev_size,
2941801fc54dSBryant G. Ly 	&tcmu_attr_emulate_write_cache,
294259526d7aSBodo Stroesser 	&tcmu_attr_tmr_notification,
2943b849b456SKenjiro Nakayama 	&tcmu_attr_nl_reply_supported,
2944801fc54dSBryant G. Ly 	NULL,
2945801fc54dSBryant G. Ly };
2946801fc54dSBryant G. Ly 
29477d7a7435SNicholas Bellinger static struct configfs_attribute **tcmu_attrs;
29487d7a7435SNicholas Bellinger 
2949892782caSMike Christie static struct configfs_attribute *tcmu_action_attrs[] = {
2950892782caSMike Christie 	&tcmu_attr_block_dev,
2951892782caSMike Christie 	&tcmu_attr_reset_ring,
2952892782caSMike Christie 	NULL,
2953892782caSMike Christie };
2954892782caSMike Christie 
29557d7a7435SNicholas Bellinger static struct target_backend_ops tcmu_ops = {
29567c9e7a6fSAndy Grover 	.name			= "user",
29577c9e7a6fSAndy Grover 	.owner			= THIS_MODULE,
295869088a04SBodo Stroesser 	.transport_flags_default = TRANSPORT_FLAG_PASSTHROUGH,
2959356ba2a8SBodo Stroesser 	.transport_flags_changeable = TRANSPORT_FLAG_PASSTHROUGH_PGR |
2960356ba2a8SBodo Stroesser 				      TRANSPORT_FLAG_PASSTHROUGH_ALUA,
29617c9e7a6fSAndy Grover 	.attach_hba		= tcmu_attach_hba,
29627c9e7a6fSAndy Grover 	.detach_hba		= tcmu_detach_hba,
29637c9e7a6fSAndy Grover 	.alloc_device		= tcmu_alloc_device,
29647c9e7a6fSAndy Grover 	.configure_device	= tcmu_configure_device,
296592634706SMike Christie 	.destroy_device		= tcmu_destroy_device,
29667c9e7a6fSAndy Grover 	.free_device		= tcmu_free_device,
29676888da81SMike Christie 	.unplug_device		= tcmu_unplug_device,
29686888da81SMike Christie 	.plug_device		= tcmu_plug_device,
29697c9e7a6fSAndy Grover 	.parse_cdb		= tcmu_parse_cdb,
2970bc2d214aSBodo Stroesser 	.tmr_notify		= tcmu_tmr_notify,
29717c9e7a6fSAndy Grover 	.set_configfs_dev_params = tcmu_set_configfs_dev_params,
29727c9e7a6fSAndy Grover 	.show_configfs_dev_params = tcmu_show_configfs_dev_params,
29737c9e7a6fSAndy Grover 	.get_device_type	= sbc_get_device_type,
29747c9e7a6fSAndy Grover 	.get_blocks		= tcmu_get_blocks,
2975892782caSMike Christie 	.tb_dev_action_attrs	= tcmu_action_attrs,
29767c9e7a6fSAndy Grover };
29777c9e7a6fSAndy Grover 
297889ec9cfdSMike Christie static void find_free_blocks(void)
2979b6df4b79SXiubo Li {
2980b6df4b79SXiubo Li 	struct tcmu_dev *udev;
2981b6df4b79SXiubo Li 	loff_t off;
2982f5ce815fSBodo Stroesser 	u32 pages_freed, total_pages_freed = 0;
2983f5ce815fSBodo Stroesser 	u32 start, end, block, total_blocks_freed = 0;
2984af1dd7ffSMike Christie 
29858b084d9dSBodo Stroesser 	if (atomic_read(&global_page_count) <= tcmu_global_max_pages)
2986af1dd7ffSMike Christie 		return;
2987b6df4b79SXiubo Li 
2988b6df4b79SXiubo Li 	mutex_lock(&root_udev_mutex);
2989b6df4b79SXiubo Li 	list_for_each_entry(udev, &root_udev, node) {
2990b6df4b79SXiubo Li 		mutex_lock(&udev->cmdr_lock);
2991b6df4b79SXiubo Li 
2992dc335a99SMike Christie 		if (!target_dev_configured(&udev->se_dev)) {
2993dc335a99SMike Christie 			mutex_unlock(&udev->cmdr_lock);
2994dc335a99SMike Christie 			continue;
2995dc335a99SMike Christie 		}
2996dc335a99SMike Christie 
2997b6df4b79SXiubo Li 		/* Try to complete the finished commands first */
2998bc2d214aSBodo Stroesser 		if (tcmu_handle_completions(udev))
2999bc2d214aSBodo Stroesser 			run_qfull_queue(udev, false);
3000b6df4b79SXiubo Li 
3001af1dd7ffSMike Christie 		/* Skip the udevs in idle */
3002af1dd7ffSMike Christie 		if (!udev->dbi_thresh) {
3003b6df4b79SXiubo Li 			mutex_unlock(&udev->cmdr_lock);
3004b6df4b79SXiubo Li 			continue;
3005b6df4b79SXiubo Li 		}
3006b6df4b79SXiubo Li 
3007b6df4b79SXiubo Li 		end = udev->dbi_max + 1;
3008b6df4b79SXiubo Li 		block = find_last_bit(udev->data_bitmap, end);
3009b6df4b79SXiubo Li 		if (block == udev->dbi_max) {
3010b6df4b79SXiubo Li 			/*
3011af1dd7ffSMike Christie 			 * The last bit is dbi_max, so it is not possible
3012af1dd7ffSMike Christie 			 * reclaim any blocks.
3013b6df4b79SXiubo Li 			 */
3014b6df4b79SXiubo Li 			mutex_unlock(&udev->cmdr_lock);
3015b6df4b79SXiubo Li 			continue;
3016b6df4b79SXiubo Li 		} else if (block == end) {
3017b6df4b79SXiubo Li 			/* The current udev will goto idle state */
3018b6df4b79SXiubo Li 			udev->dbi_thresh = start = 0;
3019b6df4b79SXiubo Li 			udev->dbi_max = 0;
3020b6df4b79SXiubo Li 		} else {
3021b6df4b79SXiubo Li 			udev->dbi_thresh = start = block + 1;
3022b6df4b79SXiubo Li 			udev->dbi_max = block;
3023b6df4b79SXiubo Li 		}
3024b6df4b79SXiubo Li 
3025b6df4b79SXiubo Li 		/* Here will truncate the data area from off */
3026e719afdcSBodo Stroesser 		off = udev->data_off + (loff_t)start * udev->data_blk_size;
3027b6df4b79SXiubo Li 		unmap_mapping_range(udev->inode->i_mapping, off, 0, 1);
3028b6df4b79SXiubo Li 
3029b6df4b79SXiubo Li 		/* Release the block pages */
3030e719afdcSBodo Stroesser 		pages_freed = tcmu_blocks_release(udev, start, end - 1);
3031b6df4b79SXiubo Li 		mutex_unlock(&udev->cmdr_lock);
3032af1dd7ffSMike Christie 
3033f5ce815fSBodo Stroesser 		total_pages_freed += pages_freed;
3034f5ce815fSBodo Stroesser 		total_blocks_freed += end - start;
3035f5ce815fSBodo Stroesser 		pr_debug("Freed %u pages (total %u) from %u blocks (total %u) from %s.\n",
3036f5ce815fSBodo Stroesser 			 pages_freed, total_pages_freed, end - start,
3037f5ce815fSBodo Stroesser 			 total_blocks_freed, udev->name);
3038b6df4b79SXiubo Li 	}
303989ec9cfdSMike Christie 	mutex_unlock(&root_udev_mutex);
304089ec9cfdSMike Christie 
30418b084d9dSBodo Stroesser 	if (atomic_read(&global_page_count) > tcmu_global_max_pages)
3042af1dd7ffSMike Christie 		schedule_delayed_work(&tcmu_unmap_work, msecs_to_jiffies(5000));
3043b6df4b79SXiubo Li }
3044b6df4b79SXiubo Li 
3045488ebe4cSMike Christie static void check_timedout_devices(void)
3046488ebe4cSMike Christie {
3047488ebe4cSMike Christie 	struct tcmu_dev *udev, *tmp_dev;
304861fb2482SBodo Stroesser 	struct tcmu_cmd *cmd, *tmp_cmd;
3049488ebe4cSMike Christie 	LIST_HEAD(devs);
3050488ebe4cSMike Christie 
3051488ebe4cSMike Christie 	spin_lock_bh(&timed_out_udevs_lock);
3052488ebe4cSMike Christie 	list_splice_init(&timed_out_udevs, &devs);
3053488ebe4cSMike Christie 
3054488ebe4cSMike Christie 	list_for_each_entry_safe(udev, tmp_dev, &devs, timedout_entry) {
3055488ebe4cSMike Christie 		list_del_init(&udev->timedout_entry);
3056488ebe4cSMike Christie 		spin_unlock_bh(&timed_out_udevs_lock);
3057488ebe4cSMike Christie 
30586fddcb77SMike Christie 		mutex_lock(&udev->cmdr_lock);
3059a94a2572SXiubo Li 
306061fb2482SBodo Stroesser 		/*
306161fb2482SBodo Stroesser 		 * If cmd_time_out is disabled but qfull is set deadline
306261fb2482SBodo Stroesser 		 * will only reflect the qfull timeout. Ignore it.
306361fb2482SBodo Stroesser 		 */
306461fb2482SBodo Stroesser 		if (udev->cmd_time_out) {
306561fb2482SBodo Stroesser 			list_for_each_entry_safe(cmd, tmp_cmd,
306661fb2482SBodo Stroesser 						 &udev->inflight_queue,
306761fb2482SBodo Stroesser 						 queue_entry) {
306861fb2482SBodo Stroesser 				tcmu_check_expired_ring_cmd(cmd);
306961fb2482SBodo Stroesser 			}
307061fb2482SBodo Stroesser 			tcmu_set_next_deadline(&udev->inflight_queue,
307161fb2482SBodo Stroesser 					       &udev->cmd_timer);
307261fb2482SBodo Stroesser 		}
307361fb2482SBodo Stroesser 		list_for_each_entry_safe(cmd, tmp_cmd, &udev->qfull_queue,
307461fb2482SBodo Stroesser 					 queue_entry) {
307561fb2482SBodo Stroesser 			tcmu_check_expired_queue_cmd(cmd);
307661fb2482SBodo Stroesser 		}
3077a94a2572SXiubo Li 		tcmu_set_next_deadline(&udev->qfull_queue, &udev->qfull_timer);
3078a94a2572SXiubo Li 
30796fddcb77SMike Christie 		mutex_unlock(&udev->cmdr_lock);
3080488ebe4cSMike Christie 
3081488ebe4cSMike Christie 		spin_lock_bh(&timed_out_udevs_lock);
3082488ebe4cSMike Christie 	}
3083488ebe4cSMike Christie 
3084488ebe4cSMike Christie 	spin_unlock_bh(&timed_out_udevs_lock);
3085488ebe4cSMike Christie }
3086488ebe4cSMike Christie 
30879972cebbSMike Christie static void tcmu_unmap_work_fn(struct work_struct *work)
308889ec9cfdSMike Christie {
3089488ebe4cSMike Christie 	check_timedout_devices();
309089ec9cfdSMike Christie 	find_free_blocks();
309189ec9cfdSMike Christie }
309289ec9cfdSMike Christie 
30937c9e7a6fSAndy Grover static int __init tcmu_module_init(void)
30947c9e7a6fSAndy Grover {
3095801fc54dSBryant G. Ly 	int ret, i, k, len = 0;
30967c9e7a6fSAndy Grover 
30977c9e7a6fSAndy Grover 	BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0);
30987c9e7a6fSAndy Grover 
3099af1dd7ffSMike Christie 	INIT_DELAYED_WORK(&tcmu_unmap_work, tcmu_unmap_work_fn);
31009972cebbSMike Christie 
31017c9e7a6fSAndy Grover 	tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache",
31027c9e7a6fSAndy Grover 				sizeof(struct tcmu_cmd),
31037c9e7a6fSAndy Grover 				__alignof__(struct tcmu_cmd),
31047c9e7a6fSAndy Grover 				0, NULL);
31057c9e7a6fSAndy Grover 	if (!tcmu_cmd_cache)
31067c9e7a6fSAndy Grover 		return -ENOMEM;
31077c9e7a6fSAndy Grover 
31087c9e7a6fSAndy Grover 	tcmu_root_device = root_device_register("tcm_user");
31097c9e7a6fSAndy Grover 	if (IS_ERR(tcmu_root_device)) {
31107c9e7a6fSAndy Grover 		ret = PTR_ERR(tcmu_root_device);
31117c9e7a6fSAndy Grover 		goto out_free_cache;
31127c9e7a6fSAndy Grover 	}
31137c9e7a6fSAndy Grover 
31147c9e7a6fSAndy Grover 	ret = genl_register_family(&tcmu_genl_family);
31157c9e7a6fSAndy Grover 	if (ret < 0) {
31167c9e7a6fSAndy Grover 		goto out_unreg_device;
31177c9e7a6fSAndy Grover 	}
31187c9e7a6fSAndy Grover 
31194703b625SBodo Stroesser 	for (i = 0; passthrough_attrib_attrs[i] != NULL; i++)
31207d7a7435SNicholas Bellinger 		len += sizeof(struct configfs_attribute *);
31214703b625SBodo Stroesser 	for (i = 0; passthrough_pr_attrib_attrs[i] != NULL; i++)
3122801fc54dSBryant G. Ly 		len += sizeof(struct configfs_attribute *);
31234703b625SBodo Stroesser 	for (i = 0; tcmu_attrib_attrs[i] != NULL; i++)
31244703b625SBodo Stroesser 		len += sizeof(struct configfs_attribute *);
3125801fc54dSBryant G. Ly 	len += sizeof(struct configfs_attribute *);
31267d7a7435SNicholas Bellinger 
31277d7a7435SNicholas Bellinger 	tcmu_attrs = kzalloc(len, GFP_KERNEL);
31287d7a7435SNicholas Bellinger 	if (!tcmu_attrs) {
31297d7a7435SNicholas Bellinger 		ret = -ENOMEM;
31307d7a7435SNicholas Bellinger 		goto out_unreg_genl;
31317d7a7435SNicholas Bellinger 	}
31327d7a7435SNicholas Bellinger 
31334703b625SBodo Stroesser 	for (i = 0; passthrough_attrib_attrs[i] != NULL; i++)
31347d7a7435SNicholas Bellinger 		tcmu_attrs[i] = passthrough_attrib_attrs[i];
31354703b625SBodo Stroesser 	for (k = 0; passthrough_pr_attrib_attrs[k] != NULL; k++)
31364703b625SBodo Stroesser 		tcmu_attrs[i++] = passthrough_pr_attrib_attrs[k];
31374703b625SBodo Stroesser 	for (k = 0; tcmu_attrib_attrs[k] != NULL; k++)
31384703b625SBodo Stroesser 		tcmu_attrs[i++] = tcmu_attrib_attrs[k];
31397d7a7435SNicholas Bellinger 	tcmu_ops.tb_dev_attrib_attrs = tcmu_attrs;
31407d7a7435SNicholas Bellinger 
31410a06d430SChristoph Hellwig 	ret = transport_backend_register(&tcmu_ops);
31427c9e7a6fSAndy Grover 	if (ret)
31437d7a7435SNicholas Bellinger 		goto out_attrs;
31447c9e7a6fSAndy Grover 
31457c9e7a6fSAndy Grover 	return 0;
31467c9e7a6fSAndy Grover 
31477d7a7435SNicholas Bellinger out_attrs:
31487d7a7435SNicholas Bellinger 	kfree(tcmu_attrs);
31497c9e7a6fSAndy Grover out_unreg_genl:
31507c9e7a6fSAndy Grover 	genl_unregister_family(&tcmu_genl_family);
31517c9e7a6fSAndy Grover out_unreg_device:
31527c9e7a6fSAndy Grover 	root_device_unregister(tcmu_root_device);
31537c9e7a6fSAndy Grover out_free_cache:
31547c9e7a6fSAndy Grover 	kmem_cache_destroy(tcmu_cmd_cache);
31557c9e7a6fSAndy Grover 
31567c9e7a6fSAndy Grover 	return ret;
31577c9e7a6fSAndy Grover }
31587c9e7a6fSAndy Grover 
31597c9e7a6fSAndy Grover static void __exit tcmu_module_exit(void)
31607c9e7a6fSAndy Grover {
3161af1dd7ffSMike Christie 	cancel_delayed_work_sync(&tcmu_unmap_work);
31620a06d430SChristoph Hellwig 	target_backend_unregister(&tcmu_ops);
31637d7a7435SNicholas Bellinger 	kfree(tcmu_attrs);
31647c9e7a6fSAndy Grover 	genl_unregister_family(&tcmu_genl_family);
31657c9e7a6fSAndy Grover 	root_device_unregister(tcmu_root_device);
31667c9e7a6fSAndy Grover 	kmem_cache_destroy(tcmu_cmd_cache);
31677c9e7a6fSAndy Grover }
31687c9e7a6fSAndy Grover 
31697c9e7a6fSAndy Grover MODULE_DESCRIPTION("TCM USER subsystem plugin");
31707c9e7a6fSAndy Grover MODULE_AUTHOR("Shaohua Li <shli@kernel.org>");
31717c9e7a6fSAndy Grover MODULE_AUTHOR("Andy Grover <agrover@redhat.com>");
31727c9e7a6fSAndy Grover MODULE_LICENSE("GPL");
31737c9e7a6fSAndy Grover 
31747c9e7a6fSAndy Grover module_init(tcmu_module_init);
31757c9e7a6fSAndy Grover module_exit(tcmu_module_exit);
3176