blob: 078ef6e3eb70efa6784f0d13a17e00ac06d11563 [file] [log] [blame]
Andy Grover7c9e7a62014-10-01 16:07:05 -07001/*
2 * Copyright (C) 2013 Shaohua Li <shli@kernel.org>
3 * Copyright (C) 2014 Red Hat, Inc.
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +03004 * Copyright (C) 2015 Arrikto, Inc.
Andy Grover7c9e7a62014-10-01 16:07:05 -07005 *
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.
9 *
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
13 * more details.
14 *
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.
18 */
19
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>
Nicholas Bellingere9f720d2014-11-28 05:11:24 +000032
Andy Grover7c9e7a62014-10-01 16:07:05 -070033#include <linux/target_core_user.h>
34
35/*
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.
39 *
40 * It uses the UIO framework to do a lot of the device-creation and
41 * introspection work for us.
42 *
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.
50 *
51 * SECURITY:
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.
58 */
59
60
61#define TCMU_TIME_OUT (30 * MSEC_PER_SEC)
62
63#define CMDR_SIZE (16 * 4096)
64#define DATA_SIZE (257 * 4096)
65
66#define TCMU_RING_SIZE (CMDR_SIZE + DATA_SIZE)
67
68static struct device *tcmu_root_device;
69
70struct tcmu_hba {
71 u32 host_id;
72};
73
Andy Grover7c9e7a62014-10-01 16:07:05 -070074#define TCMU_CONFIG_LEN 256
75
76struct tcmu_dev {
77 struct se_device se_dev;
78
79 char *name;
80 struct se_hba *hba;
81
82#define TCMU_DEV_BIT_OPEN 0
83#define TCMU_DEV_BIT_BROKEN 1
84 unsigned long flags;
Andy Grover7c9e7a62014-10-01 16:07:05 -070085
86 struct uio_info uio_info;
87
88 struct tcmu_mailbox *mb_addr;
89 size_t dev_size;
90 u32 cmdr_size;
91 u32 cmdr_last_cleaned;
92 /* Offset of data ring from start of mb */
93 size_t data_off;
94 size_t data_size;
95 /* Ring head + tail values. */
96 /* Must add data_off and mb_addr to get the address */
97 size_t data_head;
98 size_t data_tail;
99
100 wait_queue_head_t wait_cmdr;
101 /* TODO should this be a mutex? */
102 spinlock_t cmdr_lock;
103
104 struct idr commands;
105 spinlock_t commands_lock;
106
107 struct timer_list timeout;
108
109 char dev_config[TCMU_CONFIG_LEN];
110};
111
112#define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev)
113
114#define CMDR_OFF sizeof(struct tcmu_mailbox)
115
116struct tcmu_cmd {
117 struct se_cmd *se_cmd;
118 struct tcmu_dev *tcmu_dev;
119
120 uint16_t cmd_id;
121
122 /* Can't use se_cmd->data_length when cleaning up expired cmds, because if
123 cmd has been completed then accessing se_cmd is off limits */
124 size_t data_length;
125
126 unsigned long deadline;
127
128#define TCMU_CMD_BIT_EXPIRED 0
129 unsigned long flags;
130};
131
132static struct kmem_cache *tcmu_cmd_cache;
133
134/* multicast group */
135enum tcmu_multicast_groups {
136 TCMU_MCGRP_CONFIG,
137};
138
139static const struct genl_multicast_group tcmu_mcgrps[] = {
140 [TCMU_MCGRP_CONFIG] = { .name = "config", },
141};
142
143/* Our generic netlink family */
144static struct genl_family tcmu_genl_family = {
145 .id = GENL_ID_GENERATE,
146 .hdrsize = 0,
147 .name = "TCM-USER",
148 .version = 1,
149 .maxattr = TCMU_ATTR_MAX,
150 .mcgrps = tcmu_mcgrps,
151 .n_mcgrps = ARRAY_SIZE(tcmu_mcgrps),
152};
153
154static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd)
155{
156 struct se_device *se_dev = se_cmd->se_dev;
157 struct tcmu_dev *udev = TCMU_DEV(se_dev);
158 struct tcmu_cmd *tcmu_cmd;
159 int cmd_id;
160
161 tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_KERNEL);
162 if (!tcmu_cmd)
163 return NULL;
164
165 tcmu_cmd->se_cmd = se_cmd;
166 tcmu_cmd->tcmu_dev = udev;
167 tcmu_cmd->data_length = se_cmd->data_length;
168
Ilias Tsitsimpise4648b02015-04-23 21:30:09 +0300169 if (se_cmd->se_cmd_flags & SCF_BIDI) {
170 BUG_ON(!(se_cmd->t_bidi_data_sg && se_cmd->t_bidi_data_nents));
171 tcmu_cmd->data_length += se_cmd->t_bidi_data_sg->length;
172 }
173
Andy Grover7c9e7a62014-10-01 16:07:05 -0700174 tcmu_cmd->deadline = jiffies + msecs_to_jiffies(TCMU_TIME_OUT);
175
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);
181 idr_preload_end();
182
183 if (cmd_id < 0) {
184 kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
185 return NULL;
186 }
187 tcmu_cmd->cmd_id = cmd_id;
188
189 return tcmu_cmd;
190}
191
192static inline void tcmu_flush_dcache_range(void *vaddr, size_t size)
193{
194 unsigned long offset = (unsigned long) vaddr & ~PAGE_MASK;
195
196 size = round_up(size+offset, PAGE_SIZE);
197 vaddr -= offset;
198
199 while (size) {
200 flush_dcache_page(virt_to_page(vaddr));
201 size -= PAGE_SIZE;
202 }
203}
204
205/*
206 * Some ring helper functions. We don't assume size is a power of 2 so
207 * we can't use circ_buf.h.
208 */
209static inline size_t spc_used(size_t head, size_t tail, size_t size)
210{
211 int diff = head - tail;
212
213 if (diff >= 0)
214 return diff;
215 else
216 return size + diff;
217}
218
219static inline size_t spc_free(size_t head, size_t tail, size_t size)
220{
221 /* Keep 1 byte unused or we can't tell full from empty */
222 return (size - spc_used(head, tail, size) - 1);
223}
224
225static inline size_t head_to_end(size_t head, size_t size)
226{
227 return size - head;
228}
229
230#define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size)
231
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300232static void alloc_and_scatter_data_area(struct tcmu_dev *udev,
233 struct scatterlist *data_sg, unsigned int data_nents,
234 struct iovec **iov, int *iov_cnt, bool copy_data)
235{
236 int i;
237 void *from, *to;
238 size_t copy_bytes;
239 struct scatterlist *sg;
240
241 for_each_sg(data_sg, sg, data_nents, i) {
242 copy_bytes = min_t(size_t, sg->length,
243 head_to_end(udev->data_head, udev->data_size));
244 from = kmap_atomic(sg_page(sg)) + sg->offset;
245 to = (void *) udev->mb_addr + udev->data_off + udev->data_head;
246
247 if (copy_data) {
248 memcpy(to, from, copy_bytes);
249 tcmu_flush_dcache_range(to, copy_bytes);
250 }
251
252 /* Even iov_base is relative to mb_addr */
253 (*iov)->iov_len = copy_bytes;
254 (*iov)->iov_base = (void __user *) udev->data_off +
255 udev->data_head;
256 (*iov_cnt)++;
257 (*iov)++;
258
259 UPDATE_HEAD(udev->data_head, copy_bytes, udev->data_size);
260
261 /* Uh oh, we wrapped the buffer. Must split sg across 2 iovs. */
262 if (sg->length != copy_bytes) {
Sagi Grimberge2e21bd2015-06-11 19:58:34 +0300263 void *from_skip = from + copy_bytes;
264
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300265 copy_bytes = sg->length - copy_bytes;
266
267 (*iov)->iov_len = copy_bytes;
268 (*iov)->iov_base = (void __user *) udev->data_off +
269 udev->data_head;
270
271 if (copy_data) {
272 to = (void *) udev->mb_addr +
273 udev->data_off + udev->data_head;
Sagi Grimberge2e21bd2015-06-11 19:58:34 +0300274 memcpy(to, from_skip, copy_bytes);
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300275 tcmu_flush_dcache_range(to, copy_bytes);
276 }
277
278 (*iov_cnt)++;
279 (*iov)++;
280
281 UPDATE_HEAD(udev->data_head,
282 copy_bytes, udev->data_size);
283 }
284
Sagi Grimberge2e21bd2015-06-11 19:58:34 +0300285 kunmap_atomic(from - sg->offset);
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300286 }
287}
288
289static void gather_and_free_data_area(struct tcmu_dev *udev,
290 struct scatterlist *data_sg, unsigned int data_nents)
291{
292 int i;
293 void *from, *to;
294 size_t copy_bytes;
295 struct scatterlist *sg;
296
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));
301
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);
308
309 UPDATE_HEAD(udev->data_tail, copy_bytes, udev->data_size);
310
311 /* Uh oh, wrapped the data buffer for this sg's data */
312 if (sg->length != copy_bytes) {
Sagi Grimberge2e21bd2015-06-11 19:58:34 +0300313 void *to_skip = to + copy_bytes;
314
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300315 from = (void *) udev->mb_addr +
316 udev->data_off + udev->data_tail;
317 WARN_ON(udev->data_tail);
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300318 copy_bytes = sg->length - copy_bytes;
319 tcmu_flush_dcache_range(from, copy_bytes);
Sagi Grimberge2e21bd2015-06-11 19:58:34 +0300320 memcpy(to_skip, from, copy_bytes);
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300321
322 UPDATE_HEAD(udev->data_tail,
323 copy_bytes, udev->data_size);
324 }
Sagi Grimberge2e21bd2015-06-11 19:58:34 +0300325 kunmap_atomic(to - sg->offset);
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300326 }
327}
328
Andy Grover7c9e7a62014-10-01 16:07:05 -0700329/*
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300330 * We can't queue a command until we have space available on the cmd ring *and*
331 * space available on the data ring.
Andy Grover7c9e7a62014-10-01 16:07:05 -0700332 *
333 * Called with ring lock held.
334 */
Andy Groverf56574a2014-10-02 10:23:15 -0700335static bool is_ring_space_avail(struct tcmu_dev *udev, size_t cmd_size, size_t data_needed)
Andy Grover7c9e7a62014-10-01 16:07:05 -0700336{
337 struct tcmu_mailbox *mb = udev->mb_addr;
338 size_t space;
339 u32 cmd_head;
Andy Groverf56574a2014-10-02 10:23:15 -0700340 size_t cmd_needed;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700341
342 tcmu_flush_dcache_range(mb, sizeof(*mb));
343
344 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
345
Andy Groverf56574a2014-10-02 10:23:15 -0700346 /*
347 * If cmd end-of-ring space is too small then we need space for a NOP plus
348 * original cmd - cmds are internally contiguous.
349 */
350 if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size)
351 cmd_needed = cmd_size;
352 else
353 cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size);
354
Andy Grover7c9e7a62014-10-01 16:07:05 -0700355 space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size);
356 if (space < cmd_needed) {
357 pr_debug("no cmd space: %u %u %u\n", cmd_head,
358 udev->cmdr_last_cleaned, udev->cmdr_size);
359 return false;
360 }
361
362 space = spc_free(udev->data_head, udev->data_tail, udev->data_size);
363 if (space < data_needed) {
364 pr_debug("no data space: %zu %zu %zu\n", udev->data_head,
365 udev->data_tail, udev->data_size);
366 return false;
367 }
368
369 return true;
370}
371
372static int tcmu_queue_cmd_ring(struct tcmu_cmd *tcmu_cmd)
373{
374 struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
375 struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
376 size_t base_command_size, command_size;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700377 struct tcmu_mailbox *mb;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700378 struct tcmu_cmd_entry *entry;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700379 struct iovec *iov;
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300380 int iov_cnt;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700381 uint32_t cmd_head;
382 uint64_t cdb_off;
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300383 bool copy_to_data_area;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700384
385 if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags))
386 return -EINVAL;
387
388 /*
389 * Must be a certain minimum size for response sense info, but
390 * also may be larger if the iov array is large.
391 *
392 * iovs = sgl_nents+1, for end-of-ring case, plus another 1
393 * b/c size == offsetof one-past-element.
394 */
395 base_command_size = max(offsetof(struct tcmu_cmd_entry,
Ilias Tsitsimpise4648b02015-04-23 21:30:09 +0300396 req.iov[se_cmd->t_bidi_data_nents +
397 se_cmd->t_data_nents + 2]),
Andy Grover7c9e7a62014-10-01 16:07:05 -0700398 sizeof(struct tcmu_cmd_entry));
399 command_size = base_command_size
400 + round_up(scsi_command_size(se_cmd->t_task_cdb), TCMU_OP_ALIGN_SIZE);
401
402 WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1));
403
404 spin_lock_irq(&udev->cmdr_lock);
405
406 mb = udev->mb_addr;
407 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
408 if ((command_size > (udev->cmdr_size / 2))
409 || tcmu_cmd->data_length > (udev->data_size - 1))
410 pr_warn("TCMU: Request of size %zu/%zu may be too big for %u/%zu "
411 "cmd/data ring buffers\n", command_size, tcmu_cmd->data_length,
412 udev->cmdr_size, udev->data_size);
413
Andy Groverf56574a2014-10-02 10:23:15 -0700414 while (!is_ring_space_avail(udev, command_size, tcmu_cmd->data_length)) {
Andy Grover7c9e7a62014-10-01 16:07:05 -0700415 int ret;
416 DEFINE_WAIT(__wait);
417
418 prepare_to_wait(&udev->wait_cmdr, &__wait, TASK_INTERRUPTIBLE);
419
420 pr_debug("sleeping for ring space\n");
421 spin_unlock_irq(&udev->cmdr_lock);
422 ret = schedule_timeout(msecs_to_jiffies(TCMU_TIME_OUT));
423 finish_wait(&udev->wait_cmdr, &__wait);
424 if (!ret) {
425 pr_warn("tcmu: command timed out\n");
426 return -ETIMEDOUT;
427 }
428
429 spin_lock_irq(&udev->cmdr_lock);
430
431 /* We dropped cmdr_lock, cmd_head is stale */
432 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
433 }
434
Andy Groverf56574a2014-10-02 10:23:15 -0700435 /* Insert a PAD if end-of-ring space is too small */
436 if (head_to_end(cmd_head, udev->cmdr_size) < command_size) {
437 size_t pad_size = head_to_end(cmd_head, udev->cmdr_size);
438
Andy Grover7c9e7a62014-10-01 16:07:05 -0700439 entry = (void *) mb + CMDR_OFF + cmd_head;
440 tcmu_flush_dcache_range(entry, sizeof(*entry));
Andy Grover0ad46af2015-04-14 17:30:04 -0700441 tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_PAD);
442 tcmu_hdr_set_len(&entry->hdr.len_op, pad_size);
443 entry->hdr.cmd_id = 0; /* not used for PAD */
444 entry->hdr.kflags = 0;
445 entry->hdr.uflags = 0;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700446
447 UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size);
448
449 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
450 WARN_ON(cmd_head != 0);
451 }
452
453 entry = (void *) mb + CMDR_OFF + cmd_head;
454 tcmu_flush_dcache_range(entry, sizeof(*entry));
Andy Grover0ad46af2015-04-14 17:30:04 -0700455 tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD);
456 tcmu_hdr_set_len(&entry->hdr.len_op, command_size);
457 entry->hdr.cmd_id = tcmu_cmd->cmd_id;
458 entry->hdr.kflags = 0;
459 entry->hdr.uflags = 0;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700460
461 /*
462 * Fix up iovecs, and handle if allocation in data ring wrapped.
463 */
464 iov = &entry->req.iov[0];
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300465 iov_cnt = 0;
Ilias Tsitsimpise4648b02015-04-23 21:30:09 +0300466 copy_to_data_area = (se_cmd->data_direction == DMA_TO_DEVICE
467 || se_cmd->se_cmd_flags & SCF_BIDI);
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300468 alloc_and_scatter_data_area(udev, se_cmd->t_data_sg,
469 se_cmd->t_data_nents, &iov, &iov_cnt, copy_to_data_area);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700470 entry->req.iov_cnt = iov_cnt;
Andy Grover0ad46af2015-04-14 17:30:04 -0700471 entry->req.iov_dif_cnt = 0;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700472
Ilias Tsitsimpise4648b02015-04-23 21:30:09 +0300473 /* Handle BIDI commands */
474 iov_cnt = 0;
475 alloc_and_scatter_data_area(udev, se_cmd->t_bidi_data_sg,
476 se_cmd->t_bidi_data_nents, &iov, &iov_cnt, false);
477 entry->req.iov_bidi_cnt = iov_cnt;
478
Andy Grover7c9e7a62014-10-01 16:07:05 -0700479 /* All offsets relative to mb_addr, not start of entry! */
480 cdb_off = CMDR_OFF + cmd_head + base_command_size;
481 memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb));
482 entry->req.cdb_off = cdb_off;
483 tcmu_flush_dcache_range(entry, sizeof(*entry));
484
485 UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size);
486 tcmu_flush_dcache_range(mb, sizeof(*mb));
487
488 spin_unlock_irq(&udev->cmdr_lock);
489
490 /* TODO: only if FLUSH and FUA? */
491 uio_event_notify(&udev->uio_info);
492
493 mod_timer(&udev->timeout,
494 round_jiffies_up(jiffies + msecs_to_jiffies(TCMU_TIME_OUT)));
495
496 return 0;
497}
498
499static int tcmu_queue_cmd(struct se_cmd *se_cmd)
500{
501 struct se_device *se_dev = se_cmd->se_dev;
502 struct tcmu_dev *udev = TCMU_DEV(se_dev);
503 struct tcmu_cmd *tcmu_cmd;
504 int ret;
505
506 tcmu_cmd = tcmu_alloc_cmd(se_cmd);
507 if (!tcmu_cmd)
508 return -ENOMEM;
509
510 ret = tcmu_queue_cmd_ring(tcmu_cmd);
511 if (ret < 0) {
512 pr_err("TCMU: Could not queue command\n");
513 spin_lock_irq(&udev->commands_lock);
514 idr_remove(&udev->commands, tcmu_cmd->cmd_id);
515 spin_unlock_irq(&udev->commands_lock);
516
517 kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
518 }
519
520 return ret;
521}
522
523static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *entry)
524{
525 struct se_cmd *se_cmd = cmd->se_cmd;
526 struct tcmu_dev *udev = cmd->tcmu_dev;
527
528 if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
529 /* cmd has been completed already from timeout, just reclaim data
530 ring space */
531 UPDATE_HEAD(udev->data_tail, cmd->data_length, udev->data_size);
532 return;
533 }
534
Andy Grover0ad46af2015-04-14 17:30:04 -0700535 if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) {
536 UPDATE_HEAD(udev->data_tail, cmd->data_length, udev->data_size);
537 pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n",
538 cmd->se_cmd);
539 transport_generic_request_failure(cmd->se_cmd,
540 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE);
541 cmd->se_cmd = NULL;
542 kmem_cache_free(tcmu_cmd_cache, cmd);
543 return;
544 }
545
Andy Grover7c9e7a62014-10-01 16:07:05 -0700546 if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) {
547 memcpy(se_cmd->sense_buffer, entry->rsp.sense_buffer,
548 se_cmd->scsi_sense_length);
549
550 UPDATE_HEAD(udev->data_tail, cmd->data_length, udev->data_size);
Ilias Tsitsimpise4648b02015-04-23 21:30:09 +0300551 } else if (se_cmd->se_cmd_flags & SCF_BIDI) {
552 /* Discard data_out buffer */
553 UPDATE_HEAD(udev->data_tail,
554 (size_t)se_cmd->t_data_sg->length, udev->data_size);
555
556 /* Get Data-In buffer */
557 gather_and_free_data_area(udev,
558 se_cmd->t_bidi_data_sg, se_cmd->t_bidi_data_nents);
559 } else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300560 gather_and_free_data_area(udev,
561 se_cmd->t_data_sg, se_cmd->t_data_nents);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700562 } else if (se_cmd->data_direction == DMA_TO_DEVICE) {
563 UPDATE_HEAD(udev->data_tail, cmd->data_length, udev->data_size);
Ilias Tsitsimpis2bc396a2015-04-23 21:30:05 +0300564 } else if (se_cmd->data_direction != DMA_NONE) {
565 pr_warn("TCMU: data direction was %d!\n",
566 se_cmd->data_direction);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700567 }
568
569 target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status);
570 cmd->se_cmd = NULL;
571
572 kmem_cache_free(tcmu_cmd_cache, cmd);
573}
574
575static unsigned int tcmu_handle_completions(struct tcmu_dev *udev)
576{
577 struct tcmu_mailbox *mb;
578 LIST_HEAD(cpl_cmds);
579 unsigned long flags;
580 int handled = 0;
581
582 if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) {
583 pr_err("ring broken, not handling completions\n");
584 return 0;
585 }
586
587 spin_lock_irqsave(&udev->cmdr_lock, flags);
588
589 mb = udev->mb_addr;
590 tcmu_flush_dcache_range(mb, sizeof(*mb));
591
592 while (udev->cmdr_last_cleaned != ACCESS_ONCE(mb->cmd_tail)) {
593
594 struct tcmu_cmd_entry *entry = (void *) mb + CMDR_OFF + udev->cmdr_last_cleaned;
595 struct tcmu_cmd *cmd;
596
597 tcmu_flush_dcache_range(entry, sizeof(*entry));
598
Andy Grover0ad46af2015-04-14 17:30:04 -0700599 if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD) {
600 UPDATE_HEAD(udev->cmdr_last_cleaned,
601 tcmu_hdr_get_len(entry->hdr.len_op),
602 udev->cmdr_size);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700603 continue;
604 }
Andy Grover0ad46af2015-04-14 17:30:04 -0700605 WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700606
607 spin_lock(&udev->commands_lock);
Andy Grover0ad46af2015-04-14 17:30:04 -0700608 cmd = idr_find(&udev->commands, entry->hdr.cmd_id);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700609 if (cmd)
610 idr_remove(&udev->commands, cmd->cmd_id);
611 spin_unlock(&udev->commands_lock);
612
613 if (!cmd) {
614 pr_err("cmd_id not found, ring is broken\n");
615 set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags);
616 break;
617 }
618
619 tcmu_handle_completion(cmd, entry);
620
Andy Grover0ad46af2015-04-14 17:30:04 -0700621 UPDATE_HEAD(udev->cmdr_last_cleaned,
622 tcmu_hdr_get_len(entry->hdr.len_op),
623 udev->cmdr_size);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700624
625 handled++;
626 }
627
628 if (mb->cmd_tail == mb->cmd_head)
629 del_timer(&udev->timeout); /* no more pending cmds */
630
631 spin_unlock_irqrestore(&udev->cmdr_lock, flags);
632
633 wake_up(&udev->wait_cmdr);
634
635 return handled;
636}
637
638static int tcmu_check_expired_cmd(int id, void *p, void *data)
639{
640 struct tcmu_cmd *cmd = p;
641
642 if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
643 return 0;
644
645 if (!time_after(cmd->deadline, jiffies))
646 return 0;
647
648 set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags);
649 target_complete_cmd(cmd->se_cmd, SAM_STAT_CHECK_CONDITION);
650 cmd->se_cmd = NULL;
651
652 kmem_cache_free(tcmu_cmd_cache, cmd);
653
654 return 0;
655}
656
657static void tcmu_device_timedout(unsigned long data)
658{
659 struct tcmu_dev *udev = (struct tcmu_dev *)data;
660 unsigned long flags;
661 int handled;
662
663 handled = tcmu_handle_completions(udev);
664
665 pr_warn("%d completions handled from timeout\n", handled);
666
667 spin_lock_irqsave(&udev->commands_lock, flags);
668 idr_for_each(&udev->commands, tcmu_check_expired_cmd, NULL);
669 spin_unlock_irqrestore(&udev->commands_lock, flags);
670
671 /*
672 * We don't need to wakeup threads on wait_cmdr since they have their
673 * own timeout.
674 */
675}
676
677static int tcmu_attach_hba(struct se_hba *hba, u32 host_id)
678{
679 struct tcmu_hba *tcmu_hba;
680
681 tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL);
682 if (!tcmu_hba)
683 return -ENOMEM;
684
685 tcmu_hba->host_id = host_id;
686 hba->hba_ptr = tcmu_hba;
687
688 return 0;
689}
690
691static void tcmu_detach_hba(struct se_hba *hba)
692{
693 kfree(hba->hba_ptr);
694 hba->hba_ptr = NULL;
695}
696
697static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
698{
699 struct tcmu_dev *udev;
700
701 udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL);
702 if (!udev)
703 return NULL;
704
705 udev->name = kstrdup(name, GFP_KERNEL);
706 if (!udev->name) {
707 kfree(udev);
708 return NULL;
709 }
710
711 udev->hba = hba;
712
713 init_waitqueue_head(&udev->wait_cmdr);
714 spin_lock_init(&udev->cmdr_lock);
715
716 idr_init(&udev->commands);
717 spin_lock_init(&udev->commands_lock);
718
719 setup_timer(&udev->timeout, tcmu_device_timedout,
720 (unsigned long)udev);
721
Andy Grover7c9e7a62014-10-01 16:07:05 -0700722 return &udev->se_dev;
723}
724
725static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on)
726{
727 struct tcmu_dev *tcmu_dev = container_of(info, struct tcmu_dev, uio_info);
728
729 tcmu_handle_completions(tcmu_dev);
730
731 return 0;
732}
733
734/*
735 * mmap code from uio.c. Copied here because we want to hook mmap()
736 * and this stuff must come along.
737 */
738static int tcmu_find_mem_index(struct vm_area_struct *vma)
739{
740 struct tcmu_dev *udev = vma->vm_private_data;
741 struct uio_info *info = &udev->uio_info;
742
743 if (vma->vm_pgoff < MAX_UIO_MAPS) {
744 if (info->mem[vma->vm_pgoff].size == 0)
745 return -1;
746 return (int)vma->vm_pgoff;
747 }
748 return -1;
749}
750
751static int tcmu_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
752{
753 struct tcmu_dev *udev = vma->vm_private_data;
754 struct uio_info *info = &udev->uio_info;
755 struct page *page;
756 unsigned long offset;
757 void *addr;
758
759 int mi = tcmu_find_mem_index(vma);
760 if (mi < 0)
761 return VM_FAULT_SIGBUS;
762
763 /*
764 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
765 * to use mem[N].
766 */
767 offset = (vmf->pgoff - mi) << PAGE_SHIFT;
768
769 addr = (void *)(unsigned long)info->mem[mi].addr + offset;
770 if (info->mem[mi].memtype == UIO_MEM_LOGICAL)
771 page = virt_to_page(addr);
772 else
773 page = vmalloc_to_page(addr);
774 get_page(page);
775 vmf->page = page;
776 return 0;
777}
778
779static const struct vm_operations_struct tcmu_vm_ops = {
780 .fault = tcmu_vma_fault,
781};
782
783static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma)
784{
785 struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
786
787 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
788 vma->vm_ops = &tcmu_vm_ops;
789
790 vma->vm_private_data = udev;
791
792 /* Ensure the mmap is exactly the right size */
793 if (vma_pages(vma) != (TCMU_RING_SIZE >> PAGE_SHIFT))
794 return -EINVAL;
795
796 return 0;
797}
798
799static int tcmu_open(struct uio_info *info, struct inode *inode)
800{
801 struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
802
803 /* O_EXCL not supported for char devs, so fake it? */
804 if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags))
805 return -EBUSY;
806
807 pr_debug("open\n");
808
809 return 0;
810}
811
812static int tcmu_release(struct uio_info *info, struct inode *inode)
813{
814 struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
815
816 clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags);
817
818 pr_debug("close\n");
819
820 return 0;
821}
822
823static int tcmu_netlink_event(enum tcmu_genl_cmd cmd, const char *name, int minor)
824{
825 struct sk_buff *skb;
826 void *msg_header;
Nicholas Bellinger6e14eab2014-10-01 23:01:15 -0700827 int ret = -ENOMEM;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700828
829 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
830 if (!skb)
Nicholas Bellinger6e14eab2014-10-01 23:01:15 -0700831 return ret;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700832
833 msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd);
Nicholas Bellinger6e14eab2014-10-01 23:01:15 -0700834 if (!msg_header)
835 goto free_skb;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700836
837 ret = nla_put_string(skb, TCMU_ATTR_DEVICE, name);
Nicholas Bellinger6e14eab2014-10-01 23:01:15 -0700838 if (ret < 0)
839 goto free_skb;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700840
841 ret = nla_put_u32(skb, TCMU_ATTR_MINOR, minor);
Nicholas Bellinger6e14eab2014-10-01 23:01:15 -0700842 if (ret < 0)
843 goto free_skb;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700844
Johannes Berg053c0952015-01-16 22:09:00 +0100845 genlmsg_end(skb, msg_header);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700846
847 ret = genlmsg_multicast(&tcmu_genl_family, skb, 0,
848 TCMU_MCGRP_CONFIG, GFP_KERNEL);
849
850 /* We don't care if no one is listening */
851 if (ret == -ESRCH)
852 ret = 0;
853
854 return ret;
Nicholas Bellinger6e14eab2014-10-01 23:01:15 -0700855free_skb:
856 nlmsg_free(skb);
857 return ret;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700858}
859
860static int tcmu_configure_device(struct se_device *dev)
861{
862 struct tcmu_dev *udev = TCMU_DEV(dev);
863 struct tcmu_hba *hba = udev->hba->hba_ptr;
864 struct uio_info *info;
865 struct tcmu_mailbox *mb;
866 size_t size;
867 size_t used;
868 int ret = 0;
869 char *str;
870
871 info = &udev->uio_info;
872
873 size = snprintf(NULL, 0, "tcm-user/%u/%s/%s", hba->host_id, udev->name,
874 udev->dev_config);
875 size += 1; /* for \0 */
876 str = kmalloc(size, GFP_KERNEL);
877 if (!str)
878 return -ENOMEM;
879
880 used = snprintf(str, size, "tcm-user/%u/%s", hba->host_id, udev->name);
881
882 if (udev->dev_config[0])
883 snprintf(str + used, size - used, "/%s", udev->dev_config);
884
885 info->name = str;
886
887 udev->mb_addr = vzalloc(TCMU_RING_SIZE);
888 if (!udev->mb_addr) {
889 ret = -ENOMEM;
890 goto err_vzalloc;
891 }
892
893 /* mailbox fits in first part of CMDR space */
894 udev->cmdr_size = CMDR_SIZE - CMDR_OFF;
895 udev->data_off = CMDR_SIZE;
896 udev->data_size = TCMU_RING_SIZE - CMDR_SIZE;
897
898 mb = udev->mb_addr;
Andy Grover0ad46af2015-04-14 17:30:04 -0700899 mb->version = TCMU_MAILBOX_VERSION;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700900 mb->cmdr_off = CMDR_OFF;
901 mb->cmdr_size = udev->cmdr_size;
902
903 WARN_ON(!PAGE_ALIGNED(udev->data_off));
904 WARN_ON(udev->data_size % PAGE_SIZE);
905
Andy Grover0ad46af2015-04-14 17:30:04 -0700906 info->version = xstr(TCMU_MAILBOX_VERSION);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700907
908 info->mem[0].name = "tcm-user command & data buffer";
909 info->mem[0].addr = (phys_addr_t) udev->mb_addr;
910 info->mem[0].size = TCMU_RING_SIZE;
911 info->mem[0].memtype = UIO_MEM_VIRTUAL;
912
913 info->irqcontrol = tcmu_irqcontrol;
914 info->irq = UIO_IRQ_CUSTOM;
915
916 info->mmap = tcmu_mmap;
917 info->open = tcmu_open;
918 info->release = tcmu_release;
919
920 ret = uio_register_device(tcmu_root_device, info);
921 if (ret)
922 goto err_register;
923
924 /* Other attributes can be configured in userspace */
925 dev->dev_attrib.hw_block_size = 512;
926 dev->dev_attrib.hw_max_sectors = 128;
927 dev->dev_attrib.hw_queue_depth = 128;
928
929 ret = tcmu_netlink_event(TCMU_CMD_ADDED_DEVICE, udev->uio_info.name,
930 udev->uio_info.uio_dev->minor);
931 if (ret)
932 goto err_netlink;
933
934 return 0;
935
936err_netlink:
937 uio_unregister_device(&udev->uio_info);
938err_register:
939 vfree(udev->mb_addr);
940err_vzalloc:
941 kfree(info->name);
942
943 return ret;
944}
945
946static int tcmu_check_pending_cmd(int id, void *p, void *data)
947{
948 struct tcmu_cmd *cmd = p;
949
950 if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
951 return 0;
952 return -EINVAL;
953}
954
Nicholas Bellinger4cc987e2015-05-19 00:03:07 -0700955static void tcmu_dev_call_rcu(struct rcu_head *p)
956{
957 struct se_device *dev = container_of(p, struct se_device, rcu_head);
958 struct tcmu_dev *udev = TCMU_DEV(dev);
959
960 kfree(udev);
961}
962
Andy Grover7c9e7a62014-10-01 16:07:05 -0700963static void tcmu_free_device(struct se_device *dev)
964{
965 struct tcmu_dev *udev = TCMU_DEV(dev);
966 int i;
967
968 del_timer_sync(&udev->timeout);
969
970 vfree(udev->mb_addr);
971
972 /* Upper layer should drain all requests before calling this */
973 spin_lock_irq(&udev->commands_lock);
974 i = idr_for_each(&udev->commands, tcmu_check_pending_cmd, NULL);
975 idr_destroy(&udev->commands);
976 spin_unlock_irq(&udev->commands_lock);
977 WARN_ON(i);
978
979 /* Device was configured */
980 if (udev->uio_info.uio_dev) {
981 tcmu_netlink_event(TCMU_CMD_REMOVED_DEVICE, udev->uio_info.name,
982 udev->uio_info.uio_dev->minor);
983
984 uio_unregister_device(&udev->uio_info);
985 kfree(udev->uio_info.name);
986 kfree(udev->name);
987 }
Nicholas Bellinger4cc987e2015-05-19 00:03:07 -0700988 call_rcu(&dev->rcu_head, tcmu_dev_call_rcu);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700989}
990
991enum {
Andy Grover9c1cd1b2015-05-19 14:44:39 -0700992 Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_err,
Andy Grover7c9e7a62014-10-01 16:07:05 -0700993};
994
995static match_table_t tokens = {
996 {Opt_dev_config, "dev_config=%s"},
997 {Opt_dev_size, "dev_size=%u"},
Andy Grover9c1cd1b2015-05-19 14:44:39 -0700998 {Opt_hw_block_size, "hw_block_size=%u"},
Andy Grover7c9e7a62014-10-01 16:07:05 -0700999 {Opt_err, NULL}
1000};
1001
1002static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev,
1003 const char *page, ssize_t count)
1004{
1005 struct tcmu_dev *udev = TCMU_DEV(dev);
1006 char *orig, *ptr, *opts, *arg_p;
1007 substring_t args[MAX_OPT_ARGS];
1008 int ret = 0, token;
Andy Grover9c1cd1b2015-05-19 14:44:39 -07001009 unsigned long tmp_ul;
Andy Grover7c9e7a62014-10-01 16:07:05 -07001010
1011 opts = kstrdup(page, GFP_KERNEL);
1012 if (!opts)
1013 return -ENOMEM;
1014
1015 orig = opts;
1016
1017 while ((ptr = strsep(&opts, ",\n")) != NULL) {
1018 if (!*ptr)
1019 continue;
1020
1021 token = match_token(ptr, tokens, args);
1022 switch (token) {
1023 case Opt_dev_config:
1024 if (match_strlcpy(udev->dev_config, &args[0],
1025 TCMU_CONFIG_LEN) == 0) {
1026 ret = -EINVAL;
1027 break;
1028 }
1029 pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config);
1030 break;
1031 case Opt_dev_size:
1032 arg_p = match_strdup(&args[0]);
1033 if (!arg_p) {
1034 ret = -ENOMEM;
1035 break;
1036 }
1037 ret = kstrtoul(arg_p, 0, (unsigned long *) &udev->dev_size);
1038 kfree(arg_p);
1039 if (ret < 0)
1040 pr_err("kstrtoul() failed for dev_size=\n");
1041 break;
Andy Grover9c1cd1b2015-05-19 14:44:39 -07001042 case Opt_hw_block_size:
1043 arg_p = match_strdup(&args[0]);
1044 if (!arg_p) {
1045 ret = -ENOMEM;
1046 break;
1047 }
1048 ret = kstrtoul(arg_p, 0, &tmp_ul);
1049 kfree(arg_p);
1050 if (ret < 0) {
1051 pr_err("kstrtoul() failed for hw_block_size=\n");
1052 break;
1053 }
1054 if (!tmp_ul) {
1055 pr_err("hw_block_size must be nonzero\n");
1056 break;
1057 }
1058 dev->dev_attrib.hw_block_size = tmp_ul;
1059 break;
Andy Grover7c9e7a62014-10-01 16:07:05 -07001060 default:
1061 break;
1062 }
1063 }
1064
1065 kfree(orig);
1066 return (!ret) ? count : ret;
1067}
1068
1069static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b)
1070{
1071 struct tcmu_dev *udev = TCMU_DEV(dev);
1072 ssize_t bl = 0;
1073
1074 bl = sprintf(b + bl, "Config: %s ",
1075 udev->dev_config[0] ? udev->dev_config : "NULL");
Andy Grover8ee83a72015-05-01 10:43:45 -07001076 bl += sprintf(b + bl, "Size: %zu\n", udev->dev_size);
Andy Grover7c9e7a62014-10-01 16:07:05 -07001077
1078 return bl;
1079}
1080
1081static sector_t tcmu_get_blocks(struct se_device *dev)
1082{
1083 struct tcmu_dev *udev = TCMU_DEV(dev);
1084
1085 return div_u64(udev->dev_size - dev->dev_attrib.block_size,
1086 dev->dev_attrib.block_size);
1087}
1088
1089static sense_reason_t
Andy Grover7c9e7a62014-10-01 16:07:05 -07001090tcmu_pass_op(struct se_cmd *se_cmd)
1091{
1092 int ret = tcmu_queue_cmd(se_cmd);
1093
1094 if (ret != 0)
1095 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1096 else
1097 return TCM_NO_SENSE;
1098}
1099
Andy Grover7c9e7a62014-10-01 16:07:05 -07001100static sense_reason_t
1101tcmu_parse_cdb(struct se_cmd *cmd)
1102{
Andy Grover7bfea53b2015-05-19 14:44:40 -07001103 return passthrough_parse_cdb(cmd, tcmu_pass_op);
Andy Grover7c9e7a62014-10-01 16:07:05 -07001104}
1105
Christoph Hellwig0a06d432015-05-10 18:14:56 +02001106static const struct target_backend_ops tcmu_ops = {
Andy Grover7c9e7a62014-10-01 16:07:05 -07001107 .name = "user",
1108 .inquiry_prod = "USER",
1109 .inquiry_rev = TCMU_VERSION,
1110 .owner = THIS_MODULE,
Andy Grovera3541702015-05-19 14:44:41 -07001111 .transport_flags = TRANSPORT_FLAG_PASSTHROUGH,
Andy Grover7c9e7a62014-10-01 16:07:05 -07001112 .attach_hba = tcmu_attach_hba,
1113 .detach_hba = tcmu_detach_hba,
1114 .alloc_device = tcmu_alloc_device,
1115 .configure_device = tcmu_configure_device,
1116 .free_device = tcmu_free_device,
1117 .parse_cdb = tcmu_parse_cdb,
1118 .set_configfs_dev_params = tcmu_set_configfs_dev_params,
1119 .show_configfs_dev_params = tcmu_show_configfs_dev_params,
1120 .get_device_type = sbc_get_device_type,
1121 .get_blocks = tcmu_get_blocks,
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001122 .tb_dev_attrib_attrs = passthrough_attrib_attrs,
Andy Grover7c9e7a62014-10-01 16:07:05 -07001123};
1124
1125static int __init tcmu_module_init(void)
1126{
1127 int ret;
1128
1129 BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0);
1130
1131 tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache",
1132 sizeof(struct tcmu_cmd),
1133 __alignof__(struct tcmu_cmd),
1134 0, NULL);
1135 if (!tcmu_cmd_cache)
1136 return -ENOMEM;
1137
1138 tcmu_root_device = root_device_register("tcm_user");
1139 if (IS_ERR(tcmu_root_device)) {
1140 ret = PTR_ERR(tcmu_root_device);
1141 goto out_free_cache;
1142 }
1143
1144 ret = genl_register_family(&tcmu_genl_family);
1145 if (ret < 0) {
1146 goto out_unreg_device;
1147 }
1148
Christoph Hellwig0a06d432015-05-10 18:14:56 +02001149 ret = transport_backend_register(&tcmu_ops);
Andy Grover7c9e7a62014-10-01 16:07:05 -07001150 if (ret)
1151 goto out_unreg_genl;
1152
1153 return 0;
1154
1155out_unreg_genl:
1156 genl_unregister_family(&tcmu_genl_family);
1157out_unreg_device:
1158 root_device_unregister(tcmu_root_device);
1159out_free_cache:
1160 kmem_cache_destroy(tcmu_cmd_cache);
1161
1162 return ret;
1163}
1164
1165static void __exit tcmu_module_exit(void)
1166{
Christoph Hellwig0a06d432015-05-10 18:14:56 +02001167 target_backend_unregister(&tcmu_ops);
Andy Grover7c9e7a62014-10-01 16:07:05 -07001168 genl_unregister_family(&tcmu_genl_family);
1169 root_device_unregister(tcmu_root_device);
1170 kmem_cache_destroy(tcmu_cmd_cache);
1171}
1172
1173MODULE_DESCRIPTION("TCM USER subsystem plugin");
1174MODULE_AUTHOR("Shaohua Li <shli@kernel.org>");
1175MODULE_AUTHOR("Andy Grover <agrover@redhat.com>");
1176MODULE_LICENSE("GPL");
1177
1178module_init(tcmu_module_init);
1179module_exit(tcmu_module_exit);