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>
23bb9b9eb0SXiaoguang Wang #include <linux/pagemap.h>
247c9e7a6fSAndy Grover #include <net/genetlink.h>
25ba929992SBart Van Assche #include <scsi/scsi_common.h>
26ba929992SBart Van Assche #include <scsi/scsi_proto.h>
277c9e7a6fSAndy Grover #include <target/target_core_base.h>
287c9e7a6fSAndy Grover #include <target/target_core_fabric.h>
297c9e7a6fSAndy Grover #include <target/target_core_backend.h>
30e9f720d6SNicholas Bellinger 
317c9e7a6fSAndy Grover #include <linux/target_core_user.h>
327c9e7a6fSAndy Grover 
33572ccdabSRandy Dunlap /**
34572ccdabSRandy Dunlap  * DOC: Userspace I/O
35572ccdabSRandy Dunlap  * Userspace I/O
36572ccdabSRandy Dunlap  * -------------
37572ccdabSRandy Dunlap  *
387c9e7a6fSAndy Grover  * Define a shared-memory interface for LIO to pass SCSI commands and
397c9e7a6fSAndy Grover  * data to userspace for processing. This is to allow backends that
407c9e7a6fSAndy Grover  * are too complex for in-kernel support to be possible.
417c9e7a6fSAndy Grover  *
427c9e7a6fSAndy Grover  * It uses the UIO framework to do a lot of the device-creation and
437c9e7a6fSAndy Grover  * introspection work for us.
447c9e7a6fSAndy Grover  *
457c9e7a6fSAndy Grover  * See the .h file for how the ring is laid out. Note that while the
467c9e7a6fSAndy Grover  * command ring is defined, the particulars of the data area are
477c9e7a6fSAndy Grover  * not. Offset values in the command entry point to other locations
48572ccdabSRandy Dunlap  * internal to the mmap-ed area. There is separate space outside the
497c9e7a6fSAndy Grover  * command ring for data buffers. This leaves maximum flexibility for
507c9e7a6fSAndy Grover  * moving buffer allocations, or even page flipping or other
517c9e7a6fSAndy Grover  * allocation techniques, without altering the command ring layout.
527c9e7a6fSAndy Grover  *
537c9e7a6fSAndy Grover  * SECURITY:
547c9e7a6fSAndy Grover  * The user process must be assumed to be malicious. There's no way to
557c9e7a6fSAndy Grover  * prevent it breaking the command ring protocol if it wants, but in
567c9e7a6fSAndy Grover  * order to prevent other issues we must only ever read *data* from
577c9e7a6fSAndy Grover  * the shared memory area, not offsets or sizes. This applies to
587c9e7a6fSAndy Grover  * command ring entries as well as the mailbox. Extra code needed for
597c9e7a6fSAndy Grover  * this may have a 'UAM' comment.
607c9e7a6fSAndy Grover  */
617c9e7a6fSAndy Grover 
627c9e7a6fSAndy Grover #define TCMU_TIME_OUT (30 * MSEC_PER_SEC)
637c9e7a6fSAndy Grover 
64ecddbb7eSBodo Stroesser /* For mailbox plus cmd ring, the size is fixed 8MB */
65c7ede4f0SGuixin Liu #define MB_CMDR_SIZE_DEF (8 * 1024 * 1024)
66ecddbb7eSBodo Stroesser /* Offset of cmd ring is size of mailbox */
67c7ede4f0SGuixin Liu #define CMDR_OFF ((__u32)sizeof(struct tcmu_mailbox))
68c7ede4f0SGuixin Liu #define CMDR_SIZE_DEF (MB_CMDR_SIZE_DEF - CMDR_OFF)
6926418649SSheng Yang 
70b6df4b79SXiubo Li /*
71f5ce815fSBodo Stroesser  * For data area, the default block size is PAGE_SIZE and
72f5ce815fSBodo Stroesser  * the default total size is 256K * PAGE_SIZE.
73b6df4b79SXiubo Li  */
74e719afdcSBodo Stroesser #define DATA_PAGES_PER_BLK_DEF 1
75f5ce815fSBodo Stroesser #define DATA_AREA_PAGES_DEF (256 * 1024)
767c9e7a6fSAndy Grover 
77f5ce815fSBodo Stroesser #define TCMU_MBS_TO_PAGES(_mbs) ((size_t)_mbs << (20 - PAGE_SHIFT))
788b084d9dSBodo Stroesser #define TCMU_PAGES_TO_MBS(_pages) (_pages >> (20 - PAGE_SHIFT))
7980eb8761SMike Christie 
80af1dd7ffSMike Christie /*
81af1dd7ffSMike Christie  * Default number of global data blocks(512K * PAGE_SIZE)
82af1dd7ffSMike Christie  * when the unmap thread will be started.
83af1dd7ffSMike Christie  */
848b084d9dSBodo Stroesser #define TCMU_GLOBAL_MAX_PAGES_DEF (512 * 1024)
85b6df4b79SXiubo Li 
86b3af66e2SMike Christie static u8 tcmu_kern_cmd_reply_supported;
87bdaeedc1SMike Christie static u8 tcmu_netlink_blocked;
88b3af66e2SMike Christie 
897c9e7a6fSAndy Grover static struct device *tcmu_root_device;
907c9e7a6fSAndy Grover 
917c9e7a6fSAndy Grover struct tcmu_hba {
927c9e7a6fSAndy Grover 	u32 host_id;
937c9e7a6fSAndy Grover };
947c9e7a6fSAndy Grover 
957c9e7a6fSAndy Grover #define TCMU_CONFIG_LEN 256
967c9e7a6fSAndy Grover 
973228691fSMike Christie static DEFINE_MUTEX(tcmu_nl_cmd_mutex);
983228691fSMike Christie static LIST_HEAD(tcmu_nl_cmd_list);
993228691fSMike Christie 
1003228691fSMike Christie struct tcmu_dev;
1013228691fSMike Christie 
102b3af66e2SMike Christie struct tcmu_nl_cmd {
103b3af66e2SMike Christie 	/* wake up thread waiting for reply */
104b3af66e2SMike Christie 	struct completion complete;
1053228691fSMike Christie 	struct list_head nl_list;
1063228691fSMike Christie 	struct tcmu_dev *udev;
107b3af66e2SMike Christie 	int cmd;
108b3af66e2SMike Christie 	int status;
109b3af66e2SMike Christie };
110b3af66e2SMike Christie 
1117c9e7a6fSAndy Grover struct tcmu_dev {
112b6df4b79SXiubo Li 	struct list_head node;
113f3cdbe39SMike Christie 	struct kref kref;
114af1dd7ffSMike Christie 
1157c9e7a6fSAndy Grover 	struct se_device se_dev;
1166888da81SMike Christie 	struct se_dev_plug se_plug;
1177c9e7a6fSAndy Grover 
1187c9e7a6fSAndy Grover 	char *name;
1197c9e7a6fSAndy Grover 	struct se_hba *hba;
1207c9e7a6fSAndy Grover 
1217c9e7a6fSAndy Grover #define TCMU_DEV_BIT_OPEN 0
1227c9e7a6fSAndy Grover #define TCMU_DEV_BIT_BROKEN 1
123892782caSMike Christie #define TCMU_DEV_BIT_BLOCKED 2
12459526d7aSBodo Stroesser #define TCMU_DEV_BIT_TMR_NOTIFY 3
1253ac0fcb4SBodo Stroesser #define TCMU_DEV_BIT_PLUGGED 4
1267c9e7a6fSAndy Grover 	unsigned long flags;
1277c9e7a6fSAndy Grover 
1287c9e7a6fSAndy Grover 	struct uio_info uio_info;
1297c9e7a6fSAndy Grover 
130b6df4b79SXiubo Li 	struct inode *inode;
131b6df4b79SXiubo Li 
1320e0d7526SMike Christie 	uint64_t dev_size;
133ecddbb7eSBodo Stroesser 
134ecddbb7eSBodo Stroesser 	struct tcmu_mailbox *mb_addr;
135ecddbb7eSBodo Stroesser 	void *cmdr;
1367c9e7a6fSAndy Grover 	u32 cmdr_size;
1377c9e7a6fSAndy Grover 	u32 cmdr_last_cleaned;
1383d9b9555SAndy Grover 	/* Offset of data area from start of mb */
13926418649SSheng Yang 	/* Must add data_off and mb_addr to get the address */
1407c9e7a6fSAndy Grover 	size_t data_off;
141f5ce815fSBodo Stroesser 	int data_area_mb;
14280eb8761SMike Christie 	uint32_t max_blocks;
143ecddbb7eSBodo Stroesser 	size_t mmap_pages;
14426418649SSheng Yang 
145b6df4b79SXiubo Li 	struct mutex cmdr_lock;
146a94a2572SXiubo Li 	struct list_head qfull_queue;
147bc2d214aSBodo Stroesser 	struct list_head tmr_queue;
1487c9e7a6fSAndy Grover 
149141685a3SXiubo Li 	uint32_t dbi_max;
150b6df4b79SXiubo Li 	uint32_t dbi_thresh;
15180eb8761SMike Christie 	unsigned long *data_bitmap;
1528b084d9dSBodo Stroesser 	struct xarray data_pages;
153e719afdcSBodo Stroesser 	uint32_t data_pages_per_blk;
154e719afdcSBodo Stroesser 	uint32_t data_blk_size;
155141685a3SXiubo Li 
156d3cbb743SBodo Stroesser 	struct xarray commands;
1577c9e7a6fSAndy Grover 
1589103575aSMike Christie 	struct timer_list cmd_timer;
159af980e46SMike Christie 	unsigned int cmd_time_out;
160a94a2572SXiubo Li 	struct list_head inflight_queue;
1619103575aSMike Christie 
1629103575aSMike Christie 	struct timer_list qfull_timer;
1639103575aSMike Christie 	int qfull_time_out;
1649103575aSMike Christie 
165488ebe4cSMike Christie 	struct list_head timedout_entry;
1667c9e7a6fSAndy Grover 
167b3af66e2SMike Christie 	struct tcmu_nl_cmd curr_nl_cmd;
168b3af66e2SMike Christie 
1697c9e7a6fSAndy Grover 	char dev_config[TCMU_CONFIG_LEN];
170b849b456SKenjiro Nakayama 
171b849b456SKenjiro Nakayama 	int nl_reply_supported;
1727c9e7a6fSAndy Grover };
1737c9e7a6fSAndy Grover 
1747c9e7a6fSAndy Grover #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev)
1757c9e7a6fSAndy Grover 
1767c9e7a6fSAndy Grover struct tcmu_cmd {
1777c9e7a6fSAndy Grover 	struct se_cmd *se_cmd;
1787c9e7a6fSAndy Grover 	struct tcmu_dev *tcmu_dev;
179a94a2572SXiubo Li 	struct list_head queue_entry;
1807c9e7a6fSAndy Grover 
1817c9e7a6fSAndy Grover 	uint16_t cmd_id;
1827c9e7a6fSAndy Grover 
18326418649SSheng Yang 	/* Can't use se_cmd when cleaning up expired cmds, because if
1847c9e7a6fSAndy Grover 	   cmd has been completed then accessing se_cmd is off limits */
185141685a3SXiubo Li 	uint32_t dbi_cnt;
18652ef2743SBodo Stroesser 	uint32_t dbi_bidi_cnt;
187141685a3SXiubo Li 	uint32_t dbi_cur;
188141685a3SXiubo Li 	uint32_t *dbi;
1897c9e7a6fSAndy Grover 
1903c9a7c58SBodo Stroesser 	uint32_t data_len_bidi;
1913c9a7c58SBodo Stroesser 
1927c9e7a6fSAndy Grover 	unsigned long deadline;
1937c9e7a6fSAndy Grover 
1947c9e7a6fSAndy Grover #define TCMU_CMD_BIT_EXPIRED 0
195018c1491SBodo Stroesser #define TCMU_CMD_BIT_KEEP_BUF 1
1967c9e7a6fSAndy Grover 	unsigned long flags;
1977c9e7a6fSAndy Grover };
198bc2d214aSBodo Stroesser 
199bc2d214aSBodo Stroesser struct tcmu_tmr {
200bc2d214aSBodo Stroesser 	struct list_head queue_entry;
201bc2d214aSBodo Stroesser 
202bc2d214aSBodo Stroesser 	uint8_t tmr_type;
203bc2d214aSBodo Stroesser 	uint32_t tmr_cmd_cnt;
2048fdaabe1SGustavo A. R. Silva 	int16_t tmr_cmd_ids[];
205bc2d214aSBodo Stroesser };
206bc2d214aSBodo Stroesser 
207af1dd7ffSMike Christie /*
208af1dd7ffSMike Christie  * To avoid dead lock the mutex lock order should always be:
209af1dd7ffSMike Christie  *
210af1dd7ffSMike Christie  * mutex_lock(&root_udev_mutex);
211af1dd7ffSMike Christie  * ...
212af1dd7ffSMike Christie  * mutex_lock(&tcmu_dev->cmdr_lock);
213af1dd7ffSMike Christie  * mutex_unlock(&tcmu_dev->cmdr_lock);
214af1dd7ffSMike Christie  * ...
215af1dd7ffSMike Christie  * mutex_unlock(&root_udev_mutex);
216af1dd7ffSMike Christie  */
217b6df4b79SXiubo Li static DEFINE_MUTEX(root_udev_mutex);
218b6df4b79SXiubo Li static LIST_HEAD(root_udev);
219b6df4b79SXiubo Li 
220488ebe4cSMike Christie static DEFINE_SPINLOCK(timed_out_udevs_lock);
221488ebe4cSMike Christie static LIST_HEAD(timed_out_udevs);
222488ebe4cSMike Christie 
22380eb8761SMike Christie static struct kmem_cache *tcmu_cmd_cache;
22480eb8761SMike Christie 
2258b084d9dSBodo Stroesser static atomic_t global_page_count = ATOMIC_INIT(0);
226af1dd7ffSMike Christie static struct delayed_work tcmu_unmap_work;
2278b084d9dSBodo Stroesser static int tcmu_global_max_pages = TCMU_GLOBAL_MAX_PAGES_DEF;
228b6df4b79SXiubo Li 
tcmu_set_global_max_data_area(const char * str,const struct kernel_param * kp)22980eb8761SMike Christie static int tcmu_set_global_max_data_area(const char *str,
23080eb8761SMike Christie 					 const struct kernel_param *kp)
23180eb8761SMike Christie {
23280eb8761SMike Christie 	int ret, max_area_mb;
23380eb8761SMike Christie 
23480eb8761SMike Christie 	ret = kstrtoint(str, 10, &max_area_mb);
23580eb8761SMike Christie 	if (ret)
23680eb8761SMike Christie 		return -EINVAL;
23780eb8761SMike Christie 
23880eb8761SMike Christie 	if (max_area_mb <= 0) {
23980eb8761SMike Christie 		pr_err("global_max_data_area must be larger than 0.\n");
24080eb8761SMike Christie 		return -EINVAL;
24180eb8761SMike Christie 	}
24280eb8761SMike Christie 
2438b084d9dSBodo Stroesser 	tcmu_global_max_pages = TCMU_MBS_TO_PAGES(max_area_mb);
2448b084d9dSBodo Stroesser 	if (atomic_read(&global_page_count) > tcmu_global_max_pages)
24580eb8761SMike Christie 		schedule_delayed_work(&tcmu_unmap_work, 0);
24680eb8761SMike Christie 	else
24780eb8761SMike Christie 		cancel_delayed_work_sync(&tcmu_unmap_work);
24880eb8761SMike Christie 
24980eb8761SMike Christie 	return 0;
25080eb8761SMike Christie }
25180eb8761SMike Christie 
tcmu_get_global_max_data_area(char * buffer,const struct kernel_param * kp)25280eb8761SMike Christie static int tcmu_get_global_max_data_area(char *buffer,
25380eb8761SMike Christie 					 const struct kernel_param *kp)
25480eb8761SMike Christie {
2558b084d9dSBodo Stroesser 	return sprintf(buffer, "%d\n", TCMU_PAGES_TO_MBS(tcmu_global_max_pages));
25680eb8761SMike Christie }
25780eb8761SMike Christie 
25880eb8761SMike Christie static const struct kernel_param_ops tcmu_global_max_data_area_op = {
25980eb8761SMike Christie 	.set = tcmu_set_global_max_data_area,
26080eb8761SMike Christie 	.get = tcmu_get_global_max_data_area,
26180eb8761SMike Christie };
26280eb8761SMike Christie 
26380eb8761SMike Christie module_param_cb(global_max_data_area_mb, &tcmu_global_max_data_area_op, NULL,
26480eb8761SMike Christie 		S_IWUSR | S_IRUGO);
26580eb8761SMike Christie MODULE_PARM_DESC(global_max_data_area_mb,
26680eb8761SMike Christie 		 "Max MBs allowed to be allocated to all the tcmu device's "
26780eb8761SMike Christie 		 "data areas.");
2687c9e7a6fSAndy Grover 
tcmu_get_block_netlink(char * buffer,const struct kernel_param * kp)269bdaeedc1SMike Christie static int tcmu_get_block_netlink(char *buffer,
270bdaeedc1SMike Christie 				  const struct kernel_param *kp)
271bdaeedc1SMike Christie {
272bdaeedc1SMike Christie 	return sprintf(buffer, "%s\n", tcmu_netlink_blocked ?
273bdaeedc1SMike Christie 		       "blocked" : "unblocked");
274bdaeedc1SMike Christie }
275bdaeedc1SMike Christie 
tcmu_set_block_netlink(const char * str,const struct kernel_param * kp)276bdaeedc1SMike Christie static int tcmu_set_block_netlink(const char *str,
277bdaeedc1SMike Christie 				  const struct kernel_param *kp)
278bdaeedc1SMike Christie {
279bdaeedc1SMike Christie 	int ret;
280bdaeedc1SMike Christie 	u8 val;
281bdaeedc1SMike Christie 
282bdaeedc1SMike Christie 	ret = kstrtou8(str, 0, &val);
283bdaeedc1SMike Christie 	if (ret < 0)
284bdaeedc1SMike Christie 		return ret;
285bdaeedc1SMike Christie 
286bdaeedc1SMike Christie 	if (val > 1) {
287bdaeedc1SMike Christie 		pr_err("Invalid block netlink value %u\n", val);
288bdaeedc1SMike Christie 		return -EINVAL;
289bdaeedc1SMike Christie 	}
290bdaeedc1SMike Christie 
291bdaeedc1SMike Christie 	tcmu_netlink_blocked = val;
292bdaeedc1SMike Christie 	return 0;
293bdaeedc1SMike Christie }
294bdaeedc1SMike Christie 
295bdaeedc1SMike Christie static const struct kernel_param_ops tcmu_block_netlink_op = {
296bdaeedc1SMike Christie 	.set = tcmu_set_block_netlink,
297bdaeedc1SMike Christie 	.get = tcmu_get_block_netlink,
298bdaeedc1SMike Christie };
299bdaeedc1SMike Christie 
300bdaeedc1SMike Christie module_param_cb(block_netlink, &tcmu_block_netlink_op, NULL, S_IWUSR | S_IRUGO);
301bdaeedc1SMike Christie MODULE_PARM_DESC(block_netlink, "Block new netlink commands.");
302bdaeedc1SMike Christie 
tcmu_fail_netlink_cmd(struct tcmu_nl_cmd * nl_cmd)303bdaeedc1SMike Christie static int tcmu_fail_netlink_cmd(struct tcmu_nl_cmd *nl_cmd)
304bdaeedc1SMike Christie {
305bdaeedc1SMike Christie 	struct tcmu_dev *udev = nl_cmd->udev;
306bdaeedc1SMike Christie 
307bdaeedc1SMike Christie 	if (!tcmu_netlink_blocked) {
308bdaeedc1SMike Christie 		pr_err("Could not reset device's netlink interface. Netlink is not blocked.\n");
309bdaeedc1SMike Christie 		return -EBUSY;
310bdaeedc1SMike Christie 	}
311bdaeedc1SMike Christie 
312bdaeedc1SMike Christie 	if (nl_cmd->cmd != TCMU_CMD_UNSPEC) {
313bdaeedc1SMike Christie 		pr_debug("Aborting nl cmd %d on %s\n", nl_cmd->cmd, udev->name);
314bdaeedc1SMike Christie 		nl_cmd->status = -EINTR;
315bdaeedc1SMike Christie 		list_del(&nl_cmd->nl_list);
316bdaeedc1SMike Christie 		complete(&nl_cmd->complete);
317bdaeedc1SMike Christie 	}
318bdaeedc1SMike Christie 	return 0;
319bdaeedc1SMike Christie }
320bdaeedc1SMike Christie 
tcmu_set_reset_netlink(const char * str,const struct kernel_param * kp)321bdaeedc1SMike Christie static int tcmu_set_reset_netlink(const char *str,
322bdaeedc1SMike Christie 				  const struct kernel_param *kp)
323bdaeedc1SMike Christie {
324bdaeedc1SMike Christie 	struct tcmu_nl_cmd *nl_cmd, *tmp_cmd;
325bdaeedc1SMike Christie 	int ret;
326bdaeedc1SMike Christie 	u8 val;
327bdaeedc1SMike Christie 
328bdaeedc1SMike Christie 	ret = kstrtou8(str, 0, &val);
329bdaeedc1SMike Christie 	if (ret < 0)
330bdaeedc1SMike Christie 		return ret;
331bdaeedc1SMike Christie 
332bdaeedc1SMike Christie 	if (val != 1) {
333bdaeedc1SMike Christie 		pr_err("Invalid reset netlink value %u\n", val);
334bdaeedc1SMike Christie 		return -EINVAL;
335bdaeedc1SMike Christie 	}
336bdaeedc1SMike Christie 
337bdaeedc1SMike Christie 	mutex_lock(&tcmu_nl_cmd_mutex);
338bdaeedc1SMike Christie 	list_for_each_entry_safe(nl_cmd, tmp_cmd, &tcmu_nl_cmd_list, nl_list) {
339bdaeedc1SMike Christie 		ret = tcmu_fail_netlink_cmd(nl_cmd);
340bdaeedc1SMike Christie 		if (ret)
341bdaeedc1SMike Christie 			break;
342bdaeedc1SMike Christie 	}
343bdaeedc1SMike Christie 	mutex_unlock(&tcmu_nl_cmd_mutex);
344bdaeedc1SMike Christie 
345bdaeedc1SMike Christie 	return ret;
346bdaeedc1SMike Christie }
347bdaeedc1SMike Christie 
348bdaeedc1SMike Christie static const struct kernel_param_ops tcmu_reset_netlink_op = {
349bdaeedc1SMike Christie 	.set = tcmu_set_reset_netlink,
350bdaeedc1SMike Christie };
351bdaeedc1SMike Christie 
352bdaeedc1SMike Christie module_param_cb(reset_netlink, &tcmu_reset_netlink_op, NULL, S_IWUSR);
353bdaeedc1SMike Christie MODULE_PARM_DESC(reset_netlink, "Reset netlink commands.");
354bdaeedc1SMike Christie 
3557c9e7a6fSAndy Grover /* multicast group */
3567c9e7a6fSAndy Grover enum tcmu_multicast_groups {
3577c9e7a6fSAndy Grover 	TCMU_MCGRP_CONFIG,
3587c9e7a6fSAndy Grover };
3597c9e7a6fSAndy Grover 
3607c9e7a6fSAndy Grover static const struct genl_multicast_group tcmu_mcgrps[] = {
3617c9e7a6fSAndy Grover 	[TCMU_MCGRP_CONFIG] = { .name = "config", },
3627c9e7a6fSAndy Grover };
3637c9e7a6fSAndy Grover 
364b3af66e2SMike Christie static struct nla_policy tcmu_attr_policy[TCMU_ATTR_MAX+1] = {
365b3af66e2SMike Christie 	[TCMU_ATTR_DEVICE]	= { .type = NLA_STRING },
366b3af66e2SMike Christie 	[TCMU_ATTR_MINOR]	= { .type = NLA_U32 },
367b3af66e2SMike Christie 	[TCMU_ATTR_CMD_STATUS]	= { .type = NLA_S32 },
368b3af66e2SMike Christie 	[TCMU_ATTR_DEVICE_ID]	= { .type = NLA_U32 },
369b3af66e2SMike Christie 	[TCMU_ATTR_SUPP_KERN_CMD_REPLY] = { .type = NLA_U8 },
370b3af66e2SMike Christie };
371b3af66e2SMike Christie 
tcmu_genl_cmd_done(struct genl_info * info,int completed_cmd)372b3af66e2SMike Christie static int tcmu_genl_cmd_done(struct genl_info *info, int completed_cmd)
373b3af66e2SMike Christie {
3743228691fSMike Christie 	struct tcmu_dev *udev = NULL;
375b3af66e2SMike Christie 	struct tcmu_nl_cmd *nl_cmd;
376b3af66e2SMike Christie 	int dev_id, rc, ret = 0;
377b3af66e2SMike Christie 
378b3af66e2SMike Christie 	if (!info->attrs[TCMU_ATTR_CMD_STATUS] ||
379b3af66e2SMike Christie 	    !info->attrs[TCMU_ATTR_DEVICE_ID]) {
380b3af66e2SMike Christie 		printk(KERN_ERR "TCMU_ATTR_CMD_STATUS or TCMU_ATTR_DEVICE_ID not set, doing nothing\n");
381b3af66e2SMike Christie 		return -EINVAL;
382b3af66e2SMike Christie         }
383b3af66e2SMike Christie 
384b3af66e2SMike Christie 	dev_id = nla_get_u32(info->attrs[TCMU_ATTR_DEVICE_ID]);
385b3af66e2SMike Christie 	rc = nla_get_s32(info->attrs[TCMU_ATTR_CMD_STATUS]);
386b3af66e2SMike Christie 
3873228691fSMike Christie 	mutex_lock(&tcmu_nl_cmd_mutex);
3883228691fSMike Christie 	list_for_each_entry(nl_cmd, &tcmu_nl_cmd_list, nl_list) {
3893228691fSMike Christie 		if (nl_cmd->udev->se_dev.dev_index == dev_id) {
3903228691fSMike Christie 			udev = nl_cmd->udev;
3913228691fSMike Christie 			break;
392b3af66e2SMike Christie 		}
3933228691fSMike Christie 	}
394b3af66e2SMike Christie 
3953228691fSMike Christie 	if (!udev) {
3960c218e16SMike Christie 		pr_err("tcmu nl cmd %u/%d completion could not find device with dev id %u.\n",
3973228691fSMike Christie 		       completed_cmd, rc, dev_id);
3983228691fSMike Christie 		ret = -ENODEV;
3993228691fSMike Christie 		goto unlock;
4003228691fSMike Christie 	}
4013228691fSMike Christie 	list_del(&nl_cmd->nl_list);
402b3af66e2SMike Christie 
403bdaeedc1SMike Christie 	pr_debug("%s genl cmd done got id %d curr %d done %d rc %d stat %d\n",
404bdaeedc1SMike Christie 		 udev->name, dev_id, nl_cmd->cmd, completed_cmd, rc,
405bdaeedc1SMike Christie 		 nl_cmd->status);
406b3af66e2SMike Christie 
407b3af66e2SMike Christie 	if (nl_cmd->cmd != completed_cmd) {
4083228691fSMike Christie 		pr_err("Mismatched commands on %s (Expecting reply for %d. Current %d).\n",
4093228691fSMike Christie 		       udev->name, completed_cmd, nl_cmd->cmd);
410b3af66e2SMike Christie 		ret = -EINVAL;
4113228691fSMike Christie 		goto unlock;
412b3af66e2SMike Christie 	}
413b3af66e2SMike Christie 
4143228691fSMike Christie 	nl_cmd->status = rc;
415b3af66e2SMike Christie 	complete(&nl_cmd->complete);
4163228691fSMike Christie unlock:
4173228691fSMike Christie 	mutex_unlock(&tcmu_nl_cmd_mutex);
418b3af66e2SMike Christie 	return ret;
419b3af66e2SMike Christie }
420b3af66e2SMike Christie 
tcmu_genl_rm_dev_done(struct sk_buff * skb,struct genl_info * info)421b3af66e2SMike Christie static int tcmu_genl_rm_dev_done(struct sk_buff *skb, struct genl_info *info)
422b3af66e2SMike Christie {
423b3af66e2SMike Christie 	return tcmu_genl_cmd_done(info, TCMU_CMD_REMOVED_DEVICE);
424b3af66e2SMike Christie }
425b3af66e2SMike Christie 
tcmu_genl_add_dev_done(struct sk_buff * skb,struct genl_info * info)426b3af66e2SMike Christie static int tcmu_genl_add_dev_done(struct sk_buff *skb, struct genl_info *info)
427b3af66e2SMike Christie {
428b3af66e2SMike Christie 	return tcmu_genl_cmd_done(info, TCMU_CMD_ADDED_DEVICE);
429b3af66e2SMike Christie }
430b3af66e2SMike Christie 
tcmu_genl_reconfig_dev_done(struct sk_buff * skb,struct genl_info * info)431b3af66e2SMike Christie static int tcmu_genl_reconfig_dev_done(struct sk_buff *skb,
432b3af66e2SMike Christie 				       struct genl_info *info)
433b3af66e2SMike Christie {
434b3af66e2SMike Christie 	return tcmu_genl_cmd_done(info, TCMU_CMD_RECONFIG_DEVICE);
435b3af66e2SMike Christie }
436b3af66e2SMike Christie 
tcmu_genl_set_features(struct sk_buff * skb,struct genl_info * info)437b3af66e2SMike Christie static int tcmu_genl_set_features(struct sk_buff *skb, struct genl_info *info)
438b3af66e2SMike Christie {
439b3af66e2SMike Christie 	if (info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]) {
440b3af66e2SMike Christie 		tcmu_kern_cmd_reply_supported  =
441b3af66e2SMike Christie 			nla_get_u8(info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]);
442b3af66e2SMike Christie 		printk(KERN_INFO "tcmu daemon: command reply support %u.\n",
443b3af66e2SMike Christie 		       tcmu_kern_cmd_reply_supported);
444b3af66e2SMike Christie 	}
445b3af66e2SMike Christie 
446b3af66e2SMike Christie 	return 0;
447b3af66e2SMike Christie }
448b3af66e2SMike Christie 
44966a9b928SJakub Kicinski static const struct genl_small_ops tcmu_genl_ops[] = {
450b3af66e2SMike Christie 	{
451b3af66e2SMike Christie 		.cmd	= TCMU_CMD_SET_FEATURES,
452ef6243acSJohannes Berg 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
453b3af66e2SMike Christie 		.flags	= GENL_ADMIN_PERM,
454b3af66e2SMike Christie 		.doit	= tcmu_genl_set_features,
455b3af66e2SMike Christie 	},
456b3af66e2SMike Christie 	{
457b3af66e2SMike Christie 		.cmd	= TCMU_CMD_ADDED_DEVICE_DONE,
458ef6243acSJohannes Berg 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
459b3af66e2SMike Christie 		.flags	= GENL_ADMIN_PERM,
460b3af66e2SMike Christie 		.doit	= tcmu_genl_add_dev_done,
461b3af66e2SMike Christie 	},
462b3af66e2SMike Christie 	{
463b3af66e2SMike Christie 		.cmd	= TCMU_CMD_REMOVED_DEVICE_DONE,
464ef6243acSJohannes Berg 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
465b3af66e2SMike Christie 		.flags	= GENL_ADMIN_PERM,
466b3af66e2SMike Christie 		.doit	= tcmu_genl_rm_dev_done,
467b3af66e2SMike Christie 	},
468b3af66e2SMike Christie 	{
469b3af66e2SMike Christie 		.cmd	= TCMU_CMD_RECONFIG_DEVICE_DONE,
470ef6243acSJohannes Berg 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
471b3af66e2SMike Christie 		.flags	= GENL_ADMIN_PERM,
472b3af66e2SMike Christie 		.doit	= tcmu_genl_reconfig_dev_done,
473b3af66e2SMike Christie 	},
474b3af66e2SMike Christie };
475b3af66e2SMike Christie 
4767c9e7a6fSAndy Grover /* Our generic netlink family */
47756989f6dSJohannes Berg static struct genl_family tcmu_genl_family __ro_after_init = {
478489111e5SJohannes Berg 	.module = THIS_MODULE,
4797c9e7a6fSAndy Grover 	.hdrsize = 0,
4807c9e7a6fSAndy Grover 	.name = "TCM-USER",
481b3af66e2SMike Christie 	.version = 2,
4827c9e7a6fSAndy Grover 	.maxattr = TCMU_ATTR_MAX,
4833b0f31f2SJohannes Berg 	.policy = tcmu_attr_policy,
4847c9e7a6fSAndy Grover 	.mcgrps = tcmu_mcgrps,
4857c9e7a6fSAndy Grover 	.n_mcgrps = ARRAY_SIZE(tcmu_mcgrps),
48620c08b36SSheng Yang 	.netnsok = true,
48766a9b928SJakub Kicinski 	.small_ops = tcmu_genl_ops,
48866a9b928SJakub Kicinski 	.n_small_ops = ARRAY_SIZE(tcmu_genl_ops),
4899c5d03d3SJakub Kicinski 	.resv_start_op = TCMU_CMD_SET_FEATURES + 1,
4907c9e7a6fSAndy Grover };
4917c9e7a6fSAndy Grover 
492141685a3SXiubo Li #define tcmu_cmd_set_dbi_cur(cmd, index) ((cmd)->dbi_cur = (index))
493141685a3SXiubo Li #define tcmu_cmd_reset_dbi_cur(cmd) tcmu_cmd_set_dbi_cur(cmd, 0)
494141685a3SXiubo Li #define tcmu_cmd_set_dbi(cmd, index) ((cmd)->dbi[(cmd)->dbi_cur++] = (index))
495141685a3SXiubo Li #define tcmu_cmd_get_dbi(cmd) ((cmd)->dbi[(cmd)->dbi_cur++])
496141685a3SXiubo Li 
tcmu_cmd_free_data(struct tcmu_cmd * tcmu_cmd,uint32_t len)497b6df4b79SXiubo Li static void tcmu_cmd_free_data(struct tcmu_cmd *tcmu_cmd, uint32_t len)
498141685a3SXiubo Li {
499141685a3SXiubo Li 	struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
500141685a3SXiubo Li 	uint32_t i;
501141685a3SXiubo Li 
502b6df4b79SXiubo Li 	for (i = 0; i < len; i++)
503141685a3SXiubo Li 		clear_bit(tcmu_cmd->dbi[i], udev->data_bitmap);
504141685a3SXiubo Li }
505141685a3SXiubo Li 
tcmu_get_empty_block(struct tcmu_dev * udev,struct tcmu_cmd * tcmu_cmd,int prev_dbi,int length,int * iov_cnt)5067e98905eSBodo Stroesser static inline int tcmu_get_empty_block(struct tcmu_dev *udev,
5077e98905eSBodo Stroesser 				       struct tcmu_cmd *tcmu_cmd,
508f5ce815fSBodo Stroesser 				       int prev_dbi, int length, int *iov_cnt)
509141685a3SXiubo Li {
510f5ce815fSBodo Stroesser 	XA_STATE(xas, &udev->data_pages, 0);
511b6df4b79SXiubo Li 	struct page *page;
512e719afdcSBodo Stroesser 	int i, cnt, dbi, dpi;
513f5ce815fSBodo Stroesser 	int page_cnt = DIV_ROUND_UP(length, PAGE_SIZE);
514141685a3SXiubo Li 
515b6df4b79SXiubo Li 	dbi = find_first_zero_bit(udev->data_bitmap, udev->dbi_thresh);
516b6df4b79SXiubo Li 	if (dbi == udev->dbi_thresh)
5177e98905eSBodo Stroesser 		return -1;
518b6df4b79SXiubo Li 
519e719afdcSBodo Stroesser 	dpi = dbi * udev->data_pages_per_blk;
520f5ce815fSBodo Stroesser 	/* Count the number of already allocated pages */
521e719afdcSBodo Stroesser 	xas_set(&xas, dpi);
522b4150b68SBodo Stroesser 	rcu_read_lock();
523f5ce815fSBodo Stroesser 	for (cnt = 0; xas_next(&xas) && cnt < page_cnt;)
524f5ce815fSBodo Stroesser 		cnt++;
525b4150b68SBodo Stroesser 	rcu_read_unlock();
526b6df4b79SXiubo Li 
527f5ce815fSBodo Stroesser 	for (i = cnt; i < page_cnt; i++) {
5281d2ac7b6SBodo Stroesser 		/* try to get new zeroed page from the mm */
5291d2ac7b6SBodo Stroesser 		page = alloc_page(GFP_NOIO | __GFP_ZERO);
530b6df4b79SXiubo Li 		if (!page)
531f5ce815fSBodo Stroesser 			break;
532b6df4b79SXiubo Li 
533e719afdcSBodo Stroesser 		if (xa_store(&udev->data_pages, dpi + i, page, GFP_NOIO)) {
534f5ce815fSBodo Stroesser 			__free_page(page);
535f5ce815fSBodo Stroesser 			break;
536b6df4b79SXiubo Li 		}
537f5ce815fSBodo Stroesser 	}
538f5ce815fSBodo Stroesser 	if (atomic_add_return(i - cnt, &global_page_count) >
539f5ce815fSBodo Stroesser 			      tcmu_global_max_pages)
540f5ce815fSBodo Stroesser 		schedule_delayed_work(&tcmu_unmap_work, 0);
541b6df4b79SXiubo Li 
542f5ce815fSBodo Stroesser 	if (i && dbi > udev->dbi_max)
543141685a3SXiubo Li 		udev->dbi_max = dbi;
544141685a3SXiubo Li 
545141685a3SXiubo Li 	set_bit(dbi, udev->data_bitmap);
546b6df4b79SXiubo Li 	tcmu_cmd_set_dbi(tcmu_cmd, dbi);
547141685a3SXiubo Li 
5487e98905eSBodo Stroesser 	if (dbi != prev_dbi + 1)
5497e98905eSBodo Stroesser 		*iov_cnt += 1;
5507e98905eSBodo Stroesser 
551f5ce815fSBodo Stroesser 	return i == page_cnt ? dbi : -1;
552141685a3SXiubo Li }
553141685a3SXiubo Li 
tcmu_get_empty_blocks(struct tcmu_dev * udev,struct tcmu_cmd * tcmu_cmd,int length)5547e98905eSBodo Stroesser static int tcmu_get_empty_blocks(struct tcmu_dev *udev,
555f5ce815fSBodo Stroesser 				 struct tcmu_cmd *tcmu_cmd, int length)
556b6df4b79SXiubo Li {
5577e98905eSBodo Stroesser 	/* start value of dbi + 1 must not be a valid dbi */
5587e98905eSBodo Stroesser 	int dbi = -2;
559e719afdcSBodo Stroesser 	int blk_data_len, iov_cnt = 0;
560e719afdcSBodo Stroesser 	uint32_t blk_size = udev->data_blk_size;
561b6df4b79SXiubo Li 
562e719afdcSBodo Stroesser 	for (; length > 0; length -= blk_size) {
563e719afdcSBodo Stroesser 		blk_data_len = min_t(uint32_t, length, blk_size);
564e719afdcSBodo Stroesser 		dbi = tcmu_get_empty_block(udev, tcmu_cmd, dbi, blk_data_len,
565e719afdcSBodo Stroesser 					   &iov_cnt);
5667e98905eSBodo Stroesser 		if (dbi < 0)
5677e98905eSBodo Stroesser 			return -1;
568141685a3SXiubo Li 	}
5697e98905eSBodo Stroesser 	return iov_cnt;
570141685a3SXiubo Li }
571141685a3SXiubo Li 
tcmu_free_cmd(struct tcmu_cmd * tcmu_cmd)572141685a3SXiubo Li static inline void tcmu_free_cmd(struct tcmu_cmd *tcmu_cmd)
573141685a3SXiubo Li {
574141685a3SXiubo Li 	kfree(tcmu_cmd->dbi);
575141685a3SXiubo Li 	kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
576141685a3SXiubo Li }
577141685a3SXiubo Li 
tcmu_cmd_set_block_cnts(struct tcmu_cmd * cmd)57852ef2743SBodo Stroesser static inline void tcmu_cmd_set_block_cnts(struct tcmu_cmd *cmd)
579141685a3SXiubo Li {
58052ef2743SBodo Stroesser 	int i, len;
58152ef2743SBodo Stroesser 	struct se_cmd *se_cmd = cmd->se_cmd;
582e719afdcSBodo Stroesser 	uint32_t blk_size = cmd->tcmu_dev->data_blk_size;
58352ef2743SBodo Stroesser 
584e719afdcSBodo Stroesser 	cmd->dbi_cnt = DIV_ROUND_UP(se_cmd->data_length, blk_size);
585141685a3SXiubo Li 
586141685a3SXiubo Li 	if (se_cmd->se_cmd_flags & SCF_BIDI) {
587141685a3SXiubo Li 		BUG_ON(!(se_cmd->t_bidi_data_sg && se_cmd->t_bidi_data_nents));
58852ef2743SBodo Stroesser 		for (i = 0, len = 0; i < se_cmd->t_bidi_data_nents; i++)
58952ef2743SBodo Stroesser 			len += se_cmd->t_bidi_data_sg[i].length;
590e719afdcSBodo Stroesser 		cmd->dbi_bidi_cnt = DIV_ROUND_UP(len, blk_size);
59152ef2743SBodo Stroesser 		cmd->dbi_cnt += cmd->dbi_bidi_cnt;
5923c9a7c58SBodo Stroesser 		cmd->data_len_bidi = len;
593141685a3SXiubo Li 	}
594141685a3SXiubo Li }
595141685a3SXiubo Li 
new_block_to_iov(struct tcmu_dev * udev,struct tcmu_cmd * cmd,struct iovec ** iov,int prev_dbi,int len)5963c9a7c58SBodo Stroesser static int new_block_to_iov(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
597c8ed1ff8SBodo Stroesser 			    struct iovec **iov, int prev_dbi, int len)
5983c9a7c58SBodo Stroesser {
5993c9a7c58SBodo Stroesser 	/* Get the next dbi */
6003c9a7c58SBodo Stroesser 	int dbi = tcmu_cmd_get_dbi(cmd);
6013c9a7c58SBodo Stroesser 
602e719afdcSBodo Stroesser 	/* Do not add more than udev->data_blk_size to iov */
603e719afdcSBodo Stroesser 	len = min_t(int,  len, udev->data_blk_size);
604c8ed1ff8SBodo Stroesser 
6053c9a7c58SBodo Stroesser 	/*
6063c9a7c58SBodo Stroesser 	 * The following code will gather and map the blocks to the same iovec
6073c9a7c58SBodo Stroesser 	 * when the blocks are all next to each other.
6083c9a7c58SBodo Stroesser 	 */
6093c9a7c58SBodo Stroesser 	if (dbi != prev_dbi + 1) {
6103c9a7c58SBodo Stroesser 		/* dbi is not next to previous dbi, so start new iov */
6113c9a7c58SBodo Stroesser 		if (prev_dbi >= 0)
6123c9a7c58SBodo Stroesser 			(*iov)++;
6133c9a7c58SBodo Stroesser 		/* write offset relative to mb_addr */
6143c9a7c58SBodo Stroesser 		(*iov)->iov_base = (void __user *)
615e719afdcSBodo Stroesser 				   (udev->data_off + dbi * udev->data_blk_size);
6163c9a7c58SBodo Stroesser 	}
6173c9a7c58SBodo Stroesser 	(*iov)->iov_len += len;
6183c9a7c58SBodo Stroesser 
6193c9a7c58SBodo Stroesser 	return dbi;
6203c9a7c58SBodo Stroesser }
6213c9a7c58SBodo Stroesser 
tcmu_setup_iovs(struct tcmu_dev * udev,struct tcmu_cmd * cmd,struct iovec ** iov,int data_length)6223c9a7c58SBodo Stroesser static void tcmu_setup_iovs(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
6233c9a7c58SBodo Stroesser 			    struct iovec **iov, int data_length)
6243c9a7c58SBodo Stroesser {
6253c9a7c58SBodo Stroesser 	/* start value of dbi + 1 must not be a valid dbi */
6263c9a7c58SBodo Stroesser 	int dbi = -2;
6273c9a7c58SBodo Stroesser 
6283c9a7c58SBodo Stroesser 	/* We prepare the IOVs for DMA_FROM_DEVICE transfer direction */
629e719afdcSBodo Stroesser 	for (; data_length > 0; data_length -= udev->data_blk_size)
630c8ed1ff8SBodo Stroesser 		dbi = new_block_to_iov(udev, cmd, iov, dbi, data_length);
6313c9a7c58SBodo Stroesser }
6323c9a7c58SBodo Stroesser 
tcmu_alloc_cmd(struct se_cmd * se_cmd)6337c9e7a6fSAndy Grover static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd)
6347c9e7a6fSAndy Grover {
6357c9e7a6fSAndy Grover 	struct se_device *se_dev = se_cmd->se_dev;
6367c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
6377c9e7a6fSAndy Grover 	struct tcmu_cmd *tcmu_cmd;
6387c9e7a6fSAndy Grover 
6390eccce86SDamien Le Moal 	tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_NOIO);
6407c9e7a6fSAndy Grover 	if (!tcmu_cmd)
6417c9e7a6fSAndy Grover 		return NULL;
6427c9e7a6fSAndy Grover 
643a94a2572SXiubo Li 	INIT_LIST_HEAD(&tcmu_cmd->queue_entry);
6447c9e7a6fSAndy Grover 	tcmu_cmd->se_cmd = se_cmd;
6457c9e7a6fSAndy Grover 	tcmu_cmd->tcmu_dev = udev;
6467c9e7a6fSAndy Grover 
64752ef2743SBodo Stroesser 	tcmu_cmd_set_block_cnts(tcmu_cmd);
648141685a3SXiubo Li 	tcmu_cmd->dbi = kcalloc(tcmu_cmd->dbi_cnt, sizeof(uint32_t),
6490eccce86SDamien Le Moal 				GFP_NOIO);
650141685a3SXiubo Li 	if (!tcmu_cmd->dbi) {
651141685a3SXiubo Li 		kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
652141685a3SXiubo Li 		return NULL;
653141685a3SXiubo Li 	}
654141685a3SXiubo Li 
6557c9e7a6fSAndy Grover 	return tcmu_cmd;
6567c9e7a6fSAndy Grover }
6577c9e7a6fSAndy Grover 
tcmu_flush_dcache_range(void * vaddr,size_t size)6587c9e7a6fSAndy Grover static inline void tcmu_flush_dcache_range(void *vaddr, size_t size)
6597c9e7a6fSAndy Grover {
660b75d8063SGeliang Tang 	unsigned long offset = offset_in_page(vaddr);
66126d2b310Stangwenji 	void *start = vaddr - offset;
6627c9e7a6fSAndy Grover 
6637c9e7a6fSAndy Grover 	size = round_up(size+offset, PAGE_SIZE);
6647c9e7a6fSAndy Grover 
6657c9e7a6fSAndy Grover 	while (size) {
6663145550aSBodo Stroesser 		flush_dcache_page(vmalloc_to_page(start));
66726d2b310Stangwenji 		start += PAGE_SIZE;
6687c9e7a6fSAndy Grover 		size -= PAGE_SIZE;
6697c9e7a6fSAndy Grover 	}
6707c9e7a6fSAndy Grover }
6717c9e7a6fSAndy Grover 
6727c9e7a6fSAndy Grover /*
6737c9e7a6fSAndy Grover  * Some ring helper functions. We don't assume size is a power of 2 so
6747c9e7a6fSAndy Grover  * we can't use circ_buf.h.
6757c9e7a6fSAndy Grover  */
spc_used(size_t head,size_t tail,size_t size)6767c9e7a6fSAndy Grover static inline size_t spc_used(size_t head, size_t tail, size_t size)
6777c9e7a6fSAndy Grover {
6787c9e7a6fSAndy Grover 	int diff = head - tail;
6797c9e7a6fSAndy Grover 
6807c9e7a6fSAndy Grover 	if (diff >= 0)
6817c9e7a6fSAndy Grover 		return diff;
6827c9e7a6fSAndy Grover 	else
6837c9e7a6fSAndy Grover 		return size + diff;
6847c9e7a6fSAndy Grover }
6857c9e7a6fSAndy Grover 
spc_free(size_t head,size_t tail,size_t size)6867c9e7a6fSAndy Grover static inline size_t spc_free(size_t head, size_t tail, size_t size)
6877c9e7a6fSAndy Grover {
6887c9e7a6fSAndy Grover 	/* Keep 1 byte unused or we can't tell full from empty */
6897c9e7a6fSAndy Grover 	return (size - spc_used(head, tail, size) - 1);
6907c9e7a6fSAndy Grover }
6917c9e7a6fSAndy Grover 
head_to_end(size_t head,size_t size)6927c9e7a6fSAndy Grover static inline size_t head_to_end(size_t head, size_t size)
6937c9e7a6fSAndy Grover {
6947c9e7a6fSAndy Grover 	return size - head;
6957c9e7a6fSAndy Grover }
6967c9e7a6fSAndy Grover 
6977c9e7a6fSAndy Grover #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size)
6987c9e7a6fSAndy Grover 
699c8ed1ff8SBodo Stroesser #define TCMU_SG_TO_DATA_AREA 1
700c8ed1ff8SBodo Stroesser #define TCMU_DATA_AREA_TO_SG 2
701c8ed1ff8SBodo Stroesser 
tcmu_copy_data(struct tcmu_dev * udev,struct tcmu_cmd * tcmu_cmd,uint32_t direction,struct scatterlist * sg,unsigned int sg_nents,struct iovec ** iov,size_t data_len)702c8ed1ff8SBodo Stroesser static inline void tcmu_copy_data(struct tcmu_dev *udev,
703c8ed1ff8SBodo Stroesser 				  struct tcmu_cmd *tcmu_cmd, uint32_t direction,
704c8ed1ff8SBodo Stroesser 				  struct scatterlist *sg, unsigned int sg_nents,
705c8ed1ff8SBodo Stroesser 				  struct iovec **iov, size_t data_len)
706c8ed1ff8SBodo Stroesser {
707c8ed1ff8SBodo Stroesser 	/* start value of dbi + 1 must not be a valid dbi */
708c8ed1ff8SBodo Stroesser 	int dbi = -2;
709f5ce815fSBodo Stroesser 	size_t page_remaining, cp_len;
710b4150b68SBodo Stroesser 	int page_cnt, page_inx, dpi;
711c8ed1ff8SBodo Stroesser 	struct sg_mapping_iter sg_iter;
712c8ed1ff8SBodo Stroesser 	unsigned int sg_flags;
713c8ed1ff8SBodo Stroesser 	struct page *page;
714c8ed1ff8SBodo Stroesser 	void *data_page_start, *data_addr;
715c8ed1ff8SBodo Stroesser 
716c8ed1ff8SBodo Stroesser 	if (direction == TCMU_SG_TO_DATA_AREA)
717c8ed1ff8SBodo Stroesser 		sg_flags = SG_MITER_ATOMIC | SG_MITER_FROM_SG;
718c8ed1ff8SBodo Stroesser 	else
719c8ed1ff8SBodo Stroesser 		sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG;
720c8ed1ff8SBodo Stroesser 	sg_miter_start(&sg_iter, sg, sg_nents, sg_flags);
721c8ed1ff8SBodo Stroesser 
722c8ed1ff8SBodo Stroesser 	while (data_len) {
723c8ed1ff8SBodo Stroesser 		if (direction == TCMU_SG_TO_DATA_AREA)
724c8ed1ff8SBodo Stroesser 			dbi = new_block_to_iov(udev, tcmu_cmd, iov, dbi,
725c8ed1ff8SBodo Stroesser 					       data_len);
726c8ed1ff8SBodo Stroesser 		else
727c8ed1ff8SBodo Stroesser 			dbi = tcmu_cmd_get_dbi(tcmu_cmd);
728f5ce815fSBodo Stroesser 
729f5ce815fSBodo Stroesser 		page_cnt = DIV_ROUND_UP(data_len, PAGE_SIZE);
730e719afdcSBodo Stroesser 		if (page_cnt > udev->data_pages_per_blk)
731e719afdcSBodo Stroesser 			page_cnt = udev->data_pages_per_blk;
732f5ce815fSBodo Stroesser 
733b4150b68SBodo Stroesser 		dpi = dbi * udev->data_pages_per_blk;
734b4150b68SBodo Stroesser 		for (page_inx = 0; page_inx < page_cnt && data_len;
735b4150b68SBodo Stroesser 		     page_inx++, dpi++) {
736b4150b68SBodo Stroesser 			page = xa_load(&udev->data_pages, dpi);
737f5ce815fSBodo Stroesser 
738c8ed1ff8SBodo Stroesser 			if (direction == TCMU_DATA_AREA_TO_SG)
739c8ed1ff8SBodo Stroesser 				flush_dcache_page(page);
740c8ed1ff8SBodo Stroesser 			data_page_start = kmap_atomic(page);
741f5ce815fSBodo Stroesser 			page_remaining = PAGE_SIZE;
742c8ed1ff8SBodo Stroesser 
743f5ce815fSBodo Stroesser 			while (page_remaining && data_len) {
744c8ed1ff8SBodo Stroesser 				if (!sg_miter_next(&sg_iter)) {
745c8ed1ff8SBodo Stroesser 					/* set length to 0 to abort outer loop */
746c8ed1ff8SBodo Stroesser 					data_len = 0;
747f5ce815fSBodo Stroesser 					pr_debug("%s: aborting data copy due to exhausted sg_list\n",
748f5ce815fSBodo Stroesser 						 __func__);
749c8ed1ff8SBodo Stroesser 					break;
750c8ed1ff8SBodo Stroesser 				}
751f5ce815fSBodo Stroesser 				cp_len = min3(sg_iter.length, page_remaining,
752f5ce815fSBodo Stroesser 					      data_len);
753c8ed1ff8SBodo Stroesser 
754c8ed1ff8SBodo Stroesser 				data_addr = data_page_start +
755f5ce815fSBodo Stroesser 					    PAGE_SIZE - page_remaining;
756c8ed1ff8SBodo Stroesser 				if (direction == TCMU_SG_TO_DATA_AREA)
757c8ed1ff8SBodo Stroesser 					memcpy(data_addr, sg_iter.addr, cp_len);
758c8ed1ff8SBodo Stroesser 				else
759c8ed1ff8SBodo Stroesser 					memcpy(sg_iter.addr, data_addr, cp_len);
760c8ed1ff8SBodo Stroesser 
761c8ed1ff8SBodo Stroesser 				data_len -= cp_len;
762f5ce815fSBodo Stroesser 				page_remaining -= cp_len;
763c8ed1ff8SBodo Stroesser 				sg_iter.consumed = cp_len;
764c8ed1ff8SBodo Stroesser 			}
765c8ed1ff8SBodo Stroesser 			sg_miter_stop(&sg_iter);
766c8ed1ff8SBodo Stroesser 
767c8ed1ff8SBodo Stroesser 			kunmap_atomic(data_page_start);
768c8ed1ff8SBodo Stroesser 			if (direction == TCMU_SG_TO_DATA_AREA)
769c8ed1ff8SBodo Stroesser 				flush_dcache_page(page);
770c8ed1ff8SBodo Stroesser 		}
771c8ed1ff8SBodo Stroesser 	}
772f5ce815fSBodo Stroesser }
773c8ed1ff8SBodo Stroesser 
scatter_data_area(struct tcmu_dev * udev,struct tcmu_cmd * tcmu_cmd,struct iovec ** iov)7743c9a7c58SBodo Stroesser static void scatter_data_area(struct tcmu_dev *udev, struct tcmu_cmd *tcmu_cmd,
7753c9a7c58SBodo Stroesser 			      struct iovec **iov)
77626418649SSheng Yang {
7773c9a7c58SBodo Stroesser 	struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
778f97ec7dbSIlias Tsitsimpis 
779c8ed1ff8SBodo Stroesser 	tcmu_copy_data(udev, tcmu_cmd, TCMU_SG_TO_DATA_AREA, se_cmd->t_data_sg,
780c8ed1ff8SBodo Stroesser 		       se_cmd->t_data_nents, iov, se_cmd->data_length);
7813c58f737SBodo Stroesser }
782b6df4b79SXiubo Li 
gather_data_area(struct tcmu_dev * udev,struct tcmu_cmd * tcmu_cmd,bool bidi,uint32_t read_len)783c8ed1ff8SBodo Stroesser static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *tcmu_cmd,
7846c3796d1Sbstroesser@ts.fujitsu.com 			     bool bidi, uint32_t read_len)
785f97ec7dbSIlias Tsitsimpis {
786c8ed1ff8SBodo Stroesser 	struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
787c8ed1ff8SBodo Stroesser 	struct scatterlist *data_sg;
788a5d68ba8SXiubo Li 	unsigned int data_nents;
789a5d68ba8SXiubo Li 
790a5d68ba8SXiubo Li 	if (!bidi) {
791a5d68ba8SXiubo Li 		data_sg = se_cmd->t_data_sg;
792a5d68ba8SXiubo Li 		data_nents = se_cmd->t_data_nents;
793a5d68ba8SXiubo Li 	} else {
794a5d68ba8SXiubo Li 		/*
795a5d68ba8SXiubo Li 		 * For bidi case, the first count blocks are for Data-Out
796a5d68ba8SXiubo Li 		 * buffer blocks, and before gathering the Data-In buffer
79752ef2743SBodo Stroesser 		 * the Data-Out buffer blocks should be skipped.
798a5d68ba8SXiubo Li 		 */
799c8ed1ff8SBodo Stroesser 		tcmu_cmd_set_dbi_cur(tcmu_cmd,
800c8ed1ff8SBodo Stroesser 				     tcmu_cmd->dbi_cnt - tcmu_cmd->dbi_bidi_cnt);
801a5d68ba8SXiubo Li 
802a5d68ba8SXiubo Li 		data_sg = se_cmd->t_bidi_data_sg;
803a5d68ba8SXiubo Li 		data_nents = se_cmd->t_bidi_data_nents;
804a5d68ba8SXiubo Li 	}
805f97ec7dbSIlias Tsitsimpis 
806c8ed1ff8SBodo Stroesser 	tcmu_copy_data(udev, tcmu_cmd, TCMU_DATA_AREA_TO_SG, data_sg,
807c8ed1ff8SBodo Stroesser 		       data_nents, NULL, read_len);
808f97ec7dbSIlias Tsitsimpis }
809f97ec7dbSIlias Tsitsimpis 
spc_bitmap_free(unsigned long * bitmap,uint32_t thresh)810b6df4b79SXiubo Li static inline size_t spc_bitmap_free(unsigned long *bitmap, uint32_t thresh)
81126418649SSheng Yang {
8123c0f26ffSMike Christie 	return thresh - bitmap_weight(bitmap, thresh);
81326418649SSheng Yang }
81426418649SSheng Yang 
8157c9e7a6fSAndy Grover /*
8167e98905eSBodo Stroesser  * We can't queue a command until we have space available on the cmd ring.
8177c9e7a6fSAndy Grover  *
8187c9e7a6fSAndy Grover  * Called with ring lock held.
8197c9e7a6fSAndy Grover  */
is_ring_space_avail(struct tcmu_dev * udev,size_t cmd_size)8207e98905eSBodo Stroesser static bool is_ring_space_avail(struct tcmu_dev *udev, size_t cmd_size)
8217c9e7a6fSAndy Grover {
8227c9e7a6fSAndy Grover 	struct tcmu_mailbox *mb = udev->mb_addr;
8230241fd39SNicholas Bellinger 	size_t space, cmd_needed;
8247c9e7a6fSAndy Grover 	u32 cmd_head;
8257c9e7a6fSAndy Grover 
8267c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(mb, sizeof(*mb));
8277c9e7a6fSAndy Grover 
8287c9e7a6fSAndy Grover 	cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
8297c9e7a6fSAndy Grover 
830f56574a2SAndy Grover 	/*
831f56574a2SAndy Grover 	 * If cmd end-of-ring space is too small then we need space for a NOP plus
832f56574a2SAndy Grover 	 * original cmd - cmds are internally contiguous.
833f56574a2SAndy Grover 	 */
834f56574a2SAndy Grover 	if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size)
835f56574a2SAndy Grover 		cmd_needed = cmd_size;
836f56574a2SAndy Grover 	else
837f56574a2SAndy Grover 		cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size);
838f56574a2SAndy Grover 
8397c9e7a6fSAndy Grover 	space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size);
8407c9e7a6fSAndy Grover 	if (space < cmd_needed) {
8417c9e7a6fSAndy Grover 		pr_debug("no cmd space: %u %u %u\n", cmd_head,
8427c9e7a6fSAndy Grover 		       udev->cmdr_last_cleaned, udev->cmdr_size);
8437c9e7a6fSAndy Grover 		return false;
8447c9e7a6fSAndy Grover 	}
845bc2d214aSBodo Stroesser 	return true;
8467e98905eSBodo Stroesser }
8477e98905eSBodo Stroesser 
8487e98905eSBodo Stroesser /*
8497e98905eSBodo Stroesser  * We have to allocate data buffers before we can queue a command.
8507e98905eSBodo Stroesser  * Returns -1 on error (not enough space) or number of needed iovs on success
8517e98905eSBodo Stroesser  *
8527e98905eSBodo Stroesser  * Called with ring lock held.
8537e98905eSBodo Stroesser  */
tcmu_alloc_data_space(struct tcmu_dev * udev,struct tcmu_cmd * cmd,int * iov_bidi_cnt)8547e98905eSBodo Stroesser static int tcmu_alloc_data_space(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
8557e98905eSBodo Stroesser 				  int *iov_bidi_cnt)
8567e98905eSBodo Stroesser {
8577e98905eSBodo Stroesser 	int space, iov_cnt = 0, ret = 0;
8587e98905eSBodo Stroesser 
8597e98905eSBodo Stroesser 	if (!cmd->dbi_cnt)
8607e98905eSBodo Stroesser 		goto wr_iov_cnts;
861bc2d214aSBodo Stroesser 
862b6df4b79SXiubo Li 	/* try to check and get the data blocks as needed */
863b6df4b79SXiubo Li 	space = spc_bitmap_free(udev->data_bitmap, udev->dbi_thresh);
86452ef2743SBodo Stroesser 	if (space < cmd->dbi_cnt) {
86580eb8761SMike Christie 		unsigned long blocks_left =
86680eb8761SMike Christie 				(udev->max_blocks - udev->dbi_thresh) + space;
867b6df4b79SXiubo Li 
86852ef2743SBodo Stroesser 		if (blocks_left < cmd->dbi_cnt) {
869e719afdcSBodo Stroesser 			pr_debug("no data space: only %lu available, but ask for %u\n",
870e719afdcSBodo Stroesser 					blocks_left * udev->data_blk_size,
871e719afdcSBodo Stroesser 					cmd->dbi_cnt * udev->data_blk_size);
8727e98905eSBodo Stroesser 			return -1;
8737c9e7a6fSAndy Grover 		}
8747c9e7a6fSAndy Grover 
87552ef2743SBodo Stroesser 		udev->dbi_thresh += cmd->dbi_cnt;
87680eb8761SMike Christie 		if (udev->dbi_thresh > udev->max_blocks)
87780eb8761SMike Christie 			udev->dbi_thresh = udev->max_blocks;
878b6df4b79SXiubo Li 	}
879b6df4b79SXiubo Li 
880f5ce815fSBodo Stroesser 	iov_cnt = tcmu_get_empty_blocks(udev, cmd, cmd->se_cmd->data_length);
8817e98905eSBodo Stroesser 	if (iov_cnt < 0)
8827e98905eSBodo Stroesser 		return -1;
8837e98905eSBodo Stroesser 
8847e98905eSBodo Stroesser 	if (cmd->dbi_bidi_cnt) {
885f5ce815fSBodo Stroesser 		ret = tcmu_get_empty_blocks(udev, cmd, cmd->data_len_bidi);
8867e98905eSBodo Stroesser 		if (ret < 0)
8877e98905eSBodo Stroesser 			return -1;
8887e98905eSBodo Stroesser 	}
8897e98905eSBodo Stroesser wr_iov_cnts:
8907e98905eSBodo Stroesser 	*iov_bidi_cnt = ret;
8917e98905eSBodo Stroesser 	return iov_cnt + ret;
8927c9e7a6fSAndy Grover }
8937c9e7a6fSAndy Grover 
tcmu_cmd_get_base_cmd_size(size_t iov_cnt)894fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_base_cmd_size(size_t iov_cnt)
895fe25cc34SXiubo Li {
896fe25cc34SXiubo Li 	return max(offsetof(struct tcmu_cmd_entry, req.iov[iov_cnt]),
897fe25cc34SXiubo Li 			sizeof(struct tcmu_cmd_entry));
898fe25cc34SXiubo Li }
899fe25cc34SXiubo Li 
tcmu_cmd_get_cmd_size(struct tcmu_cmd * tcmu_cmd,size_t base_command_size)900fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_cmd_size(struct tcmu_cmd *tcmu_cmd,
901fe25cc34SXiubo Li 					   size_t base_command_size)
902fe25cc34SXiubo Li {
903fe25cc34SXiubo Li 	struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
904fe25cc34SXiubo Li 	size_t command_size;
905fe25cc34SXiubo Li 
906fe25cc34SXiubo Li 	command_size = base_command_size +
907fe25cc34SXiubo Li 		round_up(scsi_command_size(se_cmd->t_task_cdb),
908fe25cc34SXiubo Li 				TCMU_OP_ALIGN_SIZE);
909fe25cc34SXiubo Li 
910fe25cc34SXiubo Li 	WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1));
911fe25cc34SXiubo Li 
912fe25cc34SXiubo Li 	return command_size;
913fe25cc34SXiubo Li }
914fe25cc34SXiubo Li 
tcmu_setup_cmd_timer(struct tcmu_cmd * tcmu_cmd,unsigned int tmo,struct timer_list * timer)91561fb2482SBodo Stroesser static void tcmu_setup_cmd_timer(struct tcmu_cmd *tcmu_cmd, unsigned int tmo,
9169103575aSMike Christie 				 struct timer_list *timer)
9170d44374cSMike Christie {
9189103575aSMike Christie 	if (!tmo)
91961fb2482SBodo Stroesser 		return;
9209103575aSMike Christie 
9210d44374cSMike Christie 	tcmu_cmd->deadline = round_jiffies_up(jiffies + msecs_to_jiffies(tmo));
922a94a2572SXiubo Li 	if (!timer_pending(timer))
9239103575aSMike Christie 		mod_timer(timer, tcmu_cmd->deadline);
924a94a2572SXiubo Li 
92561fb2482SBodo Stroesser 	pr_debug("Timeout set up for cmd %p, dev = %s, tmo = %lu\n", tcmu_cmd,
92661fb2482SBodo Stroesser 		 tcmu_cmd->tcmu_dev->name, tmo / MSEC_PER_SEC);
9270d44374cSMike Christie }
9280d44374cSMike Christie 
add_to_qfull_queue(struct tcmu_cmd * tcmu_cmd)929a94a2572SXiubo Li static int add_to_qfull_queue(struct tcmu_cmd *tcmu_cmd)
930af1dd7ffSMike Christie {
931af1dd7ffSMike Christie 	struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
9329103575aSMike Christie 	unsigned int tmo;
933af1dd7ffSMike Christie 
9349103575aSMike Christie 	/*
9359103575aSMike Christie 	 * For backwards compat if qfull_time_out is not set use
9369103575aSMike Christie 	 * cmd_time_out and if that's not set use the default time out.
9379103575aSMike Christie 	 */
9389103575aSMike Christie 	if (!udev->qfull_time_out)
9399103575aSMike Christie 		return -ETIMEDOUT;
9409103575aSMike Christie 	else if (udev->qfull_time_out > 0)
9419103575aSMike Christie 		tmo = udev->qfull_time_out;
9429103575aSMike Christie 	else if (udev->cmd_time_out)
9439103575aSMike Christie 		tmo = udev->cmd_time_out;
9449103575aSMike Christie 	else
9459103575aSMike Christie 		tmo = TCMU_TIME_OUT;
9469103575aSMike Christie 
94761fb2482SBodo Stroesser 	tcmu_setup_cmd_timer(tcmu_cmd, tmo, &udev->qfull_timer);
948af1dd7ffSMike Christie 
949a94a2572SXiubo Li 	list_add_tail(&tcmu_cmd->queue_entry, &udev->qfull_queue);
95061fb2482SBodo Stroesser 	pr_debug("adding cmd %p on dev %s to ring space wait queue\n",
95161fb2482SBodo Stroesser 		 tcmu_cmd, udev->name);
952af1dd7ffSMike Christie 	return 0;
953af1dd7ffSMike Christie }
954af1dd7ffSMike Christie 
ring_insert_padding(struct tcmu_dev * udev,size_t cmd_size)9553d3f9d56SBodo Stroesser static uint32_t ring_insert_padding(struct tcmu_dev *udev, size_t cmd_size)
9563d3f9d56SBodo Stroesser {
9573d3f9d56SBodo Stroesser 	struct tcmu_cmd_entry_hdr *hdr;
9583d3f9d56SBodo Stroesser 	struct tcmu_mailbox *mb = udev->mb_addr;
9593d3f9d56SBodo Stroesser 	uint32_t cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
9603d3f9d56SBodo Stroesser 
9613d3f9d56SBodo Stroesser 	/* Insert a PAD if end-of-ring space is too small */
9623d3f9d56SBodo Stroesser 	if (head_to_end(cmd_head, udev->cmdr_size) < cmd_size) {
9633d3f9d56SBodo Stroesser 		size_t pad_size = head_to_end(cmd_head, udev->cmdr_size);
9643d3f9d56SBodo Stroesser 
965ecddbb7eSBodo Stroesser 		hdr = udev->cmdr + cmd_head;
9663d3f9d56SBodo Stroesser 		tcmu_hdr_set_op(&hdr->len_op, TCMU_OP_PAD);
9673d3f9d56SBodo Stroesser 		tcmu_hdr_set_len(&hdr->len_op, pad_size);
9683d3f9d56SBodo Stroesser 		hdr->cmd_id = 0; /* not used for PAD */
9693d3f9d56SBodo Stroesser 		hdr->kflags = 0;
9703d3f9d56SBodo Stroesser 		hdr->uflags = 0;
9713d3f9d56SBodo Stroesser 		tcmu_flush_dcache_range(hdr, sizeof(*hdr));
9723d3f9d56SBodo Stroesser 
9733d3f9d56SBodo Stroesser 		UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size);
9743d3f9d56SBodo Stroesser 		tcmu_flush_dcache_range(mb, sizeof(*mb));
9753d3f9d56SBodo Stroesser 
9763d3f9d56SBodo Stroesser 		cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
9773d3f9d56SBodo Stroesser 		WARN_ON(cmd_head != 0);
9783d3f9d56SBodo Stroesser 	}
9793d3f9d56SBodo Stroesser 
9803d3f9d56SBodo Stroesser 	return cmd_head;
9813d3f9d56SBodo Stroesser }
9823d3f9d56SBodo Stroesser 
tcmu_unplug_device(struct se_dev_plug * se_plug)9836888da81SMike Christie static void tcmu_unplug_device(struct se_dev_plug *se_plug)
9846888da81SMike Christie {
9856888da81SMike Christie 	struct se_device *se_dev = se_plug->se_dev;
9866888da81SMike Christie 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
9876888da81SMike Christie 
9883ac0fcb4SBodo Stroesser 	clear_bit(TCMU_DEV_BIT_PLUGGED, &udev->flags);
9896888da81SMike Christie 	uio_event_notify(&udev->uio_info);
9906888da81SMike Christie }
9916888da81SMike Christie 
tcmu_plug_device(struct se_device * se_dev)9926888da81SMike Christie static struct se_dev_plug *tcmu_plug_device(struct se_device *se_dev)
9936888da81SMike Christie {
9946888da81SMike Christie 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
9956888da81SMike Christie 
9963ac0fcb4SBodo Stroesser 	if (!test_and_set_bit(TCMU_DEV_BIT_PLUGGED, &udev->flags))
9976888da81SMike Christie 		return &udev->se_plug;
9986888da81SMike Christie 
9996888da81SMike Christie 	return NULL;
10006888da81SMike Christie }
10016888da81SMike Christie 
10026fd0ce79SMike Christie /**
10036fd0ce79SMike Christie  * queue_cmd_ring - queue cmd to ring or internally
10046fd0ce79SMike Christie  * @tcmu_cmd: cmd to queue
10056fd0ce79SMike Christie  * @scsi_err: TCM error code if failure (-1) returned.
10066fd0ce79SMike Christie  *
10076fd0ce79SMike Christie  * Returns:
10086fd0ce79SMike Christie  * -1 we cannot queue internally or to the ring.
10096fd0ce79SMike Christie  *  0 success
1010af1dd7ffSMike Christie  *  1 internally queued to wait for ring memory to free.
10116fd0ce79SMike Christie  */
queue_cmd_ring(struct tcmu_cmd * tcmu_cmd,sense_reason_t * scsi_err)1012e7f41104SBart Van Assche static int queue_cmd_ring(struct tcmu_cmd *tcmu_cmd, sense_reason_t *scsi_err)
10137c9e7a6fSAndy Grover {
10147c9e7a6fSAndy Grover 	struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
10157c9e7a6fSAndy Grover 	struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
10167c9e7a6fSAndy Grover 	size_t base_command_size, command_size;
10173d3f9d56SBodo Stroesser 	struct tcmu_mailbox *mb = udev->mb_addr;
10187c9e7a6fSAndy Grover 	struct tcmu_cmd_entry *entry;
10197c9e7a6fSAndy Grover 	struct iovec *iov;
1020d3cbb743SBodo Stroesser 	int iov_cnt, iov_bidi_cnt;
1021d3cbb743SBodo Stroesser 	uint32_t cmd_id, cmd_head;
10227c9e7a6fSAndy Grover 	uint64_t cdb_off;
1023e719afdcSBodo Stroesser 	uint32_t blk_size = udev->data_blk_size;
102452ef2743SBodo Stroesser 	/* size of data buffer needed */
1025e719afdcSBodo Stroesser 	size_t data_length = (size_t)tcmu_cmd->dbi_cnt * blk_size;
10267c9e7a6fSAndy Grover 
10276fd0ce79SMike Christie 	*scsi_err = TCM_NO_SENSE;
10286fd0ce79SMike Christie 
1029892782caSMike Christie 	if (test_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags)) {
1030892782caSMike Christie 		*scsi_err = TCM_LUN_BUSY;
1031892782caSMike Christie 		return -1;
1032892782caSMike Christie 	}
1033892782caSMike Christie 
10346fd0ce79SMike Christie 	if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) {
10356fd0ce79SMike Christie 		*scsi_err = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
10366fd0ce79SMike Christie 		return -1;
10376fd0ce79SMike Christie 	}
10387c9e7a6fSAndy Grover 
1039a94a2572SXiubo Li 	if (!list_empty(&udev->qfull_queue))
1040af1dd7ffSMike Christie 		goto queue;
10417c9e7a6fSAndy Grover 
1042e719afdcSBodo Stroesser 	if (data_length > (size_t)udev->max_blocks * blk_size) {
10437e98905eSBodo Stroesser 		pr_warn("TCMU: Request of size %zu is too big for %zu data area\n",
1044e719afdcSBodo Stroesser 			data_length, (size_t)udev->max_blocks * blk_size);
10456fd0ce79SMike Christie 		*scsi_err = TCM_INVALID_CDB_FIELD;
10466fd0ce79SMike Christie 		return -1;
1047554617b2SAndy Grover 	}
10487c9e7a6fSAndy Grover 
10497e98905eSBodo Stroesser 	iov_cnt = tcmu_alloc_data_space(udev, tcmu_cmd, &iov_bidi_cnt);
10507e98905eSBodo Stroesser 	if (iov_cnt < 0)
10517e98905eSBodo Stroesser 		goto free_and_queue;
10527e98905eSBodo Stroesser 
10537e98905eSBodo Stroesser 	/*
10547e98905eSBodo Stroesser 	 * Must be a certain minimum size for response sense info, but
10557e98905eSBodo Stroesser 	 * also may be larger if the iov array is large.
10567e98905eSBodo Stroesser 	 */
10577e98905eSBodo Stroesser 	base_command_size = tcmu_cmd_get_base_cmd_size(iov_cnt);
10587e98905eSBodo Stroesser 	command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size);
10597e98905eSBodo Stroesser 
10607e98905eSBodo Stroesser 	if (command_size > (udev->cmdr_size / 2)) {
10617e98905eSBodo Stroesser 		pr_warn("TCMU: Request of size %zu is too big for %u cmd ring\n",
10627e98905eSBodo Stroesser 			command_size, udev->cmdr_size);
10637e98905eSBodo Stroesser 		tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cur);
10647e98905eSBodo Stroesser 		*scsi_err = TCM_INVALID_CDB_FIELD;
10657e98905eSBodo Stroesser 		return -1;
10667e98905eSBodo Stroesser 	}
10677e98905eSBodo Stroesser 
10687e98905eSBodo Stroesser 	if (!is_ring_space_avail(udev, command_size))
1069810b8153SMike Christie 		/*
1070810b8153SMike Christie 		 * Don't leave commands partially setup because the unmap
1071810b8153SMike Christie 		 * thread might need the blocks to make forward progress.
1072810b8153SMike Christie 		 */
10737e98905eSBodo Stroesser 		goto free_and_queue;
107426418649SSheng Yang 
1075d3cbb743SBodo Stroesser 	if (xa_alloc(&udev->commands, &cmd_id, tcmu_cmd, XA_LIMIT(1, 0xffff),
1076d3cbb743SBodo Stroesser 		     GFP_NOWAIT) < 0) {
107761fb2482SBodo Stroesser 		pr_err("tcmu: Could not allocate cmd id.\n");
10786fd0ce79SMike Christie 
107961fb2482SBodo Stroesser 		tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt);
10806fd0ce79SMike Christie 		*scsi_err = TCM_OUT_OF_RESOURCES;
10816fd0ce79SMike Christie 		return -1;
10820d44374cSMike Christie 	}
108361fb2482SBodo Stroesser 	tcmu_cmd->cmd_id = cmd_id;
108461fb2482SBodo Stroesser 
108561fb2482SBodo Stroesser 	pr_debug("allocated cmd id %u for cmd %p dev %s\n", tcmu_cmd->cmd_id,
108661fb2482SBodo Stroesser 		 tcmu_cmd, udev->name);
108761fb2482SBodo Stroesser 
10887e98905eSBodo Stroesser 	cmd_head = ring_insert_padding(udev, command_size);
10897e98905eSBodo Stroesser 
1090ecddbb7eSBodo Stroesser 	entry = udev->cmdr + cmd_head;
10917e98905eSBodo Stroesser 	memset(entry, 0, command_size);
10927e98905eSBodo Stroesser 	tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD);
10937e98905eSBodo Stroesser 
10947e98905eSBodo Stroesser 	/* prepare iov list and copy data to data area if necessary */
10957e98905eSBodo Stroesser 	tcmu_cmd_reset_dbi_cur(tcmu_cmd);
10967e98905eSBodo Stroesser 	iov = &entry->req.iov[0];
10973c9a7c58SBodo Stroesser 
10983c9a7c58SBodo Stroesser 	if (se_cmd->data_direction == DMA_TO_DEVICE ||
10993c9a7c58SBodo Stroesser 	    se_cmd->se_cmd_flags & SCF_BIDI)
11003c9a7c58SBodo Stroesser 		scatter_data_area(udev, tcmu_cmd, &iov);
11013c9a7c58SBodo Stroesser 	else
11023c9a7c58SBodo Stroesser 		tcmu_setup_iovs(udev, tcmu_cmd, &iov, se_cmd->data_length);
11033c9a7c58SBodo Stroesser 
11047e98905eSBodo Stroesser 	entry->req.iov_cnt = iov_cnt - iov_bidi_cnt;
11057e98905eSBodo Stroesser 
11067e98905eSBodo Stroesser 	/* Handle BIDI commands */
11077e98905eSBodo Stroesser 	if (se_cmd->se_cmd_flags & SCF_BIDI) {
11087e98905eSBodo Stroesser 		iov++;
11093c9a7c58SBodo Stroesser 		tcmu_setup_iovs(udev, tcmu_cmd, &iov, tcmu_cmd->data_len_bidi);
11107e98905eSBodo Stroesser 		entry->req.iov_bidi_cnt = iov_bidi_cnt;
11117e98905eSBodo Stroesser 	}
11127e98905eSBodo Stroesser 
111361fb2482SBodo Stroesser 	tcmu_setup_cmd_timer(tcmu_cmd, udev->cmd_time_out, &udev->cmd_timer);
111461fb2482SBodo Stroesser 
11150d44374cSMike Christie 	entry->hdr.cmd_id = tcmu_cmd->cmd_id;
11160d44374cSMike Christie 
1117fe25cc34SXiubo Li 	tcmu_hdr_set_len(&entry->hdr.len_op, command_size);
1118fe25cc34SXiubo Li 
11197c9e7a6fSAndy Grover 	/* All offsets relative to mb_addr, not start of entry! */
11207c9e7a6fSAndy Grover 	cdb_off = CMDR_OFF + cmd_head + base_command_size;
11217c9e7a6fSAndy Grover 	memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb));
11227c9e7a6fSAndy Grover 	entry->req.cdb_off = cdb_off;
11238c4e0f21SBodo Stroesser 	tcmu_flush_dcache_range(entry, command_size);
11247c9e7a6fSAndy Grover 
11257c9e7a6fSAndy Grover 	UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size);
11267c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(mb, sizeof(*mb));
11277c9e7a6fSAndy Grover 
1128a94a2572SXiubo Li 	list_add_tail(&tcmu_cmd->queue_entry, &udev->inflight_queue);
1129a94a2572SXiubo Li 
11303ac0fcb4SBodo Stroesser 	if (!test_bit(TCMU_DEV_BIT_PLUGGED, &udev->flags))
11317c9e7a6fSAndy Grover 		uio_event_notify(&udev->uio_info);
11327c9e7a6fSAndy Grover 
11336fd0ce79SMike Christie 	return 0;
1134af1dd7ffSMike Christie 
11357e98905eSBodo Stroesser free_and_queue:
11367e98905eSBodo Stroesser 	tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cur);
11377e98905eSBodo Stroesser 	tcmu_cmd_reset_dbi_cur(tcmu_cmd);
11387e98905eSBodo Stroesser 
1139af1dd7ffSMike Christie queue:
1140a94a2572SXiubo Li 	if (add_to_qfull_queue(tcmu_cmd)) {
1141af1dd7ffSMike Christie 		*scsi_err = TCM_OUT_OF_RESOURCES;
1142af1dd7ffSMike Christie 		return -1;
1143af1dd7ffSMike Christie 	}
1144af1dd7ffSMike Christie 
1145af1dd7ffSMike Christie 	return 1;
11467c9e7a6fSAndy Grover }
11477c9e7a6fSAndy Grover 
1148bc2d214aSBodo Stroesser /**
1149bc2d214aSBodo Stroesser  * queue_tmr_ring - queue tmr info to ring or internally
1150bc2d214aSBodo Stroesser  * @udev: related tcmu_dev
1151bc2d214aSBodo Stroesser  * @tmr: tcmu_tmr containing tmr info to queue
1152bc2d214aSBodo Stroesser  *
1153bc2d214aSBodo Stroesser  * Returns:
1154bc2d214aSBodo Stroesser  *  0 success
1155bc2d214aSBodo Stroesser  *  1 internally queued to wait for ring memory to free.
1156bc2d214aSBodo Stroesser  */
1157bc2d214aSBodo Stroesser static int
queue_tmr_ring(struct tcmu_dev * udev,struct tcmu_tmr * tmr)1158bc2d214aSBodo Stroesser queue_tmr_ring(struct tcmu_dev *udev, struct tcmu_tmr *tmr)
1159bc2d214aSBodo Stroesser {
1160bc2d214aSBodo Stroesser 	struct tcmu_tmr_entry *entry;
1161bc2d214aSBodo Stroesser 	int cmd_size;
1162bc2d214aSBodo Stroesser 	int id_list_sz;
1163bc2d214aSBodo Stroesser 	struct tcmu_mailbox *mb = udev->mb_addr;
1164bc2d214aSBodo Stroesser 	uint32_t cmd_head;
1165bc2d214aSBodo Stroesser 
1166bc2d214aSBodo Stroesser 	if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags))
1167bc2d214aSBodo Stroesser 		goto out_free;
1168bc2d214aSBodo Stroesser 
1169bc2d214aSBodo Stroesser 	id_list_sz = sizeof(tmr->tmr_cmd_ids[0]) * tmr->tmr_cmd_cnt;
1170bc2d214aSBodo Stroesser 	cmd_size = round_up(sizeof(*entry) + id_list_sz, TCMU_OP_ALIGN_SIZE);
1171bc2d214aSBodo Stroesser 
1172bc2d214aSBodo Stroesser 	if (!list_empty(&udev->tmr_queue) ||
11737e98905eSBodo Stroesser 	    !is_ring_space_avail(udev, cmd_size)) {
1174bc2d214aSBodo Stroesser 		list_add_tail(&tmr->queue_entry, &udev->tmr_queue);
1175bc2d214aSBodo Stroesser 		pr_debug("adding tmr %p on dev %s to TMR ring space wait queue\n",
1176bc2d214aSBodo Stroesser 			 tmr, udev->name);
1177bc2d214aSBodo Stroesser 		return 1;
1178bc2d214aSBodo Stroesser 	}
1179bc2d214aSBodo Stroesser 
1180bc2d214aSBodo Stroesser 	cmd_head = ring_insert_padding(udev, cmd_size);
1181bc2d214aSBodo Stroesser 
1182ecddbb7eSBodo Stroesser 	entry = udev->cmdr + cmd_head;
1183bc2d214aSBodo Stroesser 	memset(entry, 0, cmd_size);
1184bc2d214aSBodo Stroesser 	tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_TMR);
1185bc2d214aSBodo Stroesser 	tcmu_hdr_set_len(&entry->hdr.len_op, cmd_size);
1186bc2d214aSBodo Stroesser 	entry->tmr_type = tmr->tmr_type;
1187bc2d214aSBodo Stroesser 	entry->cmd_cnt = tmr->tmr_cmd_cnt;
1188bc2d214aSBodo Stroesser 	memcpy(&entry->cmd_ids[0], &tmr->tmr_cmd_ids[0], id_list_sz);
1189bc2d214aSBodo Stroesser 	tcmu_flush_dcache_range(entry, cmd_size);
1190bc2d214aSBodo Stroesser 
1191bc2d214aSBodo Stroesser 	UPDATE_HEAD(mb->cmd_head, cmd_size, udev->cmdr_size);
1192bc2d214aSBodo Stroesser 	tcmu_flush_dcache_range(mb, sizeof(*mb));
1193bc2d214aSBodo Stroesser 
1194bc2d214aSBodo Stroesser 	uio_event_notify(&udev->uio_info);
1195bc2d214aSBodo Stroesser 
1196bc2d214aSBodo Stroesser out_free:
1197bc2d214aSBodo Stroesser 	kfree(tmr);
1198bc2d214aSBodo Stroesser 
1199bc2d214aSBodo Stroesser 	return 0;
1200bc2d214aSBodo Stroesser }
1201bc2d214aSBodo Stroesser 
120202eb924fSAndy Grover static sense_reason_t
tcmu_queue_cmd(struct se_cmd * se_cmd)120302eb924fSAndy Grover tcmu_queue_cmd(struct se_cmd *se_cmd)
12047c9e7a6fSAndy Grover {
1205af1dd7ffSMike Christie 	struct se_device *se_dev = se_cmd->se_dev;
1206af1dd7ffSMike Christie 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
12077c9e7a6fSAndy Grover 	struct tcmu_cmd *tcmu_cmd;
1208c9684927SBodo Stroesser 	sense_reason_t scsi_ret = TCM_CHECK_CONDITION_ABORT_CMD;
1209c9684927SBodo Stroesser 	int ret = -1;
12107c9e7a6fSAndy Grover 
12117c9e7a6fSAndy Grover 	tcmu_cmd = tcmu_alloc_cmd(se_cmd);
12127c9e7a6fSAndy Grover 	if (!tcmu_cmd)
121302eb924fSAndy Grover 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
12147c9e7a6fSAndy Grover 
1215af1dd7ffSMike Christie 	mutex_lock(&udev->cmdr_lock);
1216c9684927SBodo Stroesser 	if (!(se_cmd->transport_state & CMD_T_ABORTED))
1217af1dd7ffSMike Christie 		ret = queue_cmd_ring(tcmu_cmd, &scsi_ret);
1218af1dd7ffSMike Christie 	if (ret < 0)
1219141685a3SXiubo Li 		tcmu_free_cmd(tcmu_cmd);
1220780e1384SShin'ichiro Kawasaki 	else
1221780e1384SShin'ichiro Kawasaki 		se_cmd->priv = tcmu_cmd;
1222a3512902SBodo Stroesser 	mutex_unlock(&udev->cmdr_lock);
12236fd0ce79SMike Christie 	return scsi_ret;
12247c9e7a6fSAndy Grover }
12257c9e7a6fSAndy Grover 
tcmu_set_next_deadline(struct list_head * queue,struct timer_list * timer)1226ed212ca8SBodo Stroesser static void tcmu_set_next_deadline(struct list_head *queue,
1227ed212ca8SBodo Stroesser 				   struct timer_list *timer)
1228ed212ca8SBodo Stroesser {
1229ed212ca8SBodo Stroesser 	struct tcmu_cmd *cmd;
1230ed212ca8SBodo Stroesser 
1231ed212ca8SBodo Stroesser 	if (!list_empty(queue)) {
1232ed212ca8SBodo Stroesser 		cmd = list_first_entry(queue, struct tcmu_cmd, queue_entry);
1233ed212ca8SBodo Stroesser 		mod_timer(timer, cmd->deadline);
1234ed212ca8SBodo Stroesser 	} else
1235ed212ca8SBodo Stroesser 		del_timer(timer);
1236ed212ca8SBodo Stroesser }
1237ed212ca8SBodo Stroesser 
1238bc2d214aSBodo Stroesser static int
tcmu_tmr_type(enum tcm_tmreq_table tmf)1239bc2d214aSBodo Stroesser tcmu_tmr_type(enum tcm_tmreq_table tmf)
1240bc2d214aSBodo Stroesser {
1241bc2d214aSBodo Stroesser 	switch (tmf) {
1242bc2d214aSBodo Stroesser 	case TMR_ABORT_TASK:		return TCMU_TMR_ABORT_TASK;
1243bc2d214aSBodo Stroesser 	case TMR_ABORT_TASK_SET:	return TCMU_TMR_ABORT_TASK_SET;
1244bc2d214aSBodo Stroesser 	case TMR_CLEAR_ACA:		return TCMU_TMR_CLEAR_ACA;
1245bc2d214aSBodo Stroesser 	case TMR_CLEAR_TASK_SET:	return TCMU_TMR_CLEAR_TASK_SET;
1246bc2d214aSBodo Stroesser 	case TMR_LUN_RESET:		return TCMU_TMR_LUN_RESET;
1247bc2d214aSBodo Stroesser 	case TMR_TARGET_WARM_RESET:	return TCMU_TMR_TARGET_WARM_RESET;
1248bc2d214aSBodo Stroesser 	case TMR_TARGET_COLD_RESET:	return TCMU_TMR_TARGET_COLD_RESET;
1249bc2d214aSBodo Stroesser 	case TMR_LUN_RESET_PRO:		return TCMU_TMR_LUN_RESET_PRO;
1250bc2d214aSBodo Stroesser 	default:			return TCMU_TMR_UNKNOWN;
1251bc2d214aSBodo Stroesser 	}
1252bc2d214aSBodo Stroesser }
1253bc2d214aSBodo Stroesser 
1254bc2d214aSBodo Stroesser static void
tcmu_tmr_notify(struct se_device * se_dev,enum tcm_tmreq_table tmf,struct list_head * cmd_list)1255bc2d214aSBodo Stroesser tcmu_tmr_notify(struct se_device *se_dev, enum tcm_tmreq_table tmf,
1256bc2d214aSBodo Stroesser 		struct list_head *cmd_list)
1257bc2d214aSBodo Stroesser {
1258bc2d214aSBodo Stroesser 	int i = 0, cmd_cnt = 0;
1259bc2d214aSBodo Stroesser 	bool unqueued = false;
1260bc2d214aSBodo Stroesser 	struct tcmu_cmd *cmd;
1261bc2d214aSBodo Stroesser 	struct se_cmd *se_cmd;
1262bc2d214aSBodo Stroesser 	struct tcmu_tmr *tmr;
1263bc2d214aSBodo Stroesser 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
1264bc2d214aSBodo Stroesser 
1265bc2d214aSBodo Stroesser 	mutex_lock(&udev->cmdr_lock);
1266bc2d214aSBodo Stroesser 
1267bc2d214aSBodo Stroesser 	/* First we check for aborted commands in qfull_queue */
1268bc2d214aSBodo Stroesser 	list_for_each_entry(se_cmd, cmd_list, state_list) {
1269bc2d214aSBodo Stroesser 		i++;
1270bc2d214aSBodo Stroesser 		if (!se_cmd->priv)
1271bc2d214aSBodo Stroesser 			continue;
1272bc2d214aSBodo Stroesser 		cmd = se_cmd->priv;
1273bc2d214aSBodo Stroesser 		/* Commands on qfull queue have no id yet */
1274bc2d214aSBodo Stroesser 		if (cmd->cmd_id) {
1275bc2d214aSBodo Stroesser 			cmd_cnt++;
1276bc2d214aSBodo Stroesser 			continue;
1277bc2d214aSBodo Stroesser 		}
1278bc2d214aSBodo Stroesser 		pr_debug("Removing aborted command %p from queue on dev %s.\n",
1279bc2d214aSBodo Stroesser 			 cmd, udev->name);
1280bc2d214aSBodo Stroesser 
1281bc2d214aSBodo Stroesser 		list_del_init(&cmd->queue_entry);
1282bc2d214aSBodo Stroesser 		tcmu_free_cmd(cmd);
1283780e1384SShin'ichiro Kawasaki 		se_cmd->priv = NULL;
1284bc2d214aSBodo Stroesser 		target_complete_cmd(se_cmd, SAM_STAT_TASK_ABORTED);
1285bc2d214aSBodo Stroesser 		unqueued = true;
1286bc2d214aSBodo Stroesser 	}
1287bc2d214aSBodo Stroesser 	if (unqueued)
1288bc2d214aSBodo Stroesser 		tcmu_set_next_deadline(&udev->qfull_queue, &udev->qfull_timer);
1289bc2d214aSBodo Stroesser 
129059526d7aSBodo Stroesser 	if (!test_bit(TCMU_DEV_BIT_TMR_NOTIFY, &udev->flags))
129159526d7aSBodo Stroesser 		goto unlock;
129259526d7aSBodo Stroesser 
1293bc2d214aSBodo Stroesser 	pr_debug("TMR event %d on dev %s, aborted cmds %d, afflicted cmd_ids %d\n",
1294bc2d214aSBodo Stroesser 		 tcmu_tmr_type(tmf), udev->name, i, cmd_cnt);
1295bc2d214aSBodo Stroesser 
1296c20bda34SGustavo A. R. Silva 	tmr = kmalloc(struct_size(tmr, tmr_cmd_ids, cmd_cnt), GFP_NOIO);
1297bc2d214aSBodo Stroesser 	if (!tmr)
1298bc2d214aSBodo Stroesser 		goto unlock;
1299bc2d214aSBodo Stroesser 
1300bc2d214aSBodo Stroesser 	tmr->tmr_type = tcmu_tmr_type(tmf);
1301bc2d214aSBodo Stroesser 	tmr->tmr_cmd_cnt = cmd_cnt;
1302bc2d214aSBodo Stroesser 
1303bc2d214aSBodo Stroesser 	if (cmd_cnt != 0) {
1304bc2d214aSBodo Stroesser 		cmd_cnt = 0;
1305bc2d214aSBodo Stroesser 		list_for_each_entry(se_cmd, cmd_list, state_list) {
1306bc2d214aSBodo Stroesser 			if (!se_cmd->priv)
1307bc2d214aSBodo Stroesser 				continue;
1308bc2d214aSBodo Stroesser 			cmd = se_cmd->priv;
1309bc2d214aSBodo Stroesser 			if (cmd->cmd_id)
1310bc2d214aSBodo Stroesser 				tmr->tmr_cmd_ids[cmd_cnt++] = cmd->cmd_id;
1311bc2d214aSBodo Stroesser 		}
1312bc2d214aSBodo Stroesser 	}
1313bc2d214aSBodo Stroesser 
1314bc2d214aSBodo Stroesser 	queue_tmr_ring(udev, tmr);
1315bc2d214aSBodo Stroesser 
1316bc2d214aSBodo Stroesser unlock:
1317bc2d214aSBodo Stroesser 	mutex_unlock(&udev->cmdr_lock);
1318bc2d214aSBodo Stroesser }
1319bc2d214aSBodo Stroesser 
tcmu_handle_completion(struct tcmu_cmd * cmd,struct tcmu_cmd_entry * entry,bool keep_buf)1320018c1491SBodo Stroesser static bool tcmu_handle_completion(struct tcmu_cmd *cmd,
1321018c1491SBodo Stroesser 				   struct tcmu_cmd_entry *entry, bool keep_buf)
13227c9e7a6fSAndy Grover {
13237c9e7a6fSAndy Grover 	struct se_cmd *se_cmd = cmd->se_cmd;
13247c9e7a6fSAndy Grover 	struct tcmu_dev *udev = cmd->tcmu_dev;
13256c3796d1Sbstroesser@ts.fujitsu.com 	bool read_len_valid = false;
1326018c1491SBodo Stroesser 	bool ret = true;
1327a86a7586SDmitry Fomichev 	uint32_t read_len;
13287c9e7a6fSAndy Grover 
1329b25c7863SSheng Yang 	/*
1330b25c7863SSheng Yang 	 * cmd has been completed already from timeout, just reclaim
13313d9b9555SAndy Grover 	 * data area space and free cmd
1332b25c7863SSheng Yang 	 */
1333a86a7586SDmitry Fomichev 	if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
1334a86a7586SDmitry Fomichev 		WARN_ON_ONCE(se_cmd);
1335141685a3SXiubo Li 		goto out;
1336a86a7586SDmitry Fomichev 	}
1337018c1491SBodo Stroesser 	if (test_bit(TCMU_CMD_BIT_KEEP_BUF, &cmd->flags)) {
1338018c1491SBodo Stroesser 		pr_err("cmd_id %u already completed with KEEP_BUF, ring is broken\n",
1339018c1491SBodo Stroesser 		       entry->hdr.cmd_id);
1340018c1491SBodo Stroesser 		set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags);
1341018c1491SBodo Stroesser 		ret = false;
1342018c1491SBodo Stroesser 		goto out;
1343018c1491SBodo Stroesser 	}
1344b25c7863SSheng Yang 
1345a94a2572SXiubo Li 	list_del_init(&cmd->queue_entry);
1346a94a2572SXiubo Li 
1347141685a3SXiubo Li 	tcmu_cmd_reset_dbi_cur(cmd);
13487c9e7a6fSAndy Grover 
13490ad46af8SAndy Grover 	if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) {
13500ad46af8SAndy Grover 		pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n",
13510ad46af8SAndy Grover 			cmd->se_cmd);
1352ed97d0cdSAndy Grover 		entry->rsp.scsi_status = SAM_STAT_CHECK_CONDITION;
13536c3796d1Sbstroesser@ts.fujitsu.com 		goto done;
13546c3796d1Sbstroesser@ts.fujitsu.com 	}
13556c3796d1Sbstroesser@ts.fujitsu.com 
1356a86a7586SDmitry Fomichev 	read_len = se_cmd->data_length;
13576c3796d1Sbstroesser@ts.fujitsu.com 	if (se_cmd->data_direction == DMA_FROM_DEVICE &&
13586c3796d1Sbstroesser@ts.fujitsu.com 	    (entry->hdr.uflags & TCMU_UFLAG_READ_LEN) && entry->rsp.read_len) {
13596c3796d1Sbstroesser@ts.fujitsu.com 		read_len_valid = true;
13606c3796d1Sbstroesser@ts.fujitsu.com 		if (entry->rsp.read_len < read_len)
13616c3796d1Sbstroesser@ts.fujitsu.com 			read_len = entry->rsp.read_len;
13626c3796d1Sbstroesser@ts.fujitsu.com 	}
13636c3796d1Sbstroesser@ts.fujitsu.com 
13646c3796d1Sbstroesser@ts.fujitsu.com 	if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) {
1365406f74c2SMike Christie 		transport_copy_sense_to_cmd(se_cmd, entry->rsp.sense_buffer);
13666c3796d1Sbstroesser@ts.fujitsu.com 		if (!read_len_valid )
13676c3796d1Sbstroesser@ts.fujitsu.com 			goto done;
13686c3796d1Sbstroesser@ts.fujitsu.com 		else
13696c3796d1Sbstroesser@ts.fujitsu.com 			se_cmd->se_cmd_flags |= SCF_TREAT_READ_AS_NORMAL;
13706c3796d1Sbstroesser@ts.fujitsu.com 	}
13716c3796d1Sbstroesser@ts.fujitsu.com 	if (se_cmd->se_cmd_flags & SCF_BIDI) {
137226418649SSheng Yang 		/* Get Data-In buffer before clean up */
13736c3796d1Sbstroesser@ts.fujitsu.com 		gather_data_area(udev, cmd, true, read_len);
1374e4648b01SIlias Tsitsimpis 	} else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
13756c3796d1Sbstroesser@ts.fujitsu.com 		gather_data_area(udev, cmd, false, read_len);
13767c9e7a6fSAndy Grover 	} else if (se_cmd->data_direction == DMA_TO_DEVICE) {
1377141685a3SXiubo Li 		/* TODO: */
13782bc396a2SIlias Tsitsimpis 	} else if (se_cmd->data_direction != DMA_NONE) {
13792bc396a2SIlias Tsitsimpis 		pr_warn("TCMU: data direction was %d!\n",
13802bc396a2SIlias Tsitsimpis 			se_cmd->data_direction);
13817c9e7a6fSAndy Grover 	}
13827c9e7a6fSAndy Grover 
13836c3796d1Sbstroesser@ts.fujitsu.com done:
1384780e1384SShin'ichiro Kawasaki 	se_cmd->priv = NULL;
13856c3796d1Sbstroesser@ts.fujitsu.com 	if (read_len_valid) {
13866c3796d1Sbstroesser@ts.fujitsu.com 		pr_debug("read_len = %d\n", read_len);
13876c3796d1Sbstroesser@ts.fujitsu.com 		target_complete_cmd_with_length(cmd->se_cmd,
13886c3796d1Sbstroesser@ts.fujitsu.com 					entry->rsp.scsi_status, read_len);
13896c3796d1Sbstroesser@ts.fujitsu.com 	} else
13907c9e7a6fSAndy Grover 		target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status);
13917c9e7a6fSAndy Grover 
1392141685a3SXiubo Li out:
1393018c1491SBodo Stroesser 	if (!keep_buf) {
1394b6df4b79SXiubo Li 		tcmu_cmd_free_data(cmd, cmd->dbi_cnt);
1395141685a3SXiubo Li 		tcmu_free_cmd(cmd);
1396018c1491SBodo Stroesser 	} else {
1397018c1491SBodo Stroesser 		/*
1398018c1491SBodo Stroesser 		 * Keep this command after completion, since userspace still
1399018c1491SBodo Stroesser 		 * needs the data buffer. Mark it with TCMU_CMD_BIT_KEEP_BUF
1400018c1491SBodo Stroesser 		 * and reset potential TCMU_CMD_BIT_EXPIRED, so we don't accept
1401018c1491SBodo Stroesser 		 * a second completion later.
1402018c1491SBodo Stroesser 		 * Userspace can free the buffer later by writing the cmd_id
1403018c1491SBodo Stroesser 		 * to new action attribute free_kept_buf.
1404018c1491SBodo Stroesser 		 */
1405018c1491SBodo Stroesser 		clear_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags);
1406018c1491SBodo Stroesser 		set_bit(TCMU_CMD_BIT_KEEP_BUF, &cmd->flags);
1407018c1491SBodo Stroesser 	}
1408018c1491SBodo Stroesser 	return ret;
14097c9e7a6fSAndy Grover }
14107c9e7a6fSAndy Grover 
tcmu_run_tmr_queue(struct tcmu_dev * udev)1411bc2d214aSBodo Stroesser static int tcmu_run_tmr_queue(struct tcmu_dev *udev)
1412bc2d214aSBodo Stroesser {
1413bc2d214aSBodo Stroesser 	struct tcmu_tmr *tmr, *tmp;
1414bc2d214aSBodo Stroesser 	LIST_HEAD(tmrs);
1415bc2d214aSBodo Stroesser 
1416bc2d214aSBodo Stroesser 	if (list_empty(&udev->tmr_queue))
1417bc2d214aSBodo Stroesser 		return 1;
1418bc2d214aSBodo Stroesser 
1419bc2d214aSBodo Stroesser 	pr_debug("running %s's tmr queue\n", udev->name);
1420bc2d214aSBodo Stroesser 
1421bc2d214aSBodo Stroesser 	list_splice_init(&udev->tmr_queue, &tmrs);
1422bc2d214aSBodo Stroesser 
1423bc2d214aSBodo Stroesser 	list_for_each_entry_safe(tmr, tmp, &tmrs, queue_entry) {
1424bc2d214aSBodo Stroesser 		list_del_init(&tmr->queue_entry);
1425bc2d214aSBodo Stroesser 
1426bc2d214aSBodo Stroesser 		pr_debug("removing tmr %p on dev %s from queue\n",
1427bc2d214aSBodo Stroesser 			 tmr, udev->name);
1428bc2d214aSBodo Stroesser 
1429bc2d214aSBodo Stroesser 		if (queue_tmr_ring(udev, tmr)) {
1430bc2d214aSBodo Stroesser 			pr_debug("ran out of space during tmr queue run\n");
1431bc2d214aSBodo Stroesser 			/*
1432bc2d214aSBodo Stroesser 			 * tmr was requeued, so just put all tmrs back in
1433bc2d214aSBodo Stroesser 			 * the queue
1434bc2d214aSBodo Stroesser 			 */
1435bc2d214aSBodo Stroesser 			list_splice_tail(&tmrs, &udev->tmr_queue);
1436bc2d214aSBodo Stroesser 			return 0;
1437bc2d214aSBodo Stroesser 		}
1438bc2d214aSBodo Stroesser 	}
1439bc2d214aSBodo Stroesser 
1440bc2d214aSBodo Stroesser 	return 1;
1441bc2d214aSBodo Stroesser }
1442bc2d214aSBodo Stroesser 
tcmu_handle_completions(struct tcmu_dev * udev)14439814b55cSBodo Stroesser static bool tcmu_handle_completions(struct tcmu_dev *udev)
14447c9e7a6fSAndy Grover {
14457c9e7a6fSAndy Grover 	struct tcmu_mailbox *mb;
1446a94a2572SXiubo Li 	struct tcmu_cmd *cmd;
1447bc2d214aSBodo Stroesser 	bool free_space = false;
14487c9e7a6fSAndy Grover 
14497c9e7a6fSAndy Grover 	if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) {
14507c9e7a6fSAndy Grover 		pr_err("ring broken, not handling completions\n");
145182473125Skernel test robot 		return false;
14527c9e7a6fSAndy Grover 	}
14537c9e7a6fSAndy Grover 
14547c9e7a6fSAndy Grover 	mb = udev->mb_addr;
14557c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(mb, sizeof(*mb));
14567c9e7a6fSAndy Grover 
14576aa7de05SMark Rutland 	while (udev->cmdr_last_cleaned != READ_ONCE(mb->cmd_tail)) {
14587c9e7a6fSAndy Grover 
1459ecddbb7eSBodo Stroesser 		struct tcmu_cmd_entry *entry = udev->cmdr + udev->cmdr_last_cleaned;
1460018c1491SBodo Stroesser 		bool keep_buf;
14617c9e7a6fSAndy Grover 
14625a0c256dSBodo Stroesser 		/*
14635a0c256dSBodo Stroesser 		 * Flush max. up to end of cmd ring since current entry might
14645a0c256dSBodo Stroesser 		 * be a padding that is shorter than sizeof(*entry)
14655a0c256dSBodo Stroesser 		 */
14665a0c256dSBodo Stroesser 		size_t ring_left = head_to_end(udev->cmdr_last_cleaned,
14675a0c256dSBodo Stroesser 					       udev->cmdr_size);
14685a0c256dSBodo Stroesser 		tcmu_flush_dcache_range(entry, ring_left < sizeof(*entry) ?
14695a0c256dSBodo Stroesser 					ring_left : sizeof(*entry));
14707c9e7a6fSAndy Grover 
1471bc2d214aSBodo Stroesser 		free_space = true;
1472bc2d214aSBodo Stroesser 
1473bc2d214aSBodo Stroesser 		if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD ||
1474bc2d214aSBodo Stroesser 		    tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_TMR) {
14750ad46af8SAndy Grover 			UPDATE_HEAD(udev->cmdr_last_cleaned,
14760ad46af8SAndy Grover 				    tcmu_hdr_get_len(entry->hdr.len_op),
14770ad46af8SAndy Grover 				    udev->cmdr_size);
14787c9e7a6fSAndy Grover 			continue;
14797c9e7a6fSAndy Grover 		}
14800ad46af8SAndy Grover 		WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD);
14817c9e7a6fSAndy Grover 
1482018c1491SBodo Stroesser 		keep_buf = !!(entry->hdr.uflags & TCMU_UFLAG_KEEP_BUF);
1483018c1491SBodo Stroesser 		if (keep_buf)
1484018c1491SBodo Stroesser 			cmd = xa_load(&udev->commands, entry->hdr.cmd_id);
1485018c1491SBodo Stroesser 		else
1486d3cbb743SBodo Stroesser 			cmd = xa_erase(&udev->commands, entry->hdr.cmd_id);
14877c9e7a6fSAndy Grover 		if (!cmd) {
148888cf1073SMike Christie 			pr_err("cmd_id %u not found, ring is broken\n",
148988cf1073SMike Christie 			       entry->hdr.cmd_id);
14907c9e7a6fSAndy Grover 			set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags);
14919814b55cSBodo Stroesser 			return false;
14927c9e7a6fSAndy Grover 		}
14937c9e7a6fSAndy Grover 
1494018c1491SBodo Stroesser 		if (!tcmu_handle_completion(cmd, entry, keep_buf))
1495018c1491SBodo Stroesser 			break;
14967c9e7a6fSAndy Grover 
14970ad46af8SAndy Grover 		UPDATE_HEAD(udev->cmdr_last_cleaned,
14980ad46af8SAndy Grover 			    tcmu_hdr_get_len(entry->hdr.len_op),
14990ad46af8SAndy Grover 			    udev->cmdr_size);
15007c9e7a6fSAndy Grover 	}
1501bc2d214aSBodo Stroesser 	if (free_space)
1502bc2d214aSBodo Stroesser 		free_space = tcmu_run_tmr_queue(udev);
15037c9e7a6fSAndy Grover 
15048b084d9dSBodo Stroesser 	if (atomic_read(&global_page_count) > tcmu_global_max_pages &&
1505d3cbb743SBodo Stroesser 	    xa_empty(&udev->commands) && list_empty(&udev->qfull_queue)) {
1506af1dd7ffSMike Christie 		/*
1507ed212ca8SBodo Stroesser 		 * Allocated blocks exceeded global block limit, currently no
1508ed212ca8SBodo Stroesser 		 * more pending or waiting commands so try to reclaim blocks.
1509af1dd7ffSMike Christie 		 */
1510af1dd7ffSMike Christie 		schedule_delayed_work(&tcmu_unmap_work, 0);
1511af1dd7ffSMike Christie 	}
1512ed212ca8SBodo Stroesser 	if (udev->cmd_time_out)
1513a94a2572SXiubo Li 		tcmu_set_next_deadline(&udev->inflight_queue, &udev->cmd_timer);
15147c9e7a6fSAndy Grover 
1515bc2d214aSBodo Stroesser 	return free_space;
15167c9e7a6fSAndy Grover }
15177c9e7a6fSAndy Grover 
tcmu_check_expired_ring_cmd(struct tcmu_cmd * cmd)151861fb2482SBodo Stroesser static void tcmu_check_expired_ring_cmd(struct tcmu_cmd *cmd)
15197c9e7a6fSAndy Grover {
1520af1dd7ffSMike Christie 	struct se_cmd *se_cmd;
15217c9e7a6fSAndy Grover 
1522ed212ca8SBodo Stroesser 	if (!time_after_eq(jiffies, cmd->deadline))
152361fb2482SBodo Stroesser 		return;
15249103575aSMike Christie 
1525af1dd7ffSMike Christie 	set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags);
152640d883b0SXiubo Li 	list_del_init(&cmd->queue_entry);
152761fb2482SBodo Stroesser 	se_cmd = cmd->se_cmd;
1528a3512902SBodo Stroesser 	se_cmd->priv = NULL;
1529a86a7586SDmitry Fomichev 	cmd->se_cmd = NULL;
153061fb2482SBodo Stroesser 
153161fb2482SBodo Stroesser 	pr_debug("Timing out inflight cmd %u on dev %s.\n",
153261fb2482SBodo Stroesser 		 cmd->cmd_id, cmd->tcmu_dev->name);
153361fb2482SBodo Stroesser 
153461fb2482SBodo Stroesser 	target_complete_cmd(se_cmd, SAM_STAT_CHECK_CONDITION);
1535af1dd7ffSMike Christie }
15369103575aSMike Christie 
tcmu_check_expired_queue_cmd(struct tcmu_cmd * cmd)153761fb2482SBodo Stroesser static void tcmu_check_expired_queue_cmd(struct tcmu_cmd *cmd)
153861fb2482SBodo Stroesser {
153961fb2482SBodo Stroesser 	struct se_cmd *se_cmd;
15409103575aSMike Christie 
1541ed212ca8SBodo Stroesser 	if (!time_after_eq(jiffies, cmd->deadline))
154261fb2482SBodo Stroesser 		return;
154361fb2482SBodo Stroesser 
15449d7464b1SDan Carpenter 	pr_debug("Timing out queued cmd %p on dev %s.\n",
15459d7464b1SDan Carpenter 		  cmd, cmd->tcmu_dev->name);
15469d7464b1SDan Carpenter 
154761fb2482SBodo Stroesser 	list_del_init(&cmd->queue_entry);
154861fb2482SBodo Stroesser 	se_cmd = cmd->se_cmd;
154961fb2482SBodo Stroesser 	tcmu_free_cmd(cmd);
155061fb2482SBodo Stroesser 
1551780e1384SShin'ichiro Kawasaki 	se_cmd->priv = NULL;
155261fb2482SBodo Stroesser 	target_complete_cmd(se_cmd, SAM_STAT_TASK_SET_FULL);
15537c9e7a6fSAndy Grover }
15547c9e7a6fSAndy Grover 
tcmu_device_timedout(struct tcmu_dev * udev)15559103575aSMike Christie static void tcmu_device_timedout(struct tcmu_dev *udev)
15567c9e7a6fSAndy Grover {
1557488ebe4cSMike Christie 	spin_lock(&timed_out_udevs_lock);
1558488ebe4cSMike Christie 	if (list_empty(&udev->timedout_entry))
1559488ebe4cSMike Christie 		list_add_tail(&udev->timedout_entry, &timed_out_udevs);
1560488ebe4cSMike Christie 	spin_unlock(&timed_out_udevs_lock);
15617c9e7a6fSAndy Grover 
1562af1dd7ffSMike Christie 	schedule_delayed_work(&tcmu_unmap_work, 0);
15637c9e7a6fSAndy Grover }
15647c9e7a6fSAndy Grover 
tcmu_cmd_timedout(struct timer_list * t)15659103575aSMike Christie static void tcmu_cmd_timedout(struct timer_list *t)
15669103575aSMike Christie {
15679103575aSMike Christie 	struct tcmu_dev *udev = from_timer(udev, t, cmd_timer);
15689103575aSMike Christie 
15699103575aSMike Christie 	pr_debug("%s cmd timeout has expired\n", udev->name);
15709103575aSMike Christie 	tcmu_device_timedout(udev);
15719103575aSMike Christie }
15729103575aSMike Christie 
tcmu_qfull_timedout(struct timer_list * t)15739103575aSMike Christie static void tcmu_qfull_timedout(struct timer_list *t)
15749103575aSMike Christie {
15759103575aSMike Christie 	struct tcmu_dev *udev = from_timer(udev, t, qfull_timer);
15769103575aSMike Christie 
15779103575aSMike Christie 	pr_debug("%s qfull timeout has expired\n", udev->name);
15789103575aSMike Christie 	tcmu_device_timedout(udev);
15799103575aSMike Christie }
15809103575aSMike Christie 
tcmu_attach_hba(struct se_hba * hba,u32 host_id)15817c9e7a6fSAndy Grover static int tcmu_attach_hba(struct se_hba *hba, u32 host_id)
15827c9e7a6fSAndy Grover {
15837c9e7a6fSAndy Grover 	struct tcmu_hba *tcmu_hba;
15847c9e7a6fSAndy Grover 
15857c9e7a6fSAndy Grover 	tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL);
15867c9e7a6fSAndy Grover 	if (!tcmu_hba)
15877c9e7a6fSAndy Grover 		return -ENOMEM;
15887c9e7a6fSAndy Grover 
15897c9e7a6fSAndy Grover 	tcmu_hba->host_id = host_id;
15907c9e7a6fSAndy Grover 	hba->hba_ptr = tcmu_hba;
15917c9e7a6fSAndy Grover 
15927c9e7a6fSAndy Grover 	return 0;
15937c9e7a6fSAndy Grover }
15947c9e7a6fSAndy Grover 
tcmu_detach_hba(struct se_hba * hba)15957c9e7a6fSAndy Grover static void tcmu_detach_hba(struct se_hba *hba)
15967c9e7a6fSAndy Grover {
15977c9e7a6fSAndy Grover 	kfree(hba->hba_ptr);
15987c9e7a6fSAndy Grover 	hba->hba_ptr = NULL;
15997c9e7a6fSAndy Grover }
16007c9e7a6fSAndy Grover 
tcmu_alloc_device(struct se_hba * hba,const char * name)16017c9e7a6fSAndy Grover static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
16027c9e7a6fSAndy Grover {
16037c9e7a6fSAndy Grover 	struct tcmu_dev *udev;
16047c9e7a6fSAndy Grover 
16057c9e7a6fSAndy Grover 	udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL);
16067c9e7a6fSAndy Grover 	if (!udev)
16077c9e7a6fSAndy Grover 		return NULL;
1608f3cdbe39SMike Christie 	kref_init(&udev->kref);
16097c9e7a6fSAndy Grover 
16107c9e7a6fSAndy Grover 	udev->name = kstrdup(name, GFP_KERNEL);
16117c9e7a6fSAndy Grover 	if (!udev->name) {
16127c9e7a6fSAndy Grover 		kfree(udev);
16137c9e7a6fSAndy Grover 		return NULL;
16147c9e7a6fSAndy Grover 	}
16157c9e7a6fSAndy Grover 
16167c9e7a6fSAndy Grover 	udev->hba = hba;
1617af980e46SMike Christie 	udev->cmd_time_out = TCMU_TIME_OUT;
16189103575aSMike Christie 	udev->qfull_time_out = -1;
16197c9e7a6fSAndy Grover 
1620e719afdcSBodo Stroesser 	udev->data_pages_per_blk = DATA_PAGES_PER_BLK_DEF;
1621e719afdcSBodo Stroesser 	udev->max_blocks = DATA_AREA_PAGES_DEF / udev->data_pages_per_blk;
1622c7ede4f0SGuixin Liu 	udev->cmdr_size = CMDR_SIZE_DEF;
1623f5ce815fSBodo Stroesser 	udev->data_area_mb = TCMU_PAGES_TO_MBS(DATA_AREA_PAGES_DEF);
1624e719afdcSBodo Stroesser 
1625b6df4b79SXiubo Li 	mutex_init(&udev->cmdr_lock);
16267c9e7a6fSAndy Grover 
1627ff07e4a4SMike Christie 	INIT_LIST_HEAD(&udev->node);
1628488ebe4cSMike Christie 	INIT_LIST_HEAD(&udev->timedout_entry);
1629a94a2572SXiubo Li 	INIT_LIST_HEAD(&udev->qfull_queue);
1630bc2d214aSBodo Stroesser 	INIT_LIST_HEAD(&udev->tmr_queue);
1631a94a2572SXiubo Li 	INIT_LIST_HEAD(&udev->inflight_queue);
1632d3cbb743SBodo Stroesser 	xa_init_flags(&udev->commands, XA_FLAGS_ALLOC1);
16337c9e7a6fSAndy Grover 
16349103575aSMike Christie 	timer_setup(&udev->qfull_timer, tcmu_qfull_timedout, 0);
16359103575aSMike Christie 	timer_setup(&udev->cmd_timer, tcmu_cmd_timedout, 0);
16367c9e7a6fSAndy Grover 
16378b084d9dSBodo Stroesser 	xa_init(&udev->data_pages);
1638c22adc0bSXiubo Li 
16397c9e7a6fSAndy Grover 	return &udev->se_dev;
16407c9e7a6fSAndy Grover }
16417c9e7a6fSAndy Grover 
tcmu_dev_call_rcu(struct rcu_head * p)164243bf922cSBodo Stroesser static void tcmu_dev_call_rcu(struct rcu_head *p)
164343bf922cSBodo Stroesser {
164443bf922cSBodo Stroesser 	struct se_device *dev = container_of(p, struct se_device, rcu_head);
164543bf922cSBodo Stroesser 	struct tcmu_dev *udev = TCMU_DEV(dev);
164643bf922cSBodo Stroesser 
164743bf922cSBodo Stroesser 	kfree(udev->uio_info.name);
164843bf922cSBodo Stroesser 	kfree(udev->name);
164943bf922cSBodo Stroesser 	kfree(udev);
165043bf922cSBodo Stroesser }
165143bf922cSBodo Stroesser 
tcmu_check_and_free_pending_cmd(struct tcmu_cmd * cmd)165243bf922cSBodo Stroesser static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd)
165343bf922cSBodo Stroesser {
1654018c1491SBodo Stroesser 	if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags) ||
1655018c1491SBodo Stroesser 	    test_bit(TCMU_CMD_BIT_KEEP_BUF, &cmd->flags)) {
165643bf922cSBodo Stroesser 		kmem_cache_free(tcmu_cmd_cache, cmd);
165743bf922cSBodo Stroesser 		return 0;
165843bf922cSBodo Stroesser 	}
165943bf922cSBodo Stroesser 	return -EINVAL;
166043bf922cSBodo Stroesser }
166143bf922cSBodo Stroesser 
tcmu_blocks_release(struct tcmu_dev * udev,unsigned long first,unsigned long last)1662e719afdcSBodo Stroesser static u32 tcmu_blocks_release(struct tcmu_dev *udev, unsigned long first,
1663f7c89771SBodo Stroesser 				unsigned long last)
166443bf922cSBodo Stroesser {
166543bf922cSBodo Stroesser 	struct page *page;
1666325d5c5fSBodo Stroesser 	unsigned long dpi;
1667f5ce815fSBodo Stroesser 	u32 pages_freed = 0;
166843bf922cSBodo Stroesser 
1669325d5c5fSBodo Stroesser 	first = first * udev->data_pages_per_blk;
1670325d5c5fSBodo Stroesser 	last = (last + 1) * udev->data_pages_per_blk - 1;
1671325d5c5fSBodo Stroesser 	xa_for_each_range(&udev->data_pages, dpi, page, first, last) {
1672325d5c5fSBodo Stroesser 		xa_erase(&udev->data_pages, dpi);
1673bb9b9eb0SXiaoguang Wang 		/*
1674bb9b9eb0SXiaoguang Wang 		 * While reaching here there may be page faults occurring on
1675bb9b9eb0SXiaoguang Wang 		 * the to-be-released pages. A race condition may occur if
1676bb9b9eb0SXiaoguang Wang 		 * unmap_mapping_range() is called before page faults on these
1677bb9b9eb0SXiaoguang Wang 		 * pages have completed; a valid but stale map is created.
1678bb9b9eb0SXiaoguang Wang 		 *
1679bb9b9eb0SXiaoguang Wang 		 * If another command subsequently runs and needs to extend
1680bb9b9eb0SXiaoguang Wang 		 * dbi_thresh, it may reuse the slot corresponding to the
1681bb9b9eb0SXiaoguang Wang 		 * previous page in data_bitmap. Though we will allocate a new
1682bb9b9eb0SXiaoguang Wang 		 * page for the slot in data_area, no page fault will happen
1683bb9b9eb0SXiaoguang Wang 		 * because we have a valid map. Therefore the command's data
1684bb9b9eb0SXiaoguang Wang 		 * will be lost.
1685bb9b9eb0SXiaoguang Wang 		 *
1686bb9b9eb0SXiaoguang Wang 		 * We lock and unlock pages that are to be released to ensure
1687bb9b9eb0SXiaoguang Wang 		 * all page faults have completed. This way
1688bb9b9eb0SXiaoguang Wang 		 * unmap_mapping_range() can ensure stale maps are cleanly
1689bb9b9eb0SXiaoguang Wang 		 * removed.
1690bb9b9eb0SXiaoguang Wang 		 */
1691bb9b9eb0SXiaoguang Wang 		lock_page(page);
1692bb9b9eb0SXiaoguang Wang 		unlock_page(page);
169343bf922cSBodo Stroesser 		__free_page(page);
1694f5ce815fSBodo Stroesser 		pages_freed++;
169543bf922cSBodo Stroesser 	}
1696f5ce815fSBodo Stroesser 
1697f5ce815fSBodo Stroesser 	atomic_sub(pages_freed, &global_page_count);
1698f5ce815fSBodo Stroesser 
1699f5ce815fSBodo Stroesser 	return pages_freed;
170043bf922cSBodo Stroesser }
170143bf922cSBodo Stroesser 
tcmu_remove_all_queued_tmr(struct tcmu_dev * udev)170243bf922cSBodo Stroesser static void tcmu_remove_all_queued_tmr(struct tcmu_dev *udev)
170343bf922cSBodo Stroesser {
170443bf922cSBodo Stroesser 	struct tcmu_tmr *tmr, *tmp;
170543bf922cSBodo Stroesser 
170643bf922cSBodo Stroesser 	list_for_each_entry_safe(tmr, tmp, &udev->tmr_queue, queue_entry) {
170743bf922cSBodo Stroesser 		list_del_init(&tmr->queue_entry);
170843bf922cSBodo Stroesser 		kfree(tmr);
170943bf922cSBodo Stroesser 	}
171043bf922cSBodo Stroesser }
171143bf922cSBodo Stroesser 
tcmu_dev_kref_release(struct kref * kref)171243bf922cSBodo Stroesser static void tcmu_dev_kref_release(struct kref *kref)
171343bf922cSBodo Stroesser {
171443bf922cSBodo Stroesser 	struct tcmu_dev *udev = container_of(kref, struct tcmu_dev, kref);
171543bf922cSBodo Stroesser 	struct se_device *dev = &udev->se_dev;
171643bf922cSBodo Stroesser 	struct tcmu_cmd *cmd;
171743bf922cSBodo Stroesser 	bool all_expired = true;
1718d3cbb743SBodo Stroesser 	unsigned long i;
171943bf922cSBodo Stroesser 
172043bf922cSBodo Stroesser 	vfree(udev->mb_addr);
172143bf922cSBodo Stroesser 	udev->mb_addr = NULL;
172243bf922cSBodo Stroesser 
172343bf922cSBodo Stroesser 	spin_lock_bh(&timed_out_udevs_lock);
172443bf922cSBodo Stroesser 	if (!list_empty(&udev->timedout_entry))
172543bf922cSBodo Stroesser 		list_del(&udev->timedout_entry);
172643bf922cSBodo Stroesser 	spin_unlock_bh(&timed_out_udevs_lock);
172743bf922cSBodo Stroesser 
172843bf922cSBodo Stroesser 	/* Upper layer should drain all requests before calling this */
172943bf922cSBodo Stroesser 	mutex_lock(&udev->cmdr_lock);
1730d3cbb743SBodo Stroesser 	xa_for_each(&udev->commands, i, cmd) {
173143bf922cSBodo Stroesser 		if (tcmu_check_and_free_pending_cmd(cmd) != 0)
173243bf922cSBodo Stroesser 			all_expired = false;
173343bf922cSBodo Stroesser 	}
173443bf922cSBodo Stroesser 	/* There can be left over TMR cmds. Remove them. */
173543bf922cSBodo Stroesser 	tcmu_remove_all_queued_tmr(udev);
173643bf922cSBodo Stroesser 	if (!list_empty(&udev->qfull_queue))
173743bf922cSBodo Stroesser 		all_expired = false;
1738d3cbb743SBodo Stroesser 	xa_destroy(&udev->commands);
173943bf922cSBodo Stroesser 	WARN_ON(!all_expired);
174043bf922cSBodo Stroesser 
1741e719afdcSBodo Stroesser 	tcmu_blocks_release(udev, 0, udev->dbi_max);
174243bf922cSBodo Stroesser 	bitmap_free(udev->data_bitmap);
174343bf922cSBodo Stroesser 	mutex_unlock(&udev->cmdr_lock);
174443bf922cSBodo Stroesser 
17458f33bb24SBodo Stroesser 	pr_debug("dev_kref_release\n");
17468f33bb24SBodo Stroesser 
174743bf922cSBodo Stroesser 	call_rcu(&dev->rcu_head, tcmu_dev_call_rcu);
174843bf922cSBodo Stroesser }
174943bf922cSBodo Stroesser 
run_qfull_queue(struct tcmu_dev * udev,bool fail)175061fb2482SBodo Stroesser static void run_qfull_queue(struct tcmu_dev *udev, bool fail)
1751af1dd7ffSMike Christie {
1752af1dd7ffSMike Christie 	struct tcmu_cmd *tcmu_cmd, *tmp_cmd;
1753af1dd7ffSMike Christie 	LIST_HEAD(cmds);
1754af1dd7ffSMike Christie 	sense_reason_t scsi_ret;
1755af1dd7ffSMike Christie 	int ret;
1756af1dd7ffSMike Christie 
1757a94a2572SXiubo Li 	if (list_empty(&udev->qfull_queue))
175861fb2482SBodo Stroesser 		return;
1759af1dd7ffSMike Christie 
1760892782caSMike Christie 	pr_debug("running %s's cmdr queue forcefail %d\n", udev->name, fail);
1761af1dd7ffSMike Christie 
1762a94a2572SXiubo Li 	list_splice_init(&udev->qfull_queue, &cmds);
1763af1dd7ffSMike Christie 
1764a94a2572SXiubo Li 	list_for_each_entry_safe(tcmu_cmd, tmp_cmd, &cmds, queue_entry) {
1765a94a2572SXiubo Li 		list_del_init(&tcmu_cmd->queue_entry);
1766af1dd7ffSMike Christie 
176761fb2482SBodo Stroesser 		pr_debug("removing cmd %p on dev %s from queue\n",
176861fb2482SBodo Stroesser 			 tcmu_cmd, udev->name);
1769af1dd7ffSMike Christie 
1770892782caSMike Christie 		if (fail) {
1771892782caSMike Christie 			/*
1772892782caSMike Christie 			 * We were not able to even start the command, so
1773892782caSMike Christie 			 * fail with busy to allow a retry in case runner
1774892782caSMike Christie 			 * was only temporarily down. If the device is being
1775892782caSMike Christie 			 * removed then LIO core will do the right thing and
1776892782caSMike Christie 			 * fail the retry.
1777892782caSMike Christie 			 */
1778780e1384SShin'ichiro Kawasaki 			tcmu_cmd->se_cmd->priv = NULL;
1779892782caSMike Christie 			target_complete_cmd(tcmu_cmd->se_cmd, SAM_STAT_BUSY);
1780892782caSMike Christie 			tcmu_free_cmd(tcmu_cmd);
1781892782caSMike Christie 			continue;
1782892782caSMike Christie 		}
1783892782caSMike Christie 
1784af1dd7ffSMike Christie 		ret = queue_cmd_ring(tcmu_cmd, &scsi_ret);
1785af1dd7ffSMike Christie 		if (ret < 0) {
178661fb2482SBodo Stroesser 			pr_debug("cmd %p on dev %s failed with %u\n",
178761fb2482SBodo Stroesser 				 tcmu_cmd, udev->name, scsi_ret);
1788af1dd7ffSMike Christie 			/*
1789af1dd7ffSMike Christie 			 * Ignore scsi_ret for now. target_complete_cmd
1790af1dd7ffSMike Christie 			 * drops it.
1791af1dd7ffSMike Christie 			 */
1792780e1384SShin'ichiro Kawasaki 			tcmu_cmd->se_cmd->priv = NULL;
1793af1dd7ffSMike Christie 			target_complete_cmd(tcmu_cmd->se_cmd,
1794af1dd7ffSMike Christie 					    SAM_STAT_CHECK_CONDITION);
1795af1dd7ffSMike Christie 			tcmu_free_cmd(tcmu_cmd);
1796af1dd7ffSMike Christie 		} else if (ret > 0) {
1797af1dd7ffSMike Christie 			pr_debug("ran out of space during cmdr queue run\n");
1798af1dd7ffSMike Christie 			/*
1799af1dd7ffSMike Christie 			 * cmd was requeued, so just put all cmds back in
1800af1dd7ffSMike Christie 			 * the queue
1801af1dd7ffSMike Christie 			 */
1802a94a2572SXiubo Li 			list_splice_tail(&cmds, &udev->qfull_queue);
1803a94a2572SXiubo Li 			break;
1804af1dd7ffSMike Christie 		}
1805af1dd7ffSMike Christie 	}
1806a94a2572SXiubo Li 
1807a94a2572SXiubo Li 	tcmu_set_next_deadline(&udev->qfull_queue, &udev->qfull_timer);
1808af1dd7ffSMike Christie }
1809af1dd7ffSMike Christie 
tcmu_irqcontrol(struct uio_info * info,s32 irq_on)18107c9e7a6fSAndy Grover static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on)
18117c9e7a6fSAndy Grover {
1812af1dd7ffSMike Christie 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
18137c9e7a6fSAndy Grover 
1814af1dd7ffSMike Christie 	mutex_lock(&udev->cmdr_lock);
1815bc2d214aSBodo Stroesser 	if (tcmu_handle_completions(udev))
1816a94a2572SXiubo Li 		run_qfull_queue(udev, false);
1817af1dd7ffSMike Christie 	mutex_unlock(&udev->cmdr_lock);
18187c9e7a6fSAndy Grover 
18197c9e7a6fSAndy Grover 	return 0;
18207c9e7a6fSAndy Grover }
18217c9e7a6fSAndy Grover 
18227c9e7a6fSAndy Grover /*
18237c9e7a6fSAndy Grover  * mmap code from uio.c. Copied here because we want to hook mmap()
18247c9e7a6fSAndy Grover  * and this stuff must come along.
18257c9e7a6fSAndy Grover  */
tcmu_find_mem_index(struct vm_area_struct * vma)18267c9e7a6fSAndy Grover static int tcmu_find_mem_index(struct vm_area_struct *vma)
18277c9e7a6fSAndy Grover {
18287c9e7a6fSAndy Grover 	struct tcmu_dev *udev = vma->vm_private_data;
18297c9e7a6fSAndy Grover 	struct uio_info *info = &udev->uio_info;
18307c9e7a6fSAndy Grover 
18317c9e7a6fSAndy Grover 	if (vma->vm_pgoff < MAX_UIO_MAPS) {
18327c9e7a6fSAndy Grover 		if (info->mem[vma->vm_pgoff].size == 0)
18337c9e7a6fSAndy Grover 			return -1;
18347c9e7a6fSAndy Grover 		return (int)vma->vm_pgoff;
18357c9e7a6fSAndy Grover 	}
18367c9e7a6fSAndy Grover 	return -1;
18377c9e7a6fSAndy Grover }
18387c9e7a6fSAndy Grover 
tcmu_try_get_data_page(struct tcmu_dev * udev,uint32_t dpi)18398b084d9dSBodo Stroesser static struct page *tcmu_try_get_data_page(struct tcmu_dev *udev, uint32_t dpi)
1840b6df4b79SXiubo Li {
1841b6df4b79SXiubo Li 	struct page *page;
1842b6df4b79SXiubo Li 
1843b6df4b79SXiubo Li 	mutex_lock(&udev->cmdr_lock);
18443722e36cSBodo Stroesser 	page = xa_load(&udev->data_pages, dpi);
1845b6df4b79SXiubo Li 	if (likely(page)) {
1846a6968f7aSXiaoguang Wang 		get_page(page);
1847bb9b9eb0SXiaoguang Wang 		lock_page(page);
1848b6df4b79SXiubo Li 		mutex_unlock(&udev->cmdr_lock);
1849b6df4b79SXiubo Li 		return page;
1850b6df4b79SXiubo Li 	}
1851b6df4b79SXiubo Li 
1852b6df4b79SXiubo Li 	/*
1853c1c390baSMike Christie 	 * Userspace messed up and passed in a address not in the
1854c1c390baSMike Christie 	 * data iov passed to it.
1855b6df4b79SXiubo Li 	 */
18568b084d9dSBodo Stroesser 	pr_err("Invalid addr to data page mapping (dpi %u) on device %s\n",
18578b084d9dSBodo Stroesser 	       dpi, udev->name);
1858b6df4b79SXiubo Li 	mutex_unlock(&udev->cmdr_lock);
1859b6df4b79SXiubo Li 
18608b084d9dSBodo Stroesser 	return NULL;
1861b6df4b79SXiubo Li }
1862b6df4b79SXiubo Li 
tcmu_vma_open(struct vm_area_struct * vma)18638f33bb24SBodo Stroesser static void tcmu_vma_open(struct vm_area_struct *vma)
18648f33bb24SBodo Stroesser {
18658f33bb24SBodo Stroesser 	struct tcmu_dev *udev = vma->vm_private_data;
18668f33bb24SBodo Stroesser 
18678f33bb24SBodo Stroesser 	pr_debug("vma_open\n");
18688f33bb24SBodo Stroesser 
18698f33bb24SBodo Stroesser 	kref_get(&udev->kref);
18708f33bb24SBodo Stroesser }
18718f33bb24SBodo Stroesser 
tcmu_vma_close(struct vm_area_struct * vma)18728f33bb24SBodo Stroesser static void tcmu_vma_close(struct vm_area_struct *vma)
18738f33bb24SBodo Stroesser {
18748f33bb24SBodo Stroesser 	struct tcmu_dev *udev = vma->vm_private_data;
18758f33bb24SBodo Stroesser 
18768f33bb24SBodo Stroesser 	pr_debug("vma_close\n");
18778f33bb24SBodo Stroesser 
18788f33bb24SBodo Stroesser 	/* release ref from tcmu_vma_open */
18798f33bb24SBodo Stroesser 	kref_put(&udev->kref, tcmu_dev_kref_release);
18808f33bb24SBodo Stroesser }
18818f33bb24SBodo Stroesser 
tcmu_vma_fault(struct vm_fault * vmf)188269589c9bSSouptick Joarder static vm_fault_t tcmu_vma_fault(struct vm_fault *vmf)
18837c9e7a6fSAndy Grover {
188411bac800SDave Jiang 	struct tcmu_dev *udev = vmf->vma->vm_private_data;
18857c9e7a6fSAndy Grover 	struct uio_info *info = &udev->uio_info;
18867c9e7a6fSAndy Grover 	struct page *page;
18877c9e7a6fSAndy Grover 	unsigned long offset;
18887c9e7a6fSAndy Grover 	void *addr;
1889bb9b9eb0SXiaoguang Wang 	vm_fault_t ret = 0;
18907c9e7a6fSAndy Grover 
189111bac800SDave Jiang 	int mi = tcmu_find_mem_index(vmf->vma);
18927c9e7a6fSAndy Grover 	if (mi < 0)
18937c9e7a6fSAndy Grover 		return VM_FAULT_SIGBUS;
18947c9e7a6fSAndy Grover 
18957c9e7a6fSAndy Grover 	/*
18967c9e7a6fSAndy Grover 	 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
18977c9e7a6fSAndy Grover 	 * to use mem[N].
18987c9e7a6fSAndy Grover 	 */
18997c9e7a6fSAndy Grover 	offset = (vmf->pgoff - mi) << PAGE_SHIFT;
19007c9e7a6fSAndy Grover 
1901141685a3SXiubo Li 	if (offset < udev->data_off) {
1902141685a3SXiubo Li 		/* For the vmalloc()ed cmd area pages */
19037c9e7a6fSAndy Grover 		addr = (void *)(unsigned long)info->mem[mi].addr + offset;
19047c9e7a6fSAndy Grover 		page = vmalloc_to_page(addr);
1905a6968f7aSXiaoguang Wang 		get_page(page);
1906141685a3SXiubo Li 	} else {
19078b084d9dSBodo Stroesser 		uint32_t dpi;
1908141685a3SXiubo Li 
1909b6df4b79SXiubo Li 		/* For the dynamically growing data area pages */
19108b084d9dSBodo Stroesser 		dpi = (offset - udev->data_off) / PAGE_SIZE;
19118b084d9dSBodo Stroesser 		page = tcmu_try_get_data_page(udev, dpi);
1912b6df4b79SXiubo Li 		if (!page)
1913c1c390baSMike Christie 			return VM_FAULT_SIGBUS;
1914bb9b9eb0SXiaoguang Wang 		ret = VM_FAULT_LOCKED;
1915141685a3SXiubo Li 	}
1916141685a3SXiubo Li 
19177c9e7a6fSAndy Grover 	vmf->page = page;
1918bb9b9eb0SXiaoguang Wang 	return ret;
19197c9e7a6fSAndy Grover }
19207c9e7a6fSAndy Grover 
19217c9e7a6fSAndy Grover static const struct vm_operations_struct tcmu_vm_ops = {
19228f33bb24SBodo Stroesser 	.open = tcmu_vma_open,
19238f33bb24SBodo Stroesser 	.close = tcmu_vma_close,
19247c9e7a6fSAndy Grover 	.fault = tcmu_vma_fault,
19257c9e7a6fSAndy Grover };
19267c9e7a6fSAndy Grover 
tcmu_mmap(struct uio_info * info,struct vm_area_struct * vma)19277c9e7a6fSAndy Grover static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma)
19287c9e7a6fSAndy Grover {
19297c9e7a6fSAndy Grover 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
19307c9e7a6fSAndy Grover 
19311c71222eSSuren Baghdasaryan 	vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP);
19327c9e7a6fSAndy Grover 	vma->vm_ops = &tcmu_vm_ops;
19337c9e7a6fSAndy Grover 
19347c9e7a6fSAndy Grover 	vma->vm_private_data = udev;
19357c9e7a6fSAndy Grover 
19367c9e7a6fSAndy Grover 	/* Ensure the mmap is exactly the right size */
1937ecddbb7eSBodo Stroesser 	if (vma_pages(vma) != udev->mmap_pages)
19387c9e7a6fSAndy Grover 		return -EINVAL;
19397c9e7a6fSAndy Grover 
19408f33bb24SBodo Stroesser 	tcmu_vma_open(vma);
19418f33bb24SBodo Stroesser 
19427c9e7a6fSAndy Grover 	return 0;
19437c9e7a6fSAndy Grover }
19447c9e7a6fSAndy Grover 
tcmu_open(struct uio_info * info,struct inode * inode)19457c9e7a6fSAndy Grover static int tcmu_open(struct uio_info *info, struct inode *inode)
19467c9e7a6fSAndy Grover {
19477c9e7a6fSAndy Grover 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
19487c9e7a6fSAndy Grover 
19497c9e7a6fSAndy Grover 	/* O_EXCL not supported for char devs, so fake it? */
19507c9e7a6fSAndy Grover 	if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags))
19517c9e7a6fSAndy Grover 		return -EBUSY;
19527c9e7a6fSAndy Grover 
1953b6df4b79SXiubo Li 	udev->inode = inode;
1954b6df4b79SXiubo Li 
19557c9e7a6fSAndy Grover 	pr_debug("open\n");
19567c9e7a6fSAndy Grover 
19577c9e7a6fSAndy Grover 	return 0;
19587c9e7a6fSAndy Grover }
19597c9e7a6fSAndy Grover 
tcmu_release(struct uio_info * info,struct inode * inode)19607c9e7a6fSAndy Grover static int tcmu_release(struct uio_info *info, struct inode *inode)
19617c9e7a6fSAndy Grover {
19627c9e7a6fSAndy Grover 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
1963018c1491SBodo Stroesser 	struct tcmu_cmd *cmd;
1964018c1491SBodo Stroesser 	unsigned long i;
1965018c1491SBodo Stroesser 	bool freed = false;
1966018c1491SBodo Stroesser 
1967018c1491SBodo Stroesser 	mutex_lock(&udev->cmdr_lock);
1968018c1491SBodo Stroesser 
1969018c1491SBodo Stroesser 	xa_for_each(&udev->commands, i, cmd) {
1970018c1491SBodo Stroesser 		/* Cmds with KEEP_BUF set are no longer on the ring, but
1971018c1491SBodo Stroesser 		 * userspace still holds the data buffer. If userspace closes
1972018c1491SBodo Stroesser 		 * we implicitly free these cmds and buffers, since after new
1973018c1491SBodo Stroesser 		 * open the (new ?) userspace cannot find the cmd in the ring
1974018c1491SBodo Stroesser 		 * and thus never will release the buffer by writing cmd_id to
1975018c1491SBodo Stroesser 		 * free_kept_buf action attribute.
1976018c1491SBodo Stroesser 		 */
1977018c1491SBodo Stroesser 		if (!test_bit(TCMU_CMD_BIT_KEEP_BUF, &cmd->flags))
1978018c1491SBodo Stroesser 			continue;
1979018c1491SBodo Stroesser 		pr_debug("removing KEEP_BUF cmd %u on dev %s from ring\n",
1980018c1491SBodo Stroesser 			 cmd->cmd_id, udev->name);
1981018c1491SBodo Stroesser 		freed = true;
1982018c1491SBodo Stroesser 
1983018c1491SBodo Stroesser 		xa_erase(&udev->commands, i);
1984018c1491SBodo Stroesser 		tcmu_cmd_free_data(cmd, cmd->dbi_cnt);
1985018c1491SBodo Stroesser 		tcmu_free_cmd(cmd);
1986018c1491SBodo Stroesser 	}
1987018c1491SBodo Stroesser 	/*
1988018c1491SBodo Stroesser 	 * We only freed data space, not ring space. Therefore we dont call
1989018c1491SBodo Stroesser 	 * run_tmr_queue, but call run_qfull_queue if tmr_list is empty.
1990018c1491SBodo Stroesser 	 */
1991018c1491SBodo Stroesser 	if (freed && list_empty(&udev->tmr_queue))
1992018c1491SBodo Stroesser 		run_qfull_queue(udev, false);
1993018c1491SBodo Stroesser 
1994018c1491SBodo Stroesser 	mutex_unlock(&udev->cmdr_lock);
19957c9e7a6fSAndy Grover 
19967c9e7a6fSAndy Grover 	clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags);
19977c9e7a6fSAndy Grover 
19987c9e7a6fSAndy Grover 	pr_debug("close\n");
19998f33bb24SBodo Stroesser 
20007c9e7a6fSAndy Grover 	return 0;
20017c9e7a6fSAndy Grover }
20027c9e7a6fSAndy Grover 
tcmu_init_genl_cmd_reply(struct tcmu_dev * udev,int cmd)20039de3a1efSMike Christie static int tcmu_init_genl_cmd_reply(struct tcmu_dev *udev, int cmd)
2004b3af66e2SMike Christie {
2005b3af66e2SMike Christie 	struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd;
2006b3af66e2SMike Christie 
2007b3af66e2SMike Christie 	if (!tcmu_kern_cmd_reply_supported)
20089de3a1efSMike Christie 		return 0;
2009b849b456SKenjiro Nakayama 
2010b849b456SKenjiro Nakayama 	if (udev->nl_reply_supported <= 0)
20119de3a1efSMike Christie 		return 0;
2012b849b456SKenjiro Nakayama 
20133228691fSMike Christie 	mutex_lock(&tcmu_nl_cmd_mutex);
2014b3af66e2SMike Christie 
2015bdaeedc1SMike Christie 	if (tcmu_netlink_blocked) {
2016bdaeedc1SMike Christie 		mutex_unlock(&tcmu_nl_cmd_mutex);
2017bdaeedc1SMike Christie 		pr_warn("Failing nl cmd %d on %s. Interface is blocked.\n", cmd,
2018bdaeedc1SMike Christie 			udev->name);
2019bdaeedc1SMike Christie 		return -EAGAIN;
2020bdaeedc1SMike Christie 	}
2021b3af66e2SMike Christie 
2022b3af66e2SMike Christie 	if (nl_cmd->cmd != TCMU_CMD_UNSPEC) {
20233228691fSMike Christie 		mutex_unlock(&tcmu_nl_cmd_mutex);
20249de3a1efSMike Christie 		pr_warn("netlink cmd %d already executing on %s\n",
20259de3a1efSMike Christie 			 nl_cmd->cmd, udev->name);
20269de3a1efSMike Christie 		return -EBUSY;
2027b3af66e2SMike Christie 	}
2028b3af66e2SMike Christie 
2029b3af66e2SMike Christie 	memset(nl_cmd, 0, sizeof(*nl_cmd));
2030b3af66e2SMike Christie 	nl_cmd->cmd = cmd;
20313228691fSMike Christie 	nl_cmd->udev = udev;
2032b3af66e2SMike Christie 	init_completion(&nl_cmd->complete);
20333228691fSMike Christie 	INIT_LIST_HEAD(&nl_cmd->nl_list);
2034b3af66e2SMike Christie 
20353228691fSMike Christie 	list_add_tail(&nl_cmd->nl_list, &tcmu_nl_cmd_list);
20363228691fSMike Christie 
20373228691fSMike Christie 	mutex_unlock(&tcmu_nl_cmd_mutex);
20389de3a1efSMike Christie 	return 0;
2039b3af66e2SMike Christie }
2040b3af66e2SMike Christie 
tcmu_destroy_genl_cmd_reply(struct tcmu_dev * udev)20417d894862SLi Zhong static void tcmu_destroy_genl_cmd_reply(struct tcmu_dev *udev)
20427d894862SLi Zhong {
20437d894862SLi Zhong 	struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd;
20447d894862SLi Zhong 
20457d894862SLi Zhong 	if (!tcmu_kern_cmd_reply_supported)
20467d894862SLi Zhong 		return;
20477d894862SLi Zhong 
20487d894862SLi Zhong 	if (udev->nl_reply_supported <= 0)
20497d894862SLi Zhong 		return;
20507d894862SLi Zhong 
20517d894862SLi Zhong 	mutex_lock(&tcmu_nl_cmd_mutex);
20527d894862SLi Zhong 
20537d894862SLi Zhong 	list_del(&nl_cmd->nl_list);
20547d894862SLi Zhong 	memset(nl_cmd, 0, sizeof(*nl_cmd));
20557d894862SLi Zhong 
20567d894862SLi Zhong 	mutex_unlock(&tcmu_nl_cmd_mutex);
20577d894862SLi Zhong }
20587d894862SLi Zhong 
tcmu_wait_genl_cmd_reply(struct tcmu_dev * udev)2059b3af66e2SMike Christie static int tcmu_wait_genl_cmd_reply(struct tcmu_dev *udev)
2060b3af66e2SMike Christie {
2061b3af66e2SMike Christie 	struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd;
2062b3af66e2SMike Christie 	int ret;
2063b3af66e2SMike Christie 
2064b3af66e2SMike Christie 	if (!tcmu_kern_cmd_reply_supported)
2065b3af66e2SMike Christie 		return 0;
2066b3af66e2SMike Christie 
2067b849b456SKenjiro Nakayama 	if (udev->nl_reply_supported <= 0)
2068b849b456SKenjiro Nakayama 		return 0;
2069b849b456SKenjiro Nakayama 
2070b3af66e2SMike Christie 	pr_debug("sleeping for nl reply\n");
2071b3af66e2SMike Christie 	wait_for_completion(&nl_cmd->complete);
2072b3af66e2SMike Christie 
20733228691fSMike Christie 	mutex_lock(&tcmu_nl_cmd_mutex);
2074b3af66e2SMike Christie 	nl_cmd->cmd = TCMU_CMD_UNSPEC;
2075b3af66e2SMike Christie 	ret = nl_cmd->status;
20763228691fSMike Christie 	mutex_unlock(&tcmu_nl_cmd_mutex);
2077b3af66e2SMike Christie 
207885fae482SLuis de Bethencourt 	return ret;
2079b3af66e2SMike Christie }
2080b3af66e2SMike Christie 
tcmu_netlink_event_init(struct tcmu_dev * udev,enum tcmu_genl_cmd cmd,struct sk_buff ** buf,void ** hdr)20810e5aee39SZhu Lingshan static int tcmu_netlink_event_init(struct tcmu_dev *udev,
20820e5aee39SZhu Lingshan 				   enum tcmu_genl_cmd cmd,
20830e5aee39SZhu Lingshan 				   struct sk_buff **buf, void **hdr)
20847c9e7a6fSAndy Grover {
20857c9e7a6fSAndy Grover 	struct sk_buff *skb;
20867c9e7a6fSAndy Grover 	void *msg_header;
20876e14eab9SNicholas Bellinger 	int ret = -ENOMEM;
20887c9e7a6fSAndy Grover 
20897c9e7a6fSAndy Grover 	skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
20907c9e7a6fSAndy Grover 	if (!skb)
20916e14eab9SNicholas Bellinger 		return ret;
20927c9e7a6fSAndy Grover 
20937c9e7a6fSAndy Grover 	msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd);
20946e14eab9SNicholas Bellinger 	if (!msg_header)
20956e14eab9SNicholas Bellinger 		goto free_skb;
20967c9e7a6fSAndy Grover 
2097b3af66e2SMike Christie 	ret = nla_put_string(skb, TCMU_ATTR_DEVICE, udev->uio_info.name);
20986e14eab9SNicholas Bellinger 	if (ret < 0)
20996e14eab9SNicholas Bellinger 		goto free_skb;
21007c9e7a6fSAndy Grover 
2101b3af66e2SMike Christie 	ret = nla_put_u32(skb, TCMU_ATTR_MINOR, udev->uio_info.uio_dev->minor);
2102b3af66e2SMike Christie 	if (ret < 0)
2103b3af66e2SMike Christie 		goto free_skb;
2104b3af66e2SMike Christie 
2105b3af66e2SMike Christie 	ret = nla_put_u32(skb, TCMU_ATTR_DEVICE_ID, udev->se_dev.dev_index);
21066e14eab9SNicholas Bellinger 	if (ret < 0)
21076e14eab9SNicholas Bellinger 		goto free_skb;
21087c9e7a6fSAndy Grover 
21090e5aee39SZhu Lingshan 	*buf = skb;
21100e5aee39SZhu Lingshan 	*hdr = msg_header;
21110e5aee39SZhu Lingshan 	return ret;
21120e5aee39SZhu Lingshan 
21130e5aee39SZhu Lingshan free_skb:
21140e5aee39SZhu Lingshan 	nlmsg_free(skb);
21150e5aee39SZhu Lingshan 	return ret;
21162d76443eSMike Christie }
21172d76443eSMike Christie 
tcmu_netlink_event_send(struct tcmu_dev * udev,enum tcmu_genl_cmd cmd,struct sk_buff * skb,void * msg_header)21180e5aee39SZhu Lingshan static int tcmu_netlink_event_send(struct tcmu_dev *udev,
21190e5aee39SZhu Lingshan 				   enum tcmu_genl_cmd cmd,
212006add777SMike Christie 				   struct sk_buff *skb, void *msg_header)
21210e5aee39SZhu Lingshan {
212206add777SMike Christie 	int ret;
21238a45885cSBryant G. Ly 
2124053c095aSJohannes Berg 	genlmsg_end(skb, msg_header);
21257c9e7a6fSAndy Grover 
21269de3a1efSMike Christie 	ret = tcmu_init_genl_cmd_reply(udev, cmd);
21279de3a1efSMike Christie 	if (ret) {
21289de3a1efSMike Christie 		nlmsg_free(skb);
21299de3a1efSMike Christie 		return ret;
21309de3a1efSMike Christie 	}
2131b3af66e2SMike Christie 
213220c08b36SSheng Yang 	ret = genlmsg_multicast_allns(&tcmu_genl_family, skb, 0,
21337c9e7a6fSAndy Grover 				      TCMU_MCGRP_CONFIG, GFP_KERNEL);
21342ff717cdSCathy Avery 
21352ff717cdSCathy Avery 	/* Wait during an add as the listener may not be up yet */
21362ff717cdSCathy Avery 	if (ret == 0 ||
21372ff717cdSCathy Avery 	   (ret == -ESRCH && cmd == TCMU_CMD_ADDED_DEVICE))
21382ff717cdSCathy Avery 		return tcmu_wait_genl_cmd_reply(udev);
21397d894862SLi Zhong 	else
21407d894862SLi Zhong 		tcmu_destroy_genl_cmd_reply(udev);
21412ff717cdSCathy Avery 
21420e5aee39SZhu Lingshan 	return ret;
21430e5aee39SZhu Lingshan }
21447c9e7a6fSAndy Grover 
tcmu_send_dev_add_event(struct tcmu_dev * udev)2145e0c240acSZhu Lingshan static int tcmu_send_dev_add_event(struct tcmu_dev *udev)
2146e0c240acSZhu Lingshan {
2147e0c240acSZhu Lingshan 	struct sk_buff *skb = NULL;
2148e0c240acSZhu Lingshan 	void *msg_header = NULL;
2149e0c240acSZhu Lingshan 	int ret = 0;
2150e0c240acSZhu Lingshan 
2151e0c240acSZhu Lingshan 	ret = tcmu_netlink_event_init(udev, TCMU_CMD_ADDED_DEVICE, &skb,
2152e0c240acSZhu Lingshan 				      &msg_header);
2153e0c240acSZhu Lingshan 	if (ret < 0)
21547c9e7a6fSAndy Grover 		return ret;
215506add777SMike Christie 	return tcmu_netlink_event_send(udev, TCMU_CMD_ADDED_DEVICE, skb,
215606add777SMike Christie 				       msg_header);
2157e0c240acSZhu Lingshan }
2158e0c240acSZhu Lingshan 
tcmu_send_dev_remove_event(struct tcmu_dev * udev)2159f892bd8eSZhu Lingshan static int tcmu_send_dev_remove_event(struct tcmu_dev *udev)
2160f892bd8eSZhu Lingshan {
2161f892bd8eSZhu Lingshan 	struct sk_buff *skb = NULL;
2162f892bd8eSZhu Lingshan 	void *msg_header = NULL;
2163f892bd8eSZhu Lingshan 	int ret = 0;
2164f892bd8eSZhu Lingshan 
2165f892bd8eSZhu Lingshan 	ret = tcmu_netlink_event_init(udev, TCMU_CMD_REMOVED_DEVICE,
2166f892bd8eSZhu Lingshan 				      &skb, &msg_header);
2167f892bd8eSZhu Lingshan 	if (ret < 0)
21686e14eab9SNicholas Bellinger 		return ret;
2169f892bd8eSZhu Lingshan 	return tcmu_netlink_event_send(udev, TCMU_CMD_REMOVED_DEVICE,
217006add777SMike Christie 				       skb, msg_header);
21717c9e7a6fSAndy Grover }
21727c9e7a6fSAndy Grover 
tcmu_update_uio_info(struct tcmu_dev * udev)2173de8c5221SBryant G. Ly static int tcmu_update_uio_info(struct tcmu_dev *udev)
21747c9e7a6fSAndy Grover {
21757c9e7a6fSAndy Grover 	struct tcmu_hba *hba = udev->hba->hba_ptr;
21767c9e7a6fSAndy Grover 	struct uio_info *info;
21777c9e7a6fSAndy Grover 	char *str;
21787c9e7a6fSAndy Grover 
21797c9e7a6fSAndy Grover 	info = &udev->uio_info;
218022c2f35fSChristophe JAILLET 
218122c2f35fSChristophe JAILLET 	if (udev->dev_config[0])
218222c2f35fSChristophe JAILLET 		str = kasprintf(GFP_KERNEL, "tcm-user/%u/%s/%s", hba->host_id,
218322c2f35fSChristophe JAILLET 				udev->name, udev->dev_config);
218422c2f35fSChristophe JAILLET 	else
218522c2f35fSChristophe JAILLET 		str = kasprintf(GFP_KERNEL, "tcm-user/%u/%s", hba->host_id,
218622c2f35fSChristophe JAILLET 				udev->name);
21877c9e7a6fSAndy Grover 	if (!str)
21887c9e7a6fSAndy Grover 		return -ENOMEM;
21897c9e7a6fSAndy Grover 
2190ededd039SBryant G. Ly 	/* If the old string exists, free it */
2191ededd039SBryant G. Ly 	kfree(info->name);
21927c9e7a6fSAndy Grover 	info->name = str;
21937c9e7a6fSAndy Grover 
2194de8c5221SBryant G. Ly 	return 0;
2195de8c5221SBryant G. Ly }
2196de8c5221SBryant G. Ly 
tcmu_configure_device(struct se_device * dev)2197de8c5221SBryant G. Ly static int tcmu_configure_device(struct se_device *dev)
2198de8c5221SBryant G. Ly {
2199de8c5221SBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(dev);
2200de8c5221SBryant G. Ly 	struct uio_info *info;
2201de8c5221SBryant G. Ly 	struct tcmu_mailbox *mb;
2202f5ce815fSBodo Stroesser 	size_t data_size;
2203de8c5221SBryant G. Ly 	int ret = 0;
2204de8c5221SBryant G. Ly 
2205de8c5221SBryant G. Ly 	ret = tcmu_update_uio_info(udev);
2206de8c5221SBryant G. Ly 	if (ret)
2207de8c5221SBryant G. Ly 		return ret;
2208de8c5221SBryant G. Ly 
2209de8c5221SBryant G. Ly 	info = &udev->uio_info;
2210de8c5221SBryant G. Ly 
2211c97840c8SMike Christie 	mutex_lock(&udev->cmdr_lock);
221298effe47SAndy Shevchenko 	udev->data_bitmap = bitmap_zalloc(udev->max_blocks, GFP_KERNEL);
2213c97840c8SMike Christie 	mutex_unlock(&udev->cmdr_lock);
2214a24e7917SWei Yongjun 	if (!udev->data_bitmap) {
2215a24e7917SWei Yongjun 		ret = -ENOMEM;
221680eb8761SMike Christie 		goto err_bitmap_alloc;
2217a24e7917SWei Yongjun 	}
221880eb8761SMike Christie 
2219c7ede4f0SGuixin Liu 	mb = vzalloc(udev->cmdr_size + CMDR_OFF);
2220ecddbb7eSBodo Stroesser 	if (!mb) {
22217c9e7a6fSAndy Grover 		ret = -ENOMEM;
22227c9e7a6fSAndy Grover 		goto err_vzalloc;
22237c9e7a6fSAndy Grover 	}
22247c9e7a6fSAndy Grover 
22257c9e7a6fSAndy Grover 	/* mailbox fits in first part of CMDR space */
2226ecddbb7eSBodo Stroesser 	udev->mb_addr = mb;
2227ecddbb7eSBodo Stroesser 	udev->cmdr = (void *)mb + CMDR_OFF;
2228c7ede4f0SGuixin Liu 	udev->data_off = udev->cmdr_size + CMDR_OFF;
2229f5ce815fSBodo Stroesser 	data_size = TCMU_MBS_TO_PAGES(udev->data_area_mb) << PAGE_SHIFT;
2230c7ede4f0SGuixin Liu 	udev->mmap_pages = (data_size + udev->cmdr_size + CMDR_OFF) >> PAGE_SHIFT;
2231e719afdcSBodo Stroesser 	udev->data_blk_size = udev->data_pages_per_blk * PAGE_SIZE;
2232b6df4b79SXiubo Li 	udev->dbi_thresh = 0; /* Default in Idle state */
22337c9e7a6fSAndy Grover 
2234141685a3SXiubo Li 	/* Initialise the mailbox of the ring buffer */
22350ad46af8SAndy Grover 	mb->version = TCMU_MAILBOX_VERSION;
2236bc2d214aSBodo Stroesser 	mb->flags = TCMU_MAILBOX_FLAG_CAP_OOOC |
2237bc2d214aSBodo Stroesser 		    TCMU_MAILBOX_FLAG_CAP_READ_LEN |
2238018c1491SBodo Stroesser 		    TCMU_MAILBOX_FLAG_CAP_TMR |
2239018c1491SBodo Stroesser 		    TCMU_MAILBOX_FLAG_CAP_KEEP_BUF;
22407c9e7a6fSAndy Grover 	mb->cmdr_off = CMDR_OFF;
22417c9e7a6fSAndy Grover 	mb->cmdr_size = udev->cmdr_size;
22427c9e7a6fSAndy Grover 
22437c9e7a6fSAndy Grover 	WARN_ON(!PAGE_ALIGNED(udev->data_off));
2244f5ce815fSBodo Stroesser 	WARN_ON(data_size % PAGE_SIZE);
22457c9e7a6fSAndy Grover 
2246ac64a2ceSDavid Disseldorp 	info->version = __stringify(TCMU_MAILBOX_VERSION);
22477c9e7a6fSAndy Grover 
22487c9e7a6fSAndy Grover 	info->mem[0].name = "tcm-user command & data buffer";
22490633e123SArnd Bergmann 	info->mem[0].addr = (phys_addr_t)(uintptr_t)udev->mb_addr;
2250c7ede4f0SGuixin Liu 	info->mem[0].size = data_size + udev->cmdr_size + CMDR_OFF;
2251141685a3SXiubo Li 	info->mem[0].memtype = UIO_MEM_NONE;
22527c9e7a6fSAndy Grover 
22537c9e7a6fSAndy Grover 	info->irqcontrol = tcmu_irqcontrol;
22547c9e7a6fSAndy Grover 	info->irq = UIO_IRQ_CUSTOM;
22557c9e7a6fSAndy Grover 
22567c9e7a6fSAndy Grover 	info->mmap = tcmu_mmap;
22577c9e7a6fSAndy Grover 	info->open = tcmu_open;
22587c9e7a6fSAndy Grover 	info->release = tcmu_release;
22597c9e7a6fSAndy Grover 
22607c9e7a6fSAndy Grover 	ret = uio_register_device(tcmu_root_device, info);
22617c9e7a6fSAndy Grover 	if (ret)
22627c9e7a6fSAndy Grover 		goto err_register;
22637c9e7a6fSAndy Grover 
226481ee28deSSheng Yang 	/* User can set hw_block_size before enable the device */
226581ee28deSSheng Yang 	if (dev->dev_attrib.hw_block_size == 0)
22667c9e7a6fSAndy Grover 		dev->dev_attrib.hw_block_size = 512;
226781ee28deSSheng Yang 	/* Other attributes can be configured in userspace */
22683abaa2bfSMike Christie 	if (!dev->dev_attrib.hw_max_sectors)
22697c9e7a6fSAndy Grover 		dev->dev_attrib.hw_max_sectors = 128;
22709a8bb606SBryant G. Ly 	if (!dev->dev_attrib.emulate_write_cache)
22719a8bb606SBryant G. Ly 		dev->dev_attrib.emulate_write_cache = 0;
22727c9e7a6fSAndy Grover 	dev->dev_attrib.hw_queue_depth = 128;
22737c9e7a6fSAndy Grover 
2274b849b456SKenjiro Nakayama 	/* If user didn't explicitly disable netlink reply support, use
2275b849b456SKenjiro Nakayama 	 * module scope setting.
2276b849b456SKenjiro Nakayama 	 */
2277b849b456SKenjiro Nakayama 	if (udev->nl_reply_supported >= 0)
2278b849b456SKenjiro Nakayama 		udev->nl_reply_supported = tcmu_kern_cmd_reply_supported;
2279b849b456SKenjiro Nakayama 
2280f3cdbe39SMike Christie 	/*
2281f3cdbe39SMike Christie 	 * Get a ref incase userspace does a close on the uio device before
2282f3cdbe39SMike Christie 	 * LIO has initiated tcmu_free_device.
2283f3cdbe39SMike Christie 	 */
2284f3cdbe39SMike Christie 	kref_get(&udev->kref);
2285f3cdbe39SMike Christie 
2286e0c240acSZhu Lingshan 	ret = tcmu_send_dev_add_event(udev);
22877c9e7a6fSAndy Grover 	if (ret)
22887c9e7a6fSAndy Grover 		goto err_netlink;
22897c9e7a6fSAndy Grover 
2290b6df4b79SXiubo Li 	mutex_lock(&root_udev_mutex);
2291b6df4b79SXiubo Li 	list_add(&udev->node, &root_udev);
2292b6df4b79SXiubo Li 	mutex_unlock(&root_udev_mutex);
2293b6df4b79SXiubo Li 
22947c9e7a6fSAndy Grover 	return 0;
22957c9e7a6fSAndy Grover 
22967c9e7a6fSAndy Grover err_netlink:
2297f3cdbe39SMike Christie 	kref_put(&udev->kref, tcmu_dev_kref_release);
22987c9e7a6fSAndy Grover 	uio_unregister_device(&udev->uio_info);
22997c9e7a6fSAndy Grover err_register:
23007c9e7a6fSAndy Grover 	vfree(udev->mb_addr);
2301c22adc0bSXiubo Li 	udev->mb_addr = NULL;
23027c9e7a6fSAndy Grover err_vzalloc:
230398effe47SAndy Shevchenko 	bitmap_free(udev->data_bitmap);
230480eb8761SMike Christie 	udev->data_bitmap = NULL;
230580eb8761SMike Christie err_bitmap_alloc:
23067c9e7a6fSAndy Grover 	kfree(info->name);
2307f3cdbe39SMike Christie 	info->name = NULL;
23087c9e7a6fSAndy Grover 
23097c9e7a6fSAndy Grover 	return ret;
23107c9e7a6fSAndy Grover }
23117c9e7a6fSAndy Grover 
tcmu_free_device(struct se_device * dev)23127c9e7a6fSAndy Grover static void tcmu_free_device(struct se_device *dev)
23137c9e7a6fSAndy Grover {
23147c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
231592634706SMike Christie 
231692634706SMike Christie 	/* release ref from init */
231792634706SMike Christie 	kref_put(&udev->kref, tcmu_dev_kref_release);
231892634706SMike Christie }
231992634706SMike Christie 
tcmu_destroy_device(struct se_device * dev)232092634706SMike Christie static void tcmu_destroy_device(struct se_device *dev)
232192634706SMike Christie {
232292634706SMike Christie 	struct tcmu_dev *udev = TCMU_DEV(dev);
23237c9e7a6fSAndy Grover 
23249103575aSMike Christie 	del_timer_sync(&udev->cmd_timer);
23259103575aSMike Christie 	del_timer_sync(&udev->qfull_timer);
23267c9e7a6fSAndy Grover 
2327b6df4b79SXiubo Li 	mutex_lock(&root_udev_mutex);
2328b6df4b79SXiubo Li 	list_del(&udev->node);
2329b6df4b79SXiubo Li 	mutex_unlock(&root_udev_mutex);
2330b6df4b79SXiubo Li 
2331f892bd8eSZhu Lingshan 	tcmu_send_dev_remove_event(udev);
23327c9e7a6fSAndy Grover 
23337c9e7a6fSAndy Grover 	uio_unregister_device(&udev->uio_info);
23349260695dSMike Christie 
23359260695dSMike Christie 	/* release ref from configure */
23369260695dSMike Christie 	kref_put(&udev->kref, tcmu_dev_kref_release);
23377c9e7a6fSAndy Grover }
23387c9e7a6fSAndy Grover 
tcmu_unblock_dev(struct tcmu_dev * udev)2339892782caSMike Christie static void tcmu_unblock_dev(struct tcmu_dev *udev)
2340892782caSMike Christie {
2341892782caSMike Christie 	mutex_lock(&udev->cmdr_lock);
2342892782caSMike Christie 	clear_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags);
2343892782caSMike Christie 	mutex_unlock(&udev->cmdr_lock);
2344892782caSMike Christie }
2345892782caSMike Christie 
tcmu_block_dev(struct tcmu_dev * udev)2346892782caSMike Christie static void tcmu_block_dev(struct tcmu_dev *udev)
2347892782caSMike Christie {
2348892782caSMike Christie 	mutex_lock(&udev->cmdr_lock);
2349892782caSMike Christie 
2350892782caSMike Christie 	if (test_and_set_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags))
2351892782caSMike Christie 		goto unlock;
2352892782caSMike Christie 
2353892782caSMike Christie 	/* complete IO that has executed successfully */
2354892782caSMike Christie 	tcmu_handle_completions(udev);
2355892782caSMike Christie 	/* fail IO waiting to be queued */
2356a94a2572SXiubo Li 	run_qfull_queue(udev, true);
2357892782caSMike Christie 
2358892782caSMike Christie unlock:
2359892782caSMike Christie 	mutex_unlock(&udev->cmdr_lock);
2360892782caSMike Christie }
2361892782caSMike Christie 
tcmu_reset_ring(struct tcmu_dev * udev,u8 err_level)2362892782caSMike Christie static void tcmu_reset_ring(struct tcmu_dev *udev, u8 err_level)
2363892782caSMike Christie {
2364892782caSMike Christie 	struct tcmu_mailbox *mb;
2365892782caSMike Christie 	struct tcmu_cmd *cmd;
2366d3cbb743SBodo Stroesser 	unsigned long i;
2367892782caSMike Christie 
2368892782caSMike Christie 	mutex_lock(&udev->cmdr_lock);
2369892782caSMike Christie 
2370d3cbb743SBodo Stroesser 	xa_for_each(&udev->commands, i, cmd) {
2371018c1491SBodo Stroesser 		pr_debug("removing cmd %u on dev %s from ring %s\n",
2372892782caSMike Christie 			 cmd->cmd_id, udev->name,
2373018c1491SBodo Stroesser 			 test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags) ?
2374018c1491SBodo Stroesser 			 "(is expired)" :
2375018c1491SBodo Stroesser 			 (test_bit(TCMU_CMD_BIT_KEEP_BUF, &cmd->flags) ?
2376018c1491SBodo Stroesser 			 "(is keep buffer)" : ""));
2377892782caSMike Christie 
2378d3cbb743SBodo Stroesser 		xa_erase(&udev->commands, i);
2379018c1491SBodo Stroesser 		if (!test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags) &&
2380018c1491SBodo Stroesser 		    !test_bit(TCMU_CMD_BIT_KEEP_BUF, &cmd->flags)) {
2381a86a7586SDmitry Fomichev 			WARN_ON(!cmd->se_cmd);
2382a94a2572SXiubo Li 			list_del_init(&cmd->queue_entry);
2383780e1384SShin'ichiro Kawasaki 			cmd->se_cmd->priv = NULL;
2384892782caSMike Christie 			if (err_level == 1) {
2385892782caSMike Christie 				/*
2386892782caSMike Christie 				 * Userspace was not able to start the
2387892782caSMike Christie 				 * command or it is retryable.
2388892782caSMike Christie 				 */
2389892782caSMike Christie 				target_complete_cmd(cmd->se_cmd, SAM_STAT_BUSY);
2390892782caSMike Christie 			} else {
2391892782caSMike Christie 				/* hard failure */
2392892782caSMike Christie 				target_complete_cmd(cmd->se_cmd,
2393892782caSMike Christie 						    SAM_STAT_CHECK_CONDITION);
2394892782caSMike Christie 			}
2395892782caSMike Christie 		}
2396892782caSMike Christie 		tcmu_cmd_free_data(cmd, cmd->dbi_cnt);
2397892782caSMike Christie 		tcmu_free_cmd(cmd);
2398892782caSMike Christie 	}
2399892782caSMike Christie 
2400892782caSMike Christie 	mb = udev->mb_addr;
2401892782caSMike Christie 	tcmu_flush_dcache_range(mb, sizeof(*mb));
2402892782caSMike Christie 	pr_debug("mb last %u head %u tail %u\n", udev->cmdr_last_cleaned,
2403892782caSMike Christie 		 mb->cmd_tail, mb->cmd_head);
2404892782caSMike Christie 
2405892782caSMike Christie 	udev->cmdr_last_cleaned = 0;
2406892782caSMike Christie 	mb->cmd_tail = 0;
2407892782caSMike Christie 	mb->cmd_head = 0;
2408892782caSMike Christie 	tcmu_flush_dcache_range(mb, sizeof(*mb));
2409066f79a5SBodo Stroesser 	clear_bit(TCMU_DEV_BIT_BROKEN, &udev->flags);
2410892782caSMike Christie 
2411892782caSMike Christie 	del_timer(&udev->cmd_timer);
2412892782caSMike Christie 
2413bc2d214aSBodo Stroesser 	/*
2414bc2d214aSBodo Stroesser 	 * ring is empty and qfull queue never contains aborted commands.
2415bc2d214aSBodo Stroesser 	 * So TMRs in tmr queue do not contain relevant cmd_ids.
2416bc2d214aSBodo Stroesser 	 * After a ring reset userspace should do a fresh start, so
2417bc2d214aSBodo Stroesser 	 * even LUN RESET message is no longer relevant.
2418bc2d214aSBodo Stroesser 	 * Therefore remove all TMRs from qfull queue
2419bc2d214aSBodo Stroesser 	 */
2420bc2d214aSBodo Stroesser 	tcmu_remove_all_queued_tmr(udev);
2421bc2d214aSBodo Stroesser 
242261fb2482SBodo Stroesser 	run_qfull_queue(udev, false);
242361fb2482SBodo Stroesser 
2424892782caSMike Christie 	mutex_unlock(&udev->cmdr_lock);
2425892782caSMike Christie }
2426892782caSMike Christie 
24277c9e7a6fSAndy Grover enum {
24283abaa2bfSMike Christie 	Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_hw_max_sectors,
242908976cb5SBodo Stroesser 	Opt_nl_reply_supported, Opt_max_data_area_mb, Opt_data_pages_per_blk,
2430c7ede4f0SGuixin Liu 	Opt_cmd_ring_size_mb, Opt_err,
24317c9e7a6fSAndy Grover };
24327c9e7a6fSAndy Grover 
24337c9e7a6fSAndy Grover static match_table_t tokens = {
24347c9e7a6fSAndy Grover 	{Opt_dev_config, "dev_config=%s"},
24350e0d7526SMike Christie 	{Opt_dev_size, "dev_size=%s"},
2436b60cb1f8SMike Christie 	{Opt_hw_block_size, "hw_block_size=%d"},
2437b60cb1f8SMike Christie 	{Opt_hw_max_sectors, "hw_max_sectors=%d"},
2438b849b456SKenjiro Nakayama 	{Opt_nl_reply_supported, "nl_reply_supported=%d"},
2439c97840c8SMike Christie 	{Opt_max_data_area_mb, "max_data_area_mb=%d"},
244008976cb5SBodo Stroesser 	{Opt_data_pages_per_blk, "data_pages_per_blk=%d"},
2441c7ede4f0SGuixin Liu 	{Opt_cmd_ring_size_mb, "cmd_ring_size_mb=%d"},
24427c9e7a6fSAndy Grover 	{Opt_err, NULL}
24437c9e7a6fSAndy Grover };
24447c9e7a6fSAndy Grover 
tcmu_set_dev_attrib(substring_t * arg,u32 * dev_attrib)24453abaa2bfSMike Christie static int tcmu_set_dev_attrib(substring_t *arg, u32 *dev_attrib)
24463abaa2bfSMike Christie {
2447b60cb1f8SMike Christie 	int val, ret;
24483abaa2bfSMike Christie 
2449b60cb1f8SMike Christie 	ret = match_int(arg, &val);
24503abaa2bfSMike Christie 	if (ret < 0) {
2451b60cb1f8SMike Christie 		pr_err("match_int() failed for dev attrib. Error %d.\n",
2452b60cb1f8SMike Christie 		       ret);
24533abaa2bfSMike Christie 		return ret;
24543abaa2bfSMike Christie 	}
2455b60cb1f8SMike Christie 
2456b60cb1f8SMike Christie 	if (val <= 0) {
2457b60cb1f8SMike Christie 		pr_err("Invalid dev attrib value %d. Must be greater than zero.\n",
2458b60cb1f8SMike Christie 		       val);
24593abaa2bfSMike Christie 		return -EINVAL;
24603abaa2bfSMike Christie 	}
2461b60cb1f8SMike Christie 	*dev_attrib = val;
24623abaa2bfSMike Christie 	return 0;
24633abaa2bfSMike Christie }
24643abaa2bfSMike Christie 
tcmu_set_max_blocks_param(struct tcmu_dev * udev,substring_t * arg)2465c97840c8SMike Christie static int tcmu_set_max_blocks_param(struct tcmu_dev *udev, substring_t *arg)
2466c97840c8SMike Christie {
2467f5ce815fSBodo Stroesser 	int val, ret;
2468e719afdcSBodo Stroesser 	uint32_t pages_per_blk = udev->data_pages_per_blk;
2469c97840c8SMike Christie 
2470c97840c8SMike Christie 	ret = match_int(arg, &val);
2471c97840c8SMike Christie 	if (ret < 0) {
2472c97840c8SMike Christie 		pr_err("match_int() failed for max_data_area_mb=. Error %d.\n",
2473c97840c8SMike Christie 		       ret);
2474c97840c8SMike Christie 		return ret;
2475c97840c8SMike Christie 	}
2476f5ce815fSBodo Stroesser 	if (val <= 0) {
2477c97840c8SMike Christie 		pr_err("Invalid max_data_area %d.\n", val);
2478c97840c8SMike Christie 		return -EINVAL;
2479c97840c8SMike Christie 	}
2480f5ce815fSBodo Stroesser 	if (val > TCMU_PAGES_TO_MBS(tcmu_global_max_pages)) {
2481f5ce815fSBodo Stroesser 		pr_err("%d is too large. Adjusting max_data_area_mb to global limit of %u\n",
2482f5ce815fSBodo Stroesser 		       val, TCMU_PAGES_TO_MBS(tcmu_global_max_pages));
2483f5ce815fSBodo Stroesser 		val = TCMU_PAGES_TO_MBS(tcmu_global_max_pages);
2484f5ce815fSBodo Stroesser 	}
2485e719afdcSBodo Stroesser 	if (TCMU_MBS_TO_PAGES(val) < pages_per_blk) {
2486e719afdcSBodo Stroesser 		pr_err("Invalid max_data_area %d (%zu pages): smaller than data_pages_per_blk (%u pages).\n",
2487e719afdcSBodo Stroesser 		       val, TCMU_MBS_TO_PAGES(val), pages_per_blk);
2488f5ce815fSBodo Stroesser 		return -EINVAL;
2489f5ce815fSBodo Stroesser 	}
2490c97840c8SMike Christie 
2491c97840c8SMike Christie 	mutex_lock(&udev->cmdr_lock);
2492c97840c8SMike Christie 	if (udev->data_bitmap) {
2493c97840c8SMike Christie 		pr_err("Cannot set max_data_area_mb after it has been enabled.\n");
2494c97840c8SMike Christie 		ret = -EINVAL;
2495c97840c8SMike Christie 		goto unlock;
2496c97840c8SMike Christie 	}
2497c97840c8SMike Christie 
2498f5ce815fSBodo Stroesser 	udev->data_area_mb = val;
2499e719afdcSBodo Stroesser 	udev->max_blocks = TCMU_MBS_TO_PAGES(val) / pages_per_blk;
2500c97840c8SMike Christie 
2501c97840c8SMike Christie unlock:
2502c97840c8SMike Christie 	mutex_unlock(&udev->cmdr_lock);
2503c97840c8SMike Christie 	return ret;
2504c97840c8SMike Christie }
2505c97840c8SMike Christie 
tcmu_set_data_pages_per_blk(struct tcmu_dev * udev,substring_t * arg)250608976cb5SBodo Stroesser static int tcmu_set_data_pages_per_blk(struct tcmu_dev *udev, substring_t *arg)
250708976cb5SBodo Stroesser {
250808976cb5SBodo Stroesser 	int val, ret;
250908976cb5SBodo Stroesser 
251008976cb5SBodo Stroesser 	ret = match_int(arg, &val);
251108976cb5SBodo Stroesser 	if (ret < 0) {
251208976cb5SBodo Stroesser 		pr_err("match_int() failed for data_pages_per_blk=. Error %d.\n",
251308976cb5SBodo Stroesser 		       ret);
251408976cb5SBodo Stroesser 		return ret;
251508976cb5SBodo Stroesser 	}
251608976cb5SBodo Stroesser 
251708976cb5SBodo Stroesser 	if (val > TCMU_MBS_TO_PAGES(udev->data_area_mb)) {
251808976cb5SBodo Stroesser 		pr_err("Invalid data_pages_per_blk %d: greater than max_data_area_mb %d -> %zd pages).\n",
251908976cb5SBodo Stroesser 		       val, udev->data_area_mb,
252008976cb5SBodo Stroesser 		       TCMU_MBS_TO_PAGES(udev->data_area_mb));
252108976cb5SBodo Stroesser 		return -EINVAL;
252208976cb5SBodo Stroesser 	}
252308976cb5SBodo Stroesser 
252408976cb5SBodo Stroesser 	mutex_lock(&udev->cmdr_lock);
252508976cb5SBodo Stroesser 	if (udev->data_bitmap) {
252608976cb5SBodo Stroesser 		pr_err("Cannot set data_pages_per_blk after it has been enabled.\n");
252708976cb5SBodo Stroesser 		ret = -EINVAL;
252808976cb5SBodo Stroesser 		goto unlock;
252908976cb5SBodo Stroesser 	}
253008976cb5SBodo Stroesser 
253108976cb5SBodo Stroesser 	udev->data_pages_per_blk = val;
253208976cb5SBodo Stroesser 	udev->max_blocks = TCMU_MBS_TO_PAGES(udev->data_area_mb) / val;
253308976cb5SBodo Stroesser 
253408976cb5SBodo Stroesser unlock:
253508976cb5SBodo Stroesser 	mutex_unlock(&udev->cmdr_lock);
253608976cb5SBodo Stroesser 	return ret;
253708976cb5SBodo Stroesser }
253808976cb5SBodo Stroesser 
tcmu_set_cmd_ring_size(struct tcmu_dev * udev,substring_t * arg)2539c7ede4f0SGuixin Liu static int tcmu_set_cmd_ring_size(struct tcmu_dev *udev, substring_t *arg)
2540c7ede4f0SGuixin Liu {
2541c7ede4f0SGuixin Liu 	int val, ret;
2542c7ede4f0SGuixin Liu 
2543c7ede4f0SGuixin Liu 	ret = match_int(arg, &val);
2544c7ede4f0SGuixin Liu 	if (ret < 0) {
2545c7ede4f0SGuixin Liu 		pr_err("match_int() failed for cmd_ring_size_mb=. Error %d.\n",
2546c7ede4f0SGuixin Liu 		       ret);
2547c7ede4f0SGuixin Liu 		return ret;
2548c7ede4f0SGuixin Liu 	}
2549c7ede4f0SGuixin Liu 
2550c7ede4f0SGuixin Liu 	if (val <= 0) {
2551c7ede4f0SGuixin Liu 		pr_err("Invalid cmd_ring_size_mb %d.\n", val);
2552c7ede4f0SGuixin Liu 		return -EINVAL;
2553c7ede4f0SGuixin Liu 	}
2554c7ede4f0SGuixin Liu 
2555c7ede4f0SGuixin Liu 	mutex_lock(&udev->cmdr_lock);
2556c7ede4f0SGuixin Liu 	if (udev->data_bitmap) {
2557c7ede4f0SGuixin Liu 		pr_err("Cannot set cmd_ring_size_mb after it has been enabled.\n");
2558c7ede4f0SGuixin Liu 		ret = -EINVAL;
2559c7ede4f0SGuixin Liu 		goto unlock;
2560c7ede4f0SGuixin Liu 	}
2561c7ede4f0SGuixin Liu 
2562c7ede4f0SGuixin Liu 	udev->cmdr_size = (val << 20) - CMDR_OFF;
2563c7ede4f0SGuixin Liu 	if (val > (MB_CMDR_SIZE_DEF >> 20)) {
2564c7ede4f0SGuixin Liu 		pr_err("%d is too large. Adjusting cmd_ring_size_mb to global limit of %u\n",
2565c7ede4f0SGuixin Liu 		       val, (MB_CMDR_SIZE_DEF >> 20));
2566c7ede4f0SGuixin Liu 		udev->cmdr_size = CMDR_SIZE_DEF;
2567c7ede4f0SGuixin Liu 	}
2568c7ede4f0SGuixin Liu 
2569c7ede4f0SGuixin Liu unlock:
2570c7ede4f0SGuixin Liu 	mutex_unlock(&udev->cmdr_lock);
2571c7ede4f0SGuixin Liu 	return ret;
2572c7ede4f0SGuixin Liu }
2573c7ede4f0SGuixin Liu 
tcmu_set_configfs_dev_params(struct se_device * dev,const char * page,ssize_t count)25747c9e7a6fSAndy Grover static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev,
25757c9e7a6fSAndy Grover 		const char *page, ssize_t count)
25767c9e7a6fSAndy Grover {
25777c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
25780e0d7526SMike Christie 	char *orig, *ptr, *opts;
25797c9e7a6fSAndy Grover 	substring_t args[MAX_OPT_ARGS];
2580c97840c8SMike Christie 	int ret = 0, token;
25817c9e7a6fSAndy Grover 
25827c9e7a6fSAndy Grover 	opts = kstrdup(page, GFP_KERNEL);
25837c9e7a6fSAndy Grover 	if (!opts)
25847c9e7a6fSAndy Grover 		return -ENOMEM;
25857c9e7a6fSAndy Grover 
25867c9e7a6fSAndy Grover 	orig = opts;
25877c9e7a6fSAndy Grover 
25887c9e7a6fSAndy Grover 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
25897c9e7a6fSAndy Grover 		if (!*ptr)
25907c9e7a6fSAndy Grover 			continue;
25917c9e7a6fSAndy Grover 
25927c9e7a6fSAndy Grover 		token = match_token(ptr, tokens, args);
25937c9e7a6fSAndy Grover 		switch (token) {
25947c9e7a6fSAndy Grover 		case Opt_dev_config:
25957c9e7a6fSAndy Grover 			if (match_strlcpy(udev->dev_config, &args[0],
25967c9e7a6fSAndy Grover 					  TCMU_CONFIG_LEN) == 0) {
25977c9e7a6fSAndy Grover 				ret = -EINVAL;
25987c9e7a6fSAndy Grover 				break;
25997c9e7a6fSAndy Grover 			}
26007c9e7a6fSAndy Grover 			pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config);
26017c9e7a6fSAndy Grover 			break;
26027c9e7a6fSAndy Grover 		case Opt_dev_size:
26030e0d7526SMike Christie 			ret = match_u64(&args[0], &udev->dev_size);
26047c9e7a6fSAndy Grover 			if (ret < 0)
26050e0d7526SMike Christie 				pr_err("match_u64() failed for dev_size=. Error %d.\n",
26060e0d7526SMike Christie 				       ret);
26077c9e7a6fSAndy Grover 			break;
26089c1cd1b6SAndy Grover 		case Opt_hw_block_size:
26093abaa2bfSMike Christie 			ret = tcmu_set_dev_attrib(&args[0],
26103abaa2bfSMike Christie 					&(dev->dev_attrib.hw_block_size));
26119c1cd1b6SAndy Grover 			break;
26123abaa2bfSMike Christie 		case Opt_hw_max_sectors:
26133abaa2bfSMike Christie 			ret = tcmu_set_dev_attrib(&args[0],
26143abaa2bfSMike Christie 					&(dev->dev_attrib.hw_max_sectors));
26159c1cd1b6SAndy Grover 			break;
2616b849b456SKenjiro Nakayama 		case Opt_nl_reply_supported:
2617b60cb1f8SMike Christie 			ret = match_int(&args[0], &udev->nl_reply_supported);
2618b849b456SKenjiro Nakayama 			if (ret < 0)
2619b60cb1f8SMike Christie 				pr_err("match_int() failed for nl_reply_supported=. Error %d.\n",
2620b60cb1f8SMike Christie 				       ret);
2621b849b456SKenjiro Nakayama 			break;
262280eb8761SMike Christie 		case Opt_max_data_area_mb:
2623c97840c8SMike Christie 			ret = tcmu_set_max_blocks_param(udev, &args[0]);
262480eb8761SMike Christie 			break;
262508976cb5SBodo Stroesser 		case Opt_data_pages_per_blk:
262608976cb5SBodo Stroesser 			ret = tcmu_set_data_pages_per_blk(udev, &args[0]);
262708976cb5SBodo Stroesser 			break;
2628c7ede4f0SGuixin Liu 		case Opt_cmd_ring_size_mb:
2629c7ede4f0SGuixin Liu 			ret = tcmu_set_cmd_ring_size(udev, &args[0]);
2630c7ede4f0SGuixin Liu 			break;
26317c9e7a6fSAndy Grover 		default:
26327c9e7a6fSAndy Grover 			break;
26337c9e7a6fSAndy Grover 		}
26342579325cSMike Christie 
26352579325cSMike Christie 		if (ret)
26362579325cSMike Christie 			break;
26377c9e7a6fSAndy Grover 	}
26387c9e7a6fSAndy Grover 
26397c9e7a6fSAndy Grover 	kfree(orig);
26407c9e7a6fSAndy Grover 	return (!ret) ? count : ret;
26417c9e7a6fSAndy Grover }
26427c9e7a6fSAndy Grover 
tcmu_show_configfs_dev_params(struct se_device * dev,char * b)26437c9e7a6fSAndy Grover static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b)
26447c9e7a6fSAndy Grover {
26457c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
26467c9e7a6fSAndy Grover 	ssize_t bl = 0;
26477c9e7a6fSAndy Grover 
26487c9e7a6fSAndy Grover 	bl = sprintf(b + bl, "Config: %s ",
26497c9e7a6fSAndy Grover 		     udev->dev_config[0] ? udev->dev_config : "NULL");
26500e0d7526SMike Christie 	bl += sprintf(b + bl, "Size: %llu ", udev->dev_size);
265108976cb5SBodo Stroesser 	bl += sprintf(b + bl, "MaxDataAreaMB: %u ", udev->data_area_mb);
2652c7ede4f0SGuixin Liu 	bl += sprintf(b + bl, "DataPagesPerBlk: %u ", udev->data_pages_per_blk);
2653c7ede4f0SGuixin Liu 	bl += sprintf(b + bl, "CmdRingSizeMB: %u\n",
2654c7ede4f0SGuixin Liu 		      (udev->cmdr_size + CMDR_OFF) >> 20);
26557c9e7a6fSAndy Grover 
26567c9e7a6fSAndy Grover 	return bl;
26577c9e7a6fSAndy Grover }
26587c9e7a6fSAndy Grover 
tcmu_get_blocks(struct se_device * dev)26597c9e7a6fSAndy Grover static sector_t tcmu_get_blocks(struct se_device *dev)
26607c9e7a6fSAndy Grover {
26617c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
26627c9e7a6fSAndy Grover 
26637c9e7a6fSAndy Grover 	return div_u64(udev->dev_size - dev->dev_attrib.block_size,
26647c9e7a6fSAndy Grover 		       dev->dev_attrib.block_size);
26657c9e7a6fSAndy Grover }
26667c9e7a6fSAndy Grover 
26677c9e7a6fSAndy Grover static sense_reason_t
tcmu_parse_cdb(struct se_cmd * cmd)26687c9e7a6fSAndy Grover tcmu_parse_cdb(struct se_cmd *cmd)
26697c9e7a6fSAndy Grover {
267002eb924fSAndy Grover 	return passthrough_parse_cdb(cmd, tcmu_queue_cmd);
26719c1cd1b6SAndy Grover }
26729c1cd1b6SAndy Grover 
tcmu_cmd_time_out_show(struct config_item * item,char * page)26737d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_show(struct config_item *item, char *page)
26747d7a7435SNicholas Bellinger {
26757d7a7435SNicholas Bellinger 	struct se_dev_attrib *da = container_of(to_config_group(item),
26767d7a7435SNicholas Bellinger 					struct se_dev_attrib, da_group);
2677b5ab697cSKenjiro Nakayama 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
26787d7a7435SNicholas Bellinger 
26797d7a7435SNicholas Bellinger 	return snprintf(page, PAGE_SIZE, "%lu\n", udev->cmd_time_out / MSEC_PER_SEC);
26807d7a7435SNicholas Bellinger }
26817d7a7435SNicholas Bellinger 
tcmu_cmd_time_out_store(struct config_item * item,const char * page,size_t count)26827d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_store(struct config_item *item, const char *page,
26837d7a7435SNicholas Bellinger 				       size_t count)
26847d7a7435SNicholas Bellinger {
26857d7a7435SNicholas Bellinger 	struct se_dev_attrib *da = container_of(to_config_group(item),
26867d7a7435SNicholas Bellinger 					struct se_dev_attrib, da_group);
26877d7a7435SNicholas Bellinger 	struct tcmu_dev *udev = container_of(da->da_dev,
26887d7a7435SNicholas Bellinger 					struct tcmu_dev, se_dev);
26897d7a7435SNicholas Bellinger 	u32 val;
26907d7a7435SNicholas Bellinger 	int ret;
26917d7a7435SNicholas Bellinger 
26927d7a7435SNicholas Bellinger 	if (da->da_dev->export_count) {
26937d7a7435SNicholas Bellinger 		pr_err("Unable to set tcmu cmd_time_out while exports exist\n");
26947d7a7435SNicholas Bellinger 		return -EINVAL;
26957d7a7435SNicholas Bellinger 	}
26967d7a7435SNicholas Bellinger 
26977d7a7435SNicholas Bellinger 	ret = kstrtou32(page, 0, &val);
26987d7a7435SNicholas Bellinger 	if (ret < 0)
26997d7a7435SNicholas Bellinger 		return ret;
27007d7a7435SNicholas Bellinger 
27017d7a7435SNicholas Bellinger 	udev->cmd_time_out = val * MSEC_PER_SEC;
27027d7a7435SNicholas Bellinger 	return count;
27037d7a7435SNicholas Bellinger }
27047d7a7435SNicholas Bellinger CONFIGFS_ATTR(tcmu_, cmd_time_out);
27057d7a7435SNicholas Bellinger 
tcmu_qfull_time_out_show(struct config_item * item,char * page)27069103575aSMike Christie static ssize_t tcmu_qfull_time_out_show(struct config_item *item, char *page)
27079103575aSMike Christie {
27089103575aSMike Christie 	struct se_dev_attrib *da = container_of(to_config_group(item),
27099103575aSMike Christie 						struct se_dev_attrib, da_group);
27109103575aSMike Christie 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
27119103575aSMike Christie 
27129103575aSMike Christie 	return snprintf(page, PAGE_SIZE, "%ld\n", udev->qfull_time_out <= 0 ?
27139103575aSMike Christie 			udev->qfull_time_out :
27149103575aSMike Christie 			udev->qfull_time_out / MSEC_PER_SEC);
27159103575aSMike Christie }
27169103575aSMike Christie 
tcmu_qfull_time_out_store(struct config_item * item,const char * page,size_t count)27179103575aSMike Christie static ssize_t tcmu_qfull_time_out_store(struct config_item *item,
27189103575aSMike Christie 					 const char *page, size_t count)
27199103575aSMike Christie {
27209103575aSMike Christie 	struct se_dev_attrib *da = container_of(to_config_group(item),
27219103575aSMike Christie 					struct se_dev_attrib, da_group);
27229103575aSMike Christie 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
27239103575aSMike Christie 	s32 val;
27249103575aSMike Christie 	int ret;
27259103575aSMike Christie 
27269103575aSMike Christie 	ret = kstrtos32(page, 0, &val);
27279103575aSMike Christie 	if (ret < 0)
27289103575aSMike Christie 		return ret;
27299103575aSMike Christie 
27309103575aSMike Christie 	if (val >= 0) {
27319103575aSMike Christie 		udev->qfull_time_out = val * MSEC_PER_SEC;
2732125966dbSPrasanna Kumar Kalever 	} else if (val == -1) {
2733125966dbSPrasanna Kumar Kalever 		udev->qfull_time_out = val;
27349103575aSMike Christie 	} else {
27359103575aSMike Christie 		printk(KERN_ERR "Invalid qfull timeout value %d\n", val);
27369103575aSMike Christie 		return -EINVAL;
27379103575aSMike Christie 	}
27389103575aSMike Christie 	return count;
27399103575aSMike Christie }
27409103575aSMike Christie CONFIGFS_ATTR(tcmu_, qfull_time_out);
27419103575aSMike Christie 
tcmu_max_data_area_mb_show(struct config_item * item,char * page)274280eb8761SMike Christie static ssize_t tcmu_max_data_area_mb_show(struct config_item *item, char *page)
274380eb8761SMike Christie {
274480eb8761SMike Christie 	struct se_dev_attrib *da = container_of(to_config_group(item),
274580eb8761SMike Christie 						struct se_dev_attrib, da_group);
274680eb8761SMike Christie 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
274780eb8761SMike Christie 
2748f5ce815fSBodo Stroesser 	return snprintf(page, PAGE_SIZE, "%u\n", udev->data_area_mb);
274980eb8761SMike Christie }
275080eb8761SMike Christie CONFIGFS_ATTR_RO(tcmu_, max_data_area_mb);
275180eb8761SMike Christie 
tcmu_data_pages_per_blk_show(struct config_item * item,char * page)275208976cb5SBodo Stroesser static ssize_t tcmu_data_pages_per_blk_show(struct config_item *item,
275308976cb5SBodo Stroesser 					    char *page)
275408976cb5SBodo Stroesser {
275508976cb5SBodo Stroesser 	struct se_dev_attrib *da = container_of(to_config_group(item),
275608976cb5SBodo Stroesser 						struct se_dev_attrib, da_group);
275708976cb5SBodo Stroesser 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
275808976cb5SBodo Stroesser 
275908976cb5SBodo Stroesser 	return snprintf(page, PAGE_SIZE, "%u\n", udev->data_pages_per_blk);
276008976cb5SBodo Stroesser }
276108976cb5SBodo Stroesser CONFIGFS_ATTR_RO(tcmu_, data_pages_per_blk);
276208976cb5SBodo Stroesser 
tcmu_cmd_ring_size_mb_show(struct config_item * item,char * page)2763c7ede4f0SGuixin Liu static ssize_t tcmu_cmd_ring_size_mb_show(struct config_item *item, char *page)
2764c7ede4f0SGuixin Liu {
2765c7ede4f0SGuixin Liu 	struct se_dev_attrib *da = container_of(to_config_group(item),
2766c7ede4f0SGuixin Liu 						struct se_dev_attrib, da_group);
2767c7ede4f0SGuixin Liu 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2768c7ede4f0SGuixin Liu 
2769c7ede4f0SGuixin Liu 	return snprintf(page, PAGE_SIZE, "%u\n",
2770c7ede4f0SGuixin Liu 			(udev->cmdr_size + CMDR_OFF) >> 20);
2771c7ede4f0SGuixin Liu }
2772c7ede4f0SGuixin Liu CONFIGFS_ATTR_RO(tcmu_, cmd_ring_size_mb);
2773c7ede4f0SGuixin Liu 
tcmu_dev_config_show(struct config_item * item,char * page)27742d76443eSMike Christie static ssize_t tcmu_dev_config_show(struct config_item *item, char *page)
2775ee018252SBryant G. Ly {
2776ee018252SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
2777ee018252SBryant G. Ly 						struct se_dev_attrib, da_group);
2778ee018252SBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2779ee018252SBryant G. Ly 
2780ee018252SBryant G. Ly 	return snprintf(page, PAGE_SIZE, "%s\n", udev->dev_config);
2781ee018252SBryant G. Ly }
2782ee018252SBryant G. Ly 
tcmu_send_dev_config_event(struct tcmu_dev * udev,const char * reconfig_data)278302ccfb54SZhu Lingshan static int tcmu_send_dev_config_event(struct tcmu_dev *udev,
278402ccfb54SZhu Lingshan 				      const char *reconfig_data)
278502ccfb54SZhu Lingshan {
278602ccfb54SZhu Lingshan 	struct sk_buff *skb = NULL;
278702ccfb54SZhu Lingshan 	void *msg_header = NULL;
278802ccfb54SZhu Lingshan 	int ret = 0;
278902ccfb54SZhu Lingshan 
279002ccfb54SZhu Lingshan 	ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE,
279102ccfb54SZhu Lingshan 				      &skb, &msg_header);
279202ccfb54SZhu Lingshan 	if (ret < 0)
279302ccfb54SZhu Lingshan 		return ret;
279402ccfb54SZhu Lingshan 	ret = nla_put_string(skb, TCMU_ATTR_DEV_CFG, reconfig_data);
279502ccfb54SZhu Lingshan 	if (ret < 0) {
279602ccfb54SZhu Lingshan 		nlmsg_free(skb);
279702ccfb54SZhu Lingshan 		return ret;
279802ccfb54SZhu Lingshan 	}
279902ccfb54SZhu Lingshan 	return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE,
280006add777SMike Christie 				       skb, msg_header);
280102ccfb54SZhu Lingshan }
280202ccfb54SZhu Lingshan 
280302ccfb54SZhu Lingshan 
tcmu_dev_config_store(struct config_item * item,const char * page,size_t count)28042d76443eSMike Christie static ssize_t tcmu_dev_config_store(struct config_item *item, const char *page,
2805ee018252SBryant G. Ly 				     size_t count)
2806ee018252SBryant G. Ly {
2807ee018252SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
2808ee018252SBryant G. Ly 						struct se_dev_attrib, da_group);
2809ee018252SBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
28102d76443eSMike Christie 	int ret, len;
2811ee018252SBryant G. Ly 
28122d76443eSMike Christie 	len = strlen(page);
28132d76443eSMike Christie 	if (!len || len > TCMU_CONFIG_LEN - 1)
2814ee018252SBryant G. Ly 		return -EINVAL;
2815ee018252SBryant G. Ly 
2816ee018252SBryant G. Ly 	/* Check if device has been configured before */
281763d5be0fSMike Christie 	if (target_dev_configured(&udev->se_dev)) {
281802ccfb54SZhu Lingshan 		ret = tcmu_send_dev_config_event(udev, page);
2819ee018252SBryant G. Ly 		if (ret) {
2820ee018252SBryant G. Ly 			pr_err("Unable to reconfigure device\n");
2821ee018252SBryant G. Ly 			return ret;
2822ee018252SBryant G. Ly 		}
2823*4b2e2875SAzeem Shaikh 		strscpy(udev->dev_config, page, TCMU_CONFIG_LEN);
2824de8c5221SBryant G. Ly 
2825de8c5221SBryant G. Ly 		ret = tcmu_update_uio_info(udev);
2826de8c5221SBryant G. Ly 		if (ret)
2827de8c5221SBryant G. Ly 			return ret;
2828de8c5221SBryant G. Ly 		return count;
2829ee018252SBryant G. Ly 	}
2830*4b2e2875SAzeem Shaikh 	strscpy(udev->dev_config, page, TCMU_CONFIG_LEN);
2831ee018252SBryant G. Ly 
2832ee018252SBryant G. Ly 	return count;
2833ee018252SBryant G. Ly }
28342d76443eSMike Christie CONFIGFS_ATTR(tcmu_, dev_config);
2835ee018252SBryant G. Ly 
tcmu_dev_size_show(struct config_item * item,char * page)2836801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_show(struct config_item *item, char *page)
2837801fc54dSBryant G. Ly {
2838801fc54dSBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
2839801fc54dSBryant G. Ly 						struct se_dev_attrib, da_group);
2840801fc54dSBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2841801fc54dSBryant G. Ly 
28420e0d7526SMike Christie 	return snprintf(page, PAGE_SIZE, "%llu\n", udev->dev_size);
2843801fc54dSBryant G. Ly }
2844801fc54dSBryant G. Ly 
tcmu_send_dev_size_event(struct tcmu_dev * udev,u64 size)284584e28506SZhu Lingshan static int tcmu_send_dev_size_event(struct tcmu_dev *udev, u64 size)
284684e28506SZhu Lingshan {
284784e28506SZhu Lingshan 	struct sk_buff *skb = NULL;
284884e28506SZhu Lingshan 	void *msg_header = NULL;
284984e28506SZhu Lingshan 	int ret = 0;
285084e28506SZhu Lingshan 
285184e28506SZhu Lingshan 	ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE,
285284e28506SZhu Lingshan 				      &skb, &msg_header);
285384e28506SZhu Lingshan 	if (ret < 0)
285484e28506SZhu Lingshan 		return ret;
285584e28506SZhu Lingshan 	ret = nla_put_u64_64bit(skb, TCMU_ATTR_DEV_SIZE,
285684e28506SZhu Lingshan 				size, TCMU_ATTR_PAD);
285784e28506SZhu Lingshan 	if (ret < 0) {
285884e28506SZhu Lingshan 		nlmsg_free(skb);
285984e28506SZhu Lingshan 		return ret;
286084e28506SZhu Lingshan 	}
286184e28506SZhu Lingshan 	return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE,
286206add777SMike Christie 				       skb, msg_header);
286384e28506SZhu Lingshan }
286484e28506SZhu Lingshan 
tcmu_dev_size_store(struct config_item * item,const char * page,size_t count)2865801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_store(struct config_item *item, const char *page,
2866801fc54dSBryant G. Ly 				   size_t count)
2867801fc54dSBryant G. Ly {
2868801fc54dSBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
2869801fc54dSBryant G. Ly 						struct se_dev_attrib, da_group);
2870801fc54dSBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
28712d76443eSMike Christie 	u64 val;
2872801fc54dSBryant G. Ly 	int ret;
2873801fc54dSBryant G. Ly 
28742d76443eSMike Christie 	ret = kstrtou64(page, 0, &val);
2875801fc54dSBryant G. Ly 	if (ret < 0)
2876801fc54dSBryant G. Ly 		return ret;
2877801fc54dSBryant G. Ly 
2878801fc54dSBryant G. Ly 	/* Check if device has been configured before */
287963d5be0fSMike Christie 	if (target_dev_configured(&udev->se_dev)) {
288084e28506SZhu Lingshan 		ret = tcmu_send_dev_size_event(udev, val);
2881801fc54dSBryant G. Ly 		if (ret) {
2882801fc54dSBryant G. Ly 			pr_err("Unable to reconfigure device\n");
2883801fc54dSBryant G. Ly 			return ret;
2884801fc54dSBryant G. Ly 		}
2885801fc54dSBryant G. Ly 	}
28862d76443eSMike Christie 	udev->dev_size = val;
2887801fc54dSBryant G. Ly 	return count;
2888801fc54dSBryant G. Ly }
2889801fc54dSBryant G. Ly CONFIGFS_ATTR(tcmu_, dev_size);
2890801fc54dSBryant G. Ly 
tcmu_nl_reply_supported_show(struct config_item * item,char * page)2891b849b456SKenjiro Nakayama static ssize_t tcmu_nl_reply_supported_show(struct config_item *item,
2892b849b456SKenjiro Nakayama 		char *page)
2893b849b456SKenjiro Nakayama {
2894b849b456SKenjiro Nakayama 	struct se_dev_attrib *da = container_of(to_config_group(item),
2895b849b456SKenjiro Nakayama 						struct se_dev_attrib, da_group);
2896b849b456SKenjiro Nakayama 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2897b849b456SKenjiro Nakayama 
2898b849b456SKenjiro Nakayama 	return snprintf(page, PAGE_SIZE, "%d\n", udev->nl_reply_supported);
2899b849b456SKenjiro Nakayama }
2900b849b456SKenjiro Nakayama 
tcmu_nl_reply_supported_store(struct config_item * item,const char * page,size_t count)2901b849b456SKenjiro Nakayama static ssize_t tcmu_nl_reply_supported_store(struct config_item *item,
2902b849b456SKenjiro Nakayama 		const char *page, size_t count)
2903b849b456SKenjiro Nakayama {
2904b849b456SKenjiro Nakayama 	struct se_dev_attrib *da = container_of(to_config_group(item),
2905b849b456SKenjiro Nakayama 						struct se_dev_attrib, da_group);
2906b849b456SKenjiro Nakayama 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2907b849b456SKenjiro Nakayama 	s8 val;
2908b849b456SKenjiro Nakayama 	int ret;
2909b849b456SKenjiro Nakayama 
2910b849b456SKenjiro Nakayama 	ret = kstrtos8(page, 0, &val);
2911b849b456SKenjiro Nakayama 	if (ret < 0)
2912b849b456SKenjiro Nakayama 		return ret;
2913b849b456SKenjiro Nakayama 
2914b849b456SKenjiro Nakayama 	udev->nl_reply_supported = val;
2915b849b456SKenjiro Nakayama 	return count;
2916b849b456SKenjiro Nakayama }
2917b849b456SKenjiro Nakayama CONFIGFS_ATTR(tcmu_, nl_reply_supported);
2918b849b456SKenjiro Nakayama 
tcmu_emulate_write_cache_show(struct config_item * item,char * page)29199a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_show(struct config_item *item,
29209a8bb606SBryant G. Ly 					     char *page)
29219a8bb606SBryant G. Ly {
29229a8bb606SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
29239a8bb606SBryant G. Ly 					struct se_dev_attrib, da_group);
29249a8bb606SBryant G. Ly 
29259a8bb606SBryant G. Ly 	return snprintf(page, PAGE_SIZE, "%i\n", da->emulate_write_cache);
29269a8bb606SBryant G. Ly }
29279a8bb606SBryant G. Ly 
tcmu_send_emulate_write_cache(struct tcmu_dev * udev,u8 val)292833d065ccSZhu Lingshan static int tcmu_send_emulate_write_cache(struct tcmu_dev *udev, u8 val)
292933d065ccSZhu Lingshan {
293033d065ccSZhu Lingshan 	struct sk_buff *skb = NULL;
293133d065ccSZhu Lingshan 	void *msg_header = NULL;
293233d065ccSZhu Lingshan 	int ret = 0;
293333d065ccSZhu Lingshan 
293433d065ccSZhu Lingshan 	ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE,
293533d065ccSZhu Lingshan 				      &skb, &msg_header);
293633d065ccSZhu Lingshan 	if (ret < 0)
293733d065ccSZhu Lingshan 		return ret;
293833d065ccSZhu Lingshan 	ret = nla_put_u8(skb, TCMU_ATTR_WRITECACHE, val);
293933d065ccSZhu Lingshan 	if (ret < 0) {
294033d065ccSZhu Lingshan 		nlmsg_free(skb);
294133d065ccSZhu Lingshan 		return ret;
294233d065ccSZhu Lingshan 	}
294333d065ccSZhu Lingshan 	return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE,
294406add777SMike Christie 				       skb, msg_header);
294533d065ccSZhu Lingshan }
294633d065ccSZhu Lingshan 
tcmu_emulate_write_cache_store(struct config_item * item,const char * page,size_t count)29479a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_store(struct config_item *item,
29489a8bb606SBryant G. Ly 					      const char *page, size_t count)
29499a8bb606SBryant G. Ly {
29509a8bb606SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
29519a8bb606SBryant G. Ly 					struct se_dev_attrib, da_group);
29521068be7bSBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
29532d76443eSMike Christie 	u8 val;
29549a8bb606SBryant G. Ly 	int ret;
29559a8bb606SBryant G. Ly 
29562d76443eSMike Christie 	ret = kstrtou8(page, 0, &val);
29579a8bb606SBryant G. Ly 	if (ret < 0)
29589a8bb606SBryant G. Ly 		return ret;
29599a8bb606SBryant G. Ly 
29601068be7bSBryant G. Ly 	/* Check if device has been configured before */
296163d5be0fSMike Christie 	if (target_dev_configured(&udev->se_dev)) {
296233d065ccSZhu Lingshan 		ret = tcmu_send_emulate_write_cache(udev, val);
29631068be7bSBryant G. Ly 		if (ret) {
29641068be7bSBryant G. Ly 			pr_err("Unable to reconfigure device\n");
29651068be7bSBryant G. Ly 			return ret;
29661068be7bSBryant G. Ly 		}
29671068be7bSBryant G. Ly 	}
29682d76443eSMike Christie 
29692d76443eSMike Christie 	da->emulate_write_cache = val;
29709a8bb606SBryant G. Ly 	return count;
29719a8bb606SBryant G. Ly }
29729a8bb606SBryant G. Ly CONFIGFS_ATTR(tcmu_, emulate_write_cache);
29739a8bb606SBryant G. Ly 
tcmu_tmr_notification_show(struct config_item * item,char * page)297459526d7aSBodo Stroesser static ssize_t tcmu_tmr_notification_show(struct config_item *item, char *page)
297559526d7aSBodo Stroesser {
297659526d7aSBodo Stroesser 	struct se_dev_attrib *da = container_of(to_config_group(item),
297759526d7aSBodo Stroesser 					struct se_dev_attrib, da_group);
297859526d7aSBodo Stroesser 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
297959526d7aSBodo Stroesser 
298059526d7aSBodo Stroesser 	return snprintf(page, PAGE_SIZE, "%i\n",
298159526d7aSBodo Stroesser 			test_bit(TCMU_DEV_BIT_TMR_NOTIFY, &udev->flags));
298259526d7aSBodo Stroesser }
298359526d7aSBodo Stroesser 
tcmu_tmr_notification_store(struct config_item * item,const char * page,size_t count)298459526d7aSBodo Stroesser static ssize_t tcmu_tmr_notification_store(struct config_item *item,
298559526d7aSBodo Stroesser 					   const char *page, size_t count)
298659526d7aSBodo Stroesser {
298759526d7aSBodo Stroesser 	struct se_dev_attrib *da = container_of(to_config_group(item),
298859526d7aSBodo Stroesser 					struct se_dev_attrib, da_group);
298959526d7aSBodo Stroesser 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
299059526d7aSBodo Stroesser 	u8 val;
299159526d7aSBodo Stroesser 	int ret;
299259526d7aSBodo Stroesser 
299359526d7aSBodo Stroesser 	ret = kstrtou8(page, 0, &val);
299459526d7aSBodo Stroesser 	if (ret < 0)
299559526d7aSBodo Stroesser 		return ret;
299659526d7aSBodo Stroesser 	if (val > 1)
299759526d7aSBodo Stroesser 		return -EINVAL;
299859526d7aSBodo Stroesser 
299959526d7aSBodo Stroesser 	if (val)
300059526d7aSBodo Stroesser 		set_bit(TCMU_DEV_BIT_TMR_NOTIFY, &udev->flags);
300159526d7aSBodo Stroesser 	else
300259526d7aSBodo Stroesser 		clear_bit(TCMU_DEV_BIT_TMR_NOTIFY, &udev->flags);
300359526d7aSBodo Stroesser 	return count;
300459526d7aSBodo Stroesser }
300559526d7aSBodo Stroesser CONFIGFS_ATTR(tcmu_, tmr_notification);
300659526d7aSBodo Stroesser 
tcmu_block_dev_show(struct config_item * item,char * page)3007892782caSMike Christie static ssize_t tcmu_block_dev_show(struct config_item *item, char *page)
3008892782caSMike Christie {
3009892782caSMike Christie 	struct se_device *se_dev = container_of(to_config_group(item),
3010892782caSMike Christie 						struct se_device,
3011892782caSMike Christie 						dev_action_group);
3012892782caSMike Christie 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
3013892782caSMike Christie 
3014892782caSMike Christie 	if (test_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags))
3015892782caSMike Christie 		return snprintf(page, PAGE_SIZE, "%s\n", "blocked");
3016892782caSMike Christie 	else
3017892782caSMike Christie 		return snprintf(page, PAGE_SIZE, "%s\n", "unblocked");
3018892782caSMike Christie }
3019892782caSMike Christie 
tcmu_block_dev_store(struct config_item * item,const char * page,size_t count)3020892782caSMike Christie static ssize_t tcmu_block_dev_store(struct config_item *item, const char *page,
3021892782caSMike Christie 				    size_t count)
3022892782caSMike Christie {
3023892782caSMike Christie 	struct se_device *se_dev = container_of(to_config_group(item),
3024892782caSMike Christie 						struct se_device,
3025892782caSMike Christie 						dev_action_group);
3026892782caSMike Christie 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
3027892782caSMike Christie 	u8 val;
3028892782caSMike Christie 	int ret;
3029892782caSMike Christie 
3030a30b0473SMike Christie 	if (!target_dev_configured(&udev->se_dev)) {
3031a30b0473SMike Christie 		pr_err("Device is not configured.\n");
3032a30b0473SMike Christie 		return -EINVAL;
3033a30b0473SMike Christie 	}
3034a30b0473SMike Christie 
3035892782caSMike Christie 	ret = kstrtou8(page, 0, &val);
3036892782caSMike Christie 	if (ret < 0)
3037892782caSMike Christie 		return ret;
3038892782caSMike Christie 
3039892782caSMike Christie 	if (val > 1) {
3040892782caSMike Christie 		pr_err("Invalid block value %d\n", val);
3041892782caSMike Christie 		return -EINVAL;
3042892782caSMike Christie 	}
3043892782caSMike Christie 
3044892782caSMike Christie 	if (!val)
3045892782caSMike Christie 		tcmu_unblock_dev(udev);
3046892782caSMike Christie 	else
3047892782caSMike Christie 		tcmu_block_dev(udev);
3048892782caSMike Christie 	return count;
3049892782caSMike Christie }
3050892782caSMike Christie CONFIGFS_ATTR(tcmu_, block_dev);
3051892782caSMike Christie 
tcmu_reset_ring_store(struct config_item * item,const char * page,size_t count)3052892782caSMike Christie static ssize_t tcmu_reset_ring_store(struct config_item *item, const char *page,
3053892782caSMike Christie 				     size_t count)
3054892782caSMike Christie {
3055892782caSMike Christie 	struct se_device *se_dev = container_of(to_config_group(item),
3056892782caSMike Christie 						struct se_device,
3057892782caSMike Christie 						dev_action_group);
3058892782caSMike Christie 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
3059892782caSMike Christie 	u8 val;
3060892782caSMike Christie 	int ret;
3061892782caSMike Christie 
3062a30b0473SMike Christie 	if (!target_dev_configured(&udev->se_dev)) {
3063a30b0473SMike Christie 		pr_err("Device is not configured.\n");
3064a30b0473SMike Christie 		return -EINVAL;
3065a30b0473SMike Christie 	}
3066a30b0473SMike Christie 
3067892782caSMike Christie 	ret = kstrtou8(page, 0, &val);
3068892782caSMike Christie 	if (ret < 0)
3069892782caSMike Christie 		return ret;
3070892782caSMike Christie 
3071892782caSMike Christie 	if (val != 1 && val != 2) {
3072892782caSMike Christie 		pr_err("Invalid reset ring value %d\n", val);
3073892782caSMike Christie 		return -EINVAL;
3074892782caSMike Christie 	}
3075892782caSMike Christie 
3076892782caSMike Christie 	tcmu_reset_ring(udev, val);
3077892782caSMike Christie 	return count;
3078892782caSMike Christie }
3079892782caSMike Christie CONFIGFS_ATTR_WO(tcmu_, reset_ring);
3080892782caSMike Christie 
tcmu_free_kept_buf_store(struct config_item * item,const char * page,size_t count)3081018c1491SBodo Stroesser static ssize_t tcmu_free_kept_buf_store(struct config_item *item, const char *page,
3082018c1491SBodo Stroesser 					size_t count)
3083018c1491SBodo Stroesser {
3084018c1491SBodo Stroesser 	struct se_device *se_dev = container_of(to_config_group(item),
3085018c1491SBodo Stroesser 						struct se_device,
3086018c1491SBodo Stroesser 						dev_action_group);
3087018c1491SBodo Stroesser 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
3088018c1491SBodo Stroesser 	struct tcmu_cmd *cmd;
3089018c1491SBodo Stroesser 	u16 cmd_id;
3090018c1491SBodo Stroesser 	int ret;
3091018c1491SBodo Stroesser 
3092018c1491SBodo Stroesser 	if (!target_dev_configured(&udev->se_dev)) {
3093018c1491SBodo Stroesser 		pr_err("Device is not configured.\n");
3094018c1491SBodo Stroesser 		return -EINVAL;
3095018c1491SBodo Stroesser 	}
3096018c1491SBodo Stroesser 
3097018c1491SBodo Stroesser 	ret = kstrtou16(page, 0, &cmd_id);
3098018c1491SBodo Stroesser 	if (ret < 0)
3099018c1491SBodo Stroesser 		return ret;
3100018c1491SBodo Stroesser 
3101018c1491SBodo Stroesser 	mutex_lock(&udev->cmdr_lock);
3102018c1491SBodo Stroesser 
3103018c1491SBodo Stroesser 	{
3104018c1491SBodo Stroesser 		XA_STATE(xas, &udev->commands, cmd_id);
3105018c1491SBodo Stroesser 
3106018c1491SBodo Stroesser 		xas_lock(&xas);
3107018c1491SBodo Stroesser 		cmd = xas_load(&xas);
3108018c1491SBodo Stroesser 		if (!cmd) {
3109018c1491SBodo Stroesser 			pr_err("free_kept_buf: cmd_id %d not found\n", cmd_id);
3110018c1491SBodo Stroesser 			count = -EINVAL;
3111018c1491SBodo Stroesser 			xas_unlock(&xas);
3112018c1491SBodo Stroesser 			goto out_unlock;
3113018c1491SBodo Stroesser 		}
3114018c1491SBodo Stroesser 		if (!test_bit(TCMU_CMD_BIT_KEEP_BUF, &cmd->flags)) {
3115018c1491SBodo Stroesser 			pr_err("free_kept_buf: cmd_id %d was not completed with KEEP_BUF\n",
3116018c1491SBodo Stroesser 			       cmd_id);
3117018c1491SBodo Stroesser 			count = -EINVAL;
3118018c1491SBodo Stroesser 			xas_unlock(&xas);
3119018c1491SBodo Stroesser 			goto out_unlock;
3120018c1491SBodo Stroesser 		}
3121018c1491SBodo Stroesser 		xas_store(&xas, NULL);
3122018c1491SBodo Stroesser 		xas_unlock(&xas);
3123018c1491SBodo Stroesser 	}
3124018c1491SBodo Stroesser 
3125018c1491SBodo Stroesser 	tcmu_cmd_free_data(cmd, cmd->dbi_cnt);
3126018c1491SBodo Stroesser 	tcmu_free_cmd(cmd);
3127018c1491SBodo Stroesser 	/*
3128018c1491SBodo Stroesser 	 * We only freed data space, not ring space. Therefore we dont call
3129018c1491SBodo Stroesser 	 * run_tmr_queue, but call run_qfull_queue if tmr_list is empty.
3130018c1491SBodo Stroesser 	 */
3131018c1491SBodo Stroesser 	if (list_empty(&udev->tmr_queue))
3132018c1491SBodo Stroesser 		run_qfull_queue(udev, false);
3133018c1491SBodo Stroesser 
3134018c1491SBodo Stroesser out_unlock:
3135018c1491SBodo Stroesser 	mutex_unlock(&udev->cmdr_lock);
3136018c1491SBodo Stroesser 	return count;
3137018c1491SBodo Stroesser }
3138018c1491SBodo Stroesser CONFIGFS_ATTR_WO(tcmu_, free_kept_buf);
3139018c1491SBodo Stroesser 
31405821783bSColin Ian King static struct configfs_attribute *tcmu_attrib_attrs[] = {
3141801fc54dSBryant G. Ly 	&tcmu_attr_cmd_time_out,
31429103575aSMike Christie 	&tcmu_attr_qfull_time_out,
314380eb8761SMike Christie 	&tcmu_attr_max_data_area_mb,
314408976cb5SBodo Stroesser 	&tcmu_attr_data_pages_per_blk,
3145c7ede4f0SGuixin Liu 	&tcmu_attr_cmd_ring_size_mb,
31462d76443eSMike Christie 	&tcmu_attr_dev_config,
3147801fc54dSBryant G. Ly 	&tcmu_attr_dev_size,
3148801fc54dSBryant G. Ly 	&tcmu_attr_emulate_write_cache,
314959526d7aSBodo Stroesser 	&tcmu_attr_tmr_notification,
3150b849b456SKenjiro Nakayama 	&tcmu_attr_nl_reply_supported,
3151801fc54dSBryant G. Ly 	NULL,
3152801fc54dSBryant G. Ly };
3153801fc54dSBryant G. Ly 
31547d7a7435SNicholas Bellinger static struct configfs_attribute **tcmu_attrs;
31557d7a7435SNicholas Bellinger 
3156892782caSMike Christie static struct configfs_attribute *tcmu_action_attrs[] = {
3157892782caSMike Christie 	&tcmu_attr_block_dev,
3158892782caSMike Christie 	&tcmu_attr_reset_ring,
3159018c1491SBodo Stroesser 	&tcmu_attr_free_kept_buf,
3160892782caSMike Christie 	NULL,
3161892782caSMike Christie };
3162892782caSMike Christie 
31637d7a7435SNicholas Bellinger static struct target_backend_ops tcmu_ops = {
31647c9e7a6fSAndy Grover 	.name			= "user",
31657c9e7a6fSAndy Grover 	.owner			= THIS_MODULE,
316669088a04SBodo Stroesser 	.transport_flags_default = TRANSPORT_FLAG_PASSTHROUGH,
3167356ba2a8SBodo Stroesser 	.transport_flags_changeable = TRANSPORT_FLAG_PASSTHROUGH_PGR |
3168356ba2a8SBodo Stroesser 				      TRANSPORT_FLAG_PASSTHROUGH_ALUA,
31697c9e7a6fSAndy Grover 	.attach_hba		= tcmu_attach_hba,
31707c9e7a6fSAndy Grover 	.detach_hba		= tcmu_detach_hba,
31717c9e7a6fSAndy Grover 	.alloc_device		= tcmu_alloc_device,
31727c9e7a6fSAndy Grover 	.configure_device	= tcmu_configure_device,
317392634706SMike Christie 	.destroy_device		= tcmu_destroy_device,
31747c9e7a6fSAndy Grover 	.free_device		= tcmu_free_device,
31756888da81SMike Christie 	.unplug_device		= tcmu_unplug_device,
31766888da81SMike Christie 	.plug_device		= tcmu_plug_device,
31777c9e7a6fSAndy Grover 	.parse_cdb		= tcmu_parse_cdb,
3178bc2d214aSBodo Stroesser 	.tmr_notify		= tcmu_tmr_notify,
31797c9e7a6fSAndy Grover 	.set_configfs_dev_params = tcmu_set_configfs_dev_params,
31807c9e7a6fSAndy Grover 	.show_configfs_dev_params = tcmu_show_configfs_dev_params,
31817c9e7a6fSAndy Grover 	.get_device_type	= sbc_get_device_type,
31827c9e7a6fSAndy Grover 	.get_blocks		= tcmu_get_blocks,
3183892782caSMike Christie 	.tb_dev_action_attrs	= tcmu_action_attrs,
31847c9e7a6fSAndy Grover };
31857c9e7a6fSAndy Grover 
find_free_blocks(void)318689ec9cfdSMike Christie static void find_free_blocks(void)
3187b6df4b79SXiubo Li {
3188b6df4b79SXiubo Li 	struct tcmu_dev *udev;
3189b6df4b79SXiubo Li 	loff_t off;
3190f5ce815fSBodo Stroesser 	u32 pages_freed, total_pages_freed = 0;
3191f5ce815fSBodo Stroesser 	u32 start, end, block, total_blocks_freed = 0;
3192af1dd7ffSMike Christie 
31938b084d9dSBodo Stroesser 	if (atomic_read(&global_page_count) <= tcmu_global_max_pages)
3194af1dd7ffSMike Christie 		return;
3195b6df4b79SXiubo Li 
3196b6df4b79SXiubo Li 	mutex_lock(&root_udev_mutex);
3197b6df4b79SXiubo Li 	list_for_each_entry(udev, &root_udev, node) {
3198b6df4b79SXiubo Li 		mutex_lock(&udev->cmdr_lock);
3199b6df4b79SXiubo Li 
3200dc335a99SMike Christie 		if (!target_dev_configured(&udev->se_dev)) {
3201dc335a99SMike Christie 			mutex_unlock(&udev->cmdr_lock);
3202dc335a99SMike Christie 			continue;
3203dc335a99SMike Christie 		}
3204dc335a99SMike Christie 
3205b6df4b79SXiubo Li 		/* Try to complete the finished commands first */
3206bc2d214aSBodo Stroesser 		if (tcmu_handle_completions(udev))
3207bc2d214aSBodo Stroesser 			run_qfull_queue(udev, false);
3208b6df4b79SXiubo Li 
3209af1dd7ffSMike Christie 		/* Skip the udevs in idle */
3210af1dd7ffSMike Christie 		if (!udev->dbi_thresh) {
3211b6df4b79SXiubo Li 			mutex_unlock(&udev->cmdr_lock);
3212b6df4b79SXiubo Li 			continue;
3213b6df4b79SXiubo Li 		}
3214b6df4b79SXiubo Li 
3215b6df4b79SXiubo Li 		end = udev->dbi_max + 1;
3216b6df4b79SXiubo Li 		block = find_last_bit(udev->data_bitmap, end);
3217b6df4b79SXiubo Li 		if (block == udev->dbi_max) {
3218b6df4b79SXiubo Li 			/*
3219af1dd7ffSMike Christie 			 * The last bit is dbi_max, so it is not possible
3220af1dd7ffSMike Christie 			 * reclaim any blocks.
3221b6df4b79SXiubo Li 			 */
3222b6df4b79SXiubo Li 			mutex_unlock(&udev->cmdr_lock);
3223b6df4b79SXiubo Li 			continue;
3224b6df4b79SXiubo Li 		} else if (block == end) {
3225b6df4b79SXiubo Li 			/* The current udev will goto idle state */
3226b6df4b79SXiubo Li 			udev->dbi_thresh = start = 0;
3227b6df4b79SXiubo Li 			udev->dbi_max = 0;
3228b6df4b79SXiubo Li 		} else {
3229b6df4b79SXiubo Li 			udev->dbi_thresh = start = block + 1;
3230b6df4b79SXiubo Li 			udev->dbi_max = block;
3231b6df4b79SXiubo Li 		}
3232b6df4b79SXiubo Li 
3233bb9b9eb0SXiaoguang Wang 		/*
3234bb9b9eb0SXiaoguang Wang 		 * Release the block pages.
3235bb9b9eb0SXiaoguang Wang 		 *
3236bb9b9eb0SXiaoguang Wang 		 * Also note that since tcmu_vma_fault() gets an extra page
3237bb9b9eb0SXiaoguang Wang 		 * refcount, tcmu_blocks_release() won't free pages if pages
3238bb9b9eb0SXiaoguang Wang 		 * are mapped. This means it is safe to call
3239bb9b9eb0SXiaoguang Wang 		 * tcmu_blocks_release() before unmap_mapping_range() which
3240bb9b9eb0SXiaoguang Wang 		 * drops the refcount of any pages it unmaps and thus releases
3241bb9b9eb0SXiaoguang Wang 		 * them.
3242bb9b9eb0SXiaoguang Wang 		 */
3243bb9b9eb0SXiaoguang Wang 		pages_freed = tcmu_blocks_release(udev, start, end - 1);
3244bb9b9eb0SXiaoguang Wang 
3245b6df4b79SXiubo Li 		/* Here will truncate the data area from off */
3246e719afdcSBodo Stroesser 		off = udev->data_off + (loff_t)start * udev->data_blk_size;
3247b6df4b79SXiubo Li 		unmap_mapping_range(udev->inode->i_mapping, off, 0, 1);
3248b6df4b79SXiubo Li 
3249b6df4b79SXiubo Li 		mutex_unlock(&udev->cmdr_lock);
3250af1dd7ffSMike Christie 
3251f5ce815fSBodo Stroesser 		total_pages_freed += pages_freed;
3252f5ce815fSBodo Stroesser 		total_blocks_freed += end - start;
3253f5ce815fSBodo Stroesser 		pr_debug("Freed %u pages (total %u) from %u blocks (total %u) from %s.\n",
3254f5ce815fSBodo Stroesser 			 pages_freed, total_pages_freed, end - start,
3255f5ce815fSBodo Stroesser 			 total_blocks_freed, udev->name);
3256b6df4b79SXiubo Li 	}
325789ec9cfdSMike Christie 	mutex_unlock(&root_udev_mutex);
325889ec9cfdSMike Christie 
32598b084d9dSBodo Stroesser 	if (atomic_read(&global_page_count) > tcmu_global_max_pages)
3260af1dd7ffSMike Christie 		schedule_delayed_work(&tcmu_unmap_work, msecs_to_jiffies(5000));
3261b6df4b79SXiubo Li }
3262b6df4b79SXiubo Li 
check_timedout_devices(void)3263488ebe4cSMike Christie static void check_timedout_devices(void)
3264488ebe4cSMike Christie {
3265488ebe4cSMike Christie 	struct tcmu_dev *udev, *tmp_dev;
326661fb2482SBodo Stroesser 	struct tcmu_cmd *cmd, *tmp_cmd;
3267488ebe4cSMike Christie 	LIST_HEAD(devs);
3268488ebe4cSMike Christie 
3269488ebe4cSMike Christie 	spin_lock_bh(&timed_out_udevs_lock);
3270488ebe4cSMike Christie 	list_splice_init(&timed_out_udevs, &devs);
3271488ebe4cSMike Christie 
3272488ebe4cSMike Christie 	list_for_each_entry_safe(udev, tmp_dev, &devs, timedout_entry) {
3273488ebe4cSMike Christie 		list_del_init(&udev->timedout_entry);
3274488ebe4cSMike Christie 		spin_unlock_bh(&timed_out_udevs_lock);
3275488ebe4cSMike Christie 
32766fddcb77SMike Christie 		mutex_lock(&udev->cmdr_lock);
3277a94a2572SXiubo Li 
327861fb2482SBodo Stroesser 		/*
327961fb2482SBodo Stroesser 		 * If cmd_time_out is disabled but qfull is set deadline
328061fb2482SBodo Stroesser 		 * will only reflect the qfull timeout. Ignore it.
328161fb2482SBodo Stroesser 		 */
328261fb2482SBodo Stroesser 		if (udev->cmd_time_out) {
328361fb2482SBodo Stroesser 			list_for_each_entry_safe(cmd, tmp_cmd,
328461fb2482SBodo Stroesser 						 &udev->inflight_queue,
328561fb2482SBodo Stroesser 						 queue_entry) {
328661fb2482SBodo Stroesser 				tcmu_check_expired_ring_cmd(cmd);
328761fb2482SBodo Stroesser 			}
328861fb2482SBodo Stroesser 			tcmu_set_next_deadline(&udev->inflight_queue,
328961fb2482SBodo Stroesser 					       &udev->cmd_timer);
329061fb2482SBodo Stroesser 		}
329161fb2482SBodo Stroesser 		list_for_each_entry_safe(cmd, tmp_cmd, &udev->qfull_queue,
329261fb2482SBodo Stroesser 					 queue_entry) {
329361fb2482SBodo Stroesser 			tcmu_check_expired_queue_cmd(cmd);
329461fb2482SBodo Stroesser 		}
3295a94a2572SXiubo Li 		tcmu_set_next_deadline(&udev->qfull_queue, &udev->qfull_timer);
3296a94a2572SXiubo Li 
32976fddcb77SMike Christie 		mutex_unlock(&udev->cmdr_lock);
3298488ebe4cSMike Christie 
3299488ebe4cSMike Christie 		spin_lock_bh(&timed_out_udevs_lock);
3300488ebe4cSMike Christie 	}
3301488ebe4cSMike Christie 
3302488ebe4cSMike Christie 	spin_unlock_bh(&timed_out_udevs_lock);
3303488ebe4cSMike Christie }
3304488ebe4cSMike Christie 
tcmu_unmap_work_fn(struct work_struct * work)33059972cebbSMike Christie static void tcmu_unmap_work_fn(struct work_struct *work)
330689ec9cfdSMike Christie {
3307488ebe4cSMike Christie 	check_timedout_devices();
330889ec9cfdSMike Christie 	find_free_blocks();
330989ec9cfdSMike Christie }
331089ec9cfdSMike Christie 
tcmu_module_init(void)33117c9e7a6fSAndy Grover static int __init tcmu_module_init(void)
33127c9e7a6fSAndy Grover {
3313801fc54dSBryant G. Ly 	int ret, i, k, len = 0;
33147c9e7a6fSAndy Grover 
33157c9e7a6fSAndy Grover 	BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0);
33167c9e7a6fSAndy Grover 
3317af1dd7ffSMike Christie 	INIT_DELAYED_WORK(&tcmu_unmap_work, tcmu_unmap_work_fn);
33189972cebbSMike Christie 
33197c9e7a6fSAndy Grover 	tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache",
33207c9e7a6fSAndy Grover 				sizeof(struct tcmu_cmd),
33217c9e7a6fSAndy Grover 				__alignof__(struct tcmu_cmd),
33227c9e7a6fSAndy Grover 				0, NULL);
33237c9e7a6fSAndy Grover 	if (!tcmu_cmd_cache)
33247c9e7a6fSAndy Grover 		return -ENOMEM;
33257c9e7a6fSAndy Grover 
33267c9e7a6fSAndy Grover 	tcmu_root_device = root_device_register("tcm_user");
33277c9e7a6fSAndy Grover 	if (IS_ERR(tcmu_root_device)) {
33287c9e7a6fSAndy Grover 		ret = PTR_ERR(tcmu_root_device);
33297c9e7a6fSAndy Grover 		goto out_free_cache;
33307c9e7a6fSAndy Grover 	}
33317c9e7a6fSAndy Grover 
33327c9e7a6fSAndy Grover 	ret = genl_register_family(&tcmu_genl_family);
33337c9e7a6fSAndy Grover 	if (ret < 0) {
33347c9e7a6fSAndy Grover 		goto out_unreg_device;
33357c9e7a6fSAndy Grover 	}
33367c9e7a6fSAndy Grover 
33374703b625SBodo Stroesser 	for (i = 0; passthrough_attrib_attrs[i] != NULL; i++)
33387d7a7435SNicholas Bellinger 		len += sizeof(struct configfs_attribute *);
33394703b625SBodo Stroesser 	for (i = 0; passthrough_pr_attrib_attrs[i] != NULL; i++)
3340801fc54dSBryant G. Ly 		len += sizeof(struct configfs_attribute *);
33414703b625SBodo Stroesser 	for (i = 0; tcmu_attrib_attrs[i] != NULL; i++)
33424703b625SBodo Stroesser 		len += sizeof(struct configfs_attribute *);
3343801fc54dSBryant G. Ly 	len += sizeof(struct configfs_attribute *);
33447d7a7435SNicholas Bellinger 
33457d7a7435SNicholas Bellinger 	tcmu_attrs = kzalloc(len, GFP_KERNEL);
33467d7a7435SNicholas Bellinger 	if (!tcmu_attrs) {
33477d7a7435SNicholas Bellinger 		ret = -ENOMEM;
33487d7a7435SNicholas Bellinger 		goto out_unreg_genl;
33497d7a7435SNicholas Bellinger 	}
33507d7a7435SNicholas Bellinger 
33514703b625SBodo Stroesser 	for (i = 0; passthrough_attrib_attrs[i] != NULL; i++)
33527d7a7435SNicholas Bellinger 		tcmu_attrs[i] = passthrough_attrib_attrs[i];
33534703b625SBodo Stroesser 	for (k = 0; passthrough_pr_attrib_attrs[k] != NULL; k++)
33544703b625SBodo Stroesser 		tcmu_attrs[i++] = passthrough_pr_attrib_attrs[k];
33554703b625SBodo Stroesser 	for (k = 0; tcmu_attrib_attrs[k] != NULL; k++)
33564703b625SBodo Stroesser 		tcmu_attrs[i++] = tcmu_attrib_attrs[k];
33577d7a7435SNicholas Bellinger 	tcmu_ops.tb_dev_attrib_attrs = tcmu_attrs;
33587d7a7435SNicholas Bellinger 
33590a06d430SChristoph Hellwig 	ret = transport_backend_register(&tcmu_ops);
33607c9e7a6fSAndy Grover 	if (ret)
33617d7a7435SNicholas Bellinger 		goto out_attrs;
33627c9e7a6fSAndy Grover 
33637c9e7a6fSAndy Grover 	return 0;
33647c9e7a6fSAndy Grover 
33657d7a7435SNicholas Bellinger out_attrs:
33667d7a7435SNicholas Bellinger 	kfree(tcmu_attrs);
33677c9e7a6fSAndy Grover out_unreg_genl:
33687c9e7a6fSAndy Grover 	genl_unregister_family(&tcmu_genl_family);
33697c9e7a6fSAndy Grover out_unreg_device:
33707c9e7a6fSAndy Grover 	root_device_unregister(tcmu_root_device);
33717c9e7a6fSAndy Grover out_free_cache:
33727c9e7a6fSAndy Grover 	kmem_cache_destroy(tcmu_cmd_cache);
33737c9e7a6fSAndy Grover 
33747c9e7a6fSAndy Grover 	return ret;
33757c9e7a6fSAndy Grover }
33767c9e7a6fSAndy Grover 
tcmu_module_exit(void)33777c9e7a6fSAndy Grover static void __exit tcmu_module_exit(void)
33787c9e7a6fSAndy Grover {
3379af1dd7ffSMike Christie 	cancel_delayed_work_sync(&tcmu_unmap_work);
33800a06d430SChristoph Hellwig 	target_backend_unregister(&tcmu_ops);
33817d7a7435SNicholas Bellinger 	kfree(tcmu_attrs);
33827c9e7a6fSAndy Grover 	genl_unregister_family(&tcmu_genl_family);
33837c9e7a6fSAndy Grover 	root_device_unregister(tcmu_root_device);
33847c9e7a6fSAndy Grover 	kmem_cache_destroy(tcmu_cmd_cache);
33857c9e7a6fSAndy Grover }
33867c9e7a6fSAndy Grover 
33877c9e7a6fSAndy Grover MODULE_DESCRIPTION("TCM USER subsystem plugin");
33887c9e7a6fSAndy Grover MODULE_AUTHOR("Shaohua Li <shli@kernel.org>");
33897c9e7a6fSAndy Grover MODULE_AUTHOR("Andy Grover <agrover@redhat.com>");
33907c9e7a6fSAndy Grover MODULE_LICENSE("GPL");
33917c9e7a6fSAndy Grover 
33927c9e7a6fSAndy Grover module_init(tcmu_module_init);
33937c9e7a6fSAndy Grover module_exit(tcmu_module_exit);
3394