blob: 949e6165ef8a758b1d0aab4c80150d6c92bc501c [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) {
263 from += copy_bytes;
264 copy_bytes = sg->length - copy_bytes;
265
266 (*iov)->iov_len = copy_bytes;
267 (*iov)->iov_base = (void __user *) udev->data_off +
268 udev->data_head;
269
270 if (copy_data) {
271 to = (void *) udev->mb_addr +
272 udev->data_off + udev->data_head;
273 memcpy(to, from, copy_bytes);
274 tcmu_flush_dcache_range(to, copy_bytes);
275 }
276
277 (*iov_cnt)++;
278 (*iov)++;
279
280 UPDATE_HEAD(udev->data_head,
281 copy_bytes, udev->data_size);
282 }
283
284 kunmap_atomic(from);
285 }
286}
287
288static void gather_and_free_data_area(struct tcmu_dev *udev,
289 struct scatterlist *data_sg, unsigned int data_nents)
290{
291 int i;
292 void *from, *to;
293 size_t copy_bytes;
294 struct scatterlist *sg;
295
296 /* It'd be easier to look at entry's iovec again, but UAM */
297 for_each_sg(data_sg, sg, data_nents, i) {
298 copy_bytes = min_t(size_t, sg->length,
299 head_to_end(udev->data_tail, udev->data_size));
300
301 to = kmap_atomic(sg_page(sg)) + sg->offset;
302 WARN_ON(sg->length + sg->offset > PAGE_SIZE);
303 from = (void *) udev->mb_addr +
304 udev->data_off + udev->data_tail;
305 tcmu_flush_dcache_range(from, copy_bytes);
306 memcpy(to, from, copy_bytes);
307
308 UPDATE_HEAD(udev->data_tail, copy_bytes, udev->data_size);
309
310 /* Uh oh, wrapped the data buffer for this sg's data */
311 if (sg->length != copy_bytes) {
312 from = (void *) udev->mb_addr +
313 udev->data_off + udev->data_tail;
314 WARN_ON(udev->data_tail);
315 to += copy_bytes;
316 copy_bytes = sg->length - copy_bytes;
317 tcmu_flush_dcache_range(from, copy_bytes);
318 memcpy(to, from, copy_bytes);
319
320 UPDATE_HEAD(udev->data_tail,
321 copy_bytes, udev->data_size);
322 }
323 kunmap_atomic(to);
324 }
325}
326
Andy Grover7c9e7a62014-10-01 16:07:05 -0700327/*
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300328 * We can't queue a command until we have space available on the cmd ring *and*
329 * space available on the data ring.
Andy Grover7c9e7a62014-10-01 16:07:05 -0700330 *
331 * Called with ring lock held.
332 */
Andy Groverf56574a2014-10-02 10:23:15 -0700333static bool is_ring_space_avail(struct tcmu_dev *udev, size_t cmd_size, size_t data_needed)
Andy Grover7c9e7a62014-10-01 16:07:05 -0700334{
335 struct tcmu_mailbox *mb = udev->mb_addr;
336 size_t space;
337 u32 cmd_head;
Andy Groverf56574a2014-10-02 10:23:15 -0700338 size_t cmd_needed;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700339
340 tcmu_flush_dcache_range(mb, sizeof(*mb));
341
342 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
343
Andy Groverf56574a2014-10-02 10:23:15 -0700344 /*
345 * If cmd end-of-ring space is too small then we need space for a NOP plus
346 * original cmd - cmds are internally contiguous.
347 */
348 if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size)
349 cmd_needed = cmd_size;
350 else
351 cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size);
352
Andy Grover7c9e7a62014-10-01 16:07:05 -0700353 space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size);
354 if (space < cmd_needed) {
355 pr_debug("no cmd space: %u %u %u\n", cmd_head,
356 udev->cmdr_last_cleaned, udev->cmdr_size);
357 return false;
358 }
359
360 space = spc_free(udev->data_head, udev->data_tail, udev->data_size);
361 if (space < data_needed) {
362 pr_debug("no data space: %zu %zu %zu\n", udev->data_head,
363 udev->data_tail, udev->data_size);
364 return false;
365 }
366
367 return true;
368}
369
370static int tcmu_queue_cmd_ring(struct tcmu_cmd *tcmu_cmd)
371{
372 struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
373 struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
374 size_t base_command_size, command_size;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700375 struct tcmu_mailbox *mb;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700376 struct tcmu_cmd_entry *entry;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700377 struct iovec *iov;
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300378 int iov_cnt;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700379 uint32_t cmd_head;
380 uint64_t cdb_off;
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300381 bool copy_to_data_area;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700382
383 if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags))
384 return -EINVAL;
385
386 /*
387 * Must be a certain minimum size for response sense info, but
388 * also may be larger if the iov array is large.
389 *
390 * iovs = sgl_nents+1, for end-of-ring case, plus another 1
391 * b/c size == offsetof one-past-element.
392 */
393 base_command_size = max(offsetof(struct tcmu_cmd_entry,
Ilias Tsitsimpise4648b02015-04-23 21:30:09 +0300394 req.iov[se_cmd->t_bidi_data_nents +
395 se_cmd->t_data_nents + 2]),
Andy Grover7c9e7a62014-10-01 16:07:05 -0700396 sizeof(struct tcmu_cmd_entry));
397 command_size = base_command_size
398 + round_up(scsi_command_size(se_cmd->t_task_cdb), TCMU_OP_ALIGN_SIZE);
399
400 WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1));
401
402 spin_lock_irq(&udev->cmdr_lock);
403
404 mb = udev->mb_addr;
405 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
406 if ((command_size > (udev->cmdr_size / 2))
407 || tcmu_cmd->data_length > (udev->data_size - 1))
408 pr_warn("TCMU: Request of size %zu/%zu may be too big for %u/%zu "
409 "cmd/data ring buffers\n", command_size, tcmu_cmd->data_length,
410 udev->cmdr_size, udev->data_size);
411
Andy Groverf56574a2014-10-02 10:23:15 -0700412 while (!is_ring_space_avail(udev, command_size, tcmu_cmd->data_length)) {
Andy Grover7c9e7a62014-10-01 16:07:05 -0700413 int ret;
414 DEFINE_WAIT(__wait);
415
416 prepare_to_wait(&udev->wait_cmdr, &__wait, TASK_INTERRUPTIBLE);
417
418 pr_debug("sleeping for ring space\n");
419 spin_unlock_irq(&udev->cmdr_lock);
420 ret = schedule_timeout(msecs_to_jiffies(TCMU_TIME_OUT));
421 finish_wait(&udev->wait_cmdr, &__wait);
422 if (!ret) {
423 pr_warn("tcmu: command timed out\n");
424 return -ETIMEDOUT;
425 }
426
427 spin_lock_irq(&udev->cmdr_lock);
428
429 /* We dropped cmdr_lock, cmd_head is stale */
430 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
431 }
432
Andy Groverf56574a2014-10-02 10:23:15 -0700433 /* Insert a PAD if end-of-ring space is too small */
434 if (head_to_end(cmd_head, udev->cmdr_size) < command_size) {
435 size_t pad_size = head_to_end(cmd_head, udev->cmdr_size);
436
Andy Grover7c9e7a62014-10-01 16:07:05 -0700437 entry = (void *) mb + CMDR_OFF + cmd_head;
438 tcmu_flush_dcache_range(entry, sizeof(*entry));
Andy Grover0ad46af2015-04-14 17:30:04 -0700439 tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_PAD);
440 tcmu_hdr_set_len(&entry->hdr.len_op, pad_size);
441 entry->hdr.cmd_id = 0; /* not used for PAD */
442 entry->hdr.kflags = 0;
443 entry->hdr.uflags = 0;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700444
445 UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size);
446
447 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
448 WARN_ON(cmd_head != 0);
449 }
450
451 entry = (void *) mb + CMDR_OFF + cmd_head;
452 tcmu_flush_dcache_range(entry, sizeof(*entry));
Andy Grover0ad46af2015-04-14 17:30:04 -0700453 tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD);
454 tcmu_hdr_set_len(&entry->hdr.len_op, command_size);
455 entry->hdr.cmd_id = tcmu_cmd->cmd_id;
456 entry->hdr.kflags = 0;
457 entry->hdr.uflags = 0;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700458
459 /*
460 * Fix up iovecs, and handle if allocation in data ring wrapped.
461 */
462 iov = &entry->req.iov[0];
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300463 iov_cnt = 0;
Ilias Tsitsimpise4648b02015-04-23 21:30:09 +0300464 copy_to_data_area = (se_cmd->data_direction == DMA_TO_DEVICE
465 || se_cmd->se_cmd_flags & SCF_BIDI);
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300466 alloc_and_scatter_data_area(udev, se_cmd->t_data_sg,
467 se_cmd->t_data_nents, &iov, &iov_cnt, copy_to_data_area);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700468 entry->req.iov_cnt = iov_cnt;
Andy Grover0ad46af2015-04-14 17:30:04 -0700469 entry->req.iov_dif_cnt = 0;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700470
Ilias Tsitsimpise4648b02015-04-23 21:30:09 +0300471 /* Handle BIDI commands */
472 iov_cnt = 0;
473 alloc_and_scatter_data_area(udev, se_cmd->t_bidi_data_sg,
474 se_cmd->t_bidi_data_nents, &iov, &iov_cnt, false);
475 entry->req.iov_bidi_cnt = iov_cnt;
476
Andy Grover7c9e7a62014-10-01 16:07:05 -0700477 /* All offsets relative to mb_addr, not start of entry! */
478 cdb_off = CMDR_OFF + cmd_head + base_command_size;
479 memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb));
480 entry->req.cdb_off = cdb_off;
481 tcmu_flush_dcache_range(entry, sizeof(*entry));
482
483 UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size);
484 tcmu_flush_dcache_range(mb, sizeof(*mb));
485
486 spin_unlock_irq(&udev->cmdr_lock);
487
488 /* TODO: only if FLUSH and FUA? */
489 uio_event_notify(&udev->uio_info);
490
491 mod_timer(&udev->timeout,
492 round_jiffies_up(jiffies + msecs_to_jiffies(TCMU_TIME_OUT)));
493
494 return 0;
495}
496
497static int tcmu_queue_cmd(struct se_cmd *se_cmd)
498{
499 struct se_device *se_dev = se_cmd->se_dev;
500 struct tcmu_dev *udev = TCMU_DEV(se_dev);
501 struct tcmu_cmd *tcmu_cmd;
502 int ret;
503
504 tcmu_cmd = tcmu_alloc_cmd(se_cmd);
505 if (!tcmu_cmd)
506 return -ENOMEM;
507
508 ret = tcmu_queue_cmd_ring(tcmu_cmd);
509 if (ret < 0) {
510 pr_err("TCMU: Could not queue command\n");
511 spin_lock_irq(&udev->commands_lock);
512 idr_remove(&udev->commands, tcmu_cmd->cmd_id);
513 spin_unlock_irq(&udev->commands_lock);
514
515 kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
516 }
517
518 return ret;
519}
520
521static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *entry)
522{
523 struct se_cmd *se_cmd = cmd->se_cmd;
524 struct tcmu_dev *udev = cmd->tcmu_dev;
525
526 if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
527 /* cmd has been completed already from timeout, just reclaim data
528 ring space */
529 UPDATE_HEAD(udev->data_tail, cmd->data_length, udev->data_size);
530 return;
531 }
532
Andy Grover0ad46af2015-04-14 17:30:04 -0700533 if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) {
534 UPDATE_HEAD(udev->data_tail, cmd->data_length, udev->data_size);
535 pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n",
536 cmd->se_cmd);
537 transport_generic_request_failure(cmd->se_cmd,
538 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE);
539 cmd->se_cmd = NULL;
540 kmem_cache_free(tcmu_cmd_cache, cmd);
541 return;
542 }
543
Andy Grover7c9e7a62014-10-01 16:07:05 -0700544 if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) {
545 memcpy(se_cmd->sense_buffer, entry->rsp.sense_buffer,
546 se_cmd->scsi_sense_length);
547
548 UPDATE_HEAD(udev->data_tail, cmd->data_length, udev->data_size);
Ilias Tsitsimpise4648b02015-04-23 21:30:09 +0300549 } else if (se_cmd->se_cmd_flags & SCF_BIDI) {
550 /* Discard data_out buffer */
551 UPDATE_HEAD(udev->data_tail,
552 (size_t)se_cmd->t_data_sg->length, udev->data_size);
553
554 /* Get Data-In buffer */
555 gather_and_free_data_area(udev,
556 se_cmd->t_bidi_data_sg, se_cmd->t_bidi_data_nents);
557 } else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300558 gather_and_free_data_area(udev,
559 se_cmd->t_data_sg, se_cmd->t_data_nents);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700560 } else if (se_cmd->data_direction == DMA_TO_DEVICE) {
561 UPDATE_HEAD(udev->data_tail, cmd->data_length, udev->data_size);
Ilias Tsitsimpis2bc396a2015-04-23 21:30:05 +0300562 } else if (se_cmd->data_direction != DMA_NONE) {
563 pr_warn("TCMU: data direction was %d!\n",
564 se_cmd->data_direction);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700565 }
566
567 target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status);
568 cmd->se_cmd = NULL;
569
570 kmem_cache_free(tcmu_cmd_cache, cmd);
571}
572
573static unsigned int tcmu_handle_completions(struct tcmu_dev *udev)
574{
575 struct tcmu_mailbox *mb;
576 LIST_HEAD(cpl_cmds);
577 unsigned long flags;
578 int handled = 0;
579
580 if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) {
581 pr_err("ring broken, not handling completions\n");
582 return 0;
583 }
584
585 spin_lock_irqsave(&udev->cmdr_lock, flags);
586
587 mb = udev->mb_addr;
588 tcmu_flush_dcache_range(mb, sizeof(*mb));
589
590 while (udev->cmdr_last_cleaned != ACCESS_ONCE(mb->cmd_tail)) {
591
592 struct tcmu_cmd_entry *entry = (void *) mb + CMDR_OFF + udev->cmdr_last_cleaned;
593 struct tcmu_cmd *cmd;
594
595 tcmu_flush_dcache_range(entry, sizeof(*entry));
596
Andy Grover0ad46af2015-04-14 17:30:04 -0700597 if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD) {
598 UPDATE_HEAD(udev->cmdr_last_cleaned,
599 tcmu_hdr_get_len(entry->hdr.len_op),
600 udev->cmdr_size);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700601 continue;
602 }
Andy Grover0ad46af2015-04-14 17:30:04 -0700603 WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700604
605 spin_lock(&udev->commands_lock);
Andy Grover0ad46af2015-04-14 17:30:04 -0700606 cmd = idr_find(&udev->commands, entry->hdr.cmd_id);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700607 if (cmd)
608 idr_remove(&udev->commands, cmd->cmd_id);
609 spin_unlock(&udev->commands_lock);
610
611 if (!cmd) {
612 pr_err("cmd_id not found, ring is broken\n");
613 set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags);
614 break;
615 }
616
617 tcmu_handle_completion(cmd, entry);
618
Andy Grover0ad46af2015-04-14 17:30:04 -0700619 UPDATE_HEAD(udev->cmdr_last_cleaned,
620 tcmu_hdr_get_len(entry->hdr.len_op),
621 udev->cmdr_size);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700622
623 handled++;
624 }
625
626 if (mb->cmd_tail == mb->cmd_head)
627 del_timer(&udev->timeout); /* no more pending cmds */
628
629 spin_unlock_irqrestore(&udev->cmdr_lock, flags);
630
631 wake_up(&udev->wait_cmdr);
632
633 return handled;
634}
635
636static int tcmu_check_expired_cmd(int id, void *p, void *data)
637{
638 struct tcmu_cmd *cmd = p;
639
640 if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
641 return 0;
642
643 if (!time_after(cmd->deadline, jiffies))
644 return 0;
645
646 set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags);
647 target_complete_cmd(cmd->se_cmd, SAM_STAT_CHECK_CONDITION);
648 cmd->se_cmd = NULL;
649
650 kmem_cache_free(tcmu_cmd_cache, cmd);
651
652 return 0;
653}
654
655static void tcmu_device_timedout(unsigned long data)
656{
657 struct tcmu_dev *udev = (struct tcmu_dev *)data;
658 unsigned long flags;
659 int handled;
660
661 handled = tcmu_handle_completions(udev);
662
663 pr_warn("%d completions handled from timeout\n", handled);
664
665 spin_lock_irqsave(&udev->commands_lock, flags);
666 idr_for_each(&udev->commands, tcmu_check_expired_cmd, NULL);
667 spin_unlock_irqrestore(&udev->commands_lock, flags);
668
669 /*
670 * We don't need to wakeup threads on wait_cmdr since they have their
671 * own timeout.
672 */
673}
674
675static int tcmu_attach_hba(struct se_hba *hba, u32 host_id)
676{
677 struct tcmu_hba *tcmu_hba;
678
679 tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL);
680 if (!tcmu_hba)
681 return -ENOMEM;
682
683 tcmu_hba->host_id = host_id;
684 hba->hba_ptr = tcmu_hba;
685
686 return 0;
687}
688
689static void tcmu_detach_hba(struct se_hba *hba)
690{
691 kfree(hba->hba_ptr);
692 hba->hba_ptr = NULL;
693}
694
695static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
696{
697 struct tcmu_dev *udev;
698
699 udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL);
700 if (!udev)
701 return NULL;
702
703 udev->name = kstrdup(name, GFP_KERNEL);
704 if (!udev->name) {
705 kfree(udev);
706 return NULL;
707 }
708
709 udev->hba = hba;
710
711 init_waitqueue_head(&udev->wait_cmdr);
712 spin_lock_init(&udev->cmdr_lock);
713
714 idr_init(&udev->commands);
715 spin_lock_init(&udev->commands_lock);
716
717 setup_timer(&udev->timeout, tcmu_device_timedout,
718 (unsigned long)udev);
719
Andy Grover7c9e7a62014-10-01 16:07:05 -0700720 return &udev->se_dev;
721}
722
723static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on)
724{
725 struct tcmu_dev *tcmu_dev = container_of(info, struct tcmu_dev, uio_info);
726
727 tcmu_handle_completions(tcmu_dev);
728
729 return 0;
730}
731
732/*
733 * mmap code from uio.c. Copied here because we want to hook mmap()
734 * and this stuff must come along.
735 */
736static int tcmu_find_mem_index(struct vm_area_struct *vma)
737{
738 struct tcmu_dev *udev = vma->vm_private_data;
739 struct uio_info *info = &udev->uio_info;
740
741 if (vma->vm_pgoff < MAX_UIO_MAPS) {
742 if (info->mem[vma->vm_pgoff].size == 0)
743 return -1;
744 return (int)vma->vm_pgoff;
745 }
746 return -1;
747}
748
749static int tcmu_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
750{
751 struct tcmu_dev *udev = vma->vm_private_data;
752 struct uio_info *info = &udev->uio_info;
753 struct page *page;
754 unsigned long offset;
755 void *addr;
756
757 int mi = tcmu_find_mem_index(vma);
758 if (mi < 0)
759 return VM_FAULT_SIGBUS;
760
761 /*
762 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
763 * to use mem[N].
764 */
765 offset = (vmf->pgoff - mi) << PAGE_SHIFT;
766
767 addr = (void *)(unsigned long)info->mem[mi].addr + offset;
768 if (info->mem[mi].memtype == UIO_MEM_LOGICAL)
769 page = virt_to_page(addr);
770 else
771 page = vmalloc_to_page(addr);
772 get_page(page);
773 vmf->page = page;
774 return 0;
775}
776
777static const struct vm_operations_struct tcmu_vm_ops = {
778 .fault = tcmu_vma_fault,
779};
780
781static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma)
782{
783 struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
784
785 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
786 vma->vm_ops = &tcmu_vm_ops;
787
788 vma->vm_private_data = udev;
789
790 /* Ensure the mmap is exactly the right size */
791 if (vma_pages(vma) != (TCMU_RING_SIZE >> PAGE_SHIFT))
792 return -EINVAL;
793
794 return 0;
795}
796
797static int tcmu_open(struct uio_info *info, struct inode *inode)
798{
799 struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
800
801 /* O_EXCL not supported for char devs, so fake it? */
802 if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags))
803 return -EBUSY;
804
805 pr_debug("open\n");
806
807 return 0;
808}
809
810static int tcmu_release(struct uio_info *info, struct inode *inode)
811{
812 struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
813
814 clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags);
815
816 pr_debug("close\n");
817
818 return 0;
819}
820
821static int tcmu_netlink_event(enum tcmu_genl_cmd cmd, const char *name, int minor)
822{
823 struct sk_buff *skb;
824 void *msg_header;
Nicholas Bellinger6e14eab2014-10-01 23:01:15 -0700825 int ret = -ENOMEM;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700826
827 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
828 if (!skb)
Nicholas Bellinger6e14eab2014-10-01 23:01:15 -0700829 return ret;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700830
831 msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd);
Nicholas Bellinger6e14eab2014-10-01 23:01:15 -0700832 if (!msg_header)
833 goto free_skb;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700834
835 ret = nla_put_string(skb, TCMU_ATTR_DEVICE, name);
Nicholas Bellinger6e14eab2014-10-01 23:01:15 -0700836 if (ret < 0)
837 goto free_skb;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700838
839 ret = nla_put_u32(skb, TCMU_ATTR_MINOR, minor);
Nicholas Bellinger6e14eab2014-10-01 23:01:15 -0700840 if (ret < 0)
841 goto free_skb;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700842
Johannes Berg053c0952015-01-16 22:09:00 +0100843 genlmsg_end(skb, msg_header);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700844
845 ret = genlmsg_multicast(&tcmu_genl_family, skb, 0,
846 TCMU_MCGRP_CONFIG, GFP_KERNEL);
847
848 /* We don't care if no one is listening */
849 if (ret == -ESRCH)
850 ret = 0;
851
852 return ret;
Nicholas Bellinger6e14eab2014-10-01 23:01:15 -0700853free_skb:
854 nlmsg_free(skb);
855 return ret;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700856}
857
858static int tcmu_configure_device(struct se_device *dev)
859{
860 struct tcmu_dev *udev = TCMU_DEV(dev);
861 struct tcmu_hba *hba = udev->hba->hba_ptr;
862 struct uio_info *info;
863 struct tcmu_mailbox *mb;
864 size_t size;
865 size_t used;
866 int ret = 0;
867 char *str;
868
869 info = &udev->uio_info;
870
871 size = snprintf(NULL, 0, "tcm-user/%u/%s/%s", hba->host_id, udev->name,
872 udev->dev_config);
873 size += 1; /* for \0 */
874 str = kmalloc(size, GFP_KERNEL);
875 if (!str)
876 return -ENOMEM;
877
878 used = snprintf(str, size, "tcm-user/%u/%s", hba->host_id, udev->name);
879
880 if (udev->dev_config[0])
881 snprintf(str + used, size - used, "/%s", udev->dev_config);
882
883 info->name = str;
884
885 udev->mb_addr = vzalloc(TCMU_RING_SIZE);
886 if (!udev->mb_addr) {
887 ret = -ENOMEM;
888 goto err_vzalloc;
889 }
890
891 /* mailbox fits in first part of CMDR space */
892 udev->cmdr_size = CMDR_SIZE - CMDR_OFF;
893 udev->data_off = CMDR_SIZE;
894 udev->data_size = TCMU_RING_SIZE - CMDR_SIZE;
895
896 mb = udev->mb_addr;
Andy Grover0ad46af2015-04-14 17:30:04 -0700897 mb->version = TCMU_MAILBOX_VERSION;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700898 mb->cmdr_off = CMDR_OFF;
899 mb->cmdr_size = udev->cmdr_size;
900
901 WARN_ON(!PAGE_ALIGNED(udev->data_off));
902 WARN_ON(udev->data_size % PAGE_SIZE);
903
Andy Grover0ad46af2015-04-14 17:30:04 -0700904 info->version = xstr(TCMU_MAILBOX_VERSION);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700905
906 info->mem[0].name = "tcm-user command & data buffer";
907 info->mem[0].addr = (phys_addr_t) udev->mb_addr;
908 info->mem[0].size = TCMU_RING_SIZE;
909 info->mem[0].memtype = UIO_MEM_VIRTUAL;
910
911 info->irqcontrol = tcmu_irqcontrol;
912 info->irq = UIO_IRQ_CUSTOM;
913
914 info->mmap = tcmu_mmap;
915 info->open = tcmu_open;
916 info->release = tcmu_release;
917
918 ret = uio_register_device(tcmu_root_device, info);
919 if (ret)
920 goto err_register;
921
922 /* Other attributes can be configured in userspace */
923 dev->dev_attrib.hw_block_size = 512;
924 dev->dev_attrib.hw_max_sectors = 128;
925 dev->dev_attrib.hw_queue_depth = 128;
926
927 ret = tcmu_netlink_event(TCMU_CMD_ADDED_DEVICE, udev->uio_info.name,
928 udev->uio_info.uio_dev->minor);
929 if (ret)
930 goto err_netlink;
931
932 return 0;
933
934err_netlink:
935 uio_unregister_device(&udev->uio_info);
936err_register:
937 vfree(udev->mb_addr);
938err_vzalloc:
939 kfree(info->name);
940
941 return ret;
942}
943
944static int tcmu_check_pending_cmd(int id, void *p, void *data)
945{
946 struct tcmu_cmd *cmd = p;
947
948 if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
949 return 0;
950 return -EINVAL;
951}
952
Nicholas Bellinger4cc987e2015-05-19 00:03:07 -0700953static void tcmu_dev_call_rcu(struct rcu_head *p)
954{
955 struct se_device *dev = container_of(p, struct se_device, rcu_head);
956 struct tcmu_dev *udev = TCMU_DEV(dev);
957
958 kfree(udev);
959}
960
Andy Grover7c9e7a62014-10-01 16:07:05 -0700961static void tcmu_free_device(struct se_device *dev)
962{
963 struct tcmu_dev *udev = TCMU_DEV(dev);
964 int i;
965
966 del_timer_sync(&udev->timeout);
967
968 vfree(udev->mb_addr);
969
970 /* Upper layer should drain all requests before calling this */
971 spin_lock_irq(&udev->commands_lock);
972 i = idr_for_each(&udev->commands, tcmu_check_pending_cmd, NULL);
973 idr_destroy(&udev->commands);
974 spin_unlock_irq(&udev->commands_lock);
975 WARN_ON(i);
976
977 /* Device was configured */
978 if (udev->uio_info.uio_dev) {
979 tcmu_netlink_event(TCMU_CMD_REMOVED_DEVICE, udev->uio_info.name,
980 udev->uio_info.uio_dev->minor);
981
982 uio_unregister_device(&udev->uio_info);
983 kfree(udev->uio_info.name);
984 kfree(udev->name);
985 }
Nicholas Bellinger4cc987e2015-05-19 00:03:07 -0700986 call_rcu(&dev->rcu_head, tcmu_dev_call_rcu);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700987}
988
989enum {
Andy Grover9c1cd1b2015-05-19 14:44:39 -0700990 Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_err,
Andy Grover7c9e7a62014-10-01 16:07:05 -0700991};
992
993static match_table_t tokens = {
994 {Opt_dev_config, "dev_config=%s"},
995 {Opt_dev_size, "dev_size=%u"},
Andy Grover9c1cd1b2015-05-19 14:44:39 -0700996 {Opt_hw_block_size, "hw_block_size=%u"},
Andy Grover7c9e7a62014-10-01 16:07:05 -0700997 {Opt_err, NULL}
998};
999
1000static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev,
1001 const char *page, ssize_t count)
1002{
1003 struct tcmu_dev *udev = TCMU_DEV(dev);
1004 char *orig, *ptr, *opts, *arg_p;
1005 substring_t args[MAX_OPT_ARGS];
1006 int ret = 0, token;
Andy Grover9c1cd1b2015-05-19 14:44:39 -07001007 unsigned long tmp_ul;
Andy Grover7c9e7a62014-10-01 16:07:05 -07001008
1009 opts = kstrdup(page, GFP_KERNEL);
1010 if (!opts)
1011 return -ENOMEM;
1012
1013 orig = opts;
1014
1015 while ((ptr = strsep(&opts, ",\n")) != NULL) {
1016 if (!*ptr)
1017 continue;
1018
1019 token = match_token(ptr, tokens, args);
1020 switch (token) {
1021 case Opt_dev_config:
1022 if (match_strlcpy(udev->dev_config, &args[0],
1023 TCMU_CONFIG_LEN) == 0) {
1024 ret = -EINVAL;
1025 break;
1026 }
1027 pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config);
1028 break;
1029 case Opt_dev_size:
1030 arg_p = match_strdup(&args[0]);
1031 if (!arg_p) {
1032 ret = -ENOMEM;
1033 break;
1034 }
1035 ret = kstrtoul(arg_p, 0, (unsigned long *) &udev->dev_size);
1036 kfree(arg_p);
1037 if (ret < 0)
1038 pr_err("kstrtoul() failed for dev_size=\n");
1039 break;
Andy Grover9c1cd1b2015-05-19 14:44:39 -07001040 case Opt_hw_block_size:
1041 arg_p = match_strdup(&args[0]);
1042 if (!arg_p) {
1043 ret = -ENOMEM;
1044 break;
1045 }
1046 ret = kstrtoul(arg_p, 0, &tmp_ul);
1047 kfree(arg_p);
1048 if (ret < 0) {
1049 pr_err("kstrtoul() failed for hw_block_size=\n");
1050 break;
1051 }
1052 if (!tmp_ul) {
1053 pr_err("hw_block_size must be nonzero\n");
1054 break;
1055 }
1056 dev->dev_attrib.hw_block_size = tmp_ul;
1057 break;
Andy Grover7c9e7a62014-10-01 16:07:05 -07001058 default:
1059 break;
1060 }
1061 }
1062
1063 kfree(orig);
1064 return (!ret) ? count : ret;
1065}
1066
1067static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b)
1068{
1069 struct tcmu_dev *udev = TCMU_DEV(dev);
1070 ssize_t bl = 0;
1071
1072 bl = sprintf(b + bl, "Config: %s ",
1073 udev->dev_config[0] ? udev->dev_config : "NULL");
Andy Grover8ee83a72015-05-01 10:43:45 -07001074 bl += sprintf(b + bl, "Size: %zu\n", udev->dev_size);
Andy Grover7c9e7a62014-10-01 16:07:05 -07001075
1076 return bl;
1077}
1078
1079static sector_t tcmu_get_blocks(struct se_device *dev)
1080{
1081 struct tcmu_dev *udev = TCMU_DEV(dev);
1082
1083 return div_u64(udev->dev_size - dev->dev_attrib.block_size,
1084 dev->dev_attrib.block_size);
1085}
1086
1087static sense_reason_t
Andy Grover7c9e7a62014-10-01 16:07:05 -07001088tcmu_pass_op(struct se_cmd *se_cmd)
1089{
1090 int ret = tcmu_queue_cmd(se_cmd);
1091
1092 if (ret != 0)
1093 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1094 else
1095 return TCM_NO_SENSE;
1096}
1097
Andy Grover7c9e7a62014-10-01 16:07:05 -07001098static sense_reason_t
1099tcmu_parse_cdb(struct se_cmd *cmd)
1100{
Andy Grover7bfea53b2015-05-19 14:44:40 -07001101 return passthrough_parse_cdb(cmd, tcmu_pass_op);
Andy Grover7c9e7a62014-10-01 16:07:05 -07001102}
1103
Christoph Hellwig0a06d432015-05-10 18:14:56 +02001104static const struct target_backend_ops tcmu_ops = {
Andy Grover7c9e7a62014-10-01 16:07:05 -07001105 .name = "user",
1106 .inquiry_prod = "USER",
1107 .inquiry_rev = TCMU_VERSION,
1108 .owner = THIS_MODULE,
Andy Grovera3541702015-05-19 14:44:41 -07001109 .transport_flags = TRANSPORT_FLAG_PASSTHROUGH,
Andy Grover7c9e7a62014-10-01 16:07:05 -07001110 .attach_hba = tcmu_attach_hba,
1111 .detach_hba = tcmu_detach_hba,
1112 .alloc_device = tcmu_alloc_device,
1113 .configure_device = tcmu_configure_device,
1114 .free_device = tcmu_free_device,
1115 .parse_cdb = tcmu_parse_cdb,
1116 .set_configfs_dev_params = tcmu_set_configfs_dev_params,
1117 .show_configfs_dev_params = tcmu_show_configfs_dev_params,
1118 .get_device_type = sbc_get_device_type,
1119 .get_blocks = tcmu_get_blocks,
Christoph Hellwig5873c4d2015-05-10 18:14:57 +02001120 .tb_dev_attrib_attrs = passthrough_attrib_attrs,
Andy Grover7c9e7a62014-10-01 16:07:05 -07001121};
1122
1123static int __init tcmu_module_init(void)
1124{
1125 int ret;
1126
1127 BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0);
1128
1129 tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache",
1130 sizeof(struct tcmu_cmd),
1131 __alignof__(struct tcmu_cmd),
1132 0, NULL);
1133 if (!tcmu_cmd_cache)
1134 return -ENOMEM;
1135
1136 tcmu_root_device = root_device_register("tcm_user");
1137 if (IS_ERR(tcmu_root_device)) {
1138 ret = PTR_ERR(tcmu_root_device);
1139 goto out_free_cache;
1140 }
1141
1142 ret = genl_register_family(&tcmu_genl_family);
1143 if (ret < 0) {
1144 goto out_unreg_device;
1145 }
1146
Christoph Hellwig0a06d432015-05-10 18:14:56 +02001147 ret = transport_backend_register(&tcmu_ops);
Andy Grover7c9e7a62014-10-01 16:07:05 -07001148 if (ret)
1149 goto out_unreg_genl;
1150
1151 return 0;
1152
1153out_unreg_genl:
1154 genl_unregister_family(&tcmu_genl_family);
1155out_unreg_device:
1156 root_device_unregister(tcmu_root_device);
1157out_free_cache:
1158 kmem_cache_destroy(tcmu_cmd_cache);
1159
1160 return ret;
1161}
1162
1163static void __exit tcmu_module_exit(void)
1164{
Christoph Hellwig0a06d432015-05-10 18:14:56 +02001165 target_backend_unregister(&tcmu_ops);
Andy Grover7c9e7a62014-10-01 16:07:05 -07001166 genl_unregister_family(&tcmu_genl_family);
1167 root_device_unregister(tcmu_root_device);
1168 kmem_cache_destroy(tcmu_cmd_cache);
1169}
1170
1171MODULE_DESCRIPTION("TCM USER subsystem plugin");
1172MODULE_AUTHOR("Shaohua Li <shli@kernel.org>");
1173MODULE_AUTHOR("Andy Grover <agrover@redhat.com>");
1174MODULE_LICENSE("GPL");
1175
1176module_init(tcmu_module_init);
1177module_exit(tcmu_module_exit);