2 * Copyright (C) 2013 Shaohua Li <shli@kernel.org>
3 * Copyright (C) 2014 Red Hat, Inc.
4 * Copyright (C) 2015 Arrikto, Inc.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <linux/spinlock.h>
21 #include <linux/module.h>
22 #include <linux/idr.h>
23 #include <linux/kernel.h>
24 #include <linux/timer.h>
25 #include <linux/parser.h>
26 #include <linux/vmalloc.h>
27 #include <linux/uio_driver.h>
28 #include <linux/stringify.h>
29 #include <linux/bitops.h>
30 #include <net/genetlink.h>
31 #include <scsi/scsi_common.h>
32 #include <scsi/scsi_proto.h>
33 #include <target/target_core_base.h>
34 #include <target/target_core_fabric.h>
35 #include <target/target_core_backend.h>
37 #include <linux/target_core_user.h>
40 * Define a shared-memory interface for LIO to pass SCSI commands and
41 * data to userspace for processing. This is to allow backends that
42 * are too complex for in-kernel support to be possible.
44 * It uses the UIO framework to do a lot of the device-creation and
45 * introspection work for us.
47 * See the .h file for how the ring is laid out. Note that while the
48 * command ring is defined, the particulars of the data area are
49 * not. Offset values in the command entry point to other locations
50 * internal to the mmap()ed area. There is separate space outside the
51 * command ring for data buffers. This leaves maximum flexibility for
52 * moving buffer allocations, or even page flipping or other
53 * allocation techniques, without altering the command ring layout.
56 * The user process must be assumed to be malicious. There's no way to
57 * prevent it breaking the command ring protocol if it wants, but in
58 * order to prevent other issues we must only ever read *data* from
59 * the shared memory area, not offsets or sizes. This applies to
60 * command ring entries as well as the mailbox. Extra code needed for
61 * this may have a 'UAM' comment.
65 #define TCMU_TIME_OUT (30 * MSEC_PER_SEC)
67 #define DATA_BLOCK_BITS 256
68 #define DATA_BLOCK_SIZE 4096
70 #define CMDR_SIZE (16 * 4096)
71 #define DATA_SIZE (DATA_BLOCK_BITS * DATA_BLOCK_SIZE)
73 #define TCMU_RING_SIZE (CMDR_SIZE + DATA_SIZE)
75 static struct device
*tcmu_root_device
;
81 #define TCMU_CONFIG_LEN 256
84 struct se_device se_dev
;
89 #define TCMU_DEV_BIT_OPEN 0
90 #define TCMU_DEV_BIT_BROKEN 1
93 struct uio_info uio_info
;
95 struct tcmu_mailbox
*mb_addr
;
98 u32 cmdr_last_cleaned
;
99 /* Offset of data area from start of mb */
100 /* Must add data_off and mb_addr to get the address */
104 DECLARE_BITMAP(data_bitmap
, DATA_BLOCK_BITS
);
106 wait_queue_head_t wait_cmdr
;
107 /* TODO should this be a mutex? */
108 spinlock_t cmdr_lock
;
111 spinlock_t commands_lock
;
113 struct timer_list timeout
;
115 char dev_config
[TCMU_CONFIG_LEN
];
118 #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev)
120 #define CMDR_OFF sizeof(struct tcmu_mailbox)
123 struct se_cmd
*se_cmd
;
124 struct tcmu_dev
*tcmu_dev
;
128 /* Can't use se_cmd when cleaning up expired cmds, because if
129 cmd has been completed then accessing se_cmd is off limits */
130 DECLARE_BITMAP(data_bitmap
, DATA_BLOCK_BITS
);
132 unsigned long deadline
;
134 #define TCMU_CMD_BIT_EXPIRED 0
138 static struct kmem_cache
*tcmu_cmd_cache
;
140 /* multicast group */
141 enum tcmu_multicast_groups
{
145 static const struct genl_multicast_group tcmu_mcgrps
[] = {
146 [TCMU_MCGRP_CONFIG
] = { .name
= "config", },
149 /* Our generic netlink family */
150 static struct genl_family tcmu_genl_family
= {
151 .id
= GENL_ID_GENERATE
,
155 .maxattr
= TCMU_ATTR_MAX
,
156 .mcgrps
= tcmu_mcgrps
,
157 .n_mcgrps
= ARRAY_SIZE(tcmu_mcgrps
),
161 static struct tcmu_cmd
*tcmu_alloc_cmd(struct se_cmd
*se_cmd
)
163 struct se_device
*se_dev
= se_cmd
->se_dev
;
164 struct tcmu_dev
*udev
= TCMU_DEV(se_dev
);
165 struct tcmu_cmd
*tcmu_cmd
;
168 tcmu_cmd
= kmem_cache_zalloc(tcmu_cmd_cache
, GFP_KERNEL
);
172 tcmu_cmd
->se_cmd
= se_cmd
;
173 tcmu_cmd
->tcmu_dev
= udev
;
174 tcmu_cmd
->deadline
= jiffies
+ msecs_to_jiffies(TCMU_TIME_OUT
);
176 idr_preload(GFP_KERNEL
);
177 spin_lock_irq(&udev
->commands_lock
);
178 cmd_id
= idr_alloc(&udev
->commands
, tcmu_cmd
, 0,
179 USHRT_MAX
, GFP_NOWAIT
);
180 spin_unlock_irq(&udev
->commands_lock
);
184 kmem_cache_free(tcmu_cmd_cache
, tcmu_cmd
);
187 tcmu_cmd
->cmd_id
= cmd_id
;
192 static inline void tcmu_flush_dcache_range(void *vaddr
, size_t size
)
194 unsigned long offset
= offset_in_page(vaddr
);
196 size
= round_up(size
+offset
, PAGE_SIZE
);
200 flush_dcache_page(virt_to_page(vaddr
));
206 * Some ring helper functions. We don't assume size is a power of 2 so
207 * we can't use circ_buf.h.
209 static inline size_t spc_used(size_t head
, size_t tail
, size_t size
)
211 int diff
= head
- tail
;
219 static inline size_t spc_free(size_t head
, size_t tail
, size_t size
)
221 /* Keep 1 byte unused or we can't tell full from empty */
222 return (size
- spc_used(head
, tail
, size
) - 1);
225 static inline size_t head_to_end(size_t head
, size_t size
)
230 static inline void new_iov(struct iovec
**iov
, int *iov_cnt
,
231 struct tcmu_dev
*udev
)
240 memset(iovec
, 0, sizeof(struct iovec
));
243 #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size)
245 /* offset is relative to mb_addr */
246 static inline size_t get_block_offset(struct tcmu_dev
*dev
,
247 int block
, int remaining
)
249 return dev
->data_off
+ block
* DATA_BLOCK_SIZE
+
250 DATA_BLOCK_SIZE
- remaining
;
253 static inline size_t iov_tail(struct tcmu_dev
*udev
, struct iovec
*iov
)
255 return (size_t)iov
->iov_base
+ iov
->iov_len
;
258 static void alloc_and_scatter_data_area(struct tcmu_dev
*udev
,
259 struct scatterlist
*data_sg
, unsigned int data_nents
,
260 struct iovec
**iov
, int *iov_cnt
, bool copy_data
)
263 int block_remaining
= 0;
265 size_t copy_bytes
, to_offset
;
266 struct scatterlist
*sg
;
268 for_each_sg(data_sg
, sg
, data_nents
, i
) {
269 int sg_remaining
= sg
->length
;
270 from
= kmap_atomic(sg_page(sg
)) + sg
->offset
;
271 while (sg_remaining
> 0) {
272 if (block_remaining
== 0) {
273 block
= find_first_zero_bit(udev
->data_bitmap
,
275 block_remaining
= DATA_BLOCK_SIZE
;
276 set_bit(block
, udev
->data_bitmap
);
278 copy_bytes
= min_t(size_t, sg_remaining
,
280 to_offset
= get_block_offset(udev
, block
,
282 to
= (void *)udev
->mb_addr
+ to_offset
;
284 to_offset
== iov_tail(udev
, *iov
)) {
285 (*iov
)->iov_len
+= copy_bytes
;
287 new_iov(iov
, iov_cnt
, udev
);
288 (*iov
)->iov_base
= (void __user
*) to_offset
;
289 (*iov
)->iov_len
= copy_bytes
;
292 memcpy(to
, from
+ sg
->length
- sg_remaining
,
294 tcmu_flush_dcache_range(to
, copy_bytes
);
296 sg_remaining
-= copy_bytes
;
297 block_remaining
-= copy_bytes
;
299 kunmap_atomic(from
- sg
->offset
);
303 static void free_data_area(struct tcmu_dev
*udev
, struct tcmu_cmd
*cmd
)
305 bitmap_xor(udev
->data_bitmap
, udev
->data_bitmap
, cmd
->data_bitmap
,
309 static void gather_data_area(struct tcmu_dev
*udev
, struct tcmu_cmd
*cmd
,
312 struct se_cmd
*se_cmd
= cmd
->se_cmd
;
314 int block_remaining
= 0;
316 size_t copy_bytes
, from_offset
;
317 struct scatterlist
*sg
, *data_sg
;
318 unsigned int data_nents
;
319 DECLARE_BITMAP(bitmap
, DATA_BLOCK_BITS
);
321 bitmap_copy(bitmap
, cmd
->data_bitmap
, DATA_BLOCK_BITS
);
324 data_sg
= se_cmd
->t_data_sg
;
325 data_nents
= se_cmd
->t_data_nents
;
330 * For bidi case, the first count blocks are for Data-Out
331 * buffer blocks, and before gathering the Data-In buffer
332 * the Data-Out buffer blocks should be discarded.
334 count
= DIV_ROUND_UP(se_cmd
->data_length
, DATA_BLOCK_SIZE
);
336 block
= find_first_bit(bitmap
, DATA_BLOCK_BITS
);
337 clear_bit(block
, bitmap
);
340 data_sg
= se_cmd
->t_bidi_data_sg
;
341 data_nents
= se_cmd
->t_bidi_data_nents
;
344 for_each_sg(data_sg
, sg
, data_nents
, i
) {
345 int sg_remaining
= sg
->length
;
346 to
= kmap_atomic(sg_page(sg
)) + sg
->offset
;
347 while (sg_remaining
> 0) {
348 if (block_remaining
== 0) {
349 block
= find_first_bit(bitmap
,
351 block_remaining
= DATA_BLOCK_SIZE
;
352 clear_bit(block
, bitmap
);
354 copy_bytes
= min_t(size_t, sg_remaining
,
356 from_offset
= get_block_offset(udev
, block
,
358 from
= (void *) udev
->mb_addr
+ from_offset
;
359 tcmu_flush_dcache_range(from
, copy_bytes
);
360 memcpy(to
+ sg
->length
- sg_remaining
, from
,
363 sg_remaining
-= copy_bytes
;
364 block_remaining
-= copy_bytes
;
366 kunmap_atomic(to
- sg
->offset
);
370 static inline size_t spc_bitmap_free(unsigned long *bitmap
)
372 return DATA_BLOCK_SIZE
* (DATA_BLOCK_BITS
-
373 bitmap_weight(bitmap
, DATA_BLOCK_BITS
));
377 * We can't queue a command until we have space available on the cmd ring *and*
378 * space available on the data area.
380 * Called with ring lock held.
382 static bool is_ring_space_avail(struct tcmu_dev
*udev
, size_t cmd_size
, size_t data_needed
)
384 struct tcmu_mailbox
*mb
= udev
->mb_addr
;
385 size_t space
, cmd_needed
;
388 tcmu_flush_dcache_range(mb
, sizeof(*mb
));
390 cmd_head
= mb
->cmd_head
% udev
->cmdr_size
; /* UAM */
393 * If cmd end-of-ring space is too small then we need space for a NOP plus
394 * original cmd - cmds are internally contiguous.
396 if (head_to_end(cmd_head
, udev
->cmdr_size
) >= cmd_size
)
397 cmd_needed
= cmd_size
;
399 cmd_needed
= cmd_size
+ head_to_end(cmd_head
, udev
->cmdr_size
);
401 space
= spc_free(cmd_head
, udev
->cmdr_last_cleaned
, udev
->cmdr_size
);
402 if (space
< cmd_needed
) {
403 pr_debug("no cmd space: %u %u %u\n", cmd_head
,
404 udev
->cmdr_last_cleaned
, udev
->cmdr_size
);
408 space
= spc_bitmap_free(udev
->data_bitmap
);
409 if (space
< data_needed
) {
410 pr_debug("no data space: only %zu available, but ask for %zu\n",
418 static inline size_t tcmu_cmd_get_data_length(struct tcmu_cmd
*tcmu_cmd
)
420 struct se_cmd
*se_cmd
= tcmu_cmd
->se_cmd
;
421 size_t data_length
= round_up(se_cmd
->data_length
, DATA_BLOCK_SIZE
);
423 if (se_cmd
->se_cmd_flags
& SCF_BIDI
) {
424 BUG_ON(!(se_cmd
->t_bidi_data_sg
&& se_cmd
->t_bidi_data_nents
));
425 data_length
+= round_up(se_cmd
->t_bidi_data_sg
->length
,
432 static inline uint32_t tcmu_cmd_get_block_cnt(struct tcmu_cmd
*tcmu_cmd
)
434 size_t data_length
= tcmu_cmd_get_data_length(tcmu_cmd
);
436 return data_length
/ DATA_BLOCK_SIZE
;
439 static sense_reason_t
440 tcmu_queue_cmd_ring(struct tcmu_cmd
*tcmu_cmd
)
442 struct tcmu_dev
*udev
= tcmu_cmd
->tcmu_dev
;
443 struct se_cmd
*se_cmd
= tcmu_cmd
->se_cmd
;
444 size_t base_command_size
, command_size
;
445 struct tcmu_mailbox
*mb
;
446 struct tcmu_cmd_entry
*entry
;
451 bool copy_to_data_area
;
452 size_t data_length
= tcmu_cmd_get_data_length(tcmu_cmd
);
453 DECLARE_BITMAP(old_bitmap
, DATA_BLOCK_BITS
);
455 if (test_bit(TCMU_DEV_BIT_BROKEN
, &udev
->flags
))
456 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
459 * Must be a certain minimum size for response sense info, but
460 * also may be larger if the iov array is large.
462 * We prepare way too many iovs for potential uses here, because it's
463 * expensive to tell how many regions are freed in the bitmap
465 base_command_size
= max(offsetof(struct tcmu_cmd_entry
,
466 req
.iov
[tcmu_cmd_get_block_cnt(tcmu_cmd
)]),
467 sizeof(struct tcmu_cmd_entry
));
468 command_size
= base_command_size
469 + round_up(scsi_command_size(se_cmd
->t_task_cdb
), TCMU_OP_ALIGN_SIZE
);
471 WARN_ON(command_size
& (TCMU_OP_ALIGN_SIZE
-1));
473 spin_lock_irq(&udev
->cmdr_lock
);
476 cmd_head
= mb
->cmd_head
% udev
->cmdr_size
; /* UAM */
477 if ((command_size
> (udev
->cmdr_size
/ 2)) ||
478 data_length
> udev
->data_size
) {
479 pr_warn("TCMU: Request of size %zu/%zu is too big for %u/%zu "
480 "cmd ring/data area\n", command_size
, data_length
,
481 udev
->cmdr_size
, udev
->data_size
);
482 spin_unlock_irq(&udev
->cmdr_lock
);
483 return TCM_INVALID_CDB_FIELD
;
486 while (!is_ring_space_avail(udev
, command_size
, data_length
)) {
490 prepare_to_wait(&udev
->wait_cmdr
, &__wait
, TASK_INTERRUPTIBLE
);
492 pr_debug("sleeping for ring space\n");
493 spin_unlock_irq(&udev
->cmdr_lock
);
494 ret
= schedule_timeout(msecs_to_jiffies(TCMU_TIME_OUT
));
495 finish_wait(&udev
->wait_cmdr
, &__wait
);
497 pr_warn("tcmu: command timed out\n");
498 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
501 spin_lock_irq(&udev
->cmdr_lock
);
503 /* We dropped cmdr_lock, cmd_head is stale */
504 cmd_head
= mb
->cmd_head
% udev
->cmdr_size
; /* UAM */
507 /* Insert a PAD if end-of-ring space is too small */
508 if (head_to_end(cmd_head
, udev
->cmdr_size
) < command_size
) {
509 size_t pad_size
= head_to_end(cmd_head
, udev
->cmdr_size
);
511 entry
= (void *) mb
+ CMDR_OFF
+ cmd_head
;
512 tcmu_flush_dcache_range(entry
, sizeof(*entry
));
513 tcmu_hdr_set_op(&entry
->hdr
.len_op
, TCMU_OP_PAD
);
514 tcmu_hdr_set_len(&entry
->hdr
.len_op
, pad_size
);
515 entry
->hdr
.cmd_id
= 0; /* not used for PAD */
516 entry
->hdr
.kflags
= 0;
517 entry
->hdr
.uflags
= 0;
519 UPDATE_HEAD(mb
->cmd_head
, pad_size
, udev
->cmdr_size
);
521 cmd_head
= mb
->cmd_head
% udev
->cmdr_size
; /* UAM */
522 WARN_ON(cmd_head
!= 0);
525 entry
= (void *) mb
+ CMDR_OFF
+ cmd_head
;
526 tcmu_flush_dcache_range(entry
, sizeof(*entry
));
527 tcmu_hdr_set_op(&entry
->hdr
.len_op
, TCMU_OP_CMD
);
528 tcmu_hdr_set_len(&entry
->hdr
.len_op
, command_size
);
529 entry
->hdr
.cmd_id
= tcmu_cmd
->cmd_id
;
530 entry
->hdr
.kflags
= 0;
531 entry
->hdr
.uflags
= 0;
533 bitmap_copy(old_bitmap
, udev
->data_bitmap
, DATA_BLOCK_BITS
);
535 /* Handle allocating space from the data area */
536 iov
= &entry
->req
.iov
[0];
538 copy_to_data_area
= (se_cmd
->data_direction
== DMA_TO_DEVICE
539 || se_cmd
->se_cmd_flags
& SCF_BIDI
);
540 alloc_and_scatter_data_area(udev
, se_cmd
->t_data_sg
,
541 se_cmd
->t_data_nents
, &iov
, &iov_cnt
, copy_to_data_area
);
542 entry
->req
.iov_cnt
= iov_cnt
;
543 entry
->req
.iov_dif_cnt
= 0;
545 /* Handle BIDI commands */
546 if (se_cmd
->se_cmd_flags
& SCF_BIDI
) {
549 alloc_and_scatter_data_area(udev
, se_cmd
->t_bidi_data_sg
,
550 se_cmd
->t_bidi_data_nents
, &iov
, &iov_cnt
,
552 entry
->req
.iov_bidi_cnt
= iov_cnt
;
554 /* cmd's data_bitmap is what changed in process */
555 bitmap_xor(tcmu_cmd
->data_bitmap
, old_bitmap
, udev
->data_bitmap
,
558 /* All offsets relative to mb_addr, not start of entry! */
559 cdb_off
= CMDR_OFF
+ cmd_head
+ base_command_size
;
560 memcpy((void *) mb
+ cdb_off
, se_cmd
->t_task_cdb
, scsi_command_size(se_cmd
->t_task_cdb
));
561 entry
->req
.cdb_off
= cdb_off
;
562 tcmu_flush_dcache_range(entry
, sizeof(*entry
));
564 UPDATE_HEAD(mb
->cmd_head
, command_size
, udev
->cmdr_size
);
565 tcmu_flush_dcache_range(mb
, sizeof(*mb
));
567 spin_unlock_irq(&udev
->cmdr_lock
);
569 /* TODO: only if FLUSH and FUA? */
570 uio_event_notify(&udev
->uio_info
);
572 mod_timer(&udev
->timeout
,
573 round_jiffies_up(jiffies
+ msecs_to_jiffies(TCMU_TIME_OUT
)));
578 static sense_reason_t
579 tcmu_queue_cmd(struct se_cmd
*se_cmd
)
581 struct se_device
*se_dev
= se_cmd
->se_dev
;
582 struct tcmu_dev
*udev
= TCMU_DEV(se_dev
);
583 struct tcmu_cmd
*tcmu_cmd
;
586 tcmu_cmd
= tcmu_alloc_cmd(se_cmd
);
588 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
590 ret
= tcmu_queue_cmd_ring(tcmu_cmd
);
591 if (ret
!= TCM_NO_SENSE
) {
592 pr_err("TCMU: Could not queue command\n");
593 spin_lock_irq(&udev
->commands_lock
);
594 idr_remove(&udev
->commands
, tcmu_cmd
->cmd_id
);
595 spin_unlock_irq(&udev
->commands_lock
);
597 kmem_cache_free(tcmu_cmd_cache
, tcmu_cmd
);
603 static void tcmu_handle_completion(struct tcmu_cmd
*cmd
, struct tcmu_cmd_entry
*entry
)
605 struct se_cmd
*se_cmd
= cmd
->se_cmd
;
606 struct tcmu_dev
*udev
= cmd
->tcmu_dev
;
608 if (test_bit(TCMU_CMD_BIT_EXPIRED
, &cmd
->flags
)) {
610 * cmd has been completed already from timeout, just reclaim
611 * data area space and free cmd
613 free_data_area(udev
, cmd
);
615 kmem_cache_free(tcmu_cmd_cache
, cmd
);
619 if (entry
->hdr
.uflags
& TCMU_UFLAG_UNKNOWN_OP
) {
620 free_data_area(udev
, cmd
);
621 pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n",
623 entry
->rsp
.scsi_status
= SAM_STAT_CHECK_CONDITION
;
624 } else if (entry
->rsp
.scsi_status
== SAM_STAT_CHECK_CONDITION
) {
625 memcpy(se_cmd
->sense_buffer
, entry
->rsp
.sense_buffer
,
626 se_cmd
->scsi_sense_length
);
627 free_data_area(udev
, cmd
);
628 } else if (se_cmd
->se_cmd_flags
& SCF_BIDI
) {
629 /* Get Data-In buffer before clean up */
630 gather_data_area(udev
, cmd
, true);
631 free_data_area(udev
, cmd
);
632 } else if (se_cmd
->data_direction
== DMA_FROM_DEVICE
) {
633 gather_data_area(udev
, cmd
, false);
634 free_data_area(udev
, cmd
);
635 } else if (se_cmd
->data_direction
== DMA_TO_DEVICE
) {
636 free_data_area(udev
, cmd
);
637 } else if (se_cmd
->data_direction
!= DMA_NONE
) {
638 pr_warn("TCMU: data direction was %d!\n",
639 se_cmd
->data_direction
);
642 target_complete_cmd(cmd
->se_cmd
, entry
->rsp
.scsi_status
);
645 kmem_cache_free(tcmu_cmd_cache
, cmd
);
648 static unsigned int tcmu_handle_completions(struct tcmu_dev
*udev
)
650 struct tcmu_mailbox
*mb
;
654 if (test_bit(TCMU_DEV_BIT_BROKEN
, &udev
->flags
)) {
655 pr_err("ring broken, not handling completions\n");
659 spin_lock_irqsave(&udev
->cmdr_lock
, flags
);
662 tcmu_flush_dcache_range(mb
, sizeof(*mb
));
664 while (udev
->cmdr_last_cleaned
!= ACCESS_ONCE(mb
->cmd_tail
)) {
666 struct tcmu_cmd_entry
*entry
= (void *) mb
+ CMDR_OFF
+ udev
->cmdr_last_cleaned
;
667 struct tcmu_cmd
*cmd
;
669 tcmu_flush_dcache_range(entry
, sizeof(*entry
));
671 if (tcmu_hdr_get_op(entry
->hdr
.len_op
) == TCMU_OP_PAD
) {
672 UPDATE_HEAD(udev
->cmdr_last_cleaned
,
673 tcmu_hdr_get_len(entry
->hdr
.len_op
),
677 WARN_ON(tcmu_hdr_get_op(entry
->hdr
.len_op
) != TCMU_OP_CMD
);
679 spin_lock(&udev
->commands_lock
);
680 cmd
= idr_find(&udev
->commands
, entry
->hdr
.cmd_id
);
682 idr_remove(&udev
->commands
, cmd
->cmd_id
);
683 spin_unlock(&udev
->commands_lock
);
686 pr_err("cmd_id not found, ring is broken\n");
687 set_bit(TCMU_DEV_BIT_BROKEN
, &udev
->flags
);
691 tcmu_handle_completion(cmd
, entry
);
693 UPDATE_HEAD(udev
->cmdr_last_cleaned
,
694 tcmu_hdr_get_len(entry
->hdr
.len_op
),
700 if (mb
->cmd_tail
== mb
->cmd_head
)
701 del_timer(&udev
->timeout
); /* no more pending cmds */
703 spin_unlock_irqrestore(&udev
->cmdr_lock
, flags
);
705 wake_up(&udev
->wait_cmdr
);
710 static int tcmu_check_expired_cmd(int id
, void *p
, void *data
)
712 struct tcmu_cmd
*cmd
= p
;
714 if (test_bit(TCMU_CMD_BIT_EXPIRED
, &cmd
->flags
))
717 if (!time_after(jiffies
, cmd
->deadline
))
720 set_bit(TCMU_CMD_BIT_EXPIRED
, &cmd
->flags
);
721 target_complete_cmd(cmd
->se_cmd
, SAM_STAT_CHECK_CONDITION
);
727 static void tcmu_device_timedout(unsigned long data
)
729 struct tcmu_dev
*udev
= (struct tcmu_dev
*)data
;
733 handled
= tcmu_handle_completions(udev
);
735 pr_warn("%d completions handled from timeout\n", handled
);
737 spin_lock_irqsave(&udev
->commands_lock
, flags
);
738 idr_for_each(&udev
->commands
, tcmu_check_expired_cmd
, NULL
);
739 spin_unlock_irqrestore(&udev
->commands_lock
, flags
);
742 * We don't need to wakeup threads on wait_cmdr since they have their
747 static int tcmu_attach_hba(struct se_hba
*hba
, u32 host_id
)
749 struct tcmu_hba
*tcmu_hba
;
751 tcmu_hba
= kzalloc(sizeof(struct tcmu_hba
), GFP_KERNEL
);
755 tcmu_hba
->host_id
= host_id
;
756 hba
->hba_ptr
= tcmu_hba
;
761 static void tcmu_detach_hba(struct se_hba
*hba
)
767 static struct se_device
*tcmu_alloc_device(struct se_hba
*hba
, const char *name
)
769 struct tcmu_dev
*udev
;
771 udev
= kzalloc(sizeof(struct tcmu_dev
), GFP_KERNEL
);
775 udev
->name
= kstrdup(name
, GFP_KERNEL
);
783 init_waitqueue_head(&udev
->wait_cmdr
);
784 spin_lock_init(&udev
->cmdr_lock
);
786 idr_init(&udev
->commands
);
787 spin_lock_init(&udev
->commands_lock
);
789 setup_timer(&udev
->timeout
, tcmu_device_timedout
,
790 (unsigned long)udev
);
792 return &udev
->se_dev
;
795 static int tcmu_irqcontrol(struct uio_info
*info
, s32 irq_on
)
797 struct tcmu_dev
*tcmu_dev
= container_of(info
, struct tcmu_dev
, uio_info
);
799 tcmu_handle_completions(tcmu_dev
);
805 * mmap code from uio.c. Copied here because we want to hook mmap()
806 * and this stuff must come along.
808 static int tcmu_find_mem_index(struct vm_area_struct
*vma
)
810 struct tcmu_dev
*udev
= vma
->vm_private_data
;
811 struct uio_info
*info
= &udev
->uio_info
;
813 if (vma
->vm_pgoff
< MAX_UIO_MAPS
) {
814 if (info
->mem
[vma
->vm_pgoff
].size
== 0)
816 return (int)vma
->vm_pgoff
;
821 static int tcmu_vma_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
823 struct tcmu_dev
*udev
= vma
->vm_private_data
;
824 struct uio_info
*info
= &udev
->uio_info
;
826 unsigned long offset
;
829 int mi
= tcmu_find_mem_index(vma
);
831 return VM_FAULT_SIGBUS
;
834 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
837 offset
= (vmf
->pgoff
- mi
) << PAGE_SHIFT
;
839 addr
= (void *)(unsigned long)info
->mem
[mi
].addr
+ offset
;
840 if (info
->mem
[mi
].memtype
== UIO_MEM_LOGICAL
)
841 page
= virt_to_page(addr
);
843 page
= vmalloc_to_page(addr
);
849 static const struct vm_operations_struct tcmu_vm_ops
= {
850 .fault
= tcmu_vma_fault
,
853 static int tcmu_mmap(struct uio_info
*info
, struct vm_area_struct
*vma
)
855 struct tcmu_dev
*udev
= container_of(info
, struct tcmu_dev
, uio_info
);
857 vma
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
858 vma
->vm_ops
= &tcmu_vm_ops
;
860 vma
->vm_private_data
= udev
;
862 /* Ensure the mmap is exactly the right size */
863 if (vma_pages(vma
) != (TCMU_RING_SIZE
>> PAGE_SHIFT
))
869 static int tcmu_open(struct uio_info
*info
, struct inode
*inode
)
871 struct tcmu_dev
*udev
= container_of(info
, struct tcmu_dev
, uio_info
);
873 /* O_EXCL not supported for char devs, so fake it? */
874 if (test_and_set_bit(TCMU_DEV_BIT_OPEN
, &udev
->flags
))
882 static int tcmu_release(struct uio_info
*info
, struct inode
*inode
)
884 struct tcmu_dev
*udev
= container_of(info
, struct tcmu_dev
, uio_info
);
886 clear_bit(TCMU_DEV_BIT_OPEN
, &udev
->flags
);
893 static int tcmu_netlink_event(enum tcmu_genl_cmd cmd
, const char *name
, int minor
)
899 skb
= genlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
903 msg_header
= genlmsg_put(skb
, 0, 0, &tcmu_genl_family
, 0, cmd
);
907 ret
= nla_put_string(skb
, TCMU_ATTR_DEVICE
, name
);
911 ret
= nla_put_u32(skb
, TCMU_ATTR_MINOR
, minor
);
915 genlmsg_end(skb
, msg_header
);
917 ret
= genlmsg_multicast_allns(&tcmu_genl_family
, skb
, 0,
918 TCMU_MCGRP_CONFIG
, GFP_KERNEL
);
920 /* We don't care if no one is listening */
930 static int tcmu_configure_device(struct se_device
*dev
)
932 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
933 struct tcmu_hba
*hba
= udev
->hba
->hba_ptr
;
934 struct uio_info
*info
;
935 struct tcmu_mailbox
*mb
;
941 info
= &udev
->uio_info
;
943 size
= snprintf(NULL
, 0, "tcm-user/%u/%s/%s", hba
->host_id
, udev
->name
,
945 size
+= 1; /* for \0 */
946 str
= kmalloc(size
, GFP_KERNEL
);
950 used
= snprintf(str
, size
, "tcm-user/%u/%s", hba
->host_id
, udev
->name
);
952 if (udev
->dev_config
[0])
953 snprintf(str
+ used
, size
- used
, "/%s", udev
->dev_config
);
957 udev
->mb_addr
= vzalloc(TCMU_RING_SIZE
);
958 if (!udev
->mb_addr
) {
963 /* mailbox fits in first part of CMDR space */
964 udev
->cmdr_size
= CMDR_SIZE
- CMDR_OFF
;
965 udev
->data_off
= CMDR_SIZE
;
966 udev
->data_size
= TCMU_RING_SIZE
- CMDR_SIZE
;
969 mb
->version
= TCMU_MAILBOX_VERSION
;
970 mb
->flags
= TCMU_MAILBOX_FLAG_CAP_OOOC
;
971 mb
->cmdr_off
= CMDR_OFF
;
972 mb
->cmdr_size
= udev
->cmdr_size
;
974 WARN_ON(!PAGE_ALIGNED(udev
->data_off
));
975 WARN_ON(udev
->data_size
% PAGE_SIZE
);
976 WARN_ON(udev
->data_size
% DATA_BLOCK_SIZE
);
978 info
->version
= __stringify(TCMU_MAILBOX_VERSION
);
980 info
->mem
[0].name
= "tcm-user command & data buffer";
981 info
->mem
[0].addr
= (phys_addr_t
)(uintptr_t)udev
->mb_addr
;
982 info
->mem
[0].size
= TCMU_RING_SIZE
;
983 info
->mem
[0].memtype
= UIO_MEM_VIRTUAL
;
985 info
->irqcontrol
= tcmu_irqcontrol
;
986 info
->irq
= UIO_IRQ_CUSTOM
;
988 info
->mmap
= tcmu_mmap
;
989 info
->open
= tcmu_open
;
990 info
->release
= tcmu_release
;
992 ret
= uio_register_device(tcmu_root_device
, info
);
996 /* User can set hw_block_size before enable the device */
997 if (dev
->dev_attrib
.hw_block_size
== 0)
998 dev
->dev_attrib
.hw_block_size
= 512;
999 /* Other attributes can be configured in userspace */
1000 dev
->dev_attrib
.hw_max_sectors
= 128;
1001 dev
->dev_attrib
.hw_queue_depth
= 128;
1003 ret
= tcmu_netlink_event(TCMU_CMD_ADDED_DEVICE
, udev
->uio_info
.name
,
1004 udev
->uio_info
.uio_dev
->minor
);
1011 uio_unregister_device(&udev
->uio_info
);
1013 vfree(udev
->mb_addr
);
1020 static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd
*cmd
)
1022 if (test_bit(TCMU_CMD_BIT_EXPIRED
, &cmd
->flags
)) {
1023 kmem_cache_free(tcmu_cmd_cache
, cmd
);
1029 static void tcmu_dev_call_rcu(struct rcu_head
*p
)
1031 struct se_device
*dev
= container_of(p
, struct se_device
, rcu_head
);
1032 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
1037 static void tcmu_free_device(struct se_device
*dev
)
1039 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
1040 struct tcmu_cmd
*cmd
;
1041 bool all_expired
= true;
1044 del_timer_sync(&udev
->timeout
);
1046 vfree(udev
->mb_addr
);
1048 /* Upper layer should drain all requests before calling this */
1049 spin_lock_irq(&udev
->commands_lock
);
1050 idr_for_each_entry(&udev
->commands
, cmd
, i
) {
1051 if (tcmu_check_and_free_pending_cmd(cmd
) != 0)
1052 all_expired
= false;
1054 idr_destroy(&udev
->commands
);
1055 spin_unlock_irq(&udev
->commands_lock
);
1056 WARN_ON(!all_expired
);
1058 /* Device was configured */
1059 if (udev
->uio_info
.uio_dev
) {
1060 tcmu_netlink_event(TCMU_CMD_REMOVED_DEVICE
, udev
->uio_info
.name
,
1061 udev
->uio_info
.uio_dev
->minor
);
1063 uio_unregister_device(&udev
->uio_info
);
1064 kfree(udev
->uio_info
.name
);
1067 call_rcu(&dev
->rcu_head
, tcmu_dev_call_rcu
);
1071 Opt_dev_config
, Opt_dev_size
, Opt_hw_block_size
, Opt_err
,
1074 static match_table_t tokens
= {
1075 {Opt_dev_config
, "dev_config=%s"},
1076 {Opt_dev_size
, "dev_size=%u"},
1077 {Opt_hw_block_size
, "hw_block_size=%u"},
1081 static ssize_t
tcmu_set_configfs_dev_params(struct se_device
*dev
,
1082 const char *page
, ssize_t count
)
1084 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
1085 char *orig
, *ptr
, *opts
, *arg_p
;
1086 substring_t args
[MAX_OPT_ARGS
];
1088 unsigned long tmp_ul
;
1090 opts
= kstrdup(page
, GFP_KERNEL
);
1096 while ((ptr
= strsep(&opts
, ",\n")) != NULL
) {
1100 token
= match_token(ptr
, tokens
, args
);
1102 case Opt_dev_config
:
1103 if (match_strlcpy(udev
->dev_config
, &args
[0],
1104 TCMU_CONFIG_LEN
) == 0) {
1108 pr_debug("TCMU: Referencing Path: %s\n", udev
->dev_config
);
1111 arg_p
= match_strdup(&args
[0]);
1116 ret
= kstrtoul(arg_p
, 0, (unsigned long *) &udev
->dev_size
);
1119 pr_err("kstrtoul() failed for dev_size=\n");
1121 case Opt_hw_block_size
:
1122 arg_p
= match_strdup(&args
[0]);
1127 ret
= kstrtoul(arg_p
, 0, &tmp_ul
);
1130 pr_err("kstrtoul() failed for hw_block_size=\n");
1134 pr_err("hw_block_size must be nonzero\n");
1137 dev
->dev_attrib
.hw_block_size
= tmp_ul
;
1145 return (!ret
) ? count
: ret
;
1148 static ssize_t
tcmu_show_configfs_dev_params(struct se_device
*dev
, char *b
)
1150 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
1153 bl
= sprintf(b
+ bl
, "Config: %s ",
1154 udev
->dev_config
[0] ? udev
->dev_config
: "NULL");
1155 bl
+= sprintf(b
+ bl
, "Size: %zu\n", udev
->dev_size
);
1160 static sector_t
tcmu_get_blocks(struct se_device
*dev
)
1162 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
1164 return div_u64(udev
->dev_size
- dev
->dev_attrib
.block_size
,
1165 dev
->dev_attrib
.block_size
);
1168 static sense_reason_t
1169 tcmu_parse_cdb(struct se_cmd
*cmd
)
1171 return passthrough_parse_cdb(cmd
, tcmu_queue_cmd
);
1174 static const struct target_backend_ops tcmu_ops
= {
1176 .owner
= THIS_MODULE
,
1177 .transport_flags
= TRANSPORT_FLAG_PASSTHROUGH
,
1178 .attach_hba
= tcmu_attach_hba
,
1179 .detach_hba
= tcmu_detach_hba
,
1180 .alloc_device
= tcmu_alloc_device
,
1181 .configure_device
= tcmu_configure_device
,
1182 .free_device
= tcmu_free_device
,
1183 .parse_cdb
= tcmu_parse_cdb
,
1184 .set_configfs_dev_params
= tcmu_set_configfs_dev_params
,
1185 .show_configfs_dev_params
= tcmu_show_configfs_dev_params
,
1186 .get_device_type
= sbc_get_device_type
,
1187 .get_blocks
= tcmu_get_blocks
,
1188 .tb_dev_attrib_attrs
= passthrough_attrib_attrs
,
1191 static int __init
tcmu_module_init(void)
1195 BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry
) % TCMU_OP_ALIGN_SIZE
) != 0);
1197 tcmu_cmd_cache
= kmem_cache_create("tcmu_cmd_cache",
1198 sizeof(struct tcmu_cmd
),
1199 __alignof__(struct tcmu_cmd
),
1201 if (!tcmu_cmd_cache
)
1204 tcmu_root_device
= root_device_register("tcm_user");
1205 if (IS_ERR(tcmu_root_device
)) {
1206 ret
= PTR_ERR(tcmu_root_device
);
1207 goto out_free_cache
;
1210 ret
= genl_register_family(&tcmu_genl_family
);
1212 goto out_unreg_device
;
1215 ret
= transport_backend_register(&tcmu_ops
);
1217 goto out_unreg_genl
;
1222 genl_unregister_family(&tcmu_genl_family
);
1224 root_device_unregister(tcmu_root_device
);
1226 kmem_cache_destroy(tcmu_cmd_cache
);
1231 static void __exit
tcmu_module_exit(void)
1233 target_backend_unregister(&tcmu_ops
);
1234 genl_unregister_family(&tcmu_genl_family
);
1235 root_device_unregister(tcmu_root_device
);
1236 kmem_cache_destroy(tcmu_cmd_cache
);
1239 MODULE_DESCRIPTION("TCM USER subsystem plugin");
1240 MODULE_AUTHOR("Shaohua Li <shli@kernel.org>");
1241 MODULE_AUTHOR("Andy Grover <agrover@redhat.com>");
1242 MODULE_LICENSE("GPL");
1244 module_init(tcmu_module_init
);
1245 module_exit(tcmu_module_exit
);