17c9e7a6fSAndy Grover /*
27c9e7a6fSAndy Grover  * Copyright (C) 2013 Shaohua Li <shli@kernel.org>
37c9e7a6fSAndy Grover  * Copyright (C) 2014 Red Hat, Inc.
4f97ec7dbSIlias Tsitsimpis  * Copyright (C) 2015 Arrikto, Inc.
5141685a3SXiubo Li  * Copyright (C) 2017 Chinamobile, Inc.
67c9e7a6fSAndy Grover  *
77c9e7a6fSAndy Grover  * This program is free software; you can redistribute it and/or modify it
87c9e7a6fSAndy Grover  * under the terms and conditions of the GNU General Public License,
97c9e7a6fSAndy Grover  * version 2, as published by the Free Software Foundation.
107c9e7a6fSAndy Grover  *
117c9e7a6fSAndy Grover  * This program is distributed in the hope it will be useful, but WITHOUT
127c9e7a6fSAndy Grover  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
137c9e7a6fSAndy Grover  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
147c9e7a6fSAndy Grover  * more details.
157c9e7a6fSAndy Grover  *
167c9e7a6fSAndy Grover  * You should have received a copy of the GNU General Public License along with
177c9e7a6fSAndy Grover  * this program; if not, write to the Free Software Foundation, Inc.,
187c9e7a6fSAndy Grover  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
197c9e7a6fSAndy Grover  */
207c9e7a6fSAndy Grover 
217c9e7a6fSAndy Grover #include <linux/spinlock.h>
227c9e7a6fSAndy Grover #include <linux/module.h>
237c9e7a6fSAndy Grover #include <linux/idr.h>
24ba929992SBart Van Assche #include <linux/kernel.h>
257c9e7a6fSAndy Grover #include <linux/timer.h>
267c9e7a6fSAndy Grover #include <linux/parser.h>
275538d294SDavid S. Miller #include <linux/vmalloc.h>
287c9e7a6fSAndy Grover #include <linux/uio_driver.h>
29141685a3SXiubo Li #include <linux/radix-tree.h>
30ac64a2ceSDavid Disseldorp #include <linux/stringify.h>
3126418649SSheng Yang #include <linux/bitops.h>
32f5045724SBart Van Assche #include <linux/highmem.h>
337d7a7435SNicholas Bellinger #include <linux/configfs.h>
34b6df4b79SXiubo Li #include <linux/mutex.h>
35b6df4b79SXiubo Li #include <linux/kthread.h>
367c9e7a6fSAndy Grover #include <net/genetlink.h>
37ba929992SBart Van Assche #include <scsi/scsi_common.h>
38ba929992SBart Van Assche #include <scsi/scsi_proto.h>
397c9e7a6fSAndy Grover #include <target/target_core_base.h>
407c9e7a6fSAndy Grover #include <target/target_core_fabric.h>
417c9e7a6fSAndy Grover #include <target/target_core_backend.h>
42e9f720d6SNicholas Bellinger 
437c9e7a6fSAndy Grover #include <linux/target_core_user.h>
447c9e7a6fSAndy Grover 
457c9e7a6fSAndy Grover /*
467c9e7a6fSAndy Grover  * Define a shared-memory interface for LIO to pass SCSI commands and
477c9e7a6fSAndy Grover  * data to userspace for processing. This is to allow backends that
487c9e7a6fSAndy Grover  * are too complex for in-kernel support to be possible.
497c9e7a6fSAndy Grover  *
507c9e7a6fSAndy Grover  * It uses the UIO framework to do a lot of the device-creation and
517c9e7a6fSAndy Grover  * introspection work for us.
527c9e7a6fSAndy Grover  *
537c9e7a6fSAndy Grover  * See the .h file for how the ring is laid out. Note that while the
547c9e7a6fSAndy Grover  * command ring is defined, the particulars of the data area are
557c9e7a6fSAndy Grover  * not. Offset values in the command entry point to other locations
567c9e7a6fSAndy Grover  * internal to the mmap()ed area. There is separate space outside the
577c9e7a6fSAndy Grover  * command ring for data buffers. This leaves maximum flexibility for
587c9e7a6fSAndy Grover  * moving buffer allocations, or even page flipping or other
597c9e7a6fSAndy Grover  * allocation techniques, without altering the command ring layout.
607c9e7a6fSAndy Grover  *
617c9e7a6fSAndy Grover  * SECURITY:
627c9e7a6fSAndy Grover  * The user process must be assumed to be malicious. There's no way to
637c9e7a6fSAndy Grover  * prevent it breaking the command ring protocol if it wants, but in
647c9e7a6fSAndy Grover  * order to prevent other issues we must only ever read *data* from
657c9e7a6fSAndy Grover  * the shared memory area, not offsets or sizes. This applies to
667c9e7a6fSAndy Grover  * command ring entries as well as the mailbox. Extra code needed for
677c9e7a6fSAndy Grover  * this may have a 'UAM' comment.
687c9e7a6fSAndy Grover  */
697c9e7a6fSAndy Grover 
707c9e7a6fSAndy Grover #define TCMU_TIME_OUT (30 * MSEC_PER_SEC)
717c9e7a6fSAndy Grover 
72b6df4b79SXiubo Li /* For cmd area, the size is fixed 8MB */
73b6df4b79SXiubo Li #define CMDR_SIZE (8 * 1024 * 1024)
7426418649SSheng Yang 
75b6df4b79SXiubo Li /*
76b6df4b79SXiubo Li  * For data area, the block size is PAGE_SIZE and
77b6df4b79SXiubo Li  * the total size is 256K * PAGE_SIZE.
78b6df4b79SXiubo Li  */
79b6df4b79SXiubo Li #define DATA_BLOCK_SIZE PAGE_SIZE
80b6df4b79SXiubo Li #define DATA_BLOCK_BITS (256 * 1024)
8126418649SSheng Yang #define DATA_SIZE (DATA_BLOCK_BITS * DATA_BLOCK_SIZE)
82b6df4b79SXiubo Li #define DATA_BLOCK_INIT_BITS 128
837c9e7a6fSAndy Grover 
84b6df4b79SXiubo Li /* The total size of the ring is 8M + 256K * PAGE_SIZE */
857c9e7a6fSAndy Grover #define TCMU_RING_SIZE (CMDR_SIZE + DATA_SIZE)
867c9e7a6fSAndy Grover 
87b6df4b79SXiubo Li /* Default maximum of the global data blocks(512K * PAGE_SIZE) */
88b6df4b79SXiubo Li #define TCMU_GLOBAL_MAX_BLOCKS (512 * 1024)
89b6df4b79SXiubo Li 
907c9e7a6fSAndy Grover static struct device *tcmu_root_device;
917c9e7a6fSAndy Grover 
927c9e7a6fSAndy Grover struct tcmu_hba {
937c9e7a6fSAndy Grover 	u32 host_id;
947c9e7a6fSAndy Grover };
957c9e7a6fSAndy Grover 
967c9e7a6fSAndy Grover #define TCMU_CONFIG_LEN 256
977c9e7a6fSAndy Grover 
987c9e7a6fSAndy Grover struct tcmu_dev {
99b6df4b79SXiubo Li 	struct list_head node;
100f3cdbe39SMike Christie 	struct kref kref;
1017c9e7a6fSAndy Grover 	struct se_device se_dev;
1027c9e7a6fSAndy Grover 
1037c9e7a6fSAndy Grover 	char *name;
1047c9e7a6fSAndy Grover 	struct se_hba *hba;
1057c9e7a6fSAndy Grover 
1067c9e7a6fSAndy Grover #define TCMU_DEV_BIT_OPEN 0
1077c9e7a6fSAndy Grover #define TCMU_DEV_BIT_BROKEN 1
1087c9e7a6fSAndy Grover 	unsigned long flags;
1097c9e7a6fSAndy Grover 
1107c9e7a6fSAndy Grover 	struct uio_info uio_info;
1117c9e7a6fSAndy Grover 
112b6df4b79SXiubo Li 	struct inode *inode;
113b6df4b79SXiubo Li 
1147c9e7a6fSAndy Grover 	struct tcmu_mailbox *mb_addr;
1157c9e7a6fSAndy Grover 	size_t dev_size;
1167c9e7a6fSAndy Grover 	u32 cmdr_size;
1177c9e7a6fSAndy Grover 	u32 cmdr_last_cleaned;
1183d9b9555SAndy Grover 	/* Offset of data area from start of mb */
11926418649SSheng Yang 	/* Must add data_off and mb_addr to get the address */
1207c9e7a6fSAndy Grover 	size_t data_off;
1217c9e7a6fSAndy Grover 	size_t data_size;
12226418649SSheng Yang 
1237c9e7a6fSAndy Grover 	wait_queue_head_t wait_cmdr;
124b6df4b79SXiubo Li 	struct mutex cmdr_lock;
1257c9e7a6fSAndy Grover 
126b6df4b79SXiubo Li 	bool waiting_global;
127141685a3SXiubo Li 	uint32_t dbi_max;
128b6df4b79SXiubo Li 	uint32_t dbi_thresh;
129141685a3SXiubo Li 	DECLARE_BITMAP(data_bitmap, DATA_BLOCK_BITS);
130141685a3SXiubo Li 	struct radix_tree_root data_blocks;
131141685a3SXiubo Li 
1327c9e7a6fSAndy Grover 	struct idr commands;
1337c9e7a6fSAndy Grover 	spinlock_t commands_lock;
1347c9e7a6fSAndy Grover 
1357c9e7a6fSAndy Grover 	struct timer_list timeout;
136af980e46SMike Christie 	unsigned int cmd_time_out;
1377c9e7a6fSAndy Grover 
1387c9e7a6fSAndy Grover 	char dev_config[TCMU_CONFIG_LEN];
1397c9e7a6fSAndy Grover };
1407c9e7a6fSAndy Grover 
1417c9e7a6fSAndy Grover #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev)
1427c9e7a6fSAndy Grover 
1437c9e7a6fSAndy Grover #define CMDR_OFF sizeof(struct tcmu_mailbox)
1447c9e7a6fSAndy Grover 
1457c9e7a6fSAndy Grover struct tcmu_cmd {
1467c9e7a6fSAndy Grover 	struct se_cmd *se_cmd;
1477c9e7a6fSAndy Grover 	struct tcmu_dev *tcmu_dev;
1487c9e7a6fSAndy Grover 
1497c9e7a6fSAndy Grover 	uint16_t cmd_id;
1507c9e7a6fSAndy Grover 
15126418649SSheng Yang 	/* Can't use se_cmd when cleaning up expired cmds, because if
1527c9e7a6fSAndy Grover 	   cmd has been completed then accessing se_cmd is off limits */
153141685a3SXiubo Li 	uint32_t dbi_cnt;
154141685a3SXiubo Li 	uint32_t dbi_cur;
155141685a3SXiubo Li 	uint32_t *dbi;
1567c9e7a6fSAndy Grover 
1577c9e7a6fSAndy Grover 	unsigned long deadline;
1587c9e7a6fSAndy Grover 
1597c9e7a6fSAndy Grover #define TCMU_CMD_BIT_EXPIRED 0
1607c9e7a6fSAndy Grover 	unsigned long flags;
1617c9e7a6fSAndy Grover };
1627c9e7a6fSAndy Grover 
163b6df4b79SXiubo Li static struct task_struct *unmap_thread;
164b6df4b79SXiubo Li static wait_queue_head_t unmap_wait;
165b6df4b79SXiubo Li static DEFINE_MUTEX(root_udev_mutex);
166b6df4b79SXiubo Li static LIST_HEAD(root_udev);
167b6df4b79SXiubo Li 
168b6df4b79SXiubo Li static atomic_t global_db_count = ATOMIC_INIT(0);
169b6df4b79SXiubo Li 
1707c9e7a6fSAndy Grover static struct kmem_cache *tcmu_cmd_cache;
1717c9e7a6fSAndy Grover 
1727c9e7a6fSAndy Grover /* multicast group */
1737c9e7a6fSAndy Grover enum tcmu_multicast_groups {
1747c9e7a6fSAndy Grover 	TCMU_MCGRP_CONFIG,
1757c9e7a6fSAndy Grover };
1767c9e7a6fSAndy Grover 
1777c9e7a6fSAndy Grover static const struct genl_multicast_group tcmu_mcgrps[] = {
1787c9e7a6fSAndy Grover 	[TCMU_MCGRP_CONFIG] = { .name = "config", },
1797c9e7a6fSAndy Grover };
1807c9e7a6fSAndy Grover 
1817c9e7a6fSAndy Grover /* Our generic netlink family */
18256989f6dSJohannes Berg static struct genl_family tcmu_genl_family __ro_after_init = {
183489111e5SJohannes Berg 	.module = THIS_MODULE,
1847c9e7a6fSAndy Grover 	.hdrsize = 0,
1857c9e7a6fSAndy Grover 	.name = "TCM-USER",
1867c9e7a6fSAndy Grover 	.version = 1,
1877c9e7a6fSAndy Grover 	.maxattr = TCMU_ATTR_MAX,
1887c9e7a6fSAndy Grover 	.mcgrps = tcmu_mcgrps,
1897c9e7a6fSAndy Grover 	.n_mcgrps = ARRAY_SIZE(tcmu_mcgrps),
19020c08b36SSheng Yang 	.netnsok = true,
1917c9e7a6fSAndy Grover };
1927c9e7a6fSAndy Grover 
193141685a3SXiubo Li #define tcmu_cmd_set_dbi_cur(cmd, index) ((cmd)->dbi_cur = (index))
194141685a3SXiubo Li #define tcmu_cmd_reset_dbi_cur(cmd) tcmu_cmd_set_dbi_cur(cmd, 0)
195141685a3SXiubo Li #define tcmu_cmd_set_dbi(cmd, index) ((cmd)->dbi[(cmd)->dbi_cur++] = (index))
196141685a3SXiubo Li #define tcmu_cmd_get_dbi(cmd) ((cmd)->dbi[(cmd)->dbi_cur++])
197141685a3SXiubo Li 
198b6df4b79SXiubo Li static void tcmu_cmd_free_data(struct tcmu_cmd *tcmu_cmd, uint32_t len)
199141685a3SXiubo Li {
200141685a3SXiubo Li 	struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
201141685a3SXiubo Li 	uint32_t i;
202141685a3SXiubo Li 
203b6df4b79SXiubo Li 	for (i = 0; i < len; i++)
204141685a3SXiubo Li 		clear_bit(tcmu_cmd->dbi[i], udev->data_bitmap);
205141685a3SXiubo Li }
206141685a3SXiubo Li 
207b6df4b79SXiubo Li static inline bool tcmu_get_empty_block(struct tcmu_dev *udev,
208b6df4b79SXiubo Li 					struct tcmu_cmd *tcmu_cmd)
209141685a3SXiubo Li {
210b6df4b79SXiubo Li 	struct page *page;
211b6df4b79SXiubo Li 	int ret, dbi;
212141685a3SXiubo Li 
213b6df4b79SXiubo Li 	dbi = find_first_zero_bit(udev->data_bitmap, udev->dbi_thresh);
214b6df4b79SXiubo Li 	if (dbi == udev->dbi_thresh)
215b6df4b79SXiubo Li 		return false;
216b6df4b79SXiubo Li 
217b6df4b79SXiubo Li 	page = radix_tree_lookup(&udev->data_blocks, dbi);
218b6df4b79SXiubo Li 	if (!page) {
219b6df4b79SXiubo Li 
220b6df4b79SXiubo Li 		if (atomic_add_return(1, &global_db_count) >
221b6df4b79SXiubo Li 					TCMU_GLOBAL_MAX_BLOCKS) {
222b6df4b79SXiubo Li 			atomic_dec(&global_db_count);
223b6df4b79SXiubo Li 			return false;
224b6df4b79SXiubo Li 		}
225b6df4b79SXiubo Li 
226b6df4b79SXiubo Li 		/* try to get new page from the mm */
227b6df4b79SXiubo Li 		page = alloc_page(GFP_KERNEL);
228b6df4b79SXiubo Li 		if (!page)
229b6df4b79SXiubo Li 			return false;
230b6df4b79SXiubo Li 
231b6df4b79SXiubo Li 		ret = radix_tree_insert(&udev->data_blocks, dbi, page);
232b6df4b79SXiubo Li 		if (ret) {
233b6df4b79SXiubo Li 			__free_page(page);
234b6df4b79SXiubo Li 			return false;
235b6df4b79SXiubo Li 		}
236b6df4b79SXiubo Li 
237b6df4b79SXiubo Li 	}
238b6df4b79SXiubo Li 
239141685a3SXiubo Li 	if (dbi > udev->dbi_max)
240141685a3SXiubo Li 		udev->dbi_max = dbi;
241141685a3SXiubo Li 
242141685a3SXiubo Li 	set_bit(dbi, udev->data_bitmap);
243b6df4b79SXiubo Li 	tcmu_cmd_set_dbi(tcmu_cmd, dbi);
244141685a3SXiubo Li 
245b6df4b79SXiubo Li 	return true;
246141685a3SXiubo Li }
247141685a3SXiubo Li 
248b6df4b79SXiubo Li static bool tcmu_get_empty_blocks(struct tcmu_dev *udev,
249b6df4b79SXiubo Li 				  struct tcmu_cmd *tcmu_cmd)
250b6df4b79SXiubo Li {
251b6df4b79SXiubo Li 	int i;
252b6df4b79SXiubo Li 
253b6df4b79SXiubo Li 	udev->waiting_global = false;
254b6df4b79SXiubo Li 
255b6df4b79SXiubo Li 	for (i = tcmu_cmd->dbi_cur; i < tcmu_cmd->dbi_cnt; i++) {
256b6df4b79SXiubo Li 		if (!tcmu_get_empty_block(udev, tcmu_cmd))
257b6df4b79SXiubo Li 			goto err;
258141685a3SXiubo Li 	}
259b6df4b79SXiubo Li 	return true;
260b6df4b79SXiubo Li 
261b6df4b79SXiubo Li err:
262b6df4b79SXiubo Li 	udev->waiting_global = true;
263b6df4b79SXiubo Li 	/* Try to wake up the unmap thread */
264b6df4b79SXiubo Li 	wake_up(&unmap_wait);
265b6df4b79SXiubo Li 	return false;
266141685a3SXiubo Li }
267141685a3SXiubo Li 
268b6df4b79SXiubo Li static inline struct page *
269b6df4b79SXiubo Li tcmu_get_block_page(struct tcmu_dev *udev, uint32_t dbi)
270141685a3SXiubo Li {
271141685a3SXiubo Li 	return radix_tree_lookup(&udev->data_blocks, dbi);
272141685a3SXiubo Li }
273141685a3SXiubo Li 
274141685a3SXiubo Li static inline void tcmu_free_cmd(struct tcmu_cmd *tcmu_cmd)
275141685a3SXiubo Li {
276141685a3SXiubo Li 	kfree(tcmu_cmd->dbi);
277141685a3SXiubo Li 	kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
278141685a3SXiubo Li }
279141685a3SXiubo Li 
280141685a3SXiubo Li static inline size_t tcmu_cmd_get_data_length(struct tcmu_cmd *tcmu_cmd)
281141685a3SXiubo Li {
282141685a3SXiubo Li 	struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
283141685a3SXiubo Li 	size_t data_length = round_up(se_cmd->data_length, DATA_BLOCK_SIZE);
284141685a3SXiubo Li 
285141685a3SXiubo Li 	if (se_cmd->se_cmd_flags & SCF_BIDI) {
286141685a3SXiubo Li 		BUG_ON(!(se_cmd->t_bidi_data_sg && se_cmd->t_bidi_data_nents));
287141685a3SXiubo Li 		data_length += round_up(se_cmd->t_bidi_data_sg->length,
288141685a3SXiubo Li 				DATA_BLOCK_SIZE);
289141685a3SXiubo Li 	}
290141685a3SXiubo Li 
291141685a3SXiubo Li 	return data_length;
292141685a3SXiubo Li }
293141685a3SXiubo Li 
294141685a3SXiubo Li static inline uint32_t tcmu_cmd_get_block_cnt(struct tcmu_cmd *tcmu_cmd)
295141685a3SXiubo Li {
296141685a3SXiubo Li 	size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd);
297141685a3SXiubo Li 
298141685a3SXiubo Li 	return data_length / DATA_BLOCK_SIZE;
299141685a3SXiubo Li }
300141685a3SXiubo Li 
3017c9e7a6fSAndy Grover static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd)
3027c9e7a6fSAndy Grover {
3037c9e7a6fSAndy Grover 	struct se_device *se_dev = se_cmd->se_dev;
3047c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
3057c9e7a6fSAndy Grover 	struct tcmu_cmd *tcmu_cmd;
3067c9e7a6fSAndy Grover 	int cmd_id;
3077c9e7a6fSAndy Grover 
3087c9e7a6fSAndy Grover 	tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_KERNEL);
3097c9e7a6fSAndy Grover 	if (!tcmu_cmd)
3107c9e7a6fSAndy Grover 		return NULL;
3117c9e7a6fSAndy Grover 
3127c9e7a6fSAndy Grover 	tcmu_cmd->se_cmd = se_cmd;
3137c9e7a6fSAndy Grover 	tcmu_cmd->tcmu_dev = udev;
314af980e46SMike Christie 	if (udev->cmd_time_out)
315af980e46SMike Christie 		tcmu_cmd->deadline = jiffies +
316af980e46SMike Christie 					msecs_to_jiffies(udev->cmd_time_out);
3177c9e7a6fSAndy Grover 
318141685a3SXiubo Li 	tcmu_cmd_reset_dbi_cur(tcmu_cmd);
319141685a3SXiubo Li 	tcmu_cmd->dbi_cnt = tcmu_cmd_get_block_cnt(tcmu_cmd);
320141685a3SXiubo Li 	tcmu_cmd->dbi = kcalloc(tcmu_cmd->dbi_cnt, sizeof(uint32_t),
321141685a3SXiubo Li 				GFP_KERNEL);
322141685a3SXiubo Li 	if (!tcmu_cmd->dbi) {
323141685a3SXiubo Li 		kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
324141685a3SXiubo Li 		return NULL;
325141685a3SXiubo Li 	}
326141685a3SXiubo Li 
3277c9e7a6fSAndy Grover 	idr_preload(GFP_KERNEL);
3287c9e7a6fSAndy Grover 	spin_lock_irq(&udev->commands_lock);
3297c9e7a6fSAndy Grover 	cmd_id = idr_alloc(&udev->commands, tcmu_cmd, 0,
3307c9e7a6fSAndy Grover 		USHRT_MAX, GFP_NOWAIT);
3317c9e7a6fSAndy Grover 	spin_unlock_irq(&udev->commands_lock);
3327c9e7a6fSAndy Grover 	idr_preload_end();
3337c9e7a6fSAndy Grover 
3347c9e7a6fSAndy Grover 	if (cmd_id < 0) {
335141685a3SXiubo Li 		tcmu_free_cmd(tcmu_cmd);
3367c9e7a6fSAndy Grover 		return NULL;
3377c9e7a6fSAndy Grover 	}
3387c9e7a6fSAndy Grover 	tcmu_cmd->cmd_id = cmd_id;
3397c9e7a6fSAndy Grover 
3407c9e7a6fSAndy Grover 	return tcmu_cmd;
3417c9e7a6fSAndy Grover }
3427c9e7a6fSAndy Grover 
3437c9e7a6fSAndy Grover static inline void tcmu_flush_dcache_range(void *vaddr, size_t size)
3447c9e7a6fSAndy Grover {
345b75d8063SGeliang Tang 	unsigned long offset = offset_in_page(vaddr);
3467c9e7a6fSAndy Grover 
3477c9e7a6fSAndy Grover 	size = round_up(size+offset, PAGE_SIZE);
3487c9e7a6fSAndy Grover 	vaddr -= offset;
3497c9e7a6fSAndy Grover 
3507c9e7a6fSAndy Grover 	while (size) {
3517c9e7a6fSAndy Grover 		flush_dcache_page(virt_to_page(vaddr));
3527c9e7a6fSAndy Grover 		size -= PAGE_SIZE;
3537c9e7a6fSAndy Grover 	}
3547c9e7a6fSAndy Grover }
3557c9e7a6fSAndy Grover 
3567c9e7a6fSAndy Grover /*
3577c9e7a6fSAndy Grover  * Some ring helper functions. We don't assume size is a power of 2 so
3587c9e7a6fSAndy Grover  * we can't use circ_buf.h.
3597c9e7a6fSAndy Grover  */
3607c9e7a6fSAndy Grover static inline size_t spc_used(size_t head, size_t tail, size_t size)
3617c9e7a6fSAndy Grover {
3627c9e7a6fSAndy Grover 	int diff = head - tail;
3637c9e7a6fSAndy Grover 
3647c9e7a6fSAndy Grover 	if (diff >= 0)
3657c9e7a6fSAndy Grover 		return diff;
3667c9e7a6fSAndy Grover 	else
3677c9e7a6fSAndy Grover 		return size + diff;
3687c9e7a6fSAndy Grover }
3697c9e7a6fSAndy Grover 
3707c9e7a6fSAndy Grover static inline size_t spc_free(size_t head, size_t tail, size_t size)
3717c9e7a6fSAndy Grover {
3727c9e7a6fSAndy Grover 	/* Keep 1 byte unused or we can't tell full from empty */
3737c9e7a6fSAndy Grover 	return (size - spc_used(head, tail, size) - 1);
3747c9e7a6fSAndy Grover }
3757c9e7a6fSAndy Grover 
3767c9e7a6fSAndy Grover static inline size_t head_to_end(size_t head, size_t size)
3777c9e7a6fSAndy Grover {
3787c9e7a6fSAndy Grover 	return size - head;
3797c9e7a6fSAndy Grover }
3807c9e7a6fSAndy Grover 
381f1dbd087SSheng Yang static inline void new_iov(struct iovec **iov, int *iov_cnt,
382f1dbd087SSheng Yang 			   struct tcmu_dev *udev)
383f1dbd087SSheng Yang {
384f1dbd087SSheng Yang 	struct iovec *iovec;
385f1dbd087SSheng Yang 
386f1dbd087SSheng Yang 	if (*iov_cnt != 0)
387f1dbd087SSheng Yang 		(*iov)++;
388f1dbd087SSheng Yang 	(*iov_cnt)++;
389f1dbd087SSheng Yang 
390f1dbd087SSheng Yang 	iovec = *iov;
391f1dbd087SSheng Yang 	memset(iovec, 0, sizeof(struct iovec));
392f1dbd087SSheng Yang }
393f1dbd087SSheng Yang 
3947c9e7a6fSAndy Grover #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size)
3957c9e7a6fSAndy Grover 
39626418649SSheng Yang /* offset is relative to mb_addr */
397141685a3SXiubo Li static inline size_t get_block_offset_user(struct tcmu_dev *dev,
398141685a3SXiubo Li 		int dbi, int remaining)
39926418649SSheng Yang {
400141685a3SXiubo Li 	return dev->data_off + dbi * DATA_BLOCK_SIZE +
40126418649SSheng Yang 		DATA_BLOCK_SIZE - remaining;
40226418649SSheng Yang }
40326418649SSheng Yang 
40426418649SSheng Yang static inline size_t iov_tail(struct tcmu_dev *udev, struct iovec *iov)
40526418649SSheng Yang {
40626418649SSheng Yang 	return (size_t)iov->iov_base + iov->iov_len;
40726418649SSheng Yang }
40826418649SSheng Yang 
409b6df4b79SXiubo Li static int scatter_data_area(struct tcmu_dev *udev,
410141685a3SXiubo Li 	struct tcmu_cmd *tcmu_cmd, struct scatterlist *data_sg,
411141685a3SXiubo Li 	unsigned int data_nents, struct iovec **iov,
412141685a3SXiubo Li 	int *iov_cnt, bool copy_data)
413f97ec7dbSIlias Tsitsimpis {
414141685a3SXiubo Li 	int i, dbi;
41526418649SSheng Yang 	int block_remaining = 0;
416141685a3SXiubo Li 	void *from, *to = NULL;
417141685a3SXiubo Li 	size_t copy_bytes, to_offset, offset;
418f97ec7dbSIlias Tsitsimpis 	struct scatterlist *sg;
419b6df4b79SXiubo Li 	struct page *page;
420f97ec7dbSIlias Tsitsimpis 
421f97ec7dbSIlias Tsitsimpis 	for_each_sg(data_sg, sg, data_nents, i) {
42226418649SSheng Yang 		int sg_remaining = sg->length;
423f97ec7dbSIlias Tsitsimpis 		from = kmap_atomic(sg_page(sg)) + sg->offset;
42426418649SSheng Yang 		while (sg_remaining > 0) {
42526418649SSheng Yang 			if (block_remaining == 0) {
426b6df4b79SXiubo Li 				if (to)
427b6df4b79SXiubo Li 					kunmap_atomic(to);
428b6df4b79SXiubo Li 
42926418649SSheng Yang 				block_remaining = DATA_BLOCK_SIZE;
430b6df4b79SXiubo Li 				dbi = tcmu_cmd_get_dbi(tcmu_cmd);
431b6df4b79SXiubo Li 				page = tcmu_get_block_page(udev, dbi);
432b6df4b79SXiubo Li 				to = kmap_atomic(page);
433141685a3SXiubo Li 			}
434141685a3SXiubo Li 
43526418649SSheng Yang 			copy_bytes = min_t(size_t, sg_remaining,
43626418649SSheng Yang 					block_remaining);
437141685a3SXiubo Li 			to_offset = get_block_offset_user(udev, dbi,
43826418649SSheng Yang 					block_remaining);
439141685a3SXiubo Li 			offset = DATA_BLOCK_SIZE - block_remaining;
440141685a3SXiubo Li 			to = (void *)(unsigned long)to + offset;
441141685a3SXiubo Li 
44226418649SSheng Yang 			if (*iov_cnt != 0 &&
44326418649SSheng Yang 			    to_offset == iov_tail(udev, *iov)) {
444f1dbd087SSheng Yang 				(*iov)->iov_len += copy_bytes;
44526418649SSheng Yang 			} else {
446f1dbd087SSheng Yang 				new_iov(iov, iov_cnt, udev);
44726418649SSheng Yang 				(*iov)->iov_base = (void __user *)to_offset;
448f97ec7dbSIlias Tsitsimpis 				(*iov)->iov_len = copy_bytes;
44926418649SSheng Yang 			}
450f97ec7dbSIlias Tsitsimpis 			if (copy_data) {
45126418649SSheng Yang 				memcpy(to, from + sg->length - sg_remaining,
45226418649SSheng Yang 					copy_bytes);
453f97ec7dbSIlias Tsitsimpis 				tcmu_flush_dcache_range(to, copy_bytes);
454f97ec7dbSIlias Tsitsimpis 			}
45526418649SSheng Yang 			sg_remaining -= copy_bytes;
45626418649SSheng Yang 			block_remaining -= copy_bytes;
457f97ec7dbSIlias Tsitsimpis 		}
458e2e21bd8SSagi Grimberg 		kunmap_atomic(from - sg->offset);
459f97ec7dbSIlias Tsitsimpis 	}
460b6df4b79SXiubo Li 	if (to)
461b6df4b79SXiubo Li 		kunmap_atomic(to);
462f97ec7dbSIlias Tsitsimpis 
463141685a3SXiubo Li 	return 0;
4640c28481fSSheng Yang }
4650c28481fSSheng Yang 
466a5d68ba8SXiubo Li static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
467a5d68ba8SXiubo Li 			     bool bidi)
468f97ec7dbSIlias Tsitsimpis {
469a5d68ba8SXiubo Li 	struct se_cmd *se_cmd = cmd->se_cmd;
470141685a3SXiubo Li 	int i, dbi;
47126418649SSheng Yang 	int block_remaining = 0;
472b6df4b79SXiubo Li 	void *from = NULL, *to;
473141685a3SXiubo Li 	size_t copy_bytes, offset;
474a5d68ba8SXiubo Li 	struct scatterlist *sg, *data_sg;
475b6df4b79SXiubo Li 	struct page *page;
476a5d68ba8SXiubo Li 	unsigned int data_nents;
477141685a3SXiubo Li 	uint32_t count = 0;
478a5d68ba8SXiubo Li 
479a5d68ba8SXiubo Li 	if (!bidi) {
480a5d68ba8SXiubo Li 		data_sg = se_cmd->t_data_sg;
481a5d68ba8SXiubo Li 		data_nents = se_cmd->t_data_nents;
482a5d68ba8SXiubo Li 	} else {
483a5d68ba8SXiubo Li 
484a5d68ba8SXiubo Li 		/*
485a5d68ba8SXiubo Li 		 * For bidi case, the first count blocks are for Data-Out
486a5d68ba8SXiubo Li 		 * buffer blocks, and before gathering the Data-In buffer
487a5d68ba8SXiubo Li 		 * the Data-Out buffer blocks should be discarded.
488a5d68ba8SXiubo Li 		 */
489a5d68ba8SXiubo Li 		count = DIV_ROUND_UP(se_cmd->data_length, DATA_BLOCK_SIZE);
490a5d68ba8SXiubo Li 
491a5d68ba8SXiubo Li 		data_sg = se_cmd->t_bidi_data_sg;
492a5d68ba8SXiubo Li 		data_nents = se_cmd->t_bidi_data_nents;
493a5d68ba8SXiubo Li 	}
494f97ec7dbSIlias Tsitsimpis 
495141685a3SXiubo Li 	tcmu_cmd_set_dbi_cur(cmd, count);
496141685a3SXiubo Li 
497f97ec7dbSIlias Tsitsimpis 	for_each_sg(data_sg, sg, data_nents, i) {
49826418649SSheng Yang 		int sg_remaining = sg->length;
499f97ec7dbSIlias Tsitsimpis 		to = kmap_atomic(sg_page(sg)) + sg->offset;
50026418649SSheng Yang 		while (sg_remaining > 0) {
50126418649SSheng Yang 			if (block_remaining == 0) {
502b6df4b79SXiubo Li 				if (from)
503b6df4b79SXiubo Li 					kunmap_atomic(from);
504b6df4b79SXiubo Li 
50526418649SSheng Yang 				block_remaining = DATA_BLOCK_SIZE;
506141685a3SXiubo Li 				dbi = tcmu_cmd_get_dbi(cmd);
507b6df4b79SXiubo Li 				page = tcmu_get_block_page(udev, dbi);
508b6df4b79SXiubo Li 				from = kmap_atomic(page);
50926418649SSheng Yang 			}
51026418649SSheng Yang 			copy_bytes = min_t(size_t, sg_remaining,
51126418649SSheng Yang 					block_remaining);
512141685a3SXiubo Li 			offset = DATA_BLOCK_SIZE - block_remaining;
513141685a3SXiubo Li 			from = (void *)(unsigned long)from + offset;
514f97ec7dbSIlias Tsitsimpis 			tcmu_flush_dcache_range(from, copy_bytes);
51526418649SSheng Yang 			memcpy(to + sg->length - sg_remaining, from,
51626418649SSheng Yang 					copy_bytes);
517f97ec7dbSIlias Tsitsimpis 
51826418649SSheng Yang 			sg_remaining -= copy_bytes;
51926418649SSheng Yang 			block_remaining -= copy_bytes;
520f97ec7dbSIlias Tsitsimpis 		}
521e2e21bd8SSagi Grimberg 		kunmap_atomic(to - sg->offset);
522f97ec7dbSIlias Tsitsimpis 	}
523b6df4b79SXiubo Li 	if (from)
524b6df4b79SXiubo Li 		kunmap_atomic(from);
525f97ec7dbSIlias Tsitsimpis }
526f97ec7dbSIlias Tsitsimpis 
527b6df4b79SXiubo Li static inline size_t spc_bitmap_free(unsigned long *bitmap, uint32_t thresh)
52826418649SSheng Yang {
529b6df4b79SXiubo Li 	return DATA_BLOCK_SIZE * (thresh - bitmap_weight(bitmap, thresh));
53026418649SSheng Yang }
53126418649SSheng Yang 
5327c9e7a6fSAndy Grover /*
533f97ec7dbSIlias Tsitsimpis  * We can't queue a command until we have space available on the cmd ring *and*
5343d9b9555SAndy Grover  * space available on the data area.
5357c9e7a6fSAndy Grover  *
5367c9e7a6fSAndy Grover  * Called with ring lock held.
5377c9e7a6fSAndy Grover  */
538b6df4b79SXiubo Li static bool is_ring_space_avail(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
539b6df4b79SXiubo Li 		size_t cmd_size, size_t data_needed)
5407c9e7a6fSAndy Grover {
5417c9e7a6fSAndy Grover 	struct tcmu_mailbox *mb = udev->mb_addr;
542b6df4b79SXiubo Li 	uint32_t blocks_needed = (data_needed + DATA_BLOCK_SIZE - 1)
543b6df4b79SXiubo Li 				/ DATA_BLOCK_SIZE;
5440241fd39SNicholas Bellinger 	size_t space, cmd_needed;
5457c9e7a6fSAndy Grover 	u32 cmd_head;
5467c9e7a6fSAndy Grover 
5477c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(mb, sizeof(*mb));
5487c9e7a6fSAndy Grover 
5497c9e7a6fSAndy Grover 	cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
5507c9e7a6fSAndy Grover 
551f56574a2SAndy Grover 	/*
552f56574a2SAndy Grover 	 * If cmd end-of-ring space is too small then we need space for a NOP plus
553f56574a2SAndy Grover 	 * original cmd - cmds are internally contiguous.
554f56574a2SAndy Grover 	 */
555f56574a2SAndy Grover 	if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size)
556f56574a2SAndy Grover 		cmd_needed = cmd_size;
557f56574a2SAndy Grover 	else
558f56574a2SAndy Grover 		cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size);
559f56574a2SAndy Grover 
5607c9e7a6fSAndy Grover 	space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size);
5617c9e7a6fSAndy Grover 	if (space < cmd_needed) {
5627c9e7a6fSAndy Grover 		pr_debug("no cmd space: %u %u %u\n", cmd_head,
5637c9e7a6fSAndy Grover 		       udev->cmdr_last_cleaned, udev->cmdr_size);
5647c9e7a6fSAndy Grover 		return false;
5657c9e7a6fSAndy Grover 	}
5667c9e7a6fSAndy Grover 
567b6df4b79SXiubo Li 	/* try to check and get the data blocks as needed */
568b6df4b79SXiubo Li 	space = spc_bitmap_free(udev->data_bitmap, udev->dbi_thresh);
5697c9e7a6fSAndy Grover 	if (space < data_needed) {
570b6df4b79SXiubo Li 		unsigned long blocks_left = DATA_BLOCK_BITS - udev->dbi_thresh;
571b6df4b79SXiubo Li 		unsigned long grow;
572b6df4b79SXiubo Li 
573b6df4b79SXiubo Li 		if (blocks_left < blocks_needed) {
574b6df4b79SXiubo Li 			pr_debug("no data space: only %lu available, but ask for %zu\n",
575b6df4b79SXiubo Li 					blocks_left * DATA_BLOCK_SIZE,
576b6df4b79SXiubo Li 					data_needed);
5777c9e7a6fSAndy Grover 			return false;
5787c9e7a6fSAndy Grover 		}
5797c9e7a6fSAndy Grover 
580b6df4b79SXiubo Li 		/* Try to expand the thresh */
581b6df4b79SXiubo Li 		if (!udev->dbi_thresh) {
582b6df4b79SXiubo Li 			/* From idle state */
583b6df4b79SXiubo Li 			uint32_t init_thresh = DATA_BLOCK_INIT_BITS;
584b6df4b79SXiubo Li 
585b6df4b79SXiubo Li 			udev->dbi_thresh = max(blocks_needed, init_thresh);
586b6df4b79SXiubo Li 		} else {
587b6df4b79SXiubo Li 			/*
588b6df4b79SXiubo Li 			 * Grow the data area by max(blocks needed,
589b6df4b79SXiubo Li 			 * dbi_thresh / 2), but limited to the max
590b6df4b79SXiubo Li 			 * DATA_BLOCK_BITS size.
591b6df4b79SXiubo Li 			 */
592b6df4b79SXiubo Li 			grow = max(blocks_needed, udev->dbi_thresh / 2);
593b6df4b79SXiubo Li 			udev->dbi_thresh += grow;
594b6df4b79SXiubo Li 			if (udev->dbi_thresh > DATA_BLOCK_BITS)
595b6df4b79SXiubo Li 				udev->dbi_thresh = DATA_BLOCK_BITS;
596b6df4b79SXiubo Li 		}
597b6df4b79SXiubo Li 	}
598b6df4b79SXiubo Li 
599b6df4b79SXiubo Li 	if (!tcmu_get_empty_blocks(udev, cmd))
600b6df4b79SXiubo Li 		return false;
601b6df4b79SXiubo Li 
6027c9e7a6fSAndy Grover 	return true;
6037c9e7a6fSAndy Grover }
6047c9e7a6fSAndy Grover 
605fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_base_cmd_size(size_t iov_cnt)
606fe25cc34SXiubo Li {
607fe25cc34SXiubo Li 	return max(offsetof(struct tcmu_cmd_entry, req.iov[iov_cnt]),
608fe25cc34SXiubo Li 			sizeof(struct tcmu_cmd_entry));
609fe25cc34SXiubo Li }
610fe25cc34SXiubo Li 
611fe25cc34SXiubo Li static inline size_t tcmu_cmd_get_cmd_size(struct tcmu_cmd *tcmu_cmd,
612fe25cc34SXiubo Li 					   size_t base_command_size)
613fe25cc34SXiubo Li {
614fe25cc34SXiubo Li 	struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
615fe25cc34SXiubo Li 	size_t command_size;
616fe25cc34SXiubo Li 
617fe25cc34SXiubo Li 	command_size = base_command_size +
618fe25cc34SXiubo Li 		round_up(scsi_command_size(se_cmd->t_task_cdb),
619fe25cc34SXiubo Li 				TCMU_OP_ALIGN_SIZE);
620fe25cc34SXiubo Li 
621fe25cc34SXiubo Li 	WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1));
622fe25cc34SXiubo Li 
623fe25cc34SXiubo Li 	return command_size;
624fe25cc34SXiubo Li }
625fe25cc34SXiubo Li 
62602eb924fSAndy Grover static sense_reason_t
62702eb924fSAndy Grover tcmu_queue_cmd_ring(struct tcmu_cmd *tcmu_cmd)
6287c9e7a6fSAndy Grover {
6297c9e7a6fSAndy Grover 	struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
6307c9e7a6fSAndy Grover 	struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
6317c9e7a6fSAndy Grover 	size_t base_command_size, command_size;
6327c9e7a6fSAndy Grover 	struct tcmu_mailbox *mb;
6337c9e7a6fSAndy Grover 	struct tcmu_cmd_entry *entry;
6347c9e7a6fSAndy Grover 	struct iovec *iov;
635141685a3SXiubo Li 	int iov_cnt, ret;
6367c9e7a6fSAndy Grover 	uint32_t cmd_head;
6377c9e7a6fSAndy Grover 	uint64_t cdb_off;
638f97ec7dbSIlias Tsitsimpis 	bool copy_to_data_area;
639ab22d260SXiubo Li 	size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd);
6407c9e7a6fSAndy Grover 
6417c9e7a6fSAndy Grover 	if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags))
64202eb924fSAndy Grover 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
6437c9e7a6fSAndy Grover 
6447c9e7a6fSAndy Grover 	/*
6457c9e7a6fSAndy Grover 	 * Must be a certain minimum size for response sense info, but
6467c9e7a6fSAndy Grover 	 * also may be larger if the iov array is large.
6477c9e7a6fSAndy Grover 	 *
648fe25cc34SXiubo Li 	 * We prepare as many iovs as possbile for potential uses here,
649fe25cc34SXiubo Li 	 * because it's expensive to tell how many regions are freed in
650fe25cc34SXiubo Li 	 * the bitmap & global data pool, as the size calculated here
651fe25cc34SXiubo Li 	 * will only be used to do the checks.
652fe25cc34SXiubo Li 	 *
653fe25cc34SXiubo Li 	 * The size will be recalculated later as actually needed to save
654fe25cc34SXiubo Li 	 * cmd area memories.
6557c9e7a6fSAndy Grover 	 */
656fe25cc34SXiubo Li 	base_command_size = tcmu_cmd_get_base_cmd_size(tcmu_cmd->dbi_cnt);
657fe25cc34SXiubo Li 	command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size);
6587c9e7a6fSAndy Grover 
659b6df4b79SXiubo Li 	mutex_lock(&udev->cmdr_lock);
6607c9e7a6fSAndy Grover 
6617c9e7a6fSAndy Grover 	mb = udev->mb_addr;
6627c9e7a6fSAndy Grover 	cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
663554617b2SAndy Grover 	if ((command_size > (udev->cmdr_size / 2)) ||
664554617b2SAndy Grover 	    data_length > udev->data_size) {
665554617b2SAndy Grover 		pr_warn("TCMU: Request of size %zu/%zu is too big for %u/%zu "
6663d9b9555SAndy Grover 			"cmd ring/data area\n", command_size, data_length,
6677c9e7a6fSAndy Grover 			udev->cmdr_size, udev->data_size);
668b6df4b79SXiubo Li 		mutex_unlock(&udev->cmdr_lock);
669554617b2SAndy Grover 		return TCM_INVALID_CDB_FIELD;
670554617b2SAndy Grover 	}
6717c9e7a6fSAndy Grover 
672b6df4b79SXiubo Li 	while (!is_ring_space_avail(udev, tcmu_cmd, command_size, data_length)) {
6737c9e7a6fSAndy Grover 		int ret;
6747c9e7a6fSAndy Grover 		DEFINE_WAIT(__wait);
6757c9e7a6fSAndy Grover 
6767c9e7a6fSAndy Grover 		prepare_to_wait(&udev->wait_cmdr, &__wait, TASK_INTERRUPTIBLE);
6777c9e7a6fSAndy Grover 
6787c9e7a6fSAndy Grover 		pr_debug("sleeping for ring space\n");
679b6df4b79SXiubo Li 		mutex_unlock(&udev->cmdr_lock);
680af980e46SMike Christie 		if (udev->cmd_time_out)
681af980e46SMike Christie 			ret = schedule_timeout(
682af980e46SMike Christie 					msecs_to_jiffies(udev->cmd_time_out));
683af980e46SMike Christie 		else
6847c9e7a6fSAndy Grover 			ret = schedule_timeout(msecs_to_jiffies(TCMU_TIME_OUT));
6857c9e7a6fSAndy Grover 		finish_wait(&udev->wait_cmdr, &__wait);
6867c9e7a6fSAndy Grover 		if (!ret) {
6877c9e7a6fSAndy Grover 			pr_warn("tcmu: command timed out\n");
68802eb924fSAndy Grover 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
6897c9e7a6fSAndy Grover 		}
6907c9e7a6fSAndy Grover 
691b6df4b79SXiubo Li 		mutex_lock(&udev->cmdr_lock);
6927c9e7a6fSAndy Grover 
6937c9e7a6fSAndy Grover 		/* We dropped cmdr_lock, cmd_head is stale */
6947c9e7a6fSAndy Grover 		cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
6957c9e7a6fSAndy Grover 	}
6967c9e7a6fSAndy Grover 
697f56574a2SAndy Grover 	/* Insert a PAD if end-of-ring space is too small */
698f56574a2SAndy Grover 	if (head_to_end(cmd_head, udev->cmdr_size) < command_size) {
699f56574a2SAndy Grover 		size_t pad_size = head_to_end(cmd_head, udev->cmdr_size);
700f56574a2SAndy Grover 
7017c9e7a6fSAndy Grover 		entry = (void *) mb + CMDR_OFF + cmd_head;
7027c9e7a6fSAndy Grover 		tcmu_flush_dcache_range(entry, sizeof(*entry));
7030ad46af8SAndy Grover 		tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_PAD);
7040ad46af8SAndy Grover 		tcmu_hdr_set_len(&entry->hdr.len_op, pad_size);
7050ad46af8SAndy Grover 		entry->hdr.cmd_id = 0; /* not used for PAD */
7060ad46af8SAndy Grover 		entry->hdr.kflags = 0;
7070ad46af8SAndy Grover 		entry->hdr.uflags = 0;
7087c9e7a6fSAndy Grover 
7097c9e7a6fSAndy Grover 		UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size);
7107c9e7a6fSAndy Grover 
7117c9e7a6fSAndy Grover 		cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
7127c9e7a6fSAndy Grover 		WARN_ON(cmd_head != 0);
7137c9e7a6fSAndy Grover 	}
7147c9e7a6fSAndy Grover 
7157c9e7a6fSAndy Grover 	entry = (void *) mb + CMDR_OFF + cmd_head;
7167c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(entry, sizeof(*entry));
7170ad46af8SAndy Grover 	tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD);
7180ad46af8SAndy Grover 	entry->hdr.cmd_id = tcmu_cmd->cmd_id;
7190ad46af8SAndy Grover 	entry->hdr.kflags = 0;
7200ad46af8SAndy Grover 	entry->hdr.uflags = 0;
7217c9e7a6fSAndy Grover 
7223d9b9555SAndy Grover 	/* Handle allocating space from the data area */
723b6df4b79SXiubo Li 	tcmu_cmd_reset_dbi_cur(tcmu_cmd);
7247c9e7a6fSAndy Grover 	iov = &entry->req.iov[0];
725f97ec7dbSIlias Tsitsimpis 	iov_cnt = 0;
726e4648b01SIlias Tsitsimpis 	copy_to_data_area = (se_cmd->data_direction == DMA_TO_DEVICE
727e4648b01SIlias Tsitsimpis 		|| se_cmd->se_cmd_flags & SCF_BIDI);
728b6df4b79SXiubo Li 	ret = scatter_data_area(udev, tcmu_cmd, se_cmd->t_data_sg,
729b6df4b79SXiubo Li 				se_cmd->t_data_nents, &iov, &iov_cnt,
730b6df4b79SXiubo Li 				copy_to_data_area);
731141685a3SXiubo Li 	if (ret) {
732b6df4b79SXiubo Li 		tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt);
733b6df4b79SXiubo Li 		mutex_unlock(&udev->cmdr_lock);
734b6df4b79SXiubo Li 
735141685a3SXiubo Li 		pr_err("tcmu: alloc and scatter data failed\n");
736141685a3SXiubo Li 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
737141685a3SXiubo Li 	}
7387c9e7a6fSAndy Grover 	entry->req.iov_cnt = iov_cnt;
7390ad46af8SAndy Grover 	entry->req.iov_dif_cnt = 0;
7407c9e7a6fSAndy Grover 
741e4648b01SIlias Tsitsimpis 	/* Handle BIDI commands */
742ab22d260SXiubo Li 	if (se_cmd->se_cmd_flags & SCF_BIDI) {
743e4648b01SIlias Tsitsimpis 		iov_cnt = 0;
744ab22d260SXiubo Li 		iov++;
745b6df4b79SXiubo Li 		ret = scatter_data_area(udev, tcmu_cmd,
746141685a3SXiubo Li 					se_cmd->t_bidi_data_sg,
747141685a3SXiubo Li 					se_cmd->t_bidi_data_nents,
748141685a3SXiubo Li 					&iov, &iov_cnt, false);
749141685a3SXiubo Li 		if (ret) {
750b6df4b79SXiubo Li 			tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt);
751b6df4b79SXiubo Li 			mutex_unlock(&udev->cmdr_lock);
752b6df4b79SXiubo Li 
753141685a3SXiubo Li 			pr_err("tcmu: alloc and scatter bidi data failed\n");
754141685a3SXiubo Li 			return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
755141685a3SXiubo Li 		}
756e4648b01SIlias Tsitsimpis 		entry->req.iov_bidi_cnt = iov_cnt;
757ab22d260SXiubo Li 	}
75826418649SSheng Yang 
759fe25cc34SXiubo Li 	/*
760fe25cc34SXiubo Li 	 * Recalaulate the command's base size and size according
761fe25cc34SXiubo Li 	 * to the actual needs
762fe25cc34SXiubo Li 	 */
763fe25cc34SXiubo Li 	base_command_size = tcmu_cmd_get_base_cmd_size(entry->req.iov_cnt +
764fe25cc34SXiubo Li 						       entry->req.iov_bidi_cnt);
765fe25cc34SXiubo Li 	command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size);
766fe25cc34SXiubo Li 
767fe25cc34SXiubo Li 	tcmu_hdr_set_len(&entry->hdr.len_op, command_size);
768fe25cc34SXiubo Li 
7697c9e7a6fSAndy Grover 	/* All offsets relative to mb_addr, not start of entry! */
7707c9e7a6fSAndy Grover 	cdb_off = CMDR_OFF + cmd_head + base_command_size;
7717c9e7a6fSAndy Grover 	memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb));
7727c9e7a6fSAndy Grover 	entry->req.cdb_off = cdb_off;
7737c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(entry, sizeof(*entry));
7747c9e7a6fSAndy Grover 
7757c9e7a6fSAndy Grover 	UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size);
7767c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(mb, sizeof(*mb));
777b6df4b79SXiubo Li 	mutex_unlock(&udev->cmdr_lock);
7787c9e7a6fSAndy Grover 
7797c9e7a6fSAndy Grover 	/* TODO: only if FLUSH and FUA? */
7807c9e7a6fSAndy Grover 	uio_event_notify(&udev->uio_info);
7817c9e7a6fSAndy Grover 
782af980e46SMike Christie 	if (udev->cmd_time_out)
783af980e46SMike Christie 		mod_timer(&udev->timeout, round_jiffies_up(jiffies +
784af980e46SMike Christie 			  msecs_to_jiffies(udev->cmd_time_out)));
7857c9e7a6fSAndy Grover 
78602eb924fSAndy Grover 	return TCM_NO_SENSE;
7877c9e7a6fSAndy Grover }
7887c9e7a6fSAndy Grover 
78902eb924fSAndy Grover static sense_reason_t
79002eb924fSAndy Grover tcmu_queue_cmd(struct se_cmd *se_cmd)
7917c9e7a6fSAndy Grover {
7927c9e7a6fSAndy Grover 	struct se_device *se_dev = se_cmd->se_dev;
7937c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
7947c9e7a6fSAndy Grover 	struct tcmu_cmd *tcmu_cmd;
795ecaf597bSBart Van Assche 	sense_reason_t ret;
7967c9e7a6fSAndy Grover 
7977c9e7a6fSAndy Grover 	tcmu_cmd = tcmu_alloc_cmd(se_cmd);
7987c9e7a6fSAndy Grover 	if (!tcmu_cmd)
79902eb924fSAndy Grover 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
8007c9e7a6fSAndy Grover 
8017c9e7a6fSAndy Grover 	ret = tcmu_queue_cmd_ring(tcmu_cmd);
80202eb924fSAndy Grover 	if (ret != TCM_NO_SENSE) {
8037c9e7a6fSAndy Grover 		pr_err("TCMU: Could not queue command\n");
8047c9e7a6fSAndy Grover 		spin_lock_irq(&udev->commands_lock);
8057c9e7a6fSAndy Grover 		idr_remove(&udev->commands, tcmu_cmd->cmd_id);
8067c9e7a6fSAndy Grover 		spin_unlock_irq(&udev->commands_lock);
8077c9e7a6fSAndy Grover 
808141685a3SXiubo Li 		tcmu_free_cmd(tcmu_cmd);
8097c9e7a6fSAndy Grover 	}
8107c9e7a6fSAndy Grover 
8117c9e7a6fSAndy Grover 	return ret;
8127c9e7a6fSAndy Grover }
8137c9e7a6fSAndy Grover 
8147c9e7a6fSAndy Grover static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *entry)
8157c9e7a6fSAndy Grover {
8167c9e7a6fSAndy Grover 	struct se_cmd *se_cmd = cmd->se_cmd;
8177c9e7a6fSAndy Grover 	struct tcmu_dev *udev = cmd->tcmu_dev;
8187c9e7a6fSAndy Grover 
819b25c7863SSheng Yang 	/*
820b25c7863SSheng Yang 	 * cmd has been completed already from timeout, just reclaim
8213d9b9555SAndy Grover 	 * data area space and free cmd
822b25c7863SSheng Yang 	 */
823141685a3SXiubo Li 	if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
824141685a3SXiubo Li 		goto out;
825b25c7863SSheng Yang 
826141685a3SXiubo Li 	tcmu_cmd_reset_dbi_cur(cmd);
8277c9e7a6fSAndy Grover 
8280ad46af8SAndy Grover 	if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) {
8290ad46af8SAndy Grover 		pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n",
8300ad46af8SAndy Grover 			cmd->se_cmd);
831ed97d0cdSAndy Grover 		entry->rsp.scsi_status = SAM_STAT_CHECK_CONDITION;
832ed97d0cdSAndy Grover 	} else if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) {
8337c9e7a6fSAndy Grover 		memcpy(se_cmd->sense_buffer, entry->rsp.sense_buffer,
8347c9e7a6fSAndy Grover 			       se_cmd->scsi_sense_length);
835e4648b01SIlias Tsitsimpis 	} else if (se_cmd->se_cmd_flags & SCF_BIDI) {
83626418649SSheng Yang 		/* Get Data-In buffer before clean up */
837a5d68ba8SXiubo Li 		gather_data_area(udev, cmd, true);
838e4648b01SIlias Tsitsimpis 	} else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
839a5d68ba8SXiubo Li 		gather_data_area(udev, cmd, false);
8407c9e7a6fSAndy Grover 	} else if (se_cmd->data_direction == DMA_TO_DEVICE) {
841141685a3SXiubo Li 		/* TODO: */
8422bc396a2SIlias Tsitsimpis 	} else if (se_cmd->data_direction != DMA_NONE) {
8432bc396a2SIlias Tsitsimpis 		pr_warn("TCMU: data direction was %d!\n",
8442bc396a2SIlias Tsitsimpis 			se_cmd->data_direction);
8457c9e7a6fSAndy Grover 	}
8467c9e7a6fSAndy Grover 
8477c9e7a6fSAndy Grover 	target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status);
8487c9e7a6fSAndy Grover 
849141685a3SXiubo Li out:
850141685a3SXiubo Li 	cmd->se_cmd = NULL;
851b6df4b79SXiubo Li 	tcmu_cmd_free_data(cmd, cmd->dbi_cnt);
852141685a3SXiubo Li 	tcmu_free_cmd(cmd);
8537c9e7a6fSAndy Grover }
8547c9e7a6fSAndy Grover 
8557c9e7a6fSAndy Grover static unsigned int tcmu_handle_completions(struct tcmu_dev *udev)
8567c9e7a6fSAndy Grover {
8577c9e7a6fSAndy Grover 	struct tcmu_mailbox *mb;
8587c9e7a6fSAndy Grover 	int handled = 0;
8597c9e7a6fSAndy Grover 
8607c9e7a6fSAndy Grover 	if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) {
8617c9e7a6fSAndy Grover 		pr_err("ring broken, not handling completions\n");
8627c9e7a6fSAndy Grover 		return 0;
8637c9e7a6fSAndy Grover 	}
8647c9e7a6fSAndy Grover 
8657c9e7a6fSAndy Grover 	mb = udev->mb_addr;
8667c9e7a6fSAndy Grover 	tcmu_flush_dcache_range(mb, sizeof(*mb));
8677c9e7a6fSAndy Grover 
8687c9e7a6fSAndy Grover 	while (udev->cmdr_last_cleaned != ACCESS_ONCE(mb->cmd_tail)) {
8697c9e7a6fSAndy Grover 
8707c9e7a6fSAndy Grover 		struct tcmu_cmd_entry *entry = (void *) mb + CMDR_OFF + udev->cmdr_last_cleaned;
8717c9e7a6fSAndy Grover 		struct tcmu_cmd *cmd;
8727c9e7a6fSAndy Grover 
8737c9e7a6fSAndy Grover 		tcmu_flush_dcache_range(entry, sizeof(*entry));
8747c9e7a6fSAndy Grover 
8750ad46af8SAndy Grover 		if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD) {
8760ad46af8SAndy Grover 			UPDATE_HEAD(udev->cmdr_last_cleaned,
8770ad46af8SAndy Grover 				    tcmu_hdr_get_len(entry->hdr.len_op),
8780ad46af8SAndy Grover 				    udev->cmdr_size);
8797c9e7a6fSAndy Grover 			continue;
8807c9e7a6fSAndy Grover 		}
8810ad46af8SAndy Grover 		WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD);
8827c9e7a6fSAndy Grover 
8837c9e7a6fSAndy Grover 		spin_lock(&udev->commands_lock);
884d3e709e6SMatthew Wilcox 		cmd = idr_remove(&udev->commands, entry->hdr.cmd_id);
8857c9e7a6fSAndy Grover 		spin_unlock(&udev->commands_lock);
8867c9e7a6fSAndy Grover 
8877c9e7a6fSAndy Grover 		if (!cmd) {
8887c9e7a6fSAndy Grover 			pr_err("cmd_id not found, ring is broken\n");
8897c9e7a6fSAndy Grover 			set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags);
8907c9e7a6fSAndy Grover 			break;
8917c9e7a6fSAndy Grover 		}
8927c9e7a6fSAndy Grover 
8937c9e7a6fSAndy Grover 		tcmu_handle_completion(cmd, entry);
8947c9e7a6fSAndy Grover 
8950ad46af8SAndy Grover 		UPDATE_HEAD(udev->cmdr_last_cleaned,
8960ad46af8SAndy Grover 			    tcmu_hdr_get_len(entry->hdr.len_op),
8970ad46af8SAndy Grover 			    udev->cmdr_size);
8987c9e7a6fSAndy Grover 
8997c9e7a6fSAndy Grover 		handled++;
9007c9e7a6fSAndy Grover 	}
9017c9e7a6fSAndy Grover 
9027c9e7a6fSAndy Grover 	if (mb->cmd_tail == mb->cmd_head)
9037c9e7a6fSAndy Grover 		del_timer(&udev->timeout); /* no more pending cmds */
9047c9e7a6fSAndy Grover 
9057c9e7a6fSAndy Grover 	wake_up(&udev->wait_cmdr);
9067c9e7a6fSAndy Grover 
9077c9e7a6fSAndy Grover 	return handled;
9087c9e7a6fSAndy Grover }
9097c9e7a6fSAndy Grover 
9107c9e7a6fSAndy Grover static int tcmu_check_expired_cmd(int id, void *p, void *data)
9117c9e7a6fSAndy Grover {
9127c9e7a6fSAndy Grover 	struct tcmu_cmd *cmd = p;
9137c9e7a6fSAndy Grover 
9147c9e7a6fSAndy Grover 	if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
9157c9e7a6fSAndy Grover 		return 0;
9167c9e7a6fSAndy Grover 
917611e2267SAndy Grover 	if (!time_after(jiffies, cmd->deadline))
9187c9e7a6fSAndy Grover 		return 0;
9197c9e7a6fSAndy Grover 
9207c9e7a6fSAndy Grover 	set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags);
9217c9e7a6fSAndy Grover 	target_complete_cmd(cmd->se_cmd, SAM_STAT_CHECK_CONDITION);
9227c9e7a6fSAndy Grover 	cmd->se_cmd = NULL;
9237c9e7a6fSAndy Grover 
9247c9e7a6fSAndy Grover 	return 0;
9257c9e7a6fSAndy Grover }
9267c9e7a6fSAndy Grover 
9277c9e7a6fSAndy Grover static void tcmu_device_timedout(unsigned long data)
9287c9e7a6fSAndy Grover {
9297c9e7a6fSAndy Grover 	struct tcmu_dev *udev = (struct tcmu_dev *)data;
9307c9e7a6fSAndy Grover 	unsigned long flags;
9317c9e7a6fSAndy Grover 
9327c9e7a6fSAndy Grover 	spin_lock_irqsave(&udev->commands_lock, flags);
9337c9e7a6fSAndy Grover 	idr_for_each(&udev->commands, tcmu_check_expired_cmd, NULL);
9347c9e7a6fSAndy Grover 	spin_unlock_irqrestore(&udev->commands_lock, flags);
9357c9e7a6fSAndy Grover 
936b6df4b79SXiubo Li 	/* Try to wake up the ummap thread */
937b6df4b79SXiubo Li 	wake_up(&unmap_wait);
938b6df4b79SXiubo Li 
9397c9e7a6fSAndy Grover 	/*
9407c9e7a6fSAndy Grover 	 * We don't need to wakeup threads on wait_cmdr since they have their
9417c9e7a6fSAndy Grover 	 * own timeout.
9427c9e7a6fSAndy Grover 	 */
9437c9e7a6fSAndy Grover }
9447c9e7a6fSAndy Grover 
9457c9e7a6fSAndy Grover static int tcmu_attach_hba(struct se_hba *hba, u32 host_id)
9467c9e7a6fSAndy Grover {
9477c9e7a6fSAndy Grover 	struct tcmu_hba *tcmu_hba;
9487c9e7a6fSAndy Grover 
9497c9e7a6fSAndy Grover 	tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL);
9507c9e7a6fSAndy Grover 	if (!tcmu_hba)
9517c9e7a6fSAndy Grover 		return -ENOMEM;
9527c9e7a6fSAndy Grover 
9537c9e7a6fSAndy Grover 	tcmu_hba->host_id = host_id;
9547c9e7a6fSAndy Grover 	hba->hba_ptr = tcmu_hba;
9557c9e7a6fSAndy Grover 
9567c9e7a6fSAndy Grover 	return 0;
9577c9e7a6fSAndy Grover }
9587c9e7a6fSAndy Grover 
9597c9e7a6fSAndy Grover static void tcmu_detach_hba(struct se_hba *hba)
9607c9e7a6fSAndy Grover {
9617c9e7a6fSAndy Grover 	kfree(hba->hba_ptr);
9627c9e7a6fSAndy Grover 	hba->hba_ptr = NULL;
9637c9e7a6fSAndy Grover }
9647c9e7a6fSAndy Grover 
9657c9e7a6fSAndy Grover static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
9667c9e7a6fSAndy Grover {
9677c9e7a6fSAndy Grover 	struct tcmu_dev *udev;
9687c9e7a6fSAndy Grover 
9697c9e7a6fSAndy Grover 	udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL);
9707c9e7a6fSAndy Grover 	if (!udev)
9717c9e7a6fSAndy Grover 		return NULL;
972f3cdbe39SMike Christie 	kref_init(&udev->kref);
9737c9e7a6fSAndy Grover 
9747c9e7a6fSAndy Grover 	udev->name = kstrdup(name, GFP_KERNEL);
9757c9e7a6fSAndy Grover 	if (!udev->name) {
9767c9e7a6fSAndy Grover 		kfree(udev);
9777c9e7a6fSAndy Grover 		return NULL;
9787c9e7a6fSAndy Grover 	}
9797c9e7a6fSAndy Grover 
9807c9e7a6fSAndy Grover 	udev->hba = hba;
981af980e46SMike Christie 	udev->cmd_time_out = TCMU_TIME_OUT;
9827c9e7a6fSAndy Grover 
9837c9e7a6fSAndy Grover 	init_waitqueue_head(&udev->wait_cmdr);
984b6df4b79SXiubo Li 	mutex_init(&udev->cmdr_lock);
9857c9e7a6fSAndy Grover 
9867c9e7a6fSAndy Grover 	idr_init(&udev->commands);
9877c9e7a6fSAndy Grover 	spin_lock_init(&udev->commands_lock);
9887c9e7a6fSAndy Grover 
9897c9e7a6fSAndy Grover 	setup_timer(&udev->timeout, tcmu_device_timedout,
9907c9e7a6fSAndy Grover 		(unsigned long)udev);
9917c9e7a6fSAndy Grover 
9927c9e7a6fSAndy Grover 	return &udev->se_dev;
9937c9e7a6fSAndy Grover }
9947c9e7a6fSAndy Grover 
9957c9e7a6fSAndy Grover static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on)
9967c9e7a6fSAndy Grover {
9977c9e7a6fSAndy Grover 	struct tcmu_dev *tcmu_dev = container_of(info, struct tcmu_dev, uio_info);
9987c9e7a6fSAndy Grover 
999b6df4b79SXiubo Li 	mutex_lock(&tcmu_dev->cmdr_lock);
10007c9e7a6fSAndy Grover 	tcmu_handle_completions(tcmu_dev);
1001b6df4b79SXiubo Li 	mutex_unlock(&tcmu_dev->cmdr_lock);
10027c9e7a6fSAndy Grover 
10037c9e7a6fSAndy Grover 	return 0;
10047c9e7a6fSAndy Grover }
10057c9e7a6fSAndy Grover 
10067c9e7a6fSAndy Grover /*
10077c9e7a6fSAndy Grover  * mmap code from uio.c. Copied here because we want to hook mmap()
10087c9e7a6fSAndy Grover  * and this stuff must come along.
10097c9e7a6fSAndy Grover  */
10107c9e7a6fSAndy Grover static int tcmu_find_mem_index(struct vm_area_struct *vma)
10117c9e7a6fSAndy Grover {
10127c9e7a6fSAndy Grover 	struct tcmu_dev *udev = vma->vm_private_data;
10137c9e7a6fSAndy Grover 	struct uio_info *info = &udev->uio_info;
10147c9e7a6fSAndy Grover 
10157c9e7a6fSAndy Grover 	if (vma->vm_pgoff < MAX_UIO_MAPS) {
10167c9e7a6fSAndy Grover 		if (info->mem[vma->vm_pgoff].size == 0)
10177c9e7a6fSAndy Grover 			return -1;
10187c9e7a6fSAndy Grover 		return (int)vma->vm_pgoff;
10197c9e7a6fSAndy Grover 	}
10207c9e7a6fSAndy Grover 	return -1;
10217c9e7a6fSAndy Grover }
10227c9e7a6fSAndy Grover 
1023b6df4b79SXiubo Li static struct page *tcmu_try_get_block_page(struct tcmu_dev *udev, uint32_t dbi)
1024b6df4b79SXiubo Li {
1025b6df4b79SXiubo Li 	struct page *page;
1026b6df4b79SXiubo Li 	int ret;
1027b6df4b79SXiubo Li 
1028b6df4b79SXiubo Li 	mutex_lock(&udev->cmdr_lock);
1029b6df4b79SXiubo Li 	page = tcmu_get_block_page(udev, dbi);
1030b6df4b79SXiubo Li 	if (likely(page)) {
1031b6df4b79SXiubo Li 		mutex_unlock(&udev->cmdr_lock);
1032b6df4b79SXiubo Li 		return page;
1033b6df4b79SXiubo Li 	}
1034b6df4b79SXiubo Li 
1035b6df4b79SXiubo Li 	/*
1036b6df4b79SXiubo Li 	 * Normally it shouldn't be here:
1037b6df4b79SXiubo Li 	 * Only when the userspace has touched the blocks which
1038b6df4b79SXiubo Li 	 * are out of the tcmu_cmd's data iov[], and will return
1039b6df4b79SXiubo Li 	 * one zeroed page.
1040b6df4b79SXiubo Li 	 */
1041b6df4b79SXiubo Li 	pr_warn("Block(%u) out of cmd's iov[] has been touched!\n", dbi);
1042b6df4b79SXiubo Li 	pr_warn("Mostly it will be a bug of userspace, please have a check!\n");
1043b6df4b79SXiubo Li 
1044b6df4b79SXiubo Li 	if (dbi >= udev->dbi_thresh) {
1045b6df4b79SXiubo Li 		/* Extern the udev->dbi_thresh to dbi + 1 */
1046b6df4b79SXiubo Li 		udev->dbi_thresh = dbi + 1;
1047b6df4b79SXiubo Li 		udev->dbi_max = dbi;
1048b6df4b79SXiubo Li 	}
1049b6df4b79SXiubo Li 
1050b6df4b79SXiubo Li 	page = radix_tree_lookup(&udev->data_blocks, dbi);
1051b6df4b79SXiubo Li 	if (!page) {
1052b6df4b79SXiubo Li 		page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1053b6df4b79SXiubo Li 		if (!page) {
1054b6df4b79SXiubo Li 			mutex_unlock(&udev->cmdr_lock);
1055b6df4b79SXiubo Li 			return NULL;
1056b6df4b79SXiubo Li 		}
1057b6df4b79SXiubo Li 
1058b6df4b79SXiubo Li 		ret = radix_tree_insert(&udev->data_blocks, dbi, page);
1059b6df4b79SXiubo Li 		if (ret) {
1060b6df4b79SXiubo Li 			mutex_unlock(&udev->cmdr_lock);
1061b6df4b79SXiubo Li 			__free_page(page);
1062b6df4b79SXiubo Li 			return NULL;
1063b6df4b79SXiubo Li 		}
1064b6df4b79SXiubo Li 
1065b6df4b79SXiubo Li 		/*
1066b6df4b79SXiubo Li 		 * Since this case is rare in page fault routine, here we
1067b6df4b79SXiubo Li 		 * will allow the global_db_count >= TCMU_GLOBAL_MAX_BLOCKS
1068b6df4b79SXiubo Li 		 * to reduce possible page fault call trace.
1069b6df4b79SXiubo Li 		 */
1070b6df4b79SXiubo Li 		atomic_inc(&global_db_count);
1071b6df4b79SXiubo Li 	}
1072b6df4b79SXiubo Li 	mutex_unlock(&udev->cmdr_lock);
1073b6df4b79SXiubo Li 
1074b6df4b79SXiubo Li 	return page;
1075b6df4b79SXiubo Li }
1076b6df4b79SXiubo Li 
107711bac800SDave Jiang static int tcmu_vma_fault(struct vm_fault *vmf)
10787c9e7a6fSAndy Grover {
107911bac800SDave Jiang 	struct tcmu_dev *udev = vmf->vma->vm_private_data;
10807c9e7a6fSAndy Grover 	struct uio_info *info = &udev->uio_info;
10817c9e7a6fSAndy Grover 	struct page *page;
10827c9e7a6fSAndy Grover 	unsigned long offset;
10837c9e7a6fSAndy Grover 	void *addr;
10847c9e7a6fSAndy Grover 
108511bac800SDave Jiang 	int mi = tcmu_find_mem_index(vmf->vma);
10867c9e7a6fSAndy Grover 	if (mi < 0)
10877c9e7a6fSAndy Grover 		return VM_FAULT_SIGBUS;
10887c9e7a6fSAndy Grover 
10897c9e7a6fSAndy Grover 	/*
10907c9e7a6fSAndy Grover 	 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
10917c9e7a6fSAndy Grover 	 * to use mem[N].
10927c9e7a6fSAndy Grover 	 */
10937c9e7a6fSAndy Grover 	offset = (vmf->pgoff - mi) << PAGE_SHIFT;
10947c9e7a6fSAndy Grover 
1095141685a3SXiubo Li 	if (offset < udev->data_off) {
1096141685a3SXiubo Li 		/* For the vmalloc()ed cmd area pages */
10977c9e7a6fSAndy Grover 		addr = (void *)(unsigned long)info->mem[mi].addr + offset;
10987c9e7a6fSAndy Grover 		page = vmalloc_to_page(addr);
1099141685a3SXiubo Li 	} else {
1100141685a3SXiubo Li 		uint32_t dbi;
1101141685a3SXiubo Li 
1102b6df4b79SXiubo Li 		/* For the dynamically growing data area pages */
1103141685a3SXiubo Li 		dbi = (offset - udev->data_off) / DATA_BLOCK_SIZE;
1104b6df4b79SXiubo Li 		page = tcmu_try_get_block_page(udev, dbi);
1105b6df4b79SXiubo Li 		if (!page)
1106141685a3SXiubo Li 			return VM_FAULT_NOPAGE;
1107141685a3SXiubo Li 	}
1108141685a3SXiubo Li 
11097c9e7a6fSAndy Grover 	get_page(page);
11107c9e7a6fSAndy Grover 	vmf->page = page;
11117c9e7a6fSAndy Grover 	return 0;
11127c9e7a6fSAndy Grover }
11137c9e7a6fSAndy Grover 
11147c9e7a6fSAndy Grover static const struct vm_operations_struct tcmu_vm_ops = {
11157c9e7a6fSAndy Grover 	.fault = tcmu_vma_fault,
11167c9e7a6fSAndy Grover };
11177c9e7a6fSAndy Grover 
11187c9e7a6fSAndy Grover static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma)
11197c9e7a6fSAndy Grover {
11207c9e7a6fSAndy Grover 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
11217c9e7a6fSAndy Grover 
11227c9e7a6fSAndy Grover 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
11237c9e7a6fSAndy Grover 	vma->vm_ops = &tcmu_vm_ops;
11247c9e7a6fSAndy Grover 
11257c9e7a6fSAndy Grover 	vma->vm_private_data = udev;
11267c9e7a6fSAndy Grover 
11277c9e7a6fSAndy Grover 	/* Ensure the mmap is exactly the right size */
11287c9e7a6fSAndy Grover 	if (vma_pages(vma) != (TCMU_RING_SIZE >> PAGE_SHIFT))
11297c9e7a6fSAndy Grover 		return -EINVAL;
11307c9e7a6fSAndy Grover 
11317c9e7a6fSAndy Grover 	return 0;
11327c9e7a6fSAndy Grover }
11337c9e7a6fSAndy Grover 
11347c9e7a6fSAndy Grover static int tcmu_open(struct uio_info *info, struct inode *inode)
11357c9e7a6fSAndy Grover {
11367c9e7a6fSAndy Grover 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
11377c9e7a6fSAndy Grover 
11387c9e7a6fSAndy Grover 	/* O_EXCL not supported for char devs, so fake it? */
11397c9e7a6fSAndy Grover 	if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags))
11407c9e7a6fSAndy Grover 		return -EBUSY;
11417c9e7a6fSAndy Grover 
1142b6df4b79SXiubo Li 	udev->inode = inode;
1143b6df4b79SXiubo Li 
11447c9e7a6fSAndy Grover 	pr_debug("open\n");
11457c9e7a6fSAndy Grover 
11467c9e7a6fSAndy Grover 	return 0;
11477c9e7a6fSAndy Grover }
11487c9e7a6fSAndy Grover 
1149f3cdbe39SMike Christie static void tcmu_dev_call_rcu(struct rcu_head *p)
1150f3cdbe39SMike Christie {
1151f3cdbe39SMike Christie 	struct se_device *dev = container_of(p, struct se_device, rcu_head);
1152f3cdbe39SMike Christie 	struct tcmu_dev *udev = TCMU_DEV(dev);
1153f3cdbe39SMike Christie 
1154f3cdbe39SMike Christie 	kfree(udev->uio_info.name);
1155f3cdbe39SMike Christie 	kfree(udev->name);
1156f3cdbe39SMike Christie 	kfree(udev);
1157f3cdbe39SMike Christie }
1158f3cdbe39SMike Christie 
1159f3cdbe39SMike Christie static void tcmu_dev_kref_release(struct kref *kref)
1160f3cdbe39SMike Christie {
1161f3cdbe39SMike Christie 	struct tcmu_dev *udev = container_of(kref, struct tcmu_dev, kref);
1162f3cdbe39SMike Christie 	struct se_device *dev = &udev->se_dev;
1163f3cdbe39SMike Christie 
1164f3cdbe39SMike Christie 	call_rcu(&dev->rcu_head, tcmu_dev_call_rcu);
1165f3cdbe39SMike Christie }
1166f3cdbe39SMike Christie 
11677c9e7a6fSAndy Grover static int tcmu_release(struct uio_info *info, struct inode *inode)
11687c9e7a6fSAndy Grover {
11697c9e7a6fSAndy Grover 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
11707c9e7a6fSAndy Grover 
11717c9e7a6fSAndy Grover 	clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags);
11727c9e7a6fSAndy Grover 
11737c9e7a6fSAndy Grover 	pr_debug("close\n");
1174f3cdbe39SMike Christie 	/* release ref from configure */
1175f3cdbe39SMike Christie 	kref_put(&udev->kref, tcmu_dev_kref_release);
11767c9e7a6fSAndy Grover 	return 0;
11777c9e7a6fSAndy Grover }
11787c9e7a6fSAndy Grover 
11797c9e7a6fSAndy Grover static int tcmu_netlink_event(enum tcmu_genl_cmd cmd, const char *name, int minor)
11807c9e7a6fSAndy Grover {
11817c9e7a6fSAndy Grover 	struct sk_buff *skb;
11827c9e7a6fSAndy Grover 	void *msg_header;
11836e14eab9SNicholas Bellinger 	int ret = -ENOMEM;
11847c9e7a6fSAndy Grover 
11857c9e7a6fSAndy Grover 	skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
11867c9e7a6fSAndy Grover 	if (!skb)
11876e14eab9SNicholas Bellinger 		return ret;
11887c9e7a6fSAndy Grover 
11897c9e7a6fSAndy Grover 	msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd);
11906e14eab9SNicholas Bellinger 	if (!msg_header)
11916e14eab9SNicholas Bellinger 		goto free_skb;
11927c9e7a6fSAndy Grover 
11937c9e7a6fSAndy Grover 	ret = nla_put_string(skb, TCMU_ATTR_DEVICE, name);
11946e14eab9SNicholas Bellinger 	if (ret < 0)
11956e14eab9SNicholas Bellinger 		goto free_skb;
11967c9e7a6fSAndy Grover 
11977c9e7a6fSAndy Grover 	ret = nla_put_u32(skb, TCMU_ATTR_MINOR, minor);
11986e14eab9SNicholas Bellinger 	if (ret < 0)
11996e14eab9SNicholas Bellinger 		goto free_skb;
12007c9e7a6fSAndy Grover 
1201053c095aSJohannes Berg 	genlmsg_end(skb, msg_header);
12027c9e7a6fSAndy Grover 
120320c08b36SSheng Yang 	ret = genlmsg_multicast_allns(&tcmu_genl_family, skb, 0,
12047c9e7a6fSAndy Grover 				TCMU_MCGRP_CONFIG, GFP_KERNEL);
12057c9e7a6fSAndy Grover 
12067c9e7a6fSAndy Grover 	/* We don't care if no one is listening */
12077c9e7a6fSAndy Grover 	if (ret == -ESRCH)
12087c9e7a6fSAndy Grover 		ret = 0;
12097c9e7a6fSAndy Grover 
12107c9e7a6fSAndy Grover 	return ret;
12116e14eab9SNicholas Bellinger free_skb:
12126e14eab9SNicholas Bellinger 	nlmsg_free(skb);
12136e14eab9SNicholas Bellinger 	return ret;
12147c9e7a6fSAndy Grover }
12157c9e7a6fSAndy Grover 
12167c9e7a6fSAndy Grover static int tcmu_configure_device(struct se_device *dev)
12177c9e7a6fSAndy Grover {
12187c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
12197c9e7a6fSAndy Grover 	struct tcmu_hba *hba = udev->hba->hba_ptr;
12207c9e7a6fSAndy Grover 	struct uio_info *info;
12217c9e7a6fSAndy Grover 	struct tcmu_mailbox *mb;
12227c9e7a6fSAndy Grover 	size_t size;
12237c9e7a6fSAndy Grover 	size_t used;
12247c9e7a6fSAndy Grover 	int ret = 0;
12257c9e7a6fSAndy Grover 	char *str;
12267c9e7a6fSAndy Grover 
12277c9e7a6fSAndy Grover 	info = &udev->uio_info;
12287c9e7a6fSAndy Grover 
12297c9e7a6fSAndy Grover 	size = snprintf(NULL, 0, "tcm-user/%u/%s/%s", hba->host_id, udev->name,
12307c9e7a6fSAndy Grover 			udev->dev_config);
12317c9e7a6fSAndy Grover 	size += 1; /* for \0 */
12327c9e7a6fSAndy Grover 	str = kmalloc(size, GFP_KERNEL);
12337c9e7a6fSAndy Grover 	if (!str)
12347c9e7a6fSAndy Grover 		return -ENOMEM;
12357c9e7a6fSAndy Grover 
12367c9e7a6fSAndy Grover 	used = snprintf(str, size, "tcm-user/%u/%s", hba->host_id, udev->name);
12377c9e7a6fSAndy Grover 
12387c9e7a6fSAndy Grover 	if (udev->dev_config[0])
12397c9e7a6fSAndy Grover 		snprintf(str + used, size - used, "/%s", udev->dev_config);
12407c9e7a6fSAndy Grover 
12417c9e7a6fSAndy Grover 	info->name = str;
12427c9e7a6fSAndy Grover 
1243141685a3SXiubo Li 	udev->mb_addr = vzalloc(CMDR_SIZE);
12447c9e7a6fSAndy Grover 	if (!udev->mb_addr) {
12457c9e7a6fSAndy Grover 		ret = -ENOMEM;
12467c9e7a6fSAndy Grover 		goto err_vzalloc;
12477c9e7a6fSAndy Grover 	}
12487c9e7a6fSAndy Grover 
12497c9e7a6fSAndy Grover 	/* mailbox fits in first part of CMDR space */
12507c9e7a6fSAndy Grover 	udev->cmdr_size = CMDR_SIZE - CMDR_OFF;
12517c9e7a6fSAndy Grover 	udev->data_off = CMDR_SIZE;
1252141685a3SXiubo Li 	udev->data_size = DATA_SIZE;
1253b6df4b79SXiubo Li 	udev->dbi_thresh = 0; /* Default in Idle state */
1254b6df4b79SXiubo Li 	udev->waiting_global = false;
12557c9e7a6fSAndy Grover 
1256141685a3SXiubo Li 	/* Initialise the mailbox of the ring buffer */
12577c9e7a6fSAndy Grover 	mb = udev->mb_addr;
12580ad46af8SAndy Grover 	mb->version = TCMU_MAILBOX_VERSION;
125932c76de3SSheng Yang 	mb->flags = TCMU_MAILBOX_FLAG_CAP_OOOC;
12607c9e7a6fSAndy Grover 	mb->cmdr_off = CMDR_OFF;
12617c9e7a6fSAndy Grover 	mb->cmdr_size = udev->cmdr_size;
12627c9e7a6fSAndy Grover 
12637c9e7a6fSAndy Grover 	WARN_ON(!PAGE_ALIGNED(udev->data_off));
12647c9e7a6fSAndy Grover 	WARN_ON(udev->data_size % PAGE_SIZE);
126526418649SSheng Yang 	WARN_ON(udev->data_size % DATA_BLOCK_SIZE);
12667c9e7a6fSAndy Grover 
1267b6df4b79SXiubo Li 	INIT_RADIX_TREE(&udev->data_blocks, GFP_KERNEL);
1268141685a3SXiubo Li 
1269ac64a2ceSDavid Disseldorp 	info->version = __stringify(TCMU_MAILBOX_VERSION);
12707c9e7a6fSAndy Grover 
12717c9e7a6fSAndy Grover 	info->mem[0].name = "tcm-user command & data buffer";
12720633e123SArnd Bergmann 	info->mem[0].addr = (phys_addr_t)(uintptr_t)udev->mb_addr;
12737c9e7a6fSAndy Grover 	info->mem[0].size = TCMU_RING_SIZE;
1274141685a3SXiubo Li 	info->mem[0].memtype = UIO_MEM_NONE;
12757c9e7a6fSAndy Grover 
12767c9e7a6fSAndy Grover 	info->irqcontrol = tcmu_irqcontrol;
12777c9e7a6fSAndy Grover 	info->irq = UIO_IRQ_CUSTOM;
12787c9e7a6fSAndy Grover 
12797c9e7a6fSAndy Grover 	info->mmap = tcmu_mmap;
12807c9e7a6fSAndy Grover 	info->open = tcmu_open;
12817c9e7a6fSAndy Grover 	info->release = tcmu_release;
12827c9e7a6fSAndy Grover 
12837c9e7a6fSAndy Grover 	ret = uio_register_device(tcmu_root_device, info);
12847c9e7a6fSAndy Grover 	if (ret)
12857c9e7a6fSAndy Grover 		goto err_register;
12867c9e7a6fSAndy Grover 
128781ee28deSSheng Yang 	/* User can set hw_block_size before enable the device */
128881ee28deSSheng Yang 	if (dev->dev_attrib.hw_block_size == 0)
12897c9e7a6fSAndy Grover 		dev->dev_attrib.hw_block_size = 512;
129081ee28deSSheng Yang 	/* Other attributes can be configured in userspace */
12913abaa2bfSMike Christie 	if (!dev->dev_attrib.hw_max_sectors)
12927c9e7a6fSAndy Grover 		dev->dev_attrib.hw_max_sectors = 128;
12939a8bb606SBryant G. Ly 	if (!dev->dev_attrib.emulate_write_cache)
12949a8bb606SBryant G. Ly 		dev->dev_attrib.emulate_write_cache = 0;
12957c9e7a6fSAndy Grover 	dev->dev_attrib.hw_queue_depth = 128;
12967c9e7a6fSAndy Grover 
1297f3cdbe39SMike Christie 	/*
1298f3cdbe39SMike Christie 	 * Get a ref incase userspace does a close on the uio device before
1299f3cdbe39SMike Christie 	 * LIO has initiated tcmu_free_device.
1300f3cdbe39SMike Christie 	 */
1301f3cdbe39SMike Christie 	kref_get(&udev->kref);
1302f3cdbe39SMike Christie 
13037c9e7a6fSAndy Grover 	ret = tcmu_netlink_event(TCMU_CMD_ADDED_DEVICE, udev->uio_info.name,
13047c9e7a6fSAndy Grover 				 udev->uio_info.uio_dev->minor);
13057c9e7a6fSAndy Grover 	if (ret)
13067c9e7a6fSAndy Grover 		goto err_netlink;
13077c9e7a6fSAndy Grover 
1308b6df4b79SXiubo Li 	mutex_lock(&root_udev_mutex);
1309b6df4b79SXiubo Li 	list_add(&udev->node, &root_udev);
1310b6df4b79SXiubo Li 	mutex_unlock(&root_udev_mutex);
1311b6df4b79SXiubo Li 
13127c9e7a6fSAndy Grover 	return 0;
13137c9e7a6fSAndy Grover 
13147c9e7a6fSAndy Grover err_netlink:
1315f3cdbe39SMike Christie 	kref_put(&udev->kref, tcmu_dev_kref_release);
13167c9e7a6fSAndy Grover 	uio_unregister_device(&udev->uio_info);
13177c9e7a6fSAndy Grover err_register:
13187c9e7a6fSAndy Grover 	vfree(udev->mb_addr);
13197c9e7a6fSAndy Grover err_vzalloc:
13207c9e7a6fSAndy Grover 	kfree(info->name);
1321f3cdbe39SMike Christie 	info->name = NULL;
13227c9e7a6fSAndy Grover 
13237c9e7a6fSAndy Grover 	return ret;
13247c9e7a6fSAndy Grover }
13257c9e7a6fSAndy Grover 
1326b25c7863SSheng Yang static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd)
13277c9e7a6fSAndy Grover {
1328b25c7863SSheng Yang 	if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
1329b25c7863SSheng Yang 		kmem_cache_free(tcmu_cmd_cache, cmd);
13307c9e7a6fSAndy Grover 		return 0;
1331b25c7863SSheng Yang 	}
13327c9e7a6fSAndy Grover 	return -EINVAL;
13337c9e7a6fSAndy Grover }
13347c9e7a6fSAndy Grover 
1335972c7f16SMike Christie static bool tcmu_dev_configured(struct tcmu_dev *udev)
1336972c7f16SMike Christie {
1337972c7f16SMike Christie 	return udev->uio_info.uio_dev ? true : false;
1338972c7f16SMike Christie }
1339972c7f16SMike Christie 
1340b6df4b79SXiubo Li static void tcmu_blocks_release(struct tcmu_dev *udev)
1341b6df4b79SXiubo Li {
1342b6df4b79SXiubo Li 	int i;
1343b6df4b79SXiubo Li 	struct page *page;
1344b6df4b79SXiubo Li 
1345b6df4b79SXiubo Li 	/* Try to release all block pages */
1346b6df4b79SXiubo Li 	mutex_lock(&udev->cmdr_lock);
1347b6df4b79SXiubo Li 	for (i = 0; i <= udev->dbi_max; i++) {
1348b6df4b79SXiubo Li 		page = radix_tree_delete(&udev->data_blocks, i);
1349b6df4b79SXiubo Li 		if (page) {
1350b6df4b79SXiubo Li 			__free_page(page);
1351b6df4b79SXiubo Li 			atomic_dec(&global_db_count);
1352b6df4b79SXiubo Li 		}
1353b6df4b79SXiubo Li 	}
1354b6df4b79SXiubo Li 	mutex_unlock(&udev->cmdr_lock);
1355b6df4b79SXiubo Li }
1356b6df4b79SXiubo Li 
13577c9e7a6fSAndy Grover static void tcmu_free_device(struct se_device *dev)
13587c9e7a6fSAndy Grover {
13597c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
1360b25c7863SSheng Yang 	struct tcmu_cmd *cmd;
1361b25c7863SSheng Yang 	bool all_expired = true;
13627c9e7a6fSAndy Grover 	int i;
13637c9e7a6fSAndy Grover 
13647c9e7a6fSAndy Grover 	del_timer_sync(&udev->timeout);
13657c9e7a6fSAndy Grover 
1366b6df4b79SXiubo Li 	mutex_lock(&root_udev_mutex);
1367b6df4b79SXiubo Li 	list_del(&udev->node);
1368b6df4b79SXiubo Li 	mutex_unlock(&root_udev_mutex);
1369b6df4b79SXiubo Li 
13707c9e7a6fSAndy Grover 	vfree(udev->mb_addr);
13717c9e7a6fSAndy Grover 
13727c9e7a6fSAndy Grover 	/* Upper layer should drain all requests before calling this */
13737c9e7a6fSAndy Grover 	spin_lock_irq(&udev->commands_lock);
1374b25c7863SSheng Yang 	idr_for_each_entry(&udev->commands, cmd, i) {
1375b25c7863SSheng Yang 		if (tcmu_check_and_free_pending_cmd(cmd) != 0)
1376b25c7863SSheng Yang 			all_expired = false;
1377b25c7863SSheng Yang 	}
13787c9e7a6fSAndy Grover 	idr_destroy(&udev->commands);
13797c9e7a6fSAndy Grover 	spin_unlock_irq(&udev->commands_lock);
1380b25c7863SSheng Yang 	WARN_ON(!all_expired);
13817c9e7a6fSAndy Grover 
1382b6df4b79SXiubo Li 	tcmu_blocks_release(udev);
1383141685a3SXiubo Li 
1384972c7f16SMike Christie 	if (tcmu_dev_configured(udev)) {
13857c9e7a6fSAndy Grover 		tcmu_netlink_event(TCMU_CMD_REMOVED_DEVICE, udev->uio_info.name,
13867c9e7a6fSAndy Grover 				   udev->uio_info.uio_dev->minor);
13877c9e7a6fSAndy Grover 
13887c9e7a6fSAndy Grover 		uio_unregister_device(&udev->uio_info);
13897c9e7a6fSAndy Grover 	}
1390f3cdbe39SMike Christie 
1391f3cdbe39SMike Christie 	/* release ref from init */
1392f3cdbe39SMike Christie 	kref_put(&udev->kref, tcmu_dev_kref_release);
13937c9e7a6fSAndy Grover }
13947c9e7a6fSAndy Grover 
13957c9e7a6fSAndy Grover enum {
13963abaa2bfSMike Christie 	Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_hw_max_sectors,
13977d7a7435SNicholas Bellinger 	Opt_err,
13987c9e7a6fSAndy Grover };
13997c9e7a6fSAndy Grover 
14007c9e7a6fSAndy Grover static match_table_t tokens = {
14017c9e7a6fSAndy Grover 	{Opt_dev_config, "dev_config=%s"},
14027c9e7a6fSAndy Grover 	{Opt_dev_size, "dev_size=%u"},
14039c1cd1b6SAndy Grover 	{Opt_hw_block_size, "hw_block_size=%u"},
14043abaa2bfSMike Christie 	{Opt_hw_max_sectors, "hw_max_sectors=%u"},
14057c9e7a6fSAndy Grover 	{Opt_err, NULL}
14067c9e7a6fSAndy Grover };
14077c9e7a6fSAndy Grover 
14083abaa2bfSMike Christie static int tcmu_set_dev_attrib(substring_t *arg, u32 *dev_attrib)
14093abaa2bfSMike Christie {
14103abaa2bfSMike Christie 	unsigned long tmp_ul;
14113abaa2bfSMike Christie 	char *arg_p;
14123abaa2bfSMike Christie 	int ret;
14133abaa2bfSMike Christie 
14143abaa2bfSMike Christie 	arg_p = match_strdup(arg);
14153abaa2bfSMike Christie 	if (!arg_p)
14163abaa2bfSMike Christie 		return -ENOMEM;
14173abaa2bfSMike Christie 
14183abaa2bfSMike Christie 	ret = kstrtoul(arg_p, 0, &tmp_ul);
14193abaa2bfSMike Christie 	kfree(arg_p);
14203abaa2bfSMike Christie 	if (ret < 0) {
14213abaa2bfSMike Christie 		pr_err("kstrtoul() failed for dev attrib\n");
14223abaa2bfSMike Christie 		return ret;
14233abaa2bfSMike Christie 	}
14243abaa2bfSMike Christie 	if (!tmp_ul) {
14253abaa2bfSMike Christie 		pr_err("dev attrib must be nonzero\n");
14263abaa2bfSMike Christie 		return -EINVAL;
14273abaa2bfSMike Christie 	}
14283abaa2bfSMike Christie 	*dev_attrib = tmp_ul;
14293abaa2bfSMike Christie 	return 0;
14303abaa2bfSMike Christie }
14313abaa2bfSMike Christie 
14327c9e7a6fSAndy Grover static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev,
14337c9e7a6fSAndy Grover 		const char *page, ssize_t count)
14347c9e7a6fSAndy Grover {
14357c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
14367c9e7a6fSAndy Grover 	char *orig, *ptr, *opts, *arg_p;
14377c9e7a6fSAndy Grover 	substring_t args[MAX_OPT_ARGS];
14387c9e7a6fSAndy Grover 	int ret = 0, token;
14397c9e7a6fSAndy Grover 
14407c9e7a6fSAndy Grover 	opts = kstrdup(page, GFP_KERNEL);
14417c9e7a6fSAndy Grover 	if (!opts)
14427c9e7a6fSAndy Grover 		return -ENOMEM;
14437c9e7a6fSAndy Grover 
14447c9e7a6fSAndy Grover 	orig = opts;
14457c9e7a6fSAndy Grover 
14467c9e7a6fSAndy Grover 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
14477c9e7a6fSAndy Grover 		if (!*ptr)
14487c9e7a6fSAndy Grover 			continue;
14497c9e7a6fSAndy Grover 
14507c9e7a6fSAndy Grover 		token = match_token(ptr, tokens, args);
14517c9e7a6fSAndy Grover 		switch (token) {
14527c9e7a6fSAndy Grover 		case Opt_dev_config:
14537c9e7a6fSAndy Grover 			if (match_strlcpy(udev->dev_config, &args[0],
14547c9e7a6fSAndy Grover 					  TCMU_CONFIG_LEN) == 0) {
14557c9e7a6fSAndy Grover 				ret = -EINVAL;
14567c9e7a6fSAndy Grover 				break;
14577c9e7a6fSAndy Grover 			}
14587c9e7a6fSAndy Grover 			pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config);
14597c9e7a6fSAndy Grover 			break;
14607c9e7a6fSAndy Grover 		case Opt_dev_size:
14617c9e7a6fSAndy Grover 			arg_p = match_strdup(&args[0]);
14627c9e7a6fSAndy Grover 			if (!arg_p) {
14637c9e7a6fSAndy Grover 				ret = -ENOMEM;
14647c9e7a6fSAndy Grover 				break;
14657c9e7a6fSAndy Grover 			}
14667c9e7a6fSAndy Grover 			ret = kstrtoul(arg_p, 0, (unsigned long *) &udev->dev_size);
14677c9e7a6fSAndy Grover 			kfree(arg_p);
14687c9e7a6fSAndy Grover 			if (ret < 0)
14697c9e7a6fSAndy Grover 				pr_err("kstrtoul() failed for dev_size=\n");
14707c9e7a6fSAndy Grover 			break;
14719c1cd1b6SAndy Grover 		case Opt_hw_block_size:
14723abaa2bfSMike Christie 			ret = tcmu_set_dev_attrib(&args[0],
14733abaa2bfSMike Christie 					&(dev->dev_attrib.hw_block_size));
14749c1cd1b6SAndy Grover 			break;
14753abaa2bfSMike Christie 		case Opt_hw_max_sectors:
14763abaa2bfSMike Christie 			ret = tcmu_set_dev_attrib(&args[0],
14773abaa2bfSMike Christie 					&(dev->dev_attrib.hw_max_sectors));
14789c1cd1b6SAndy Grover 			break;
14797c9e7a6fSAndy Grover 		default:
14807c9e7a6fSAndy Grover 			break;
14817c9e7a6fSAndy Grover 		}
14822579325cSMike Christie 
14832579325cSMike Christie 		if (ret)
14842579325cSMike Christie 			break;
14857c9e7a6fSAndy Grover 	}
14867c9e7a6fSAndy Grover 
14877c9e7a6fSAndy Grover 	kfree(orig);
14887c9e7a6fSAndy Grover 	return (!ret) ? count : ret;
14897c9e7a6fSAndy Grover }
14907c9e7a6fSAndy Grover 
14917c9e7a6fSAndy Grover static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b)
14927c9e7a6fSAndy Grover {
14937c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
14947c9e7a6fSAndy Grover 	ssize_t bl = 0;
14957c9e7a6fSAndy Grover 
14967c9e7a6fSAndy Grover 	bl = sprintf(b + bl, "Config: %s ",
14977c9e7a6fSAndy Grover 		     udev->dev_config[0] ? udev->dev_config : "NULL");
14987d7a7435SNicholas Bellinger 	bl += sprintf(b + bl, "Size: %zu\n", udev->dev_size);
14997c9e7a6fSAndy Grover 
15007c9e7a6fSAndy Grover 	return bl;
15017c9e7a6fSAndy Grover }
15027c9e7a6fSAndy Grover 
15037c9e7a6fSAndy Grover static sector_t tcmu_get_blocks(struct se_device *dev)
15047c9e7a6fSAndy Grover {
15057c9e7a6fSAndy Grover 	struct tcmu_dev *udev = TCMU_DEV(dev);
15067c9e7a6fSAndy Grover 
15077c9e7a6fSAndy Grover 	return div_u64(udev->dev_size - dev->dev_attrib.block_size,
15087c9e7a6fSAndy Grover 		       dev->dev_attrib.block_size);
15097c9e7a6fSAndy Grover }
15107c9e7a6fSAndy Grover 
15117c9e7a6fSAndy Grover static sense_reason_t
15127c9e7a6fSAndy Grover tcmu_parse_cdb(struct se_cmd *cmd)
15137c9e7a6fSAndy Grover {
151402eb924fSAndy Grover 	return passthrough_parse_cdb(cmd, tcmu_queue_cmd);
15159c1cd1b6SAndy Grover }
15169c1cd1b6SAndy Grover 
15177d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_show(struct config_item *item, char *page)
15187d7a7435SNicholas Bellinger {
15197d7a7435SNicholas Bellinger 	struct se_dev_attrib *da = container_of(to_config_group(item),
15207d7a7435SNicholas Bellinger 					struct se_dev_attrib, da_group);
15217d7a7435SNicholas Bellinger 	struct tcmu_dev *udev = container_of(da->da_dev,
15227d7a7435SNicholas Bellinger 					struct tcmu_dev, se_dev);
15237d7a7435SNicholas Bellinger 
15247d7a7435SNicholas Bellinger 	return snprintf(page, PAGE_SIZE, "%lu\n", udev->cmd_time_out / MSEC_PER_SEC);
15257d7a7435SNicholas Bellinger }
15267d7a7435SNicholas Bellinger 
15277d7a7435SNicholas Bellinger static ssize_t tcmu_cmd_time_out_store(struct config_item *item, const char *page,
15287d7a7435SNicholas Bellinger 				       size_t count)
15297d7a7435SNicholas Bellinger {
15307d7a7435SNicholas Bellinger 	struct se_dev_attrib *da = container_of(to_config_group(item),
15317d7a7435SNicholas Bellinger 					struct se_dev_attrib, da_group);
15327d7a7435SNicholas Bellinger 	struct tcmu_dev *udev = container_of(da->da_dev,
15337d7a7435SNicholas Bellinger 					struct tcmu_dev, se_dev);
15347d7a7435SNicholas Bellinger 	u32 val;
15357d7a7435SNicholas Bellinger 	int ret;
15367d7a7435SNicholas Bellinger 
15377d7a7435SNicholas Bellinger 	if (da->da_dev->export_count) {
15387d7a7435SNicholas Bellinger 		pr_err("Unable to set tcmu cmd_time_out while exports exist\n");
15397d7a7435SNicholas Bellinger 		return -EINVAL;
15407d7a7435SNicholas Bellinger 	}
15417d7a7435SNicholas Bellinger 
15427d7a7435SNicholas Bellinger 	ret = kstrtou32(page, 0, &val);
15437d7a7435SNicholas Bellinger 	if (ret < 0)
15447d7a7435SNicholas Bellinger 		return ret;
15457d7a7435SNicholas Bellinger 
15467d7a7435SNicholas Bellinger 	udev->cmd_time_out = val * MSEC_PER_SEC;
15477d7a7435SNicholas Bellinger 	return count;
15487d7a7435SNicholas Bellinger }
15497d7a7435SNicholas Bellinger CONFIGFS_ATTR(tcmu_, cmd_time_out);
15507d7a7435SNicholas Bellinger 
1551801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_show(struct config_item *item, char *page)
1552801fc54dSBryant G. Ly {
1553801fc54dSBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
1554801fc54dSBryant G. Ly 						struct se_dev_attrib, da_group);
1555801fc54dSBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
1556801fc54dSBryant G. Ly 
1557801fc54dSBryant G. Ly 	return snprintf(page, PAGE_SIZE, "%zu\n", udev->dev_size);
1558801fc54dSBryant G. Ly }
1559801fc54dSBryant G. Ly 
1560801fc54dSBryant G. Ly static ssize_t tcmu_dev_size_store(struct config_item *item, const char *page,
1561801fc54dSBryant G. Ly 				   size_t count)
1562801fc54dSBryant G. Ly {
1563801fc54dSBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
1564801fc54dSBryant G. Ly 						struct se_dev_attrib, da_group);
1565801fc54dSBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
1566801fc54dSBryant G. Ly 	unsigned long val;
1567801fc54dSBryant G. Ly 	int ret;
1568801fc54dSBryant G. Ly 
1569801fc54dSBryant G. Ly 	ret = kstrtoul(page, 0, &val);
1570801fc54dSBryant G. Ly 	if (ret < 0)
1571801fc54dSBryant G. Ly 		return ret;
1572801fc54dSBryant G. Ly 	udev->dev_size = val;
1573801fc54dSBryant G. Ly 
1574801fc54dSBryant G. Ly 	/* Check if device has been configured before */
1575801fc54dSBryant G. Ly 	if (tcmu_dev_configured(udev)) {
1576801fc54dSBryant G. Ly 		ret = tcmu_netlink_event(TCMU_CMD_RECONFIG_DEVICE,
1577801fc54dSBryant G. Ly 					 udev->uio_info.name,
1578801fc54dSBryant G. Ly 					 udev->uio_info.uio_dev->minor);
1579801fc54dSBryant G. Ly 		if (ret) {
1580801fc54dSBryant G. Ly 			pr_err("Unable to reconfigure device\n");
1581801fc54dSBryant G. Ly 			return ret;
1582801fc54dSBryant G. Ly 		}
1583801fc54dSBryant G. Ly 	}
1584801fc54dSBryant G. Ly 
1585801fc54dSBryant G. Ly 	return count;
1586801fc54dSBryant G. Ly }
1587801fc54dSBryant G. Ly CONFIGFS_ATTR(tcmu_, dev_size);
1588801fc54dSBryant G. Ly 
15899a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_show(struct config_item *item,
15909a8bb606SBryant G. Ly 					     char *page)
15919a8bb606SBryant G. Ly {
15929a8bb606SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
15939a8bb606SBryant G. Ly 					struct se_dev_attrib, da_group);
15949a8bb606SBryant G. Ly 
15959a8bb606SBryant G. Ly 	return snprintf(page, PAGE_SIZE, "%i\n", da->emulate_write_cache);
15969a8bb606SBryant G. Ly }
15979a8bb606SBryant G. Ly 
15989a8bb606SBryant G. Ly static ssize_t tcmu_emulate_write_cache_store(struct config_item *item,
15999a8bb606SBryant G. Ly 					      const char *page, size_t count)
16009a8bb606SBryant G. Ly {
16019a8bb606SBryant G. Ly 	struct se_dev_attrib *da = container_of(to_config_group(item),
16029a8bb606SBryant G. Ly 					struct se_dev_attrib, da_group);
16031068be7bSBryant G. Ly 	struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
16049a8bb606SBryant G. Ly 	int val;
16059a8bb606SBryant G. Ly 	int ret;
16069a8bb606SBryant G. Ly 
16079a8bb606SBryant G. Ly 	ret = kstrtouint(page, 0, &val);
16089a8bb606SBryant G. Ly 	if (ret < 0)
16099a8bb606SBryant G. Ly 		return ret;
16109a8bb606SBryant G. Ly 
16119a8bb606SBryant G. Ly 	da->emulate_write_cache = val;
16121068be7bSBryant G. Ly 
16131068be7bSBryant G. Ly 	/* Check if device has been configured before */
16141068be7bSBryant G. Ly 	if (tcmu_dev_configured(udev)) {
16151068be7bSBryant G. Ly 		ret = tcmu_netlink_event(TCMU_CMD_RECONFIG_DEVICE,
16161068be7bSBryant G. Ly 					 udev->uio_info.name,
16171068be7bSBryant G. Ly 					 udev->uio_info.uio_dev->minor);
16181068be7bSBryant G. Ly 		if (ret) {
16191068be7bSBryant G. Ly 			pr_err("Unable to reconfigure device\n");
16201068be7bSBryant G. Ly 			return ret;
16211068be7bSBryant G. Ly 		}
16221068be7bSBryant G. Ly 	}
16239a8bb606SBryant G. Ly 	return count;
16249a8bb606SBryant G. Ly }
16259a8bb606SBryant G. Ly CONFIGFS_ATTR(tcmu_, emulate_write_cache);
16269a8bb606SBryant G. Ly 
1627801fc54dSBryant G. Ly struct configfs_attribute *tcmu_attrib_attrs[] = {
1628801fc54dSBryant G. Ly 	&tcmu_attr_cmd_time_out,
1629801fc54dSBryant G. Ly 	&tcmu_attr_dev_size,
1630801fc54dSBryant G. Ly 	&tcmu_attr_emulate_write_cache,
1631801fc54dSBryant G. Ly 	NULL,
1632801fc54dSBryant G. Ly };
1633801fc54dSBryant G. Ly 
16347d7a7435SNicholas Bellinger static struct configfs_attribute **tcmu_attrs;
16357d7a7435SNicholas Bellinger 
16367d7a7435SNicholas Bellinger static struct target_backend_ops tcmu_ops = {
16377c9e7a6fSAndy Grover 	.name			= "user",
16387c9e7a6fSAndy Grover 	.owner			= THIS_MODULE,
1639a3541703SAndy Grover 	.transport_flags	= TRANSPORT_FLAG_PASSTHROUGH,
16407c9e7a6fSAndy Grover 	.attach_hba		= tcmu_attach_hba,
16417c9e7a6fSAndy Grover 	.detach_hba		= tcmu_detach_hba,
16427c9e7a6fSAndy Grover 	.alloc_device		= tcmu_alloc_device,
16437c9e7a6fSAndy Grover 	.configure_device	= tcmu_configure_device,
16447c9e7a6fSAndy Grover 	.free_device		= tcmu_free_device,
16457c9e7a6fSAndy Grover 	.parse_cdb		= tcmu_parse_cdb,
16467c9e7a6fSAndy Grover 	.set_configfs_dev_params = tcmu_set_configfs_dev_params,
16477c9e7a6fSAndy Grover 	.show_configfs_dev_params = tcmu_show_configfs_dev_params,
16487c9e7a6fSAndy Grover 	.get_device_type	= sbc_get_device_type,
16497c9e7a6fSAndy Grover 	.get_blocks		= tcmu_get_blocks,
16507d7a7435SNicholas Bellinger 	.tb_dev_attrib_attrs	= NULL,
16517c9e7a6fSAndy Grover };
16527c9e7a6fSAndy Grover 
1653b6df4b79SXiubo Li static int unmap_thread_fn(void *data)
1654b6df4b79SXiubo Li {
1655b6df4b79SXiubo Li 	struct tcmu_dev *udev;
1656b6df4b79SXiubo Li 	loff_t off;
1657b6df4b79SXiubo Li 	uint32_t start, end, block;
1658b6df4b79SXiubo Li 	struct page *page;
1659b6df4b79SXiubo Li 	int i;
1660b6df4b79SXiubo Li 
1661b6df4b79SXiubo Li 	while (1) {
1662b6df4b79SXiubo Li 		DEFINE_WAIT(__wait);
1663b6df4b79SXiubo Li 
1664b6df4b79SXiubo Li 		prepare_to_wait(&unmap_wait, &__wait, TASK_INTERRUPTIBLE);
1665b6df4b79SXiubo Li 		schedule();
1666b6df4b79SXiubo Li 		finish_wait(&unmap_wait, &__wait);
1667b6df4b79SXiubo Li 
1668d906d8afSMike Christie 		if (kthread_should_stop())
1669d906d8afSMike Christie 			break;
1670d906d8afSMike Christie 
1671b6df4b79SXiubo Li 		mutex_lock(&root_udev_mutex);
1672b6df4b79SXiubo Li 		list_for_each_entry(udev, &root_udev, node) {
1673b6df4b79SXiubo Li 			mutex_lock(&udev->cmdr_lock);
1674b6df4b79SXiubo Li 
1675b6df4b79SXiubo Li 			/* Try to complete the finished commands first */
1676b6df4b79SXiubo Li 			tcmu_handle_completions(udev);
1677b6df4b79SXiubo Li 
1678b6df4b79SXiubo Li 			/* Skip the udevs waiting the global pool or in idle */
1679b6df4b79SXiubo Li 			if (udev->waiting_global || !udev->dbi_thresh) {
1680b6df4b79SXiubo Li 				mutex_unlock(&udev->cmdr_lock);
1681b6df4b79SXiubo Li 				continue;
1682b6df4b79SXiubo Li 			}
1683b6df4b79SXiubo Li 
1684b6df4b79SXiubo Li 			end = udev->dbi_max + 1;
1685b6df4b79SXiubo Li 			block = find_last_bit(udev->data_bitmap, end);
1686b6df4b79SXiubo Li 			if (block == udev->dbi_max) {
1687b6df4b79SXiubo Li 				/*
1688b6df4b79SXiubo Li 				 * The last bit is dbi_max, so there is
1689b6df4b79SXiubo Li 				 * no need to shrink any blocks.
1690b6df4b79SXiubo Li 				 */
1691b6df4b79SXiubo Li 				mutex_unlock(&udev->cmdr_lock);
1692b6df4b79SXiubo Li 				continue;
1693b6df4b79SXiubo Li 			} else if (block == end) {
1694b6df4b79SXiubo Li 				/* The current udev will goto idle state */
1695b6df4b79SXiubo Li 				udev->dbi_thresh = start = 0;
1696b6df4b79SXiubo Li 				udev->dbi_max = 0;
1697b6df4b79SXiubo Li 			} else {
1698b6df4b79SXiubo Li 				udev->dbi_thresh = start = block + 1;
1699b6df4b79SXiubo Li 				udev->dbi_max = block;
1700b6df4b79SXiubo Li 			}
1701b6df4b79SXiubo Li 
1702b6df4b79SXiubo Li 			/* Here will truncate the data area from off */
1703b6df4b79SXiubo Li 			off = udev->data_off + start * DATA_BLOCK_SIZE;
1704b6df4b79SXiubo Li 			unmap_mapping_range(udev->inode->i_mapping, off, 0, 1);
1705b6df4b79SXiubo Li 
1706b6df4b79SXiubo Li 			/* Release the block pages */
1707b6df4b79SXiubo Li 			for (i = start; i < end; i++) {
1708b6df4b79SXiubo Li 				page = radix_tree_delete(&udev->data_blocks, i);
1709b6df4b79SXiubo Li 				if (page) {
1710b6df4b79SXiubo Li 					__free_page(page);
1711b6df4b79SXiubo Li 					atomic_dec(&global_db_count);
1712b6df4b79SXiubo Li 				}
1713b6df4b79SXiubo Li 			}
1714b6df4b79SXiubo Li 			mutex_unlock(&udev->cmdr_lock);
1715b6df4b79SXiubo Li 		}
1716b6df4b79SXiubo Li 
1717b6df4b79SXiubo Li 		/*
1718b6df4b79SXiubo Li 		 * Try to wake up the udevs who are waiting
1719b6df4b79SXiubo Li 		 * for the global data pool.
1720b6df4b79SXiubo Li 		 */
1721b6df4b79SXiubo Li 		list_for_each_entry(udev, &root_udev, node) {
1722b6df4b79SXiubo Li 			if (udev->waiting_global)
1723b6df4b79SXiubo Li 				wake_up(&udev->wait_cmdr);
1724b6df4b79SXiubo Li 		}
1725b6df4b79SXiubo Li 		mutex_unlock(&root_udev_mutex);
1726b6df4b79SXiubo Li 	}
1727b6df4b79SXiubo Li 
1728b6df4b79SXiubo Li 	return 0;
1729b6df4b79SXiubo Li }
1730b6df4b79SXiubo Li 
17317c9e7a6fSAndy Grover static int __init tcmu_module_init(void)
17327c9e7a6fSAndy Grover {
1733801fc54dSBryant G. Ly 	int ret, i, k, len = 0;
17347c9e7a6fSAndy Grover 
17357c9e7a6fSAndy Grover 	BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0);
17367c9e7a6fSAndy Grover 
17377c9e7a6fSAndy Grover 	tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache",
17387c9e7a6fSAndy Grover 				sizeof(struct tcmu_cmd),
17397c9e7a6fSAndy Grover 				__alignof__(struct tcmu_cmd),
17407c9e7a6fSAndy Grover 				0, NULL);
17417c9e7a6fSAndy Grover 	if (!tcmu_cmd_cache)
17427c9e7a6fSAndy Grover 		return -ENOMEM;
17437c9e7a6fSAndy Grover 
17447c9e7a6fSAndy Grover 	tcmu_root_device = root_device_register("tcm_user");
17457c9e7a6fSAndy Grover 	if (IS_ERR(tcmu_root_device)) {
17467c9e7a6fSAndy Grover 		ret = PTR_ERR(tcmu_root_device);
17477c9e7a6fSAndy Grover 		goto out_free_cache;
17487c9e7a6fSAndy Grover 	}
17497c9e7a6fSAndy Grover 
17507c9e7a6fSAndy Grover 	ret = genl_register_family(&tcmu_genl_family);
17517c9e7a6fSAndy Grover 	if (ret < 0) {
17527c9e7a6fSAndy Grover 		goto out_unreg_device;
17537c9e7a6fSAndy Grover 	}
17547c9e7a6fSAndy Grover 
17557d7a7435SNicholas Bellinger 	for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) {
17567d7a7435SNicholas Bellinger 		len += sizeof(struct configfs_attribute *);
17577d7a7435SNicholas Bellinger 	}
1758801fc54dSBryant G. Ly 	for (i = 0; tcmu_attrib_attrs[i] != NULL; i++) {
1759801fc54dSBryant G. Ly 		len += sizeof(struct configfs_attribute *);
1760801fc54dSBryant G. Ly 	}
1761801fc54dSBryant G. Ly 	len += sizeof(struct configfs_attribute *);
17627d7a7435SNicholas Bellinger 
17637d7a7435SNicholas Bellinger 	tcmu_attrs = kzalloc(len, GFP_KERNEL);
17647d7a7435SNicholas Bellinger 	if (!tcmu_attrs) {
17657d7a7435SNicholas Bellinger 		ret = -ENOMEM;
17667d7a7435SNicholas Bellinger 		goto out_unreg_genl;
17677d7a7435SNicholas Bellinger 	}
17687d7a7435SNicholas Bellinger 
17697d7a7435SNicholas Bellinger 	for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) {
17707d7a7435SNicholas Bellinger 		tcmu_attrs[i] = passthrough_attrib_attrs[i];
17717d7a7435SNicholas Bellinger 	}
1772801fc54dSBryant G. Ly 	for (k = 0; tcmu_attrib_attrs[k] != NULL; k++) {
1773801fc54dSBryant G. Ly 		tcmu_attrs[i] = tcmu_attrib_attrs[k];
17749a8bb606SBryant G. Ly 		i++;
1775801fc54dSBryant G. Ly 	}
17767d7a7435SNicholas Bellinger 	tcmu_ops.tb_dev_attrib_attrs = tcmu_attrs;
17777d7a7435SNicholas Bellinger 
17780a06d430SChristoph Hellwig 	ret = transport_backend_register(&tcmu_ops);
17797c9e7a6fSAndy Grover 	if (ret)
17807d7a7435SNicholas Bellinger 		goto out_attrs;
17817c9e7a6fSAndy Grover 
1782b6df4b79SXiubo Li 	init_waitqueue_head(&unmap_wait);
1783b6df4b79SXiubo Li 	unmap_thread = kthread_run(unmap_thread_fn, NULL, "tcmu_unmap");
1784b6df4b79SXiubo Li 	if (IS_ERR(unmap_thread)) {
1785b6df4b79SXiubo Li 		ret = PTR_ERR(unmap_thread);
1786b6df4b79SXiubo Li 		goto out_unreg_transport;
1787b6df4b79SXiubo Li 	}
1788b6df4b79SXiubo Li 
17897c9e7a6fSAndy Grover 	return 0;
17907c9e7a6fSAndy Grover 
1791b6df4b79SXiubo Li out_unreg_transport:
1792b6df4b79SXiubo Li 	target_backend_unregister(&tcmu_ops);
17937d7a7435SNicholas Bellinger out_attrs:
17947d7a7435SNicholas Bellinger 	kfree(tcmu_attrs);
17957c9e7a6fSAndy Grover out_unreg_genl:
17967c9e7a6fSAndy Grover 	genl_unregister_family(&tcmu_genl_family);
17977c9e7a6fSAndy Grover out_unreg_device:
17987c9e7a6fSAndy Grover 	root_device_unregister(tcmu_root_device);
17997c9e7a6fSAndy Grover out_free_cache:
18007c9e7a6fSAndy Grover 	kmem_cache_destroy(tcmu_cmd_cache);
18017c9e7a6fSAndy Grover 
18027c9e7a6fSAndy Grover 	return ret;
18037c9e7a6fSAndy Grover }
18047c9e7a6fSAndy Grover 
18057c9e7a6fSAndy Grover static void __exit tcmu_module_exit(void)
18067c9e7a6fSAndy Grover {
1807b6df4b79SXiubo Li 	kthread_stop(unmap_thread);
18080a06d430SChristoph Hellwig 	target_backend_unregister(&tcmu_ops);
18097d7a7435SNicholas Bellinger 	kfree(tcmu_attrs);
18107c9e7a6fSAndy Grover 	genl_unregister_family(&tcmu_genl_family);
18117c9e7a6fSAndy Grover 	root_device_unregister(tcmu_root_device);
18127c9e7a6fSAndy Grover 	kmem_cache_destroy(tcmu_cmd_cache);
18137c9e7a6fSAndy Grover }
18147c9e7a6fSAndy Grover 
18157c9e7a6fSAndy Grover MODULE_DESCRIPTION("TCM USER subsystem plugin");
18167c9e7a6fSAndy Grover MODULE_AUTHOR("Shaohua Li <shli@kernel.org>");
18177c9e7a6fSAndy Grover MODULE_AUTHOR("Andy Grover <agrover@redhat.com>");
18187c9e7a6fSAndy Grover MODULE_LICENSE("GPL");
18197c9e7a6fSAndy Grover 
18207c9e7a6fSAndy Grover module_init(tcmu_module_init);
18217c9e7a6fSAndy Grover module_exit(tcmu_module_exit);
1822