Linux 4.19.133
[linux/fpc-iii.git] / drivers / target / target_core_user.c
blob8da89925a874df9740fec483c2d491715ea558df
1 /*
2 * Copyright (C) 2013 Shaohua Li <shli@kernel.org>
3 * Copyright (C) 2014 Red Hat, Inc.
4 * Copyright (C) 2015 Arrikto, Inc.
5 * Copyright (C) 2017 Chinamobile, Inc.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <linux/spinlock.h>
22 #include <linux/module.h>
23 #include <linux/idr.h>
24 #include <linux/kernel.h>
25 #include <linux/timer.h>
26 #include <linux/parser.h>
27 #include <linux/vmalloc.h>
28 #include <linux/uio_driver.h>
29 #include <linux/radix-tree.h>
30 #include <linux/stringify.h>
31 #include <linux/bitops.h>
32 #include <linux/highmem.h>
33 #include <linux/configfs.h>
34 #include <linux/mutex.h>
35 #include <linux/workqueue.h>
36 #include <net/genetlink.h>
37 #include <scsi/scsi_common.h>
38 #include <scsi/scsi_proto.h>
39 #include <target/target_core_base.h>
40 #include <target/target_core_fabric.h>
41 #include <target/target_core_backend.h>
43 #include <linux/target_core_user.h>
45 /**
46 * DOC: Userspace I/O
47 * Userspace I/O
48 * -------------
50 * Define a shared-memory interface for LIO to pass SCSI commands and
51 * data to userspace for processing. This is to allow backends that
52 * are too complex for in-kernel support to be possible.
54 * It uses the UIO framework to do a lot of the device-creation and
55 * introspection work for us.
57 * See the .h file for how the ring is laid out. Note that while the
58 * command ring is defined, the particulars of the data area are
59 * not. Offset values in the command entry point to other locations
60 * internal to the mmap-ed area. There is separate space outside the
61 * command ring for data buffers. This leaves maximum flexibility for
62 * moving buffer allocations, or even page flipping or other
63 * allocation techniques, without altering the command ring layout.
65 * SECURITY:
66 * The user process must be assumed to be malicious. There's no way to
67 * prevent it breaking the command ring protocol if it wants, but in
68 * order to prevent other issues we must only ever read *data* from
69 * the shared memory area, not offsets or sizes. This applies to
70 * command ring entries as well as the mailbox. Extra code needed for
71 * this may have a 'UAM' comment.
74 #define TCMU_TIME_OUT (30 * MSEC_PER_SEC)
76 /* For cmd area, the size is fixed 8MB */
77 #define CMDR_SIZE (8 * 1024 * 1024)
80 * For data area, the block size is PAGE_SIZE and
81 * the total size is 256K * PAGE_SIZE.
83 #define DATA_BLOCK_SIZE PAGE_SIZE
84 #define DATA_BLOCK_SHIFT PAGE_SHIFT
85 #define DATA_BLOCK_BITS_DEF (256 * 1024)
87 #define TCMU_MBS_TO_BLOCKS(_mbs) (_mbs << (20 - DATA_BLOCK_SHIFT))
88 #define TCMU_BLOCKS_TO_MBS(_blocks) (_blocks >> (20 - DATA_BLOCK_SHIFT))
91 * Default number of global data blocks(512K * PAGE_SIZE)
92 * when the unmap thread will be started.
94 #define TCMU_GLOBAL_MAX_BLOCKS_DEF (512 * 1024)
96 static u8 tcmu_kern_cmd_reply_supported;
97 static u8 tcmu_netlink_blocked;
99 static struct device *tcmu_root_device;
101 struct tcmu_hba {
102 u32 host_id;
105 #define TCMU_CONFIG_LEN 256
107 static DEFINE_MUTEX(tcmu_nl_cmd_mutex);
108 static LIST_HEAD(tcmu_nl_cmd_list);
110 struct tcmu_dev;
112 struct tcmu_nl_cmd {
113 /* wake up thread waiting for reply */
114 struct completion complete;
115 struct list_head nl_list;
116 struct tcmu_dev *udev;
117 int cmd;
118 int status;
121 struct tcmu_dev {
122 struct list_head node;
123 struct kref kref;
125 struct se_device se_dev;
127 char *name;
128 struct se_hba *hba;
130 #define TCMU_DEV_BIT_OPEN 0
131 #define TCMU_DEV_BIT_BROKEN 1
132 #define TCMU_DEV_BIT_BLOCKED 2
133 unsigned long flags;
135 struct uio_info uio_info;
137 struct inode *inode;
139 struct tcmu_mailbox *mb_addr;
140 uint64_t dev_size;
141 u32 cmdr_size;
142 u32 cmdr_last_cleaned;
143 /* Offset of data area from start of mb */
144 /* Must add data_off and mb_addr to get the address */
145 size_t data_off;
146 size_t data_size;
147 uint32_t max_blocks;
148 size_t ring_size;
150 struct mutex cmdr_lock;
151 struct list_head qfull_queue;
153 uint32_t dbi_max;
154 uint32_t dbi_thresh;
155 unsigned long *data_bitmap;
156 struct radix_tree_root data_blocks;
158 struct idr commands;
160 struct timer_list cmd_timer;
161 unsigned int cmd_time_out;
162 struct list_head inflight_queue;
164 struct timer_list qfull_timer;
165 int qfull_time_out;
167 struct list_head timedout_entry;
169 struct tcmu_nl_cmd curr_nl_cmd;
171 char dev_config[TCMU_CONFIG_LEN];
173 int nl_reply_supported;
176 #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev)
178 #define CMDR_OFF sizeof(struct tcmu_mailbox)
180 struct tcmu_cmd {
181 struct se_cmd *se_cmd;
182 struct tcmu_dev *tcmu_dev;
183 struct list_head queue_entry;
185 uint16_t cmd_id;
187 /* Can't use se_cmd when cleaning up expired cmds, because if
188 cmd has been completed then accessing se_cmd is off limits */
189 uint32_t dbi_cnt;
190 uint32_t dbi_cur;
191 uint32_t *dbi;
193 unsigned long deadline;
195 #define TCMU_CMD_BIT_EXPIRED 0
196 #define TCMU_CMD_BIT_INFLIGHT 1
197 unsigned long flags;
200 * To avoid dead lock the mutex lock order should always be:
202 * mutex_lock(&root_udev_mutex);
203 * ...
204 * mutex_lock(&tcmu_dev->cmdr_lock);
205 * mutex_unlock(&tcmu_dev->cmdr_lock);
206 * ...
207 * mutex_unlock(&root_udev_mutex);
209 static DEFINE_MUTEX(root_udev_mutex);
210 static LIST_HEAD(root_udev);
212 static DEFINE_SPINLOCK(timed_out_udevs_lock);
213 static LIST_HEAD(timed_out_udevs);
215 static struct kmem_cache *tcmu_cmd_cache;
217 static atomic_t global_db_count = ATOMIC_INIT(0);
218 static struct delayed_work tcmu_unmap_work;
219 static int tcmu_global_max_blocks = TCMU_GLOBAL_MAX_BLOCKS_DEF;
221 static int tcmu_set_global_max_data_area(const char *str,
222 const struct kernel_param *kp)
224 int ret, max_area_mb;
226 ret = kstrtoint(str, 10, &max_area_mb);
227 if (ret)
228 return -EINVAL;
230 if (max_area_mb <= 0) {
231 pr_err("global_max_data_area must be larger than 0.\n");
232 return -EINVAL;
235 tcmu_global_max_blocks = TCMU_MBS_TO_BLOCKS(max_area_mb);
236 if (atomic_read(&global_db_count) > tcmu_global_max_blocks)
237 schedule_delayed_work(&tcmu_unmap_work, 0);
238 else
239 cancel_delayed_work_sync(&tcmu_unmap_work);
241 return 0;
244 static int tcmu_get_global_max_data_area(char *buffer,
245 const struct kernel_param *kp)
247 return sprintf(buffer, "%d", TCMU_BLOCKS_TO_MBS(tcmu_global_max_blocks));
250 static const struct kernel_param_ops tcmu_global_max_data_area_op = {
251 .set = tcmu_set_global_max_data_area,
252 .get = tcmu_get_global_max_data_area,
255 module_param_cb(global_max_data_area_mb, &tcmu_global_max_data_area_op, NULL,
256 S_IWUSR | S_IRUGO);
257 MODULE_PARM_DESC(global_max_data_area_mb,
258 "Max MBs allowed to be allocated to all the tcmu device's "
259 "data areas.");
261 static int tcmu_get_block_netlink(char *buffer,
262 const struct kernel_param *kp)
264 return sprintf(buffer, "%s\n", tcmu_netlink_blocked ?
265 "blocked" : "unblocked");
268 static int tcmu_set_block_netlink(const char *str,
269 const struct kernel_param *kp)
271 int ret;
272 u8 val;
274 ret = kstrtou8(str, 0, &val);
275 if (ret < 0)
276 return ret;
278 if (val > 1) {
279 pr_err("Invalid block netlink value %u\n", val);
280 return -EINVAL;
283 tcmu_netlink_blocked = val;
284 return 0;
287 static const struct kernel_param_ops tcmu_block_netlink_op = {
288 .set = tcmu_set_block_netlink,
289 .get = tcmu_get_block_netlink,
292 module_param_cb(block_netlink, &tcmu_block_netlink_op, NULL, S_IWUSR | S_IRUGO);
293 MODULE_PARM_DESC(block_netlink, "Block new netlink commands.");
295 static int tcmu_fail_netlink_cmd(struct tcmu_nl_cmd *nl_cmd)
297 struct tcmu_dev *udev = nl_cmd->udev;
299 if (!tcmu_netlink_blocked) {
300 pr_err("Could not reset device's netlink interface. Netlink is not blocked.\n");
301 return -EBUSY;
304 if (nl_cmd->cmd != TCMU_CMD_UNSPEC) {
305 pr_debug("Aborting nl cmd %d on %s\n", nl_cmd->cmd, udev->name);
306 nl_cmd->status = -EINTR;
307 list_del(&nl_cmd->nl_list);
308 complete(&nl_cmd->complete);
310 return 0;
313 static int tcmu_set_reset_netlink(const char *str,
314 const struct kernel_param *kp)
316 struct tcmu_nl_cmd *nl_cmd, *tmp_cmd;
317 int ret;
318 u8 val;
320 ret = kstrtou8(str, 0, &val);
321 if (ret < 0)
322 return ret;
324 if (val != 1) {
325 pr_err("Invalid reset netlink value %u\n", val);
326 return -EINVAL;
329 mutex_lock(&tcmu_nl_cmd_mutex);
330 list_for_each_entry_safe(nl_cmd, tmp_cmd, &tcmu_nl_cmd_list, nl_list) {
331 ret = tcmu_fail_netlink_cmd(nl_cmd);
332 if (ret)
333 break;
335 mutex_unlock(&tcmu_nl_cmd_mutex);
337 return ret;
340 static const struct kernel_param_ops tcmu_reset_netlink_op = {
341 .set = tcmu_set_reset_netlink,
344 module_param_cb(reset_netlink, &tcmu_reset_netlink_op, NULL, S_IWUSR);
345 MODULE_PARM_DESC(reset_netlink, "Reset netlink commands.");
347 /* multicast group */
348 enum tcmu_multicast_groups {
349 TCMU_MCGRP_CONFIG,
352 static const struct genl_multicast_group tcmu_mcgrps[] = {
353 [TCMU_MCGRP_CONFIG] = { .name = "config", },
356 static struct nla_policy tcmu_attr_policy[TCMU_ATTR_MAX+1] = {
357 [TCMU_ATTR_DEVICE] = { .type = NLA_STRING },
358 [TCMU_ATTR_MINOR] = { .type = NLA_U32 },
359 [TCMU_ATTR_CMD_STATUS] = { .type = NLA_S32 },
360 [TCMU_ATTR_DEVICE_ID] = { .type = NLA_U32 },
361 [TCMU_ATTR_SUPP_KERN_CMD_REPLY] = { .type = NLA_U8 },
364 static int tcmu_genl_cmd_done(struct genl_info *info, int completed_cmd)
366 struct tcmu_dev *udev = NULL;
367 struct tcmu_nl_cmd *nl_cmd;
368 int dev_id, rc, ret = 0;
370 if (!info->attrs[TCMU_ATTR_CMD_STATUS] ||
371 !info->attrs[TCMU_ATTR_DEVICE_ID]) {
372 printk(KERN_ERR "TCMU_ATTR_CMD_STATUS or TCMU_ATTR_DEVICE_ID not set, doing nothing\n");
373 return -EINVAL;
376 dev_id = nla_get_u32(info->attrs[TCMU_ATTR_DEVICE_ID]);
377 rc = nla_get_s32(info->attrs[TCMU_ATTR_CMD_STATUS]);
379 mutex_lock(&tcmu_nl_cmd_mutex);
380 list_for_each_entry(nl_cmd, &tcmu_nl_cmd_list, nl_list) {
381 if (nl_cmd->udev->se_dev.dev_index == dev_id) {
382 udev = nl_cmd->udev;
383 break;
387 if (!udev) {
388 pr_err("tcmu nl cmd %u/%d completion could not find device with dev id %u.\n",
389 completed_cmd, rc, dev_id);
390 ret = -ENODEV;
391 goto unlock;
393 list_del(&nl_cmd->nl_list);
395 pr_debug("%s genl cmd done got id %d curr %d done %d rc %d stat %d\n",
396 udev->name, dev_id, nl_cmd->cmd, completed_cmd, rc,
397 nl_cmd->status);
399 if (nl_cmd->cmd != completed_cmd) {
400 pr_err("Mismatched commands on %s (Expecting reply for %d. Current %d).\n",
401 udev->name, completed_cmd, nl_cmd->cmd);
402 ret = -EINVAL;
403 goto unlock;
406 nl_cmd->status = rc;
407 complete(&nl_cmd->complete);
408 unlock:
409 mutex_unlock(&tcmu_nl_cmd_mutex);
410 return ret;
413 static int tcmu_genl_rm_dev_done(struct sk_buff *skb, struct genl_info *info)
415 return tcmu_genl_cmd_done(info, TCMU_CMD_REMOVED_DEVICE);
418 static int tcmu_genl_add_dev_done(struct sk_buff *skb, struct genl_info *info)
420 return tcmu_genl_cmd_done(info, TCMU_CMD_ADDED_DEVICE);
423 static int tcmu_genl_reconfig_dev_done(struct sk_buff *skb,
424 struct genl_info *info)
426 return tcmu_genl_cmd_done(info, TCMU_CMD_RECONFIG_DEVICE);
429 static int tcmu_genl_set_features(struct sk_buff *skb, struct genl_info *info)
431 if (info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]) {
432 tcmu_kern_cmd_reply_supported =
433 nla_get_u8(info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]);
434 printk(KERN_INFO "tcmu daemon: command reply support %u.\n",
435 tcmu_kern_cmd_reply_supported);
438 return 0;
441 static const struct genl_ops tcmu_genl_ops[] = {
443 .cmd = TCMU_CMD_SET_FEATURES,
444 .flags = GENL_ADMIN_PERM,
445 .policy = tcmu_attr_policy,
446 .doit = tcmu_genl_set_features,
449 .cmd = TCMU_CMD_ADDED_DEVICE_DONE,
450 .flags = GENL_ADMIN_PERM,
451 .policy = tcmu_attr_policy,
452 .doit = tcmu_genl_add_dev_done,
455 .cmd = TCMU_CMD_REMOVED_DEVICE_DONE,
456 .flags = GENL_ADMIN_PERM,
457 .policy = tcmu_attr_policy,
458 .doit = tcmu_genl_rm_dev_done,
461 .cmd = TCMU_CMD_RECONFIG_DEVICE_DONE,
462 .flags = GENL_ADMIN_PERM,
463 .policy = tcmu_attr_policy,
464 .doit = tcmu_genl_reconfig_dev_done,
468 /* Our generic netlink family */
469 static struct genl_family tcmu_genl_family __ro_after_init = {
470 .module = THIS_MODULE,
471 .hdrsize = 0,
472 .name = "TCM-USER",
473 .version = 2,
474 .maxattr = TCMU_ATTR_MAX,
475 .mcgrps = tcmu_mcgrps,
476 .n_mcgrps = ARRAY_SIZE(tcmu_mcgrps),
477 .netnsok = true,
478 .ops = tcmu_genl_ops,
479 .n_ops = ARRAY_SIZE(tcmu_genl_ops),
482 #define tcmu_cmd_set_dbi_cur(cmd, index) ((cmd)->dbi_cur = (index))
483 #define tcmu_cmd_reset_dbi_cur(cmd) tcmu_cmd_set_dbi_cur(cmd, 0)
484 #define tcmu_cmd_set_dbi(cmd, index) ((cmd)->dbi[(cmd)->dbi_cur++] = (index))
485 #define tcmu_cmd_get_dbi(cmd) ((cmd)->dbi[(cmd)->dbi_cur++])
487 static void tcmu_cmd_free_data(struct tcmu_cmd *tcmu_cmd, uint32_t len)
489 struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
490 uint32_t i;
492 for (i = 0; i < len; i++)
493 clear_bit(tcmu_cmd->dbi[i], udev->data_bitmap);
496 static inline bool tcmu_get_empty_block(struct tcmu_dev *udev,
497 struct tcmu_cmd *tcmu_cmd)
499 struct page *page;
500 int ret, dbi;
502 dbi = find_first_zero_bit(udev->data_bitmap, udev->dbi_thresh);
503 if (dbi == udev->dbi_thresh)
504 return false;
506 page = radix_tree_lookup(&udev->data_blocks, dbi);
507 if (!page) {
508 if (atomic_add_return(1, &global_db_count) >
509 tcmu_global_max_blocks)
510 schedule_delayed_work(&tcmu_unmap_work, 0);
512 /* try to get new page from the mm */
513 page = alloc_page(GFP_KERNEL);
514 if (!page)
515 goto err_alloc;
517 ret = radix_tree_insert(&udev->data_blocks, dbi, page);
518 if (ret)
519 goto err_insert;
522 if (dbi > udev->dbi_max)
523 udev->dbi_max = dbi;
525 set_bit(dbi, udev->data_bitmap);
526 tcmu_cmd_set_dbi(tcmu_cmd, dbi);
528 return true;
529 err_insert:
530 __free_page(page);
531 err_alloc:
532 atomic_dec(&global_db_count);
533 return false;
536 static bool tcmu_get_empty_blocks(struct tcmu_dev *udev,
537 struct tcmu_cmd *tcmu_cmd)
539 int i;
541 for (i = tcmu_cmd->dbi_cur; i < tcmu_cmd->dbi_cnt; i++) {
542 if (!tcmu_get_empty_block(udev, tcmu_cmd))
543 return false;
545 return true;
548 static inline struct page *
549 tcmu_get_block_page(struct tcmu_dev *udev, uint32_t dbi)
551 return radix_tree_lookup(&udev->data_blocks, dbi);
554 static inline void tcmu_free_cmd(struct tcmu_cmd *tcmu_cmd)
556 kfree(tcmu_cmd->dbi);
557 kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
560 static inline size_t tcmu_cmd_get_data_length(struct tcmu_cmd *tcmu_cmd)
562 struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
563 size_t data_length = round_up(se_cmd->data_length, DATA_BLOCK_SIZE);
565 if (se_cmd->se_cmd_flags & SCF_BIDI) {
566 BUG_ON(!(se_cmd->t_bidi_data_sg && se_cmd->t_bidi_data_nents));
567 data_length += round_up(se_cmd->t_bidi_data_sg->length,
568 DATA_BLOCK_SIZE);
571 return data_length;
574 static inline uint32_t tcmu_cmd_get_block_cnt(struct tcmu_cmd *tcmu_cmd)
576 size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd);
578 return data_length / DATA_BLOCK_SIZE;
581 static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd)
583 struct se_device *se_dev = se_cmd->se_dev;
584 struct tcmu_dev *udev = TCMU_DEV(se_dev);
585 struct tcmu_cmd *tcmu_cmd;
587 tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_KERNEL);
588 if (!tcmu_cmd)
589 return NULL;
591 INIT_LIST_HEAD(&tcmu_cmd->queue_entry);
592 tcmu_cmd->se_cmd = se_cmd;
593 tcmu_cmd->tcmu_dev = udev;
595 tcmu_cmd_reset_dbi_cur(tcmu_cmd);
596 tcmu_cmd->dbi_cnt = tcmu_cmd_get_block_cnt(tcmu_cmd);
597 tcmu_cmd->dbi = kcalloc(tcmu_cmd->dbi_cnt, sizeof(uint32_t),
598 GFP_KERNEL);
599 if (!tcmu_cmd->dbi) {
600 kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
601 return NULL;
604 return tcmu_cmd;
607 static inline void tcmu_flush_dcache_range(void *vaddr, size_t size)
609 unsigned long offset = offset_in_page(vaddr);
610 void *start = vaddr - offset;
612 size = round_up(size+offset, PAGE_SIZE);
614 while (size) {
615 flush_dcache_page(virt_to_page(start));
616 start += PAGE_SIZE;
617 size -= PAGE_SIZE;
622 * Some ring helper functions. We don't assume size is a power of 2 so
623 * we can't use circ_buf.h.
625 static inline size_t spc_used(size_t head, size_t tail, size_t size)
627 int diff = head - tail;
629 if (diff >= 0)
630 return diff;
631 else
632 return size + diff;
635 static inline size_t spc_free(size_t head, size_t tail, size_t size)
637 /* Keep 1 byte unused or we can't tell full from empty */
638 return (size - spc_used(head, tail, size) - 1);
641 static inline size_t head_to_end(size_t head, size_t size)
643 return size - head;
646 static inline void new_iov(struct iovec **iov, int *iov_cnt)
648 struct iovec *iovec;
650 if (*iov_cnt != 0)
651 (*iov)++;
652 (*iov_cnt)++;
654 iovec = *iov;
655 memset(iovec, 0, sizeof(struct iovec));
658 #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size)
660 /* offset is relative to mb_addr */
661 static inline size_t get_block_offset_user(struct tcmu_dev *dev,
662 int dbi, int remaining)
664 return dev->data_off + dbi * DATA_BLOCK_SIZE +
665 DATA_BLOCK_SIZE - remaining;
668 static inline size_t iov_tail(struct iovec *iov)
670 return (size_t)iov->iov_base + iov->iov_len;
673 static void scatter_data_area(struct tcmu_dev *udev,
674 struct tcmu_cmd *tcmu_cmd, struct scatterlist *data_sg,
675 unsigned int data_nents, struct iovec **iov,
676 int *iov_cnt, bool copy_data)
678 int i, dbi;
679 int block_remaining = 0;
680 void *from, *to = NULL;
681 size_t copy_bytes, to_offset, offset;
682 struct scatterlist *sg;
683 struct page *page;
685 for_each_sg(data_sg, sg, data_nents, i) {
686 int sg_remaining = sg->length;
687 from = kmap_atomic(sg_page(sg)) + sg->offset;
688 while (sg_remaining > 0) {
689 if (block_remaining == 0) {
690 if (to)
691 kunmap_atomic(to);
693 block_remaining = DATA_BLOCK_SIZE;
694 dbi = tcmu_cmd_get_dbi(tcmu_cmd);
695 page = tcmu_get_block_page(udev, dbi);
696 to = kmap_atomic(page);
700 * Covert to virtual offset of the ring data area.
702 to_offset = get_block_offset_user(udev, dbi,
703 block_remaining);
706 * The following code will gather and map the blocks
707 * to the same iovec when the blocks are all next to
708 * each other.
710 copy_bytes = min_t(size_t, sg_remaining,
711 block_remaining);
712 if (*iov_cnt != 0 &&
713 to_offset == iov_tail(*iov)) {
715 * Will append to the current iovec, because
716 * the current block page is next to the
717 * previous one.
719 (*iov)->iov_len += copy_bytes;
720 } else {
722 * Will allocate a new iovec because we are
723 * first time here or the current block page
724 * is not next to the previous one.
726 new_iov(iov, iov_cnt);
727 (*iov)->iov_base = (void __user *)to_offset;
728 (*iov)->iov_len = copy_bytes;
731 if (copy_data) {
732 offset = DATA_BLOCK_SIZE - block_remaining;
733 memcpy(to + offset,
734 from + sg->length - sg_remaining,
735 copy_bytes);
736 tcmu_flush_dcache_range(to, copy_bytes);
739 sg_remaining -= copy_bytes;
740 block_remaining -= copy_bytes;
742 kunmap_atomic(from - sg->offset);
745 if (to)
746 kunmap_atomic(to);
749 static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
750 bool bidi, uint32_t read_len)
752 struct se_cmd *se_cmd = cmd->se_cmd;
753 int i, dbi;
754 int block_remaining = 0;
755 void *from = NULL, *to;
756 size_t copy_bytes, offset;
757 struct scatterlist *sg, *data_sg;
758 struct page *page;
759 unsigned int data_nents;
760 uint32_t count = 0;
762 if (!bidi) {
763 data_sg = se_cmd->t_data_sg;
764 data_nents = se_cmd->t_data_nents;
765 } else {
768 * For bidi case, the first count blocks are for Data-Out
769 * buffer blocks, and before gathering the Data-In buffer
770 * the Data-Out buffer blocks should be discarded.
772 count = DIV_ROUND_UP(se_cmd->data_length, DATA_BLOCK_SIZE);
774 data_sg = se_cmd->t_bidi_data_sg;
775 data_nents = se_cmd->t_bidi_data_nents;
778 tcmu_cmd_set_dbi_cur(cmd, count);
780 for_each_sg(data_sg, sg, data_nents, i) {
781 int sg_remaining = sg->length;
782 to = kmap_atomic(sg_page(sg)) + sg->offset;
783 while (sg_remaining > 0 && read_len > 0) {
784 if (block_remaining == 0) {
785 if (from)
786 kunmap_atomic(from);
788 block_remaining = DATA_BLOCK_SIZE;
789 dbi = tcmu_cmd_get_dbi(cmd);
790 page = tcmu_get_block_page(udev, dbi);
791 from = kmap_atomic(page);
793 copy_bytes = min_t(size_t, sg_remaining,
794 block_remaining);
795 if (read_len < copy_bytes)
796 copy_bytes = read_len;
797 offset = DATA_BLOCK_SIZE - block_remaining;
798 tcmu_flush_dcache_range(from, copy_bytes);
799 memcpy(to + sg->length - sg_remaining, from + offset,
800 copy_bytes);
802 sg_remaining -= copy_bytes;
803 block_remaining -= copy_bytes;
804 read_len -= copy_bytes;
806 kunmap_atomic(to - sg->offset);
807 if (read_len == 0)
808 break;
810 if (from)
811 kunmap_atomic(from);
814 static inline size_t spc_bitmap_free(unsigned long *bitmap, uint32_t thresh)
816 return thresh - bitmap_weight(bitmap, thresh);
820 * We can't queue a command until we have space available on the cmd ring *and*
821 * space available on the data area.
823 * Called with ring lock held.
825 static bool is_ring_space_avail(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
826 size_t cmd_size, size_t data_needed)
828 struct tcmu_mailbox *mb = udev->mb_addr;
829 uint32_t blocks_needed = (data_needed + DATA_BLOCK_SIZE - 1)
830 / DATA_BLOCK_SIZE;
831 size_t space, cmd_needed;
832 u32 cmd_head;
834 tcmu_flush_dcache_range(mb, sizeof(*mb));
836 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
839 * If cmd end-of-ring space is too small then we need space for a NOP plus
840 * original cmd - cmds are internally contiguous.
842 if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size)
843 cmd_needed = cmd_size;
844 else
845 cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size);
847 space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size);
848 if (space < cmd_needed) {
849 pr_debug("no cmd space: %u %u %u\n", cmd_head,
850 udev->cmdr_last_cleaned, udev->cmdr_size);
851 return false;
854 /* try to check and get the data blocks as needed */
855 space = spc_bitmap_free(udev->data_bitmap, udev->dbi_thresh);
856 if ((space * DATA_BLOCK_SIZE) < data_needed) {
857 unsigned long blocks_left =
858 (udev->max_blocks - udev->dbi_thresh) + space;
860 if (blocks_left < blocks_needed) {
861 pr_debug("no data space: only %lu available, but ask for %zu\n",
862 blocks_left * DATA_BLOCK_SIZE,
863 data_needed);
864 return false;
867 udev->dbi_thresh += blocks_needed;
868 if (udev->dbi_thresh > udev->max_blocks)
869 udev->dbi_thresh = udev->max_blocks;
872 return tcmu_get_empty_blocks(udev, cmd);
875 static inline size_t tcmu_cmd_get_base_cmd_size(size_t iov_cnt)
877 return max(offsetof(struct tcmu_cmd_entry, req.iov[iov_cnt]),
878 sizeof(struct tcmu_cmd_entry));
881 static inline size_t tcmu_cmd_get_cmd_size(struct tcmu_cmd *tcmu_cmd,
882 size_t base_command_size)
884 struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
885 size_t command_size;
887 command_size = base_command_size +
888 round_up(scsi_command_size(se_cmd->t_task_cdb),
889 TCMU_OP_ALIGN_SIZE);
891 WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1));
893 return command_size;
896 static void tcmu_setup_cmd_timer(struct tcmu_cmd *tcmu_cmd, unsigned int tmo,
897 struct timer_list *timer)
899 if (!tmo)
900 return;
902 tcmu_cmd->deadline = round_jiffies_up(jiffies + msecs_to_jiffies(tmo));
903 if (!timer_pending(timer))
904 mod_timer(timer, tcmu_cmd->deadline);
906 pr_debug("Timeout set up for cmd %p, dev = %s, tmo = %lu\n", tcmu_cmd,
907 tcmu_cmd->tcmu_dev->name, tmo / MSEC_PER_SEC);
910 static int add_to_qfull_queue(struct tcmu_cmd *tcmu_cmd)
912 struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
913 unsigned int tmo;
916 * For backwards compat if qfull_time_out is not set use
917 * cmd_time_out and if that's not set use the default time out.
919 if (!udev->qfull_time_out)
920 return -ETIMEDOUT;
921 else if (udev->qfull_time_out > 0)
922 tmo = udev->qfull_time_out;
923 else if (udev->cmd_time_out)
924 tmo = udev->cmd_time_out;
925 else
926 tmo = TCMU_TIME_OUT;
928 tcmu_setup_cmd_timer(tcmu_cmd, tmo, &udev->qfull_timer);
930 list_add_tail(&tcmu_cmd->queue_entry, &udev->qfull_queue);
931 pr_debug("adding cmd %p on dev %s to ring space wait queue\n",
932 tcmu_cmd, udev->name);
933 return 0;
937 * queue_cmd_ring - queue cmd to ring or internally
938 * @tcmu_cmd: cmd to queue
939 * @scsi_err: TCM error code if failure (-1) returned.
941 * Returns:
942 * -1 we cannot queue internally or to the ring.
943 * 0 success
944 * 1 internally queued to wait for ring memory to free.
946 static int queue_cmd_ring(struct tcmu_cmd *tcmu_cmd, sense_reason_t *scsi_err)
948 struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
949 struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
950 size_t base_command_size, command_size;
951 struct tcmu_mailbox *mb;
952 struct tcmu_cmd_entry *entry;
953 struct iovec *iov;
954 int iov_cnt, cmd_id;
955 uint32_t cmd_head;
956 uint64_t cdb_off;
957 bool copy_to_data_area;
958 size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd);
960 *scsi_err = TCM_NO_SENSE;
962 if (test_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags)) {
963 *scsi_err = TCM_LUN_BUSY;
964 return -1;
967 if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) {
968 *scsi_err = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
969 return -1;
973 * Must be a certain minimum size for response sense info, but
974 * also may be larger if the iov array is large.
976 * We prepare as many iovs as possbile for potential uses here,
977 * because it's expensive to tell how many regions are freed in
978 * the bitmap & global data pool, as the size calculated here
979 * will only be used to do the checks.
981 * The size will be recalculated later as actually needed to save
982 * cmd area memories.
984 base_command_size = tcmu_cmd_get_base_cmd_size(tcmu_cmd->dbi_cnt);
985 command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size);
987 if (!list_empty(&udev->qfull_queue))
988 goto queue;
990 mb = udev->mb_addr;
991 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
992 if ((command_size > (udev->cmdr_size / 2)) ||
993 data_length > udev->data_size) {
994 pr_warn("TCMU: Request of size %zu/%zu is too big for %u/%zu "
995 "cmd ring/data area\n", command_size, data_length,
996 udev->cmdr_size, udev->data_size);
997 *scsi_err = TCM_INVALID_CDB_FIELD;
998 return -1;
1001 if (!is_ring_space_avail(udev, tcmu_cmd, command_size, data_length)) {
1003 * Don't leave commands partially setup because the unmap
1004 * thread might need the blocks to make forward progress.
1006 tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cur);
1007 tcmu_cmd_reset_dbi_cur(tcmu_cmd);
1008 goto queue;
1011 /* Insert a PAD if end-of-ring space is too small */
1012 if (head_to_end(cmd_head, udev->cmdr_size) < command_size) {
1013 size_t pad_size = head_to_end(cmd_head, udev->cmdr_size);
1015 entry = (void *) mb + CMDR_OFF + cmd_head;
1016 tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_PAD);
1017 tcmu_hdr_set_len(&entry->hdr.len_op, pad_size);
1018 entry->hdr.cmd_id = 0; /* not used for PAD */
1019 entry->hdr.kflags = 0;
1020 entry->hdr.uflags = 0;
1021 tcmu_flush_dcache_range(entry, sizeof(*entry));
1023 UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size);
1024 tcmu_flush_dcache_range(mb, sizeof(*mb));
1026 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
1027 WARN_ON(cmd_head != 0);
1030 entry = (void *) mb + CMDR_OFF + cmd_head;
1031 memset(entry, 0, command_size);
1032 tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD);
1034 /* Handle allocating space from the data area */
1035 tcmu_cmd_reset_dbi_cur(tcmu_cmd);
1036 iov = &entry->req.iov[0];
1037 iov_cnt = 0;
1038 copy_to_data_area = (se_cmd->data_direction == DMA_TO_DEVICE
1039 || se_cmd->se_cmd_flags & SCF_BIDI);
1040 scatter_data_area(udev, tcmu_cmd, se_cmd->t_data_sg,
1041 se_cmd->t_data_nents, &iov, &iov_cnt,
1042 copy_to_data_area);
1043 entry->req.iov_cnt = iov_cnt;
1045 /* Handle BIDI commands */
1046 iov_cnt = 0;
1047 if (se_cmd->se_cmd_flags & SCF_BIDI) {
1048 iov++;
1049 scatter_data_area(udev, tcmu_cmd, se_cmd->t_bidi_data_sg,
1050 se_cmd->t_bidi_data_nents, &iov, &iov_cnt,
1051 false);
1053 entry->req.iov_bidi_cnt = iov_cnt;
1055 cmd_id = idr_alloc(&udev->commands, tcmu_cmd, 1, USHRT_MAX, GFP_NOWAIT);
1056 if (cmd_id < 0) {
1057 pr_err("tcmu: Could not allocate cmd id.\n");
1059 tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt);
1060 *scsi_err = TCM_OUT_OF_RESOURCES;
1061 return -1;
1063 tcmu_cmd->cmd_id = cmd_id;
1065 pr_debug("allocated cmd id %u for cmd %p dev %s\n", tcmu_cmd->cmd_id,
1066 tcmu_cmd, udev->name);
1068 tcmu_setup_cmd_timer(tcmu_cmd, udev->cmd_time_out, &udev->cmd_timer);
1070 entry->hdr.cmd_id = tcmu_cmd->cmd_id;
1073 * Recalaulate the command's base size and size according
1074 * to the actual needs
1076 base_command_size = tcmu_cmd_get_base_cmd_size(entry->req.iov_cnt +
1077 entry->req.iov_bidi_cnt);
1078 command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size);
1080 tcmu_hdr_set_len(&entry->hdr.len_op, command_size);
1082 /* All offsets relative to mb_addr, not start of entry! */
1083 cdb_off = CMDR_OFF + cmd_head + base_command_size;
1084 memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb));
1085 entry->req.cdb_off = cdb_off;
1086 tcmu_flush_dcache_range(entry, sizeof(*entry));
1088 UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size);
1089 tcmu_flush_dcache_range(mb, sizeof(*mb));
1091 list_add_tail(&tcmu_cmd->queue_entry, &udev->inflight_queue);
1092 set_bit(TCMU_CMD_BIT_INFLIGHT, &tcmu_cmd->flags);
1094 /* TODO: only if FLUSH and FUA? */
1095 uio_event_notify(&udev->uio_info);
1097 return 0;
1099 queue:
1100 if (add_to_qfull_queue(tcmu_cmd)) {
1101 *scsi_err = TCM_OUT_OF_RESOURCES;
1102 return -1;
1105 return 1;
1108 static sense_reason_t
1109 tcmu_queue_cmd(struct se_cmd *se_cmd)
1111 struct se_device *se_dev = se_cmd->se_dev;
1112 struct tcmu_dev *udev = TCMU_DEV(se_dev);
1113 struct tcmu_cmd *tcmu_cmd;
1114 sense_reason_t scsi_ret;
1115 int ret;
1117 tcmu_cmd = tcmu_alloc_cmd(se_cmd);
1118 if (!tcmu_cmd)
1119 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1121 mutex_lock(&udev->cmdr_lock);
1122 ret = queue_cmd_ring(tcmu_cmd, &scsi_ret);
1123 mutex_unlock(&udev->cmdr_lock);
1124 if (ret < 0)
1125 tcmu_free_cmd(tcmu_cmd);
1126 return scsi_ret;
1129 static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *entry)
1131 struct se_cmd *se_cmd = cmd->se_cmd;
1132 struct tcmu_dev *udev = cmd->tcmu_dev;
1133 bool read_len_valid = false;
1134 uint32_t read_len;
1137 * cmd has been completed already from timeout, just reclaim
1138 * data area space and free cmd
1140 if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
1141 WARN_ON_ONCE(se_cmd);
1142 goto out;
1145 list_del_init(&cmd->queue_entry);
1147 tcmu_cmd_reset_dbi_cur(cmd);
1149 if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) {
1150 pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n",
1151 cmd->se_cmd);
1152 entry->rsp.scsi_status = SAM_STAT_CHECK_CONDITION;
1153 goto done;
1156 read_len = se_cmd->data_length;
1157 if (se_cmd->data_direction == DMA_FROM_DEVICE &&
1158 (entry->hdr.uflags & TCMU_UFLAG_READ_LEN) && entry->rsp.read_len) {
1159 read_len_valid = true;
1160 if (entry->rsp.read_len < read_len)
1161 read_len = entry->rsp.read_len;
1164 if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) {
1165 transport_copy_sense_to_cmd(se_cmd, entry->rsp.sense_buffer);
1166 if (!read_len_valid )
1167 goto done;
1168 else
1169 se_cmd->se_cmd_flags |= SCF_TREAT_READ_AS_NORMAL;
1171 if (se_cmd->se_cmd_flags & SCF_BIDI) {
1172 /* Get Data-In buffer before clean up */
1173 gather_data_area(udev, cmd, true, read_len);
1174 } else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
1175 gather_data_area(udev, cmd, false, read_len);
1176 } else if (se_cmd->data_direction == DMA_TO_DEVICE) {
1177 /* TODO: */
1178 } else if (se_cmd->data_direction != DMA_NONE) {
1179 pr_warn("TCMU: data direction was %d!\n",
1180 se_cmd->data_direction);
1183 done:
1184 if (read_len_valid) {
1185 pr_debug("read_len = %d\n", read_len);
1186 target_complete_cmd_with_length(cmd->se_cmd,
1187 entry->rsp.scsi_status, read_len);
1188 } else
1189 target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status);
1191 out:
1192 cmd->se_cmd = NULL;
1193 tcmu_cmd_free_data(cmd, cmd->dbi_cnt);
1194 tcmu_free_cmd(cmd);
1197 static void tcmu_set_next_deadline(struct list_head *queue,
1198 struct timer_list *timer)
1200 struct tcmu_cmd *tcmu_cmd, *tmp_cmd;
1201 unsigned long deadline = 0;
1203 list_for_each_entry_safe(tcmu_cmd, tmp_cmd, queue, queue_entry) {
1204 if (!time_after(jiffies, tcmu_cmd->deadline)) {
1205 deadline = tcmu_cmd->deadline;
1206 break;
1210 if (deadline)
1211 mod_timer(timer, deadline);
1212 else
1213 del_timer(timer);
1216 static unsigned int tcmu_handle_completions(struct tcmu_dev *udev)
1218 struct tcmu_mailbox *mb;
1219 struct tcmu_cmd *cmd;
1220 int handled = 0;
1222 if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) {
1223 pr_err("ring broken, not handling completions\n");
1224 return 0;
1227 mb = udev->mb_addr;
1228 tcmu_flush_dcache_range(mb, sizeof(*mb));
1230 while (udev->cmdr_last_cleaned != READ_ONCE(mb->cmd_tail)) {
1232 struct tcmu_cmd_entry *entry = (void *) mb + CMDR_OFF + udev->cmdr_last_cleaned;
1234 tcmu_flush_dcache_range(entry, sizeof(*entry));
1236 if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD) {
1237 UPDATE_HEAD(udev->cmdr_last_cleaned,
1238 tcmu_hdr_get_len(entry->hdr.len_op),
1239 udev->cmdr_size);
1240 continue;
1242 WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD);
1244 cmd = idr_remove(&udev->commands, entry->hdr.cmd_id);
1245 if (!cmd) {
1246 pr_err("cmd_id %u not found, ring is broken\n",
1247 entry->hdr.cmd_id);
1248 set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags);
1249 break;
1252 tcmu_handle_completion(cmd, entry);
1254 UPDATE_HEAD(udev->cmdr_last_cleaned,
1255 tcmu_hdr_get_len(entry->hdr.len_op),
1256 udev->cmdr_size);
1258 handled++;
1261 if (mb->cmd_tail == mb->cmd_head) {
1262 /* no more pending commands */
1263 del_timer(&udev->cmd_timer);
1265 if (list_empty(&udev->qfull_queue)) {
1267 * no more pending or waiting commands so try to
1268 * reclaim blocks if needed.
1270 if (atomic_read(&global_db_count) >
1271 tcmu_global_max_blocks)
1272 schedule_delayed_work(&tcmu_unmap_work, 0);
1274 } else if (udev->cmd_time_out) {
1275 tcmu_set_next_deadline(&udev->inflight_queue, &udev->cmd_timer);
1278 return handled;
1281 static void tcmu_check_expired_ring_cmd(struct tcmu_cmd *cmd)
1283 struct se_cmd *se_cmd;
1285 if (!time_after(jiffies, cmd->deadline))
1286 return;
1288 set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags);
1289 list_del_init(&cmd->queue_entry);
1290 se_cmd = cmd->se_cmd;
1291 cmd->se_cmd = NULL;
1293 pr_debug("Timing out inflight cmd %u on dev %s.\n",
1294 cmd->cmd_id, cmd->tcmu_dev->name);
1296 target_complete_cmd(se_cmd, SAM_STAT_CHECK_CONDITION);
1299 static void tcmu_check_expired_queue_cmd(struct tcmu_cmd *cmd)
1301 struct se_cmd *se_cmd;
1303 if (!time_after(jiffies, cmd->deadline))
1304 return;
1306 pr_debug("Timing out queued cmd %p on dev %s.\n",
1307 cmd, cmd->tcmu_dev->name);
1309 list_del_init(&cmd->queue_entry);
1310 se_cmd = cmd->se_cmd;
1311 tcmu_free_cmd(cmd);
1313 target_complete_cmd(se_cmd, SAM_STAT_TASK_SET_FULL);
1316 static void tcmu_device_timedout(struct tcmu_dev *udev)
1318 spin_lock(&timed_out_udevs_lock);
1319 if (list_empty(&udev->timedout_entry))
1320 list_add_tail(&udev->timedout_entry, &timed_out_udevs);
1321 spin_unlock(&timed_out_udevs_lock);
1323 schedule_delayed_work(&tcmu_unmap_work, 0);
1326 static void tcmu_cmd_timedout(struct timer_list *t)
1328 struct tcmu_dev *udev = from_timer(udev, t, cmd_timer);
1330 pr_debug("%s cmd timeout has expired\n", udev->name);
1331 tcmu_device_timedout(udev);
1334 static void tcmu_qfull_timedout(struct timer_list *t)
1336 struct tcmu_dev *udev = from_timer(udev, t, qfull_timer);
1338 pr_debug("%s qfull timeout has expired\n", udev->name);
1339 tcmu_device_timedout(udev);
1342 static int tcmu_attach_hba(struct se_hba *hba, u32 host_id)
1344 struct tcmu_hba *tcmu_hba;
1346 tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL);
1347 if (!tcmu_hba)
1348 return -ENOMEM;
1350 tcmu_hba->host_id = host_id;
1351 hba->hba_ptr = tcmu_hba;
1353 return 0;
1356 static void tcmu_detach_hba(struct se_hba *hba)
1358 kfree(hba->hba_ptr);
1359 hba->hba_ptr = NULL;
1362 static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
1364 struct tcmu_dev *udev;
1366 udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL);
1367 if (!udev)
1368 return NULL;
1369 kref_init(&udev->kref);
1371 udev->name = kstrdup(name, GFP_KERNEL);
1372 if (!udev->name) {
1373 kfree(udev);
1374 return NULL;
1377 udev->hba = hba;
1378 udev->cmd_time_out = TCMU_TIME_OUT;
1379 udev->qfull_time_out = -1;
1381 udev->max_blocks = DATA_BLOCK_BITS_DEF;
1382 mutex_init(&udev->cmdr_lock);
1384 INIT_LIST_HEAD(&udev->node);
1385 INIT_LIST_HEAD(&udev->timedout_entry);
1386 INIT_LIST_HEAD(&udev->qfull_queue);
1387 INIT_LIST_HEAD(&udev->inflight_queue);
1388 idr_init(&udev->commands);
1390 timer_setup(&udev->qfull_timer, tcmu_qfull_timedout, 0);
1391 timer_setup(&udev->cmd_timer, tcmu_cmd_timedout, 0);
1393 INIT_RADIX_TREE(&udev->data_blocks, GFP_KERNEL);
1395 return &udev->se_dev;
1398 static void run_qfull_queue(struct tcmu_dev *udev, bool fail)
1400 struct tcmu_cmd *tcmu_cmd, *tmp_cmd;
1401 LIST_HEAD(cmds);
1402 sense_reason_t scsi_ret;
1403 int ret;
1405 if (list_empty(&udev->qfull_queue))
1406 return;
1408 pr_debug("running %s's cmdr queue forcefail %d\n", udev->name, fail);
1410 list_splice_init(&udev->qfull_queue, &cmds);
1412 list_for_each_entry_safe(tcmu_cmd, tmp_cmd, &cmds, queue_entry) {
1413 list_del_init(&tcmu_cmd->queue_entry);
1415 pr_debug("removing cmd %p on dev %s from queue\n",
1416 tcmu_cmd, udev->name);
1418 if (fail) {
1420 * We were not able to even start the command, so
1421 * fail with busy to allow a retry in case runner
1422 * was only temporarily down. If the device is being
1423 * removed then LIO core will do the right thing and
1424 * fail the retry.
1426 target_complete_cmd(tcmu_cmd->se_cmd, SAM_STAT_BUSY);
1427 tcmu_free_cmd(tcmu_cmd);
1428 continue;
1431 ret = queue_cmd_ring(tcmu_cmd, &scsi_ret);
1432 if (ret < 0) {
1433 pr_debug("cmd %p on dev %s failed with %u\n",
1434 tcmu_cmd, udev->name, scsi_ret);
1436 * Ignore scsi_ret for now. target_complete_cmd
1437 * drops it.
1439 target_complete_cmd(tcmu_cmd->se_cmd,
1440 SAM_STAT_CHECK_CONDITION);
1441 tcmu_free_cmd(tcmu_cmd);
1442 } else if (ret > 0) {
1443 pr_debug("ran out of space during cmdr queue run\n");
1445 * cmd was requeued, so just put all cmds back in
1446 * the queue
1448 list_splice_tail(&cmds, &udev->qfull_queue);
1449 break;
1453 tcmu_set_next_deadline(&udev->qfull_queue, &udev->qfull_timer);
1456 static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on)
1458 struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
1460 mutex_lock(&udev->cmdr_lock);
1461 tcmu_handle_completions(udev);
1462 run_qfull_queue(udev, false);
1463 mutex_unlock(&udev->cmdr_lock);
1465 return 0;
1469 * mmap code from uio.c. Copied here because we want to hook mmap()
1470 * and this stuff must come along.
1472 static int tcmu_find_mem_index(struct vm_area_struct *vma)
1474 struct tcmu_dev *udev = vma->vm_private_data;
1475 struct uio_info *info = &udev->uio_info;
1477 if (vma->vm_pgoff < MAX_UIO_MAPS) {
1478 if (info->mem[vma->vm_pgoff].size == 0)
1479 return -1;
1480 return (int)vma->vm_pgoff;
1482 return -1;
1485 static struct page *tcmu_try_get_block_page(struct tcmu_dev *udev, uint32_t dbi)
1487 struct page *page;
1489 mutex_lock(&udev->cmdr_lock);
1490 page = tcmu_get_block_page(udev, dbi);
1491 if (likely(page)) {
1492 mutex_unlock(&udev->cmdr_lock);
1493 return page;
1497 * Userspace messed up and passed in a address not in the
1498 * data iov passed to it.
1500 pr_err("Invalid addr to data block mapping (dbi %u) on device %s\n",
1501 dbi, udev->name);
1502 page = NULL;
1503 mutex_unlock(&udev->cmdr_lock);
1505 return page;
1508 static vm_fault_t tcmu_vma_fault(struct vm_fault *vmf)
1510 struct tcmu_dev *udev = vmf->vma->vm_private_data;
1511 struct uio_info *info = &udev->uio_info;
1512 struct page *page;
1513 unsigned long offset;
1514 void *addr;
1516 int mi = tcmu_find_mem_index(vmf->vma);
1517 if (mi < 0)
1518 return VM_FAULT_SIGBUS;
1521 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
1522 * to use mem[N].
1524 offset = (vmf->pgoff - mi) << PAGE_SHIFT;
1526 if (offset < udev->data_off) {
1527 /* For the vmalloc()ed cmd area pages */
1528 addr = (void *)(unsigned long)info->mem[mi].addr + offset;
1529 page = vmalloc_to_page(addr);
1530 } else {
1531 uint32_t dbi;
1533 /* For the dynamically growing data area pages */
1534 dbi = (offset - udev->data_off) / DATA_BLOCK_SIZE;
1535 page = tcmu_try_get_block_page(udev, dbi);
1536 if (!page)
1537 return VM_FAULT_SIGBUS;
1540 get_page(page);
1541 vmf->page = page;
1542 return 0;
1545 static const struct vm_operations_struct tcmu_vm_ops = {
1546 .fault = tcmu_vma_fault,
1549 static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma)
1551 struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
1553 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
1554 vma->vm_ops = &tcmu_vm_ops;
1556 vma->vm_private_data = udev;
1558 /* Ensure the mmap is exactly the right size */
1559 if (vma_pages(vma) != (udev->ring_size >> PAGE_SHIFT))
1560 return -EINVAL;
1562 return 0;
1565 static int tcmu_open(struct uio_info *info, struct inode *inode)
1567 struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
1569 /* O_EXCL not supported for char devs, so fake it? */
1570 if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags))
1571 return -EBUSY;
1573 udev->inode = inode;
1574 kref_get(&udev->kref);
1576 pr_debug("open\n");
1578 return 0;
1581 static void tcmu_dev_call_rcu(struct rcu_head *p)
1583 struct se_device *dev = container_of(p, struct se_device, rcu_head);
1584 struct tcmu_dev *udev = TCMU_DEV(dev);
1586 kfree(udev->uio_info.name);
1587 kfree(udev->name);
1588 kfree(udev);
1591 static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd)
1593 if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
1594 kmem_cache_free(tcmu_cmd_cache, cmd);
1595 return 0;
1597 return -EINVAL;
1600 static void tcmu_blocks_release(struct radix_tree_root *blocks,
1601 int start, int end)
1603 int i;
1604 struct page *page;
1606 for (i = start; i < end; i++) {
1607 page = radix_tree_delete(blocks, i);
1608 if (page) {
1609 __free_page(page);
1610 atomic_dec(&global_db_count);
1615 static void tcmu_dev_kref_release(struct kref *kref)
1617 struct tcmu_dev *udev = container_of(kref, struct tcmu_dev, kref);
1618 struct se_device *dev = &udev->se_dev;
1619 struct tcmu_cmd *cmd;
1620 bool all_expired = true;
1621 int i;
1623 vfree(udev->mb_addr);
1624 udev->mb_addr = NULL;
1626 spin_lock_bh(&timed_out_udevs_lock);
1627 if (!list_empty(&udev->timedout_entry))
1628 list_del(&udev->timedout_entry);
1629 spin_unlock_bh(&timed_out_udevs_lock);
1631 /* Upper layer should drain all requests before calling this */
1632 mutex_lock(&udev->cmdr_lock);
1633 idr_for_each_entry(&udev->commands, cmd, i) {
1634 if (tcmu_check_and_free_pending_cmd(cmd) != 0)
1635 all_expired = false;
1637 if (!list_empty(&udev->qfull_queue))
1638 all_expired = false;
1639 idr_destroy(&udev->commands);
1640 WARN_ON(!all_expired);
1642 tcmu_blocks_release(&udev->data_blocks, 0, udev->dbi_max + 1);
1643 kfree(udev->data_bitmap);
1644 mutex_unlock(&udev->cmdr_lock);
1646 call_rcu(&dev->rcu_head, tcmu_dev_call_rcu);
1649 static int tcmu_release(struct uio_info *info, struct inode *inode)
1651 struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
1653 clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags);
1655 pr_debug("close\n");
1656 /* release ref from open */
1657 kref_put(&udev->kref, tcmu_dev_kref_release);
1658 return 0;
1661 static int tcmu_init_genl_cmd_reply(struct tcmu_dev *udev, int cmd)
1663 struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd;
1665 if (!tcmu_kern_cmd_reply_supported)
1666 return 0;
1668 if (udev->nl_reply_supported <= 0)
1669 return 0;
1671 mutex_lock(&tcmu_nl_cmd_mutex);
1673 if (tcmu_netlink_blocked) {
1674 mutex_unlock(&tcmu_nl_cmd_mutex);
1675 pr_warn("Failing nl cmd %d on %s. Interface is blocked.\n", cmd,
1676 udev->name);
1677 return -EAGAIN;
1680 if (nl_cmd->cmd != TCMU_CMD_UNSPEC) {
1681 mutex_unlock(&tcmu_nl_cmd_mutex);
1682 pr_warn("netlink cmd %d already executing on %s\n",
1683 nl_cmd->cmd, udev->name);
1684 return -EBUSY;
1687 memset(nl_cmd, 0, sizeof(*nl_cmd));
1688 nl_cmd->cmd = cmd;
1689 nl_cmd->udev = udev;
1690 init_completion(&nl_cmd->complete);
1691 INIT_LIST_HEAD(&nl_cmd->nl_list);
1693 list_add_tail(&nl_cmd->nl_list, &tcmu_nl_cmd_list);
1695 mutex_unlock(&tcmu_nl_cmd_mutex);
1696 return 0;
1699 static int tcmu_wait_genl_cmd_reply(struct tcmu_dev *udev)
1701 struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd;
1702 int ret;
1704 if (!tcmu_kern_cmd_reply_supported)
1705 return 0;
1707 if (udev->nl_reply_supported <= 0)
1708 return 0;
1710 pr_debug("sleeping for nl reply\n");
1711 wait_for_completion(&nl_cmd->complete);
1713 mutex_lock(&tcmu_nl_cmd_mutex);
1714 nl_cmd->cmd = TCMU_CMD_UNSPEC;
1715 ret = nl_cmd->status;
1716 mutex_unlock(&tcmu_nl_cmd_mutex);
1718 return ret;
1721 static int tcmu_netlink_event_init(struct tcmu_dev *udev,
1722 enum tcmu_genl_cmd cmd,
1723 struct sk_buff **buf, void **hdr)
1725 struct sk_buff *skb;
1726 void *msg_header;
1727 int ret = -ENOMEM;
1729 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1730 if (!skb)
1731 return ret;
1733 msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd);
1734 if (!msg_header)
1735 goto free_skb;
1737 ret = nla_put_string(skb, TCMU_ATTR_DEVICE, udev->uio_info.name);
1738 if (ret < 0)
1739 goto free_skb;
1741 ret = nla_put_u32(skb, TCMU_ATTR_MINOR, udev->uio_info.uio_dev->minor);
1742 if (ret < 0)
1743 goto free_skb;
1745 ret = nla_put_u32(skb, TCMU_ATTR_DEVICE_ID, udev->se_dev.dev_index);
1746 if (ret < 0)
1747 goto free_skb;
1749 *buf = skb;
1750 *hdr = msg_header;
1751 return ret;
1753 free_skb:
1754 nlmsg_free(skb);
1755 return ret;
1758 static int tcmu_netlink_event_send(struct tcmu_dev *udev,
1759 enum tcmu_genl_cmd cmd,
1760 struct sk_buff *skb, void *msg_header)
1762 int ret;
1764 genlmsg_end(skb, msg_header);
1766 ret = tcmu_init_genl_cmd_reply(udev, cmd);
1767 if (ret) {
1768 nlmsg_free(skb);
1769 return ret;
1772 ret = genlmsg_multicast_allns(&tcmu_genl_family, skb, 0,
1773 TCMU_MCGRP_CONFIG, GFP_KERNEL);
1774 /* We don't care if no one is listening */
1775 if (ret == -ESRCH)
1776 ret = 0;
1777 if (!ret)
1778 ret = tcmu_wait_genl_cmd_reply(udev);
1779 return ret;
1782 static int tcmu_send_dev_add_event(struct tcmu_dev *udev)
1784 struct sk_buff *skb = NULL;
1785 void *msg_header = NULL;
1786 int ret = 0;
1788 ret = tcmu_netlink_event_init(udev, TCMU_CMD_ADDED_DEVICE, &skb,
1789 &msg_header);
1790 if (ret < 0)
1791 return ret;
1792 return tcmu_netlink_event_send(udev, TCMU_CMD_ADDED_DEVICE, skb,
1793 msg_header);
1796 static int tcmu_send_dev_remove_event(struct tcmu_dev *udev)
1798 struct sk_buff *skb = NULL;
1799 void *msg_header = NULL;
1800 int ret = 0;
1802 ret = tcmu_netlink_event_init(udev, TCMU_CMD_REMOVED_DEVICE,
1803 &skb, &msg_header);
1804 if (ret < 0)
1805 return ret;
1806 return tcmu_netlink_event_send(udev, TCMU_CMD_REMOVED_DEVICE,
1807 skb, msg_header);
1810 static int tcmu_update_uio_info(struct tcmu_dev *udev)
1812 struct tcmu_hba *hba = udev->hba->hba_ptr;
1813 struct uio_info *info;
1814 size_t size, used;
1815 char *str;
1817 info = &udev->uio_info;
1818 size = snprintf(NULL, 0, "tcm-user/%u/%s/%s", hba->host_id, udev->name,
1819 udev->dev_config);
1820 size += 1; /* for \0 */
1821 str = kmalloc(size, GFP_KERNEL);
1822 if (!str)
1823 return -ENOMEM;
1825 used = snprintf(str, size, "tcm-user/%u/%s", hba->host_id, udev->name);
1826 if (udev->dev_config[0])
1827 snprintf(str + used, size - used, "/%s", udev->dev_config);
1829 /* If the old string exists, free it */
1830 kfree(info->name);
1831 info->name = str;
1833 return 0;
1836 static int tcmu_configure_device(struct se_device *dev)
1838 struct tcmu_dev *udev = TCMU_DEV(dev);
1839 struct uio_info *info;
1840 struct tcmu_mailbox *mb;
1841 int ret = 0;
1843 ret = tcmu_update_uio_info(udev);
1844 if (ret)
1845 return ret;
1847 info = &udev->uio_info;
1849 mutex_lock(&udev->cmdr_lock);
1850 udev->data_bitmap = kcalloc(BITS_TO_LONGS(udev->max_blocks),
1851 sizeof(unsigned long),
1852 GFP_KERNEL);
1853 mutex_unlock(&udev->cmdr_lock);
1854 if (!udev->data_bitmap) {
1855 ret = -ENOMEM;
1856 goto err_bitmap_alloc;
1859 udev->mb_addr = vzalloc(CMDR_SIZE);
1860 if (!udev->mb_addr) {
1861 ret = -ENOMEM;
1862 goto err_vzalloc;
1865 /* mailbox fits in first part of CMDR space */
1866 udev->cmdr_size = CMDR_SIZE - CMDR_OFF;
1867 udev->data_off = CMDR_SIZE;
1868 udev->data_size = udev->max_blocks * DATA_BLOCK_SIZE;
1869 udev->dbi_thresh = 0; /* Default in Idle state */
1871 /* Initialise the mailbox of the ring buffer */
1872 mb = udev->mb_addr;
1873 mb->version = TCMU_MAILBOX_VERSION;
1874 mb->flags = TCMU_MAILBOX_FLAG_CAP_OOOC | TCMU_MAILBOX_FLAG_CAP_READ_LEN;
1875 mb->cmdr_off = CMDR_OFF;
1876 mb->cmdr_size = udev->cmdr_size;
1878 WARN_ON(!PAGE_ALIGNED(udev->data_off));
1879 WARN_ON(udev->data_size % PAGE_SIZE);
1880 WARN_ON(udev->data_size % DATA_BLOCK_SIZE);
1882 info->version = __stringify(TCMU_MAILBOX_VERSION);
1884 info->mem[0].name = "tcm-user command & data buffer";
1885 info->mem[0].addr = (phys_addr_t)(uintptr_t)udev->mb_addr;
1886 info->mem[0].size = udev->ring_size = udev->data_size + CMDR_SIZE;
1887 info->mem[0].memtype = UIO_MEM_NONE;
1889 info->irqcontrol = tcmu_irqcontrol;
1890 info->irq = UIO_IRQ_CUSTOM;
1892 info->mmap = tcmu_mmap;
1893 info->open = tcmu_open;
1894 info->release = tcmu_release;
1896 ret = uio_register_device(tcmu_root_device, info);
1897 if (ret)
1898 goto err_register;
1900 /* User can set hw_block_size before enable the device */
1901 if (dev->dev_attrib.hw_block_size == 0)
1902 dev->dev_attrib.hw_block_size = 512;
1903 /* Other attributes can be configured in userspace */
1904 if (!dev->dev_attrib.hw_max_sectors)
1905 dev->dev_attrib.hw_max_sectors = 128;
1906 if (!dev->dev_attrib.emulate_write_cache)
1907 dev->dev_attrib.emulate_write_cache = 0;
1908 dev->dev_attrib.hw_queue_depth = 128;
1910 /* If user didn't explicitly disable netlink reply support, use
1911 * module scope setting.
1913 if (udev->nl_reply_supported >= 0)
1914 udev->nl_reply_supported = tcmu_kern_cmd_reply_supported;
1917 * Get a ref incase userspace does a close on the uio device before
1918 * LIO has initiated tcmu_free_device.
1920 kref_get(&udev->kref);
1922 ret = tcmu_send_dev_add_event(udev);
1923 if (ret)
1924 goto err_netlink;
1926 mutex_lock(&root_udev_mutex);
1927 list_add(&udev->node, &root_udev);
1928 mutex_unlock(&root_udev_mutex);
1930 return 0;
1932 err_netlink:
1933 kref_put(&udev->kref, tcmu_dev_kref_release);
1934 uio_unregister_device(&udev->uio_info);
1935 err_register:
1936 vfree(udev->mb_addr);
1937 udev->mb_addr = NULL;
1938 err_vzalloc:
1939 kfree(udev->data_bitmap);
1940 udev->data_bitmap = NULL;
1941 err_bitmap_alloc:
1942 kfree(info->name);
1943 info->name = NULL;
1945 return ret;
1948 static void tcmu_free_device(struct se_device *dev)
1950 struct tcmu_dev *udev = TCMU_DEV(dev);
1952 /* release ref from init */
1953 kref_put(&udev->kref, tcmu_dev_kref_release);
1956 static void tcmu_destroy_device(struct se_device *dev)
1958 struct tcmu_dev *udev = TCMU_DEV(dev);
1960 del_timer_sync(&udev->cmd_timer);
1961 del_timer_sync(&udev->qfull_timer);
1963 mutex_lock(&root_udev_mutex);
1964 list_del(&udev->node);
1965 mutex_unlock(&root_udev_mutex);
1967 tcmu_send_dev_remove_event(udev);
1969 uio_unregister_device(&udev->uio_info);
1971 /* release ref from configure */
1972 kref_put(&udev->kref, tcmu_dev_kref_release);
1975 static void tcmu_unblock_dev(struct tcmu_dev *udev)
1977 mutex_lock(&udev->cmdr_lock);
1978 clear_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags);
1979 mutex_unlock(&udev->cmdr_lock);
1982 static void tcmu_block_dev(struct tcmu_dev *udev)
1984 mutex_lock(&udev->cmdr_lock);
1986 if (test_and_set_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags))
1987 goto unlock;
1989 /* complete IO that has executed successfully */
1990 tcmu_handle_completions(udev);
1991 /* fail IO waiting to be queued */
1992 run_qfull_queue(udev, true);
1994 unlock:
1995 mutex_unlock(&udev->cmdr_lock);
1998 static void tcmu_reset_ring(struct tcmu_dev *udev, u8 err_level)
2000 struct tcmu_mailbox *mb;
2001 struct tcmu_cmd *cmd;
2002 int i;
2004 mutex_lock(&udev->cmdr_lock);
2006 idr_for_each_entry(&udev->commands, cmd, i) {
2007 pr_debug("removing cmd %u on dev %s from ring (is expired %d)\n",
2008 cmd->cmd_id, udev->name,
2009 test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags));
2011 idr_remove(&udev->commands, i);
2012 if (!test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
2013 WARN_ON(!cmd->se_cmd);
2014 list_del_init(&cmd->queue_entry);
2015 if (err_level == 1) {
2017 * Userspace was not able to start the
2018 * command or it is retryable.
2020 target_complete_cmd(cmd->se_cmd, SAM_STAT_BUSY);
2021 } else {
2022 /* hard failure */
2023 target_complete_cmd(cmd->se_cmd,
2024 SAM_STAT_CHECK_CONDITION);
2027 tcmu_cmd_free_data(cmd, cmd->dbi_cnt);
2028 tcmu_free_cmd(cmd);
2031 mb = udev->mb_addr;
2032 tcmu_flush_dcache_range(mb, sizeof(*mb));
2033 pr_debug("mb last %u head %u tail %u\n", udev->cmdr_last_cleaned,
2034 mb->cmd_tail, mb->cmd_head);
2036 udev->cmdr_last_cleaned = 0;
2037 mb->cmd_tail = 0;
2038 mb->cmd_head = 0;
2039 tcmu_flush_dcache_range(mb, sizeof(*mb));
2040 clear_bit(TCMU_DEV_BIT_BROKEN, &udev->flags);
2042 del_timer(&udev->cmd_timer);
2044 run_qfull_queue(udev, false);
2046 mutex_unlock(&udev->cmdr_lock);
2049 enum {
2050 Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_hw_max_sectors,
2051 Opt_nl_reply_supported, Opt_max_data_area_mb, Opt_err,
2054 static match_table_t tokens = {
2055 {Opt_dev_config, "dev_config=%s"},
2056 {Opt_dev_size, "dev_size=%s"},
2057 {Opt_hw_block_size, "hw_block_size=%d"},
2058 {Opt_hw_max_sectors, "hw_max_sectors=%d"},
2059 {Opt_nl_reply_supported, "nl_reply_supported=%d"},
2060 {Opt_max_data_area_mb, "max_data_area_mb=%d"},
2061 {Opt_err, NULL}
2064 static int tcmu_set_dev_attrib(substring_t *arg, u32 *dev_attrib)
2066 int val, ret;
2068 ret = match_int(arg, &val);
2069 if (ret < 0) {
2070 pr_err("match_int() failed for dev attrib. Error %d.\n",
2071 ret);
2072 return ret;
2075 if (val <= 0) {
2076 pr_err("Invalid dev attrib value %d. Must be greater than zero.\n",
2077 val);
2078 return -EINVAL;
2080 *dev_attrib = val;
2081 return 0;
2084 static int tcmu_set_max_blocks_param(struct tcmu_dev *udev, substring_t *arg)
2086 int val, ret;
2088 ret = match_int(arg, &val);
2089 if (ret < 0) {
2090 pr_err("match_int() failed for max_data_area_mb=. Error %d.\n",
2091 ret);
2092 return ret;
2095 if (val <= 0) {
2096 pr_err("Invalid max_data_area %d.\n", val);
2097 return -EINVAL;
2100 mutex_lock(&udev->cmdr_lock);
2101 if (udev->data_bitmap) {
2102 pr_err("Cannot set max_data_area_mb after it has been enabled.\n");
2103 ret = -EINVAL;
2104 goto unlock;
2107 udev->max_blocks = TCMU_MBS_TO_BLOCKS(val);
2108 if (udev->max_blocks > tcmu_global_max_blocks) {
2109 pr_err("%d is too large. Adjusting max_data_area_mb to global limit of %u\n",
2110 val, TCMU_BLOCKS_TO_MBS(tcmu_global_max_blocks));
2111 udev->max_blocks = tcmu_global_max_blocks;
2114 unlock:
2115 mutex_unlock(&udev->cmdr_lock);
2116 return ret;
2119 static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev,
2120 const char *page, ssize_t count)
2122 struct tcmu_dev *udev = TCMU_DEV(dev);
2123 char *orig, *ptr, *opts;
2124 substring_t args[MAX_OPT_ARGS];
2125 int ret = 0, token;
2127 opts = kstrdup(page, GFP_KERNEL);
2128 if (!opts)
2129 return -ENOMEM;
2131 orig = opts;
2133 while ((ptr = strsep(&opts, ",\n")) != NULL) {
2134 if (!*ptr)
2135 continue;
2137 token = match_token(ptr, tokens, args);
2138 switch (token) {
2139 case Opt_dev_config:
2140 if (match_strlcpy(udev->dev_config, &args[0],
2141 TCMU_CONFIG_LEN) == 0) {
2142 ret = -EINVAL;
2143 break;
2145 pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config);
2146 break;
2147 case Opt_dev_size:
2148 ret = match_u64(&args[0], &udev->dev_size);
2149 if (ret < 0)
2150 pr_err("match_u64() failed for dev_size=. Error %d.\n",
2151 ret);
2152 break;
2153 case Opt_hw_block_size:
2154 ret = tcmu_set_dev_attrib(&args[0],
2155 &(dev->dev_attrib.hw_block_size));
2156 break;
2157 case Opt_hw_max_sectors:
2158 ret = tcmu_set_dev_attrib(&args[0],
2159 &(dev->dev_attrib.hw_max_sectors));
2160 break;
2161 case Opt_nl_reply_supported:
2162 ret = match_int(&args[0], &udev->nl_reply_supported);
2163 if (ret < 0)
2164 pr_err("match_int() failed for nl_reply_supported=. Error %d.\n",
2165 ret);
2166 break;
2167 case Opt_max_data_area_mb:
2168 ret = tcmu_set_max_blocks_param(udev, &args[0]);
2169 break;
2170 default:
2171 break;
2174 if (ret)
2175 break;
2178 kfree(orig);
2179 return (!ret) ? count : ret;
2182 static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b)
2184 struct tcmu_dev *udev = TCMU_DEV(dev);
2185 ssize_t bl = 0;
2187 bl = sprintf(b + bl, "Config: %s ",
2188 udev->dev_config[0] ? udev->dev_config : "NULL");
2189 bl += sprintf(b + bl, "Size: %llu ", udev->dev_size);
2190 bl += sprintf(b + bl, "MaxDataAreaMB: %u\n",
2191 TCMU_BLOCKS_TO_MBS(udev->max_blocks));
2193 return bl;
2196 static sector_t tcmu_get_blocks(struct se_device *dev)
2198 struct tcmu_dev *udev = TCMU_DEV(dev);
2200 return div_u64(udev->dev_size - dev->dev_attrib.block_size,
2201 dev->dev_attrib.block_size);
2204 static sense_reason_t
2205 tcmu_parse_cdb(struct se_cmd *cmd)
2207 return passthrough_parse_cdb(cmd, tcmu_queue_cmd);
2210 static ssize_t tcmu_cmd_time_out_show(struct config_item *item, char *page)
2212 struct se_dev_attrib *da = container_of(to_config_group(item),
2213 struct se_dev_attrib, da_group);
2214 struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2216 return snprintf(page, PAGE_SIZE, "%lu\n", udev->cmd_time_out / MSEC_PER_SEC);
2219 static ssize_t tcmu_cmd_time_out_store(struct config_item *item, const char *page,
2220 size_t count)
2222 struct se_dev_attrib *da = container_of(to_config_group(item),
2223 struct se_dev_attrib, da_group);
2224 struct tcmu_dev *udev = container_of(da->da_dev,
2225 struct tcmu_dev, se_dev);
2226 u32 val;
2227 int ret;
2229 if (da->da_dev->export_count) {
2230 pr_err("Unable to set tcmu cmd_time_out while exports exist\n");
2231 return -EINVAL;
2234 ret = kstrtou32(page, 0, &val);
2235 if (ret < 0)
2236 return ret;
2238 udev->cmd_time_out = val * MSEC_PER_SEC;
2239 return count;
2241 CONFIGFS_ATTR(tcmu_, cmd_time_out);
2243 static ssize_t tcmu_qfull_time_out_show(struct config_item *item, char *page)
2245 struct se_dev_attrib *da = container_of(to_config_group(item),
2246 struct se_dev_attrib, da_group);
2247 struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2249 return snprintf(page, PAGE_SIZE, "%ld\n", udev->qfull_time_out <= 0 ?
2250 udev->qfull_time_out :
2251 udev->qfull_time_out / MSEC_PER_SEC);
2254 static ssize_t tcmu_qfull_time_out_store(struct config_item *item,
2255 const char *page, size_t count)
2257 struct se_dev_attrib *da = container_of(to_config_group(item),
2258 struct se_dev_attrib, da_group);
2259 struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2260 s32 val;
2261 int ret;
2263 ret = kstrtos32(page, 0, &val);
2264 if (ret < 0)
2265 return ret;
2267 if (val >= 0) {
2268 udev->qfull_time_out = val * MSEC_PER_SEC;
2269 } else if (val == -1) {
2270 udev->qfull_time_out = val;
2271 } else {
2272 printk(KERN_ERR "Invalid qfull timeout value %d\n", val);
2273 return -EINVAL;
2275 return count;
2277 CONFIGFS_ATTR(tcmu_, qfull_time_out);
2279 static ssize_t tcmu_max_data_area_mb_show(struct config_item *item, char *page)
2281 struct se_dev_attrib *da = container_of(to_config_group(item),
2282 struct se_dev_attrib, da_group);
2283 struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2285 return snprintf(page, PAGE_SIZE, "%u\n",
2286 TCMU_BLOCKS_TO_MBS(udev->max_blocks));
2288 CONFIGFS_ATTR_RO(tcmu_, max_data_area_mb);
2290 static ssize_t tcmu_dev_config_show(struct config_item *item, char *page)
2292 struct se_dev_attrib *da = container_of(to_config_group(item),
2293 struct se_dev_attrib, da_group);
2294 struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2296 return snprintf(page, PAGE_SIZE, "%s\n", udev->dev_config);
2299 static int tcmu_send_dev_config_event(struct tcmu_dev *udev,
2300 const char *reconfig_data)
2302 struct sk_buff *skb = NULL;
2303 void *msg_header = NULL;
2304 int ret = 0;
2306 ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE,
2307 &skb, &msg_header);
2308 if (ret < 0)
2309 return ret;
2310 ret = nla_put_string(skb, TCMU_ATTR_DEV_CFG, reconfig_data);
2311 if (ret < 0) {
2312 nlmsg_free(skb);
2313 return ret;
2315 return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE,
2316 skb, msg_header);
2320 static ssize_t tcmu_dev_config_store(struct config_item *item, const char *page,
2321 size_t count)
2323 struct se_dev_attrib *da = container_of(to_config_group(item),
2324 struct se_dev_attrib, da_group);
2325 struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2326 int ret, len;
2328 len = strlen(page);
2329 if (!len || len > TCMU_CONFIG_LEN - 1)
2330 return -EINVAL;
2332 /* Check if device has been configured before */
2333 if (target_dev_configured(&udev->se_dev)) {
2334 ret = tcmu_send_dev_config_event(udev, page);
2335 if (ret) {
2336 pr_err("Unable to reconfigure device\n");
2337 return ret;
2339 strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN);
2341 ret = tcmu_update_uio_info(udev);
2342 if (ret)
2343 return ret;
2344 return count;
2346 strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN);
2348 return count;
2350 CONFIGFS_ATTR(tcmu_, dev_config);
2352 static ssize_t tcmu_dev_size_show(struct config_item *item, char *page)
2354 struct se_dev_attrib *da = container_of(to_config_group(item),
2355 struct se_dev_attrib, da_group);
2356 struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2358 return snprintf(page, PAGE_SIZE, "%llu\n", udev->dev_size);
2361 static int tcmu_send_dev_size_event(struct tcmu_dev *udev, u64 size)
2363 struct sk_buff *skb = NULL;
2364 void *msg_header = NULL;
2365 int ret = 0;
2367 ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE,
2368 &skb, &msg_header);
2369 if (ret < 0)
2370 return ret;
2371 ret = nla_put_u64_64bit(skb, TCMU_ATTR_DEV_SIZE,
2372 size, TCMU_ATTR_PAD);
2373 if (ret < 0) {
2374 nlmsg_free(skb);
2375 return ret;
2377 return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE,
2378 skb, msg_header);
2381 static ssize_t tcmu_dev_size_store(struct config_item *item, const char *page,
2382 size_t count)
2384 struct se_dev_attrib *da = container_of(to_config_group(item),
2385 struct se_dev_attrib, da_group);
2386 struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2387 u64 val;
2388 int ret;
2390 ret = kstrtou64(page, 0, &val);
2391 if (ret < 0)
2392 return ret;
2394 /* Check if device has been configured before */
2395 if (target_dev_configured(&udev->se_dev)) {
2396 ret = tcmu_send_dev_size_event(udev, val);
2397 if (ret) {
2398 pr_err("Unable to reconfigure device\n");
2399 return ret;
2402 udev->dev_size = val;
2403 return count;
2405 CONFIGFS_ATTR(tcmu_, dev_size);
2407 static ssize_t tcmu_nl_reply_supported_show(struct config_item *item,
2408 char *page)
2410 struct se_dev_attrib *da = container_of(to_config_group(item),
2411 struct se_dev_attrib, da_group);
2412 struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2414 return snprintf(page, PAGE_SIZE, "%d\n", udev->nl_reply_supported);
2417 static ssize_t tcmu_nl_reply_supported_store(struct config_item *item,
2418 const char *page, size_t count)
2420 struct se_dev_attrib *da = container_of(to_config_group(item),
2421 struct se_dev_attrib, da_group);
2422 struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2423 s8 val;
2424 int ret;
2426 ret = kstrtos8(page, 0, &val);
2427 if (ret < 0)
2428 return ret;
2430 udev->nl_reply_supported = val;
2431 return count;
2433 CONFIGFS_ATTR(tcmu_, nl_reply_supported);
2435 static ssize_t tcmu_emulate_write_cache_show(struct config_item *item,
2436 char *page)
2438 struct se_dev_attrib *da = container_of(to_config_group(item),
2439 struct se_dev_attrib, da_group);
2441 return snprintf(page, PAGE_SIZE, "%i\n", da->emulate_write_cache);
2444 static int tcmu_send_emulate_write_cache(struct tcmu_dev *udev, u8 val)
2446 struct sk_buff *skb = NULL;
2447 void *msg_header = NULL;
2448 int ret = 0;
2450 ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE,
2451 &skb, &msg_header);
2452 if (ret < 0)
2453 return ret;
2454 ret = nla_put_u8(skb, TCMU_ATTR_WRITECACHE, val);
2455 if (ret < 0) {
2456 nlmsg_free(skb);
2457 return ret;
2459 return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE,
2460 skb, msg_header);
2463 static ssize_t tcmu_emulate_write_cache_store(struct config_item *item,
2464 const char *page, size_t count)
2466 struct se_dev_attrib *da = container_of(to_config_group(item),
2467 struct se_dev_attrib, da_group);
2468 struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2469 u8 val;
2470 int ret;
2472 ret = kstrtou8(page, 0, &val);
2473 if (ret < 0)
2474 return ret;
2476 /* Check if device has been configured before */
2477 if (target_dev_configured(&udev->se_dev)) {
2478 ret = tcmu_send_emulate_write_cache(udev, val);
2479 if (ret) {
2480 pr_err("Unable to reconfigure device\n");
2481 return ret;
2485 da->emulate_write_cache = val;
2486 return count;
2488 CONFIGFS_ATTR(tcmu_, emulate_write_cache);
2490 static ssize_t tcmu_block_dev_show(struct config_item *item, char *page)
2492 struct se_device *se_dev = container_of(to_config_group(item),
2493 struct se_device,
2494 dev_action_group);
2495 struct tcmu_dev *udev = TCMU_DEV(se_dev);
2497 if (test_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags))
2498 return snprintf(page, PAGE_SIZE, "%s\n", "blocked");
2499 else
2500 return snprintf(page, PAGE_SIZE, "%s\n", "unblocked");
2503 static ssize_t tcmu_block_dev_store(struct config_item *item, const char *page,
2504 size_t count)
2506 struct se_device *se_dev = container_of(to_config_group(item),
2507 struct se_device,
2508 dev_action_group);
2509 struct tcmu_dev *udev = TCMU_DEV(se_dev);
2510 u8 val;
2511 int ret;
2513 if (!target_dev_configured(&udev->se_dev)) {
2514 pr_err("Device is not configured.\n");
2515 return -EINVAL;
2518 ret = kstrtou8(page, 0, &val);
2519 if (ret < 0)
2520 return ret;
2522 if (val > 1) {
2523 pr_err("Invalid block value %d\n", val);
2524 return -EINVAL;
2527 if (!val)
2528 tcmu_unblock_dev(udev);
2529 else
2530 tcmu_block_dev(udev);
2531 return count;
2533 CONFIGFS_ATTR(tcmu_, block_dev);
2535 static ssize_t tcmu_reset_ring_store(struct config_item *item, const char *page,
2536 size_t count)
2538 struct se_device *se_dev = container_of(to_config_group(item),
2539 struct se_device,
2540 dev_action_group);
2541 struct tcmu_dev *udev = TCMU_DEV(se_dev);
2542 u8 val;
2543 int ret;
2545 if (!target_dev_configured(&udev->se_dev)) {
2546 pr_err("Device is not configured.\n");
2547 return -EINVAL;
2550 ret = kstrtou8(page, 0, &val);
2551 if (ret < 0)
2552 return ret;
2554 if (val != 1 && val != 2) {
2555 pr_err("Invalid reset ring value %d\n", val);
2556 return -EINVAL;
2559 tcmu_reset_ring(udev, val);
2560 return count;
2562 CONFIGFS_ATTR_WO(tcmu_, reset_ring);
2564 static struct configfs_attribute *tcmu_attrib_attrs[] = {
2565 &tcmu_attr_cmd_time_out,
2566 &tcmu_attr_qfull_time_out,
2567 &tcmu_attr_max_data_area_mb,
2568 &tcmu_attr_dev_config,
2569 &tcmu_attr_dev_size,
2570 &tcmu_attr_emulate_write_cache,
2571 &tcmu_attr_nl_reply_supported,
2572 NULL,
2575 static struct configfs_attribute **tcmu_attrs;
2577 static struct configfs_attribute *tcmu_action_attrs[] = {
2578 &tcmu_attr_block_dev,
2579 &tcmu_attr_reset_ring,
2580 NULL,
2583 static struct target_backend_ops tcmu_ops = {
2584 .name = "user",
2585 .owner = THIS_MODULE,
2586 .transport_flags = TRANSPORT_FLAG_PASSTHROUGH,
2587 .attach_hba = tcmu_attach_hba,
2588 .detach_hba = tcmu_detach_hba,
2589 .alloc_device = tcmu_alloc_device,
2590 .configure_device = tcmu_configure_device,
2591 .destroy_device = tcmu_destroy_device,
2592 .free_device = tcmu_free_device,
2593 .parse_cdb = tcmu_parse_cdb,
2594 .set_configfs_dev_params = tcmu_set_configfs_dev_params,
2595 .show_configfs_dev_params = tcmu_show_configfs_dev_params,
2596 .get_device_type = sbc_get_device_type,
2597 .get_blocks = tcmu_get_blocks,
2598 .tb_dev_action_attrs = tcmu_action_attrs,
2601 static void find_free_blocks(void)
2603 struct tcmu_dev *udev;
2604 loff_t off;
2605 u32 start, end, block, total_freed = 0;
2607 if (atomic_read(&global_db_count) <= tcmu_global_max_blocks)
2608 return;
2610 mutex_lock(&root_udev_mutex);
2611 list_for_each_entry(udev, &root_udev, node) {
2612 mutex_lock(&udev->cmdr_lock);
2614 if (!target_dev_configured(&udev->se_dev)) {
2615 mutex_unlock(&udev->cmdr_lock);
2616 continue;
2619 /* Try to complete the finished commands first */
2620 tcmu_handle_completions(udev);
2622 /* Skip the udevs in idle */
2623 if (!udev->dbi_thresh) {
2624 mutex_unlock(&udev->cmdr_lock);
2625 continue;
2628 end = udev->dbi_max + 1;
2629 block = find_last_bit(udev->data_bitmap, end);
2630 if (block == udev->dbi_max) {
2632 * The last bit is dbi_max, so it is not possible
2633 * reclaim any blocks.
2635 mutex_unlock(&udev->cmdr_lock);
2636 continue;
2637 } else if (block == end) {
2638 /* The current udev will goto idle state */
2639 udev->dbi_thresh = start = 0;
2640 udev->dbi_max = 0;
2641 } else {
2642 udev->dbi_thresh = start = block + 1;
2643 udev->dbi_max = block;
2646 /* Here will truncate the data area from off */
2647 off = udev->data_off + start * DATA_BLOCK_SIZE;
2648 unmap_mapping_range(udev->inode->i_mapping, off, 0, 1);
2650 /* Release the block pages */
2651 tcmu_blocks_release(&udev->data_blocks, start, end);
2652 mutex_unlock(&udev->cmdr_lock);
2654 total_freed += end - start;
2655 pr_debug("Freed %u blocks (total %u) from %s.\n", end - start,
2656 total_freed, udev->name);
2658 mutex_unlock(&root_udev_mutex);
2660 if (atomic_read(&global_db_count) > tcmu_global_max_blocks)
2661 schedule_delayed_work(&tcmu_unmap_work, msecs_to_jiffies(5000));
2664 static void check_timedout_devices(void)
2666 struct tcmu_dev *udev, *tmp_dev;
2667 struct tcmu_cmd *cmd, *tmp_cmd;
2668 LIST_HEAD(devs);
2670 spin_lock_bh(&timed_out_udevs_lock);
2671 list_splice_init(&timed_out_udevs, &devs);
2673 list_for_each_entry_safe(udev, tmp_dev, &devs, timedout_entry) {
2674 list_del_init(&udev->timedout_entry);
2675 spin_unlock_bh(&timed_out_udevs_lock);
2677 mutex_lock(&udev->cmdr_lock);
2680 * If cmd_time_out is disabled but qfull is set deadline
2681 * will only reflect the qfull timeout. Ignore it.
2683 if (udev->cmd_time_out) {
2684 list_for_each_entry_safe(cmd, tmp_cmd,
2685 &udev->inflight_queue,
2686 queue_entry) {
2687 tcmu_check_expired_ring_cmd(cmd);
2689 tcmu_set_next_deadline(&udev->inflight_queue,
2690 &udev->cmd_timer);
2692 list_for_each_entry_safe(cmd, tmp_cmd, &udev->qfull_queue,
2693 queue_entry) {
2694 tcmu_check_expired_queue_cmd(cmd);
2696 tcmu_set_next_deadline(&udev->qfull_queue, &udev->qfull_timer);
2698 mutex_unlock(&udev->cmdr_lock);
2700 spin_lock_bh(&timed_out_udevs_lock);
2703 spin_unlock_bh(&timed_out_udevs_lock);
2706 static void tcmu_unmap_work_fn(struct work_struct *work)
2708 check_timedout_devices();
2709 find_free_blocks();
2712 static int __init tcmu_module_init(void)
2714 int ret, i, k, len = 0;
2716 BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0);
2718 INIT_DELAYED_WORK(&tcmu_unmap_work, tcmu_unmap_work_fn);
2720 tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache",
2721 sizeof(struct tcmu_cmd),
2722 __alignof__(struct tcmu_cmd),
2723 0, NULL);
2724 if (!tcmu_cmd_cache)
2725 return -ENOMEM;
2727 tcmu_root_device = root_device_register("tcm_user");
2728 if (IS_ERR(tcmu_root_device)) {
2729 ret = PTR_ERR(tcmu_root_device);
2730 goto out_free_cache;
2733 ret = genl_register_family(&tcmu_genl_family);
2734 if (ret < 0) {
2735 goto out_unreg_device;
2738 for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) {
2739 len += sizeof(struct configfs_attribute *);
2741 for (i = 0; tcmu_attrib_attrs[i] != NULL; i++) {
2742 len += sizeof(struct configfs_attribute *);
2744 len += sizeof(struct configfs_attribute *);
2746 tcmu_attrs = kzalloc(len, GFP_KERNEL);
2747 if (!tcmu_attrs) {
2748 ret = -ENOMEM;
2749 goto out_unreg_genl;
2752 for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) {
2753 tcmu_attrs[i] = passthrough_attrib_attrs[i];
2755 for (k = 0; tcmu_attrib_attrs[k] != NULL; k++) {
2756 tcmu_attrs[i] = tcmu_attrib_attrs[k];
2757 i++;
2759 tcmu_ops.tb_dev_attrib_attrs = tcmu_attrs;
2761 ret = transport_backend_register(&tcmu_ops);
2762 if (ret)
2763 goto out_attrs;
2765 return 0;
2767 out_attrs:
2768 kfree(tcmu_attrs);
2769 out_unreg_genl:
2770 genl_unregister_family(&tcmu_genl_family);
2771 out_unreg_device:
2772 root_device_unregister(tcmu_root_device);
2773 out_free_cache:
2774 kmem_cache_destroy(tcmu_cmd_cache);
2776 return ret;
2779 static void __exit tcmu_module_exit(void)
2781 cancel_delayed_work_sync(&tcmu_unmap_work);
2782 target_backend_unregister(&tcmu_ops);
2783 kfree(tcmu_attrs);
2784 genl_unregister_family(&tcmu_genl_family);
2785 root_device_unregister(tcmu_root_device);
2786 kmem_cache_destroy(tcmu_cmd_cache);
2789 MODULE_DESCRIPTION("TCM USER subsystem plugin");
2790 MODULE_AUTHOR("Shaohua Li <shli@kernel.org>");
2791 MODULE_AUTHOR("Andy Grover <agrover@redhat.com>");
2792 MODULE_LICENSE("GPL");
2794 module_init(tcmu_module_init);
2795 module_exit(tcmu_module_exit);