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/kthread.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_BITS (256 * 1024)
81 #define DATA_SIZE (DATA_BLOCK_BITS * DATA_BLOCK_SIZE)
82 #define DATA_BLOCK_INIT_BITS 128
84 /* The total size of the ring is 8M + 256K * PAGE_SIZE */
85 #define TCMU_RING_SIZE (CMDR_SIZE + DATA_SIZE)
87 /* Default maximum of the global data blocks(512K * PAGE_SIZE) */
88 #define TCMU_GLOBAL_MAX_BLOCKS (512 * 1024)
90 static u8 tcmu_kern_cmd_reply_supported
;
92 static struct device
*tcmu_root_device
;
98 #define TCMU_CONFIG_LEN 256
101 /* wake up thread waiting for reply */
102 struct completion complete
;
108 struct list_head node
;
110 struct se_device se_dev
;
115 #define TCMU_DEV_BIT_OPEN 0
116 #define TCMU_DEV_BIT_BROKEN 1
119 struct uio_info uio_info
;
123 struct tcmu_mailbox
*mb_addr
;
126 u32 cmdr_last_cleaned
;
127 /* Offset of data area from start of mb */
128 /* Must add data_off and mb_addr to get the address */
132 wait_queue_head_t wait_cmdr
;
133 struct mutex cmdr_lock
;
138 DECLARE_BITMAP(data_bitmap
, DATA_BLOCK_BITS
);
139 struct radix_tree_root data_blocks
;
142 spinlock_t commands_lock
;
144 struct timer_list timeout
;
145 unsigned int cmd_time_out
;
147 spinlock_t nl_cmd_lock
;
148 struct tcmu_nl_cmd curr_nl_cmd
;
149 /* wake up threads waiting on curr_nl_cmd */
150 wait_queue_head_t nl_cmd_wq
;
152 char dev_config
[TCMU_CONFIG_LEN
];
155 #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev)
157 #define CMDR_OFF sizeof(struct tcmu_mailbox)
160 struct se_cmd
*se_cmd
;
161 struct tcmu_dev
*tcmu_dev
;
165 /* Can't use se_cmd when cleaning up expired cmds, because if
166 cmd has been completed then accessing se_cmd is off limits */
171 unsigned long deadline
;
173 #define TCMU_CMD_BIT_EXPIRED 0
177 static struct task_struct
*unmap_thread
;
178 static wait_queue_head_t unmap_wait
;
179 static DEFINE_MUTEX(root_udev_mutex
);
180 static LIST_HEAD(root_udev
);
182 static atomic_t global_db_count
= ATOMIC_INIT(0);
184 static struct kmem_cache
*tcmu_cmd_cache
;
186 /* multicast group */
187 enum tcmu_multicast_groups
{
191 static const struct genl_multicast_group tcmu_mcgrps
[] = {
192 [TCMU_MCGRP_CONFIG
] = { .name
= "config", },
195 static struct nla_policy tcmu_attr_policy
[TCMU_ATTR_MAX
+1] = {
196 [TCMU_ATTR_DEVICE
] = { .type
= NLA_STRING
},
197 [TCMU_ATTR_MINOR
] = { .type
= NLA_U32
},
198 [TCMU_ATTR_CMD_STATUS
] = { .type
= NLA_S32
},
199 [TCMU_ATTR_DEVICE_ID
] = { .type
= NLA_U32
},
200 [TCMU_ATTR_SUPP_KERN_CMD_REPLY
] = { .type
= NLA_U8
},
203 static int tcmu_genl_cmd_done(struct genl_info
*info
, int completed_cmd
)
205 struct se_device
*dev
;
206 struct tcmu_dev
*udev
;
207 struct tcmu_nl_cmd
*nl_cmd
;
208 int dev_id
, rc
, ret
= 0;
209 bool is_removed
= (completed_cmd
== TCMU_CMD_REMOVED_DEVICE
);
211 if (!info
->attrs
[TCMU_ATTR_CMD_STATUS
] ||
212 !info
->attrs
[TCMU_ATTR_DEVICE_ID
]) {
213 printk(KERN_ERR
"TCMU_ATTR_CMD_STATUS or TCMU_ATTR_DEVICE_ID not set, doing nothing\n");
217 dev_id
= nla_get_u32(info
->attrs
[TCMU_ATTR_DEVICE_ID
]);
218 rc
= nla_get_s32(info
->attrs
[TCMU_ATTR_CMD_STATUS
]);
220 dev
= target_find_device(dev_id
, !is_removed
);
222 printk(KERN_ERR
"tcmu nl cmd %u/%u completion could not find device with dev id %u.\n",
223 completed_cmd
, rc
, dev_id
);
226 udev
= TCMU_DEV(dev
);
228 spin_lock(&udev
->nl_cmd_lock
);
229 nl_cmd
= &udev
->curr_nl_cmd
;
231 pr_debug("genl cmd done got id %d curr %d done %d rc %d\n", dev_id
,
232 nl_cmd
->cmd
, completed_cmd
, rc
);
234 if (nl_cmd
->cmd
!= completed_cmd
) {
235 printk(KERN_ERR
"Mismatched commands (Expecting reply for %d. Current %d).\n",
236 completed_cmd
, nl_cmd
->cmd
);
242 spin_unlock(&udev
->nl_cmd_lock
);
244 target_undepend_item(&dev
->dev_group
.cg_item
);
246 complete(&nl_cmd
->complete
);
250 static int tcmu_genl_rm_dev_done(struct sk_buff
*skb
, struct genl_info
*info
)
252 return tcmu_genl_cmd_done(info
, TCMU_CMD_REMOVED_DEVICE
);
255 static int tcmu_genl_add_dev_done(struct sk_buff
*skb
, struct genl_info
*info
)
257 return tcmu_genl_cmd_done(info
, TCMU_CMD_ADDED_DEVICE
);
260 static int tcmu_genl_reconfig_dev_done(struct sk_buff
*skb
,
261 struct genl_info
*info
)
263 return tcmu_genl_cmd_done(info
, TCMU_CMD_RECONFIG_DEVICE
);
266 static int tcmu_genl_set_features(struct sk_buff
*skb
, struct genl_info
*info
)
268 if (info
->attrs
[TCMU_ATTR_SUPP_KERN_CMD_REPLY
]) {
269 tcmu_kern_cmd_reply_supported
=
270 nla_get_u8(info
->attrs
[TCMU_ATTR_SUPP_KERN_CMD_REPLY
]);
271 printk(KERN_INFO
"tcmu daemon: command reply support %u.\n",
272 tcmu_kern_cmd_reply_supported
);
278 static const struct genl_ops tcmu_genl_ops
[] = {
280 .cmd
= TCMU_CMD_SET_FEATURES
,
281 .flags
= GENL_ADMIN_PERM
,
282 .policy
= tcmu_attr_policy
,
283 .doit
= tcmu_genl_set_features
,
286 .cmd
= TCMU_CMD_ADDED_DEVICE_DONE
,
287 .flags
= GENL_ADMIN_PERM
,
288 .policy
= tcmu_attr_policy
,
289 .doit
= tcmu_genl_add_dev_done
,
292 .cmd
= TCMU_CMD_REMOVED_DEVICE_DONE
,
293 .flags
= GENL_ADMIN_PERM
,
294 .policy
= tcmu_attr_policy
,
295 .doit
= tcmu_genl_rm_dev_done
,
298 .cmd
= TCMU_CMD_RECONFIG_DEVICE_DONE
,
299 .flags
= GENL_ADMIN_PERM
,
300 .policy
= tcmu_attr_policy
,
301 .doit
= tcmu_genl_reconfig_dev_done
,
305 /* Our generic netlink family */
306 static struct genl_family tcmu_genl_family __ro_after_init
= {
307 .module
= THIS_MODULE
,
311 .maxattr
= TCMU_ATTR_MAX
,
312 .mcgrps
= tcmu_mcgrps
,
313 .n_mcgrps
= ARRAY_SIZE(tcmu_mcgrps
),
315 .ops
= tcmu_genl_ops
,
316 .n_ops
= ARRAY_SIZE(tcmu_genl_ops
),
319 #define tcmu_cmd_set_dbi_cur(cmd, index) ((cmd)->dbi_cur = (index))
320 #define tcmu_cmd_reset_dbi_cur(cmd) tcmu_cmd_set_dbi_cur(cmd, 0)
321 #define tcmu_cmd_set_dbi(cmd, index) ((cmd)->dbi[(cmd)->dbi_cur++] = (index))
322 #define tcmu_cmd_get_dbi(cmd) ((cmd)->dbi[(cmd)->dbi_cur++])
324 static void tcmu_cmd_free_data(struct tcmu_cmd
*tcmu_cmd
, uint32_t len
)
326 struct tcmu_dev
*udev
= tcmu_cmd
->tcmu_dev
;
329 for (i
= 0; i
< len
; i
++)
330 clear_bit(tcmu_cmd
->dbi
[i
], udev
->data_bitmap
);
333 static inline bool tcmu_get_empty_block(struct tcmu_dev
*udev
,
334 struct tcmu_cmd
*tcmu_cmd
)
339 dbi
= find_first_zero_bit(udev
->data_bitmap
, udev
->dbi_thresh
);
340 if (dbi
== udev
->dbi_thresh
)
343 page
= radix_tree_lookup(&udev
->data_blocks
, dbi
);
345 if (atomic_add_return(1, &global_db_count
) >
346 TCMU_GLOBAL_MAX_BLOCKS
) {
347 atomic_dec(&global_db_count
);
351 /* try to get new page from the mm */
352 page
= alloc_page(GFP_KERNEL
);
356 ret
= radix_tree_insert(&udev
->data_blocks
, dbi
, page
);
361 if (dbi
> udev
->dbi_max
)
364 set_bit(dbi
, udev
->data_bitmap
);
365 tcmu_cmd_set_dbi(tcmu_cmd
, dbi
);
371 atomic_dec(&global_db_count
);
375 static bool tcmu_get_empty_blocks(struct tcmu_dev
*udev
,
376 struct tcmu_cmd
*tcmu_cmd
)
380 udev
->waiting_global
= false;
382 for (i
= tcmu_cmd
->dbi_cur
; i
< tcmu_cmd
->dbi_cnt
; i
++) {
383 if (!tcmu_get_empty_block(udev
, tcmu_cmd
))
389 udev
->waiting_global
= true;
390 /* Try to wake up the unmap thread */
391 wake_up(&unmap_wait
);
395 static inline struct page
*
396 tcmu_get_block_page(struct tcmu_dev
*udev
, uint32_t dbi
)
398 return radix_tree_lookup(&udev
->data_blocks
, dbi
);
401 static inline void tcmu_free_cmd(struct tcmu_cmd
*tcmu_cmd
)
403 kfree(tcmu_cmd
->dbi
);
404 kmem_cache_free(tcmu_cmd_cache
, tcmu_cmd
);
407 static inline size_t tcmu_cmd_get_data_length(struct tcmu_cmd
*tcmu_cmd
)
409 struct se_cmd
*se_cmd
= tcmu_cmd
->se_cmd
;
410 size_t data_length
= round_up(se_cmd
->data_length
, DATA_BLOCK_SIZE
);
412 if (se_cmd
->se_cmd_flags
& SCF_BIDI
) {
413 BUG_ON(!(se_cmd
->t_bidi_data_sg
&& se_cmd
->t_bidi_data_nents
));
414 data_length
+= round_up(se_cmd
->t_bidi_data_sg
->length
,
421 static inline uint32_t tcmu_cmd_get_block_cnt(struct tcmu_cmd
*tcmu_cmd
)
423 size_t data_length
= tcmu_cmd_get_data_length(tcmu_cmd
);
425 return data_length
/ DATA_BLOCK_SIZE
;
428 static struct tcmu_cmd
*tcmu_alloc_cmd(struct se_cmd
*se_cmd
)
430 struct se_device
*se_dev
= se_cmd
->se_dev
;
431 struct tcmu_dev
*udev
= TCMU_DEV(se_dev
);
432 struct tcmu_cmd
*tcmu_cmd
;
435 tcmu_cmd
= kmem_cache_zalloc(tcmu_cmd_cache
, GFP_KERNEL
);
439 tcmu_cmd
->se_cmd
= se_cmd
;
440 tcmu_cmd
->tcmu_dev
= udev
;
441 if (udev
->cmd_time_out
)
442 tcmu_cmd
->deadline
= jiffies
+
443 msecs_to_jiffies(udev
->cmd_time_out
);
445 tcmu_cmd_reset_dbi_cur(tcmu_cmd
);
446 tcmu_cmd
->dbi_cnt
= tcmu_cmd_get_block_cnt(tcmu_cmd
);
447 tcmu_cmd
->dbi
= kcalloc(tcmu_cmd
->dbi_cnt
, sizeof(uint32_t),
449 if (!tcmu_cmd
->dbi
) {
450 kmem_cache_free(tcmu_cmd_cache
, tcmu_cmd
);
454 idr_preload(GFP_KERNEL
);
455 spin_lock_irq(&udev
->commands_lock
);
456 cmd_id
= idr_alloc(&udev
->commands
, tcmu_cmd
, 0,
457 USHRT_MAX
, GFP_NOWAIT
);
458 spin_unlock_irq(&udev
->commands_lock
);
462 tcmu_free_cmd(tcmu_cmd
);
465 tcmu_cmd
->cmd_id
= cmd_id
;
470 static inline void tcmu_flush_dcache_range(void *vaddr
, size_t size
)
472 unsigned long offset
= offset_in_page(vaddr
);
474 size
= round_up(size
+offset
, PAGE_SIZE
);
478 flush_dcache_page(virt_to_page(vaddr
));
484 * Some ring helper functions. We don't assume size is a power of 2 so
485 * we can't use circ_buf.h.
487 static inline size_t spc_used(size_t head
, size_t tail
, size_t size
)
489 int diff
= head
- tail
;
497 static inline size_t spc_free(size_t head
, size_t tail
, size_t size
)
499 /* Keep 1 byte unused or we can't tell full from empty */
500 return (size
- spc_used(head
, tail
, size
) - 1);
503 static inline size_t head_to_end(size_t head
, size_t size
)
508 static inline void new_iov(struct iovec
**iov
, int *iov_cnt
,
509 struct tcmu_dev
*udev
)
518 memset(iovec
, 0, sizeof(struct iovec
));
521 #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size)
523 /* offset is relative to mb_addr */
524 static inline size_t get_block_offset_user(struct tcmu_dev
*dev
,
525 int dbi
, int remaining
)
527 return dev
->data_off
+ dbi
* DATA_BLOCK_SIZE
+
528 DATA_BLOCK_SIZE
- remaining
;
531 static inline size_t iov_tail(struct iovec
*iov
)
533 return (size_t)iov
->iov_base
+ iov
->iov_len
;
536 static int scatter_data_area(struct tcmu_dev
*udev
,
537 struct tcmu_cmd
*tcmu_cmd
, struct scatterlist
*data_sg
,
538 unsigned int data_nents
, struct iovec
**iov
,
539 int *iov_cnt
, bool copy_data
)
542 int block_remaining
= 0;
543 void *from
, *to
= NULL
;
544 size_t copy_bytes
, to_offset
, offset
;
545 struct scatterlist
*sg
;
548 for_each_sg(data_sg
, sg
, data_nents
, i
) {
549 int sg_remaining
= sg
->length
;
550 from
= kmap_atomic(sg_page(sg
)) + sg
->offset
;
551 while (sg_remaining
> 0) {
552 if (block_remaining
== 0) {
556 block_remaining
= DATA_BLOCK_SIZE
;
557 dbi
= tcmu_cmd_get_dbi(tcmu_cmd
);
558 page
= tcmu_get_block_page(udev
, dbi
);
559 to
= kmap_atomic(page
);
562 copy_bytes
= min_t(size_t, sg_remaining
,
564 to_offset
= get_block_offset_user(udev
, dbi
,
568 to_offset
== iov_tail(*iov
)) {
569 (*iov
)->iov_len
+= copy_bytes
;
571 new_iov(iov
, iov_cnt
, udev
);
572 (*iov
)->iov_base
= (void __user
*)to_offset
;
573 (*iov
)->iov_len
= copy_bytes
;
576 offset
= DATA_BLOCK_SIZE
- block_remaining
;
578 from
+ sg
->length
- sg_remaining
,
580 tcmu_flush_dcache_range(to
, copy_bytes
);
582 sg_remaining
-= copy_bytes
;
583 block_remaining
-= copy_bytes
;
585 kunmap_atomic(from
- sg
->offset
);
593 static void gather_data_area(struct tcmu_dev
*udev
, struct tcmu_cmd
*cmd
,
596 struct se_cmd
*se_cmd
= cmd
->se_cmd
;
598 int block_remaining
= 0;
599 void *from
= NULL
, *to
;
600 size_t copy_bytes
, offset
;
601 struct scatterlist
*sg
, *data_sg
;
603 unsigned int data_nents
;
607 data_sg
= se_cmd
->t_data_sg
;
608 data_nents
= se_cmd
->t_data_nents
;
612 * For bidi case, the first count blocks are for Data-Out
613 * buffer blocks, and before gathering the Data-In buffer
614 * the Data-Out buffer blocks should be discarded.
616 count
= DIV_ROUND_UP(se_cmd
->data_length
, DATA_BLOCK_SIZE
);
618 data_sg
= se_cmd
->t_bidi_data_sg
;
619 data_nents
= se_cmd
->t_bidi_data_nents
;
622 tcmu_cmd_set_dbi_cur(cmd
, count
);
624 for_each_sg(data_sg
, sg
, data_nents
, i
) {
625 int sg_remaining
= sg
->length
;
626 to
= kmap_atomic(sg_page(sg
)) + sg
->offset
;
627 while (sg_remaining
> 0) {
628 if (block_remaining
== 0) {
632 block_remaining
= DATA_BLOCK_SIZE
;
633 dbi
= tcmu_cmd_get_dbi(cmd
);
634 page
= tcmu_get_block_page(udev
, dbi
);
635 from
= kmap_atomic(page
);
637 copy_bytes
= min_t(size_t, sg_remaining
,
639 offset
= DATA_BLOCK_SIZE
- block_remaining
;
640 tcmu_flush_dcache_range(from
, copy_bytes
);
641 memcpy(to
+ sg
->length
- sg_remaining
, from
+ offset
,
644 sg_remaining
-= copy_bytes
;
645 block_remaining
-= copy_bytes
;
647 kunmap_atomic(to
- sg
->offset
);
653 static inline size_t spc_bitmap_free(unsigned long *bitmap
, uint32_t thresh
)
655 return DATA_BLOCK_SIZE
* (thresh
- bitmap_weight(bitmap
, thresh
));
659 * We can't queue a command until we have space available on the cmd ring *and*
660 * space available on the data area.
662 * Called with ring lock held.
664 static bool is_ring_space_avail(struct tcmu_dev
*udev
, struct tcmu_cmd
*cmd
,
665 size_t cmd_size
, size_t data_needed
)
667 struct tcmu_mailbox
*mb
= udev
->mb_addr
;
668 uint32_t blocks_needed
= (data_needed
+ DATA_BLOCK_SIZE
- 1)
670 size_t space
, cmd_needed
;
673 tcmu_flush_dcache_range(mb
, sizeof(*mb
));
675 cmd_head
= mb
->cmd_head
% udev
->cmdr_size
; /* UAM */
678 * If cmd end-of-ring space is too small then we need space for a NOP plus
679 * original cmd - cmds are internally contiguous.
681 if (head_to_end(cmd_head
, udev
->cmdr_size
) >= cmd_size
)
682 cmd_needed
= cmd_size
;
684 cmd_needed
= cmd_size
+ head_to_end(cmd_head
, udev
->cmdr_size
);
686 space
= spc_free(cmd_head
, udev
->cmdr_last_cleaned
, udev
->cmdr_size
);
687 if (space
< cmd_needed
) {
688 pr_debug("no cmd space: %u %u %u\n", cmd_head
,
689 udev
->cmdr_last_cleaned
, udev
->cmdr_size
);
693 /* try to check and get the data blocks as needed */
694 space
= spc_bitmap_free(udev
->data_bitmap
, udev
->dbi_thresh
);
695 if (space
< data_needed
) {
696 unsigned long blocks_left
= DATA_BLOCK_BITS
- udev
->dbi_thresh
;
699 if (blocks_left
< blocks_needed
) {
700 pr_debug("no data space: only %lu available, but ask for %zu\n",
701 blocks_left
* DATA_BLOCK_SIZE
,
706 /* Try to expand the thresh */
707 if (!udev
->dbi_thresh
) {
708 /* From idle state */
709 uint32_t init_thresh
= DATA_BLOCK_INIT_BITS
;
711 udev
->dbi_thresh
= max(blocks_needed
, init_thresh
);
714 * Grow the data area by max(blocks needed,
715 * dbi_thresh / 2), but limited to the max
716 * DATA_BLOCK_BITS size.
718 grow
= max(blocks_needed
, udev
->dbi_thresh
/ 2);
719 udev
->dbi_thresh
+= grow
;
720 if (udev
->dbi_thresh
> DATA_BLOCK_BITS
)
721 udev
->dbi_thresh
= DATA_BLOCK_BITS
;
725 return tcmu_get_empty_blocks(udev
, cmd
);
728 static inline size_t tcmu_cmd_get_base_cmd_size(size_t iov_cnt
)
730 return max(offsetof(struct tcmu_cmd_entry
, req
.iov
[iov_cnt
]),
731 sizeof(struct tcmu_cmd_entry
));
734 static inline size_t tcmu_cmd_get_cmd_size(struct tcmu_cmd
*tcmu_cmd
,
735 size_t base_command_size
)
737 struct se_cmd
*se_cmd
= tcmu_cmd
->se_cmd
;
740 command_size
= base_command_size
+
741 round_up(scsi_command_size(se_cmd
->t_task_cdb
),
744 WARN_ON(command_size
& (TCMU_OP_ALIGN_SIZE
-1));
749 static sense_reason_t
750 tcmu_queue_cmd_ring(struct tcmu_cmd
*tcmu_cmd
)
752 struct tcmu_dev
*udev
= tcmu_cmd
->tcmu_dev
;
753 struct se_cmd
*se_cmd
= tcmu_cmd
->se_cmd
;
754 size_t base_command_size
, command_size
;
755 struct tcmu_mailbox
*mb
;
756 struct tcmu_cmd_entry
*entry
;
761 bool copy_to_data_area
;
762 size_t data_length
= tcmu_cmd_get_data_length(tcmu_cmd
);
764 if (test_bit(TCMU_DEV_BIT_BROKEN
, &udev
->flags
))
765 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
768 * Must be a certain minimum size for response sense info, but
769 * also may be larger if the iov array is large.
771 * We prepare as many iovs as possbile for potential uses here,
772 * because it's expensive to tell how many regions are freed in
773 * the bitmap & global data pool, as the size calculated here
774 * will only be used to do the checks.
776 * The size will be recalculated later as actually needed to save
779 base_command_size
= tcmu_cmd_get_base_cmd_size(tcmu_cmd
->dbi_cnt
);
780 command_size
= tcmu_cmd_get_cmd_size(tcmu_cmd
, base_command_size
);
782 mutex_lock(&udev
->cmdr_lock
);
785 cmd_head
= mb
->cmd_head
% udev
->cmdr_size
; /* UAM */
786 if ((command_size
> (udev
->cmdr_size
/ 2)) ||
787 data_length
> udev
->data_size
) {
788 pr_warn("TCMU: Request of size %zu/%zu is too big for %u/%zu "
789 "cmd ring/data area\n", command_size
, data_length
,
790 udev
->cmdr_size
, udev
->data_size
);
791 mutex_unlock(&udev
->cmdr_lock
);
792 return TCM_INVALID_CDB_FIELD
;
795 while (!is_ring_space_avail(udev
, tcmu_cmd
, command_size
, data_length
)) {
800 * Don't leave commands partially setup because the unmap
801 * thread might need the blocks to make forward progress.
803 tcmu_cmd_free_data(tcmu_cmd
, tcmu_cmd
->dbi_cur
);
804 tcmu_cmd_reset_dbi_cur(tcmu_cmd
);
806 prepare_to_wait(&udev
->wait_cmdr
, &__wait
, TASK_INTERRUPTIBLE
);
808 pr_debug("sleeping for ring space\n");
809 mutex_unlock(&udev
->cmdr_lock
);
810 if (udev
->cmd_time_out
)
811 ret
= schedule_timeout(
812 msecs_to_jiffies(udev
->cmd_time_out
));
814 ret
= schedule_timeout(msecs_to_jiffies(TCMU_TIME_OUT
));
815 finish_wait(&udev
->wait_cmdr
, &__wait
);
817 pr_warn("tcmu: command timed out\n");
818 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
821 mutex_lock(&udev
->cmdr_lock
);
823 /* We dropped cmdr_lock, cmd_head is stale */
824 cmd_head
= mb
->cmd_head
% udev
->cmdr_size
; /* UAM */
827 /* Insert a PAD if end-of-ring space is too small */
828 if (head_to_end(cmd_head
, udev
->cmdr_size
) < command_size
) {
829 size_t pad_size
= head_to_end(cmd_head
, udev
->cmdr_size
);
831 entry
= (void *) mb
+ CMDR_OFF
+ cmd_head
;
832 tcmu_hdr_set_op(&entry
->hdr
.len_op
, TCMU_OP_PAD
);
833 tcmu_hdr_set_len(&entry
->hdr
.len_op
, pad_size
);
834 entry
->hdr
.cmd_id
= 0; /* not used for PAD */
835 entry
->hdr
.kflags
= 0;
836 entry
->hdr
.uflags
= 0;
837 tcmu_flush_dcache_range(entry
, sizeof(*entry
));
839 UPDATE_HEAD(mb
->cmd_head
, pad_size
, udev
->cmdr_size
);
840 tcmu_flush_dcache_range(mb
, sizeof(*mb
));
842 cmd_head
= mb
->cmd_head
% udev
->cmdr_size
; /* UAM */
843 WARN_ON(cmd_head
!= 0);
846 entry
= (void *) mb
+ CMDR_OFF
+ cmd_head
;
847 memset(entry
, 0, command_size
);
848 tcmu_hdr_set_op(&entry
->hdr
.len_op
, TCMU_OP_CMD
);
849 entry
->hdr
.cmd_id
= tcmu_cmd
->cmd_id
;
851 /* Handle allocating space from the data area */
852 tcmu_cmd_reset_dbi_cur(tcmu_cmd
);
853 iov
= &entry
->req
.iov
[0];
855 copy_to_data_area
= (se_cmd
->data_direction
== DMA_TO_DEVICE
856 || se_cmd
->se_cmd_flags
& SCF_BIDI
);
857 ret
= scatter_data_area(udev
, tcmu_cmd
, se_cmd
->t_data_sg
,
858 se_cmd
->t_data_nents
, &iov
, &iov_cnt
,
861 tcmu_cmd_free_data(tcmu_cmd
, tcmu_cmd
->dbi_cnt
);
862 mutex_unlock(&udev
->cmdr_lock
);
864 pr_err("tcmu: alloc and scatter data failed\n");
865 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
867 entry
->req
.iov_cnt
= iov_cnt
;
869 /* Handle BIDI commands */
871 if (se_cmd
->se_cmd_flags
& SCF_BIDI
) {
873 ret
= scatter_data_area(udev
, tcmu_cmd
,
874 se_cmd
->t_bidi_data_sg
,
875 se_cmd
->t_bidi_data_nents
,
876 &iov
, &iov_cnt
, false);
878 tcmu_cmd_free_data(tcmu_cmd
, tcmu_cmd
->dbi_cnt
);
879 mutex_unlock(&udev
->cmdr_lock
);
881 pr_err("tcmu: alloc and scatter bidi data failed\n");
882 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
885 entry
->req
.iov_bidi_cnt
= iov_cnt
;
888 * Recalaulate the command's base size and size according
889 * to the actual needs
891 base_command_size
= tcmu_cmd_get_base_cmd_size(entry
->req
.iov_cnt
+
892 entry
->req
.iov_bidi_cnt
);
893 command_size
= tcmu_cmd_get_cmd_size(tcmu_cmd
, base_command_size
);
895 tcmu_hdr_set_len(&entry
->hdr
.len_op
, command_size
);
897 /* All offsets relative to mb_addr, not start of entry! */
898 cdb_off
= CMDR_OFF
+ cmd_head
+ base_command_size
;
899 memcpy((void *) mb
+ cdb_off
, se_cmd
->t_task_cdb
, scsi_command_size(se_cmd
->t_task_cdb
));
900 entry
->req
.cdb_off
= cdb_off
;
901 tcmu_flush_dcache_range(entry
, sizeof(*entry
));
903 UPDATE_HEAD(mb
->cmd_head
, command_size
, udev
->cmdr_size
);
904 tcmu_flush_dcache_range(mb
, sizeof(*mb
));
905 mutex_unlock(&udev
->cmdr_lock
);
907 /* TODO: only if FLUSH and FUA? */
908 uio_event_notify(&udev
->uio_info
);
910 if (udev
->cmd_time_out
)
911 mod_timer(&udev
->timeout
, round_jiffies_up(jiffies
+
912 msecs_to_jiffies(udev
->cmd_time_out
)));
917 static sense_reason_t
918 tcmu_queue_cmd(struct se_cmd
*se_cmd
)
920 struct se_device
*se_dev
= se_cmd
->se_dev
;
921 struct tcmu_dev
*udev
= TCMU_DEV(se_dev
);
922 struct tcmu_cmd
*tcmu_cmd
;
925 tcmu_cmd
= tcmu_alloc_cmd(se_cmd
);
927 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
929 ret
= tcmu_queue_cmd_ring(tcmu_cmd
);
930 if (ret
!= TCM_NO_SENSE
) {
931 pr_err("TCMU: Could not queue command\n");
932 spin_lock_irq(&udev
->commands_lock
);
933 idr_remove(&udev
->commands
, tcmu_cmd
->cmd_id
);
934 spin_unlock_irq(&udev
->commands_lock
);
936 tcmu_free_cmd(tcmu_cmd
);
942 static void tcmu_handle_completion(struct tcmu_cmd
*cmd
, struct tcmu_cmd_entry
*entry
)
944 struct se_cmd
*se_cmd
= cmd
->se_cmd
;
945 struct tcmu_dev
*udev
= cmd
->tcmu_dev
;
948 * cmd has been completed already from timeout, just reclaim
949 * data area space and free cmd
951 if (test_bit(TCMU_CMD_BIT_EXPIRED
, &cmd
->flags
))
954 tcmu_cmd_reset_dbi_cur(cmd
);
956 if (entry
->hdr
.uflags
& TCMU_UFLAG_UNKNOWN_OP
) {
957 pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n",
959 entry
->rsp
.scsi_status
= SAM_STAT_CHECK_CONDITION
;
960 } else if (entry
->rsp
.scsi_status
== SAM_STAT_CHECK_CONDITION
) {
961 transport_copy_sense_to_cmd(se_cmd
, entry
->rsp
.sense_buffer
);
962 } else if (se_cmd
->se_cmd_flags
& SCF_BIDI
) {
963 /* Get Data-In buffer before clean up */
964 gather_data_area(udev
, cmd
, true);
965 } else if (se_cmd
->data_direction
== DMA_FROM_DEVICE
) {
966 gather_data_area(udev
, cmd
, false);
967 } else if (se_cmd
->data_direction
== DMA_TO_DEVICE
) {
969 } else if (se_cmd
->data_direction
!= DMA_NONE
) {
970 pr_warn("TCMU: data direction was %d!\n",
971 se_cmd
->data_direction
);
974 target_complete_cmd(cmd
->se_cmd
, entry
->rsp
.scsi_status
);
978 tcmu_cmd_free_data(cmd
, cmd
->dbi_cnt
);
982 static unsigned int tcmu_handle_completions(struct tcmu_dev
*udev
)
984 struct tcmu_mailbox
*mb
;
987 if (test_bit(TCMU_DEV_BIT_BROKEN
, &udev
->flags
)) {
988 pr_err("ring broken, not handling completions\n");
993 tcmu_flush_dcache_range(mb
, sizeof(*mb
));
995 while (udev
->cmdr_last_cleaned
!= ACCESS_ONCE(mb
->cmd_tail
)) {
997 struct tcmu_cmd_entry
*entry
= (void *) mb
+ CMDR_OFF
+ udev
->cmdr_last_cleaned
;
998 struct tcmu_cmd
*cmd
;
1000 tcmu_flush_dcache_range(entry
, sizeof(*entry
));
1002 if (tcmu_hdr_get_op(entry
->hdr
.len_op
) == TCMU_OP_PAD
) {
1003 UPDATE_HEAD(udev
->cmdr_last_cleaned
,
1004 tcmu_hdr_get_len(entry
->hdr
.len_op
),
1008 WARN_ON(tcmu_hdr_get_op(entry
->hdr
.len_op
) != TCMU_OP_CMD
);
1010 spin_lock(&udev
->commands_lock
);
1011 cmd
= idr_remove(&udev
->commands
, entry
->hdr
.cmd_id
);
1012 spin_unlock(&udev
->commands_lock
);
1015 pr_err("cmd_id not found, ring is broken\n");
1016 set_bit(TCMU_DEV_BIT_BROKEN
, &udev
->flags
);
1020 tcmu_handle_completion(cmd
, entry
);
1022 UPDATE_HEAD(udev
->cmdr_last_cleaned
,
1023 tcmu_hdr_get_len(entry
->hdr
.len_op
),
1029 if (mb
->cmd_tail
== mb
->cmd_head
)
1030 del_timer(&udev
->timeout
); /* no more pending cmds */
1032 wake_up(&udev
->wait_cmdr
);
1037 static int tcmu_check_expired_cmd(int id
, void *p
, void *data
)
1039 struct tcmu_cmd
*cmd
= p
;
1041 if (test_bit(TCMU_CMD_BIT_EXPIRED
, &cmd
->flags
))
1044 if (!time_after(jiffies
, cmd
->deadline
))
1047 set_bit(TCMU_CMD_BIT_EXPIRED
, &cmd
->flags
);
1048 target_complete_cmd(cmd
->se_cmd
, SAM_STAT_CHECK_CONDITION
);
1054 static void tcmu_device_timedout(unsigned long data
)
1056 struct tcmu_dev
*udev
= (struct tcmu_dev
*)data
;
1057 unsigned long flags
;
1059 spin_lock_irqsave(&udev
->commands_lock
, flags
);
1060 idr_for_each(&udev
->commands
, tcmu_check_expired_cmd
, NULL
);
1061 spin_unlock_irqrestore(&udev
->commands_lock
, flags
);
1063 /* Try to wake up the ummap thread */
1064 wake_up(&unmap_wait
);
1067 * We don't need to wakeup threads on wait_cmdr since they have their
1072 static int tcmu_attach_hba(struct se_hba
*hba
, u32 host_id
)
1074 struct tcmu_hba
*tcmu_hba
;
1076 tcmu_hba
= kzalloc(sizeof(struct tcmu_hba
), GFP_KERNEL
);
1080 tcmu_hba
->host_id
= host_id
;
1081 hba
->hba_ptr
= tcmu_hba
;
1086 static void tcmu_detach_hba(struct se_hba
*hba
)
1088 kfree(hba
->hba_ptr
);
1089 hba
->hba_ptr
= NULL
;
1092 static struct se_device
*tcmu_alloc_device(struct se_hba
*hba
, const char *name
)
1094 struct tcmu_dev
*udev
;
1096 udev
= kzalloc(sizeof(struct tcmu_dev
), GFP_KERNEL
);
1099 kref_init(&udev
->kref
);
1101 udev
->name
= kstrdup(name
, GFP_KERNEL
);
1108 udev
->cmd_time_out
= TCMU_TIME_OUT
;
1110 init_waitqueue_head(&udev
->wait_cmdr
);
1111 mutex_init(&udev
->cmdr_lock
);
1113 idr_init(&udev
->commands
);
1114 spin_lock_init(&udev
->commands_lock
);
1116 setup_timer(&udev
->timeout
, tcmu_device_timedout
,
1117 (unsigned long)udev
);
1119 init_waitqueue_head(&udev
->nl_cmd_wq
);
1120 spin_lock_init(&udev
->nl_cmd_lock
);
1122 return &udev
->se_dev
;
1125 static int tcmu_irqcontrol(struct uio_info
*info
, s32 irq_on
)
1127 struct tcmu_dev
*tcmu_dev
= container_of(info
, struct tcmu_dev
, uio_info
);
1129 mutex_lock(&tcmu_dev
->cmdr_lock
);
1130 tcmu_handle_completions(tcmu_dev
);
1131 mutex_unlock(&tcmu_dev
->cmdr_lock
);
1137 * mmap code from uio.c. Copied here because we want to hook mmap()
1138 * and this stuff must come along.
1140 static int tcmu_find_mem_index(struct vm_area_struct
*vma
)
1142 struct tcmu_dev
*udev
= vma
->vm_private_data
;
1143 struct uio_info
*info
= &udev
->uio_info
;
1145 if (vma
->vm_pgoff
< MAX_UIO_MAPS
) {
1146 if (info
->mem
[vma
->vm_pgoff
].size
== 0)
1148 return (int)vma
->vm_pgoff
;
1153 static struct page
*tcmu_try_get_block_page(struct tcmu_dev
*udev
, uint32_t dbi
)
1158 mutex_lock(&udev
->cmdr_lock
);
1159 page
= tcmu_get_block_page(udev
, dbi
);
1161 mutex_unlock(&udev
->cmdr_lock
);
1166 * Normally it shouldn't be here:
1167 * Only when the userspace has touched the blocks which
1168 * are out of the tcmu_cmd's data iov[], and will return
1171 pr_warn("Block(%u) out of cmd's iov[] has been touched!\n", dbi
);
1172 pr_warn("Mostly it will be a bug of userspace, please have a check!\n");
1174 if (dbi
>= udev
->dbi_thresh
) {
1175 /* Extern the udev->dbi_thresh to dbi + 1 */
1176 udev
->dbi_thresh
= dbi
+ 1;
1177 udev
->dbi_max
= dbi
;
1180 page
= radix_tree_lookup(&udev
->data_blocks
, dbi
);
1182 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
1184 mutex_unlock(&udev
->cmdr_lock
);
1188 ret
= radix_tree_insert(&udev
->data_blocks
, dbi
, page
);
1190 mutex_unlock(&udev
->cmdr_lock
);
1196 * Since this case is rare in page fault routine, here we
1197 * will allow the global_db_count >= TCMU_GLOBAL_MAX_BLOCKS
1198 * to reduce possible page fault call trace.
1200 atomic_inc(&global_db_count
);
1202 mutex_unlock(&udev
->cmdr_lock
);
1207 static int tcmu_vma_fault(struct vm_fault
*vmf
)
1209 struct tcmu_dev
*udev
= vmf
->vma
->vm_private_data
;
1210 struct uio_info
*info
= &udev
->uio_info
;
1212 unsigned long offset
;
1215 int mi
= tcmu_find_mem_index(vmf
->vma
);
1217 return VM_FAULT_SIGBUS
;
1220 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
1223 offset
= (vmf
->pgoff
- mi
) << PAGE_SHIFT
;
1225 if (offset
< udev
->data_off
) {
1226 /* For the vmalloc()ed cmd area pages */
1227 addr
= (void *)(unsigned long)info
->mem
[mi
].addr
+ offset
;
1228 page
= vmalloc_to_page(addr
);
1232 /* For the dynamically growing data area pages */
1233 dbi
= (offset
- udev
->data_off
) / DATA_BLOCK_SIZE
;
1234 page
= tcmu_try_get_block_page(udev
, dbi
);
1236 return VM_FAULT_NOPAGE
;
1244 static const struct vm_operations_struct tcmu_vm_ops
= {
1245 .fault
= tcmu_vma_fault
,
1248 static int tcmu_mmap(struct uio_info
*info
, struct vm_area_struct
*vma
)
1250 struct tcmu_dev
*udev
= container_of(info
, struct tcmu_dev
, uio_info
);
1252 vma
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
1253 vma
->vm_ops
= &tcmu_vm_ops
;
1255 vma
->vm_private_data
= udev
;
1257 /* Ensure the mmap is exactly the right size */
1258 if (vma_pages(vma
) != (TCMU_RING_SIZE
>> PAGE_SHIFT
))
1264 static int tcmu_open(struct uio_info
*info
, struct inode
*inode
)
1266 struct tcmu_dev
*udev
= container_of(info
, struct tcmu_dev
, uio_info
);
1268 /* O_EXCL not supported for char devs, so fake it? */
1269 if (test_and_set_bit(TCMU_DEV_BIT_OPEN
, &udev
->flags
))
1272 udev
->inode
= inode
;
1273 kref_get(&udev
->kref
);
1280 static void tcmu_dev_call_rcu(struct rcu_head
*p
)
1282 struct se_device
*dev
= container_of(p
, struct se_device
, rcu_head
);
1283 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
1285 kfree(udev
->uio_info
.name
);
1290 static void tcmu_dev_kref_release(struct kref
*kref
)
1292 struct tcmu_dev
*udev
= container_of(kref
, struct tcmu_dev
, kref
);
1293 struct se_device
*dev
= &udev
->se_dev
;
1295 call_rcu(&dev
->rcu_head
, tcmu_dev_call_rcu
);
1298 static int tcmu_release(struct uio_info
*info
, struct inode
*inode
)
1300 struct tcmu_dev
*udev
= container_of(info
, struct tcmu_dev
, uio_info
);
1302 clear_bit(TCMU_DEV_BIT_OPEN
, &udev
->flags
);
1304 pr_debug("close\n");
1305 /* release ref from open */
1306 kref_put(&udev
->kref
, tcmu_dev_kref_release
);
1310 static void tcmu_init_genl_cmd_reply(struct tcmu_dev
*udev
, int cmd
)
1312 struct tcmu_nl_cmd
*nl_cmd
= &udev
->curr_nl_cmd
;
1314 if (!tcmu_kern_cmd_reply_supported
)
1317 spin_lock(&udev
->nl_cmd_lock
);
1319 if (nl_cmd
->cmd
!= TCMU_CMD_UNSPEC
) {
1320 spin_unlock(&udev
->nl_cmd_lock
);
1321 pr_debug("sleeping for open nl cmd\n");
1322 wait_event(udev
->nl_cmd_wq
, (nl_cmd
->cmd
== TCMU_CMD_UNSPEC
));
1326 memset(nl_cmd
, 0, sizeof(*nl_cmd
));
1328 init_completion(&nl_cmd
->complete
);
1330 spin_unlock(&udev
->nl_cmd_lock
);
1333 static int tcmu_wait_genl_cmd_reply(struct tcmu_dev
*udev
)
1335 struct tcmu_nl_cmd
*nl_cmd
= &udev
->curr_nl_cmd
;
1337 DEFINE_WAIT(__wait
);
1339 if (!tcmu_kern_cmd_reply_supported
)
1342 pr_debug("sleeping for nl reply\n");
1343 wait_for_completion(&nl_cmd
->complete
);
1345 spin_lock(&udev
->nl_cmd_lock
);
1346 nl_cmd
->cmd
= TCMU_CMD_UNSPEC
;
1347 ret
= nl_cmd
->status
;
1349 spin_unlock(&udev
->nl_cmd_lock
);
1351 wake_up_all(&udev
->nl_cmd_wq
);
1356 static int tcmu_netlink_event(struct tcmu_dev
*udev
, enum tcmu_genl_cmd cmd
,
1357 int reconfig_attr
, const void *reconfig_data
)
1359 struct sk_buff
*skb
;
1363 skb
= genlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
1367 msg_header
= genlmsg_put(skb
, 0, 0, &tcmu_genl_family
, 0, cmd
);
1371 ret
= nla_put_string(skb
, TCMU_ATTR_DEVICE
, udev
->uio_info
.name
);
1375 ret
= nla_put_u32(skb
, TCMU_ATTR_MINOR
, udev
->uio_info
.uio_dev
->minor
);
1379 ret
= nla_put_u32(skb
, TCMU_ATTR_DEVICE_ID
, udev
->se_dev
.dev_index
);
1383 if (cmd
== TCMU_CMD_RECONFIG_DEVICE
) {
1384 switch (reconfig_attr
) {
1385 case TCMU_ATTR_DEV_CFG
:
1386 ret
= nla_put_string(skb
, reconfig_attr
, reconfig_data
);
1388 case TCMU_ATTR_DEV_SIZE
:
1389 ret
= nla_put_u64_64bit(skb
, reconfig_attr
,
1390 *((u64
*)reconfig_data
),
1393 case TCMU_ATTR_WRITECACHE
:
1394 ret
= nla_put_u8(skb
, reconfig_attr
,
1395 *((u8
*)reconfig_data
));
1405 genlmsg_end(skb
, msg_header
);
1407 tcmu_init_genl_cmd_reply(udev
, cmd
);
1409 ret
= genlmsg_multicast_allns(&tcmu_genl_family
, skb
, 0,
1410 TCMU_MCGRP_CONFIG
, GFP_KERNEL
);
1411 /* We don't care if no one is listening */
1415 ret
= tcmu_wait_genl_cmd_reply(udev
);
1423 static int tcmu_update_uio_info(struct tcmu_dev
*udev
)
1425 struct tcmu_hba
*hba
= udev
->hba
->hba_ptr
;
1426 struct uio_info
*info
;
1430 info
= &udev
->uio_info
;
1431 size
= snprintf(NULL
, 0, "tcm-user/%u/%s/%s", hba
->host_id
, udev
->name
,
1433 size
+= 1; /* for \0 */
1434 str
= kmalloc(size
, GFP_KERNEL
);
1438 used
= snprintf(str
, size
, "tcm-user/%u/%s", hba
->host_id
, udev
->name
);
1439 if (udev
->dev_config
[0])
1440 snprintf(str
+ used
, size
- used
, "/%s", udev
->dev_config
);
1442 /* If the old string exists, free it */
1449 static int tcmu_configure_device(struct se_device
*dev
)
1451 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
1452 struct uio_info
*info
;
1453 struct tcmu_mailbox
*mb
;
1456 ret
= tcmu_update_uio_info(udev
);
1460 info
= &udev
->uio_info
;
1462 udev
->mb_addr
= vzalloc(CMDR_SIZE
);
1463 if (!udev
->mb_addr
) {
1468 /* mailbox fits in first part of CMDR space */
1469 udev
->cmdr_size
= CMDR_SIZE
- CMDR_OFF
;
1470 udev
->data_off
= CMDR_SIZE
;
1471 udev
->data_size
= DATA_SIZE
;
1472 udev
->dbi_thresh
= 0; /* Default in Idle state */
1473 udev
->waiting_global
= false;
1475 /* Initialise the mailbox of the ring buffer */
1477 mb
->version
= TCMU_MAILBOX_VERSION
;
1478 mb
->flags
= TCMU_MAILBOX_FLAG_CAP_OOOC
;
1479 mb
->cmdr_off
= CMDR_OFF
;
1480 mb
->cmdr_size
= udev
->cmdr_size
;
1482 WARN_ON(!PAGE_ALIGNED(udev
->data_off
));
1483 WARN_ON(udev
->data_size
% PAGE_SIZE
);
1484 WARN_ON(udev
->data_size
% DATA_BLOCK_SIZE
);
1486 INIT_RADIX_TREE(&udev
->data_blocks
, GFP_KERNEL
);
1488 info
->version
= __stringify(TCMU_MAILBOX_VERSION
);
1490 info
->mem
[0].name
= "tcm-user command & data buffer";
1491 info
->mem
[0].addr
= (phys_addr_t
)(uintptr_t)udev
->mb_addr
;
1492 info
->mem
[0].size
= TCMU_RING_SIZE
;
1493 info
->mem
[0].memtype
= UIO_MEM_NONE
;
1495 info
->irqcontrol
= tcmu_irqcontrol
;
1496 info
->irq
= UIO_IRQ_CUSTOM
;
1498 info
->mmap
= tcmu_mmap
;
1499 info
->open
= tcmu_open
;
1500 info
->release
= tcmu_release
;
1502 ret
= uio_register_device(tcmu_root_device
, info
);
1506 /* User can set hw_block_size before enable the device */
1507 if (dev
->dev_attrib
.hw_block_size
== 0)
1508 dev
->dev_attrib
.hw_block_size
= 512;
1509 /* Other attributes can be configured in userspace */
1510 if (!dev
->dev_attrib
.hw_max_sectors
)
1511 dev
->dev_attrib
.hw_max_sectors
= 128;
1512 if (!dev
->dev_attrib
.emulate_write_cache
)
1513 dev
->dev_attrib
.emulate_write_cache
= 0;
1514 dev
->dev_attrib
.hw_queue_depth
= 128;
1517 * Get a ref incase userspace does a close on the uio device before
1518 * LIO has initiated tcmu_free_device.
1520 kref_get(&udev
->kref
);
1522 ret
= tcmu_netlink_event(udev
, TCMU_CMD_ADDED_DEVICE
, 0, NULL
);
1526 mutex_lock(&root_udev_mutex
);
1527 list_add(&udev
->node
, &root_udev
);
1528 mutex_unlock(&root_udev_mutex
);
1533 kref_put(&udev
->kref
, tcmu_dev_kref_release
);
1534 uio_unregister_device(&udev
->uio_info
);
1536 vfree(udev
->mb_addr
);
1544 static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd
*cmd
)
1546 if (test_bit(TCMU_CMD_BIT_EXPIRED
, &cmd
->flags
)) {
1547 kmem_cache_free(tcmu_cmd_cache
, cmd
);
1553 static bool tcmu_dev_configured(struct tcmu_dev
*udev
)
1555 return udev
->uio_info
.uio_dev
? true : false;
1558 static void tcmu_blocks_release(struct tcmu_dev
*udev
)
1563 /* Try to release all block pages */
1564 mutex_lock(&udev
->cmdr_lock
);
1565 for (i
= 0; i
<= udev
->dbi_max
; i
++) {
1566 page
= radix_tree_delete(&udev
->data_blocks
, i
);
1569 atomic_dec(&global_db_count
);
1572 mutex_unlock(&udev
->cmdr_lock
);
1575 static void tcmu_free_device(struct se_device
*dev
)
1577 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
1579 /* release ref from init */
1580 kref_put(&udev
->kref
, tcmu_dev_kref_release
);
1583 static void tcmu_destroy_device(struct se_device
*dev
)
1585 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
1586 struct tcmu_cmd
*cmd
;
1587 bool all_expired
= true;
1590 del_timer_sync(&udev
->timeout
);
1592 mutex_lock(&root_udev_mutex
);
1593 list_del(&udev
->node
);
1594 mutex_unlock(&root_udev_mutex
);
1596 vfree(udev
->mb_addr
);
1598 /* Upper layer should drain all requests before calling this */
1599 spin_lock_irq(&udev
->commands_lock
);
1600 idr_for_each_entry(&udev
->commands
, cmd
, i
) {
1601 if (tcmu_check_and_free_pending_cmd(cmd
) != 0)
1602 all_expired
= false;
1604 idr_destroy(&udev
->commands
);
1605 spin_unlock_irq(&udev
->commands_lock
);
1606 WARN_ON(!all_expired
);
1608 tcmu_blocks_release(udev
);
1610 tcmu_netlink_event(udev
, TCMU_CMD_REMOVED_DEVICE
, 0, NULL
);
1612 uio_unregister_device(&udev
->uio_info
);
1614 /* release ref from configure */
1615 kref_put(&udev
->kref
, tcmu_dev_kref_release
);
1619 Opt_dev_config
, Opt_dev_size
, Opt_hw_block_size
, Opt_hw_max_sectors
,
1623 static match_table_t tokens
= {
1624 {Opt_dev_config
, "dev_config=%s"},
1625 {Opt_dev_size
, "dev_size=%u"},
1626 {Opt_hw_block_size
, "hw_block_size=%u"},
1627 {Opt_hw_max_sectors
, "hw_max_sectors=%u"},
1631 static int tcmu_set_dev_attrib(substring_t
*arg
, u32
*dev_attrib
)
1633 unsigned long tmp_ul
;
1637 arg_p
= match_strdup(arg
);
1641 ret
= kstrtoul(arg_p
, 0, &tmp_ul
);
1644 pr_err("kstrtoul() failed for dev attrib\n");
1648 pr_err("dev attrib must be nonzero\n");
1651 *dev_attrib
= tmp_ul
;
1655 static ssize_t
tcmu_set_configfs_dev_params(struct se_device
*dev
,
1656 const char *page
, ssize_t count
)
1658 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
1659 char *orig
, *ptr
, *opts
, *arg_p
;
1660 substring_t args
[MAX_OPT_ARGS
];
1663 opts
= kstrdup(page
, GFP_KERNEL
);
1669 while ((ptr
= strsep(&opts
, ",\n")) != NULL
) {
1673 token
= match_token(ptr
, tokens
, args
);
1675 case Opt_dev_config
:
1676 if (match_strlcpy(udev
->dev_config
, &args
[0],
1677 TCMU_CONFIG_LEN
) == 0) {
1681 pr_debug("TCMU: Referencing Path: %s\n", udev
->dev_config
);
1684 arg_p
= match_strdup(&args
[0]);
1689 ret
= kstrtoul(arg_p
, 0, (unsigned long *) &udev
->dev_size
);
1692 pr_err("kstrtoul() failed for dev_size=\n");
1694 case Opt_hw_block_size
:
1695 ret
= tcmu_set_dev_attrib(&args
[0],
1696 &(dev
->dev_attrib
.hw_block_size
));
1698 case Opt_hw_max_sectors
:
1699 ret
= tcmu_set_dev_attrib(&args
[0],
1700 &(dev
->dev_attrib
.hw_max_sectors
));
1711 return (!ret
) ? count
: ret
;
1714 static ssize_t
tcmu_show_configfs_dev_params(struct se_device
*dev
, char *b
)
1716 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
1719 bl
= sprintf(b
+ bl
, "Config: %s ",
1720 udev
->dev_config
[0] ? udev
->dev_config
: "NULL");
1721 bl
+= sprintf(b
+ bl
, "Size: %zu\n", udev
->dev_size
);
1726 static sector_t
tcmu_get_blocks(struct se_device
*dev
)
1728 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
1730 return div_u64(udev
->dev_size
- dev
->dev_attrib
.block_size
,
1731 dev
->dev_attrib
.block_size
);
1734 static sense_reason_t
1735 tcmu_parse_cdb(struct se_cmd
*cmd
)
1737 return passthrough_parse_cdb(cmd
, tcmu_queue_cmd
);
1740 static ssize_t
tcmu_cmd_time_out_show(struct config_item
*item
, char *page
)
1742 struct se_dev_attrib
*da
= container_of(to_config_group(item
),
1743 struct se_dev_attrib
, da_group
);
1744 struct tcmu_dev
*udev
= container_of(da
->da_dev
,
1745 struct tcmu_dev
, se_dev
);
1747 return snprintf(page
, PAGE_SIZE
, "%lu\n", udev
->cmd_time_out
/ MSEC_PER_SEC
);
1750 static ssize_t
tcmu_cmd_time_out_store(struct config_item
*item
, const char *page
,
1753 struct se_dev_attrib
*da
= container_of(to_config_group(item
),
1754 struct se_dev_attrib
, da_group
);
1755 struct tcmu_dev
*udev
= container_of(da
->da_dev
,
1756 struct tcmu_dev
, se_dev
);
1760 if (da
->da_dev
->export_count
) {
1761 pr_err("Unable to set tcmu cmd_time_out while exports exist\n");
1765 ret
= kstrtou32(page
, 0, &val
);
1769 udev
->cmd_time_out
= val
* MSEC_PER_SEC
;
1772 CONFIGFS_ATTR(tcmu_
, cmd_time_out
);
1774 static ssize_t
tcmu_dev_config_show(struct config_item
*item
, char *page
)
1776 struct se_dev_attrib
*da
= container_of(to_config_group(item
),
1777 struct se_dev_attrib
, da_group
);
1778 struct tcmu_dev
*udev
= TCMU_DEV(da
->da_dev
);
1780 return snprintf(page
, PAGE_SIZE
, "%s\n", udev
->dev_config
);
1783 static ssize_t
tcmu_dev_config_store(struct config_item
*item
, const char *page
,
1786 struct se_dev_attrib
*da
= container_of(to_config_group(item
),
1787 struct se_dev_attrib
, da_group
);
1788 struct tcmu_dev
*udev
= TCMU_DEV(da
->da_dev
);
1792 if (!len
|| len
> TCMU_CONFIG_LEN
- 1)
1795 /* Check if device has been configured before */
1796 if (tcmu_dev_configured(udev
)) {
1797 ret
= tcmu_netlink_event(udev
, TCMU_CMD_RECONFIG_DEVICE
,
1798 TCMU_ATTR_DEV_CFG
, page
);
1800 pr_err("Unable to reconfigure device\n");
1803 strlcpy(udev
->dev_config
, page
, TCMU_CONFIG_LEN
);
1805 ret
= tcmu_update_uio_info(udev
);
1810 strlcpy(udev
->dev_config
, page
, TCMU_CONFIG_LEN
);
1814 CONFIGFS_ATTR(tcmu_
, dev_config
);
1816 static ssize_t
tcmu_dev_size_show(struct config_item
*item
, char *page
)
1818 struct se_dev_attrib
*da
= container_of(to_config_group(item
),
1819 struct se_dev_attrib
, da_group
);
1820 struct tcmu_dev
*udev
= TCMU_DEV(da
->da_dev
);
1822 return snprintf(page
, PAGE_SIZE
, "%zu\n", udev
->dev_size
);
1825 static ssize_t
tcmu_dev_size_store(struct config_item
*item
, const char *page
,
1828 struct se_dev_attrib
*da
= container_of(to_config_group(item
),
1829 struct se_dev_attrib
, da_group
);
1830 struct tcmu_dev
*udev
= TCMU_DEV(da
->da_dev
);
1834 ret
= kstrtou64(page
, 0, &val
);
1838 /* Check if device has been configured before */
1839 if (tcmu_dev_configured(udev
)) {
1840 ret
= tcmu_netlink_event(udev
, TCMU_CMD_RECONFIG_DEVICE
,
1841 TCMU_ATTR_DEV_SIZE
, &val
);
1843 pr_err("Unable to reconfigure device\n");
1847 udev
->dev_size
= val
;
1850 CONFIGFS_ATTR(tcmu_
, dev_size
);
1852 static ssize_t
tcmu_emulate_write_cache_show(struct config_item
*item
,
1855 struct se_dev_attrib
*da
= container_of(to_config_group(item
),
1856 struct se_dev_attrib
, da_group
);
1858 return snprintf(page
, PAGE_SIZE
, "%i\n", da
->emulate_write_cache
);
1861 static ssize_t
tcmu_emulate_write_cache_store(struct config_item
*item
,
1862 const char *page
, size_t count
)
1864 struct se_dev_attrib
*da
= container_of(to_config_group(item
),
1865 struct se_dev_attrib
, da_group
);
1866 struct tcmu_dev
*udev
= TCMU_DEV(da
->da_dev
);
1870 ret
= kstrtou8(page
, 0, &val
);
1874 /* Check if device has been configured before */
1875 if (tcmu_dev_configured(udev
)) {
1876 ret
= tcmu_netlink_event(udev
, TCMU_CMD_RECONFIG_DEVICE
,
1877 TCMU_ATTR_WRITECACHE
, &val
);
1879 pr_err("Unable to reconfigure device\n");
1884 da
->emulate_write_cache
= val
;
1887 CONFIGFS_ATTR(tcmu_
, emulate_write_cache
);
1889 static struct configfs_attribute
*tcmu_attrib_attrs
[] = {
1890 &tcmu_attr_cmd_time_out
,
1891 &tcmu_attr_dev_config
,
1892 &tcmu_attr_dev_size
,
1893 &tcmu_attr_emulate_write_cache
,
1897 static struct configfs_attribute
**tcmu_attrs
;
1899 static struct target_backend_ops tcmu_ops
= {
1901 .owner
= THIS_MODULE
,
1902 .transport_flags
= TRANSPORT_FLAG_PASSTHROUGH
,
1903 .attach_hba
= tcmu_attach_hba
,
1904 .detach_hba
= tcmu_detach_hba
,
1905 .alloc_device
= tcmu_alloc_device
,
1906 .configure_device
= tcmu_configure_device
,
1907 .destroy_device
= tcmu_destroy_device
,
1908 .free_device
= tcmu_free_device
,
1909 .parse_cdb
= tcmu_parse_cdb
,
1910 .set_configfs_dev_params
= tcmu_set_configfs_dev_params
,
1911 .show_configfs_dev_params
= tcmu_show_configfs_dev_params
,
1912 .get_device_type
= sbc_get_device_type
,
1913 .get_blocks
= tcmu_get_blocks
,
1914 .tb_dev_attrib_attrs
= NULL
,
1917 static int unmap_thread_fn(void *data
)
1919 struct tcmu_dev
*udev
;
1921 uint32_t start
, end
, block
;
1925 while (!kthread_should_stop()) {
1926 DEFINE_WAIT(__wait
);
1928 prepare_to_wait(&unmap_wait
, &__wait
, TASK_INTERRUPTIBLE
);
1930 finish_wait(&unmap_wait
, &__wait
);
1932 if (kthread_should_stop())
1935 mutex_lock(&root_udev_mutex
);
1936 list_for_each_entry(udev
, &root_udev
, node
) {
1937 mutex_lock(&udev
->cmdr_lock
);
1939 /* Try to complete the finished commands first */
1940 tcmu_handle_completions(udev
);
1942 /* Skip the udevs waiting the global pool or in idle */
1943 if (udev
->waiting_global
|| !udev
->dbi_thresh
) {
1944 mutex_unlock(&udev
->cmdr_lock
);
1948 end
= udev
->dbi_max
+ 1;
1949 block
= find_last_bit(udev
->data_bitmap
, end
);
1950 if (block
== udev
->dbi_max
) {
1952 * The last bit is dbi_max, so there is
1953 * no need to shrink any blocks.
1955 mutex_unlock(&udev
->cmdr_lock
);
1957 } else if (block
== end
) {
1958 /* The current udev will goto idle state */
1959 udev
->dbi_thresh
= start
= 0;
1962 udev
->dbi_thresh
= start
= block
+ 1;
1963 udev
->dbi_max
= block
;
1966 /* Here will truncate the data area from off */
1967 off
= udev
->data_off
+ start
* DATA_BLOCK_SIZE
;
1968 unmap_mapping_range(udev
->inode
->i_mapping
, off
, 0, 1);
1970 /* Release the block pages */
1971 for (i
= start
; i
< end
; i
++) {
1972 page
= radix_tree_delete(&udev
->data_blocks
, i
);
1975 atomic_dec(&global_db_count
);
1978 mutex_unlock(&udev
->cmdr_lock
);
1982 * Try to wake up the udevs who are waiting
1983 * for the global data pool.
1985 list_for_each_entry(udev
, &root_udev
, node
) {
1986 if (udev
->waiting_global
)
1987 wake_up(&udev
->wait_cmdr
);
1989 mutex_unlock(&root_udev_mutex
);
1995 static int __init
tcmu_module_init(void)
1997 int ret
, i
, k
, len
= 0;
1999 BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry
) % TCMU_OP_ALIGN_SIZE
) != 0);
2001 tcmu_cmd_cache
= kmem_cache_create("tcmu_cmd_cache",
2002 sizeof(struct tcmu_cmd
),
2003 __alignof__(struct tcmu_cmd
),
2005 if (!tcmu_cmd_cache
)
2008 tcmu_root_device
= root_device_register("tcm_user");
2009 if (IS_ERR(tcmu_root_device
)) {
2010 ret
= PTR_ERR(tcmu_root_device
);
2011 goto out_free_cache
;
2014 ret
= genl_register_family(&tcmu_genl_family
);
2016 goto out_unreg_device
;
2019 for (i
= 0; passthrough_attrib_attrs
[i
] != NULL
; i
++) {
2020 len
+= sizeof(struct configfs_attribute
*);
2022 for (i
= 0; tcmu_attrib_attrs
[i
] != NULL
; i
++) {
2023 len
+= sizeof(struct configfs_attribute
*);
2025 len
+= sizeof(struct configfs_attribute
*);
2027 tcmu_attrs
= kzalloc(len
, GFP_KERNEL
);
2030 goto out_unreg_genl
;
2033 for (i
= 0; passthrough_attrib_attrs
[i
] != NULL
; i
++) {
2034 tcmu_attrs
[i
] = passthrough_attrib_attrs
[i
];
2036 for (k
= 0; tcmu_attrib_attrs
[k
] != NULL
; k
++) {
2037 tcmu_attrs
[i
] = tcmu_attrib_attrs
[k
];
2040 tcmu_ops
.tb_dev_attrib_attrs
= tcmu_attrs
;
2042 ret
= transport_backend_register(&tcmu_ops
);
2046 init_waitqueue_head(&unmap_wait
);
2047 unmap_thread
= kthread_run(unmap_thread_fn
, NULL
, "tcmu_unmap");
2048 if (IS_ERR(unmap_thread
)) {
2049 ret
= PTR_ERR(unmap_thread
);
2050 goto out_unreg_transport
;
2055 out_unreg_transport
:
2056 target_backend_unregister(&tcmu_ops
);
2060 genl_unregister_family(&tcmu_genl_family
);
2062 root_device_unregister(tcmu_root_device
);
2064 kmem_cache_destroy(tcmu_cmd_cache
);
2069 static void __exit
tcmu_module_exit(void)
2071 kthread_stop(unmap_thread
);
2072 target_backend_unregister(&tcmu_ops
);
2074 genl_unregister_family(&tcmu_genl_family
);
2075 root_device_unregister(tcmu_root_device
);
2076 kmem_cache_destroy(tcmu_cmd_cache
);
2079 MODULE_DESCRIPTION("TCM USER subsystem plugin");
2080 MODULE_AUTHOR("Shaohua Li <shli@kernel.org>");
2081 MODULE_AUTHOR("Andy Grover <agrover@redhat.com>");
2082 MODULE_LICENSE("GPL");
2084 module_init(tcmu_module_init
);
2085 module_exit(tcmu_module_exit
);