1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 Shaohua Li <shli@kernel.org>
4 * Copyright (C) 2014 Red Hat, Inc.
5 * Copyright (C) 2015 Arrikto, Inc.
6 * Copyright (C) 2017 Chinamobile, Inc.
9 #include <linux/spinlock.h>
10 #include <linux/module.h>
11 #include <linux/idr.h>
12 #include <linux/kernel.h>
13 #include <linux/timer.h>
14 #include <linux/parser.h>
15 #include <linux/vmalloc.h>
16 #include <linux/uio_driver.h>
17 #include <linux/radix-tree.h>
18 #include <linux/stringify.h>
19 #include <linux/bitops.h>
20 #include <linux/highmem.h>
21 #include <linux/configfs.h>
22 #include <linux/mutex.h>
23 #include <linux/workqueue.h>
24 #include <net/genetlink.h>
25 #include <scsi/scsi_common.h>
26 #include <scsi/scsi_proto.h>
27 #include <target/target_core_base.h>
28 #include <target/target_core_fabric.h>
29 #include <target/target_core_backend.h>
31 #include <linux/target_core_user.h>
38 * Define a shared-memory interface for LIO to pass SCSI commands and
39 * data to userspace for processing. This is to allow backends that
40 * are too complex for in-kernel support to be possible.
42 * It uses the UIO framework to do a lot of the device-creation and
43 * introspection work for us.
45 * See the .h file for how the ring is laid out. Note that while the
46 * command ring is defined, the particulars of the data area are
47 * not. Offset values in the command entry point to other locations
48 * internal to the mmap-ed area. There is separate space outside the
49 * command ring for data buffers. This leaves maximum flexibility for
50 * moving buffer allocations, or even page flipping or other
51 * allocation techniques, without altering the command ring layout.
54 * The user process must be assumed to be malicious. There's no way to
55 * prevent it breaking the command ring protocol if it wants, but in
56 * order to prevent other issues we must only ever read *data* from
57 * the shared memory area, not offsets or sizes. This applies to
58 * command ring entries as well as the mailbox. Extra code needed for
59 * this may have a 'UAM' comment.
62 #define TCMU_TIME_OUT (30 * MSEC_PER_SEC)
64 /* For cmd area, the size is fixed 8MB */
65 #define CMDR_SIZE (8 * 1024 * 1024)
68 * For data area, the block size is PAGE_SIZE and
69 * the total size is 256K * PAGE_SIZE.
71 #define DATA_BLOCK_SIZE PAGE_SIZE
72 #define DATA_BLOCK_SHIFT PAGE_SHIFT
73 #define DATA_BLOCK_BITS_DEF (256 * 1024)
75 #define TCMU_MBS_TO_BLOCKS(_mbs) (_mbs << (20 - DATA_BLOCK_SHIFT))
76 #define TCMU_BLOCKS_TO_MBS(_blocks) (_blocks >> (20 - DATA_BLOCK_SHIFT))
79 * Default number of global data blocks(512K * PAGE_SIZE)
80 * when the unmap thread will be started.
82 #define TCMU_GLOBAL_MAX_BLOCKS_DEF (512 * 1024)
84 static u8 tcmu_kern_cmd_reply_supported
;
85 static u8 tcmu_netlink_blocked
;
87 static struct device
*tcmu_root_device
;
93 #define TCMU_CONFIG_LEN 256
95 static DEFINE_MUTEX(tcmu_nl_cmd_mutex
);
96 static LIST_HEAD(tcmu_nl_cmd_list
);
101 /* wake up thread waiting for reply */
102 struct completion complete
;
103 struct list_head nl_list
;
104 struct tcmu_dev
*udev
;
110 struct list_head node
;
113 struct se_device se_dev
;
118 #define TCMU_DEV_BIT_OPEN 0
119 #define TCMU_DEV_BIT_BROKEN 1
120 #define TCMU_DEV_BIT_BLOCKED 2
123 struct uio_info uio_info
;
127 struct tcmu_mailbox
*mb_addr
;
130 u32 cmdr_last_cleaned
;
131 /* Offset of data area from start of mb */
132 /* Must add data_off and mb_addr to get the address */
138 struct mutex cmdr_lock
;
139 struct list_head qfull_queue
;
143 unsigned long *data_bitmap
;
144 struct radix_tree_root data_blocks
;
148 struct timer_list cmd_timer
;
149 unsigned int cmd_time_out
;
150 struct list_head inflight_queue
;
152 struct timer_list qfull_timer
;
155 struct list_head timedout_entry
;
157 struct tcmu_nl_cmd curr_nl_cmd
;
159 char dev_config
[TCMU_CONFIG_LEN
];
161 int nl_reply_supported
;
164 #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev)
166 #define CMDR_OFF sizeof(struct tcmu_mailbox)
169 struct se_cmd
*se_cmd
;
170 struct tcmu_dev
*tcmu_dev
;
171 struct list_head queue_entry
;
175 /* Can't use se_cmd when cleaning up expired cmds, because if
176 cmd has been completed then accessing se_cmd is off limits */
181 unsigned long deadline
;
183 #define TCMU_CMD_BIT_EXPIRED 0
184 #define TCMU_CMD_BIT_INFLIGHT 1
188 * To avoid dead lock the mutex lock order should always be:
190 * mutex_lock(&root_udev_mutex);
192 * mutex_lock(&tcmu_dev->cmdr_lock);
193 * mutex_unlock(&tcmu_dev->cmdr_lock);
195 * mutex_unlock(&root_udev_mutex);
197 static DEFINE_MUTEX(root_udev_mutex
);
198 static LIST_HEAD(root_udev
);
200 static DEFINE_SPINLOCK(timed_out_udevs_lock
);
201 static LIST_HEAD(timed_out_udevs
);
203 static struct kmem_cache
*tcmu_cmd_cache
;
205 static atomic_t global_db_count
= ATOMIC_INIT(0);
206 static struct delayed_work tcmu_unmap_work
;
207 static int tcmu_global_max_blocks
= TCMU_GLOBAL_MAX_BLOCKS_DEF
;
209 static int tcmu_set_global_max_data_area(const char *str
,
210 const struct kernel_param
*kp
)
212 int ret
, max_area_mb
;
214 ret
= kstrtoint(str
, 10, &max_area_mb
);
218 if (max_area_mb
<= 0) {
219 pr_err("global_max_data_area must be larger than 0.\n");
223 tcmu_global_max_blocks
= TCMU_MBS_TO_BLOCKS(max_area_mb
);
224 if (atomic_read(&global_db_count
) > tcmu_global_max_blocks
)
225 schedule_delayed_work(&tcmu_unmap_work
, 0);
227 cancel_delayed_work_sync(&tcmu_unmap_work
);
232 static int tcmu_get_global_max_data_area(char *buffer
,
233 const struct kernel_param
*kp
)
235 return sprintf(buffer
, "%d", TCMU_BLOCKS_TO_MBS(tcmu_global_max_blocks
));
238 static const struct kernel_param_ops tcmu_global_max_data_area_op
= {
239 .set
= tcmu_set_global_max_data_area
,
240 .get
= tcmu_get_global_max_data_area
,
243 module_param_cb(global_max_data_area_mb
, &tcmu_global_max_data_area_op
, NULL
,
245 MODULE_PARM_DESC(global_max_data_area_mb
,
246 "Max MBs allowed to be allocated to all the tcmu device's "
249 static int tcmu_get_block_netlink(char *buffer
,
250 const struct kernel_param
*kp
)
252 return sprintf(buffer
, "%s\n", tcmu_netlink_blocked
?
253 "blocked" : "unblocked");
256 static int tcmu_set_block_netlink(const char *str
,
257 const struct kernel_param
*kp
)
262 ret
= kstrtou8(str
, 0, &val
);
267 pr_err("Invalid block netlink value %u\n", val
);
271 tcmu_netlink_blocked
= val
;
275 static const struct kernel_param_ops tcmu_block_netlink_op
= {
276 .set
= tcmu_set_block_netlink
,
277 .get
= tcmu_get_block_netlink
,
280 module_param_cb(block_netlink
, &tcmu_block_netlink_op
, NULL
, S_IWUSR
| S_IRUGO
);
281 MODULE_PARM_DESC(block_netlink
, "Block new netlink commands.");
283 static int tcmu_fail_netlink_cmd(struct tcmu_nl_cmd
*nl_cmd
)
285 struct tcmu_dev
*udev
= nl_cmd
->udev
;
287 if (!tcmu_netlink_blocked
) {
288 pr_err("Could not reset device's netlink interface. Netlink is not blocked.\n");
292 if (nl_cmd
->cmd
!= TCMU_CMD_UNSPEC
) {
293 pr_debug("Aborting nl cmd %d on %s\n", nl_cmd
->cmd
, udev
->name
);
294 nl_cmd
->status
= -EINTR
;
295 list_del(&nl_cmd
->nl_list
);
296 complete(&nl_cmd
->complete
);
301 static int tcmu_set_reset_netlink(const char *str
,
302 const struct kernel_param
*kp
)
304 struct tcmu_nl_cmd
*nl_cmd
, *tmp_cmd
;
308 ret
= kstrtou8(str
, 0, &val
);
313 pr_err("Invalid reset netlink value %u\n", val
);
317 mutex_lock(&tcmu_nl_cmd_mutex
);
318 list_for_each_entry_safe(nl_cmd
, tmp_cmd
, &tcmu_nl_cmd_list
, nl_list
) {
319 ret
= tcmu_fail_netlink_cmd(nl_cmd
);
323 mutex_unlock(&tcmu_nl_cmd_mutex
);
328 static const struct kernel_param_ops tcmu_reset_netlink_op
= {
329 .set
= tcmu_set_reset_netlink
,
332 module_param_cb(reset_netlink
, &tcmu_reset_netlink_op
, NULL
, S_IWUSR
);
333 MODULE_PARM_DESC(reset_netlink
, "Reset netlink commands.");
335 /* multicast group */
336 enum tcmu_multicast_groups
{
340 static const struct genl_multicast_group tcmu_mcgrps
[] = {
341 [TCMU_MCGRP_CONFIG
] = { .name
= "config", },
344 static struct nla_policy tcmu_attr_policy
[TCMU_ATTR_MAX
+1] = {
345 [TCMU_ATTR_DEVICE
] = { .type
= NLA_STRING
},
346 [TCMU_ATTR_MINOR
] = { .type
= NLA_U32
},
347 [TCMU_ATTR_CMD_STATUS
] = { .type
= NLA_S32
},
348 [TCMU_ATTR_DEVICE_ID
] = { .type
= NLA_U32
},
349 [TCMU_ATTR_SUPP_KERN_CMD_REPLY
] = { .type
= NLA_U8
},
352 static int tcmu_genl_cmd_done(struct genl_info
*info
, int completed_cmd
)
354 struct tcmu_dev
*udev
= NULL
;
355 struct tcmu_nl_cmd
*nl_cmd
;
356 int dev_id
, rc
, ret
= 0;
358 if (!info
->attrs
[TCMU_ATTR_CMD_STATUS
] ||
359 !info
->attrs
[TCMU_ATTR_DEVICE_ID
]) {
360 printk(KERN_ERR
"TCMU_ATTR_CMD_STATUS or TCMU_ATTR_DEVICE_ID not set, doing nothing\n");
364 dev_id
= nla_get_u32(info
->attrs
[TCMU_ATTR_DEVICE_ID
]);
365 rc
= nla_get_s32(info
->attrs
[TCMU_ATTR_CMD_STATUS
]);
367 mutex_lock(&tcmu_nl_cmd_mutex
);
368 list_for_each_entry(nl_cmd
, &tcmu_nl_cmd_list
, nl_list
) {
369 if (nl_cmd
->udev
->se_dev
.dev_index
== dev_id
) {
376 pr_err("tcmu nl cmd %u/%d completion could not find device with dev id %u.\n",
377 completed_cmd
, rc
, dev_id
);
381 list_del(&nl_cmd
->nl_list
);
383 pr_debug("%s genl cmd done got id %d curr %d done %d rc %d stat %d\n",
384 udev
->name
, dev_id
, nl_cmd
->cmd
, completed_cmd
, rc
,
387 if (nl_cmd
->cmd
!= completed_cmd
) {
388 pr_err("Mismatched commands on %s (Expecting reply for %d. Current %d).\n",
389 udev
->name
, completed_cmd
, nl_cmd
->cmd
);
395 complete(&nl_cmd
->complete
);
397 mutex_unlock(&tcmu_nl_cmd_mutex
);
401 static int tcmu_genl_rm_dev_done(struct sk_buff
*skb
, struct genl_info
*info
)
403 return tcmu_genl_cmd_done(info
, TCMU_CMD_REMOVED_DEVICE
);
406 static int tcmu_genl_add_dev_done(struct sk_buff
*skb
, struct genl_info
*info
)
408 return tcmu_genl_cmd_done(info
, TCMU_CMD_ADDED_DEVICE
);
411 static int tcmu_genl_reconfig_dev_done(struct sk_buff
*skb
,
412 struct genl_info
*info
)
414 return tcmu_genl_cmd_done(info
, TCMU_CMD_RECONFIG_DEVICE
);
417 static int tcmu_genl_set_features(struct sk_buff
*skb
, struct genl_info
*info
)
419 if (info
->attrs
[TCMU_ATTR_SUPP_KERN_CMD_REPLY
]) {
420 tcmu_kern_cmd_reply_supported
=
421 nla_get_u8(info
->attrs
[TCMU_ATTR_SUPP_KERN_CMD_REPLY
]);
422 printk(KERN_INFO
"tcmu daemon: command reply support %u.\n",
423 tcmu_kern_cmd_reply_supported
);
429 static const struct genl_ops tcmu_genl_ops
[] = {
431 .cmd
= TCMU_CMD_SET_FEATURES
,
432 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
433 .flags
= GENL_ADMIN_PERM
,
434 .doit
= tcmu_genl_set_features
,
437 .cmd
= TCMU_CMD_ADDED_DEVICE_DONE
,
438 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
439 .flags
= GENL_ADMIN_PERM
,
440 .doit
= tcmu_genl_add_dev_done
,
443 .cmd
= TCMU_CMD_REMOVED_DEVICE_DONE
,
444 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
445 .flags
= GENL_ADMIN_PERM
,
446 .doit
= tcmu_genl_rm_dev_done
,
449 .cmd
= TCMU_CMD_RECONFIG_DEVICE_DONE
,
450 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
451 .flags
= GENL_ADMIN_PERM
,
452 .doit
= tcmu_genl_reconfig_dev_done
,
456 /* Our generic netlink family */
457 static struct genl_family tcmu_genl_family __ro_after_init
= {
458 .module
= THIS_MODULE
,
462 .maxattr
= TCMU_ATTR_MAX
,
463 .policy
= tcmu_attr_policy
,
464 .mcgrps
= tcmu_mcgrps
,
465 .n_mcgrps
= ARRAY_SIZE(tcmu_mcgrps
),
467 .ops
= tcmu_genl_ops
,
468 .n_ops
= ARRAY_SIZE(tcmu_genl_ops
),
471 #define tcmu_cmd_set_dbi_cur(cmd, index) ((cmd)->dbi_cur = (index))
472 #define tcmu_cmd_reset_dbi_cur(cmd) tcmu_cmd_set_dbi_cur(cmd, 0)
473 #define tcmu_cmd_set_dbi(cmd, index) ((cmd)->dbi[(cmd)->dbi_cur++] = (index))
474 #define tcmu_cmd_get_dbi(cmd) ((cmd)->dbi[(cmd)->dbi_cur++])
476 static void tcmu_cmd_free_data(struct tcmu_cmd
*tcmu_cmd
, uint32_t len
)
478 struct tcmu_dev
*udev
= tcmu_cmd
->tcmu_dev
;
481 for (i
= 0; i
< len
; i
++)
482 clear_bit(tcmu_cmd
->dbi
[i
], udev
->data_bitmap
);
485 static inline bool tcmu_get_empty_block(struct tcmu_dev
*udev
,
486 struct tcmu_cmd
*tcmu_cmd
)
491 dbi
= find_first_zero_bit(udev
->data_bitmap
, udev
->dbi_thresh
);
492 if (dbi
== udev
->dbi_thresh
)
495 page
= radix_tree_lookup(&udev
->data_blocks
, dbi
);
497 if (atomic_add_return(1, &global_db_count
) >
498 tcmu_global_max_blocks
)
499 schedule_delayed_work(&tcmu_unmap_work
, 0);
501 /* try to get new page from the mm */
502 page
= alloc_page(GFP_NOIO
);
506 ret
= radix_tree_insert(&udev
->data_blocks
, dbi
, page
);
511 if (dbi
> udev
->dbi_max
)
514 set_bit(dbi
, udev
->data_bitmap
);
515 tcmu_cmd_set_dbi(tcmu_cmd
, dbi
);
521 atomic_dec(&global_db_count
);
525 static bool tcmu_get_empty_blocks(struct tcmu_dev
*udev
,
526 struct tcmu_cmd
*tcmu_cmd
)
530 for (i
= tcmu_cmd
->dbi_cur
; i
< tcmu_cmd
->dbi_cnt
; i
++) {
531 if (!tcmu_get_empty_block(udev
, tcmu_cmd
))
537 static inline struct page
*
538 tcmu_get_block_page(struct tcmu_dev
*udev
, uint32_t dbi
)
540 return radix_tree_lookup(&udev
->data_blocks
, dbi
);
543 static inline void tcmu_free_cmd(struct tcmu_cmd
*tcmu_cmd
)
545 kfree(tcmu_cmd
->dbi
);
546 kmem_cache_free(tcmu_cmd_cache
, tcmu_cmd
);
549 static inline size_t tcmu_cmd_get_data_length(struct tcmu_cmd
*tcmu_cmd
)
551 struct se_cmd
*se_cmd
= tcmu_cmd
->se_cmd
;
552 size_t data_length
= round_up(se_cmd
->data_length
, DATA_BLOCK_SIZE
);
554 if (se_cmd
->se_cmd_flags
& SCF_BIDI
) {
555 BUG_ON(!(se_cmd
->t_bidi_data_sg
&& se_cmd
->t_bidi_data_nents
));
556 data_length
+= round_up(se_cmd
->t_bidi_data_sg
->length
,
563 static inline uint32_t tcmu_cmd_get_block_cnt(struct tcmu_cmd
*tcmu_cmd
)
565 size_t data_length
= tcmu_cmd_get_data_length(tcmu_cmd
);
567 return data_length
/ DATA_BLOCK_SIZE
;
570 static struct tcmu_cmd
*tcmu_alloc_cmd(struct se_cmd
*se_cmd
)
572 struct se_device
*se_dev
= se_cmd
->se_dev
;
573 struct tcmu_dev
*udev
= TCMU_DEV(se_dev
);
574 struct tcmu_cmd
*tcmu_cmd
;
576 tcmu_cmd
= kmem_cache_zalloc(tcmu_cmd_cache
, GFP_NOIO
);
580 INIT_LIST_HEAD(&tcmu_cmd
->queue_entry
);
581 tcmu_cmd
->se_cmd
= se_cmd
;
582 tcmu_cmd
->tcmu_dev
= udev
;
584 tcmu_cmd_reset_dbi_cur(tcmu_cmd
);
585 tcmu_cmd
->dbi_cnt
= tcmu_cmd_get_block_cnt(tcmu_cmd
);
586 tcmu_cmd
->dbi
= kcalloc(tcmu_cmd
->dbi_cnt
, sizeof(uint32_t),
588 if (!tcmu_cmd
->dbi
) {
589 kmem_cache_free(tcmu_cmd_cache
, tcmu_cmd
);
596 static inline void tcmu_flush_dcache_range(void *vaddr
, size_t size
)
598 unsigned long offset
= offset_in_page(vaddr
);
599 void *start
= vaddr
- offset
;
601 size
= round_up(size
+offset
, PAGE_SIZE
);
604 flush_dcache_page(virt_to_page(start
));
611 * Some ring helper functions. We don't assume size is a power of 2 so
612 * we can't use circ_buf.h.
614 static inline size_t spc_used(size_t head
, size_t tail
, size_t size
)
616 int diff
= head
- tail
;
624 static inline size_t spc_free(size_t head
, size_t tail
, size_t size
)
626 /* Keep 1 byte unused or we can't tell full from empty */
627 return (size
- spc_used(head
, tail
, size
) - 1);
630 static inline size_t head_to_end(size_t head
, size_t size
)
635 static inline void new_iov(struct iovec
**iov
, int *iov_cnt
)
644 memset(iovec
, 0, sizeof(struct iovec
));
647 #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size)
649 /* offset is relative to mb_addr */
650 static inline size_t get_block_offset_user(struct tcmu_dev
*dev
,
651 int dbi
, int remaining
)
653 return dev
->data_off
+ dbi
* DATA_BLOCK_SIZE
+
654 DATA_BLOCK_SIZE
- remaining
;
657 static inline size_t iov_tail(struct iovec
*iov
)
659 return (size_t)iov
->iov_base
+ iov
->iov_len
;
662 static void scatter_data_area(struct tcmu_dev
*udev
,
663 struct tcmu_cmd
*tcmu_cmd
, struct scatterlist
*data_sg
,
664 unsigned int data_nents
, struct iovec
**iov
,
665 int *iov_cnt
, bool copy_data
)
668 int block_remaining
= 0;
669 void *from
, *to
= NULL
;
670 size_t copy_bytes
, to_offset
, offset
;
671 struct scatterlist
*sg
;
674 for_each_sg(data_sg
, sg
, data_nents
, i
) {
675 int sg_remaining
= sg
->length
;
676 from
= kmap_atomic(sg_page(sg
)) + sg
->offset
;
677 while (sg_remaining
> 0) {
678 if (block_remaining
== 0) {
682 block_remaining
= DATA_BLOCK_SIZE
;
683 dbi
= tcmu_cmd_get_dbi(tcmu_cmd
);
684 page
= tcmu_get_block_page(udev
, dbi
);
685 to
= kmap_atomic(page
);
689 * Covert to virtual offset of the ring data area.
691 to_offset
= get_block_offset_user(udev
, dbi
,
695 * The following code will gather and map the blocks
696 * to the same iovec when the blocks are all next to
699 copy_bytes
= min_t(size_t, sg_remaining
,
702 to_offset
== iov_tail(*iov
)) {
704 * Will append to the current iovec, because
705 * the current block page is next to the
708 (*iov
)->iov_len
+= copy_bytes
;
711 * Will allocate a new iovec because we are
712 * first time here or the current block page
713 * is not next to the previous one.
715 new_iov(iov
, iov_cnt
);
716 (*iov
)->iov_base
= (void __user
*)to_offset
;
717 (*iov
)->iov_len
= copy_bytes
;
721 offset
= DATA_BLOCK_SIZE
- block_remaining
;
723 from
+ sg
->length
- sg_remaining
,
725 tcmu_flush_dcache_range(to
, copy_bytes
);
728 sg_remaining
-= copy_bytes
;
729 block_remaining
-= copy_bytes
;
731 kunmap_atomic(from
- sg
->offset
);
738 static void gather_data_area(struct tcmu_dev
*udev
, struct tcmu_cmd
*cmd
,
739 bool bidi
, uint32_t read_len
)
741 struct se_cmd
*se_cmd
= cmd
->se_cmd
;
743 int block_remaining
= 0;
744 void *from
= NULL
, *to
;
745 size_t copy_bytes
, offset
;
746 struct scatterlist
*sg
, *data_sg
;
748 unsigned int data_nents
;
752 data_sg
= se_cmd
->t_data_sg
;
753 data_nents
= se_cmd
->t_data_nents
;
757 * For bidi case, the first count blocks are for Data-Out
758 * buffer blocks, and before gathering the Data-In buffer
759 * the Data-Out buffer blocks should be discarded.
761 count
= DIV_ROUND_UP(se_cmd
->data_length
, DATA_BLOCK_SIZE
);
763 data_sg
= se_cmd
->t_bidi_data_sg
;
764 data_nents
= se_cmd
->t_bidi_data_nents
;
767 tcmu_cmd_set_dbi_cur(cmd
, count
);
769 for_each_sg(data_sg
, sg
, data_nents
, i
) {
770 int sg_remaining
= sg
->length
;
771 to
= kmap_atomic(sg_page(sg
)) + sg
->offset
;
772 while (sg_remaining
> 0 && read_len
> 0) {
773 if (block_remaining
== 0) {
777 block_remaining
= DATA_BLOCK_SIZE
;
778 dbi
= tcmu_cmd_get_dbi(cmd
);
779 page
= tcmu_get_block_page(udev
, dbi
);
780 from
= kmap_atomic(page
);
782 copy_bytes
= min_t(size_t, sg_remaining
,
784 if (read_len
< copy_bytes
)
785 copy_bytes
= read_len
;
786 offset
= DATA_BLOCK_SIZE
- block_remaining
;
787 tcmu_flush_dcache_range(from
, copy_bytes
);
788 memcpy(to
+ sg
->length
- sg_remaining
, from
+ offset
,
791 sg_remaining
-= copy_bytes
;
792 block_remaining
-= copy_bytes
;
793 read_len
-= copy_bytes
;
795 kunmap_atomic(to
- sg
->offset
);
803 static inline size_t spc_bitmap_free(unsigned long *bitmap
, uint32_t thresh
)
805 return thresh
- bitmap_weight(bitmap
, thresh
);
809 * We can't queue a command until we have space available on the cmd ring *and*
810 * space available on the data area.
812 * Called with ring lock held.
814 static bool is_ring_space_avail(struct tcmu_dev
*udev
, struct tcmu_cmd
*cmd
,
815 size_t cmd_size
, size_t data_needed
)
817 struct tcmu_mailbox
*mb
= udev
->mb_addr
;
818 uint32_t blocks_needed
= (data_needed
+ DATA_BLOCK_SIZE
- 1)
820 size_t space
, cmd_needed
;
823 tcmu_flush_dcache_range(mb
, sizeof(*mb
));
825 cmd_head
= mb
->cmd_head
% udev
->cmdr_size
; /* UAM */
828 * If cmd end-of-ring space is too small then we need space for a NOP plus
829 * original cmd - cmds are internally contiguous.
831 if (head_to_end(cmd_head
, udev
->cmdr_size
) >= cmd_size
)
832 cmd_needed
= cmd_size
;
834 cmd_needed
= cmd_size
+ head_to_end(cmd_head
, udev
->cmdr_size
);
836 space
= spc_free(cmd_head
, udev
->cmdr_last_cleaned
, udev
->cmdr_size
);
837 if (space
< cmd_needed
) {
838 pr_debug("no cmd space: %u %u %u\n", cmd_head
,
839 udev
->cmdr_last_cleaned
, udev
->cmdr_size
);
843 /* try to check and get the data blocks as needed */
844 space
= spc_bitmap_free(udev
->data_bitmap
, udev
->dbi_thresh
);
845 if ((space
* DATA_BLOCK_SIZE
) < data_needed
) {
846 unsigned long blocks_left
=
847 (udev
->max_blocks
- udev
->dbi_thresh
) + space
;
849 if (blocks_left
< blocks_needed
) {
850 pr_debug("no data space: only %lu available, but ask for %zu\n",
851 blocks_left
* DATA_BLOCK_SIZE
,
856 udev
->dbi_thresh
+= blocks_needed
;
857 if (udev
->dbi_thresh
> udev
->max_blocks
)
858 udev
->dbi_thresh
= udev
->max_blocks
;
861 return tcmu_get_empty_blocks(udev
, cmd
);
864 static inline size_t tcmu_cmd_get_base_cmd_size(size_t iov_cnt
)
866 return max(offsetof(struct tcmu_cmd_entry
, req
.iov
[iov_cnt
]),
867 sizeof(struct tcmu_cmd_entry
));
870 static inline size_t tcmu_cmd_get_cmd_size(struct tcmu_cmd
*tcmu_cmd
,
871 size_t base_command_size
)
873 struct se_cmd
*se_cmd
= tcmu_cmd
->se_cmd
;
876 command_size
= base_command_size
+
877 round_up(scsi_command_size(se_cmd
->t_task_cdb
),
880 WARN_ON(command_size
& (TCMU_OP_ALIGN_SIZE
-1));
885 static int tcmu_setup_cmd_timer(struct tcmu_cmd
*tcmu_cmd
, unsigned int tmo
,
886 struct timer_list
*timer
)
888 struct tcmu_dev
*udev
= tcmu_cmd
->tcmu_dev
;
891 if (tcmu_cmd
->cmd_id
)
894 cmd_id
= idr_alloc(&udev
->commands
, tcmu_cmd
, 1, USHRT_MAX
, GFP_NOWAIT
);
896 pr_err("tcmu: Could not allocate cmd id.\n");
899 tcmu_cmd
->cmd_id
= cmd_id
;
901 pr_debug("allocated cmd %u for dev %s tmo %lu\n", tcmu_cmd
->cmd_id
,
902 udev
->name
, tmo
/ MSEC_PER_SEC
);
908 tcmu_cmd
->deadline
= round_jiffies_up(jiffies
+ msecs_to_jiffies(tmo
));
909 if (!timer_pending(timer
))
910 mod_timer(timer
, tcmu_cmd
->deadline
);
915 static int add_to_qfull_queue(struct tcmu_cmd
*tcmu_cmd
)
917 struct tcmu_dev
*udev
= tcmu_cmd
->tcmu_dev
;
922 * For backwards compat if qfull_time_out is not set use
923 * cmd_time_out and if that's not set use the default time out.
925 if (!udev
->qfull_time_out
)
927 else if (udev
->qfull_time_out
> 0)
928 tmo
= udev
->qfull_time_out
;
929 else if (udev
->cmd_time_out
)
930 tmo
= udev
->cmd_time_out
;
934 ret
= tcmu_setup_cmd_timer(tcmu_cmd
, tmo
, &udev
->qfull_timer
);
938 list_add_tail(&tcmu_cmd
->queue_entry
, &udev
->qfull_queue
);
939 pr_debug("adding cmd %u on dev %s to ring space wait queue\n",
940 tcmu_cmd
->cmd_id
, udev
->name
);
945 * queue_cmd_ring - queue cmd to ring or internally
946 * @tcmu_cmd: cmd to queue
947 * @scsi_err: TCM error code if failure (-1) returned.
950 * -1 we cannot queue internally or to the ring.
952 * 1 internally queued to wait for ring memory to free.
954 static int queue_cmd_ring(struct tcmu_cmd
*tcmu_cmd
, sense_reason_t
*scsi_err
)
956 struct tcmu_dev
*udev
= tcmu_cmd
->tcmu_dev
;
957 struct se_cmd
*se_cmd
= tcmu_cmd
->se_cmd
;
958 size_t base_command_size
, command_size
;
959 struct tcmu_mailbox
*mb
;
960 struct tcmu_cmd_entry
*entry
;
965 bool copy_to_data_area
;
966 size_t data_length
= tcmu_cmd_get_data_length(tcmu_cmd
);
968 *scsi_err
= TCM_NO_SENSE
;
970 if (test_bit(TCMU_DEV_BIT_BLOCKED
, &udev
->flags
)) {
971 *scsi_err
= TCM_LUN_BUSY
;
975 if (test_bit(TCMU_DEV_BIT_BROKEN
, &udev
->flags
)) {
976 *scsi_err
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
981 * Must be a certain minimum size for response sense info, but
982 * also may be larger if the iov array is large.
984 * We prepare as many iovs as possbile for potential uses here,
985 * because it's expensive to tell how many regions are freed in
986 * the bitmap & global data pool, as the size calculated here
987 * will only be used to do the checks.
989 * The size will be recalculated later as actually needed to save
992 base_command_size
= tcmu_cmd_get_base_cmd_size(tcmu_cmd
->dbi_cnt
);
993 command_size
= tcmu_cmd_get_cmd_size(tcmu_cmd
, base_command_size
);
995 if (!list_empty(&udev
->qfull_queue
))
999 cmd_head
= mb
->cmd_head
% udev
->cmdr_size
; /* UAM */
1000 if ((command_size
> (udev
->cmdr_size
/ 2)) ||
1001 data_length
> udev
->data_size
) {
1002 pr_warn("TCMU: Request of size %zu/%zu is too big for %u/%zu "
1003 "cmd ring/data area\n", command_size
, data_length
,
1004 udev
->cmdr_size
, udev
->data_size
);
1005 *scsi_err
= TCM_INVALID_CDB_FIELD
;
1009 if (!is_ring_space_avail(udev
, tcmu_cmd
, command_size
, data_length
)) {
1011 * Don't leave commands partially setup because the unmap
1012 * thread might need the blocks to make forward progress.
1014 tcmu_cmd_free_data(tcmu_cmd
, tcmu_cmd
->dbi_cur
);
1015 tcmu_cmd_reset_dbi_cur(tcmu_cmd
);
1019 /* Insert a PAD if end-of-ring space is too small */
1020 if (head_to_end(cmd_head
, udev
->cmdr_size
) < command_size
) {
1021 size_t pad_size
= head_to_end(cmd_head
, udev
->cmdr_size
);
1023 entry
= (void *) mb
+ CMDR_OFF
+ cmd_head
;
1024 tcmu_hdr_set_op(&entry
->hdr
.len_op
, TCMU_OP_PAD
);
1025 tcmu_hdr_set_len(&entry
->hdr
.len_op
, pad_size
);
1026 entry
->hdr
.cmd_id
= 0; /* not used for PAD */
1027 entry
->hdr
.kflags
= 0;
1028 entry
->hdr
.uflags
= 0;
1029 tcmu_flush_dcache_range(entry
, sizeof(*entry
));
1031 UPDATE_HEAD(mb
->cmd_head
, pad_size
, udev
->cmdr_size
);
1032 tcmu_flush_dcache_range(mb
, sizeof(*mb
));
1034 cmd_head
= mb
->cmd_head
% udev
->cmdr_size
; /* UAM */
1035 WARN_ON(cmd_head
!= 0);
1038 entry
= (void *) mb
+ CMDR_OFF
+ cmd_head
;
1039 memset(entry
, 0, command_size
);
1040 tcmu_hdr_set_op(&entry
->hdr
.len_op
, TCMU_OP_CMD
);
1042 /* Handle allocating space from the data area */
1043 tcmu_cmd_reset_dbi_cur(tcmu_cmd
);
1044 iov
= &entry
->req
.iov
[0];
1046 copy_to_data_area
= (se_cmd
->data_direction
== DMA_TO_DEVICE
1047 || se_cmd
->se_cmd_flags
& SCF_BIDI
);
1048 scatter_data_area(udev
, tcmu_cmd
, se_cmd
->t_data_sg
,
1049 se_cmd
->t_data_nents
, &iov
, &iov_cnt
,
1051 entry
->req
.iov_cnt
= iov_cnt
;
1053 /* Handle BIDI commands */
1055 if (se_cmd
->se_cmd_flags
& SCF_BIDI
) {
1057 scatter_data_area(udev
, tcmu_cmd
, se_cmd
->t_bidi_data_sg
,
1058 se_cmd
->t_bidi_data_nents
, &iov
, &iov_cnt
,
1061 entry
->req
.iov_bidi_cnt
= iov_cnt
;
1063 ret
= tcmu_setup_cmd_timer(tcmu_cmd
, udev
->cmd_time_out
,
1066 tcmu_cmd_free_data(tcmu_cmd
, tcmu_cmd
->dbi_cnt
);
1068 *scsi_err
= TCM_OUT_OF_RESOURCES
;
1071 entry
->hdr
.cmd_id
= tcmu_cmd
->cmd_id
;
1074 * Recalaulate the command's base size and size according
1075 * to the actual needs
1077 base_command_size
= tcmu_cmd_get_base_cmd_size(entry
->req
.iov_cnt
+
1078 entry
->req
.iov_bidi_cnt
);
1079 command_size
= tcmu_cmd_get_cmd_size(tcmu_cmd
, base_command_size
);
1081 tcmu_hdr_set_len(&entry
->hdr
.len_op
, command_size
);
1083 /* All offsets relative to mb_addr, not start of entry! */
1084 cdb_off
= CMDR_OFF
+ cmd_head
+ base_command_size
;
1085 memcpy((void *) mb
+ cdb_off
, se_cmd
->t_task_cdb
, scsi_command_size(se_cmd
->t_task_cdb
));
1086 entry
->req
.cdb_off
= cdb_off
;
1087 tcmu_flush_dcache_range(entry
, sizeof(*entry
));
1089 UPDATE_HEAD(mb
->cmd_head
, command_size
, udev
->cmdr_size
);
1090 tcmu_flush_dcache_range(mb
, sizeof(*mb
));
1092 list_add_tail(&tcmu_cmd
->queue_entry
, &udev
->inflight_queue
);
1093 set_bit(TCMU_CMD_BIT_INFLIGHT
, &tcmu_cmd
->flags
);
1095 /* TODO: only if FLUSH and FUA? */
1096 uio_event_notify(&udev
->uio_info
);
1101 if (add_to_qfull_queue(tcmu_cmd
)) {
1102 *scsi_err
= TCM_OUT_OF_RESOURCES
;
1109 static sense_reason_t
1110 tcmu_queue_cmd(struct se_cmd
*se_cmd
)
1112 struct se_device
*se_dev
= se_cmd
->se_dev
;
1113 struct tcmu_dev
*udev
= TCMU_DEV(se_dev
);
1114 struct tcmu_cmd
*tcmu_cmd
;
1115 sense_reason_t scsi_ret
;
1118 tcmu_cmd
= tcmu_alloc_cmd(se_cmd
);
1120 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1122 mutex_lock(&udev
->cmdr_lock
);
1123 ret
= queue_cmd_ring(tcmu_cmd
, &scsi_ret
);
1124 mutex_unlock(&udev
->cmdr_lock
);
1126 tcmu_free_cmd(tcmu_cmd
);
1130 static void tcmu_handle_completion(struct tcmu_cmd
*cmd
, struct tcmu_cmd_entry
*entry
)
1132 struct se_cmd
*se_cmd
= cmd
->se_cmd
;
1133 struct tcmu_dev
*udev
= cmd
->tcmu_dev
;
1134 bool read_len_valid
= false;
1138 * cmd has been completed already from timeout, just reclaim
1139 * data area space and free cmd
1141 if (test_bit(TCMU_CMD_BIT_EXPIRED
, &cmd
->flags
)) {
1142 WARN_ON_ONCE(se_cmd
);
1146 list_del_init(&cmd
->queue_entry
);
1148 tcmu_cmd_reset_dbi_cur(cmd
);
1150 if (entry
->hdr
.uflags
& TCMU_UFLAG_UNKNOWN_OP
) {
1151 pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n",
1153 entry
->rsp
.scsi_status
= SAM_STAT_CHECK_CONDITION
;
1157 read_len
= se_cmd
->data_length
;
1158 if (se_cmd
->data_direction
== DMA_FROM_DEVICE
&&
1159 (entry
->hdr
.uflags
& TCMU_UFLAG_READ_LEN
) && entry
->rsp
.read_len
) {
1160 read_len_valid
= true;
1161 if (entry
->rsp
.read_len
< read_len
)
1162 read_len
= entry
->rsp
.read_len
;
1165 if (entry
->rsp
.scsi_status
== SAM_STAT_CHECK_CONDITION
) {
1166 transport_copy_sense_to_cmd(se_cmd
, entry
->rsp
.sense_buffer
);
1167 if (!read_len_valid
)
1170 se_cmd
->se_cmd_flags
|= SCF_TREAT_READ_AS_NORMAL
;
1172 if (se_cmd
->se_cmd_flags
& SCF_BIDI
) {
1173 /* Get Data-In buffer before clean up */
1174 gather_data_area(udev
, cmd
, true, read_len
);
1175 } else if (se_cmd
->data_direction
== DMA_FROM_DEVICE
) {
1176 gather_data_area(udev
, cmd
, false, read_len
);
1177 } else if (se_cmd
->data_direction
== DMA_TO_DEVICE
) {
1179 } else if (se_cmd
->data_direction
!= DMA_NONE
) {
1180 pr_warn("TCMU: data direction was %d!\n",
1181 se_cmd
->data_direction
);
1185 if (read_len_valid
) {
1186 pr_debug("read_len = %d\n", read_len
);
1187 target_complete_cmd_with_length(cmd
->se_cmd
,
1188 entry
->rsp
.scsi_status
, read_len
);
1190 target_complete_cmd(cmd
->se_cmd
, entry
->rsp
.scsi_status
);
1194 tcmu_cmd_free_data(cmd
, cmd
->dbi_cnt
);
1198 static void tcmu_set_next_deadline(struct list_head
*queue
,
1199 struct timer_list
*timer
)
1201 struct tcmu_cmd
*tcmu_cmd
, *tmp_cmd
;
1202 unsigned long deadline
= 0;
1204 list_for_each_entry_safe(tcmu_cmd
, tmp_cmd
, queue
, queue_entry
) {
1205 if (!time_after(jiffies
, tcmu_cmd
->deadline
)) {
1206 deadline
= tcmu_cmd
->deadline
;
1212 mod_timer(timer
, deadline
);
1217 static unsigned int tcmu_handle_completions(struct tcmu_dev
*udev
)
1219 struct tcmu_mailbox
*mb
;
1220 struct tcmu_cmd
*cmd
;
1223 if (test_bit(TCMU_DEV_BIT_BROKEN
, &udev
->flags
)) {
1224 pr_err("ring broken, not handling completions\n");
1229 tcmu_flush_dcache_range(mb
, sizeof(*mb
));
1231 while (udev
->cmdr_last_cleaned
!= READ_ONCE(mb
->cmd_tail
)) {
1233 struct tcmu_cmd_entry
*entry
= (void *) mb
+ CMDR_OFF
+ udev
->cmdr_last_cleaned
;
1235 tcmu_flush_dcache_range(entry
, sizeof(*entry
));
1237 if (tcmu_hdr_get_op(entry
->hdr
.len_op
) == TCMU_OP_PAD
) {
1238 UPDATE_HEAD(udev
->cmdr_last_cleaned
,
1239 tcmu_hdr_get_len(entry
->hdr
.len_op
),
1243 WARN_ON(tcmu_hdr_get_op(entry
->hdr
.len_op
) != TCMU_OP_CMD
);
1245 cmd
= idr_remove(&udev
->commands
, entry
->hdr
.cmd_id
);
1247 pr_err("cmd_id %u not found, ring is broken\n",
1249 set_bit(TCMU_DEV_BIT_BROKEN
, &udev
->flags
);
1253 tcmu_handle_completion(cmd
, entry
);
1255 UPDATE_HEAD(udev
->cmdr_last_cleaned
,
1256 tcmu_hdr_get_len(entry
->hdr
.len_op
),
1262 if (mb
->cmd_tail
== mb
->cmd_head
) {
1263 /* no more pending commands */
1264 del_timer(&udev
->cmd_timer
);
1266 if (list_empty(&udev
->qfull_queue
)) {
1268 * no more pending or waiting commands so try to
1269 * reclaim blocks if needed.
1271 if (atomic_read(&global_db_count
) >
1272 tcmu_global_max_blocks
)
1273 schedule_delayed_work(&tcmu_unmap_work
, 0);
1275 } else if (udev
->cmd_time_out
) {
1276 tcmu_set_next_deadline(&udev
->inflight_queue
, &udev
->cmd_timer
);
1282 static int tcmu_check_expired_cmd(int id
, void *p
, void *data
)
1284 struct tcmu_cmd
*cmd
= p
;
1285 struct tcmu_dev
*udev
= cmd
->tcmu_dev
;
1287 struct se_cmd
*se_cmd
;
1290 if (test_bit(TCMU_CMD_BIT_EXPIRED
, &cmd
->flags
))
1293 if (!time_after(jiffies
, cmd
->deadline
))
1296 is_running
= test_bit(TCMU_CMD_BIT_INFLIGHT
, &cmd
->flags
);
1297 se_cmd
= cmd
->se_cmd
;
1301 * If cmd_time_out is disabled but qfull is set deadline
1302 * will only reflect the qfull timeout. Ignore it.
1304 if (!udev
->cmd_time_out
)
1307 set_bit(TCMU_CMD_BIT_EXPIRED
, &cmd
->flags
);
1309 * target_complete_cmd will translate this to LUN COMM FAILURE
1311 scsi_status
= SAM_STAT_CHECK_CONDITION
;
1312 list_del_init(&cmd
->queue_entry
);
1315 list_del_init(&cmd
->queue_entry
);
1316 idr_remove(&udev
->commands
, id
);
1318 scsi_status
= SAM_STAT_TASK_SET_FULL
;
1321 pr_debug("Timing out cmd %u on dev %s that is %s.\n",
1322 id
, udev
->name
, is_running
? "inflight" : "queued");
1324 target_complete_cmd(se_cmd
, scsi_status
);
1328 static void tcmu_device_timedout(struct tcmu_dev
*udev
)
1330 spin_lock(&timed_out_udevs_lock
);
1331 if (list_empty(&udev
->timedout_entry
))
1332 list_add_tail(&udev
->timedout_entry
, &timed_out_udevs
);
1333 spin_unlock(&timed_out_udevs_lock
);
1335 schedule_delayed_work(&tcmu_unmap_work
, 0);
1338 static void tcmu_cmd_timedout(struct timer_list
*t
)
1340 struct tcmu_dev
*udev
= from_timer(udev
, t
, cmd_timer
);
1342 pr_debug("%s cmd timeout has expired\n", udev
->name
);
1343 tcmu_device_timedout(udev
);
1346 static void tcmu_qfull_timedout(struct timer_list
*t
)
1348 struct tcmu_dev
*udev
= from_timer(udev
, t
, qfull_timer
);
1350 pr_debug("%s qfull timeout has expired\n", udev
->name
);
1351 tcmu_device_timedout(udev
);
1354 static int tcmu_attach_hba(struct se_hba
*hba
, u32 host_id
)
1356 struct tcmu_hba
*tcmu_hba
;
1358 tcmu_hba
= kzalloc(sizeof(struct tcmu_hba
), GFP_KERNEL
);
1362 tcmu_hba
->host_id
= host_id
;
1363 hba
->hba_ptr
= tcmu_hba
;
1368 static void tcmu_detach_hba(struct se_hba
*hba
)
1370 kfree(hba
->hba_ptr
);
1371 hba
->hba_ptr
= NULL
;
1374 static struct se_device
*tcmu_alloc_device(struct se_hba
*hba
, const char *name
)
1376 struct tcmu_dev
*udev
;
1378 udev
= kzalloc(sizeof(struct tcmu_dev
), GFP_KERNEL
);
1381 kref_init(&udev
->kref
);
1383 udev
->name
= kstrdup(name
, GFP_KERNEL
);
1390 udev
->cmd_time_out
= TCMU_TIME_OUT
;
1391 udev
->qfull_time_out
= -1;
1393 udev
->max_blocks
= DATA_BLOCK_BITS_DEF
;
1394 mutex_init(&udev
->cmdr_lock
);
1396 INIT_LIST_HEAD(&udev
->node
);
1397 INIT_LIST_HEAD(&udev
->timedout_entry
);
1398 INIT_LIST_HEAD(&udev
->qfull_queue
);
1399 INIT_LIST_HEAD(&udev
->inflight_queue
);
1400 idr_init(&udev
->commands
);
1402 timer_setup(&udev
->qfull_timer
, tcmu_qfull_timedout
, 0);
1403 timer_setup(&udev
->cmd_timer
, tcmu_cmd_timedout
, 0);
1405 INIT_RADIX_TREE(&udev
->data_blocks
, GFP_KERNEL
);
1407 return &udev
->se_dev
;
1410 static bool run_qfull_queue(struct tcmu_dev
*udev
, bool fail
)
1412 struct tcmu_cmd
*tcmu_cmd
, *tmp_cmd
;
1414 bool drained
= true;
1415 sense_reason_t scsi_ret
;
1418 if (list_empty(&udev
->qfull_queue
))
1421 pr_debug("running %s's cmdr queue forcefail %d\n", udev
->name
, fail
);
1423 list_splice_init(&udev
->qfull_queue
, &cmds
);
1425 list_for_each_entry_safe(tcmu_cmd
, tmp_cmd
, &cmds
, queue_entry
) {
1426 list_del_init(&tcmu_cmd
->queue_entry
);
1428 pr_debug("removing cmd %u on dev %s from queue\n",
1429 tcmu_cmd
->cmd_id
, udev
->name
);
1432 idr_remove(&udev
->commands
, tcmu_cmd
->cmd_id
);
1434 * We were not able to even start the command, so
1435 * fail with busy to allow a retry in case runner
1436 * was only temporarily down. If the device is being
1437 * removed then LIO core will do the right thing and
1440 target_complete_cmd(tcmu_cmd
->se_cmd
, SAM_STAT_BUSY
);
1441 tcmu_free_cmd(tcmu_cmd
);
1445 ret
= queue_cmd_ring(tcmu_cmd
, &scsi_ret
);
1447 pr_debug("cmd %u on dev %s failed with %u\n",
1448 tcmu_cmd
->cmd_id
, udev
->name
, scsi_ret
);
1450 idr_remove(&udev
->commands
, tcmu_cmd
->cmd_id
);
1452 * Ignore scsi_ret for now. target_complete_cmd
1455 target_complete_cmd(tcmu_cmd
->se_cmd
,
1456 SAM_STAT_CHECK_CONDITION
);
1457 tcmu_free_cmd(tcmu_cmd
);
1458 } else if (ret
> 0) {
1459 pr_debug("ran out of space during cmdr queue run\n");
1461 * cmd was requeued, so just put all cmds back in
1464 list_splice_tail(&cmds
, &udev
->qfull_queue
);
1470 tcmu_set_next_deadline(&udev
->qfull_queue
, &udev
->qfull_timer
);
1474 static int tcmu_irqcontrol(struct uio_info
*info
, s32 irq_on
)
1476 struct tcmu_dev
*udev
= container_of(info
, struct tcmu_dev
, uio_info
);
1478 mutex_lock(&udev
->cmdr_lock
);
1479 tcmu_handle_completions(udev
);
1480 run_qfull_queue(udev
, false);
1481 mutex_unlock(&udev
->cmdr_lock
);
1487 * mmap code from uio.c. Copied here because we want to hook mmap()
1488 * and this stuff must come along.
1490 static int tcmu_find_mem_index(struct vm_area_struct
*vma
)
1492 struct tcmu_dev
*udev
= vma
->vm_private_data
;
1493 struct uio_info
*info
= &udev
->uio_info
;
1495 if (vma
->vm_pgoff
< MAX_UIO_MAPS
) {
1496 if (info
->mem
[vma
->vm_pgoff
].size
== 0)
1498 return (int)vma
->vm_pgoff
;
1503 static struct page
*tcmu_try_get_block_page(struct tcmu_dev
*udev
, uint32_t dbi
)
1507 mutex_lock(&udev
->cmdr_lock
);
1508 page
= tcmu_get_block_page(udev
, dbi
);
1510 mutex_unlock(&udev
->cmdr_lock
);
1515 * Userspace messed up and passed in a address not in the
1516 * data iov passed to it.
1518 pr_err("Invalid addr to data block mapping (dbi %u) on device %s\n",
1521 mutex_unlock(&udev
->cmdr_lock
);
1526 static vm_fault_t
tcmu_vma_fault(struct vm_fault
*vmf
)
1528 struct tcmu_dev
*udev
= vmf
->vma
->vm_private_data
;
1529 struct uio_info
*info
= &udev
->uio_info
;
1531 unsigned long offset
;
1534 int mi
= tcmu_find_mem_index(vmf
->vma
);
1536 return VM_FAULT_SIGBUS
;
1539 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
1542 offset
= (vmf
->pgoff
- mi
) << PAGE_SHIFT
;
1544 if (offset
< udev
->data_off
) {
1545 /* For the vmalloc()ed cmd area pages */
1546 addr
= (void *)(unsigned long)info
->mem
[mi
].addr
+ offset
;
1547 page
= vmalloc_to_page(addr
);
1551 /* For the dynamically growing data area pages */
1552 dbi
= (offset
- udev
->data_off
) / DATA_BLOCK_SIZE
;
1553 page
= tcmu_try_get_block_page(udev
, dbi
);
1555 return VM_FAULT_SIGBUS
;
1563 static const struct vm_operations_struct tcmu_vm_ops
= {
1564 .fault
= tcmu_vma_fault
,
1567 static int tcmu_mmap(struct uio_info
*info
, struct vm_area_struct
*vma
)
1569 struct tcmu_dev
*udev
= container_of(info
, struct tcmu_dev
, uio_info
);
1571 vma
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
1572 vma
->vm_ops
= &tcmu_vm_ops
;
1574 vma
->vm_private_data
= udev
;
1576 /* Ensure the mmap is exactly the right size */
1577 if (vma_pages(vma
) != (udev
->ring_size
>> PAGE_SHIFT
))
1583 static int tcmu_open(struct uio_info
*info
, struct inode
*inode
)
1585 struct tcmu_dev
*udev
= container_of(info
, struct tcmu_dev
, uio_info
);
1587 /* O_EXCL not supported for char devs, so fake it? */
1588 if (test_and_set_bit(TCMU_DEV_BIT_OPEN
, &udev
->flags
))
1591 udev
->inode
= inode
;
1592 kref_get(&udev
->kref
);
1599 static void tcmu_dev_call_rcu(struct rcu_head
*p
)
1601 struct se_device
*dev
= container_of(p
, struct se_device
, rcu_head
);
1602 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
1604 kfree(udev
->uio_info
.name
);
1609 static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd
*cmd
)
1611 if (test_bit(TCMU_CMD_BIT_EXPIRED
, &cmd
->flags
)) {
1612 kmem_cache_free(tcmu_cmd_cache
, cmd
);
1618 static void tcmu_blocks_release(struct radix_tree_root
*blocks
,
1624 for (i
= start
; i
< end
; i
++) {
1625 page
= radix_tree_delete(blocks
, i
);
1628 atomic_dec(&global_db_count
);
1633 static void tcmu_dev_kref_release(struct kref
*kref
)
1635 struct tcmu_dev
*udev
= container_of(kref
, struct tcmu_dev
, kref
);
1636 struct se_device
*dev
= &udev
->se_dev
;
1637 struct tcmu_cmd
*cmd
;
1638 bool all_expired
= true;
1641 vfree(udev
->mb_addr
);
1642 udev
->mb_addr
= NULL
;
1644 spin_lock_bh(&timed_out_udevs_lock
);
1645 if (!list_empty(&udev
->timedout_entry
))
1646 list_del(&udev
->timedout_entry
);
1647 spin_unlock_bh(&timed_out_udevs_lock
);
1649 /* Upper layer should drain all requests before calling this */
1650 mutex_lock(&udev
->cmdr_lock
);
1651 idr_for_each_entry(&udev
->commands
, cmd
, i
) {
1652 if (tcmu_check_and_free_pending_cmd(cmd
) != 0)
1653 all_expired
= false;
1655 idr_destroy(&udev
->commands
);
1656 WARN_ON(!all_expired
);
1658 tcmu_blocks_release(&udev
->data_blocks
, 0, udev
->dbi_max
+ 1);
1659 bitmap_free(udev
->data_bitmap
);
1660 mutex_unlock(&udev
->cmdr_lock
);
1662 call_rcu(&dev
->rcu_head
, tcmu_dev_call_rcu
);
1665 static int tcmu_release(struct uio_info
*info
, struct inode
*inode
)
1667 struct tcmu_dev
*udev
= container_of(info
, struct tcmu_dev
, uio_info
);
1669 clear_bit(TCMU_DEV_BIT_OPEN
, &udev
->flags
);
1671 pr_debug("close\n");
1672 /* release ref from open */
1673 kref_put(&udev
->kref
, tcmu_dev_kref_release
);
1677 static int tcmu_init_genl_cmd_reply(struct tcmu_dev
*udev
, int cmd
)
1679 struct tcmu_nl_cmd
*nl_cmd
= &udev
->curr_nl_cmd
;
1681 if (!tcmu_kern_cmd_reply_supported
)
1684 if (udev
->nl_reply_supported
<= 0)
1687 mutex_lock(&tcmu_nl_cmd_mutex
);
1689 if (tcmu_netlink_blocked
) {
1690 mutex_unlock(&tcmu_nl_cmd_mutex
);
1691 pr_warn("Failing nl cmd %d on %s. Interface is blocked.\n", cmd
,
1696 if (nl_cmd
->cmd
!= TCMU_CMD_UNSPEC
) {
1697 mutex_unlock(&tcmu_nl_cmd_mutex
);
1698 pr_warn("netlink cmd %d already executing on %s\n",
1699 nl_cmd
->cmd
, udev
->name
);
1703 memset(nl_cmd
, 0, sizeof(*nl_cmd
));
1705 nl_cmd
->udev
= udev
;
1706 init_completion(&nl_cmd
->complete
);
1707 INIT_LIST_HEAD(&nl_cmd
->nl_list
);
1709 list_add_tail(&nl_cmd
->nl_list
, &tcmu_nl_cmd_list
);
1711 mutex_unlock(&tcmu_nl_cmd_mutex
);
1715 static void tcmu_destroy_genl_cmd_reply(struct tcmu_dev
*udev
)
1717 struct tcmu_nl_cmd
*nl_cmd
= &udev
->curr_nl_cmd
;
1719 if (!tcmu_kern_cmd_reply_supported
)
1722 if (udev
->nl_reply_supported
<= 0)
1725 mutex_lock(&tcmu_nl_cmd_mutex
);
1727 list_del(&nl_cmd
->nl_list
);
1728 memset(nl_cmd
, 0, sizeof(*nl_cmd
));
1730 mutex_unlock(&tcmu_nl_cmd_mutex
);
1733 static int tcmu_wait_genl_cmd_reply(struct tcmu_dev
*udev
)
1735 struct tcmu_nl_cmd
*nl_cmd
= &udev
->curr_nl_cmd
;
1738 if (!tcmu_kern_cmd_reply_supported
)
1741 if (udev
->nl_reply_supported
<= 0)
1744 pr_debug("sleeping for nl reply\n");
1745 wait_for_completion(&nl_cmd
->complete
);
1747 mutex_lock(&tcmu_nl_cmd_mutex
);
1748 nl_cmd
->cmd
= TCMU_CMD_UNSPEC
;
1749 ret
= nl_cmd
->status
;
1750 mutex_unlock(&tcmu_nl_cmd_mutex
);
1755 static int tcmu_netlink_event_init(struct tcmu_dev
*udev
,
1756 enum tcmu_genl_cmd cmd
,
1757 struct sk_buff
**buf
, void **hdr
)
1759 struct sk_buff
*skb
;
1763 skb
= genlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1767 msg_header
= genlmsg_put(skb
, 0, 0, &tcmu_genl_family
, 0, cmd
);
1771 ret
= nla_put_string(skb
, TCMU_ATTR_DEVICE
, udev
->uio_info
.name
);
1775 ret
= nla_put_u32(skb
, TCMU_ATTR_MINOR
, udev
->uio_info
.uio_dev
->minor
);
1779 ret
= nla_put_u32(skb
, TCMU_ATTR_DEVICE_ID
, udev
->se_dev
.dev_index
);
1792 static int tcmu_netlink_event_send(struct tcmu_dev
*udev
,
1793 enum tcmu_genl_cmd cmd
,
1794 struct sk_buff
*skb
, void *msg_header
)
1798 genlmsg_end(skb
, msg_header
);
1800 ret
= tcmu_init_genl_cmd_reply(udev
, cmd
);
1806 ret
= genlmsg_multicast_allns(&tcmu_genl_family
, skb
, 0,
1807 TCMU_MCGRP_CONFIG
, GFP_KERNEL
);
1809 /* Wait during an add as the listener may not be up yet */
1811 (ret
== -ESRCH
&& cmd
== TCMU_CMD_ADDED_DEVICE
))
1812 return tcmu_wait_genl_cmd_reply(udev
);
1814 tcmu_destroy_genl_cmd_reply(udev
);
1819 static int tcmu_send_dev_add_event(struct tcmu_dev
*udev
)
1821 struct sk_buff
*skb
= NULL
;
1822 void *msg_header
= NULL
;
1825 ret
= tcmu_netlink_event_init(udev
, TCMU_CMD_ADDED_DEVICE
, &skb
,
1829 return tcmu_netlink_event_send(udev
, TCMU_CMD_ADDED_DEVICE
, skb
,
1833 static int tcmu_send_dev_remove_event(struct tcmu_dev
*udev
)
1835 struct sk_buff
*skb
= NULL
;
1836 void *msg_header
= NULL
;
1839 ret
= tcmu_netlink_event_init(udev
, TCMU_CMD_REMOVED_DEVICE
,
1843 return tcmu_netlink_event_send(udev
, TCMU_CMD_REMOVED_DEVICE
,
1847 static int tcmu_update_uio_info(struct tcmu_dev
*udev
)
1849 struct tcmu_hba
*hba
= udev
->hba
->hba_ptr
;
1850 struct uio_info
*info
;
1853 info
= &udev
->uio_info
;
1855 if (udev
->dev_config
[0])
1856 str
= kasprintf(GFP_KERNEL
, "tcm-user/%u/%s/%s", hba
->host_id
,
1857 udev
->name
, udev
->dev_config
);
1859 str
= kasprintf(GFP_KERNEL
, "tcm-user/%u/%s", hba
->host_id
,
1864 /* If the old string exists, free it */
1871 static int tcmu_configure_device(struct se_device
*dev
)
1873 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
1874 struct uio_info
*info
;
1875 struct tcmu_mailbox
*mb
;
1878 ret
= tcmu_update_uio_info(udev
);
1882 info
= &udev
->uio_info
;
1884 mutex_lock(&udev
->cmdr_lock
);
1885 udev
->data_bitmap
= bitmap_zalloc(udev
->max_blocks
, GFP_KERNEL
);
1886 mutex_unlock(&udev
->cmdr_lock
);
1887 if (!udev
->data_bitmap
) {
1889 goto err_bitmap_alloc
;
1892 udev
->mb_addr
= vzalloc(CMDR_SIZE
);
1893 if (!udev
->mb_addr
) {
1898 /* mailbox fits in first part of CMDR space */
1899 udev
->cmdr_size
= CMDR_SIZE
- CMDR_OFF
;
1900 udev
->data_off
= CMDR_SIZE
;
1901 udev
->data_size
= udev
->max_blocks
* DATA_BLOCK_SIZE
;
1902 udev
->dbi_thresh
= 0; /* Default in Idle state */
1904 /* Initialise the mailbox of the ring buffer */
1906 mb
->version
= TCMU_MAILBOX_VERSION
;
1907 mb
->flags
= TCMU_MAILBOX_FLAG_CAP_OOOC
| TCMU_MAILBOX_FLAG_CAP_READ_LEN
;
1908 mb
->cmdr_off
= CMDR_OFF
;
1909 mb
->cmdr_size
= udev
->cmdr_size
;
1911 WARN_ON(!PAGE_ALIGNED(udev
->data_off
));
1912 WARN_ON(udev
->data_size
% PAGE_SIZE
);
1913 WARN_ON(udev
->data_size
% DATA_BLOCK_SIZE
);
1915 info
->version
= __stringify(TCMU_MAILBOX_VERSION
);
1917 info
->mem
[0].name
= "tcm-user command & data buffer";
1918 info
->mem
[0].addr
= (phys_addr_t
)(uintptr_t)udev
->mb_addr
;
1919 info
->mem
[0].size
= udev
->ring_size
= udev
->data_size
+ CMDR_SIZE
;
1920 info
->mem
[0].memtype
= UIO_MEM_NONE
;
1922 info
->irqcontrol
= tcmu_irqcontrol
;
1923 info
->irq
= UIO_IRQ_CUSTOM
;
1925 info
->mmap
= tcmu_mmap
;
1926 info
->open
= tcmu_open
;
1927 info
->release
= tcmu_release
;
1929 ret
= uio_register_device(tcmu_root_device
, info
);
1933 /* User can set hw_block_size before enable the device */
1934 if (dev
->dev_attrib
.hw_block_size
== 0)
1935 dev
->dev_attrib
.hw_block_size
= 512;
1936 /* Other attributes can be configured in userspace */
1937 if (!dev
->dev_attrib
.hw_max_sectors
)
1938 dev
->dev_attrib
.hw_max_sectors
= 128;
1939 if (!dev
->dev_attrib
.emulate_write_cache
)
1940 dev
->dev_attrib
.emulate_write_cache
= 0;
1941 dev
->dev_attrib
.hw_queue_depth
= 128;
1943 /* If user didn't explicitly disable netlink reply support, use
1944 * module scope setting.
1946 if (udev
->nl_reply_supported
>= 0)
1947 udev
->nl_reply_supported
= tcmu_kern_cmd_reply_supported
;
1950 * Get a ref incase userspace does a close on the uio device before
1951 * LIO has initiated tcmu_free_device.
1953 kref_get(&udev
->kref
);
1955 ret
= tcmu_send_dev_add_event(udev
);
1959 mutex_lock(&root_udev_mutex
);
1960 list_add(&udev
->node
, &root_udev
);
1961 mutex_unlock(&root_udev_mutex
);
1966 kref_put(&udev
->kref
, tcmu_dev_kref_release
);
1967 uio_unregister_device(&udev
->uio_info
);
1969 vfree(udev
->mb_addr
);
1970 udev
->mb_addr
= NULL
;
1972 bitmap_free(udev
->data_bitmap
);
1973 udev
->data_bitmap
= NULL
;
1981 static void tcmu_free_device(struct se_device
*dev
)
1983 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
1985 /* release ref from init */
1986 kref_put(&udev
->kref
, tcmu_dev_kref_release
);
1989 static void tcmu_destroy_device(struct se_device
*dev
)
1991 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
1993 del_timer_sync(&udev
->cmd_timer
);
1994 del_timer_sync(&udev
->qfull_timer
);
1996 mutex_lock(&root_udev_mutex
);
1997 list_del(&udev
->node
);
1998 mutex_unlock(&root_udev_mutex
);
2000 tcmu_send_dev_remove_event(udev
);
2002 uio_unregister_device(&udev
->uio_info
);
2004 /* release ref from configure */
2005 kref_put(&udev
->kref
, tcmu_dev_kref_release
);
2008 static void tcmu_unblock_dev(struct tcmu_dev
*udev
)
2010 mutex_lock(&udev
->cmdr_lock
);
2011 clear_bit(TCMU_DEV_BIT_BLOCKED
, &udev
->flags
);
2012 mutex_unlock(&udev
->cmdr_lock
);
2015 static void tcmu_block_dev(struct tcmu_dev
*udev
)
2017 mutex_lock(&udev
->cmdr_lock
);
2019 if (test_and_set_bit(TCMU_DEV_BIT_BLOCKED
, &udev
->flags
))
2022 /* complete IO that has executed successfully */
2023 tcmu_handle_completions(udev
);
2024 /* fail IO waiting to be queued */
2025 run_qfull_queue(udev
, true);
2028 mutex_unlock(&udev
->cmdr_lock
);
2031 static void tcmu_reset_ring(struct tcmu_dev
*udev
, u8 err_level
)
2033 struct tcmu_mailbox
*mb
;
2034 struct tcmu_cmd
*cmd
;
2037 mutex_lock(&udev
->cmdr_lock
);
2039 idr_for_each_entry(&udev
->commands
, cmd
, i
) {
2040 if (!test_bit(TCMU_CMD_BIT_INFLIGHT
, &cmd
->flags
))
2043 pr_debug("removing cmd %u on dev %s from ring (is expired %d)\n",
2044 cmd
->cmd_id
, udev
->name
,
2045 test_bit(TCMU_CMD_BIT_EXPIRED
, &cmd
->flags
));
2047 idr_remove(&udev
->commands
, i
);
2048 if (!test_bit(TCMU_CMD_BIT_EXPIRED
, &cmd
->flags
)) {
2049 WARN_ON(!cmd
->se_cmd
);
2050 list_del_init(&cmd
->queue_entry
);
2051 if (err_level
== 1) {
2053 * Userspace was not able to start the
2054 * command or it is retryable.
2056 target_complete_cmd(cmd
->se_cmd
, SAM_STAT_BUSY
);
2059 target_complete_cmd(cmd
->se_cmd
,
2060 SAM_STAT_CHECK_CONDITION
);
2063 tcmu_cmd_free_data(cmd
, cmd
->dbi_cnt
);
2068 tcmu_flush_dcache_range(mb
, sizeof(*mb
));
2069 pr_debug("mb last %u head %u tail %u\n", udev
->cmdr_last_cleaned
,
2070 mb
->cmd_tail
, mb
->cmd_head
);
2072 udev
->cmdr_last_cleaned
= 0;
2075 tcmu_flush_dcache_range(mb
, sizeof(*mb
));
2077 del_timer(&udev
->cmd_timer
);
2079 mutex_unlock(&udev
->cmdr_lock
);
2083 Opt_dev_config
, Opt_dev_size
, Opt_hw_block_size
, Opt_hw_max_sectors
,
2084 Opt_nl_reply_supported
, Opt_max_data_area_mb
, Opt_err
,
2087 static match_table_t tokens
= {
2088 {Opt_dev_config
, "dev_config=%s"},
2089 {Opt_dev_size
, "dev_size=%s"},
2090 {Opt_hw_block_size
, "hw_block_size=%d"},
2091 {Opt_hw_max_sectors
, "hw_max_sectors=%d"},
2092 {Opt_nl_reply_supported
, "nl_reply_supported=%d"},
2093 {Opt_max_data_area_mb
, "max_data_area_mb=%d"},
2097 static int tcmu_set_dev_attrib(substring_t
*arg
, u32
*dev_attrib
)
2101 ret
= match_int(arg
, &val
);
2103 pr_err("match_int() failed for dev attrib. Error %d.\n",
2109 pr_err("Invalid dev attrib value %d. Must be greater than zero.\n",
2117 static int tcmu_set_max_blocks_param(struct tcmu_dev
*udev
, substring_t
*arg
)
2121 ret
= match_int(arg
, &val
);
2123 pr_err("match_int() failed for max_data_area_mb=. Error %d.\n",
2129 pr_err("Invalid max_data_area %d.\n", val
);
2133 mutex_lock(&udev
->cmdr_lock
);
2134 if (udev
->data_bitmap
) {
2135 pr_err("Cannot set max_data_area_mb after it has been enabled.\n");
2140 udev
->max_blocks
= TCMU_MBS_TO_BLOCKS(val
);
2141 if (udev
->max_blocks
> tcmu_global_max_blocks
) {
2142 pr_err("%d is too large. Adjusting max_data_area_mb to global limit of %u\n",
2143 val
, TCMU_BLOCKS_TO_MBS(tcmu_global_max_blocks
));
2144 udev
->max_blocks
= tcmu_global_max_blocks
;
2148 mutex_unlock(&udev
->cmdr_lock
);
2152 static ssize_t
tcmu_set_configfs_dev_params(struct se_device
*dev
,
2153 const char *page
, ssize_t count
)
2155 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
2156 char *orig
, *ptr
, *opts
;
2157 substring_t args
[MAX_OPT_ARGS
];
2160 opts
= kstrdup(page
, GFP_KERNEL
);
2166 while ((ptr
= strsep(&opts
, ",\n")) != NULL
) {
2170 token
= match_token(ptr
, tokens
, args
);
2172 case Opt_dev_config
:
2173 if (match_strlcpy(udev
->dev_config
, &args
[0],
2174 TCMU_CONFIG_LEN
) == 0) {
2178 pr_debug("TCMU: Referencing Path: %s\n", udev
->dev_config
);
2181 ret
= match_u64(&args
[0], &udev
->dev_size
);
2183 pr_err("match_u64() failed for dev_size=. Error %d.\n",
2186 case Opt_hw_block_size
:
2187 ret
= tcmu_set_dev_attrib(&args
[0],
2188 &(dev
->dev_attrib
.hw_block_size
));
2190 case Opt_hw_max_sectors
:
2191 ret
= tcmu_set_dev_attrib(&args
[0],
2192 &(dev
->dev_attrib
.hw_max_sectors
));
2194 case Opt_nl_reply_supported
:
2195 ret
= match_int(&args
[0], &udev
->nl_reply_supported
);
2197 pr_err("match_int() failed for nl_reply_supported=. Error %d.\n",
2200 case Opt_max_data_area_mb
:
2201 ret
= tcmu_set_max_blocks_param(udev
, &args
[0]);
2212 return (!ret
) ? count
: ret
;
2215 static ssize_t
tcmu_show_configfs_dev_params(struct se_device
*dev
, char *b
)
2217 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
2220 bl
= sprintf(b
+ bl
, "Config: %s ",
2221 udev
->dev_config
[0] ? udev
->dev_config
: "NULL");
2222 bl
+= sprintf(b
+ bl
, "Size: %llu ", udev
->dev_size
);
2223 bl
+= sprintf(b
+ bl
, "MaxDataAreaMB: %u\n",
2224 TCMU_BLOCKS_TO_MBS(udev
->max_blocks
));
2229 static sector_t
tcmu_get_blocks(struct se_device
*dev
)
2231 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
2233 return div_u64(udev
->dev_size
- dev
->dev_attrib
.block_size
,
2234 dev
->dev_attrib
.block_size
);
2237 static sense_reason_t
2238 tcmu_parse_cdb(struct se_cmd
*cmd
)
2240 return passthrough_parse_cdb(cmd
, tcmu_queue_cmd
);
2243 static ssize_t
tcmu_cmd_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
, "%lu\n", udev
->cmd_time_out
/ MSEC_PER_SEC
);
2252 static ssize_t
tcmu_cmd_time_out_store(struct config_item
*item
, const char *page
,
2255 struct se_dev_attrib
*da
= container_of(to_config_group(item
),
2256 struct se_dev_attrib
, da_group
);
2257 struct tcmu_dev
*udev
= container_of(da
->da_dev
,
2258 struct tcmu_dev
, se_dev
);
2262 if (da
->da_dev
->export_count
) {
2263 pr_err("Unable to set tcmu cmd_time_out while exports exist\n");
2267 ret
= kstrtou32(page
, 0, &val
);
2271 udev
->cmd_time_out
= val
* MSEC_PER_SEC
;
2274 CONFIGFS_ATTR(tcmu_
, cmd_time_out
);
2276 static ssize_t
tcmu_qfull_time_out_show(struct config_item
*item
, char *page
)
2278 struct se_dev_attrib
*da
= container_of(to_config_group(item
),
2279 struct se_dev_attrib
, da_group
);
2280 struct tcmu_dev
*udev
= TCMU_DEV(da
->da_dev
);
2282 return snprintf(page
, PAGE_SIZE
, "%ld\n", udev
->qfull_time_out
<= 0 ?
2283 udev
->qfull_time_out
:
2284 udev
->qfull_time_out
/ MSEC_PER_SEC
);
2287 static ssize_t
tcmu_qfull_time_out_store(struct config_item
*item
,
2288 const char *page
, size_t count
)
2290 struct se_dev_attrib
*da
= container_of(to_config_group(item
),
2291 struct se_dev_attrib
, da_group
);
2292 struct tcmu_dev
*udev
= TCMU_DEV(da
->da_dev
);
2296 ret
= kstrtos32(page
, 0, &val
);
2301 udev
->qfull_time_out
= val
* MSEC_PER_SEC
;
2302 } else if (val
== -1) {
2303 udev
->qfull_time_out
= val
;
2305 printk(KERN_ERR
"Invalid qfull timeout value %d\n", val
);
2310 CONFIGFS_ATTR(tcmu_
, qfull_time_out
);
2312 static ssize_t
tcmu_max_data_area_mb_show(struct config_item
*item
, char *page
)
2314 struct se_dev_attrib
*da
= container_of(to_config_group(item
),
2315 struct se_dev_attrib
, da_group
);
2316 struct tcmu_dev
*udev
= TCMU_DEV(da
->da_dev
);
2318 return snprintf(page
, PAGE_SIZE
, "%u\n",
2319 TCMU_BLOCKS_TO_MBS(udev
->max_blocks
));
2321 CONFIGFS_ATTR_RO(tcmu_
, max_data_area_mb
);
2323 static ssize_t
tcmu_dev_config_show(struct config_item
*item
, char *page
)
2325 struct se_dev_attrib
*da
= container_of(to_config_group(item
),
2326 struct se_dev_attrib
, da_group
);
2327 struct tcmu_dev
*udev
= TCMU_DEV(da
->da_dev
);
2329 return snprintf(page
, PAGE_SIZE
, "%s\n", udev
->dev_config
);
2332 static int tcmu_send_dev_config_event(struct tcmu_dev
*udev
,
2333 const char *reconfig_data
)
2335 struct sk_buff
*skb
= NULL
;
2336 void *msg_header
= NULL
;
2339 ret
= tcmu_netlink_event_init(udev
, TCMU_CMD_RECONFIG_DEVICE
,
2343 ret
= nla_put_string(skb
, TCMU_ATTR_DEV_CFG
, reconfig_data
);
2348 return tcmu_netlink_event_send(udev
, TCMU_CMD_RECONFIG_DEVICE
,
2353 static ssize_t
tcmu_dev_config_store(struct config_item
*item
, const char *page
,
2356 struct se_dev_attrib
*da
= container_of(to_config_group(item
),
2357 struct se_dev_attrib
, da_group
);
2358 struct tcmu_dev
*udev
= TCMU_DEV(da
->da_dev
);
2362 if (!len
|| len
> TCMU_CONFIG_LEN
- 1)
2365 /* Check if device has been configured before */
2366 if (target_dev_configured(&udev
->se_dev
)) {
2367 ret
= tcmu_send_dev_config_event(udev
, page
);
2369 pr_err("Unable to reconfigure device\n");
2372 strlcpy(udev
->dev_config
, page
, TCMU_CONFIG_LEN
);
2374 ret
= tcmu_update_uio_info(udev
);
2379 strlcpy(udev
->dev_config
, page
, TCMU_CONFIG_LEN
);
2383 CONFIGFS_ATTR(tcmu_
, dev_config
);
2385 static ssize_t
tcmu_dev_size_show(struct config_item
*item
, char *page
)
2387 struct se_dev_attrib
*da
= container_of(to_config_group(item
),
2388 struct se_dev_attrib
, da_group
);
2389 struct tcmu_dev
*udev
= TCMU_DEV(da
->da_dev
);
2391 return snprintf(page
, PAGE_SIZE
, "%llu\n", udev
->dev_size
);
2394 static int tcmu_send_dev_size_event(struct tcmu_dev
*udev
, u64 size
)
2396 struct sk_buff
*skb
= NULL
;
2397 void *msg_header
= NULL
;
2400 ret
= tcmu_netlink_event_init(udev
, TCMU_CMD_RECONFIG_DEVICE
,
2404 ret
= nla_put_u64_64bit(skb
, TCMU_ATTR_DEV_SIZE
,
2405 size
, TCMU_ATTR_PAD
);
2410 return tcmu_netlink_event_send(udev
, TCMU_CMD_RECONFIG_DEVICE
,
2414 static ssize_t
tcmu_dev_size_store(struct config_item
*item
, const char *page
,
2417 struct se_dev_attrib
*da
= container_of(to_config_group(item
),
2418 struct se_dev_attrib
, da_group
);
2419 struct tcmu_dev
*udev
= TCMU_DEV(da
->da_dev
);
2423 ret
= kstrtou64(page
, 0, &val
);
2427 /* Check if device has been configured before */
2428 if (target_dev_configured(&udev
->se_dev
)) {
2429 ret
= tcmu_send_dev_size_event(udev
, val
);
2431 pr_err("Unable to reconfigure device\n");
2435 udev
->dev_size
= val
;
2438 CONFIGFS_ATTR(tcmu_
, dev_size
);
2440 static ssize_t
tcmu_nl_reply_supported_show(struct config_item
*item
,
2443 struct se_dev_attrib
*da
= container_of(to_config_group(item
),
2444 struct se_dev_attrib
, da_group
);
2445 struct tcmu_dev
*udev
= TCMU_DEV(da
->da_dev
);
2447 return snprintf(page
, PAGE_SIZE
, "%d\n", udev
->nl_reply_supported
);
2450 static ssize_t
tcmu_nl_reply_supported_store(struct config_item
*item
,
2451 const char *page
, size_t count
)
2453 struct se_dev_attrib
*da
= container_of(to_config_group(item
),
2454 struct se_dev_attrib
, da_group
);
2455 struct tcmu_dev
*udev
= TCMU_DEV(da
->da_dev
);
2459 ret
= kstrtos8(page
, 0, &val
);
2463 udev
->nl_reply_supported
= val
;
2466 CONFIGFS_ATTR(tcmu_
, nl_reply_supported
);
2468 static ssize_t
tcmu_emulate_write_cache_show(struct config_item
*item
,
2471 struct se_dev_attrib
*da
= container_of(to_config_group(item
),
2472 struct se_dev_attrib
, da_group
);
2474 return snprintf(page
, PAGE_SIZE
, "%i\n", da
->emulate_write_cache
);
2477 static int tcmu_send_emulate_write_cache(struct tcmu_dev
*udev
, u8 val
)
2479 struct sk_buff
*skb
= NULL
;
2480 void *msg_header
= NULL
;
2483 ret
= tcmu_netlink_event_init(udev
, TCMU_CMD_RECONFIG_DEVICE
,
2487 ret
= nla_put_u8(skb
, TCMU_ATTR_WRITECACHE
, val
);
2492 return tcmu_netlink_event_send(udev
, TCMU_CMD_RECONFIG_DEVICE
,
2496 static ssize_t
tcmu_emulate_write_cache_store(struct config_item
*item
,
2497 const char *page
, size_t count
)
2499 struct se_dev_attrib
*da
= container_of(to_config_group(item
),
2500 struct se_dev_attrib
, da_group
);
2501 struct tcmu_dev
*udev
= TCMU_DEV(da
->da_dev
);
2505 ret
= kstrtou8(page
, 0, &val
);
2509 /* Check if device has been configured before */
2510 if (target_dev_configured(&udev
->se_dev
)) {
2511 ret
= tcmu_send_emulate_write_cache(udev
, val
);
2513 pr_err("Unable to reconfigure device\n");
2518 da
->emulate_write_cache
= val
;
2521 CONFIGFS_ATTR(tcmu_
, emulate_write_cache
);
2523 static ssize_t
tcmu_block_dev_show(struct config_item
*item
, char *page
)
2525 struct se_device
*se_dev
= container_of(to_config_group(item
),
2528 struct tcmu_dev
*udev
= TCMU_DEV(se_dev
);
2530 if (test_bit(TCMU_DEV_BIT_BLOCKED
, &udev
->flags
))
2531 return snprintf(page
, PAGE_SIZE
, "%s\n", "blocked");
2533 return snprintf(page
, PAGE_SIZE
, "%s\n", "unblocked");
2536 static ssize_t
tcmu_block_dev_store(struct config_item
*item
, const char *page
,
2539 struct se_device
*se_dev
= container_of(to_config_group(item
),
2542 struct tcmu_dev
*udev
= TCMU_DEV(se_dev
);
2546 if (!target_dev_configured(&udev
->se_dev
)) {
2547 pr_err("Device is not configured.\n");
2551 ret
= kstrtou8(page
, 0, &val
);
2556 pr_err("Invalid block value %d\n", val
);
2561 tcmu_unblock_dev(udev
);
2563 tcmu_block_dev(udev
);
2566 CONFIGFS_ATTR(tcmu_
, block_dev
);
2568 static ssize_t
tcmu_reset_ring_store(struct config_item
*item
, const char *page
,
2571 struct se_device
*se_dev
= container_of(to_config_group(item
),
2574 struct tcmu_dev
*udev
= TCMU_DEV(se_dev
);
2578 if (!target_dev_configured(&udev
->se_dev
)) {
2579 pr_err("Device is not configured.\n");
2583 ret
= kstrtou8(page
, 0, &val
);
2587 if (val
!= 1 && val
!= 2) {
2588 pr_err("Invalid reset ring value %d\n", val
);
2592 tcmu_reset_ring(udev
, val
);
2595 CONFIGFS_ATTR_WO(tcmu_
, reset_ring
);
2597 static struct configfs_attribute
*tcmu_attrib_attrs
[] = {
2598 &tcmu_attr_cmd_time_out
,
2599 &tcmu_attr_qfull_time_out
,
2600 &tcmu_attr_max_data_area_mb
,
2601 &tcmu_attr_dev_config
,
2602 &tcmu_attr_dev_size
,
2603 &tcmu_attr_emulate_write_cache
,
2604 &tcmu_attr_nl_reply_supported
,
2608 static struct configfs_attribute
**tcmu_attrs
;
2610 static struct configfs_attribute
*tcmu_action_attrs
[] = {
2611 &tcmu_attr_block_dev
,
2612 &tcmu_attr_reset_ring
,
2616 static struct target_backend_ops tcmu_ops
= {
2618 .owner
= THIS_MODULE
,
2619 .transport_flags
= TRANSPORT_FLAG_PASSTHROUGH
,
2620 .attach_hba
= tcmu_attach_hba
,
2621 .detach_hba
= tcmu_detach_hba
,
2622 .alloc_device
= tcmu_alloc_device
,
2623 .configure_device
= tcmu_configure_device
,
2624 .destroy_device
= tcmu_destroy_device
,
2625 .free_device
= tcmu_free_device
,
2626 .parse_cdb
= tcmu_parse_cdb
,
2627 .set_configfs_dev_params
= tcmu_set_configfs_dev_params
,
2628 .show_configfs_dev_params
= tcmu_show_configfs_dev_params
,
2629 .get_device_type
= sbc_get_device_type
,
2630 .get_blocks
= tcmu_get_blocks
,
2631 .tb_dev_action_attrs
= tcmu_action_attrs
,
2634 static void find_free_blocks(void)
2636 struct tcmu_dev
*udev
;
2638 u32 start
, end
, block
, total_freed
= 0;
2640 if (atomic_read(&global_db_count
) <= tcmu_global_max_blocks
)
2643 mutex_lock(&root_udev_mutex
);
2644 list_for_each_entry(udev
, &root_udev
, node
) {
2645 mutex_lock(&udev
->cmdr_lock
);
2647 if (!target_dev_configured(&udev
->se_dev
)) {
2648 mutex_unlock(&udev
->cmdr_lock
);
2652 /* Try to complete the finished commands first */
2653 tcmu_handle_completions(udev
);
2655 /* Skip the udevs in idle */
2656 if (!udev
->dbi_thresh
) {
2657 mutex_unlock(&udev
->cmdr_lock
);
2661 end
= udev
->dbi_max
+ 1;
2662 block
= find_last_bit(udev
->data_bitmap
, end
);
2663 if (block
== udev
->dbi_max
) {
2665 * The last bit is dbi_max, so it is not possible
2666 * reclaim any blocks.
2668 mutex_unlock(&udev
->cmdr_lock
);
2670 } else if (block
== end
) {
2671 /* The current udev will goto idle state */
2672 udev
->dbi_thresh
= start
= 0;
2675 udev
->dbi_thresh
= start
= block
+ 1;
2676 udev
->dbi_max
= block
;
2679 /* Here will truncate the data area from off */
2680 off
= udev
->data_off
+ start
* DATA_BLOCK_SIZE
;
2681 unmap_mapping_range(udev
->inode
->i_mapping
, off
, 0, 1);
2683 /* Release the block pages */
2684 tcmu_blocks_release(&udev
->data_blocks
, start
, end
);
2685 mutex_unlock(&udev
->cmdr_lock
);
2687 total_freed
+= end
- start
;
2688 pr_debug("Freed %u blocks (total %u) from %s.\n", end
- start
,
2689 total_freed
, udev
->name
);
2691 mutex_unlock(&root_udev_mutex
);
2693 if (atomic_read(&global_db_count
) > tcmu_global_max_blocks
)
2694 schedule_delayed_work(&tcmu_unmap_work
, msecs_to_jiffies(5000));
2697 static void check_timedout_devices(void)
2699 struct tcmu_dev
*udev
, *tmp_dev
;
2702 spin_lock_bh(&timed_out_udevs_lock
);
2703 list_splice_init(&timed_out_udevs
, &devs
);
2705 list_for_each_entry_safe(udev
, tmp_dev
, &devs
, timedout_entry
) {
2706 list_del_init(&udev
->timedout_entry
);
2707 spin_unlock_bh(&timed_out_udevs_lock
);
2709 mutex_lock(&udev
->cmdr_lock
);
2710 idr_for_each(&udev
->commands
, tcmu_check_expired_cmd
, NULL
);
2712 tcmu_set_next_deadline(&udev
->inflight_queue
, &udev
->cmd_timer
);
2713 tcmu_set_next_deadline(&udev
->qfull_queue
, &udev
->qfull_timer
);
2715 mutex_unlock(&udev
->cmdr_lock
);
2717 spin_lock_bh(&timed_out_udevs_lock
);
2720 spin_unlock_bh(&timed_out_udevs_lock
);
2723 static void tcmu_unmap_work_fn(struct work_struct
*work
)
2725 check_timedout_devices();
2729 static int __init
tcmu_module_init(void)
2731 int ret
, i
, k
, len
= 0;
2733 BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry
) % TCMU_OP_ALIGN_SIZE
) != 0);
2735 INIT_DELAYED_WORK(&tcmu_unmap_work
, tcmu_unmap_work_fn
);
2737 tcmu_cmd_cache
= kmem_cache_create("tcmu_cmd_cache",
2738 sizeof(struct tcmu_cmd
),
2739 __alignof__(struct tcmu_cmd
),
2741 if (!tcmu_cmd_cache
)
2744 tcmu_root_device
= root_device_register("tcm_user");
2745 if (IS_ERR(tcmu_root_device
)) {
2746 ret
= PTR_ERR(tcmu_root_device
);
2747 goto out_free_cache
;
2750 ret
= genl_register_family(&tcmu_genl_family
);
2752 goto out_unreg_device
;
2755 for (i
= 0; passthrough_attrib_attrs
[i
] != NULL
; i
++) {
2756 len
+= sizeof(struct configfs_attribute
*);
2758 for (i
= 0; tcmu_attrib_attrs
[i
] != NULL
; i
++) {
2759 len
+= sizeof(struct configfs_attribute
*);
2761 len
+= sizeof(struct configfs_attribute
*);
2763 tcmu_attrs
= kzalloc(len
, GFP_KERNEL
);
2766 goto out_unreg_genl
;
2769 for (i
= 0; passthrough_attrib_attrs
[i
] != NULL
; i
++) {
2770 tcmu_attrs
[i
] = passthrough_attrib_attrs
[i
];
2772 for (k
= 0; tcmu_attrib_attrs
[k
] != NULL
; k
++) {
2773 tcmu_attrs
[i
] = tcmu_attrib_attrs
[k
];
2776 tcmu_ops
.tb_dev_attrib_attrs
= tcmu_attrs
;
2778 ret
= transport_backend_register(&tcmu_ops
);
2787 genl_unregister_family(&tcmu_genl_family
);
2789 root_device_unregister(tcmu_root_device
);
2791 kmem_cache_destroy(tcmu_cmd_cache
);
2796 static void __exit
tcmu_module_exit(void)
2798 cancel_delayed_work_sync(&tcmu_unmap_work
);
2799 target_backend_unregister(&tcmu_ops
);
2801 genl_unregister_family(&tcmu_genl_family
);
2802 root_device_unregister(tcmu_root_device
);
2803 kmem_cache_destroy(tcmu_cmd_cache
);
2806 MODULE_DESCRIPTION("TCM USER subsystem plugin");
2807 MODULE_AUTHOR("Shaohua Li <shli@kernel.org>");
2808 MODULE_AUTHOR("Andy Grover <agrover@redhat.com>");
2809 MODULE_LICENSE("GPL");
2811 module_init(tcmu_module_init
);
2812 module_exit(tcmu_module_exit
);