blob: 2b0d26e268bed26bebc2b4c66f629517b79f5332 [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#include <target/target_core_backend_configfs.h>
33
Andy Grover7c9e7a62014-10-01 16:07:05 -070034#include <linux/target_core_user.h>
35
36/*
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.
40 *
41 * It uses the UIO framework to do a lot of the device-creation and
42 * introspection work for us.
43 *
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.
51 *
52 * SECURITY:
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.
59 */
60
61
62#define TCMU_TIME_OUT (30 * MSEC_PER_SEC)
63
64#define CMDR_SIZE (16 * 4096)
65#define DATA_SIZE (257 * 4096)
66
67#define TCMU_RING_SIZE (CMDR_SIZE + DATA_SIZE)
68
69static struct device *tcmu_root_device;
70
71struct tcmu_hba {
72 u32 host_id;
73};
74
Andy Grover7c9e7a62014-10-01 16:07:05 -070075#define TCMU_CONFIG_LEN 256
76
77struct tcmu_dev {
78 struct se_device se_dev;
79
80 char *name;
81 struct se_hba *hba;
82
83#define TCMU_DEV_BIT_OPEN 0
84#define TCMU_DEV_BIT_BROKEN 1
85 unsigned long flags;
Andy Grover7c9e7a62014-10-01 16:07:05 -070086
87 struct uio_info uio_info;
88
89 struct tcmu_mailbox *mb_addr;
90 size_t dev_size;
91 u32 cmdr_size;
92 u32 cmdr_last_cleaned;
93 /* Offset of data ring from start of mb */
94 size_t data_off;
95 size_t data_size;
96 /* Ring head + tail values. */
97 /* Must add data_off and mb_addr to get the address */
98 size_t data_head;
99 size_t data_tail;
100
101 wait_queue_head_t wait_cmdr;
102 /* TODO should this be a mutex? */
103 spinlock_t cmdr_lock;
104
105 struct idr commands;
106 spinlock_t commands_lock;
107
108 struct timer_list timeout;
109
110 char dev_config[TCMU_CONFIG_LEN];
111};
112
113#define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev)
114
115#define CMDR_OFF sizeof(struct tcmu_mailbox)
116
117struct tcmu_cmd {
118 struct se_cmd *se_cmd;
119 struct tcmu_dev *tcmu_dev;
120
121 uint16_t cmd_id;
122
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 */
125 size_t data_length;
126
127 unsigned long deadline;
128
129#define TCMU_CMD_BIT_EXPIRED 0
130 unsigned long flags;
131};
132
133static struct kmem_cache *tcmu_cmd_cache;
134
135/* multicast group */
136enum tcmu_multicast_groups {
137 TCMU_MCGRP_CONFIG,
138};
139
140static const struct genl_multicast_group tcmu_mcgrps[] = {
141 [TCMU_MCGRP_CONFIG] = { .name = "config", },
142};
143
144/* Our generic netlink family */
145static struct genl_family tcmu_genl_family = {
146 .id = GENL_ID_GENERATE,
147 .hdrsize = 0,
148 .name = "TCM-USER",
149 .version = 1,
150 .maxattr = TCMU_ATTR_MAX,
151 .mcgrps = tcmu_mcgrps,
152 .n_mcgrps = ARRAY_SIZE(tcmu_mcgrps),
153};
154
155static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd)
156{
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;
160 int cmd_id;
161
162 tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_KERNEL);
163 if (!tcmu_cmd)
164 return NULL;
165
166 tcmu_cmd->se_cmd = se_cmd;
167 tcmu_cmd->tcmu_dev = udev;
168 tcmu_cmd->data_length = se_cmd->data_length;
169
170 tcmu_cmd->deadline = jiffies + msecs_to_jiffies(TCMU_TIME_OUT);
171
172 idr_preload(GFP_KERNEL);
173 spin_lock_irq(&udev->commands_lock);
174 cmd_id = idr_alloc(&udev->commands, tcmu_cmd, 0,
175 USHRT_MAX, GFP_NOWAIT);
176 spin_unlock_irq(&udev->commands_lock);
177 idr_preload_end();
178
179 if (cmd_id < 0) {
180 kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
181 return NULL;
182 }
183 tcmu_cmd->cmd_id = cmd_id;
184
185 return tcmu_cmd;
186}
187
188static inline void tcmu_flush_dcache_range(void *vaddr, size_t size)
189{
190 unsigned long offset = (unsigned long) vaddr & ~PAGE_MASK;
191
192 size = round_up(size+offset, PAGE_SIZE);
193 vaddr -= offset;
194
195 while (size) {
196 flush_dcache_page(virt_to_page(vaddr));
197 size -= PAGE_SIZE;
198 }
199}
200
201/*
202 * Some ring helper functions. We don't assume size is a power of 2 so
203 * we can't use circ_buf.h.
204 */
205static inline size_t spc_used(size_t head, size_t tail, size_t size)
206{
207 int diff = head - tail;
208
209 if (diff >= 0)
210 return diff;
211 else
212 return size + diff;
213}
214
215static inline size_t spc_free(size_t head, size_t tail, size_t size)
216{
217 /* Keep 1 byte unused or we can't tell full from empty */
218 return (size - spc_used(head, tail, size) - 1);
219}
220
221static inline size_t head_to_end(size_t head, size_t size)
222{
223 return size - head;
224}
225
226#define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size)
227
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300228static void alloc_and_scatter_data_area(struct tcmu_dev *udev,
229 struct scatterlist *data_sg, unsigned int data_nents,
230 struct iovec **iov, int *iov_cnt, bool copy_data)
231{
232 int i;
233 void *from, *to;
234 size_t copy_bytes;
235 struct scatterlist *sg;
236
237 for_each_sg(data_sg, sg, data_nents, i) {
238 copy_bytes = min_t(size_t, sg->length,
239 head_to_end(udev->data_head, udev->data_size));
240 from = kmap_atomic(sg_page(sg)) + sg->offset;
241 to = (void *) udev->mb_addr + udev->data_off + udev->data_head;
242
243 if (copy_data) {
244 memcpy(to, from, copy_bytes);
245 tcmu_flush_dcache_range(to, copy_bytes);
246 }
247
248 /* Even iov_base is relative to mb_addr */
249 (*iov)->iov_len = copy_bytes;
250 (*iov)->iov_base = (void __user *) udev->data_off +
251 udev->data_head;
252 (*iov_cnt)++;
253 (*iov)++;
254
255 UPDATE_HEAD(udev->data_head, copy_bytes, udev->data_size);
256
257 /* Uh oh, we wrapped the buffer. Must split sg across 2 iovs. */
258 if (sg->length != copy_bytes) {
259 from += copy_bytes;
260 copy_bytes = sg->length - copy_bytes;
261
262 (*iov)->iov_len = copy_bytes;
263 (*iov)->iov_base = (void __user *) udev->data_off +
264 udev->data_head;
265
266 if (copy_data) {
267 to = (void *) udev->mb_addr +
268 udev->data_off + udev->data_head;
269 memcpy(to, from, copy_bytes);
270 tcmu_flush_dcache_range(to, copy_bytes);
271 }
272
273 (*iov_cnt)++;
274 (*iov)++;
275
276 UPDATE_HEAD(udev->data_head,
277 copy_bytes, udev->data_size);
278 }
279
280 kunmap_atomic(from);
281 }
282}
283
284static void gather_and_free_data_area(struct tcmu_dev *udev,
285 struct scatterlist *data_sg, unsigned int data_nents)
286{
287 int i;
288 void *from, *to;
289 size_t copy_bytes;
290 struct scatterlist *sg;
291
292 /* It'd be easier to look at entry's iovec again, but UAM */
293 for_each_sg(data_sg, sg, data_nents, i) {
294 copy_bytes = min_t(size_t, sg->length,
295 head_to_end(udev->data_tail, udev->data_size));
296
297 to = kmap_atomic(sg_page(sg)) + sg->offset;
298 WARN_ON(sg->length + sg->offset > PAGE_SIZE);
299 from = (void *) udev->mb_addr +
300 udev->data_off + udev->data_tail;
301 tcmu_flush_dcache_range(from, copy_bytes);
302 memcpy(to, from, copy_bytes);
303
304 UPDATE_HEAD(udev->data_tail, copy_bytes, udev->data_size);
305
306 /* Uh oh, wrapped the data buffer for this sg's data */
307 if (sg->length != copy_bytes) {
308 from = (void *) udev->mb_addr +
309 udev->data_off + udev->data_tail;
310 WARN_ON(udev->data_tail);
311 to += copy_bytes;
312 copy_bytes = sg->length - copy_bytes;
313 tcmu_flush_dcache_range(from, copy_bytes);
314 memcpy(to, from, copy_bytes);
315
316 UPDATE_HEAD(udev->data_tail,
317 copy_bytes, udev->data_size);
318 }
319 kunmap_atomic(to);
320 }
321}
322
Andy Grover7c9e7a62014-10-01 16:07:05 -0700323/*
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300324 * We can't queue a command until we have space available on the cmd ring *and*
325 * space available on the data ring.
Andy Grover7c9e7a62014-10-01 16:07:05 -0700326 *
327 * Called with ring lock held.
328 */
Andy Groverf56574a2014-10-02 10:23:15 -0700329static bool is_ring_space_avail(struct tcmu_dev *udev, size_t cmd_size, size_t data_needed)
Andy Grover7c9e7a62014-10-01 16:07:05 -0700330{
331 struct tcmu_mailbox *mb = udev->mb_addr;
332 size_t space;
333 u32 cmd_head;
Andy Groverf56574a2014-10-02 10:23:15 -0700334 size_t cmd_needed;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700335
336 tcmu_flush_dcache_range(mb, sizeof(*mb));
337
338 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
339
Andy Groverf56574a2014-10-02 10:23:15 -0700340 /*
341 * If cmd end-of-ring space is too small then we need space for a NOP plus
342 * original cmd - cmds are internally contiguous.
343 */
344 if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size)
345 cmd_needed = cmd_size;
346 else
347 cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size);
348
Andy Grover7c9e7a62014-10-01 16:07:05 -0700349 space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size);
350 if (space < cmd_needed) {
351 pr_debug("no cmd space: %u %u %u\n", cmd_head,
352 udev->cmdr_last_cleaned, udev->cmdr_size);
353 return false;
354 }
355
356 space = spc_free(udev->data_head, udev->data_tail, udev->data_size);
357 if (space < data_needed) {
358 pr_debug("no data space: %zu %zu %zu\n", udev->data_head,
359 udev->data_tail, udev->data_size);
360 return false;
361 }
362
363 return true;
364}
365
366static int tcmu_queue_cmd_ring(struct tcmu_cmd *tcmu_cmd)
367{
368 struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
369 struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
370 size_t base_command_size, command_size;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700371 struct tcmu_mailbox *mb;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700372 struct tcmu_cmd_entry *entry;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700373 struct iovec *iov;
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300374 int iov_cnt;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700375 uint32_t cmd_head;
376 uint64_t cdb_off;
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300377 bool copy_to_data_area;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700378
379 if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags))
380 return -EINVAL;
381
382 /*
383 * Must be a certain minimum size for response sense info, but
384 * also may be larger if the iov array is large.
385 *
386 * iovs = sgl_nents+1, for end-of-ring case, plus another 1
387 * b/c size == offsetof one-past-element.
388 */
389 base_command_size = max(offsetof(struct tcmu_cmd_entry,
390 req.iov[se_cmd->t_data_nents + 2]),
391 sizeof(struct tcmu_cmd_entry));
392 command_size = base_command_size
393 + round_up(scsi_command_size(se_cmd->t_task_cdb), TCMU_OP_ALIGN_SIZE);
394
395 WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1));
396
397 spin_lock_irq(&udev->cmdr_lock);
398
399 mb = udev->mb_addr;
400 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
401 if ((command_size > (udev->cmdr_size / 2))
402 || tcmu_cmd->data_length > (udev->data_size - 1))
403 pr_warn("TCMU: Request of size %zu/%zu may be too big for %u/%zu "
404 "cmd/data ring buffers\n", command_size, tcmu_cmd->data_length,
405 udev->cmdr_size, udev->data_size);
406
Andy Groverf56574a2014-10-02 10:23:15 -0700407 while (!is_ring_space_avail(udev, command_size, tcmu_cmd->data_length)) {
Andy Grover7c9e7a62014-10-01 16:07:05 -0700408 int ret;
409 DEFINE_WAIT(__wait);
410
411 prepare_to_wait(&udev->wait_cmdr, &__wait, TASK_INTERRUPTIBLE);
412
413 pr_debug("sleeping for ring space\n");
414 spin_unlock_irq(&udev->cmdr_lock);
415 ret = schedule_timeout(msecs_to_jiffies(TCMU_TIME_OUT));
416 finish_wait(&udev->wait_cmdr, &__wait);
417 if (!ret) {
418 pr_warn("tcmu: command timed out\n");
419 return -ETIMEDOUT;
420 }
421
422 spin_lock_irq(&udev->cmdr_lock);
423
424 /* We dropped cmdr_lock, cmd_head is stale */
425 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
426 }
427
Andy Groverf56574a2014-10-02 10:23:15 -0700428 /* Insert a PAD if end-of-ring space is too small */
429 if (head_to_end(cmd_head, udev->cmdr_size) < command_size) {
430 size_t pad_size = head_to_end(cmd_head, udev->cmdr_size);
431
Andy Grover7c9e7a62014-10-01 16:07:05 -0700432 entry = (void *) mb + CMDR_OFF + cmd_head;
433 tcmu_flush_dcache_range(entry, sizeof(*entry));
Andy Grover0ad46af2015-04-14 17:30:04 -0700434 tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_PAD);
435 tcmu_hdr_set_len(&entry->hdr.len_op, pad_size);
436 entry->hdr.cmd_id = 0; /* not used for PAD */
437 entry->hdr.kflags = 0;
438 entry->hdr.uflags = 0;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700439
440 UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size);
441
442 cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
443 WARN_ON(cmd_head != 0);
444 }
445
446 entry = (void *) mb + CMDR_OFF + cmd_head;
447 tcmu_flush_dcache_range(entry, sizeof(*entry));
Andy Grover0ad46af2015-04-14 17:30:04 -0700448 tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD);
449 tcmu_hdr_set_len(&entry->hdr.len_op, command_size);
450 entry->hdr.cmd_id = tcmu_cmd->cmd_id;
451 entry->hdr.kflags = 0;
452 entry->hdr.uflags = 0;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700453
454 /*
455 * Fix up iovecs, and handle if allocation in data ring wrapped.
456 */
457 iov = &entry->req.iov[0];
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300458 iov_cnt = 0;
459 copy_to_data_area = (se_cmd->data_direction == DMA_TO_DEVICE);
460 alloc_and_scatter_data_area(udev, se_cmd->t_data_sg,
461 se_cmd->t_data_nents, &iov, &iov_cnt, copy_to_data_area);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700462 entry->req.iov_cnt = iov_cnt;
Andy Grover0ad46af2015-04-14 17:30:04 -0700463 entry->req.iov_bidi_cnt = 0;
464 entry->req.iov_dif_cnt = 0;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700465
466 /* All offsets relative to mb_addr, not start of entry! */
467 cdb_off = CMDR_OFF + cmd_head + base_command_size;
468 memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb));
469 entry->req.cdb_off = cdb_off;
470 tcmu_flush_dcache_range(entry, sizeof(*entry));
471
472 UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size);
473 tcmu_flush_dcache_range(mb, sizeof(*mb));
474
475 spin_unlock_irq(&udev->cmdr_lock);
476
477 /* TODO: only if FLUSH and FUA? */
478 uio_event_notify(&udev->uio_info);
479
480 mod_timer(&udev->timeout,
481 round_jiffies_up(jiffies + msecs_to_jiffies(TCMU_TIME_OUT)));
482
483 return 0;
484}
485
486static int tcmu_queue_cmd(struct se_cmd *se_cmd)
487{
488 struct se_device *se_dev = se_cmd->se_dev;
489 struct tcmu_dev *udev = TCMU_DEV(se_dev);
490 struct tcmu_cmd *tcmu_cmd;
491 int ret;
492
493 tcmu_cmd = tcmu_alloc_cmd(se_cmd);
494 if (!tcmu_cmd)
495 return -ENOMEM;
496
497 ret = tcmu_queue_cmd_ring(tcmu_cmd);
498 if (ret < 0) {
499 pr_err("TCMU: Could not queue command\n");
500 spin_lock_irq(&udev->commands_lock);
501 idr_remove(&udev->commands, tcmu_cmd->cmd_id);
502 spin_unlock_irq(&udev->commands_lock);
503
504 kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
505 }
506
507 return ret;
508}
509
510static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *entry)
511{
512 struct se_cmd *se_cmd = cmd->se_cmd;
513 struct tcmu_dev *udev = cmd->tcmu_dev;
514
515 if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
516 /* cmd has been completed already from timeout, just reclaim data
517 ring space */
518 UPDATE_HEAD(udev->data_tail, cmd->data_length, udev->data_size);
519 return;
520 }
521
Andy Grover0ad46af2015-04-14 17:30:04 -0700522 if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) {
523 UPDATE_HEAD(udev->data_tail, cmd->data_length, udev->data_size);
524 pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n",
525 cmd->se_cmd);
526 transport_generic_request_failure(cmd->se_cmd,
527 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE);
528 cmd->se_cmd = NULL;
529 kmem_cache_free(tcmu_cmd_cache, cmd);
530 return;
531 }
532
Andy Grover7c9e7a62014-10-01 16:07:05 -0700533 if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) {
534 memcpy(se_cmd->sense_buffer, entry->rsp.sense_buffer,
535 se_cmd->scsi_sense_length);
536
537 UPDATE_HEAD(udev->data_tail, cmd->data_length, udev->data_size);
538 }
539 else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
Ilias Tsitsimpisf97ec7d2015-04-23 21:47:00 +0300540 gather_and_free_data_area(udev,
541 se_cmd->t_data_sg, se_cmd->t_data_nents);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700542 } else if (se_cmd->data_direction == DMA_TO_DEVICE) {
543 UPDATE_HEAD(udev->data_tail, cmd->data_length, udev->data_size);
Ilias Tsitsimpis2bc396a2015-04-23 21:30:05 +0300544 } else if (se_cmd->data_direction != DMA_NONE) {
545 pr_warn("TCMU: data direction was %d!\n",
546 se_cmd->data_direction);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700547 }
548
549 target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status);
550 cmd->se_cmd = NULL;
551
552 kmem_cache_free(tcmu_cmd_cache, cmd);
553}
554
555static unsigned int tcmu_handle_completions(struct tcmu_dev *udev)
556{
557 struct tcmu_mailbox *mb;
558 LIST_HEAD(cpl_cmds);
559 unsigned long flags;
560 int handled = 0;
561
562 if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) {
563 pr_err("ring broken, not handling completions\n");
564 return 0;
565 }
566
567 spin_lock_irqsave(&udev->cmdr_lock, flags);
568
569 mb = udev->mb_addr;
570 tcmu_flush_dcache_range(mb, sizeof(*mb));
571
572 while (udev->cmdr_last_cleaned != ACCESS_ONCE(mb->cmd_tail)) {
573
574 struct tcmu_cmd_entry *entry = (void *) mb + CMDR_OFF + udev->cmdr_last_cleaned;
575 struct tcmu_cmd *cmd;
576
577 tcmu_flush_dcache_range(entry, sizeof(*entry));
578
Andy Grover0ad46af2015-04-14 17:30:04 -0700579 if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD) {
580 UPDATE_HEAD(udev->cmdr_last_cleaned,
581 tcmu_hdr_get_len(entry->hdr.len_op),
582 udev->cmdr_size);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700583 continue;
584 }
Andy Grover0ad46af2015-04-14 17:30:04 -0700585 WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700586
587 spin_lock(&udev->commands_lock);
Andy Grover0ad46af2015-04-14 17:30:04 -0700588 cmd = idr_find(&udev->commands, entry->hdr.cmd_id);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700589 if (cmd)
590 idr_remove(&udev->commands, cmd->cmd_id);
591 spin_unlock(&udev->commands_lock);
592
593 if (!cmd) {
594 pr_err("cmd_id not found, ring is broken\n");
595 set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags);
596 break;
597 }
598
599 tcmu_handle_completion(cmd, entry);
600
Andy Grover0ad46af2015-04-14 17:30:04 -0700601 UPDATE_HEAD(udev->cmdr_last_cleaned,
602 tcmu_hdr_get_len(entry->hdr.len_op),
603 udev->cmdr_size);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700604
605 handled++;
606 }
607
608 if (mb->cmd_tail == mb->cmd_head)
609 del_timer(&udev->timeout); /* no more pending cmds */
610
611 spin_unlock_irqrestore(&udev->cmdr_lock, flags);
612
613 wake_up(&udev->wait_cmdr);
614
615 return handled;
616}
617
618static int tcmu_check_expired_cmd(int id, void *p, void *data)
619{
620 struct tcmu_cmd *cmd = p;
621
622 if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
623 return 0;
624
625 if (!time_after(cmd->deadline, jiffies))
626 return 0;
627
628 set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags);
629 target_complete_cmd(cmd->se_cmd, SAM_STAT_CHECK_CONDITION);
630 cmd->se_cmd = NULL;
631
632 kmem_cache_free(tcmu_cmd_cache, cmd);
633
634 return 0;
635}
636
637static void tcmu_device_timedout(unsigned long data)
638{
639 struct tcmu_dev *udev = (struct tcmu_dev *)data;
640 unsigned long flags;
641 int handled;
642
643 handled = tcmu_handle_completions(udev);
644
645 pr_warn("%d completions handled from timeout\n", handled);
646
647 spin_lock_irqsave(&udev->commands_lock, flags);
648 idr_for_each(&udev->commands, tcmu_check_expired_cmd, NULL);
649 spin_unlock_irqrestore(&udev->commands_lock, flags);
650
651 /*
652 * We don't need to wakeup threads on wait_cmdr since they have their
653 * own timeout.
654 */
655}
656
657static int tcmu_attach_hba(struct se_hba *hba, u32 host_id)
658{
659 struct tcmu_hba *tcmu_hba;
660
661 tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL);
662 if (!tcmu_hba)
663 return -ENOMEM;
664
665 tcmu_hba->host_id = host_id;
666 hba->hba_ptr = tcmu_hba;
667
668 return 0;
669}
670
671static void tcmu_detach_hba(struct se_hba *hba)
672{
673 kfree(hba->hba_ptr);
674 hba->hba_ptr = NULL;
675}
676
677static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
678{
679 struct tcmu_dev *udev;
680
681 udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL);
682 if (!udev)
683 return NULL;
684
685 udev->name = kstrdup(name, GFP_KERNEL);
686 if (!udev->name) {
687 kfree(udev);
688 return NULL;
689 }
690
691 udev->hba = hba;
692
693 init_waitqueue_head(&udev->wait_cmdr);
694 spin_lock_init(&udev->cmdr_lock);
695
696 idr_init(&udev->commands);
697 spin_lock_init(&udev->commands_lock);
698
699 setup_timer(&udev->timeout, tcmu_device_timedout,
700 (unsigned long)udev);
701
Andy Grover7c9e7a62014-10-01 16:07:05 -0700702 return &udev->se_dev;
703}
704
705static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on)
706{
707 struct tcmu_dev *tcmu_dev = container_of(info, struct tcmu_dev, uio_info);
708
709 tcmu_handle_completions(tcmu_dev);
710
711 return 0;
712}
713
714/*
715 * mmap code from uio.c. Copied here because we want to hook mmap()
716 * and this stuff must come along.
717 */
718static int tcmu_find_mem_index(struct vm_area_struct *vma)
719{
720 struct tcmu_dev *udev = vma->vm_private_data;
721 struct uio_info *info = &udev->uio_info;
722
723 if (vma->vm_pgoff < MAX_UIO_MAPS) {
724 if (info->mem[vma->vm_pgoff].size == 0)
725 return -1;
726 return (int)vma->vm_pgoff;
727 }
728 return -1;
729}
730
731static int tcmu_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
732{
733 struct tcmu_dev *udev = vma->vm_private_data;
734 struct uio_info *info = &udev->uio_info;
735 struct page *page;
736 unsigned long offset;
737 void *addr;
738
739 int mi = tcmu_find_mem_index(vma);
740 if (mi < 0)
741 return VM_FAULT_SIGBUS;
742
743 /*
744 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
745 * to use mem[N].
746 */
747 offset = (vmf->pgoff - mi) << PAGE_SHIFT;
748
749 addr = (void *)(unsigned long)info->mem[mi].addr + offset;
750 if (info->mem[mi].memtype == UIO_MEM_LOGICAL)
751 page = virt_to_page(addr);
752 else
753 page = vmalloc_to_page(addr);
754 get_page(page);
755 vmf->page = page;
756 return 0;
757}
758
759static const struct vm_operations_struct tcmu_vm_ops = {
760 .fault = tcmu_vma_fault,
761};
762
763static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma)
764{
765 struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
766
767 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
768 vma->vm_ops = &tcmu_vm_ops;
769
770 vma->vm_private_data = udev;
771
772 /* Ensure the mmap is exactly the right size */
773 if (vma_pages(vma) != (TCMU_RING_SIZE >> PAGE_SHIFT))
774 return -EINVAL;
775
776 return 0;
777}
778
779static int tcmu_open(struct uio_info *info, struct inode *inode)
780{
781 struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
782
783 /* O_EXCL not supported for char devs, so fake it? */
784 if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags))
785 return -EBUSY;
786
787 pr_debug("open\n");
788
789 return 0;
790}
791
792static int tcmu_release(struct uio_info *info, struct inode *inode)
793{
794 struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
795
796 clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags);
797
798 pr_debug("close\n");
799
800 return 0;
801}
802
803static int tcmu_netlink_event(enum tcmu_genl_cmd cmd, const char *name, int minor)
804{
805 struct sk_buff *skb;
806 void *msg_header;
Nicholas Bellinger6e14eab2014-10-01 23:01:15 -0700807 int ret = -ENOMEM;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700808
809 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
810 if (!skb)
Nicholas Bellinger6e14eab2014-10-01 23:01:15 -0700811 return ret;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700812
813 msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd);
Nicholas Bellinger6e14eab2014-10-01 23:01:15 -0700814 if (!msg_header)
815 goto free_skb;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700816
817 ret = nla_put_string(skb, TCMU_ATTR_DEVICE, name);
Nicholas Bellinger6e14eab2014-10-01 23:01:15 -0700818 if (ret < 0)
819 goto free_skb;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700820
821 ret = nla_put_u32(skb, TCMU_ATTR_MINOR, minor);
Nicholas Bellinger6e14eab2014-10-01 23:01:15 -0700822 if (ret < 0)
823 goto free_skb;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700824
Johannes Berg053c0952015-01-16 22:09:00 +0100825 genlmsg_end(skb, msg_header);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700826
827 ret = genlmsg_multicast(&tcmu_genl_family, skb, 0,
828 TCMU_MCGRP_CONFIG, GFP_KERNEL);
829
830 /* We don't care if no one is listening */
831 if (ret == -ESRCH)
832 ret = 0;
833
834 return ret;
Nicholas Bellinger6e14eab2014-10-01 23:01:15 -0700835free_skb:
836 nlmsg_free(skb);
837 return ret;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700838}
839
840static int tcmu_configure_device(struct se_device *dev)
841{
842 struct tcmu_dev *udev = TCMU_DEV(dev);
843 struct tcmu_hba *hba = udev->hba->hba_ptr;
844 struct uio_info *info;
845 struct tcmu_mailbox *mb;
846 size_t size;
847 size_t used;
848 int ret = 0;
849 char *str;
850
851 info = &udev->uio_info;
852
853 size = snprintf(NULL, 0, "tcm-user/%u/%s/%s", hba->host_id, udev->name,
854 udev->dev_config);
855 size += 1; /* for \0 */
856 str = kmalloc(size, GFP_KERNEL);
857 if (!str)
858 return -ENOMEM;
859
860 used = snprintf(str, size, "tcm-user/%u/%s", hba->host_id, udev->name);
861
862 if (udev->dev_config[0])
863 snprintf(str + used, size - used, "/%s", udev->dev_config);
864
865 info->name = str;
866
867 udev->mb_addr = vzalloc(TCMU_RING_SIZE);
868 if (!udev->mb_addr) {
869 ret = -ENOMEM;
870 goto err_vzalloc;
871 }
872
873 /* mailbox fits in first part of CMDR space */
874 udev->cmdr_size = CMDR_SIZE - CMDR_OFF;
875 udev->data_off = CMDR_SIZE;
876 udev->data_size = TCMU_RING_SIZE - CMDR_SIZE;
877
878 mb = udev->mb_addr;
Andy Grover0ad46af2015-04-14 17:30:04 -0700879 mb->version = TCMU_MAILBOX_VERSION;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700880 mb->cmdr_off = CMDR_OFF;
881 mb->cmdr_size = udev->cmdr_size;
882
883 WARN_ON(!PAGE_ALIGNED(udev->data_off));
884 WARN_ON(udev->data_size % PAGE_SIZE);
885
Andy Grover0ad46af2015-04-14 17:30:04 -0700886 info->version = xstr(TCMU_MAILBOX_VERSION);
Andy Grover7c9e7a62014-10-01 16:07:05 -0700887
888 info->mem[0].name = "tcm-user command & data buffer";
889 info->mem[0].addr = (phys_addr_t) udev->mb_addr;
890 info->mem[0].size = TCMU_RING_SIZE;
891 info->mem[0].memtype = UIO_MEM_VIRTUAL;
892
893 info->irqcontrol = tcmu_irqcontrol;
894 info->irq = UIO_IRQ_CUSTOM;
895
896 info->mmap = tcmu_mmap;
897 info->open = tcmu_open;
898 info->release = tcmu_release;
899
900 ret = uio_register_device(tcmu_root_device, info);
901 if (ret)
902 goto err_register;
903
904 /* Other attributes can be configured in userspace */
905 dev->dev_attrib.hw_block_size = 512;
906 dev->dev_attrib.hw_max_sectors = 128;
907 dev->dev_attrib.hw_queue_depth = 128;
908
909 ret = tcmu_netlink_event(TCMU_CMD_ADDED_DEVICE, udev->uio_info.name,
910 udev->uio_info.uio_dev->minor);
911 if (ret)
912 goto err_netlink;
913
914 return 0;
915
916err_netlink:
917 uio_unregister_device(&udev->uio_info);
918err_register:
919 vfree(udev->mb_addr);
920err_vzalloc:
921 kfree(info->name);
922
923 return ret;
924}
925
926static int tcmu_check_pending_cmd(int id, void *p, void *data)
927{
928 struct tcmu_cmd *cmd = p;
929
930 if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
931 return 0;
932 return -EINVAL;
933}
934
935static void tcmu_free_device(struct se_device *dev)
936{
937 struct tcmu_dev *udev = TCMU_DEV(dev);
938 int i;
939
940 del_timer_sync(&udev->timeout);
941
942 vfree(udev->mb_addr);
943
944 /* Upper layer should drain all requests before calling this */
945 spin_lock_irq(&udev->commands_lock);
946 i = idr_for_each(&udev->commands, tcmu_check_pending_cmd, NULL);
947 idr_destroy(&udev->commands);
948 spin_unlock_irq(&udev->commands_lock);
949 WARN_ON(i);
950
951 /* Device was configured */
952 if (udev->uio_info.uio_dev) {
953 tcmu_netlink_event(TCMU_CMD_REMOVED_DEVICE, udev->uio_info.name,
954 udev->uio_info.uio_dev->minor);
955
956 uio_unregister_device(&udev->uio_info);
957 kfree(udev->uio_info.name);
958 kfree(udev->name);
959 }
960
961 kfree(udev);
962}
963
964enum {
Andy Grover9c1cd1b2015-05-19 14:44:39 -0700965 Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_err,
Andy Grover7c9e7a62014-10-01 16:07:05 -0700966};
967
968static match_table_t tokens = {
969 {Opt_dev_config, "dev_config=%s"},
970 {Opt_dev_size, "dev_size=%u"},
Andy Grover9c1cd1b2015-05-19 14:44:39 -0700971 {Opt_hw_block_size, "hw_block_size=%u"},
Andy Grover7c9e7a62014-10-01 16:07:05 -0700972 {Opt_err, NULL}
973};
974
975static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev,
976 const char *page, ssize_t count)
977{
978 struct tcmu_dev *udev = TCMU_DEV(dev);
979 char *orig, *ptr, *opts, *arg_p;
980 substring_t args[MAX_OPT_ARGS];
981 int ret = 0, token;
Andy Grover9c1cd1b2015-05-19 14:44:39 -0700982 unsigned long tmp_ul;
Andy Grover7c9e7a62014-10-01 16:07:05 -0700983
984 opts = kstrdup(page, GFP_KERNEL);
985 if (!opts)
986 return -ENOMEM;
987
988 orig = opts;
989
990 while ((ptr = strsep(&opts, ",\n")) != NULL) {
991 if (!*ptr)
992 continue;
993
994 token = match_token(ptr, tokens, args);
995 switch (token) {
996 case Opt_dev_config:
997 if (match_strlcpy(udev->dev_config, &args[0],
998 TCMU_CONFIG_LEN) == 0) {
999 ret = -EINVAL;
1000 break;
1001 }
1002 pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config);
1003 break;
1004 case Opt_dev_size:
1005 arg_p = match_strdup(&args[0]);
1006 if (!arg_p) {
1007 ret = -ENOMEM;
1008 break;
1009 }
1010 ret = kstrtoul(arg_p, 0, (unsigned long *) &udev->dev_size);
1011 kfree(arg_p);
1012 if (ret < 0)
1013 pr_err("kstrtoul() failed for dev_size=\n");
1014 break;
Andy Grover9c1cd1b2015-05-19 14:44:39 -07001015 case Opt_hw_block_size:
1016 arg_p = match_strdup(&args[0]);
1017 if (!arg_p) {
1018 ret = -ENOMEM;
1019 break;
1020 }
1021 ret = kstrtoul(arg_p, 0, &tmp_ul);
1022 kfree(arg_p);
1023 if (ret < 0) {
1024 pr_err("kstrtoul() failed for hw_block_size=\n");
1025 break;
1026 }
1027 if (!tmp_ul) {
1028 pr_err("hw_block_size must be nonzero\n");
1029 break;
1030 }
1031 dev->dev_attrib.hw_block_size = tmp_ul;
1032 break;
Andy Grover7c9e7a62014-10-01 16:07:05 -07001033 default:
1034 break;
1035 }
1036 }
1037
1038 kfree(orig);
1039 return (!ret) ? count : ret;
1040}
1041
1042static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b)
1043{
1044 struct tcmu_dev *udev = TCMU_DEV(dev);
1045 ssize_t bl = 0;
1046
1047 bl = sprintf(b + bl, "Config: %s ",
1048 udev->dev_config[0] ? udev->dev_config : "NULL");
Andy Grover8ee83a72015-05-01 10:43:45 -07001049 bl += sprintf(b + bl, "Size: %zu\n", udev->dev_size);
Andy Grover7c9e7a62014-10-01 16:07:05 -07001050
1051 return bl;
1052}
1053
1054static sector_t tcmu_get_blocks(struct se_device *dev)
1055{
1056 struct tcmu_dev *udev = TCMU_DEV(dev);
1057
1058 return div_u64(udev->dev_size - dev->dev_attrib.block_size,
1059 dev->dev_attrib.block_size);
1060}
1061
1062static sense_reason_t
Andy Grover7c9e7a62014-10-01 16:07:05 -07001063tcmu_pass_op(struct se_cmd *se_cmd)
1064{
1065 int ret = tcmu_queue_cmd(se_cmd);
1066
1067 if (ret != 0)
1068 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1069 else
1070 return TCM_NO_SENSE;
1071}
1072
Andy Grover7c9e7a62014-10-01 16:07:05 -07001073static sense_reason_t
1074tcmu_parse_cdb(struct se_cmd *cmd)
1075{
Andy Grover7bfea53b2015-05-19 14:44:40 -07001076 return passthrough_parse_cdb(cmd, tcmu_pass_op);
Andy Grover7c9e7a62014-10-01 16:07:05 -07001077}
1078
Andy Grover9c1cd1b2015-05-19 14:44:39 -07001079DEF_TB_DEV_ATTRIB_RO(tcmu, hw_pi_prot_type);
1080TB_DEV_ATTR_RO(tcmu, hw_pi_prot_type);
1081
1082DEF_TB_DEV_ATTRIB_RO(tcmu, hw_block_size);
1083TB_DEV_ATTR_RO(tcmu, hw_block_size);
1084
1085DEF_TB_DEV_ATTRIB_RO(tcmu, hw_max_sectors);
1086TB_DEV_ATTR_RO(tcmu, hw_max_sectors);
1087
1088DEF_TB_DEV_ATTRIB_RO(tcmu, hw_queue_depth);
1089TB_DEV_ATTR_RO(tcmu, hw_queue_depth);
Nicholas Bellingere9f720d2014-11-28 05:11:24 +00001090
1091static struct configfs_attribute *tcmu_backend_dev_attrs[] = {
Nicholas Bellingere9f720d2014-11-28 05:11:24 +00001092 &tcmu_dev_attrib_hw_pi_prot_type.attr,
Nicholas Bellingere9f720d2014-11-28 05:11:24 +00001093 &tcmu_dev_attrib_hw_block_size.attr,
Nicholas Bellingere9f720d2014-11-28 05:11:24 +00001094 &tcmu_dev_attrib_hw_max_sectors.attr,
Nicholas Bellingere9f720d2014-11-28 05:11:24 +00001095 &tcmu_dev_attrib_hw_queue_depth.attr,
Nicholas Bellingere9f720d2014-11-28 05:11:24 +00001096 NULL,
1097};
1098
Andy Grover7c9e7a62014-10-01 16:07:05 -07001099static struct se_subsystem_api tcmu_template = {
1100 .name = "user",
1101 .inquiry_prod = "USER",
1102 .inquiry_rev = TCMU_VERSION,
1103 .owner = THIS_MODULE,
Andy Grovera3541702015-05-19 14:44:41 -07001104 .transport_flags = TRANSPORT_FLAG_PASSTHROUGH,
Andy Grover7c9e7a62014-10-01 16:07:05 -07001105 .attach_hba = tcmu_attach_hba,
1106 .detach_hba = tcmu_detach_hba,
1107 .alloc_device = tcmu_alloc_device,
1108 .configure_device = tcmu_configure_device,
1109 .free_device = tcmu_free_device,
1110 .parse_cdb = tcmu_parse_cdb,
1111 .set_configfs_dev_params = tcmu_set_configfs_dev_params,
1112 .show_configfs_dev_params = tcmu_show_configfs_dev_params,
1113 .get_device_type = sbc_get_device_type,
1114 .get_blocks = tcmu_get_blocks,
1115};
1116
1117static int __init tcmu_module_init(void)
1118{
Nicholas Bellingere9f720d2014-11-28 05:11:24 +00001119 struct target_backend_cits *tbc = &tcmu_template.tb_cits;
Andy Grover7c9e7a62014-10-01 16:07:05 -07001120 int ret;
1121
1122 BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0);
1123
1124 tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache",
1125 sizeof(struct tcmu_cmd),
1126 __alignof__(struct tcmu_cmd),
1127 0, NULL);
1128 if (!tcmu_cmd_cache)
1129 return -ENOMEM;
1130
1131 tcmu_root_device = root_device_register("tcm_user");
1132 if (IS_ERR(tcmu_root_device)) {
1133 ret = PTR_ERR(tcmu_root_device);
1134 goto out_free_cache;
1135 }
1136
1137 ret = genl_register_family(&tcmu_genl_family);
1138 if (ret < 0) {
1139 goto out_unreg_device;
1140 }
1141
Nicholas Bellingere9f720d2014-11-28 05:11:24 +00001142 target_core_setup_sub_cits(&tcmu_template);
1143 tbc->tb_dev_attrib_cit.ct_attrs = tcmu_backend_dev_attrs;
1144
Andy Grover7c9e7a62014-10-01 16:07:05 -07001145 ret = transport_subsystem_register(&tcmu_template);
1146 if (ret)
1147 goto out_unreg_genl;
1148
1149 return 0;
1150
1151out_unreg_genl:
1152 genl_unregister_family(&tcmu_genl_family);
1153out_unreg_device:
1154 root_device_unregister(tcmu_root_device);
1155out_free_cache:
1156 kmem_cache_destroy(tcmu_cmd_cache);
1157
1158 return ret;
1159}
1160
1161static void __exit tcmu_module_exit(void)
1162{
1163 transport_subsystem_release(&tcmu_template);
1164 genl_unregister_family(&tcmu_genl_family);
1165 root_device_unregister(tcmu_root_device);
1166 kmem_cache_destroy(tcmu_cmd_cache);
1167}
1168
1169MODULE_DESCRIPTION("TCM USER subsystem plugin");
1170MODULE_AUTHOR("Shaohua Li <shli@kernel.org>");
1171MODULE_AUTHOR("Andy Grover <agrover@redhat.com>");
1172MODULE_LICENSE("GPL");
1173
1174module_init(tcmu_module_init);
1175module_exit(tcmu_module_exit);