2 * Copyright (C) 2013 Shaohua Li <shli@kernel.org>
3 * Copyright (C) 2014 Red Hat, Inc.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <linux/spinlock.h>
20 #include <linux/module.h>
21 #include <linux/idr.h>
22 #include <linux/timer.h>
23 #include <linux/parser.h>
24 #include <scsi/scsi.h>
25 #include <scsi/scsi_host.h>
26 #include <linux/uio_driver.h>
27 #include <net/genetlink.h>
28 #include <target/target_core_base.h>
29 #include <target/target_core_fabric.h>
30 #include <target/target_core_backend.h>
31 #include <target/target_core_backend_configfs.h>
33 #include <linux/target_core_user.h>
36 * Define a shared-memory interface for LIO to pass SCSI commands and
37 * data to userspace for processing. This is to allow backends that
38 * are too complex for in-kernel support to be possible.
40 * It uses the UIO framework to do a lot of the device-creation and
41 * introspection work for us.
43 * See the .h file for how the ring is laid out. Note that while the
44 * command ring is defined, the particulars of the data area are
45 * not. Offset values in the command entry point to other locations
46 * internal to the mmap()ed area. There is separate space outside the
47 * command ring for data buffers. This leaves maximum flexibility for
48 * moving buffer allocations, or even page flipping or other
49 * allocation techniques, without altering the command ring layout.
52 * The user process must be assumed to be malicious. There's no way to
53 * prevent it breaking the command ring protocol if it wants, but in
54 * order to prevent other issues we must only ever read *data* from
55 * the shared memory area, not offsets or sizes. This applies to
56 * command ring entries as well as the mailbox. Extra code needed for
57 * this may have a 'UAM' comment.
61 #define TCMU_TIME_OUT (30 * MSEC_PER_SEC)
63 #define CMDR_SIZE (16 * 4096)
64 #define DATA_SIZE (257 * 4096)
66 #define TCMU_RING_SIZE (CMDR_SIZE + DATA_SIZE)
68 static struct device
*tcmu_root_device
;
74 /* User wants all cmds or just some */
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
92 enum passthru_level pass_level
;
94 struct uio_info uio_info
;
96 struct tcmu_mailbox
*mb_addr
;
99 u32 cmdr_last_cleaned
;
100 /* Offset of data ring from start of mb */
103 /* Ring head + tail values. */
104 /* Must add data_off and mb_addr to get the address */
108 wait_queue_head_t wait_cmdr
;
109 /* TODO should this be a mutex? */
110 spinlock_t cmdr_lock
;
113 spinlock_t commands_lock
;
115 struct timer_list timeout
;
117 char dev_config
[TCMU_CONFIG_LEN
];
120 #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev)
122 #define CMDR_OFF sizeof(struct tcmu_mailbox)
125 struct se_cmd
*se_cmd
;
126 struct tcmu_dev
*tcmu_dev
;
130 /* Can't use se_cmd->data_length when cleaning up expired cmds, because if
131 cmd has been completed then accessing se_cmd is off limits */
134 unsigned long deadline
;
136 #define TCMU_CMD_BIT_EXPIRED 0
140 static struct kmem_cache
*tcmu_cmd_cache
;
142 /* multicast group */
143 enum tcmu_multicast_groups
{
147 static const struct genl_multicast_group tcmu_mcgrps
[] = {
148 [TCMU_MCGRP_CONFIG
] = { .name
= "config", },
151 /* Our generic netlink family */
152 static struct genl_family tcmu_genl_family
= {
153 .id
= GENL_ID_GENERATE
,
157 .maxattr
= TCMU_ATTR_MAX
,
158 .mcgrps
= tcmu_mcgrps
,
159 .n_mcgrps
= ARRAY_SIZE(tcmu_mcgrps
),
162 static struct tcmu_cmd
*tcmu_alloc_cmd(struct se_cmd
*se_cmd
)
164 struct se_device
*se_dev
= se_cmd
->se_dev
;
165 struct tcmu_dev
*udev
= TCMU_DEV(se_dev
);
166 struct tcmu_cmd
*tcmu_cmd
;
169 tcmu_cmd
= kmem_cache_zalloc(tcmu_cmd_cache
, GFP_KERNEL
);
173 tcmu_cmd
->se_cmd
= se_cmd
;
174 tcmu_cmd
->tcmu_dev
= udev
;
175 tcmu_cmd
->data_length
= se_cmd
->data_length
;
177 tcmu_cmd
->deadline
= jiffies
+ msecs_to_jiffies(TCMU_TIME_OUT
);
179 idr_preload(GFP_KERNEL
);
180 spin_lock_irq(&udev
->commands_lock
);
181 cmd_id
= idr_alloc(&udev
->commands
, tcmu_cmd
, 0,
182 USHRT_MAX
, GFP_NOWAIT
);
183 spin_unlock_irq(&udev
->commands_lock
);
187 kmem_cache_free(tcmu_cmd_cache
, tcmu_cmd
);
190 tcmu_cmd
->cmd_id
= cmd_id
;
195 static inline void tcmu_flush_dcache_range(void *vaddr
, size_t size
)
197 unsigned long offset
= (unsigned long) vaddr
& ~PAGE_MASK
;
199 size
= round_up(size
+offset
, PAGE_SIZE
);
203 flush_dcache_page(virt_to_page(vaddr
));
209 * Some ring helper functions. We don't assume size is a power of 2 so
210 * we can't use circ_buf.h.
212 static inline size_t spc_used(size_t head
, size_t tail
, size_t size
)
214 int diff
= head
- tail
;
222 static inline size_t spc_free(size_t head
, size_t tail
, size_t size
)
224 /* Keep 1 byte unused or we can't tell full from empty */
225 return (size
- spc_used(head
, tail
, size
) - 1);
228 static inline size_t head_to_end(size_t head
, size_t size
)
233 #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size)
236 * We can't queue a command until we have space available on the cmd ring *and* space
237 * space avail on the data ring.
239 * Called with ring lock held.
241 static bool is_ring_space_avail(struct tcmu_dev
*udev
, size_t cmd_size
, size_t data_needed
)
243 struct tcmu_mailbox
*mb
= udev
->mb_addr
;
248 tcmu_flush_dcache_range(mb
, sizeof(*mb
));
250 cmd_head
= mb
->cmd_head
% udev
->cmdr_size
; /* UAM */
253 * If cmd end-of-ring space is too small then we need space for a NOP plus
254 * original cmd - cmds are internally contiguous.
256 if (head_to_end(cmd_head
, udev
->cmdr_size
) >= cmd_size
)
257 cmd_needed
= cmd_size
;
259 cmd_needed
= cmd_size
+ head_to_end(cmd_head
, udev
->cmdr_size
);
261 space
= spc_free(cmd_head
, udev
->cmdr_last_cleaned
, udev
->cmdr_size
);
262 if (space
< cmd_needed
) {
263 pr_debug("no cmd space: %u %u %u\n", cmd_head
,
264 udev
->cmdr_last_cleaned
, udev
->cmdr_size
);
268 space
= spc_free(udev
->data_head
, udev
->data_tail
, udev
->data_size
);
269 if (space
< data_needed
) {
270 pr_debug("no data space: %zu %zu %zu\n", udev
->data_head
,
271 udev
->data_tail
, udev
->data_size
);
278 static int tcmu_queue_cmd_ring(struct tcmu_cmd
*tcmu_cmd
)
280 struct tcmu_dev
*udev
= tcmu_cmd
->tcmu_dev
;
281 struct se_cmd
*se_cmd
= tcmu_cmd
->se_cmd
;
282 size_t base_command_size
, command_size
;
283 struct tcmu_mailbox
*mb
;
284 struct tcmu_cmd_entry
*entry
;
286 struct scatterlist
*sg
;
292 if (test_bit(TCMU_DEV_BIT_BROKEN
, &udev
->flags
))
296 * Must be a certain minimum size for response sense info, but
297 * also may be larger if the iov array is large.
299 * iovs = sgl_nents+1, for end-of-ring case, plus another 1
300 * b/c size == offsetof one-past-element.
302 base_command_size
= max(offsetof(struct tcmu_cmd_entry
,
303 req
.iov
[se_cmd
->t_data_nents
+ 2]),
304 sizeof(struct tcmu_cmd_entry
));
305 command_size
= base_command_size
306 + round_up(scsi_command_size(se_cmd
->t_task_cdb
), TCMU_OP_ALIGN_SIZE
);
308 WARN_ON(command_size
& (TCMU_OP_ALIGN_SIZE
-1));
310 spin_lock_irq(&udev
->cmdr_lock
);
313 cmd_head
= mb
->cmd_head
% udev
->cmdr_size
; /* UAM */
314 if ((command_size
> (udev
->cmdr_size
/ 2))
315 || tcmu_cmd
->data_length
> (udev
->data_size
- 1))
316 pr_warn("TCMU: Request of size %zu/%zu may be too big for %u/%zu "
317 "cmd/data ring buffers\n", command_size
, tcmu_cmd
->data_length
,
318 udev
->cmdr_size
, udev
->data_size
);
320 while (!is_ring_space_avail(udev
, command_size
, tcmu_cmd
->data_length
)) {
324 prepare_to_wait(&udev
->wait_cmdr
, &__wait
, TASK_INTERRUPTIBLE
);
326 pr_debug("sleeping for ring space\n");
327 spin_unlock_irq(&udev
->cmdr_lock
);
328 ret
= schedule_timeout(msecs_to_jiffies(TCMU_TIME_OUT
));
329 finish_wait(&udev
->wait_cmdr
, &__wait
);
331 pr_warn("tcmu: command timed out\n");
335 spin_lock_irq(&udev
->cmdr_lock
);
337 /* We dropped cmdr_lock, cmd_head is stale */
338 cmd_head
= mb
->cmd_head
% udev
->cmdr_size
; /* UAM */
341 /* Insert a PAD if end-of-ring space is too small */
342 if (head_to_end(cmd_head
, udev
->cmdr_size
) < command_size
) {
343 size_t pad_size
= head_to_end(cmd_head
, udev
->cmdr_size
);
345 entry
= (void *) mb
+ CMDR_OFF
+ cmd_head
;
346 tcmu_flush_dcache_range(entry
, sizeof(*entry
));
347 tcmu_hdr_set_op(&entry
->hdr
, TCMU_OP_PAD
);
348 tcmu_hdr_set_len(&entry
->hdr
, pad_size
);
350 UPDATE_HEAD(mb
->cmd_head
, pad_size
, udev
->cmdr_size
);
352 cmd_head
= mb
->cmd_head
% udev
->cmdr_size
; /* UAM */
353 WARN_ON(cmd_head
!= 0);
356 entry
= (void *) mb
+ CMDR_OFF
+ cmd_head
;
357 tcmu_flush_dcache_range(entry
, sizeof(*entry
));
358 tcmu_hdr_set_op(&entry
->hdr
, TCMU_OP_CMD
);
359 tcmu_hdr_set_len(&entry
->hdr
, command_size
);
360 entry
->cmd_id
= tcmu_cmd
->cmd_id
;
363 * Fix up iovecs, and handle if allocation in data ring wrapped.
365 iov
= &entry
->req
.iov
[0];
366 for_each_sg(se_cmd
->t_data_sg
, sg
, se_cmd
->t_data_nents
, i
) {
367 size_t copy_bytes
= min((size_t)sg
->length
,
368 head_to_end(udev
->data_head
, udev
->data_size
));
369 void *from
= kmap_atomic(sg_page(sg
)) + sg
->offset
;
370 void *to
= (void *) mb
+ udev
->data_off
+ udev
->data_head
;
372 if (tcmu_cmd
->se_cmd
->data_direction
== DMA_TO_DEVICE
) {
373 memcpy(to
, from
, copy_bytes
);
374 tcmu_flush_dcache_range(to
, copy_bytes
);
377 /* Even iov_base is relative to mb_addr */
378 iov
->iov_len
= copy_bytes
;
379 iov
->iov_base
= (void *) udev
->data_off
+ udev
->data_head
;
383 UPDATE_HEAD(udev
->data_head
, copy_bytes
, udev
->data_size
);
385 /* Uh oh, we wrapped the buffer. Must split sg across 2 iovs. */
386 if (sg
->length
!= copy_bytes
) {
388 copy_bytes
= sg
->length
- copy_bytes
;
390 iov
->iov_len
= copy_bytes
;
391 iov
->iov_base
= (void *) udev
->data_off
+ udev
->data_head
;
393 if (se_cmd
->data_direction
== DMA_TO_DEVICE
) {
394 to
= (void *) mb
+ udev
->data_off
+ udev
->data_head
;
395 memcpy(to
, from
, copy_bytes
);
396 tcmu_flush_dcache_range(to
, copy_bytes
);
402 UPDATE_HEAD(udev
->data_head
, copy_bytes
, udev
->data_size
);
407 entry
->req
.iov_cnt
= iov_cnt
;
409 /* All offsets relative to mb_addr, not start of entry! */
410 cdb_off
= CMDR_OFF
+ cmd_head
+ base_command_size
;
411 memcpy((void *) mb
+ cdb_off
, se_cmd
->t_task_cdb
, scsi_command_size(se_cmd
->t_task_cdb
));
412 entry
->req
.cdb_off
= cdb_off
;
413 tcmu_flush_dcache_range(entry
, sizeof(*entry
));
415 UPDATE_HEAD(mb
->cmd_head
, command_size
, udev
->cmdr_size
);
416 tcmu_flush_dcache_range(mb
, sizeof(*mb
));
418 spin_unlock_irq(&udev
->cmdr_lock
);
420 /* TODO: only if FLUSH and FUA? */
421 uio_event_notify(&udev
->uio_info
);
423 mod_timer(&udev
->timeout
,
424 round_jiffies_up(jiffies
+ msecs_to_jiffies(TCMU_TIME_OUT
)));
429 static int tcmu_queue_cmd(struct se_cmd
*se_cmd
)
431 struct se_device
*se_dev
= se_cmd
->se_dev
;
432 struct tcmu_dev
*udev
= TCMU_DEV(se_dev
);
433 struct tcmu_cmd
*tcmu_cmd
;
436 tcmu_cmd
= tcmu_alloc_cmd(se_cmd
);
440 ret
= tcmu_queue_cmd_ring(tcmu_cmd
);
442 pr_err("TCMU: Could not queue command\n");
443 spin_lock_irq(&udev
->commands_lock
);
444 idr_remove(&udev
->commands
, tcmu_cmd
->cmd_id
);
445 spin_unlock_irq(&udev
->commands_lock
);
447 kmem_cache_free(tcmu_cmd_cache
, tcmu_cmd
);
453 static void tcmu_handle_completion(struct tcmu_cmd
*cmd
, struct tcmu_cmd_entry
*entry
)
455 struct se_cmd
*se_cmd
= cmd
->se_cmd
;
456 struct tcmu_dev
*udev
= cmd
->tcmu_dev
;
458 if (test_bit(TCMU_CMD_BIT_EXPIRED
, &cmd
->flags
)) {
459 /* cmd has been completed already from timeout, just reclaim data
461 UPDATE_HEAD(udev
->data_tail
, cmd
->data_length
, udev
->data_size
);
465 if (entry
->rsp
.scsi_status
== SAM_STAT_CHECK_CONDITION
) {
466 memcpy(se_cmd
->sense_buffer
, entry
->rsp
.sense_buffer
,
467 se_cmd
->scsi_sense_length
);
469 UPDATE_HEAD(udev
->data_tail
, cmd
->data_length
, udev
->data_size
);
471 else if (se_cmd
->data_direction
== DMA_FROM_DEVICE
) {
472 struct scatterlist
*sg
;
475 /* It'd be easier to look at entry's iovec again, but UAM */
476 for_each_sg(se_cmd
->t_data_sg
, sg
, se_cmd
->t_data_nents
, i
) {
481 copy_bytes
= min((size_t)sg
->length
,
482 head_to_end(udev
->data_tail
, udev
->data_size
));
484 to
= kmap_atomic(sg_page(sg
)) + sg
->offset
;
485 WARN_ON(sg
->length
+ sg
->offset
> PAGE_SIZE
);
486 from
= (void *) udev
->mb_addr
+ udev
->data_off
+ udev
->data_tail
;
487 tcmu_flush_dcache_range(from
, copy_bytes
);
488 memcpy(to
, from
, copy_bytes
);
490 UPDATE_HEAD(udev
->data_tail
, copy_bytes
, udev
->data_size
);
492 /* Uh oh, wrapped the data buffer for this sg's data */
493 if (sg
->length
!= copy_bytes
) {
494 from
= (void *) udev
->mb_addr
+ udev
->data_off
+ udev
->data_tail
;
495 WARN_ON(udev
->data_tail
);
497 copy_bytes
= sg
->length
- copy_bytes
;
498 tcmu_flush_dcache_range(from
, copy_bytes
);
499 memcpy(to
, from
, copy_bytes
);
501 UPDATE_HEAD(udev
->data_tail
, copy_bytes
, udev
->data_size
);
507 } else if (se_cmd
->data_direction
== DMA_TO_DEVICE
) {
508 UPDATE_HEAD(udev
->data_tail
, cmd
->data_length
, udev
->data_size
);
510 pr_warn("TCMU: data direction was %d!\n", se_cmd
->data_direction
);
513 target_complete_cmd(cmd
->se_cmd
, entry
->rsp
.scsi_status
);
516 kmem_cache_free(tcmu_cmd_cache
, cmd
);
519 static unsigned int tcmu_handle_completions(struct tcmu_dev
*udev
)
521 struct tcmu_mailbox
*mb
;
526 if (test_bit(TCMU_DEV_BIT_BROKEN
, &udev
->flags
)) {
527 pr_err("ring broken, not handling completions\n");
531 spin_lock_irqsave(&udev
->cmdr_lock
, flags
);
534 tcmu_flush_dcache_range(mb
, sizeof(*mb
));
536 while (udev
->cmdr_last_cleaned
!= ACCESS_ONCE(mb
->cmd_tail
)) {
538 struct tcmu_cmd_entry
*entry
= (void *) mb
+ CMDR_OFF
+ udev
->cmdr_last_cleaned
;
539 struct tcmu_cmd
*cmd
;
541 tcmu_flush_dcache_range(entry
, sizeof(*entry
));
543 if (tcmu_hdr_get_op(&entry
->hdr
) == TCMU_OP_PAD
) {
544 UPDATE_HEAD(udev
->cmdr_last_cleaned
, tcmu_hdr_get_len(&entry
->hdr
), udev
->cmdr_size
);
547 WARN_ON(tcmu_hdr_get_op(&entry
->hdr
) != TCMU_OP_CMD
);
549 spin_lock(&udev
->commands_lock
);
550 cmd
= idr_find(&udev
->commands
, entry
->cmd_id
);
552 idr_remove(&udev
->commands
, cmd
->cmd_id
);
553 spin_unlock(&udev
->commands_lock
);
556 pr_err("cmd_id not found, ring is broken\n");
557 set_bit(TCMU_DEV_BIT_BROKEN
, &udev
->flags
);
561 tcmu_handle_completion(cmd
, entry
);
563 UPDATE_HEAD(udev
->cmdr_last_cleaned
, tcmu_hdr_get_len(&entry
->hdr
), udev
->cmdr_size
);
568 if (mb
->cmd_tail
== mb
->cmd_head
)
569 del_timer(&udev
->timeout
); /* no more pending cmds */
571 spin_unlock_irqrestore(&udev
->cmdr_lock
, flags
);
573 wake_up(&udev
->wait_cmdr
);
578 static int tcmu_check_expired_cmd(int id
, void *p
, void *data
)
580 struct tcmu_cmd
*cmd
= p
;
582 if (test_bit(TCMU_CMD_BIT_EXPIRED
, &cmd
->flags
))
585 if (!time_after(cmd
->deadline
, jiffies
))
588 set_bit(TCMU_CMD_BIT_EXPIRED
, &cmd
->flags
);
589 target_complete_cmd(cmd
->se_cmd
, SAM_STAT_CHECK_CONDITION
);
592 kmem_cache_free(tcmu_cmd_cache
, cmd
);
597 static void tcmu_device_timedout(unsigned long data
)
599 struct tcmu_dev
*udev
= (struct tcmu_dev
*)data
;
603 handled
= tcmu_handle_completions(udev
);
605 pr_warn("%d completions handled from timeout\n", handled
);
607 spin_lock_irqsave(&udev
->commands_lock
, flags
);
608 idr_for_each(&udev
->commands
, tcmu_check_expired_cmd
, NULL
);
609 spin_unlock_irqrestore(&udev
->commands_lock
, flags
);
612 * We don't need to wakeup threads on wait_cmdr since they have their
617 static int tcmu_attach_hba(struct se_hba
*hba
, u32 host_id
)
619 struct tcmu_hba
*tcmu_hba
;
621 tcmu_hba
= kzalloc(sizeof(struct tcmu_hba
), GFP_KERNEL
);
625 tcmu_hba
->host_id
= host_id
;
626 hba
->hba_ptr
= tcmu_hba
;
631 static void tcmu_detach_hba(struct se_hba
*hba
)
637 static struct se_device
*tcmu_alloc_device(struct se_hba
*hba
, const char *name
)
639 struct tcmu_dev
*udev
;
641 udev
= kzalloc(sizeof(struct tcmu_dev
), GFP_KERNEL
);
645 udev
->name
= kstrdup(name
, GFP_KERNEL
);
653 init_waitqueue_head(&udev
->wait_cmdr
);
654 spin_lock_init(&udev
->cmdr_lock
);
656 idr_init(&udev
->commands
);
657 spin_lock_init(&udev
->commands_lock
);
659 setup_timer(&udev
->timeout
, tcmu_device_timedout
,
660 (unsigned long)udev
);
662 udev
->pass_level
= TCMU_PASS_ALL
;
664 return &udev
->se_dev
;
667 static int tcmu_irqcontrol(struct uio_info
*info
, s32 irq_on
)
669 struct tcmu_dev
*tcmu_dev
= container_of(info
, struct tcmu_dev
, uio_info
);
671 tcmu_handle_completions(tcmu_dev
);
677 * mmap code from uio.c. Copied here because we want to hook mmap()
678 * and this stuff must come along.
680 static int tcmu_find_mem_index(struct vm_area_struct
*vma
)
682 struct tcmu_dev
*udev
= vma
->vm_private_data
;
683 struct uio_info
*info
= &udev
->uio_info
;
685 if (vma
->vm_pgoff
< MAX_UIO_MAPS
) {
686 if (info
->mem
[vma
->vm_pgoff
].size
== 0)
688 return (int)vma
->vm_pgoff
;
693 static int tcmu_vma_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
695 struct tcmu_dev
*udev
= vma
->vm_private_data
;
696 struct uio_info
*info
= &udev
->uio_info
;
698 unsigned long offset
;
701 int mi
= tcmu_find_mem_index(vma
);
703 return VM_FAULT_SIGBUS
;
706 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
709 offset
= (vmf
->pgoff
- mi
) << PAGE_SHIFT
;
711 addr
= (void *)(unsigned long)info
->mem
[mi
].addr
+ offset
;
712 if (info
->mem
[mi
].memtype
== UIO_MEM_LOGICAL
)
713 page
= virt_to_page(addr
);
715 page
= vmalloc_to_page(addr
);
721 static const struct vm_operations_struct tcmu_vm_ops
= {
722 .fault
= tcmu_vma_fault
,
725 static int tcmu_mmap(struct uio_info
*info
, struct vm_area_struct
*vma
)
727 struct tcmu_dev
*udev
= container_of(info
, struct tcmu_dev
, uio_info
);
729 vma
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
730 vma
->vm_ops
= &tcmu_vm_ops
;
732 vma
->vm_private_data
= udev
;
734 /* Ensure the mmap is exactly the right size */
735 if (vma_pages(vma
) != (TCMU_RING_SIZE
>> PAGE_SHIFT
))
741 static int tcmu_open(struct uio_info
*info
, struct inode
*inode
)
743 struct tcmu_dev
*udev
= container_of(info
, struct tcmu_dev
, uio_info
);
745 /* O_EXCL not supported for char devs, so fake it? */
746 if (test_and_set_bit(TCMU_DEV_BIT_OPEN
, &udev
->flags
))
754 static int tcmu_release(struct uio_info
*info
, struct inode
*inode
)
756 struct tcmu_dev
*udev
= container_of(info
, struct tcmu_dev
, uio_info
);
758 clear_bit(TCMU_DEV_BIT_OPEN
, &udev
->flags
);
765 static int tcmu_netlink_event(enum tcmu_genl_cmd cmd
, const char *name
, int minor
)
771 skb
= genlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
775 msg_header
= genlmsg_put(skb
, 0, 0, &tcmu_genl_family
, 0, cmd
);
779 ret
= nla_put_string(skb
, TCMU_ATTR_DEVICE
, name
);
783 ret
= nla_put_u32(skb
, TCMU_ATTR_MINOR
, minor
);
787 ret
= genlmsg_end(skb
, msg_header
);
791 ret
= genlmsg_multicast(&tcmu_genl_family
, skb
, 0,
792 TCMU_MCGRP_CONFIG
, GFP_KERNEL
);
794 /* We don't care if no one is listening */
804 static int tcmu_configure_device(struct se_device
*dev
)
806 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
807 struct tcmu_hba
*hba
= udev
->hba
->hba_ptr
;
808 struct uio_info
*info
;
809 struct tcmu_mailbox
*mb
;
815 info
= &udev
->uio_info
;
817 size
= snprintf(NULL
, 0, "tcm-user/%u/%s/%s", hba
->host_id
, udev
->name
,
819 size
+= 1; /* for \0 */
820 str
= kmalloc(size
, GFP_KERNEL
);
824 used
= snprintf(str
, size
, "tcm-user/%u/%s", hba
->host_id
, udev
->name
);
826 if (udev
->dev_config
[0])
827 snprintf(str
+ used
, size
- used
, "/%s", udev
->dev_config
);
831 udev
->mb_addr
= vzalloc(TCMU_RING_SIZE
);
832 if (!udev
->mb_addr
) {
837 /* mailbox fits in first part of CMDR space */
838 udev
->cmdr_size
= CMDR_SIZE
- CMDR_OFF
;
839 udev
->data_off
= CMDR_SIZE
;
840 udev
->data_size
= TCMU_RING_SIZE
- CMDR_SIZE
;
844 mb
->cmdr_off
= CMDR_OFF
;
845 mb
->cmdr_size
= udev
->cmdr_size
;
847 WARN_ON(!PAGE_ALIGNED(udev
->data_off
));
848 WARN_ON(udev
->data_size
% PAGE_SIZE
);
852 info
->mem
[0].name
= "tcm-user command & data buffer";
853 info
->mem
[0].addr
= (phys_addr_t
) udev
->mb_addr
;
854 info
->mem
[0].size
= TCMU_RING_SIZE
;
855 info
->mem
[0].memtype
= UIO_MEM_VIRTUAL
;
857 info
->irqcontrol
= tcmu_irqcontrol
;
858 info
->irq
= UIO_IRQ_CUSTOM
;
860 info
->mmap
= tcmu_mmap
;
861 info
->open
= tcmu_open
;
862 info
->release
= tcmu_release
;
864 ret
= uio_register_device(tcmu_root_device
, info
);
868 /* Other attributes can be configured in userspace */
869 dev
->dev_attrib
.hw_block_size
= 512;
870 dev
->dev_attrib
.hw_max_sectors
= 128;
871 dev
->dev_attrib
.hw_queue_depth
= 128;
873 ret
= tcmu_netlink_event(TCMU_CMD_ADDED_DEVICE
, udev
->uio_info
.name
,
874 udev
->uio_info
.uio_dev
->minor
);
881 uio_unregister_device(&udev
->uio_info
);
883 vfree(udev
->mb_addr
);
890 static int tcmu_check_pending_cmd(int id
, void *p
, void *data
)
892 struct tcmu_cmd
*cmd
= p
;
894 if (test_bit(TCMU_CMD_BIT_EXPIRED
, &cmd
->flags
))
899 static void tcmu_free_device(struct se_device
*dev
)
901 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
904 del_timer_sync(&udev
->timeout
);
906 vfree(udev
->mb_addr
);
908 /* Upper layer should drain all requests before calling this */
909 spin_lock_irq(&udev
->commands_lock
);
910 i
= idr_for_each(&udev
->commands
, tcmu_check_pending_cmd
, NULL
);
911 idr_destroy(&udev
->commands
);
912 spin_unlock_irq(&udev
->commands_lock
);
915 /* Device was configured */
916 if (udev
->uio_info
.uio_dev
) {
917 tcmu_netlink_event(TCMU_CMD_REMOVED_DEVICE
, udev
->uio_info
.name
,
918 udev
->uio_info
.uio_dev
->minor
);
920 uio_unregister_device(&udev
->uio_info
);
921 kfree(udev
->uio_info
.name
);
929 Opt_dev_config
, Opt_dev_size
, Opt_err
, Opt_pass_level
,
932 static match_table_t tokens
= {
933 {Opt_dev_config
, "dev_config=%s"},
934 {Opt_dev_size
, "dev_size=%u"},
935 {Opt_pass_level
, "pass_level=%u"},
939 static ssize_t
tcmu_set_configfs_dev_params(struct se_device
*dev
,
940 const char *page
, ssize_t count
)
942 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
943 char *orig
, *ptr
, *opts
, *arg_p
;
944 substring_t args
[MAX_OPT_ARGS
];
948 opts
= kstrdup(page
, GFP_KERNEL
);
954 while ((ptr
= strsep(&opts
, ",\n")) != NULL
) {
958 token
= match_token(ptr
, tokens
, args
);
961 if (match_strlcpy(udev
->dev_config
, &args
[0],
962 TCMU_CONFIG_LEN
) == 0) {
966 pr_debug("TCMU: Referencing Path: %s\n", udev
->dev_config
);
969 arg_p
= match_strdup(&args
[0]);
974 ret
= kstrtoul(arg_p
, 0, (unsigned long *) &udev
->dev_size
);
977 pr_err("kstrtoul() failed for dev_size=\n");
980 match_int(args
, &arg
);
981 if (arg
>= TCMU_PASS_INVALID
) {
982 pr_warn("TCMU: Invalid pass_level: %d\n", arg
);
986 pr_debug("TCMU: Setting pass_level to %d\n", arg
);
987 udev
->pass_level
= arg
;
995 return (!ret
) ? count
: ret
;
998 static ssize_t
tcmu_show_configfs_dev_params(struct se_device
*dev
, char *b
)
1000 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
1003 bl
= sprintf(b
+ bl
, "Config: %s ",
1004 udev
->dev_config
[0] ? udev
->dev_config
: "NULL");
1005 bl
+= sprintf(b
+ bl
, "Size: %zu PassLevel: %u\n",
1006 udev
->dev_size
, udev
->pass_level
);
1011 static sector_t
tcmu_get_blocks(struct se_device
*dev
)
1013 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
1015 return div_u64(udev
->dev_size
- dev
->dev_attrib
.block_size
,
1016 dev
->dev_attrib
.block_size
);
1019 static sense_reason_t
1020 tcmu_execute_rw(struct se_cmd
*se_cmd
, struct scatterlist
*sgl
, u32 sgl_nents
,
1021 enum dma_data_direction data_direction
)
1025 ret
= tcmu_queue_cmd(se_cmd
);
1028 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1030 return TCM_NO_SENSE
;
1033 static sense_reason_t
1034 tcmu_pass_op(struct se_cmd
*se_cmd
)
1036 int ret
= tcmu_queue_cmd(se_cmd
);
1039 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1041 return TCM_NO_SENSE
;
1044 static struct sbc_ops tcmu_sbc_ops
= {
1045 .execute_rw
= tcmu_execute_rw
,
1046 .execute_sync_cache
= tcmu_pass_op
,
1047 .execute_write_same
= tcmu_pass_op
,
1048 .execute_write_same_unmap
= tcmu_pass_op
,
1049 .execute_unmap
= tcmu_pass_op
,
1052 static sense_reason_t
1053 tcmu_parse_cdb(struct se_cmd
*cmd
)
1055 unsigned char *cdb
= cmd
->t_task_cdb
;
1056 struct tcmu_dev
*udev
= TCMU_DEV(cmd
->se_dev
);
1059 switch (udev
->pass_level
) {
1061 /* We're just like pscsi, then */
1063 * For REPORT LUNS we always need to emulate the response, for everything
1068 cmd
->execute_cmd
= spc_emulate_report_luns
;
1079 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_CDB
;
1082 cmd
->execute_cmd
= tcmu_pass_op
;
1087 ret
= sbc_parse_cdb(cmd
, &tcmu_sbc_ops
);
1090 pr_err("Unknown tcm-user pass level %d\n", udev
->pass_level
);
1091 ret
= TCM_CHECK_CONDITION_ABORT_CMD
;
1097 DEF_TB_DEFAULT_ATTRIBS(tcmu
);
1099 static struct configfs_attribute
*tcmu_backend_dev_attrs
[] = {
1100 &tcmu_dev_attrib_emulate_model_alias
.attr
,
1101 &tcmu_dev_attrib_emulate_dpo
.attr
,
1102 &tcmu_dev_attrib_emulate_fua_write
.attr
,
1103 &tcmu_dev_attrib_emulate_fua_read
.attr
,
1104 &tcmu_dev_attrib_emulate_write_cache
.attr
,
1105 &tcmu_dev_attrib_emulate_ua_intlck_ctrl
.attr
,
1106 &tcmu_dev_attrib_emulate_tas
.attr
,
1107 &tcmu_dev_attrib_emulate_tpu
.attr
,
1108 &tcmu_dev_attrib_emulate_tpws
.attr
,
1109 &tcmu_dev_attrib_emulate_caw
.attr
,
1110 &tcmu_dev_attrib_emulate_3pc
.attr
,
1111 &tcmu_dev_attrib_pi_prot_type
.attr
,
1112 &tcmu_dev_attrib_hw_pi_prot_type
.attr
,
1113 &tcmu_dev_attrib_pi_prot_format
.attr
,
1114 &tcmu_dev_attrib_enforce_pr_isids
.attr
,
1115 &tcmu_dev_attrib_is_nonrot
.attr
,
1116 &tcmu_dev_attrib_emulate_rest_reord
.attr
,
1117 &tcmu_dev_attrib_force_pr_aptpl
.attr
,
1118 &tcmu_dev_attrib_hw_block_size
.attr
,
1119 &tcmu_dev_attrib_block_size
.attr
,
1120 &tcmu_dev_attrib_hw_max_sectors
.attr
,
1121 &tcmu_dev_attrib_fabric_max_sectors
.attr
,
1122 &tcmu_dev_attrib_optimal_sectors
.attr
,
1123 &tcmu_dev_attrib_hw_queue_depth
.attr
,
1124 &tcmu_dev_attrib_queue_depth
.attr
,
1125 &tcmu_dev_attrib_max_unmap_lba_count
.attr
,
1126 &tcmu_dev_attrib_max_unmap_block_desc_count
.attr
,
1127 &tcmu_dev_attrib_unmap_granularity
.attr
,
1128 &tcmu_dev_attrib_unmap_granularity_alignment
.attr
,
1129 &tcmu_dev_attrib_max_write_same_len
.attr
,
1133 static struct se_subsystem_api tcmu_template
= {
1135 .inquiry_prod
= "USER",
1136 .inquiry_rev
= TCMU_VERSION
,
1137 .owner
= THIS_MODULE
,
1138 .transport_type
= TRANSPORT_PLUGIN_VHBA_PDEV
,
1139 .attach_hba
= tcmu_attach_hba
,
1140 .detach_hba
= tcmu_detach_hba
,
1141 .alloc_device
= tcmu_alloc_device
,
1142 .configure_device
= tcmu_configure_device
,
1143 .free_device
= tcmu_free_device
,
1144 .parse_cdb
= tcmu_parse_cdb
,
1145 .set_configfs_dev_params
= tcmu_set_configfs_dev_params
,
1146 .show_configfs_dev_params
= tcmu_show_configfs_dev_params
,
1147 .get_device_type
= sbc_get_device_type
,
1148 .get_blocks
= tcmu_get_blocks
,
1151 static int __init
tcmu_module_init(void)
1153 struct target_backend_cits
*tbc
= &tcmu_template
.tb_cits
;
1156 BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry
) % TCMU_OP_ALIGN_SIZE
) != 0);
1158 tcmu_cmd_cache
= kmem_cache_create("tcmu_cmd_cache",
1159 sizeof(struct tcmu_cmd
),
1160 __alignof__(struct tcmu_cmd
),
1162 if (!tcmu_cmd_cache
)
1165 tcmu_root_device
= root_device_register("tcm_user");
1166 if (IS_ERR(tcmu_root_device
)) {
1167 ret
= PTR_ERR(tcmu_root_device
);
1168 goto out_free_cache
;
1171 ret
= genl_register_family(&tcmu_genl_family
);
1173 goto out_unreg_device
;
1176 target_core_setup_sub_cits(&tcmu_template
);
1177 tbc
->tb_dev_attrib_cit
.ct_attrs
= tcmu_backend_dev_attrs
;
1179 ret
= transport_subsystem_register(&tcmu_template
);
1181 goto out_unreg_genl
;
1186 genl_unregister_family(&tcmu_genl_family
);
1188 root_device_unregister(tcmu_root_device
);
1190 kmem_cache_destroy(tcmu_cmd_cache
);
1195 static void __exit
tcmu_module_exit(void)
1197 transport_subsystem_release(&tcmu_template
);
1198 genl_unregister_family(&tcmu_genl_family
);
1199 root_device_unregister(tcmu_root_device
);
1200 kmem_cache_destroy(tcmu_cmd_cache
);
1203 MODULE_DESCRIPTION("TCM USER subsystem plugin");
1204 MODULE_AUTHOR("Shaohua Li <shli@kernel.org>");
1205 MODULE_AUTHOR("Andy Grover <agrover@redhat.com>");
1206 MODULE_LICENSE("GPL");
1208 module_init(tcmu_module_init
);
1209 module_exit(tcmu_module_exit
);