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/timer.h>
24 #include <linux/parser.h>
25 #include <scsi/scsi.h>
26 #include <scsi/scsi_host.h>
27 #include <linux/uio_driver.h>
28 #include <net/genetlink.h>
29 #include <target/target_core_base.h>
30 #include <target/target_core_fabric.h>
31 #include <target/target_core_backend.h>
32 #include <target/target_core_backend_configfs.h>
34 #include <linux/target_core_user.h>
37 * Define a shared-memory interface for LIO to pass SCSI commands and
38 * data to userspace for processing. This is to allow backends that
39 * are too complex for in-kernel support to be possible.
41 * It uses the UIO framework to do a lot of the device-creation and
42 * introspection work for us.
44 * See the .h file for how the ring is laid out. Note that while the
45 * command ring is defined, the particulars of the data area are
46 * not. Offset values in the command entry point to other locations
47 * internal to the mmap()ed area. There is separate space outside the
48 * command ring for data buffers. This leaves maximum flexibility for
49 * moving buffer allocations, or even page flipping or other
50 * allocation techniques, without altering the command ring layout.
53 * The user process must be assumed to be malicious. There's no way to
54 * prevent it breaking the command ring protocol if it wants, but in
55 * order to prevent other issues we must only ever read *data* from
56 * the shared memory area, not offsets or sizes. This applies to
57 * command ring entries as well as the mailbox. Extra code needed for
58 * this may have a 'UAM' comment.
62 #define TCMU_TIME_OUT (30 * MSEC_PER_SEC)
64 #define CMDR_SIZE (16 * 4096)
65 #define DATA_SIZE (257 * 4096)
67 #define TCMU_RING_SIZE (CMDR_SIZE + DATA_SIZE)
69 static struct device
*tcmu_root_device
;
75 #define TCMU_CONFIG_LEN 256
78 struct se_device se_dev
;
83 #define TCMU_DEV_BIT_OPEN 0
84 #define TCMU_DEV_BIT_BROKEN 1
87 struct uio_info uio_info
;
89 struct tcmu_mailbox
*mb_addr
;
92 u32 cmdr_last_cleaned
;
93 /* Offset of data ring from start of mb */
96 /* Ring head + tail values. */
97 /* Must add data_off and mb_addr to get the address */
101 wait_queue_head_t wait_cmdr
;
102 /* TODO should this be a mutex? */
103 spinlock_t cmdr_lock
;
106 spinlock_t commands_lock
;
108 struct timer_list timeout
;
110 char dev_config
[TCMU_CONFIG_LEN
];
113 #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev)
115 #define CMDR_OFF sizeof(struct tcmu_mailbox)
118 struct se_cmd
*se_cmd
;
119 struct tcmu_dev
*tcmu_dev
;
123 /* Can't use se_cmd->data_length when cleaning up expired cmds, because if
124 cmd has been completed then accessing se_cmd is off limits */
127 unsigned long deadline
;
129 #define TCMU_CMD_BIT_EXPIRED 0
133 static struct kmem_cache
*tcmu_cmd_cache
;
135 /* multicast group */
136 enum tcmu_multicast_groups
{
140 static const struct genl_multicast_group tcmu_mcgrps
[] = {
141 [TCMU_MCGRP_CONFIG
] = { .name
= "config", },
144 /* Our generic netlink family */
145 static struct genl_family tcmu_genl_family
= {
146 .id
= GENL_ID_GENERATE
,
150 .maxattr
= TCMU_ATTR_MAX
,
151 .mcgrps
= tcmu_mcgrps
,
152 .n_mcgrps
= ARRAY_SIZE(tcmu_mcgrps
),
155 static struct tcmu_cmd
*tcmu_alloc_cmd(struct se_cmd
*se_cmd
)
157 struct se_device
*se_dev
= se_cmd
->se_dev
;
158 struct tcmu_dev
*udev
= TCMU_DEV(se_dev
);
159 struct tcmu_cmd
*tcmu_cmd
;
162 tcmu_cmd
= kmem_cache_zalloc(tcmu_cmd_cache
, GFP_KERNEL
);
166 tcmu_cmd
->se_cmd
= se_cmd
;
167 tcmu_cmd
->tcmu_dev
= udev
;
168 tcmu_cmd
->data_length
= se_cmd
->data_length
;
170 if (se_cmd
->se_cmd_flags
& SCF_BIDI
) {
171 BUG_ON(!(se_cmd
->t_bidi_data_sg
&& se_cmd
->t_bidi_data_nents
));
172 tcmu_cmd
->data_length
+= se_cmd
->t_bidi_data_sg
->length
;
175 tcmu_cmd
->deadline
= jiffies
+ msecs_to_jiffies(TCMU_TIME_OUT
);
177 idr_preload(GFP_KERNEL
);
178 spin_lock_irq(&udev
->commands_lock
);
179 cmd_id
= idr_alloc(&udev
->commands
, tcmu_cmd
, 0,
180 USHRT_MAX
, GFP_NOWAIT
);
181 spin_unlock_irq(&udev
->commands_lock
);
185 kmem_cache_free(tcmu_cmd_cache
, tcmu_cmd
);
188 tcmu_cmd
->cmd_id
= cmd_id
;
193 static inline void tcmu_flush_dcache_range(void *vaddr
, size_t size
)
195 unsigned long offset
= (unsigned long) vaddr
& ~PAGE_MASK
;
197 size
= round_up(size
+offset
, PAGE_SIZE
);
201 flush_dcache_page(virt_to_page(vaddr
));
207 * Some ring helper functions. We don't assume size is a power of 2 so
208 * we can't use circ_buf.h.
210 static inline size_t spc_used(size_t head
, size_t tail
, size_t size
)
212 int diff
= head
- tail
;
220 static inline size_t spc_free(size_t head
, size_t tail
, size_t size
)
222 /* Keep 1 byte unused or we can't tell full from empty */
223 return (size
- spc_used(head
, tail
, size
) - 1);
226 static inline size_t head_to_end(size_t head
, size_t size
)
231 #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size)
233 static void alloc_and_scatter_data_area(struct tcmu_dev
*udev
,
234 struct scatterlist
*data_sg
, unsigned int data_nents
,
235 struct iovec
**iov
, int *iov_cnt
, bool copy_data
)
240 struct scatterlist
*sg
;
242 for_each_sg(data_sg
, sg
, data_nents
, i
) {
243 copy_bytes
= min_t(size_t, sg
->length
,
244 head_to_end(udev
->data_head
, udev
->data_size
));
245 from
= kmap_atomic(sg_page(sg
)) + sg
->offset
;
246 to
= (void *) udev
->mb_addr
+ udev
->data_off
+ udev
->data_head
;
249 memcpy(to
, from
, copy_bytes
);
250 tcmu_flush_dcache_range(to
, copy_bytes
);
253 /* Even iov_base is relative to mb_addr */
254 (*iov
)->iov_len
= copy_bytes
;
255 (*iov
)->iov_base
= (void __user
*) udev
->data_off
+
260 UPDATE_HEAD(udev
->data_head
, copy_bytes
, udev
->data_size
);
262 /* Uh oh, we wrapped the buffer. Must split sg across 2 iovs. */
263 if (sg
->length
!= copy_bytes
) {
265 copy_bytes
= sg
->length
- copy_bytes
;
267 (*iov
)->iov_len
= copy_bytes
;
268 (*iov
)->iov_base
= (void __user
*) udev
->data_off
+
272 to
= (void *) udev
->mb_addr
+
273 udev
->data_off
+ udev
->data_head
;
274 memcpy(to
, from
, copy_bytes
);
275 tcmu_flush_dcache_range(to
, copy_bytes
);
281 UPDATE_HEAD(udev
->data_head
,
282 copy_bytes
, udev
->data_size
);
289 static void gather_and_free_data_area(struct tcmu_dev
*udev
,
290 struct scatterlist
*data_sg
, unsigned int data_nents
)
295 struct scatterlist
*sg
;
297 /* It'd be easier to look at entry's iovec again, but UAM */
298 for_each_sg(data_sg
, sg
, data_nents
, i
) {
299 copy_bytes
= min_t(size_t, sg
->length
,
300 head_to_end(udev
->data_tail
, udev
->data_size
));
302 to
= kmap_atomic(sg_page(sg
)) + sg
->offset
;
303 WARN_ON(sg
->length
+ sg
->offset
> PAGE_SIZE
);
304 from
= (void *) udev
->mb_addr
+
305 udev
->data_off
+ udev
->data_tail
;
306 tcmu_flush_dcache_range(from
, copy_bytes
);
307 memcpy(to
, from
, copy_bytes
);
309 UPDATE_HEAD(udev
->data_tail
, copy_bytes
, udev
->data_size
);
311 /* Uh oh, wrapped the data buffer for this sg's data */
312 if (sg
->length
!= copy_bytes
) {
313 from
= (void *) udev
->mb_addr
+
314 udev
->data_off
+ udev
->data_tail
;
315 WARN_ON(udev
->data_tail
);
317 copy_bytes
= sg
->length
- copy_bytes
;
318 tcmu_flush_dcache_range(from
, copy_bytes
);
319 memcpy(to
, from
, copy_bytes
);
321 UPDATE_HEAD(udev
->data_tail
,
322 copy_bytes
, udev
->data_size
);
329 * We can't queue a command until we have space available on the cmd ring *and*
330 * space available on the data ring.
332 * Called with ring lock held.
334 static bool is_ring_space_avail(struct tcmu_dev
*udev
, size_t cmd_size
, size_t data_needed
)
336 struct tcmu_mailbox
*mb
= udev
->mb_addr
;
341 tcmu_flush_dcache_range(mb
, sizeof(*mb
));
343 cmd_head
= mb
->cmd_head
% udev
->cmdr_size
; /* UAM */
346 * If cmd end-of-ring space is too small then we need space for a NOP plus
347 * original cmd - cmds are internally contiguous.
349 if (head_to_end(cmd_head
, udev
->cmdr_size
) >= cmd_size
)
350 cmd_needed
= cmd_size
;
352 cmd_needed
= cmd_size
+ head_to_end(cmd_head
, udev
->cmdr_size
);
354 space
= spc_free(cmd_head
, udev
->cmdr_last_cleaned
, udev
->cmdr_size
);
355 if (space
< cmd_needed
) {
356 pr_debug("no cmd space: %u %u %u\n", cmd_head
,
357 udev
->cmdr_last_cleaned
, udev
->cmdr_size
);
361 space
= spc_free(udev
->data_head
, udev
->data_tail
, udev
->data_size
);
362 if (space
< data_needed
) {
363 pr_debug("no data space: %zu %zu %zu\n", udev
->data_head
,
364 udev
->data_tail
, udev
->data_size
);
371 static int tcmu_queue_cmd_ring(struct tcmu_cmd
*tcmu_cmd
)
373 struct tcmu_dev
*udev
= tcmu_cmd
->tcmu_dev
;
374 struct se_cmd
*se_cmd
= tcmu_cmd
->se_cmd
;
375 size_t base_command_size
, command_size
;
376 struct tcmu_mailbox
*mb
;
377 struct tcmu_cmd_entry
*entry
;
382 bool copy_to_data_area
;
384 if (test_bit(TCMU_DEV_BIT_BROKEN
, &udev
->flags
))
388 * Must be a certain minimum size for response sense info, but
389 * also may be larger if the iov array is large.
391 * iovs = sgl_nents+1, for end-of-ring case, plus another 1
392 * b/c size == offsetof one-past-element.
394 base_command_size
= max(offsetof(struct tcmu_cmd_entry
,
395 req
.iov
[se_cmd
->t_bidi_data_nents
+
396 se_cmd
->t_data_nents
+ 2]),
397 sizeof(struct tcmu_cmd_entry
));
398 command_size
= base_command_size
399 + round_up(scsi_command_size(se_cmd
->t_task_cdb
), TCMU_OP_ALIGN_SIZE
);
401 WARN_ON(command_size
& (TCMU_OP_ALIGN_SIZE
-1));
403 spin_lock_irq(&udev
->cmdr_lock
);
406 cmd_head
= mb
->cmd_head
% udev
->cmdr_size
; /* UAM */
407 if ((command_size
> (udev
->cmdr_size
/ 2))
408 || tcmu_cmd
->data_length
> (udev
->data_size
- 1))
409 pr_warn("TCMU: Request of size %zu/%zu may be too big for %u/%zu "
410 "cmd/data ring buffers\n", command_size
, tcmu_cmd
->data_length
,
411 udev
->cmdr_size
, udev
->data_size
);
413 while (!is_ring_space_avail(udev
, command_size
, tcmu_cmd
->data_length
)) {
417 prepare_to_wait(&udev
->wait_cmdr
, &__wait
, TASK_INTERRUPTIBLE
);
419 pr_debug("sleeping for ring space\n");
420 spin_unlock_irq(&udev
->cmdr_lock
);
421 ret
= schedule_timeout(msecs_to_jiffies(TCMU_TIME_OUT
));
422 finish_wait(&udev
->wait_cmdr
, &__wait
);
424 pr_warn("tcmu: command timed out\n");
428 spin_lock_irq(&udev
->cmdr_lock
);
430 /* We dropped cmdr_lock, cmd_head is stale */
431 cmd_head
= mb
->cmd_head
% udev
->cmdr_size
; /* UAM */
434 /* Insert a PAD if end-of-ring space is too small */
435 if (head_to_end(cmd_head
, udev
->cmdr_size
) < command_size
) {
436 size_t pad_size
= head_to_end(cmd_head
, udev
->cmdr_size
);
438 entry
= (void *) mb
+ CMDR_OFF
+ cmd_head
;
439 tcmu_flush_dcache_range(entry
, sizeof(*entry
));
440 tcmu_hdr_set_op(&entry
->hdr
.len_op
, TCMU_OP_PAD
);
441 tcmu_hdr_set_len(&entry
->hdr
.len_op
, pad_size
);
442 entry
->hdr
.cmd_id
= 0; /* not used for PAD */
443 entry
->hdr
.kflags
= 0;
444 entry
->hdr
.uflags
= 0;
446 UPDATE_HEAD(mb
->cmd_head
, pad_size
, udev
->cmdr_size
);
448 cmd_head
= mb
->cmd_head
% udev
->cmdr_size
; /* UAM */
449 WARN_ON(cmd_head
!= 0);
452 entry
= (void *) mb
+ CMDR_OFF
+ cmd_head
;
453 tcmu_flush_dcache_range(entry
, sizeof(*entry
));
454 tcmu_hdr_set_op(&entry
->hdr
.len_op
, TCMU_OP_CMD
);
455 tcmu_hdr_set_len(&entry
->hdr
.len_op
, command_size
);
456 entry
->hdr
.cmd_id
= tcmu_cmd
->cmd_id
;
457 entry
->hdr
.kflags
= 0;
458 entry
->hdr
.uflags
= 0;
461 * Fix up iovecs, and handle if allocation in data ring wrapped.
463 iov
= &entry
->req
.iov
[0];
465 copy_to_data_area
= (se_cmd
->data_direction
== DMA_TO_DEVICE
466 || se_cmd
->se_cmd_flags
& SCF_BIDI
);
467 alloc_and_scatter_data_area(udev
, se_cmd
->t_data_sg
,
468 se_cmd
->t_data_nents
, &iov
, &iov_cnt
, copy_to_data_area
);
469 entry
->req
.iov_cnt
= iov_cnt
;
470 entry
->req
.iov_dif_cnt
= 0;
472 /* Handle BIDI commands */
474 alloc_and_scatter_data_area(udev
, se_cmd
->t_bidi_data_sg
,
475 se_cmd
->t_bidi_data_nents
, &iov
, &iov_cnt
, false);
476 entry
->req
.iov_bidi_cnt
= iov_cnt
;
478 /* All offsets relative to mb_addr, not start of entry! */
479 cdb_off
= CMDR_OFF
+ cmd_head
+ base_command_size
;
480 memcpy((void *) mb
+ cdb_off
, se_cmd
->t_task_cdb
, scsi_command_size(se_cmd
->t_task_cdb
));
481 entry
->req
.cdb_off
= cdb_off
;
482 tcmu_flush_dcache_range(entry
, sizeof(*entry
));
484 UPDATE_HEAD(mb
->cmd_head
, command_size
, udev
->cmdr_size
);
485 tcmu_flush_dcache_range(mb
, sizeof(*mb
));
487 spin_unlock_irq(&udev
->cmdr_lock
);
489 /* TODO: only if FLUSH and FUA? */
490 uio_event_notify(&udev
->uio_info
);
492 mod_timer(&udev
->timeout
,
493 round_jiffies_up(jiffies
+ msecs_to_jiffies(TCMU_TIME_OUT
)));
498 static int tcmu_queue_cmd(struct se_cmd
*se_cmd
)
500 struct se_device
*se_dev
= se_cmd
->se_dev
;
501 struct tcmu_dev
*udev
= TCMU_DEV(se_dev
);
502 struct tcmu_cmd
*tcmu_cmd
;
505 tcmu_cmd
= tcmu_alloc_cmd(se_cmd
);
509 ret
= tcmu_queue_cmd_ring(tcmu_cmd
);
511 pr_err("TCMU: Could not queue command\n");
512 spin_lock_irq(&udev
->commands_lock
);
513 idr_remove(&udev
->commands
, tcmu_cmd
->cmd_id
);
514 spin_unlock_irq(&udev
->commands_lock
);
516 kmem_cache_free(tcmu_cmd_cache
, tcmu_cmd
);
522 static void tcmu_handle_completion(struct tcmu_cmd
*cmd
, struct tcmu_cmd_entry
*entry
)
524 struct se_cmd
*se_cmd
= cmd
->se_cmd
;
525 struct tcmu_dev
*udev
= cmd
->tcmu_dev
;
527 if (test_bit(TCMU_CMD_BIT_EXPIRED
, &cmd
->flags
)) {
528 /* cmd has been completed already from timeout, just reclaim data
530 UPDATE_HEAD(udev
->data_tail
, cmd
->data_length
, udev
->data_size
);
534 if (entry
->hdr
.uflags
& TCMU_UFLAG_UNKNOWN_OP
) {
535 UPDATE_HEAD(udev
->data_tail
, cmd
->data_length
, udev
->data_size
);
536 pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n",
538 transport_generic_request_failure(cmd
->se_cmd
,
539 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
);
541 kmem_cache_free(tcmu_cmd_cache
, cmd
);
545 if (entry
->rsp
.scsi_status
== SAM_STAT_CHECK_CONDITION
) {
546 memcpy(se_cmd
->sense_buffer
, entry
->rsp
.sense_buffer
,
547 se_cmd
->scsi_sense_length
);
549 UPDATE_HEAD(udev
->data_tail
, cmd
->data_length
, udev
->data_size
);
550 } else if (se_cmd
->se_cmd_flags
& SCF_BIDI
) {
551 /* Discard data_out buffer */
552 UPDATE_HEAD(udev
->data_tail
,
553 (size_t)se_cmd
->t_data_sg
->length
, udev
->data_size
);
555 /* Get Data-In buffer */
556 gather_and_free_data_area(udev
,
557 se_cmd
->t_bidi_data_sg
, se_cmd
->t_bidi_data_nents
);
558 } else if (se_cmd
->data_direction
== DMA_FROM_DEVICE
) {
559 gather_and_free_data_area(udev
,
560 se_cmd
->t_data_sg
, se_cmd
->t_data_nents
);
561 } else if (se_cmd
->data_direction
== DMA_TO_DEVICE
) {
562 UPDATE_HEAD(udev
->data_tail
, cmd
->data_length
, udev
->data_size
);
563 } else if (se_cmd
->data_direction
!= DMA_NONE
) {
564 pr_warn("TCMU: data direction was %d!\n",
565 se_cmd
->data_direction
);
568 target_complete_cmd(cmd
->se_cmd
, entry
->rsp
.scsi_status
);
571 kmem_cache_free(tcmu_cmd_cache
, cmd
);
574 static unsigned int tcmu_handle_completions(struct tcmu_dev
*udev
)
576 struct tcmu_mailbox
*mb
;
581 if (test_bit(TCMU_DEV_BIT_BROKEN
, &udev
->flags
)) {
582 pr_err("ring broken, not handling completions\n");
586 spin_lock_irqsave(&udev
->cmdr_lock
, flags
);
589 tcmu_flush_dcache_range(mb
, sizeof(*mb
));
591 while (udev
->cmdr_last_cleaned
!= ACCESS_ONCE(mb
->cmd_tail
)) {
593 struct tcmu_cmd_entry
*entry
= (void *) mb
+ CMDR_OFF
+ udev
->cmdr_last_cleaned
;
594 struct tcmu_cmd
*cmd
;
596 tcmu_flush_dcache_range(entry
, sizeof(*entry
));
598 if (tcmu_hdr_get_op(entry
->hdr
.len_op
) == TCMU_OP_PAD
) {
599 UPDATE_HEAD(udev
->cmdr_last_cleaned
,
600 tcmu_hdr_get_len(entry
->hdr
.len_op
),
604 WARN_ON(tcmu_hdr_get_op(entry
->hdr
.len_op
) != TCMU_OP_CMD
);
606 spin_lock(&udev
->commands_lock
);
607 cmd
= idr_find(&udev
->commands
, entry
->hdr
.cmd_id
);
609 idr_remove(&udev
->commands
, cmd
->cmd_id
);
610 spin_unlock(&udev
->commands_lock
);
613 pr_err("cmd_id not found, ring is broken\n");
614 set_bit(TCMU_DEV_BIT_BROKEN
, &udev
->flags
);
618 tcmu_handle_completion(cmd
, entry
);
620 UPDATE_HEAD(udev
->cmdr_last_cleaned
,
621 tcmu_hdr_get_len(entry
->hdr
.len_op
),
627 if (mb
->cmd_tail
== mb
->cmd_head
)
628 del_timer(&udev
->timeout
); /* no more pending cmds */
630 spin_unlock_irqrestore(&udev
->cmdr_lock
, flags
);
632 wake_up(&udev
->wait_cmdr
);
637 static int tcmu_check_expired_cmd(int id
, void *p
, void *data
)
639 struct tcmu_cmd
*cmd
= p
;
641 if (test_bit(TCMU_CMD_BIT_EXPIRED
, &cmd
->flags
))
644 if (!time_after(cmd
->deadline
, jiffies
))
647 set_bit(TCMU_CMD_BIT_EXPIRED
, &cmd
->flags
);
648 target_complete_cmd(cmd
->se_cmd
, SAM_STAT_CHECK_CONDITION
);
651 kmem_cache_free(tcmu_cmd_cache
, cmd
);
656 static void tcmu_device_timedout(unsigned long data
)
658 struct tcmu_dev
*udev
= (struct tcmu_dev
*)data
;
662 handled
= tcmu_handle_completions(udev
);
664 pr_warn("%d completions handled from timeout\n", handled
);
666 spin_lock_irqsave(&udev
->commands_lock
, flags
);
667 idr_for_each(&udev
->commands
, tcmu_check_expired_cmd
, NULL
);
668 spin_unlock_irqrestore(&udev
->commands_lock
, flags
);
671 * We don't need to wakeup threads on wait_cmdr since they have their
676 static int tcmu_attach_hba(struct se_hba
*hba
, u32 host_id
)
678 struct tcmu_hba
*tcmu_hba
;
680 tcmu_hba
= kzalloc(sizeof(struct tcmu_hba
), GFP_KERNEL
);
684 tcmu_hba
->host_id
= host_id
;
685 hba
->hba_ptr
= tcmu_hba
;
690 static void tcmu_detach_hba(struct se_hba
*hba
)
696 static struct se_device
*tcmu_alloc_device(struct se_hba
*hba
, const char *name
)
698 struct tcmu_dev
*udev
;
700 udev
= kzalloc(sizeof(struct tcmu_dev
), GFP_KERNEL
);
704 udev
->name
= kstrdup(name
, GFP_KERNEL
);
712 init_waitqueue_head(&udev
->wait_cmdr
);
713 spin_lock_init(&udev
->cmdr_lock
);
715 idr_init(&udev
->commands
);
716 spin_lock_init(&udev
->commands_lock
);
718 setup_timer(&udev
->timeout
, tcmu_device_timedout
,
719 (unsigned long)udev
);
721 return &udev
->se_dev
;
724 static int tcmu_irqcontrol(struct uio_info
*info
, s32 irq_on
)
726 struct tcmu_dev
*tcmu_dev
= container_of(info
, struct tcmu_dev
, uio_info
);
728 tcmu_handle_completions(tcmu_dev
);
734 * mmap code from uio.c. Copied here because we want to hook mmap()
735 * and this stuff must come along.
737 static int tcmu_find_mem_index(struct vm_area_struct
*vma
)
739 struct tcmu_dev
*udev
= vma
->vm_private_data
;
740 struct uio_info
*info
= &udev
->uio_info
;
742 if (vma
->vm_pgoff
< MAX_UIO_MAPS
) {
743 if (info
->mem
[vma
->vm_pgoff
].size
== 0)
745 return (int)vma
->vm_pgoff
;
750 static int tcmu_vma_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
752 struct tcmu_dev
*udev
= vma
->vm_private_data
;
753 struct uio_info
*info
= &udev
->uio_info
;
755 unsigned long offset
;
758 int mi
= tcmu_find_mem_index(vma
);
760 return VM_FAULT_SIGBUS
;
763 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
766 offset
= (vmf
->pgoff
- mi
) << PAGE_SHIFT
;
768 addr
= (void *)(unsigned long)info
->mem
[mi
].addr
+ offset
;
769 if (info
->mem
[mi
].memtype
== UIO_MEM_LOGICAL
)
770 page
= virt_to_page(addr
);
772 page
= vmalloc_to_page(addr
);
778 static const struct vm_operations_struct tcmu_vm_ops
= {
779 .fault
= tcmu_vma_fault
,
782 static int tcmu_mmap(struct uio_info
*info
, struct vm_area_struct
*vma
)
784 struct tcmu_dev
*udev
= container_of(info
, struct tcmu_dev
, uio_info
);
786 vma
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
787 vma
->vm_ops
= &tcmu_vm_ops
;
789 vma
->vm_private_data
= udev
;
791 /* Ensure the mmap is exactly the right size */
792 if (vma_pages(vma
) != (TCMU_RING_SIZE
>> PAGE_SHIFT
))
798 static int tcmu_open(struct uio_info
*info
, struct inode
*inode
)
800 struct tcmu_dev
*udev
= container_of(info
, struct tcmu_dev
, uio_info
);
802 /* O_EXCL not supported for char devs, so fake it? */
803 if (test_and_set_bit(TCMU_DEV_BIT_OPEN
, &udev
->flags
))
811 static int tcmu_release(struct uio_info
*info
, struct inode
*inode
)
813 struct tcmu_dev
*udev
= container_of(info
, struct tcmu_dev
, uio_info
);
815 clear_bit(TCMU_DEV_BIT_OPEN
, &udev
->flags
);
822 static int tcmu_netlink_event(enum tcmu_genl_cmd cmd
, const char *name
, int minor
)
828 skb
= genlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
832 msg_header
= genlmsg_put(skb
, 0, 0, &tcmu_genl_family
, 0, cmd
);
836 ret
= nla_put_string(skb
, TCMU_ATTR_DEVICE
, name
);
840 ret
= nla_put_u32(skb
, TCMU_ATTR_MINOR
, minor
);
844 genlmsg_end(skb
, msg_header
);
846 ret
= genlmsg_multicast(&tcmu_genl_family
, skb
, 0,
847 TCMU_MCGRP_CONFIG
, GFP_KERNEL
);
849 /* We don't care if no one is listening */
859 static int tcmu_configure_device(struct se_device
*dev
)
861 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
862 struct tcmu_hba
*hba
= udev
->hba
->hba_ptr
;
863 struct uio_info
*info
;
864 struct tcmu_mailbox
*mb
;
870 info
= &udev
->uio_info
;
872 size
= snprintf(NULL
, 0, "tcm-user/%u/%s/%s", hba
->host_id
, udev
->name
,
874 size
+= 1; /* for \0 */
875 str
= kmalloc(size
, GFP_KERNEL
);
879 used
= snprintf(str
, size
, "tcm-user/%u/%s", hba
->host_id
, udev
->name
);
881 if (udev
->dev_config
[0])
882 snprintf(str
+ used
, size
- used
, "/%s", udev
->dev_config
);
886 udev
->mb_addr
= vzalloc(TCMU_RING_SIZE
);
887 if (!udev
->mb_addr
) {
892 /* mailbox fits in first part of CMDR space */
893 udev
->cmdr_size
= CMDR_SIZE
- CMDR_OFF
;
894 udev
->data_off
= CMDR_SIZE
;
895 udev
->data_size
= TCMU_RING_SIZE
- CMDR_SIZE
;
898 mb
->version
= TCMU_MAILBOX_VERSION
;
899 mb
->cmdr_off
= CMDR_OFF
;
900 mb
->cmdr_size
= udev
->cmdr_size
;
902 WARN_ON(!PAGE_ALIGNED(udev
->data_off
));
903 WARN_ON(udev
->data_size
% PAGE_SIZE
);
905 info
->version
= xstr(TCMU_MAILBOX_VERSION
);
907 info
->mem
[0].name
= "tcm-user command & data buffer";
908 info
->mem
[0].addr
= (phys_addr_t
) udev
->mb_addr
;
909 info
->mem
[0].size
= TCMU_RING_SIZE
;
910 info
->mem
[0].memtype
= UIO_MEM_VIRTUAL
;
912 info
->irqcontrol
= tcmu_irqcontrol
;
913 info
->irq
= UIO_IRQ_CUSTOM
;
915 info
->mmap
= tcmu_mmap
;
916 info
->open
= tcmu_open
;
917 info
->release
= tcmu_release
;
919 ret
= uio_register_device(tcmu_root_device
, info
);
923 /* Other attributes can be configured in userspace */
924 dev
->dev_attrib
.hw_block_size
= 512;
925 dev
->dev_attrib
.hw_max_sectors
= 128;
926 dev
->dev_attrib
.hw_queue_depth
= 128;
928 ret
= tcmu_netlink_event(TCMU_CMD_ADDED_DEVICE
, udev
->uio_info
.name
,
929 udev
->uio_info
.uio_dev
->minor
);
936 uio_unregister_device(&udev
->uio_info
);
938 vfree(udev
->mb_addr
);
945 static int tcmu_check_pending_cmd(int id
, void *p
, void *data
)
947 struct tcmu_cmd
*cmd
= p
;
949 if (test_bit(TCMU_CMD_BIT_EXPIRED
, &cmd
->flags
))
954 static void tcmu_free_device(struct se_device
*dev
)
956 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
959 del_timer_sync(&udev
->timeout
);
961 vfree(udev
->mb_addr
);
963 /* Upper layer should drain all requests before calling this */
964 spin_lock_irq(&udev
->commands_lock
);
965 i
= idr_for_each(&udev
->commands
, tcmu_check_pending_cmd
, NULL
);
966 idr_destroy(&udev
->commands
);
967 spin_unlock_irq(&udev
->commands_lock
);
970 /* Device was configured */
971 if (udev
->uio_info
.uio_dev
) {
972 tcmu_netlink_event(TCMU_CMD_REMOVED_DEVICE
, udev
->uio_info
.name
,
973 udev
->uio_info
.uio_dev
->minor
);
975 uio_unregister_device(&udev
->uio_info
);
976 kfree(udev
->uio_info
.name
);
984 Opt_dev_config
, Opt_dev_size
, Opt_hw_block_size
, Opt_err
,
987 static match_table_t tokens
= {
988 {Opt_dev_config
, "dev_config=%s"},
989 {Opt_dev_size
, "dev_size=%u"},
990 {Opt_hw_block_size
, "hw_block_size=%u"},
994 static ssize_t
tcmu_set_configfs_dev_params(struct se_device
*dev
,
995 const char *page
, ssize_t count
)
997 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
998 char *orig
, *ptr
, *opts
, *arg_p
;
999 substring_t args
[MAX_OPT_ARGS
];
1001 unsigned long tmp_ul
;
1003 opts
= kstrdup(page
, GFP_KERNEL
);
1009 while ((ptr
= strsep(&opts
, ",\n")) != NULL
) {
1013 token
= match_token(ptr
, tokens
, args
);
1015 case Opt_dev_config
:
1016 if (match_strlcpy(udev
->dev_config
, &args
[0],
1017 TCMU_CONFIG_LEN
) == 0) {
1021 pr_debug("TCMU: Referencing Path: %s\n", udev
->dev_config
);
1024 arg_p
= match_strdup(&args
[0]);
1029 ret
= kstrtoul(arg_p
, 0, (unsigned long *) &udev
->dev_size
);
1032 pr_err("kstrtoul() failed for dev_size=\n");
1034 case Opt_hw_block_size
:
1035 arg_p
= match_strdup(&args
[0]);
1040 ret
= kstrtoul(arg_p
, 0, &tmp_ul
);
1043 pr_err("kstrtoul() failed for hw_block_size=\n");
1047 pr_err("hw_block_size must be nonzero\n");
1050 dev
->dev_attrib
.hw_block_size
= tmp_ul
;
1058 return (!ret
) ? count
: ret
;
1061 static ssize_t
tcmu_show_configfs_dev_params(struct se_device
*dev
, char *b
)
1063 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
1066 bl
= sprintf(b
+ bl
, "Config: %s ",
1067 udev
->dev_config
[0] ? udev
->dev_config
: "NULL");
1068 bl
+= sprintf(b
+ bl
, "Size: %zu\n", udev
->dev_size
);
1073 static sector_t
tcmu_get_blocks(struct se_device
*dev
)
1075 struct tcmu_dev
*udev
= TCMU_DEV(dev
);
1077 return div_u64(udev
->dev_size
- dev
->dev_attrib
.block_size
,
1078 dev
->dev_attrib
.block_size
);
1081 static sense_reason_t
1082 tcmu_pass_op(struct se_cmd
*se_cmd
)
1084 int ret
= tcmu_queue_cmd(se_cmd
);
1087 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1089 return TCM_NO_SENSE
;
1092 static sense_reason_t
1093 tcmu_parse_cdb(struct se_cmd
*cmd
)
1095 return passthrough_parse_cdb(cmd
, tcmu_pass_op
);
1098 DEF_TB_DEV_ATTRIB_RO(tcmu
, hw_pi_prot_type
);
1099 TB_DEV_ATTR_RO(tcmu
, hw_pi_prot_type
);
1101 DEF_TB_DEV_ATTRIB_RO(tcmu
, hw_block_size
);
1102 TB_DEV_ATTR_RO(tcmu
, hw_block_size
);
1104 DEF_TB_DEV_ATTRIB_RO(tcmu
, hw_max_sectors
);
1105 TB_DEV_ATTR_RO(tcmu
, hw_max_sectors
);
1107 DEF_TB_DEV_ATTRIB_RO(tcmu
, hw_queue_depth
);
1108 TB_DEV_ATTR_RO(tcmu
, hw_queue_depth
);
1110 static struct configfs_attribute
*tcmu_backend_dev_attrs
[] = {
1111 &tcmu_dev_attrib_hw_pi_prot_type
.attr
,
1112 &tcmu_dev_attrib_hw_block_size
.attr
,
1113 &tcmu_dev_attrib_hw_max_sectors
.attr
,
1114 &tcmu_dev_attrib_hw_queue_depth
.attr
,
1118 static struct se_subsystem_api tcmu_template
= {
1120 .inquiry_prod
= "USER",
1121 .inquiry_rev
= TCMU_VERSION
,
1122 .owner
= THIS_MODULE
,
1123 .transport_flags
= TRANSPORT_FLAG_PASSTHROUGH
,
1124 .attach_hba
= tcmu_attach_hba
,
1125 .detach_hba
= tcmu_detach_hba
,
1126 .alloc_device
= tcmu_alloc_device
,
1127 .configure_device
= tcmu_configure_device
,
1128 .free_device
= tcmu_free_device
,
1129 .parse_cdb
= tcmu_parse_cdb
,
1130 .set_configfs_dev_params
= tcmu_set_configfs_dev_params
,
1131 .show_configfs_dev_params
= tcmu_show_configfs_dev_params
,
1132 .get_device_type
= sbc_get_device_type
,
1133 .get_blocks
= tcmu_get_blocks
,
1136 static int __init
tcmu_module_init(void)
1138 struct target_backend_cits
*tbc
= &tcmu_template
.tb_cits
;
1141 BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry
) % TCMU_OP_ALIGN_SIZE
) != 0);
1143 tcmu_cmd_cache
= kmem_cache_create("tcmu_cmd_cache",
1144 sizeof(struct tcmu_cmd
),
1145 __alignof__(struct tcmu_cmd
),
1147 if (!tcmu_cmd_cache
)
1150 tcmu_root_device
= root_device_register("tcm_user");
1151 if (IS_ERR(tcmu_root_device
)) {
1152 ret
= PTR_ERR(tcmu_root_device
);
1153 goto out_free_cache
;
1156 ret
= genl_register_family(&tcmu_genl_family
);
1158 goto out_unreg_device
;
1161 target_core_setup_sub_cits(&tcmu_template
);
1162 tbc
->tb_dev_attrib_cit
.ct_attrs
= tcmu_backend_dev_attrs
;
1164 ret
= transport_subsystem_register(&tcmu_template
);
1166 goto out_unreg_genl
;
1171 genl_unregister_family(&tcmu_genl_family
);
1173 root_device_unregister(tcmu_root_device
);
1175 kmem_cache_destroy(tcmu_cmd_cache
);
1180 static void __exit
tcmu_module_exit(void)
1182 transport_subsystem_release(&tcmu_template
);
1183 genl_unregister_family(&tcmu_genl_family
);
1184 root_device_unregister(tcmu_root_device
);
1185 kmem_cache_destroy(tcmu_cmd_cache
);
1188 MODULE_DESCRIPTION("TCM USER subsystem plugin");
1189 MODULE_AUTHOR("Shaohua Li <shli@kernel.org>");
1190 MODULE_AUTHOR("Andy Grover <agrover@redhat.com>");
1191 MODULE_LICENSE("GPL");
1193 module_init(tcmu_module_init
);
1194 module_exit(tcmu_module_exit
);