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
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>
46 * Define a shared-memory interface for LIO to pass SCSI commands and
47 * data to userspace for processing. This is to allow backends that
48 * are too complex for in-kernel support to be possible.
50 * It uses the UIO framework to do a lot of the device-creation and
51 * introspection work for us.
53 * See the .h file for how the ring is laid out. Note that while the
54 * command ring is defined, the particulars of the data area are
55 * not. Offset values in the command entry point to other locations
56 * internal to the mmap()ed area. There is separate space outside the
57 * command ring for data buffers. This leaves maximum flexibility for
58 * moving buffer allocations, or even page flipping or other
59 * allocation techniques, without altering the command ring layout.
62 * The user process must be assumed to be malicious. There's no way to
63 * prevent it breaking the command ring protocol if it wants, but in
64 * order to prevent other issues we must only ever read *data* from
65 * the shared memory area, not offsets or sizes. This applies to
66 * command ring entries as well as the mailbox. Extra code needed for
67 * this may have a 'UAM' comment.
70 #define TCMU_TIME_OUT (30 * MSEC_PER_SEC)
72 /* For cmd area, the size is fixed 8MB */
73 #define CMDR_SIZE (8 * 1024 * 1024)
76 * For data area, the block size is PAGE_SIZE and
77 * the total size is 256K * PAGE_SIZE.
79 #define DATA_BLOCK_SIZE PAGE_SIZE
80 #define DATA_BLOCK_SHIFT PAGE_SHIFT
81 #define DATA_BLOCK_BITS_DEF (256 * 1024)
82 #define DATA_SIZE (DATA_BLOCK_BITS * DATA_BLOCK_SIZE)
84 #define TCMU_MBS_TO_BLOCKS(_mbs) (_mbs << (20 - DATA_BLOCK_SHIFT))
85 #define TCMU_BLOCKS_TO_MBS(_blocks) (_blocks >> (20 - DATA_BLOCK_SHIFT))
87 /* The total size of the ring is 8M + 256K * PAGE_SIZE */
88 #define TCMU_RING_SIZE (CMDR_SIZE + DATA_SIZE)
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;
98 static struct device *tcmu_root_device;
104 #define TCMU_CONFIG_LEN 256
107 /* wake up thread waiting for reply */
108 struct completion complete;
114 struct list_head node;
117 struct se_device se_dev;
122 #define TCMU_DEV_BIT_OPEN 0
123 #define TCMU_DEV_BIT_BROKEN 1
126 struct uio_info uio_info;
130 struct tcmu_mailbox *mb_addr;
133 u32 cmdr_last_cleaned;
134 /* Offset of data area from start of mb */
135 /* Must add data_off and mb_addr to get the address */
141 struct mutex cmdr_lock;
142 struct list_head cmdr_queue;
146 unsigned long *data_bitmap;
147 struct radix_tree_root data_blocks;
151 struct timer_list cmd_timer;
152 unsigned int cmd_time_out;
154 struct timer_list qfull_timer;
157 struct list_head timedout_entry;
159 spinlock_t nl_cmd_lock;
160 struct tcmu_nl_cmd curr_nl_cmd;
161 /* wake up threads waiting on curr_nl_cmd */
162 wait_queue_head_t nl_cmd_wq;
164 char dev_config[TCMU_CONFIG_LEN];
166 int nl_reply_supported;
169 #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev)
171 #define CMDR_OFF sizeof(struct tcmu_mailbox)
174 struct se_cmd *se_cmd;
175 struct tcmu_dev *tcmu_dev;
176 struct list_head cmdr_queue_entry;
180 /* Can't use se_cmd when cleaning up expired cmds, because if
181 cmd has been completed then accessing se_cmd is off limits */
186 unsigned long deadline;
188 #define TCMU_CMD_BIT_EXPIRED 0
192 * To avoid dead lock the mutex lock order should always be:
194 * mutex_lock(&root_udev_mutex);
196 * mutex_lock(&tcmu_dev->cmdr_lock);
197 * mutex_unlock(&tcmu_dev->cmdr_lock);
199 * mutex_unlock(&root_udev_mutex);
201 static DEFINE_MUTEX(root_udev_mutex);
202 static LIST_HEAD(root_udev);
204 static DEFINE_SPINLOCK(timed_out_udevs_lock);
205 static LIST_HEAD(timed_out_udevs);
207 static struct kmem_cache *tcmu_cmd_cache;
209 static atomic_t global_db_count = ATOMIC_INIT(0);
210 static struct delayed_work tcmu_unmap_work;
211 static int tcmu_global_max_blocks = TCMU_GLOBAL_MAX_BLOCKS_DEF;
213 static int tcmu_set_global_max_data_area(const char *str,
214 const struct kernel_param *kp)
216 int ret, max_area_mb;
218 ret = kstrtoint(str, 10, &max_area_mb);
222 if (max_area_mb <= 0) {
223 pr_err("global_max_data_area must be larger than 0.\n");
227 tcmu_global_max_blocks = TCMU_MBS_TO_BLOCKS(max_area_mb);
228 if (atomic_read(&global_db_count) > tcmu_global_max_blocks)
229 schedule_delayed_work(&tcmu_unmap_work, 0);
231 cancel_delayed_work_sync(&tcmu_unmap_work);
236 static int tcmu_get_global_max_data_area(char *buffer,
237 const struct kernel_param *kp)
239 return sprintf(buffer, "%d", TCMU_BLOCKS_TO_MBS(tcmu_global_max_blocks));
242 static const struct kernel_param_ops tcmu_global_max_data_area_op = {
243 .set = tcmu_set_global_max_data_area,
244 .get = tcmu_get_global_max_data_area,
247 module_param_cb(global_max_data_area_mb, &tcmu_global_max_data_area_op, NULL,
249 MODULE_PARM_DESC(global_max_data_area_mb,
250 "Max MBs allowed to be allocated to all the tcmu device's "
253 /* multicast group */
254 enum tcmu_multicast_groups {
258 static const struct genl_multicast_group tcmu_mcgrps[] = {
259 [TCMU_MCGRP_CONFIG] = { .name = "config", },
262 static struct nla_policy tcmu_attr_policy[TCMU_ATTR_MAX+1] = {
263 [TCMU_ATTR_DEVICE] = { .type = NLA_STRING },
264 [TCMU_ATTR_MINOR] = { .type = NLA_U32 },
265 [TCMU_ATTR_CMD_STATUS] = { .type = NLA_S32 },
266 [TCMU_ATTR_DEVICE_ID] = { .type = NLA_U32 },
267 [TCMU_ATTR_SUPP_KERN_CMD_REPLY] = { .type = NLA_U8 },
270 static int tcmu_genl_cmd_done(struct genl_info *info, int completed_cmd)
272 struct se_device *dev;
273 struct tcmu_dev *udev;
274 struct tcmu_nl_cmd *nl_cmd;
275 int dev_id, rc, ret = 0;
276 bool is_removed = (completed_cmd == TCMU_CMD_REMOVED_DEVICE);
278 if (!info->attrs[TCMU_ATTR_CMD_STATUS] ||
279 !info->attrs[TCMU_ATTR_DEVICE_ID]) {
280 printk(KERN_ERR "TCMU_ATTR_CMD_STATUS or TCMU_ATTR_DEVICE_ID not set, doing nothing\n");
284 dev_id = nla_get_u32(info->attrs[TCMU_ATTR_DEVICE_ID]);
285 rc = nla_get_s32(info->attrs[TCMU_ATTR_CMD_STATUS]);
287 dev = target_find_device(dev_id, !is_removed);
289 printk(KERN_ERR "tcmu nl cmd %u/%u completion could not find device with dev id %u.\n",
290 completed_cmd, rc, dev_id);
293 udev = TCMU_DEV(dev);
295 spin_lock(&udev->nl_cmd_lock);
296 nl_cmd = &udev->curr_nl_cmd;
298 pr_debug("genl cmd done got id %d curr %d done %d rc %d\n", dev_id,
299 nl_cmd->cmd, completed_cmd, rc);
301 if (nl_cmd->cmd != completed_cmd) {
302 printk(KERN_ERR "Mismatched commands (Expecting reply for %d. Current %d).\n",
303 completed_cmd, nl_cmd->cmd);
309 spin_unlock(&udev->nl_cmd_lock);
311 target_undepend_item(&dev->dev_group.cg_item);
313 complete(&nl_cmd->complete);
317 static int tcmu_genl_rm_dev_done(struct sk_buff *skb, struct genl_info *info)
319 return tcmu_genl_cmd_done(info, TCMU_CMD_REMOVED_DEVICE);
322 static int tcmu_genl_add_dev_done(struct sk_buff *skb, struct genl_info *info)
324 return tcmu_genl_cmd_done(info, TCMU_CMD_ADDED_DEVICE);
327 static int tcmu_genl_reconfig_dev_done(struct sk_buff *skb,
328 struct genl_info *info)
330 return tcmu_genl_cmd_done(info, TCMU_CMD_RECONFIG_DEVICE);
333 static int tcmu_genl_set_features(struct sk_buff *skb, struct genl_info *info)
335 if (info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]) {
336 tcmu_kern_cmd_reply_supported =
337 nla_get_u8(info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]);
338 printk(KERN_INFO "tcmu daemon: command reply support %u.\n",
339 tcmu_kern_cmd_reply_supported);
345 static const struct genl_ops tcmu_genl_ops[] = {
347 .cmd = TCMU_CMD_SET_FEATURES,
348 .flags = GENL_ADMIN_PERM,
349 .policy = tcmu_attr_policy,
350 .doit = tcmu_genl_set_features,
353 .cmd = TCMU_CMD_ADDED_DEVICE_DONE,
354 .flags = GENL_ADMIN_PERM,
355 .policy = tcmu_attr_policy,
356 .doit = tcmu_genl_add_dev_done,
359 .cmd = TCMU_CMD_REMOVED_DEVICE_DONE,
360 .flags = GENL_ADMIN_PERM,
361 .policy = tcmu_attr_policy,
362 .doit = tcmu_genl_rm_dev_done,
365 .cmd = TCMU_CMD_RECONFIG_DEVICE_DONE,
366 .flags = GENL_ADMIN_PERM,
367 .policy = tcmu_attr_policy,
368 .doit = tcmu_genl_reconfig_dev_done,
372 /* Our generic netlink family */
373 static struct genl_family tcmu_genl_family __ro_after_init = {
374 .module = THIS_MODULE,
378 .maxattr = TCMU_ATTR_MAX,
379 .mcgrps = tcmu_mcgrps,
380 .n_mcgrps = ARRAY_SIZE(tcmu_mcgrps),
382 .ops = tcmu_genl_ops,
383 .n_ops = ARRAY_SIZE(tcmu_genl_ops),
386 #define tcmu_cmd_set_dbi_cur(cmd, index) ((cmd)->dbi_cur = (index))
387 #define tcmu_cmd_reset_dbi_cur(cmd) tcmu_cmd_set_dbi_cur(cmd, 0)
388 #define tcmu_cmd_set_dbi(cmd, index) ((cmd)->dbi[(cmd)->dbi_cur++] = (index))
389 #define tcmu_cmd_get_dbi(cmd) ((cmd)->dbi[(cmd)->dbi_cur++])
391 static void tcmu_cmd_free_data(struct tcmu_cmd *tcmu_cmd, uint32_t len)
393 struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
396 for (i = 0; i < len; i++)
397 clear_bit(tcmu_cmd->dbi[i], udev->data_bitmap);
400 static inline bool tcmu_get_empty_block(struct tcmu_dev *udev,
401 struct tcmu_cmd *tcmu_cmd)
406 dbi = find_first_zero_bit(udev->data_bitmap, udev->dbi_thresh);
407 if (dbi == udev->dbi_thresh)
410 page = radix_tree_lookup(&udev->data_blocks, dbi);
412 if (atomic_add_return(1, &global_db_count) >
413 tcmu_global_max_blocks)
414 schedule_delayed_work(&tcmu_unmap_work, 0);
416 /* try to get new page from the mm */
417 page = alloc_page(GFP_KERNEL);
421 ret = radix_tree_insert(&udev->data_blocks, dbi, page);
426 if (dbi > udev->dbi_max)
429 set_bit(dbi, udev->data_bitmap);
430 tcmu_cmd_set_dbi(tcmu_cmd, dbi);
436 atomic_dec(&global_db_count);
440 static bool tcmu_get_empty_blocks(struct tcmu_dev *udev,
441 struct tcmu_cmd *tcmu_cmd)
445 for (i = tcmu_cmd->dbi_cur; i < tcmu_cmd->dbi_cnt; i++) {
446 if (!tcmu_get_empty_block(udev, tcmu_cmd))
452 static inline struct page *
453 tcmu_get_block_page(struct tcmu_dev *udev, uint32_t dbi)
455 return radix_tree_lookup(&udev->data_blocks, dbi);
458 static inline void tcmu_free_cmd(struct tcmu_cmd *tcmu_cmd)
460 kfree(tcmu_cmd->dbi);
461 kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
464 static inline size_t tcmu_cmd_get_data_length(struct tcmu_cmd *tcmu_cmd)
466 struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
467 size_t data_length = round_up(se_cmd->data_length, DATA_BLOCK_SIZE);
469 if (se_cmd->se_cmd_flags & SCF_BIDI) {
470 BUG_ON(!(se_cmd->t_bidi_data_sg && se_cmd->t_bidi_data_nents));
471 data_length += round_up(se_cmd->t_bidi_data_sg->length,
478 static inline uint32_t tcmu_cmd_get_block_cnt(struct tcmu_cmd *tcmu_cmd)
480 size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd);
482 return data_length / DATA_BLOCK_SIZE;
485 static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd)
487 struct se_device *se_dev = se_cmd->se_dev;
488 struct tcmu_dev *udev = TCMU_DEV(se_dev);
489 struct tcmu_cmd *tcmu_cmd;
491 tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_KERNEL);
495 INIT_LIST_HEAD(&tcmu_cmd->cmdr_queue_entry);
496 tcmu_cmd->se_cmd = se_cmd;
497 tcmu_cmd->tcmu_dev = udev;
499 tcmu_cmd_reset_dbi_cur(tcmu_cmd);
500 tcmu_cmd->dbi_cnt = tcmu_cmd_get_block_cnt(tcmu_cmd);
501 tcmu_cmd->dbi = kcalloc(tcmu_cmd->dbi_cnt, sizeof(uint32_t),
503 if (!tcmu_cmd->dbi) {
504 kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
511 static inline void tcmu_flush_dcache_range(void *vaddr, size_t size)
513 unsigned long offset = offset_in_page(vaddr);
514 void *start = vaddr - offset;
516 size = round_up(size+offset, PAGE_SIZE);
519 flush_dcache_page(virt_to_page(start));
526 * Some ring helper functions. We don't assume size is a power of 2 so
527 * we can't use circ_buf.h.
529 static inline size_t spc_used(size_t head, size_t tail, size_t size)
531 int diff = head - tail;
539 static inline size_t spc_free(size_t head, size_t tail, size_t size)
541 /* Keep 1 byte unused or we can't tell full from empty */
542 return (size - spc_used(head, tail, size) - 1);
545 static inline size_t head_to_end(size_t head, size_t size)
550 static inline void new_iov(struct iovec **iov, int *iov_cnt)
559 memset(iovec, 0, sizeof(struct iovec));
562 #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size)
564 /* offset is relative to mb_addr */
565 static inline size_t get_block_offset_user(struct tcmu_dev *dev,
566 int dbi, int remaining)
568 return dev->data_off + dbi * DATA_BLOCK_SIZE +
569 DATA_BLOCK_SIZE - remaining;
572 static inline size_t iov_tail(struct iovec *iov)
574 return (size_t)iov->iov_base + iov->iov_len;
577 static void scatter_data_area(struct tcmu_dev *udev,
578 struct tcmu_cmd *tcmu_cmd, struct scatterlist *data_sg,
579 unsigned int data_nents, struct iovec **iov,
580 int *iov_cnt, bool copy_data)
583 int block_remaining = 0;
584 void *from, *to = NULL;
585 size_t copy_bytes, to_offset, offset;
586 struct scatterlist *sg;
589 for_each_sg(data_sg, sg, data_nents, i) {
590 int sg_remaining = sg->length;
591 from = kmap_atomic(sg_page(sg)) + sg->offset;
592 while (sg_remaining > 0) {
593 if (block_remaining == 0) {
597 block_remaining = DATA_BLOCK_SIZE;
598 dbi = tcmu_cmd_get_dbi(tcmu_cmd);
599 page = tcmu_get_block_page(udev, dbi);
600 to = kmap_atomic(page);
604 * Covert to virtual offset of the ring data area.
606 to_offset = get_block_offset_user(udev, dbi,
610 * The following code will gather and map the blocks
611 * to the same iovec when the blocks are all next to
614 copy_bytes = min_t(size_t, sg_remaining,
617 to_offset == iov_tail(*iov)) {
619 * Will append to the current iovec, because
620 * the current block page is next to the
623 (*iov)->iov_len += copy_bytes;
626 * Will allocate a new iovec because we are
627 * first time here or the current block page
628 * is not next to the previous one.
630 new_iov(iov, iov_cnt);
631 (*iov)->iov_base = (void __user *)to_offset;
632 (*iov)->iov_len = copy_bytes;
636 offset = DATA_BLOCK_SIZE - block_remaining;
638 from + sg->length - sg_remaining,
640 tcmu_flush_dcache_range(to, copy_bytes);
643 sg_remaining -= copy_bytes;
644 block_remaining -= copy_bytes;
646 kunmap_atomic(from - sg->offset);
653 static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
656 struct se_cmd *se_cmd = cmd->se_cmd;
658 int block_remaining = 0;
659 void *from = NULL, *to;
660 size_t copy_bytes, offset;
661 struct scatterlist *sg, *data_sg;
663 unsigned int data_nents;
667 data_sg = se_cmd->t_data_sg;
668 data_nents = se_cmd->t_data_nents;
672 * For bidi case, the first count blocks are for Data-Out
673 * buffer blocks, and before gathering the Data-In buffer
674 * the Data-Out buffer blocks should be discarded.
676 count = DIV_ROUND_UP(se_cmd->data_length, DATA_BLOCK_SIZE);
678 data_sg = se_cmd->t_bidi_data_sg;
679 data_nents = se_cmd->t_bidi_data_nents;
682 tcmu_cmd_set_dbi_cur(cmd, count);
684 for_each_sg(data_sg, sg, data_nents, i) {
685 int sg_remaining = sg->length;
686 to = kmap_atomic(sg_page(sg)) + sg->offset;
687 while (sg_remaining > 0) {
688 if (block_remaining == 0) {
692 block_remaining = DATA_BLOCK_SIZE;
693 dbi = tcmu_cmd_get_dbi(cmd);
694 page = tcmu_get_block_page(udev, dbi);
695 from = kmap_atomic(page);
697 copy_bytes = min_t(size_t, sg_remaining,
699 offset = DATA_BLOCK_SIZE - block_remaining;
700 tcmu_flush_dcache_range(from, copy_bytes);
701 memcpy(to + sg->length - sg_remaining, from + offset,
704 sg_remaining -= copy_bytes;
705 block_remaining -= copy_bytes;
707 kunmap_atomic(to - sg->offset);
713 static inline size_t spc_bitmap_free(unsigned long *bitmap, uint32_t thresh)
715 return thresh - bitmap_weight(bitmap, thresh);
719 * We can't queue a command until we have space available on the cmd ring *and*
720 * space available on the data area.
722 * Called with ring lock held.
724 static bool is_ring_space_avail(struct tcmu_dev *udev, struct tcmu_cmd *cmd,
725 size_t cmd_size, size_t data_needed)
727 struct tcmu_mailbox *mb = udev->mb_addr;
728 uint32_t blocks_needed = (data_needed + DATA_BLOCK_SIZE - 1)
730 size_t space, cmd_needed;
733 tcmu_flush_dcache_range(mb, sizeof(*mb));
735 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
738 * If cmd end-of-ring space is too small then we need space for a NOP plus
739 * original cmd - cmds are internally contiguous.
741 if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size)
742 cmd_needed = cmd_size;
744 cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size);
746 space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size);
747 if (space < cmd_needed) {
748 pr_debug("no cmd space: %u %u %u\n", cmd_head,
749 udev->cmdr_last_cleaned, udev->cmdr_size);
753 /* try to check and get the data blocks as needed */
754 space = spc_bitmap_free(udev->data_bitmap, udev->dbi_thresh);
755 if ((space * DATA_BLOCK_SIZE) < data_needed) {
756 unsigned long blocks_left =
757 (udev->max_blocks - udev->dbi_thresh) + space;
759 if (blocks_left < blocks_needed) {
760 pr_debug("no data space: only %lu available, but ask for %zu\n",
761 blocks_left * DATA_BLOCK_SIZE,
766 udev->dbi_thresh += blocks_needed;
767 if (udev->dbi_thresh > udev->max_blocks)
768 udev->dbi_thresh = udev->max_blocks;
771 return tcmu_get_empty_blocks(udev, cmd);
774 static inline size_t tcmu_cmd_get_base_cmd_size(size_t iov_cnt)
776 return max(offsetof(struct tcmu_cmd_entry, req.iov[iov_cnt]),
777 sizeof(struct tcmu_cmd_entry));
780 static inline size_t tcmu_cmd_get_cmd_size(struct tcmu_cmd *tcmu_cmd,
781 size_t base_command_size)
783 struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
786 command_size = base_command_size +
787 round_up(scsi_command_size(se_cmd->t_task_cdb),
790 WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1));
795 static int tcmu_setup_cmd_timer(struct tcmu_cmd *tcmu_cmd, unsigned int tmo,
796 struct timer_list *timer)
798 struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
801 if (tcmu_cmd->cmd_id)
804 cmd_id = idr_alloc(&udev->commands, tcmu_cmd, 1, USHRT_MAX, GFP_NOWAIT);
806 pr_err("tcmu: Could not allocate cmd id.\n");
809 tcmu_cmd->cmd_id = cmd_id;
811 pr_debug("allocated cmd %u for dev %s tmo %lu\n", tcmu_cmd->cmd_id,
812 udev->name, tmo / MSEC_PER_SEC);
818 tcmu_cmd->deadline = round_jiffies_up(jiffies + msecs_to_jiffies(tmo));
819 mod_timer(timer, tcmu_cmd->deadline);
823 static int add_to_cmdr_queue(struct tcmu_cmd *tcmu_cmd)
825 struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
830 * For backwards compat if qfull_time_out is not set use
831 * cmd_time_out and if that's not set use the default time out.
833 if (!udev->qfull_time_out)
835 else if (udev->qfull_time_out > 0)
836 tmo = udev->qfull_time_out;
837 else if (udev->cmd_time_out)
838 tmo = udev->cmd_time_out;
842 ret = tcmu_setup_cmd_timer(tcmu_cmd, tmo, &udev->qfull_timer);
846 list_add_tail(&tcmu_cmd->cmdr_queue_entry, &udev->cmdr_queue);
847 pr_debug("adding cmd %u on dev %s to ring space wait queue\n",
848 tcmu_cmd->cmd_id, udev->name);
853 * queue_cmd_ring - queue cmd to ring or internally
854 * @tcmu_cmd: cmd to queue
855 * @scsi_err: TCM error code if failure (-1) returned.
858 * -1 we cannot queue internally or to the ring.
860 * 1 internally queued to wait for ring memory to free.
862 static sense_reason_t queue_cmd_ring(struct tcmu_cmd *tcmu_cmd, int *scsi_err)
864 struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
865 struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
866 size_t base_command_size, command_size;
867 struct tcmu_mailbox *mb;
868 struct tcmu_cmd_entry *entry;
873 bool copy_to_data_area;
874 size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd);
876 *scsi_err = TCM_NO_SENSE;
878 if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) {
879 *scsi_err = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
884 * Must be a certain minimum size for response sense info, but
885 * also may be larger if the iov array is large.
887 * We prepare as many iovs as possbile for potential uses here,
888 * because it's expensive to tell how many regions are freed in
889 * the bitmap & global data pool, as the size calculated here
890 * will only be used to do the checks.
892 * The size will be recalculated later as actually needed to save
895 base_command_size = tcmu_cmd_get_base_cmd_size(tcmu_cmd->dbi_cnt);
896 command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size);
898 if (!list_empty(&udev->cmdr_queue))
902 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
903 if ((command_size > (udev->cmdr_size / 2)) ||
904 data_length > udev->data_size) {
905 pr_warn("TCMU: Request of size %zu/%zu is too big for %u/%zu "
906 "cmd ring/data area\n", command_size, data_length,
907 udev->cmdr_size, udev->data_size);
908 *scsi_err = TCM_INVALID_CDB_FIELD;
912 if (!is_ring_space_avail(udev, tcmu_cmd, command_size, data_length)) {
914 * Don't leave commands partially setup because the unmap
915 * thread might need the blocks to make forward progress.
917 tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cur);
918 tcmu_cmd_reset_dbi_cur(tcmu_cmd);
922 /* Insert a PAD if end-of-ring space is too small */
923 if (head_to_end(cmd_head, udev->cmdr_size) < command_size) {
924 size_t pad_size = head_to_end(cmd_head, udev->cmdr_size);
926 entry = (void *) mb + CMDR_OFF + cmd_head;
927 tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_PAD);
928 tcmu_hdr_set_len(&entry->hdr.len_op, pad_size);
929 entry->hdr.cmd_id = 0; /* not used for PAD */
930 entry->hdr.kflags = 0;
931 entry->hdr.uflags = 0;
932 tcmu_flush_dcache_range(entry, sizeof(*entry));
934 UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size);
935 tcmu_flush_dcache_range(mb, sizeof(*mb));
937 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
938 WARN_ON(cmd_head != 0);
941 entry = (void *) mb + CMDR_OFF + cmd_head;
942 memset(entry, 0, command_size);
943 tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD);
945 /* Handle allocating space from the data area */
946 tcmu_cmd_reset_dbi_cur(tcmu_cmd);
947 iov = &entry->req.iov[0];
949 copy_to_data_area = (se_cmd->data_direction == DMA_TO_DEVICE
950 || se_cmd->se_cmd_flags & SCF_BIDI);
951 scatter_data_area(udev, tcmu_cmd, se_cmd->t_data_sg,
952 se_cmd->t_data_nents, &iov, &iov_cnt,
954 entry->req.iov_cnt = iov_cnt;
956 /* Handle BIDI commands */
958 if (se_cmd->se_cmd_flags & SCF_BIDI) {
960 scatter_data_area(udev, tcmu_cmd, se_cmd->t_bidi_data_sg,
961 se_cmd->t_bidi_data_nents, &iov, &iov_cnt,
964 entry->req.iov_bidi_cnt = iov_cnt;
966 ret = tcmu_setup_cmd_timer(tcmu_cmd, udev->cmd_time_out,
969 tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt);
970 mutex_unlock(&udev->cmdr_lock);
972 *scsi_err = TCM_OUT_OF_RESOURCES;
975 entry->hdr.cmd_id = tcmu_cmd->cmd_id;
978 * Recalaulate the command's base size and size according
979 * to the actual needs
981 base_command_size = tcmu_cmd_get_base_cmd_size(entry->req.iov_cnt +
982 entry->req.iov_bidi_cnt);
983 command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size);
985 tcmu_hdr_set_len(&entry->hdr.len_op, command_size);
987 /* All offsets relative to mb_addr, not start of entry! */
988 cdb_off = CMDR_OFF + cmd_head + base_command_size;
989 memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb));
990 entry->req.cdb_off = cdb_off;
991 tcmu_flush_dcache_range(entry, sizeof(*entry));
993 UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size);
994 tcmu_flush_dcache_range(mb, sizeof(*mb));
996 /* TODO: only if FLUSH and FUA? */
997 uio_event_notify(&udev->uio_info);
1002 if (add_to_cmdr_queue(tcmu_cmd)) {
1003 *scsi_err = TCM_OUT_OF_RESOURCES;
1010 static sense_reason_t
1011 tcmu_queue_cmd(struct se_cmd *se_cmd)
1013 struct se_device *se_dev = se_cmd->se_dev;
1014 struct tcmu_dev *udev = TCMU_DEV(se_dev);
1015 struct tcmu_cmd *tcmu_cmd;
1016 sense_reason_t scsi_ret;
1019 tcmu_cmd = tcmu_alloc_cmd(se_cmd);
1021 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1023 mutex_lock(&udev->cmdr_lock);
1024 ret = queue_cmd_ring(tcmu_cmd, &scsi_ret);
1025 mutex_unlock(&udev->cmdr_lock);
1027 tcmu_free_cmd(tcmu_cmd);
1031 static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *entry)
1033 struct se_cmd *se_cmd = cmd->se_cmd;
1034 struct tcmu_dev *udev = cmd->tcmu_dev;
1037 * cmd has been completed already from timeout, just reclaim
1038 * data area space and free cmd
1040 if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
1043 tcmu_cmd_reset_dbi_cur(cmd);
1045 if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) {
1046 pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n",
1048 entry->rsp.scsi_status = SAM_STAT_CHECK_CONDITION;
1049 } else if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) {
1050 transport_copy_sense_to_cmd(se_cmd, entry->rsp.sense_buffer);
1051 } else if (se_cmd->se_cmd_flags & SCF_BIDI) {
1052 /* Get Data-In buffer before clean up */
1053 gather_data_area(udev, cmd, true);
1054 } else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
1055 gather_data_area(udev, cmd, false);
1056 } else if (se_cmd->data_direction == DMA_TO_DEVICE) {
1058 } else if (se_cmd->data_direction != DMA_NONE) {
1059 pr_warn("TCMU: data direction was %d!\n",
1060 se_cmd->data_direction);
1063 target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status);
1067 tcmu_cmd_free_data(cmd, cmd->dbi_cnt);
1071 static unsigned int tcmu_handle_completions(struct tcmu_dev *udev)
1073 struct tcmu_mailbox *mb;
1076 if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) {
1077 pr_err("ring broken, not handling completions\n");
1082 tcmu_flush_dcache_range(mb, sizeof(*mb));
1084 while (udev->cmdr_last_cleaned != READ_ONCE(mb->cmd_tail)) {
1086 struct tcmu_cmd_entry *entry = (void *) mb + CMDR_OFF + udev->cmdr_last_cleaned;
1087 struct tcmu_cmd *cmd;
1089 tcmu_flush_dcache_range(entry, sizeof(*entry));
1091 if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD) {
1092 UPDATE_HEAD(udev->cmdr_last_cleaned,
1093 tcmu_hdr_get_len(entry->hdr.len_op),
1097 WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD);
1099 cmd = idr_remove(&udev->commands, entry->hdr.cmd_id);
1101 pr_err("cmd_id not found, ring is broken\n");
1102 set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags);
1106 tcmu_handle_completion(cmd, entry);
1108 UPDATE_HEAD(udev->cmdr_last_cleaned,
1109 tcmu_hdr_get_len(entry->hdr.len_op),
1115 if (mb->cmd_tail == mb->cmd_head) {
1116 /* no more pending commands */
1117 del_timer(&udev->cmd_timer);
1119 if (list_empty(&udev->cmdr_queue)) {
1121 * no more pending or waiting commands so try to
1122 * reclaim blocks if needed.
1124 if (atomic_read(&global_db_count) >
1125 tcmu_global_max_blocks)
1126 schedule_delayed_work(&tcmu_unmap_work, 0);
1133 static int tcmu_check_expired_cmd(int id, void *p, void *data)
1135 struct tcmu_cmd *cmd = p;
1136 struct tcmu_dev *udev = cmd->tcmu_dev;
1138 struct se_cmd *se_cmd;
1141 if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
1144 if (!time_after(jiffies, cmd->deadline))
1147 is_running = list_empty(&cmd->cmdr_queue_entry);
1151 * If cmd_time_out is disabled but qfull is set deadline
1152 * will only reflect the qfull timeout. Ignore it.
1154 if (!udev->cmd_time_out)
1157 set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags);
1159 * target_complete_cmd will translate this to LUN COMM FAILURE
1161 scsi_status = SAM_STAT_CHECK_CONDITION;
1163 list_del_init(&cmd->cmdr_queue_entry);
1165 idr_remove(&udev->commands, id);
1167 scsi_status = SAM_STAT_TASK_SET_FULL;
1170 pr_debug("Timing out cmd %u on dev %s that is %s.\n",
1171 id, udev->name, is_running ? "inflight" : "queued");
1173 se_cmd = cmd->se_cmd;
1175 target_complete_cmd(se_cmd, scsi_status);
1179 static void tcmu_device_timedout(struct tcmu_dev *udev)
1181 spin_lock(&timed_out_udevs_lock);
1182 if (list_empty(&udev->timedout_entry))
1183 list_add_tail(&udev->timedout_entry, &timed_out_udevs);
1184 spin_unlock(&timed_out_udevs_lock);
1186 schedule_delayed_work(&tcmu_unmap_work, 0);
1189 static void tcmu_cmd_timedout(struct timer_list *t)
1191 struct tcmu_dev *udev = from_timer(udev, t, cmd_timer);
1193 pr_debug("%s cmd timeout has expired\n", udev->name);
1194 tcmu_device_timedout(udev);
1197 static void tcmu_qfull_timedout(struct timer_list *t)
1199 struct tcmu_dev *udev = from_timer(udev, t, qfull_timer);
1201 pr_debug("%s qfull timeout has expired\n", udev->name);
1202 tcmu_device_timedout(udev);
1205 static int tcmu_attach_hba(struct se_hba *hba, u32 host_id)
1207 struct tcmu_hba *tcmu_hba;
1209 tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL);
1213 tcmu_hba->host_id = host_id;
1214 hba->hba_ptr = tcmu_hba;
1219 static void tcmu_detach_hba(struct se_hba *hba)
1221 kfree(hba->hba_ptr);
1222 hba->hba_ptr = NULL;
1225 static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
1227 struct tcmu_dev *udev;
1229 udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL);
1232 kref_init(&udev->kref);
1234 udev->name = kstrdup(name, GFP_KERNEL);
1241 udev->cmd_time_out = TCMU_TIME_OUT;
1242 udev->qfull_time_out = -1;
1244 udev->max_blocks = DATA_BLOCK_BITS_DEF;
1245 mutex_init(&udev->cmdr_lock);
1247 INIT_LIST_HEAD(&udev->timedout_entry);
1248 INIT_LIST_HEAD(&udev->cmdr_queue);
1249 idr_init(&udev->commands);
1251 timer_setup(&udev->qfull_timer, tcmu_qfull_timedout, 0);
1252 timer_setup(&udev->cmd_timer, tcmu_cmd_timedout, 0);
1254 init_waitqueue_head(&udev->nl_cmd_wq);
1255 spin_lock_init(&udev->nl_cmd_lock);
1257 INIT_RADIX_TREE(&udev->data_blocks, GFP_KERNEL);
1259 return &udev->se_dev;
1262 static bool run_cmdr_queue(struct tcmu_dev *udev)
1264 struct tcmu_cmd *tcmu_cmd, *tmp_cmd;
1266 bool drained = true;
1267 sense_reason_t scsi_ret;
1270 if (list_empty(&udev->cmdr_queue))
1273 pr_debug("running %s's cmdr queue\n", udev->name);
1275 list_splice_init(&udev->cmdr_queue, &cmds);
1277 list_for_each_entry_safe(tcmu_cmd, tmp_cmd, &cmds, cmdr_queue_entry) {
1278 list_del_init(&tcmu_cmd->cmdr_queue_entry);
1280 pr_debug("removing cmd %u on dev %s from queue\n",
1281 tcmu_cmd->cmd_id, udev->name);
1283 ret = queue_cmd_ring(tcmu_cmd, &scsi_ret);
1285 pr_debug("cmd %u on dev %s failed with %u\n",
1286 tcmu_cmd->cmd_id, udev->name, scsi_ret);
1288 idr_remove(&udev->commands, tcmu_cmd->cmd_id);
1290 * Ignore scsi_ret for now. target_complete_cmd
1293 target_complete_cmd(tcmu_cmd->se_cmd,
1294 SAM_STAT_CHECK_CONDITION);
1295 tcmu_free_cmd(tcmu_cmd);
1296 } else if (ret > 0) {
1297 pr_debug("ran out of space during cmdr queue run\n");
1299 * cmd was requeued, so just put all cmds back in
1302 list_splice_tail(&cmds, &udev->cmdr_queue);
1307 if (list_empty(&udev->cmdr_queue))
1308 del_timer(&udev->qfull_timer);
1313 static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on)
1315 struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
1317 mutex_lock(&udev->cmdr_lock);
1318 tcmu_handle_completions(udev);
1319 run_cmdr_queue(udev);
1320 mutex_unlock(&udev->cmdr_lock);
1326 * mmap code from uio.c. Copied here because we want to hook mmap()
1327 * and this stuff must come along.
1329 static int tcmu_find_mem_index(struct vm_area_struct *vma)
1331 struct tcmu_dev *udev = vma->vm_private_data;
1332 struct uio_info *info = &udev->uio_info;
1334 if (vma->vm_pgoff < MAX_UIO_MAPS) {
1335 if (info->mem[vma->vm_pgoff].size == 0)
1337 return (int)vma->vm_pgoff;
1342 static struct page *tcmu_try_get_block_page(struct tcmu_dev *udev, uint32_t dbi)
1347 mutex_lock(&udev->cmdr_lock);
1348 page = tcmu_get_block_page(udev, dbi);
1350 mutex_unlock(&udev->cmdr_lock);
1355 * Normally it shouldn't be here:
1356 * Only when the userspace has touched the blocks which
1357 * are out of the tcmu_cmd's data iov[], and will return
1360 pr_warn("Block(%u) out of cmd's iov[] has been touched!\n", dbi);
1361 pr_warn("Mostly it will be a bug of userspace, please have a check!\n");
1363 if (dbi >= udev->dbi_thresh) {
1364 /* Extern the udev->dbi_thresh to dbi + 1 */
1365 udev->dbi_thresh = dbi + 1;
1366 udev->dbi_max = dbi;
1369 page = radix_tree_lookup(&udev->data_blocks, dbi);
1371 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1373 mutex_unlock(&udev->cmdr_lock);
1377 ret = radix_tree_insert(&udev->data_blocks, dbi, page);
1379 mutex_unlock(&udev->cmdr_lock);
1385 * Since this case is rare in page fault routine, here we
1386 * will allow the global_db_count >= tcmu_global_max_blocks
1387 * to reduce possible page fault call trace.
1389 atomic_inc(&global_db_count);
1391 mutex_unlock(&udev->cmdr_lock);
1396 static int tcmu_vma_fault(struct vm_fault *vmf)
1398 struct tcmu_dev *udev = vmf->vma->vm_private_data;
1399 struct uio_info *info = &udev->uio_info;
1401 unsigned long offset;
1404 int mi = tcmu_find_mem_index(vmf->vma);
1406 return VM_FAULT_SIGBUS;
1409 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
1412 offset = (vmf->pgoff - mi) << PAGE_SHIFT;
1414 if (offset < udev->data_off) {
1415 /* For the vmalloc()ed cmd area pages */
1416 addr = (void *)(unsigned long)info->mem[mi].addr + offset;
1417 page = vmalloc_to_page(addr);
1421 /* For the dynamically growing data area pages */
1422 dbi = (offset - udev->data_off) / DATA_BLOCK_SIZE;
1423 page = tcmu_try_get_block_page(udev, dbi);
1425 return VM_FAULT_NOPAGE;
1433 static const struct vm_operations_struct tcmu_vm_ops = {
1434 .fault = tcmu_vma_fault,
1437 static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma)
1439 struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
1441 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
1442 vma->vm_ops = &tcmu_vm_ops;
1444 vma->vm_private_data = udev;
1446 /* Ensure the mmap is exactly the right size */
1447 if (vma_pages(vma) != (udev->ring_size >> PAGE_SHIFT))
1453 static int tcmu_open(struct uio_info *info, struct inode *inode)
1455 struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
1457 /* O_EXCL not supported for char devs, so fake it? */
1458 if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags))
1461 udev->inode = inode;
1462 kref_get(&udev->kref);
1469 static void tcmu_dev_call_rcu(struct rcu_head *p)
1471 struct se_device *dev = container_of(p, struct se_device, rcu_head);
1472 struct tcmu_dev *udev = TCMU_DEV(dev);
1474 kfree(udev->uio_info.name);
1479 static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd)
1481 if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
1482 kmem_cache_free(tcmu_cmd_cache, cmd);
1488 static void tcmu_blocks_release(struct radix_tree_root *blocks,
1494 for (i = start; i < end; i++) {
1495 page = radix_tree_delete(blocks, i);
1498 atomic_dec(&global_db_count);
1503 static void tcmu_dev_kref_release(struct kref *kref)
1505 struct tcmu_dev *udev = container_of(kref, struct tcmu_dev, kref);
1506 struct se_device *dev = &udev->se_dev;
1507 struct tcmu_cmd *cmd;
1508 bool all_expired = true;
1511 vfree(udev->mb_addr);
1512 udev->mb_addr = NULL;
1514 spin_lock_bh(&timed_out_udevs_lock);
1515 if (!list_empty(&udev->timedout_entry))
1516 list_del(&udev->timedout_entry);
1517 spin_unlock_bh(&timed_out_udevs_lock);
1519 /* Upper layer should drain all requests before calling this */
1520 mutex_lock(&udev->cmdr_lock);
1521 idr_for_each_entry(&udev->commands, cmd, i) {
1522 if (tcmu_check_and_free_pending_cmd(cmd) != 0)
1523 all_expired = false;
1525 idr_destroy(&udev->commands);
1526 WARN_ON(!all_expired);
1528 tcmu_blocks_release(&udev->data_blocks, 0, udev->dbi_max + 1);
1529 kfree(udev->data_bitmap);
1530 mutex_unlock(&udev->cmdr_lock);
1532 call_rcu(&dev->rcu_head, tcmu_dev_call_rcu);
1535 static int tcmu_release(struct uio_info *info, struct inode *inode)
1537 struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
1539 clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags);
1541 pr_debug("close\n");
1542 /* release ref from open */
1543 kref_put(&udev->kref, tcmu_dev_kref_release);
1547 static void tcmu_init_genl_cmd_reply(struct tcmu_dev *udev, int cmd)
1549 struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd;
1551 if (!tcmu_kern_cmd_reply_supported)
1554 if (udev->nl_reply_supported <= 0)
1558 spin_lock(&udev->nl_cmd_lock);
1560 if (nl_cmd->cmd != TCMU_CMD_UNSPEC) {
1561 spin_unlock(&udev->nl_cmd_lock);
1562 pr_debug("sleeping for open nl cmd\n");
1563 wait_event(udev->nl_cmd_wq, (nl_cmd->cmd == TCMU_CMD_UNSPEC));
1567 memset(nl_cmd, 0, sizeof(*nl_cmd));
1569 init_completion(&nl_cmd->complete);
1571 spin_unlock(&udev->nl_cmd_lock);
1574 static int tcmu_wait_genl_cmd_reply(struct tcmu_dev *udev)
1576 struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd;
1578 DEFINE_WAIT(__wait);
1580 if (!tcmu_kern_cmd_reply_supported)
1583 if (udev->nl_reply_supported <= 0)
1586 pr_debug("sleeping for nl reply\n");
1587 wait_for_completion(&nl_cmd->complete);
1589 spin_lock(&udev->nl_cmd_lock);
1590 nl_cmd->cmd = TCMU_CMD_UNSPEC;
1591 ret = nl_cmd->status;
1593 spin_unlock(&udev->nl_cmd_lock);
1595 wake_up_all(&udev->nl_cmd_wq);
1600 static int tcmu_netlink_event(struct tcmu_dev *udev, enum tcmu_genl_cmd cmd,
1601 int reconfig_attr, const void *reconfig_data)
1603 struct sk_buff *skb;
1607 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1611 msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd);
1615 ret = nla_put_string(skb, TCMU_ATTR_DEVICE, udev->uio_info.name);
1619 ret = nla_put_u32(skb, TCMU_ATTR_MINOR, udev->uio_info.uio_dev->minor);
1623 ret = nla_put_u32(skb, TCMU_ATTR_DEVICE_ID, udev->se_dev.dev_index);
1627 if (cmd == TCMU_CMD_RECONFIG_DEVICE) {
1628 switch (reconfig_attr) {
1629 case TCMU_ATTR_DEV_CFG:
1630 ret = nla_put_string(skb, reconfig_attr, reconfig_data);
1632 case TCMU_ATTR_DEV_SIZE:
1633 ret = nla_put_u64_64bit(skb, reconfig_attr,
1634 *((u64 *)reconfig_data),
1637 case TCMU_ATTR_WRITECACHE:
1638 ret = nla_put_u8(skb, reconfig_attr,
1639 *((u8 *)reconfig_data));
1649 genlmsg_end(skb, msg_header);
1651 tcmu_init_genl_cmd_reply(udev, cmd);
1653 ret = genlmsg_multicast_allns(&tcmu_genl_family, skb, 0,
1654 TCMU_MCGRP_CONFIG, GFP_KERNEL);
1655 /* We don't care if no one is listening */
1659 ret = tcmu_wait_genl_cmd_reply(udev);
1667 static int tcmu_update_uio_info(struct tcmu_dev *udev)
1669 struct tcmu_hba *hba = udev->hba->hba_ptr;
1670 struct uio_info *info;
1674 info = &udev->uio_info;
1675 size = snprintf(NULL, 0, "tcm-user/%u/%s/%s", hba->host_id, udev->name,
1677 size += 1; /* for \0 */
1678 str = kmalloc(size, GFP_KERNEL);
1682 used = snprintf(str, size, "tcm-user/%u/%s", hba->host_id, udev->name);
1683 if (udev->dev_config[0])
1684 snprintf(str + used, size - used, "/%s", udev->dev_config);
1686 /* If the old string exists, free it */
1693 static int tcmu_configure_device(struct se_device *dev)
1695 struct tcmu_dev *udev = TCMU_DEV(dev);
1696 struct uio_info *info;
1697 struct tcmu_mailbox *mb;
1700 ret = tcmu_update_uio_info(udev);
1704 info = &udev->uio_info;
1706 udev->data_bitmap = kzalloc(BITS_TO_LONGS(udev->max_blocks) *
1707 sizeof(unsigned long), GFP_KERNEL);
1708 if (!udev->data_bitmap)
1709 goto err_bitmap_alloc;
1711 udev->mb_addr = vzalloc(CMDR_SIZE);
1712 if (!udev->mb_addr) {
1717 /* mailbox fits in first part of CMDR space */
1718 udev->cmdr_size = CMDR_SIZE - CMDR_OFF;
1719 udev->data_off = CMDR_SIZE;
1720 udev->data_size = udev->max_blocks * DATA_BLOCK_SIZE;
1721 udev->dbi_thresh = 0; /* Default in Idle state */
1723 /* Initialise the mailbox of the ring buffer */
1725 mb->version = TCMU_MAILBOX_VERSION;
1726 mb->flags = TCMU_MAILBOX_FLAG_CAP_OOOC;
1727 mb->cmdr_off = CMDR_OFF;
1728 mb->cmdr_size = udev->cmdr_size;
1730 WARN_ON(!PAGE_ALIGNED(udev->data_off));
1731 WARN_ON(udev->data_size % PAGE_SIZE);
1732 WARN_ON(udev->data_size % DATA_BLOCK_SIZE);
1734 info->version = __stringify(TCMU_MAILBOX_VERSION);
1736 info->mem[0].name = "tcm-user command & data buffer";
1737 info->mem[0].addr = (phys_addr_t)(uintptr_t)udev->mb_addr;
1738 info->mem[0].size = udev->ring_size = udev->data_size + CMDR_SIZE;
1739 info->mem[0].memtype = UIO_MEM_NONE;
1741 info->irqcontrol = tcmu_irqcontrol;
1742 info->irq = UIO_IRQ_CUSTOM;
1744 info->mmap = tcmu_mmap;
1745 info->open = tcmu_open;
1746 info->release = tcmu_release;
1748 ret = uio_register_device(tcmu_root_device, info);
1752 /* User can set hw_block_size before enable the device */
1753 if (dev->dev_attrib.hw_block_size == 0)
1754 dev->dev_attrib.hw_block_size = 512;
1755 /* Other attributes can be configured in userspace */
1756 if (!dev->dev_attrib.hw_max_sectors)
1757 dev->dev_attrib.hw_max_sectors = 128;
1758 if (!dev->dev_attrib.emulate_write_cache)
1759 dev->dev_attrib.emulate_write_cache = 0;
1760 dev->dev_attrib.hw_queue_depth = 128;
1762 /* If user didn't explicitly disable netlink reply support, use
1763 * module scope setting.
1765 if (udev->nl_reply_supported >= 0)
1766 udev->nl_reply_supported = tcmu_kern_cmd_reply_supported;
1769 * Get a ref incase userspace does a close on the uio device before
1770 * LIO has initiated tcmu_free_device.
1772 kref_get(&udev->kref);
1774 ret = tcmu_netlink_event(udev, TCMU_CMD_ADDED_DEVICE, 0, NULL);
1778 mutex_lock(&root_udev_mutex);
1779 list_add(&udev->node, &root_udev);
1780 mutex_unlock(&root_udev_mutex);
1785 kref_put(&udev->kref, tcmu_dev_kref_release);
1786 uio_unregister_device(&udev->uio_info);
1788 vfree(udev->mb_addr);
1789 udev->mb_addr = NULL;
1791 kfree(udev->data_bitmap);
1792 udev->data_bitmap = NULL;
1800 static bool tcmu_dev_configured(struct tcmu_dev *udev)
1802 return udev->uio_info.uio_dev ? true : false;
1805 static void tcmu_free_device(struct se_device *dev)
1807 struct tcmu_dev *udev = TCMU_DEV(dev);
1809 /* release ref from init */
1810 kref_put(&udev->kref, tcmu_dev_kref_release);
1813 static void tcmu_destroy_device(struct se_device *dev)
1815 struct tcmu_dev *udev = TCMU_DEV(dev);
1817 del_timer_sync(&udev->cmd_timer);
1818 del_timer_sync(&udev->qfull_timer);
1820 mutex_lock(&root_udev_mutex);
1821 list_del(&udev->node);
1822 mutex_unlock(&root_udev_mutex);
1824 tcmu_netlink_event(udev, TCMU_CMD_REMOVED_DEVICE, 0, NULL);
1826 uio_unregister_device(&udev->uio_info);
1828 /* release ref from configure */
1829 kref_put(&udev->kref, tcmu_dev_kref_release);
1833 Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_hw_max_sectors,
1834 Opt_nl_reply_supported, Opt_max_data_area_mb, Opt_err,
1837 static match_table_t tokens = {
1838 {Opt_dev_config, "dev_config=%s"},
1839 {Opt_dev_size, "dev_size=%u"},
1840 {Opt_hw_block_size, "hw_block_size=%u"},
1841 {Opt_hw_max_sectors, "hw_max_sectors=%u"},
1842 {Opt_nl_reply_supported, "nl_reply_supported=%d"},
1843 {Opt_max_data_area_mb, "max_data_area_mb=%u"},
1847 static int tcmu_set_dev_attrib(substring_t *arg, u32 *dev_attrib)
1849 unsigned long tmp_ul;
1853 arg_p = match_strdup(arg);
1857 ret = kstrtoul(arg_p, 0, &tmp_ul);
1860 pr_err("kstrtoul() failed for dev attrib\n");
1864 pr_err("dev attrib must be nonzero\n");
1867 *dev_attrib = tmp_ul;
1871 static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev,
1872 const char *page, ssize_t count)
1874 struct tcmu_dev *udev = TCMU_DEV(dev);
1875 char *orig, *ptr, *opts, *arg_p;
1876 substring_t args[MAX_OPT_ARGS];
1877 int ret = 0, token, tmpval;
1879 opts = kstrdup(page, GFP_KERNEL);
1885 while ((ptr = strsep(&opts, ",\n")) != NULL) {
1889 token = match_token(ptr, tokens, args);
1891 case Opt_dev_config:
1892 if (match_strlcpy(udev->dev_config, &args[0],
1893 TCMU_CONFIG_LEN) == 0) {
1897 pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config);
1900 arg_p = match_strdup(&args[0]);
1905 ret = kstrtoul(arg_p, 0, (unsigned long *) &udev->dev_size);
1908 pr_err("kstrtoul() failed for dev_size=\n");
1910 case Opt_hw_block_size:
1911 ret = tcmu_set_dev_attrib(&args[0],
1912 &(dev->dev_attrib.hw_block_size));
1914 case Opt_hw_max_sectors:
1915 ret = tcmu_set_dev_attrib(&args[0],
1916 &(dev->dev_attrib.hw_max_sectors));
1918 case Opt_nl_reply_supported:
1919 arg_p = match_strdup(&args[0]);
1924 ret = kstrtoint(arg_p, 0, &udev->nl_reply_supported);
1927 pr_err("kstrtoint() failed for nl_reply_supported=\n");
1929 case Opt_max_data_area_mb:
1930 if (dev->export_count) {
1931 pr_err("Unable to set max_data_area_mb while exports exist\n");
1936 arg_p = match_strdup(&args[0]);
1941 ret = kstrtoint(arg_p, 0, &tmpval);
1944 pr_err("kstrtoint() failed for max_data_area_mb=\n");
1949 pr_err("Invalid max_data_area %d\n", tmpval);
1954 udev->max_blocks = TCMU_MBS_TO_BLOCKS(tmpval);
1955 if (udev->max_blocks > tcmu_global_max_blocks) {
1956 pr_err("%d is too large. Adjusting max_data_area_mb to global limit of %u\n",
1958 TCMU_BLOCKS_TO_MBS(tcmu_global_max_blocks));
1959 udev->max_blocks = tcmu_global_max_blocks;
1971 return (!ret) ? count : ret;
1974 static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b)
1976 struct tcmu_dev *udev = TCMU_DEV(dev);
1979 bl = sprintf(b + bl, "Config: %s ",
1980 udev->dev_config[0] ? udev->dev_config : "NULL");
1981 bl += sprintf(b + bl, "Size: %zu ", udev->dev_size);
1982 bl += sprintf(b + bl, "MaxDataAreaMB: %u\n",
1983 TCMU_BLOCKS_TO_MBS(udev->max_blocks));
1988 static sector_t tcmu_get_blocks(struct se_device *dev)
1990 struct tcmu_dev *udev = TCMU_DEV(dev);
1992 return div_u64(udev->dev_size - dev->dev_attrib.block_size,
1993 dev->dev_attrib.block_size);
1996 static sense_reason_t
1997 tcmu_parse_cdb(struct se_cmd *cmd)
1999 return passthrough_parse_cdb(cmd, tcmu_queue_cmd);
2002 static ssize_t tcmu_cmd_time_out_show(struct config_item *item, char *page)
2004 struct se_dev_attrib *da = container_of(to_config_group(item),
2005 struct se_dev_attrib, da_group);
2006 struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2008 return snprintf(page, PAGE_SIZE, "%lu\n", udev->cmd_time_out / MSEC_PER_SEC);
2011 static ssize_t tcmu_cmd_time_out_store(struct config_item *item, const char *page,
2014 struct se_dev_attrib *da = container_of(to_config_group(item),
2015 struct se_dev_attrib, da_group);
2016 struct tcmu_dev *udev = container_of(da->da_dev,
2017 struct tcmu_dev, se_dev);
2021 if (da->da_dev->export_count) {
2022 pr_err("Unable to set tcmu cmd_time_out while exports exist\n");
2026 ret = kstrtou32(page, 0, &val);
2030 udev->cmd_time_out = val * MSEC_PER_SEC;
2033 CONFIGFS_ATTR(tcmu_, cmd_time_out);
2035 static ssize_t tcmu_qfull_time_out_show(struct config_item *item, char *page)
2037 struct se_dev_attrib *da = container_of(to_config_group(item),
2038 struct se_dev_attrib, da_group);
2039 struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2041 return snprintf(page, PAGE_SIZE, "%ld\n", udev->qfull_time_out <= 0 ?
2042 udev->qfull_time_out :
2043 udev->qfull_time_out / MSEC_PER_SEC);
2046 static ssize_t tcmu_qfull_time_out_store(struct config_item *item,
2047 const char *page, size_t count)
2049 struct se_dev_attrib *da = container_of(to_config_group(item),
2050 struct se_dev_attrib, da_group);
2051 struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2055 ret = kstrtos32(page, 0, &val);
2060 udev->qfull_time_out = val * MSEC_PER_SEC;
2062 printk(KERN_ERR "Invalid qfull timeout value %d\n", val);
2067 CONFIGFS_ATTR(tcmu_, qfull_time_out);
2069 static ssize_t tcmu_max_data_area_mb_show(struct config_item *item, char *page)
2071 struct se_dev_attrib *da = container_of(to_config_group(item),
2072 struct se_dev_attrib, da_group);
2073 struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2075 return snprintf(page, PAGE_SIZE, "%u\n",
2076 TCMU_BLOCKS_TO_MBS(udev->max_blocks));
2078 CONFIGFS_ATTR_RO(tcmu_, max_data_area_mb);
2080 static ssize_t tcmu_dev_config_show(struct config_item *item, char *page)
2082 struct se_dev_attrib *da = container_of(to_config_group(item),
2083 struct se_dev_attrib, da_group);
2084 struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2086 return snprintf(page, PAGE_SIZE, "%s\n", udev->dev_config);
2089 static ssize_t tcmu_dev_config_store(struct config_item *item, const char *page,
2092 struct se_dev_attrib *da = container_of(to_config_group(item),
2093 struct se_dev_attrib, da_group);
2094 struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2098 if (!len || len > TCMU_CONFIG_LEN - 1)
2101 /* Check if device has been configured before */
2102 if (tcmu_dev_configured(udev)) {
2103 ret = tcmu_netlink_event(udev, TCMU_CMD_RECONFIG_DEVICE,
2104 TCMU_ATTR_DEV_CFG, page);
2106 pr_err("Unable to reconfigure device\n");
2109 strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN);
2111 ret = tcmu_update_uio_info(udev);
2116 strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN);
2120 CONFIGFS_ATTR(tcmu_, dev_config);
2122 static ssize_t tcmu_dev_size_show(struct config_item *item, char *page)
2124 struct se_dev_attrib *da = container_of(to_config_group(item),
2125 struct se_dev_attrib, da_group);
2126 struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2128 return snprintf(page, PAGE_SIZE, "%zu\n", udev->dev_size);
2131 static ssize_t tcmu_dev_size_store(struct config_item *item, const char *page,
2134 struct se_dev_attrib *da = container_of(to_config_group(item),
2135 struct se_dev_attrib, da_group);
2136 struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2140 ret = kstrtou64(page, 0, &val);
2144 /* Check if device has been configured before */
2145 if (tcmu_dev_configured(udev)) {
2146 ret = tcmu_netlink_event(udev, TCMU_CMD_RECONFIG_DEVICE,
2147 TCMU_ATTR_DEV_SIZE, &val);
2149 pr_err("Unable to reconfigure device\n");
2153 udev->dev_size = val;
2156 CONFIGFS_ATTR(tcmu_, dev_size);
2158 static ssize_t tcmu_nl_reply_supported_show(struct config_item *item,
2161 struct se_dev_attrib *da = container_of(to_config_group(item),
2162 struct se_dev_attrib, da_group);
2163 struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2165 return snprintf(page, PAGE_SIZE, "%d\n", udev->nl_reply_supported);
2168 static ssize_t tcmu_nl_reply_supported_store(struct config_item *item,
2169 const char *page, size_t count)
2171 struct se_dev_attrib *da = container_of(to_config_group(item),
2172 struct se_dev_attrib, da_group);
2173 struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2177 ret = kstrtos8(page, 0, &val);
2181 udev->nl_reply_supported = val;
2184 CONFIGFS_ATTR(tcmu_, nl_reply_supported);
2186 static ssize_t tcmu_emulate_write_cache_show(struct config_item *item,
2189 struct se_dev_attrib *da = container_of(to_config_group(item),
2190 struct se_dev_attrib, da_group);
2192 return snprintf(page, PAGE_SIZE, "%i\n", da->emulate_write_cache);
2195 static ssize_t tcmu_emulate_write_cache_store(struct config_item *item,
2196 const char *page, size_t count)
2198 struct se_dev_attrib *da = container_of(to_config_group(item),
2199 struct se_dev_attrib, da_group);
2200 struct tcmu_dev *udev = TCMU_DEV(da->da_dev);
2204 ret = kstrtou8(page, 0, &val);
2208 /* Check if device has been configured before */
2209 if (tcmu_dev_configured(udev)) {
2210 ret = tcmu_netlink_event(udev, TCMU_CMD_RECONFIG_DEVICE,
2211 TCMU_ATTR_WRITECACHE, &val);
2213 pr_err("Unable to reconfigure device\n");
2218 da->emulate_write_cache = val;
2221 CONFIGFS_ATTR(tcmu_, emulate_write_cache);
2223 static struct configfs_attribute *tcmu_attrib_attrs[] = {
2224 &tcmu_attr_cmd_time_out,
2225 &tcmu_attr_qfull_time_out,
2226 &tcmu_attr_max_data_area_mb,
2227 &tcmu_attr_dev_config,
2228 &tcmu_attr_dev_size,
2229 &tcmu_attr_emulate_write_cache,
2230 &tcmu_attr_nl_reply_supported,
2234 static struct configfs_attribute **tcmu_attrs;
2236 static struct target_backend_ops tcmu_ops = {
2238 .owner = THIS_MODULE,
2239 .transport_flags = TRANSPORT_FLAG_PASSTHROUGH,
2240 .attach_hba = tcmu_attach_hba,
2241 .detach_hba = tcmu_detach_hba,
2242 .alloc_device = tcmu_alloc_device,
2243 .configure_device = tcmu_configure_device,
2244 .destroy_device = tcmu_destroy_device,
2245 .free_device = tcmu_free_device,
2246 .parse_cdb = tcmu_parse_cdb,
2247 .set_configfs_dev_params = tcmu_set_configfs_dev_params,
2248 .show_configfs_dev_params = tcmu_show_configfs_dev_params,
2249 .get_device_type = sbc_get_device_type,
2250 .get_blocks = tcmu_get_blocks,
2251 .tb_dev_attrib_attrs = NULL,
2254 static void find_free_blocks(void)
2256 struct tcmu_dev *udev;
2258 u32 start, end, block, total_freed = 0;
2260 if (atomic_read(&global_db_count) <= tcmu_global_max_blocks)
2263 mutex_lock(&root_udev_mutex);
2264 list_for_each_entry(udev, &root_udev, node) {
2265 mutex_lock(&udev->cmdr_lock);
2267 /* Try to complete the finished commands first */
2268 tcmu_handle_completions(udev);
2270 /* Skip the udevs in idle */
2271 if (!udev->dbi_thresh) {
2272 mutex_unlock(&udev->cmdr_lock);
2276 end = udev->dbi_max + 1;
2277 block = find_last_bit(udev->data_bitmap, end);
2278 if (block == udev->dbi_max) {
2280 * The last bit is dbi_max, so it is not possible
2281 * reclaim any blocks.
2283 mutex_unlock(&udev->cmdr_lock);
2285 } else if (block == end) {
2286 /* The current udev will goto idle state */
2287 udev->dbi_thresh = start = 0;
2290 udev->dbi_thresh = start = block + 1;
2291 udev->dbi_max = block;
2294 /* Here will truncate the data area from off */
2295 off = udev->data_off + start * DATA_BLOCK_SIZE;
2296 unmap_mapping_range(udev->inode->i_mapping, off, 0, 1);
2298 /* Release the block pages */
2299 tcmu_blocks_release(&udev->data_blocks, start, end);
2300 mutex_unlock(&udev->cmdr_lock);
2302 total_freed += end - start;
2303 pr_debug("Freed %u blocks (total %u) from %s.\n", end - start,
2304 total_freed, udev->name);
2306 mutex_unlock(&root_udev_mutex);
2308 if (atomic_read(&global_db_count) > tcmu_global_max_blocks)
2309 schedule_delayed_work(&tcmu_unmap_work, msecs_to_jiffies(5000));
2312 static void check_timedout_devices(void)
2314 struct tcmu_dev *udev, *tmp_dev;
2317 spin_lock_bh(&timed_out_udevs_lock);
2318 list_splice_init(&timed_out_udevs, &devs);
2320 list_for_each_entry_safe(udev, tmp_dev, &devs, timedout_entry) {
2321 list_del_init(&udev->timedout_entry);
2322 spin_unlock_bh(&timed_out_udevs_lock);
2324 mutex_lock(&udev->cmdr_lock);
2325 idr_for_each(&udev->commands, tcmu_check_expired_cmd, NULL);
2326 mutex_unlock(&udev->cmdr_lock);
2328 spin_lock_bh(&timed_out_udevs_lock);
2331 spin_unlock_bh(&timed_out_udevs_lock);
2334 static void tcmu_unmap_work_fn(struct work_struct *work)
2336 check_timedout_devices();
2340 static int __init tcmu_module_init(void)
2342 int ret, i, k, len = 0;
2344 BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0);
2346 INIT_DELAYED_WORK(&tcmu_unmap_work, tcmu_unmap_work_fn);
2348 tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache",
2349 sizeof(struct tcmu_cmd),
2350 __alignof__(struct tcmu_cmd),
2352 if (!tcmu_cmd_cache)
2355 tcmu_root_device = root_device_register("tcm_user");
2356 if (IS_ERR(tcmu_root_device)) {
2357 ret = PTR_ERR(tcmu_root_device);
2358 goto out_free_cache;
2361 ret = genl_register_family(&tcmu_genl_family);
2363 goto out_unreg_device;
2366 for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) {
2367 len += sizeof(struct configfs_attribute *);
2369 for (i = 0; tcmu_attrib_attrs[i] != NULL; i++) {
2370 len += sizeof(struct configfs_attribute *);
2372 len += sizeof(struct configfs_attribute *);
2374 tcmu_attrs = kzalloc(len, GFP_KERNEL);
2377 goto out_unreg_genl;
2380 for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) {
2381 tcmu_attrs[i] = passthrough_attrib_attrs[i];
2383 for (k = 0; tcmu_attrib_attrs[k] != NULL; k++) {
2384 tcmu_attrs[i] = tcmu_attrib_attrs[k];
2387 tcmu_ops.tb_dev_attrib_attrs = tcmu_attrs;
2389 ret = transport_backend_register(&tcmu_ops);
2398 genl_unregister_family(&tcmu_genl_family);
2400 root_device_unregister(tcmu_root_device);
2402 kmem_cache_destroy(tcmu_cmd_cache);
2407 static void __exit tcmu_module_exit(void)
2409 cancel_delayed_work_sync(&tcmu_unmap_work);
2410 target_backend_unregister(&tcmu_ops);
2412 genl_unregister_family(&tcmu_genl_family);
2413 root_device_unregister(tcmu_root_device);
2414 kmem_cache_destroy(tcmu_cmd_cache);
2417 MODULE_DESCRIPTION("TCM USER subsystem plugin");
2418 MODULE_AUTHOR("Shaohua Li <shli@kernel.org>");
2419 MODULE_AUTHOR("Andy Grover <agrover@redhat.com>");
2420 MODULE_LICENSE("GPL");
2422 module_init(tcmu_module_init);
2423 module_exit(tcmu_module_exit);