1 /*
2  * Copyright (C) 2013 Shaohua Li <shli@kernel.org>
3  * Copyright (C) 2014 Red Hat, Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #include <linux/spinlock.h>
20 #include <linux/module.h>
21 #include <linux/idr.h>
22 #include <linux/timer.h>
23 #include <linux/parser.h>
24 #include <scsi/scsi.h>
25 #include <scsi/scsi_host.h>
26 #include <linux/uio_driver.h>
27 #include <net/genetlink.h>
28 #include <target/target_core_base.h>
29 #include <target/target_core_fabric.h>
30 #include <target/target_core_backend.h>
31 #include <target/target_core_backend_configfs.h>
32 
33 #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 
68 static struct device *tcmu_root_device;
69 
70 struct tcmu_hba {
71 	u32 host_id;
72 };
73 
74 /* User wants all cmds or just some */
75 enum passthru_level {
76 	TCMU_PASS_ALL = 0,
77 	TCMU_PASS_IO,
78 	TCMU_PASS_INVALID,
79 };
80 
81 #define TCMU_CONFIG_LEN 256
82 
83 struct tcmu_dev {
84 	struct se_device se_dev;
85 
86 	char *name;
87 	struct se_hba *hba;
88 
89 #define TCMU_DEV_BIT_OPEN 0
90 #define TCMU_DEV_BIT_BROKEN 1
91 	unsigned long flags;
92 	enum passthru_level pass_level;
93 
94 	struct uio_info uio_info;
95 
96 	struct tcmu_mailbox *mb_addr;
97 	size_t dev_size;
98 	u32 cmdr_size;
99 	u32 cmdr_last_cleaned;
100 	/* Offset of data ring from start of mb */
101 	size_t data_off;
102 	size_t data_size;
103 	/* Ring head + tail values. */
104 	/* Must add data_off and mb_addr to get the address */
105 	size_t data_head;
106 	size_t data_tail;
107 
108 	wait_queue_head_t wait_cmdr;
109 	/* TODO should this be a mutex? */
110 	spinlock_t cmdr_lock;
111 
112 	struct idr commands;
113 	spinlock_t commands_lock;
114 
115 	struct timer_list timeout;
116 
117 	char dev_config[TCMU_CONFIG_LEN];
118 };
119 
120 #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev)
121 
122 #define CMDR_OFF sizeof(struct tcmu_mailbox)
123 
124 struct tcmu_cmd {
125 	struct se_cmd *se_cmd;
126 	struct tcmu_dev *tcmu_dev;
127 
128 	uint16_t cmd_id;
129 
130 	/* Can't use se_cmd->data_length when cleaning up expired cmds, because if
131 	   cmd has been completed then accessing se_cmd is off limits */
132 	size_t data_length;
133 
134 	unsigned long deadline;
135 
136 #define TCMU_CMD_BIT_EXPIRED 0
137 	unsigned long flags;
138 };
139 
140 static struct kmem_cache *tcmu_cmd_cache;
141 
142 /* multicast group */
143 enum tcmu_multicast_groups {
144 	TCMU_MCGRP_CONFIG,
145 };
146 
147 static const struct genl_multicast_group tcmu_mcgrps[] = {
148 	[TCMU_MCGRP_CONFIG] = { .name = "config", },
149 };
150 
151 /* Our generic netlink family */
152 static struct genl_family tcmu_genl_family = {
153 	.id = GENL_ID_GENERATE,
154 	.hdrsize = 0,
155 	.name = "TCM-USER",
156 	.version = 1,
157 	.maxattr = TCMU_ATTR_MAX,
158 	.mcgrps = tcmu_mcgrps,
159 	.n_mcgrps = ARRAY_SIZE(tcmu_mcgrps),
160 };
161 
162 static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd)
163 {
164 	struct se_device *se_dev = se_cmd->se_dev;
165 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
166 	struct tcmu_cmd *tcmu_cmd;
167 	int cmd_id;
168 
169 	tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_KERNEL);
170 	if (!tcmu_cmd)
171 		return NULL;
172 
173 	tcmu_cmd->se_cmd = se_cmd;
174 	tcmu_cmd->tcmu_dev = udev;
175 	tcmu_cmd->data_length = se_cmd->data_length;
176 
177 	tcmu_cmd->deadline = jiffies + msecs_to_jiffies(TCMU_TIME_OUT);
178 
179 	idr_preload(GFP_KERNEL);
180 	spin_lock_irq(&udev->commands_lock);
181 	cmd_id = idr_alloc(&udev->commands, tcmu_cmd, 0,
182 		USHRT_MAX, GFP_NOWAIT);
183 	spin_unlock_irq(&udev->commands_lock);
184 	idr_preload_end();
185 
186 	if (cmd_id < 0) {
187 		kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
188 		return NULL;
189 	}
190 	tcmu_cmd->cmd_id = cmd_id;
191 
192 	return tcmu_cmd;
193 }
194 
195 static inline void tcmu_flush_dcache_range(void *vaddr, size_t size)
196 {
197 	unsigned long offset = (unsigned long) vaddr & ~PAGE_MASK;
198 
199 	size = round_up(size+offset, PAGE_SIZE);
200 	vaddr -= offset;
201 
202 	while (size) {
203 		flush_dcache_page(virt_to_page(vaddr));
204 		size -= PAGE_SIZE;
205 	}
206 }
207 
208 /*
209  * Some ring helper functions. We don't assume size is a power of 2 so
210  * we can't use circ_buf.h.
211  */
212 static inline size_t spc_used(size_t head, size_t tail, size_t size)
213 {
214 	int diff = head - tail;
215 
216 	if (diff >= 0)
217 		return diff;
218 	else
219 		return size + diff;
220 }
221 
222 static inline size_t spc_free(size_t head, size_t tail, size_t size)
223 {
224 	/* Keep 1 byte unused or we can't tell full from empty */
225 	return (size - spc_used(head, tail, size) - 1);
226 }
227 
228 static inline size_t head_to_end(size_t head, size_t size)
229 {
230 	return size - head;
231 }
232 
233 #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size)
234 
235 /*
236  * We can't queue a command until we have space available on the cmd ring *and* space
237  * space avail on the data ring.
238  *
239  * Called with ring lock held.
240  */
241 static bool is_ring_space_avail(struct tcmu_dev *udev, size_t cmd_size, size_t data_needed)
242 {
243 	struct tcmu_mailbox *mb = udev->mb_addr;
244 	size_t space;
245 	u32 cmd_head;
246 	size_t cmd_needed;
247 
248 	tcmu_flush_dcache_range(mb, sizeof(*mb));
249 
250 	cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
251 
252 	/*
253 	 * If cmd end-of-ring space is too small then we need space for a NOP plus
254 	 * original cmd - cmds are internally contiguous.
255 	 */
256 	if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size)
257 		cmd_needed = cmd_size;
258 	else
259 		cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size);
260 
261 	space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size);
262 	if (space < cmd_needed) {
263 		pr_debug("no cmd space: %u %u %u\n", cmd_head,
264 		       udev->cmdr_last_cleaned, udev->cmdr_size);
265 		return false;
266 	}
267 
268 	space = spc_free(udev->data_head, udev->data_tail, udev->data_size);
269 	if (space < data_needed) {
270 		pr_debug("no data space: %zu %zu %zu\n", udev->data_head,
271 		       udev->data_tail, udev->data_size);
272 		return false;
273 	}
274 
275 	return true;
276 }
277 
278 static int tcmu_queue_cmd_ring(struct tcmu_cmd *tcmu_cmd)
279 {
280 	struct tcmu_dev *udev = tcmu_cmd->tcmu_dev;
281 	struct se_cmd *se_cmd = tcmu_cmd->se_cmd;
282 	size_t base_command_size, command_size;
283 	struct tcmu_mailbox *mb;
284 	struct tcmu_cmd_entry *entry;
285 	int i;
286 	struct scatterlist *sg;
287 	struct iovec *iov;
288 	int iov_cnt = 0;
289 	uint32_t cmd_head;
290 	uint64_t cdb_off;
291 
292 	if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags))
293 		return -EINVAL;
294 
295 	/*
296 	 * Must be a certain minimum size for response sense info, but
297 	 * also may be larger if the iov array is large.
298 	 *
299 	 * iovs = sgl_nents+1, for end-of-ring case, plus another 1
300 	 * b/c size == offsetof one-past-element.
301 	*/
302 	base_command_size = max(offsetof(struct tcmu_cmd_entry,
303 					 req.iov[se_cmd->t_data_nents + 2]),
304 				sizeof(struct tcmu_cmd_entry));
305 	command_size = base_command_size
306 		+ round_up(scsi_command_size(se_cmd->t_task_cdb), TCMU_OP_ALIGN_SIZE);
307 
308 	WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1));
309 
310 	spin_lock_irq(&udev->cmdr_lock);
311 
312 	mb = udev->mb_addr;
313 	cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
314 	if ((command_size > (udev->cmdr_size / 2))
315 	    || tcmu_cmd->data_length > (udev->data_size - 1))
316 		pr_warn("TCMU: Request of size %zu/%zu may be too big for %u/%zu "
317 			"cmd/data ring buffers\n", command_size, tcmu_cmd->data_length,
318 			udev->cmdr_size, udev->data_size);
319 
320 	while (!is_ring_space_avail(udev, command_size, tcmu_cmd->data_length)) {
321 		int ret;
322 		DEFINE_WAIT(__wait);
323 
324 		prepare_to_wait(&udev->wait_cmdr, &__wait, TASK_INTERRUPTIBLE);
325 
326 		pr_debug("sleeping for ring space\n");
327 		spin_unlock_irq(&udev->cmdr_lock);
328 		ret = schedule_timeout(msecs_to_jiffies(TCMU_TIME_OUT));
329 		finish_wait(&udev->wait_cmdr, &__wait);
330 		if (!ret) {
331 			pr_warn("tcmu: command timed out\n");
332 			return -ETIMEDOUT;
333 		}
334 
335 		spin_lock_irq(&udev->cmdr_lock);
336 
337 		/* We dropped cmdr_lock, cmd_head is stale */
338 		cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
339 	}
340 
341 	/* Insert a PAD if end-of-ring space is too small */
342 	if (head_to_end(cmd_head, udev->cmdr_size) < command_size) {
343 		size_t pad_size = head_to_end(cmd_head, udev->cmdr_size);
344 
345 		entry = (void *) mb + CMDR_OFF + cmd_head;
346 		tcmu_flush_dcache_range(entry, sizeof(*entry));
347 		tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_PAD);
348 		tcmu_hdr_set_len(&entry->hdr.len_op, pad_size);
349 		entry->hdr.cmd_id = 0; /* not used for PAD */
350 		entry->hdr.kflags = 0;
351 		entry->hdr.uflags = 0;
352 
353 		UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size);
354 
355 		cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */
356 		WARN_ON(cmd_head != 0);
357 	}
358 
359 	entry = (void *) mb + CMDR_OFF + cmd_head;
360 	tcmu_flush_dcache_range(entry, sizeof(*entry));
361 	tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD);
362 	tcmu_hdr_set_len(&entry->hdr.len_op, command_size);
363 	entry->hdr.cmd_id = tcmu_cmd->cmd_id;
364 	entry->hdr.kflags = 0;
365 	entry->hdr.uflags = 0;
366 
367 	/*
368 	 * Fix up iovecs, and handle if allocation in data ring wrapped.
369 	 */
370 	iov = &entry->req.iov[0];
371 	for_each_sg(se_cmd->t_data_sg, sg, se_cmd->t_data_nents, i) {
372 		size_t copy_bytes = min((size_t)sg->length,
373 				     head_to_end(udev->data_head, udev->data_size));
374 		void *from = kmap_atomic(sg_page(sg)) + sg->offset;
375 		void *to = (void *) mb + udev->data_off + udev->data_head;
376 
377 		if (tcmu_cmd->se_cmd->data_direction == DMA_TO_DEVICE) {
378 			memcpy(to, from, copy_bytes);
379 			tcmu_flush_dcache_range(to, copy_bytes);
380 		}
381 
382 		/* Even iov_base is relative to mb_addr */
383 		iov->iov_len = copy_bytes;
384 		iov->iov_base = (void __user *) udev->data_off +
385 						udev->data_head;
386 		iov_cnt++;
387 		iov++;
388 
389 		UPDATE_HEAD(udev->data_head, copy_bytes, udev->data_size);
390 
391 		/* Uh oh, we wrapped the buffer. Must split sg across 2 iovs. */
392 		if (sg->length != copy_bytes) {
393 			from += copy_bytes;
394 			copy_bytes = sg->length - copy_bytes;
395 
396 			iov->iov_len = copy_bytes;
397 			iov->iov_base = (void __user *) udev->data_off +
398 							udev->data_head;
399 
400 			if (se_cmd->data_direction == DMA_TO_DEVICE) {
401 				to = (void *) mb + udev->data_off + udev->data_head;
402 				memcpy(to, from, copy_bytes);
403 				tcmu_flush_dcache_range(to, copy_bytes);
404 			}
405 
406 			iov_cnt++;
407 			iov++;
408 
409 			UPDATE_HEAD(udev->data_head, copy_bytes, udev->data_size);
410 		}
411 
412 		kunmap_atomic(from);
413 	}
414 	entry->req.iov_cnt = iov_cnt;
415 	entry->req.iov_bidi_cnt = 0;
416 	entry->req.iov_dif_cnt = 0;
417 
418 	/* All offsets relative to mb_addr, not start of entry! */
419 	cdb_off = CMDR_OFF + cmd_head + base_command_size;
420 	memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb));
421 	entry->req.cdb_off = cdb_off;
422 	tcmu_flush_dcache_range(entry, sizeof(*entry));
423 
424 	UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size);
425 	tcmu_flush_dcache_range(mb, sizeof(*mb));
426 
427 	spin_unlock_irq(&udev->cmdr_lock);
428 
429 	/* TODO: only if FLUSH and FUA? */
430 	uio_event_notify(&udev->uio_info);
431 
432 	mod_timer(&udev->timeout,
433 		round_jiffies_up(jiffies + msecs_to_jiffies(TCMU_TIME_OUT)));
434 
435 	return 0;
436 }
437 
438 static int tcmu_queue_cmd(struct se_cmd *se_cmd)
439 {
440 	struct se_device *se_dev = se_cmd->se_dev;
441 	struct tcmu_dev *udev = TCMU_DEV(se_dev);
442 	struct tcmu_cmd *tcmu_cmd;
443 	int ret;
444 
445 	tcmu_cmd = tcmu_alloc_cmd(se_cmd);
446 	if (!tcmu_cmd)
447 		return -ENOMEM;
448 
449 	ret = tcmu_queue_cmd_ring(tcmu_cmd);
450 	if (ret < 0) {
451 		pr_err("TCMU: Could not queue command\n");
452 		spin_lock_irq(&udev->commands_lock);
453 		idr_remove(&udev->commands, tcmu_cmd->cmd_id);
454 		spin_unlock_irq(&udev->commands_lock);
455 
456 		kmem_cache_free(tcmu_cmd_cache, tcmu_cmd);
457 	}
458 
459 	return ret;
460 }
461 
462 static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *entry)
463 {
464 	struct se_cmd *se_cmd = cmd->se_cmd;
465 	struct tcmu_dev *udev = cmd->tcmu_dev;
466 
467 	if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) {
468 		/* cmd has been completed already from timeout, just reclaim data
469 		   ring space */
470 		UPDATE_HEAD(udev->data_tail, cmd->data_length, udev->data_size);
471 		return;
472 	}
473 
474 	if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) {
475 		UPDATE_HEAD(udev->data_tail, cmd->data_length, udev->data_size);
476 		pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n",
477 			cmd->se_cmd);
478 		transport_generic_request_failure(cmd->se_cmd,
479 			TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE);
480 		cmd->se_cmd = NULL;
481 		kmem_cache_free(tcmu_cmd_cache, cmd);
482 		return;
483 	}
484 
485 	if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) {
486 		memcpy(se_cmd->sense_buffer, entry->rsp.sense_buffer,
487 			       se_cmd->scsi_sense_length);
488 
489 		UPDATE_HEAD(udev->data_tail, cmd->data_length, udev->data_size);
490 	}
491 	else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
492 		struct scatterlist *sg;
493 		int i;
494 
495 		/* It'd be easier to look at entry's iovec again, but UAM */
496 		for_each_sg(se_cmd->t_data_sg, sg, se_cmd->t_data_nents, i) {
497 			size_t copy_bytes;
498 			void *to;
499 			void *from;
500 
501 			copy_bytes = min((size_t)sg->length,
502 					 head_to_end(udev->data_tail, udev->data_size));
503 
504 			to = kmap_atomic(sg_page(sg)) + sg->offset;
505 			WARN_ON(sg->length + sg->offset > PAGE_SIZE);
506 			from = (void *) udev->mb_addr + udev->data_off + udev->data_tail;
507 			tcmu_flush_dcache_range(from, copy_bytes);
508 			memcpy(to, from, copy_bytes);
509 
510 			UPDATE_HEAD(udev->data_tail, copy_bytes, udev->data_size);
511 
512 			/* Uh oh, wrapped the data buffer for this sg's data */
513 			if (sg->length != copy_bytes) {
514 				from = (void *) udev->mb_addr + udev->data_off + udev->data_tail;
515 				WARN_ON(udev->data_tail);
516 				to += copy_bytes;
517 				copy_bytes = sg->length - copy_bytes;
518 				tcmu_flush_dcache_range(from, copy_bytes);
519 				memcpy(to, from, copy_bytes);
520 
521 				UPDATE_HEAD(udev->data_tail, copy_bytes, udev->data_size);
522 			}
523 
524 			kunmap_atomic(to);
525 		}
526 
527 	} else if (se_cmd->data_direction == DMA_TO_DEVICE) {
528 		UPDATE_HEAD(udev->data_tail, cmd->data_length, udev->data_size);
529 	} else {
530 		pr_warn("TCMU: data direction was %d!\n", se_cmd->data_direction);
531 	}
532 
533 	target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status);
534 	cmd->se_cmd = NULL;
535 
536 	kmem_cache_free(tcmu_cmd_cache, cmd);
537 }
538 
539 static unsigned int tcmu_handle_completions(struct tcmu_dev *udev)
540 {
541 	struct tcmu_mailbox *mb;
542 	LIST_HEAD(cpl_cmds);
543 	unsigned long flags;
544 	int handled = 0;
545 
546 	if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) {
547 		pr_err("ring broken, not handling completions\n");
548 		return 0;
549 	}
550 
551 	spin_lock_irqsave(&udev->cmdr_lock, flags);
552 
553 	mb = udev->mb_addr;
554 	tcmu_flush_dcache_range(mb, sizeof(*mb));
555 
556 	while (udev->cmdr_last_cleaned != ACCESS_ONCE(mb->cmd_tail)) {
557 
558 		struct tcmu_cmd_entry *entry = (void *) mb + CMDR_OFF + udev->cmdr_last_cleaned;
559 		struct tcmu_cmd *cmd;
560 
561 		tcmu_flush_dcache_range(entry, sizeof(*entry));
562 
563 		if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD) {
564 			UPDATE_HEAD(udev->cmdr_last_cleaned,
565 				    tcmu_hdr_get_len(entry->hdr.len_op),
566 				    udev->cmdr_size);
567 			continue;
568 		}
569 		WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD);
570 
571 		spin_lock(&udev->commands_lock);
572 		cmd = idr_find(&udev->commands, entry->hdr.cmd_id);
573 		if (cmd)
574 			idr_remove(&udev->commands, cmd->cmd_id);
575 		spin_unlock(&udev->commands_lock);
576 
577 		if (!cmd) {
578 			pr_err("cmd_id not found, ring is broken\n");
579 			set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags);
580 			break;
581 		}
582 
583 		tcmu_handle_completion(cmd, entry);
584 
585 		UPDATE_HEAD(udev->cmdr_last_cleaned,
586 			    tcmu_hdr_get_len(entry->hdr.len_op),
587 			    udev->cmdr_size);
588 
589 		handled++;
590 	}
591 
592 	if (mb->cmd_tail == mb->cmd_head)
593 		del_timer(&udev->timeout); /* no more pending cmds */
594 
595 	spin_unlock_irqrestore(&udev->cmdr_lock, flags);
596 
597 	wake_up(&udev->wait_cmdr);
598 
599 	return handled;
600 }
601 
602 static int tcmu_check_expired_cmd(int id, void *p, void *data)
603 {
604 	struct tcmu_cmd *cmd = p;
605 
606 	if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
607 		return 0;
608 
609 	if (!time_after(cmd->deadline, jiffies))
610 		return 0;
611 
612 	set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags);
613 	target_complete_cmd(cmd->se_cmd, SAM_STAT_CHECK_CONDITION);
614 	cmd->se_cmd = NULL;
615 
616 	kmem_cache_free(tcmu_cmd_cache, cmd);
617 
618 	return 0;
619 }
620 
621 static void tcmu_device_timedout(unsigned long data)
622 {
623 	struct tcmu_dev *udev = (struct tcmu_dev *)data;
624 	unsigned long flags;
625 	int handled;
626 
627 	handled = tcmu_handle_completions(udev);
628 
629 	pr_warn("%d completions handled from timeout\n", handled);
630 
631 	spin_lock_irqsave(&udev->commands_lock, flags);
632 	idr_for_each(&udev->commands, tcmu_check_expired_cmd, NULL);
633 	spin_unlock_irqrestore(&udev->commands_lock, flags);
634 
635 	/*
636 	 * We don't need to wakeup threads on wait_cmdr since they have their
637 	 * own timeout.
638 	 */
639 }
640 
641 static int tcmu_attach_hba(struct se_hba *hba, u32 host_id)
642 {
643 	struct tcmu_hba *tcmu_hba;
644 
645 	tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL);
646 	if (!tcmu_hba)
647 		return -ENOMEM;
648 
649 	tcmu_hba->host_id = host_id;
650 	hba->hba_ptr = tcmu_hba;
651 
652 	return 0;
653 }
654 
655 static void tcmu_detach_hba(struct se_hba *hba)
656 {
657 	kfree(hba->hba_ptr);
658 	hba->hba_ptr = NULL;
659 }
660 
661 static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name)
662 {
663 	struct tcmu_dev *udev;
664 
665 	udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL);
666 	if (!udev)
667 		return NULL;
668 
669 	udev->name = kstrdup(name, GFP_KERNEL);
670 	if (!udev->name) {
671 		kfree(udev);
672 		return NULL;
673 	}
674 
675 	udev->hba = hba;
676 
677 	init_waitqueue_head(&udev->wait_cmdr);
678 	spin_lock_init(&udev->cmdr_lock);
679 
680 	idr_init(&udev->commands);
681 	spin_lock_init(&udev->commands_lock);
682 
683 	setup_timer(&udev->timeout, tcmu_device_timedout,
684 		(unsigned long)udev);
685 
686 	udev->pass_level = TCMU_PASS_ALL;
687 
688 	return &udev->se_dev;
689 }
690 
691 static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on)
692 {
693 	struct tcmu_dev *tcmu_dev = container_of(info, struct tcmu_dev, uio_info);
694 
695 	tcmu_handle_completions(tcmu_dev);
696 
697 	return 0;
698 }
699 
700 /*
701  * mmap code from uio.c. Copied here because we want to hook mmap()
702  * and this stuff must come along.
703  */
704 static int tcmu_find_mem_index(struct vm_area_struct *vma)
705 {
706 	struct tcmu_dev *udev = vma->vm_private_data;
707 	struct uio_info *info = &udev->uio_info;
708 
709 	if (vma->vm_pgoff < MAX_UIO_MAPS) {
710 		if (info->mem[vma->vm_pgoff].size == 0)
711 			return -1;
712 		return (int)vma->vm_pgoff;
713 	}
714 	return -1;
715 }
716 
717 static int tcmu_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
718 {
719 	struct tcmu_dev *udev = vma->vm_private_data;
720 	struct uio_info *info = &udev->uio_info;
721 	struct page *page;
722 	unsigned long offset;
723 	void *addr;
724 
725 	int mi = tcmu_find_mem_index(vma);
726 	if (mi < 0)
727 		return VM_FAULT_SIGBUS;
728 
729 	/*
730 	 * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
731 	 * to use mem[N].
732 	 */
733 	offset = (vmf->pgoff - mi) << PAGE_SHIFT;
734 
735 	addr = (void *)(unsigned long)info->mem[mi].addr + offset;
736 	if (info->mem[mi].memtype == UIO_MEM_LOGICAL)
737 		page = virt_to_page(addr);
738 	else
739 		page = vmalloc_to_page(addr);
740 	get_page(page);
741 	vmf->page = page;
742 	return 0;
743 }
744 
745 static const struct vm_operations_struct tcmu_vm_ops = {
746 	.fault = tcmu_vma_fault,
747 };
748 
749 static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma)
750 {
751 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
752 
753 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
754 	vma->vm_ops = &tcmu_vm_ops;
755 
756 	vma->vm_private_data = udev;
757 
758 	/* Ensure the mmap is exactly the right size */
759 	if (vma_pages(vma) != (TCMU_RING_SIZE >> PAGE_SHIFT))
760 		return -EINVAL;
761 
762 	return 0;
763 }
764 
765 static int tcmu_open(struct uio_info *info, struct inode *inode)
766 {
767 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
768 
769 	/* O_EXCL not supported for char devs, so fake it? */
770 	if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags))
771 		return -EBUSY;
772 
773 	pr_debug("open\n");
774 
775 	return 0;
776 }
777 
778 static int tcmu_release(struct uio_info *info, struct inode *inode)
779 {
780 	struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info);
781 
782 	clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags);
783 
784 	pr_debug("close\n");
785 
786 	return 0;
787 }
788 
789 static int tcmu_netlink_event(enum tcmu_genl_cmd cmd, const char *name, int minor)
790 {
791 	struct sk_buff *skb;
792 	void *msg_header;
793 	int ret = -ENOMEM;
794 
795 	skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
796 	if (!skb)
797 		return ret;
798 
799 	msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd);
800 	if (!msg_header)
801 		goto free_skb;
802 
803 	ret = nla_put_string(skb, TCMU_ATTR_DEVICE, name);
804 	if (ret < 0)
805 		goto free_skb;
806 
807 	ret = nla_put_u32(skb, TCMU_ATTR_MINOR, minor);
808 	if (ret < 0)
809 		goto free_skb;
810 
811 	genlmsg_end(skb, msg_header);
812 
813 	ret = genlmsg_multicast(&tcmu_genl_family, skb, 0,
814 				TCMU_MCGRP_CONFIG, GFP_KERNEL);
815 
816 	/* We don't care if no one is listening */
817 	if (ret == -ESRCH)
818 		ret = 0;
819 
820 	return ret;
821 free_skb:
822 	nlmsg_free(skb);
823 	return ret;
824 }
825 
826 static int tcmu_configure_device(struct se_device *dev)
827 {
828 	struct tcmu_dev *udev = TCMU_DEV(dev);
829 	struct tcmu_hba *hba = udev->hba->hba_ptr;
830 	struct uio_info *info;
831 	struct tcmu_mailbox *mb;
832 	size_t size;
833 	size_t used;
834 	int ret = 0;
835 	char *str;
836 
837 	info = &udev->uio_info;
838 
839 	size = snprintf(NULL, 0, "tcm-user/%u/%s/%s", hba->host_id, udev->name,
840 			udev->dev_config);
841 	size += 1; /* for \0 */
842 	str = kmalloc(size, GFP_KERNEL);
843 	if (!str)
844 		return -ENOMEM;
845 
846 	used = snprintf(str, size, "tcm-user/%u/%s", hba->host_id, udev->name);
847 
848 	if (udev->dev_config[0])
849 		snprintf(str + used, size - used, "/%s", udev->dev_config);
850 
851 	info->name = str;
852 
853 	udev->mb_addr = vzalloc(TCMU_RING_SIZE);
854 	if (!udev->mb_addr) {
855 		ret = -ENOMEM;
856 		goto err_vzalloc;
857 	}
858 
859 	/* mailbox fits in first part of CMDR space */
860 	udev->cmdr_size = CMDR_SIZE - CMDR_OFF;
861 	udev->data_off = CMDR_SIZE;
862 	udev->data_size = TCMU_RING_SIZE - CMDR_SIZE;
863 
864 	mb = udev->mb_addr;
865 	mb->version = TCMU_MAILBOX_VERSION;
866 	mb->cmdr_off = CMDR_OFF;
867 	mb->cmdr_size = udev->cmdr_size;
868 
869 	WARN_ON(!PAGE_ALIGNED(udev->data_off));
870 	WARN_ON(udev->data_size % PAGE_SIZE);
871 
872 	info->version = xstr(TCMU_MAILBOX_VERSION);
873 
874 	info->mem[0].name = "tcm-user command & data buffer";
875 	info->mem[0].addr = (phys_addr_t) udev->mb_addr;
876 	info->mem[0].size = TCMU_RING_SIZE;
877 	info->mem[0].memtype = UIO_MEM_VIRTUAL;
878 
879 	info->irqcontrol = tcmu_irqcontrol;
880 	info->irq = UIO_IRQ_CUSTOM;
881 
882 	info->mmap = tcmu_mmap;
883 	info->open = tcmu_open;
884 	info->release = tcmu_release;
885 
886 	ret = uio_register_device(tcmu_root_device, info);
887 	if (ret)
888 		goto err_register;
889 
890 	/* Other attributes can be configured in userspace */
891 	dev->dev_attrib.hw_block_size = 512;
892 	dev->dev_attrib.hw_max_sectors = 128;
893 	dev->dev_attrib.hw_queue_depth = 128;
894 
895 	ret = tcmu_netlink_event(TCMU_CMD_ADDED_DEVICE, udev->uio_info.name,
896 				 udev->uio_info.uio_dev->minor);
897 	if (ret)
898 		goto err_netlink;
899 
900 	return 0;
901 
902 err_netlink:
903 	uio_unregister_device(&udev->uio_info);
904 err_register:
905 	vfree(udev->mb_addr);
906 err_vzalloc:
907 	kfree(info->name);
908 
909 	return ret;
910 }
911 
912 static int tcmu_check_pending_cmd(int id, void *p, void *data)
913 {
914 	struct tcmu_cmd *cmd = p;
915 
916 	if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
917 		return 0;
918 	return -EINVAL;
919 }
920 
921 static void tcmu_free_device(struct se_device *dev)
922 {
923 	struct tcmu_dev *udev = TCMU_DEV(dev);
924 	int i;
925 
926 	del_timer_sync(&udev->timeout);
927 
928 	vfree(udev->mb_addr);
929 
930 	/* Upper layer should drain all requests before calling this */
931 	spin_lock_irq(&udev->commands_lock);
932 	i = idr_for_each(&udev->commands, tcmu_check_pending_cmd, NULL);
933 	idr_destroy(&udev->commands);
934 	spin_unlock_irq(&udev->commands_lock);
935 	WARN_ON(i);
936 
937 	/* Device was configured */
938 	if (udev->uio_info.uio_dev) {
939 		tcmu_netlink_event(TCMU_CMD_REMOVED_DEVICE, udev->uio_info.name,
940 				   udev->uio_info.uio_dev->minor);
941 
942 		uio_unregister_device(&udev->uio_info);
943 		kfree(udev->uio_info.name);
944 		kfree(udev->name);
945 	}
946 
947 	kfree(udev);
948 }
949 
950 enum {
951 	Opt_dev_config, Opt_dev_size, Opt_err, Opt_pass_level,
952 };
953 
954 static match_table_t tokens = {
955 	{Opt_dev_config, "dev_config=%s"},
956 	{Opt_dev_size, "dev_size=%u"},
957 	{Opt_pass_level, "pass_level=%u"},
958 	{Opt_err, NULL}
959 };
960 
961 static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev,
962 		const char *page, ssize_t count)
963 {
964 	struct tcmu_dev *udev = TCMU_DEV(dev);
965 	char *orig, *ptr, *opts, *arg_p;
966 	substring_t args[MAX_OPT_ARGS];
967 	int ret = 0, token;
968 	int arg;
969 
970 	opts = kstrdup(page, GFP_KERNEL);
971 	if (!opts)
972 		return -ENOMEM;
973 
974 	orig = opts;
975 
976 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
977 		if (!*ptr)
978 			continue;
979 
980 		token = match_token(ptr, tokens, args);
981 		switch (token) {
982 		case Opt_dev_config:
983 			if (match_strlcpy(udev->dev_config, &args[0],
984 					  TCMU_CONFIG_LEN) == 0) {
985 				ret = -EINVAL;
986 				break;
987 			}
988 			pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config);
989 			break;
990 		case Opt_dev_size:
991 			arg_p = match_strdup(&args[0]);
992 			if (!arg_p) {
993 				ret = -ENOMEM;
994 				break;
995 			}
996 			ret = kstrtoul(arg_p, 0, (unsigned long *) &udev->dev_size);
997 			kfree(arg_p);
998 			if (ret < 0)
999 				pr_err("kstrtoul() failed for dev_size=\n");
1000 			break;
1001 		case Opt_pass_level:
1002 			match_int(args, &arg);
1003 			if (arg >= TCMU_PASS_INVALID) {
1004 				pr_warn("TCMU: Invalid pass_level: %d\n", arg);
1005 				break;
1006 			}
1007 
1008 			pr_debug("TCMU: Setting pass_level to %d\n", arg);
1009 			udev->pass_level = arg;
1010 			break;
1011 		default:
1012 			break;
1013 		}
1014 	}
1015 
1016 	kfree(orig);
1017 	return (!ret) ? count : ret;
1018 }
1019 
1020 static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b)
1021 {
1022 	struct tcmu_dev *udev = TCMU_DEV(dev);
1023 	ssize_t bl = 0;
1024 
1025 	bl = sprintf(b + bl, "Config: %s ",
1026 		     udev->dev_config[0] ? udev->dev_config : "NULL");
1027 	bl += sprintf(b + bl, "Size: %zu PassLevel: %u\n",
1028 		      udev->dev_size, udev->pass_level);
1029 
1030 	return bl;
1031 }
1032 
1033 static sector_t tcmu_get_blocks(struct se_device *dev)
1034 {
1035 	struct tcmu_dev *udev = TCMU_DEV(dev);
1036 
1037 	return div_u64(udev->dev_size - dev->dev_attrib.block_size,
1038 		       dev->dev_attrib.block_size);
1039 }
1040 
1041 static sense_reason_t
1042 tcmu_execute_rw(struct se_cmd *se_cmd, struct scatterlist *sgl, u32 sgl_nents,
1043 		enum dma_data_direction data_direction)
1044 {
1045 	int ret;
1046 
1047 	ret = tcmu_queue_cmd(se_cmd);
1048 
1049 	if (ret != 0)
1050 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1051 	else
1052 		return TCM_NO_SENSE;
1053 }
1054 
1055 static sense_reason_t
1056 tcmu_pass_op(struct se_cmd *se_cmd)
1057 {
1058 	int ret = tcmu_queue_cmd(se_cmd);
1059 
1060 	if (ret != 0)
1061 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1062 	else
1063 		return TCM_NO_SENSE;
1064 }
1065 
1066 static struct sbc_ops tcmu_sbc_ops = {
1067 	.execute_rw = tcmu_execute_rw,
1068 	.execute_sync_cache	= tcmu_pass_op,
1069 	.execute_write_same	= tcmu_pass_op,
1070 	.execute_write_same_unmap = tcmu_pass_op,
1071 	.execute_unmap		= tcmu_pass_op,
1072 };
1073 
1074 static sense_reason_t
1075 tcmu_parse_cdb(struct se_cmd *cmd)
1076 {
1077 	unsigned char *cdb = cmd->t_task_cdb;
1078 	struct tcmu_dev *udev = TCMU_DEV(cmd->se_dev);
1079 	sense_reason_t ret;
1080 
1081 	switch (udev->pass_level) {
1082 	case TCMU_PASS_ALL:
1083 		/* We're just like pscsi, then */
1084 		/*
1085 		 * For REPORT LUNS we always need to emulate the response, for everything
1086 		 * else, pass it up.
1087 		 */
1088 		switch (cdb[0]) {
1089 		case REPORT_LUNS:
1090 			cmd->execute_cmd = spc_emulate_report_luns;
1091 			break;
1092 		case READ_6:
1093 		case READ_10:
1094 		case READ_12:
1095 		case READ_16:
1096 		case WRITE_6:
1097 		case WRITE_10:
1098 		case WRITE_12:
1099 		case WRITE_16:
1100 		case WRITE_VERIFY:
1101 			cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
1102 			/* FALLTHROUGH */
1103 		default:
1104 			cmd->execute_cmd = tcmu_pass_op;
1105 		}
1106 		ret = TCM_NO_SENSE;
1107 		break;
1108 	case TCMU_PASS_IO:
1109 		ret = sbc_parse_cdb(cmd, &tcmu_sbc_ops);
1110 		break;
1111 	default:
1112 		pr_err("Unknown tcm-user pass level %d\n", udev->pass_level);
1113 		ret = TCM_CHECK_CONDITION_ABORT_CMD;
1114 	}
1115 
1116 	return ret;
1117 }
1118 
1119 DEF_TB_DEFAULT_ATTRIBS(tcmu);
1120 
1121 static struct configfs_attribute *tcmu_backend_dev_attrs[] = {
1122 	&tcmu_dev_attrib_emulate_model_alias.attr,
1123 	&tcmu_dev_attrib_emulate_dpo.attr,
1124 	&tcmu_dev_attrib_emulate_fua_write.attr,
1125 	&tcmu_dev_attrib_emulate_fua_read.attr,
1126 	&tcmu_dev_attrib_emulate_write_cache.attr,
1127 	&tcmu_dev_attrib_emulate_ua_intlck_ctrl.attr,
1128 	&tcmu_dev_attrib_emulate_tas.attr,
1129 	&tcmu_dev_attrib_emulate_tpu.attr,
1130 	&tcmu_dev_attrib_emulate_tpws.attr,
1131 	&tcmu_dev_attrib_emulate_caw.attr,
1132 	&tcmu_dev_attrib_emulate_3pc.attr,
1133 	&tcmu_dev_attrib_pi_prot_type.attr,
1134 	&tcmu_dev_attrib_hw_pi_prot_type.attr,
1135 	&tcmu_dev_attrib_pi_prot_format.attr,
1136 	&tcmu_dev_attrib_enforce_pr_isids.attr,
1137 	&tcmu_dev_attrib_is_nonrot.attr,
1138 	&tcmu_dev_attrib_emulate_rest_reord.attr,
1139 	&tcmu_dev_attrib_force_pr_aptpl.attr,
1140 	&tcmu_dev_attrib_hw_block_size.attr,
1141 	&tcmu_dev_attrib_block_size.attr,
1142 	&tcmu_dev_attrib_hw_max_sectors.attr,
1143 	&tcmu_dev_attrib_optimal_sectors.attr,
1144 	&tcmu_dev_attrib_hw_queue_depth.attr,
1145 	&tcmu_dev_attrib_queue_depth.attr,
1146 	&tcmu_dev_attrib_max_unmap_lba_count.attr,
1147 	&tcmu_dev_attrib_max_unmap_block_desc_count.attr,
1148 	&tcmu_dev_attrib_unmap_granularity.attr,
1149 	&tcmu_dev_attrib_unmap_granularity_alignment.attr,
1150 	&tcmu_dev_attrib_max_write_same_len.attr,
1151 	NULL,
1152 };
1153 
1154 static struct se_subsystem_api tcmu_template = {
1155 	.name			= "user",
1156 	.inquiry_prod		= "USER",
1157 	.inquiry_rev		= TCMU_VERSION,
1158 	.owner			= THIS_MODULE,
1159 	.transport_type		= TRANSPORT_PLUGIN_VHBA_PDEV,
1160 	.attach_hba		= tcmu_attach_hba,
1161 	.detach_hba		= tcmu_detach_hba,
1162 	.alloc_device		= tcmu_alloc_device,
1163 	.configure_device	= tcmu_configure_device,
1164 	.free_device		= tcmu_free_device,
1165 	.parse_cdb		= tcmu_parse_cdb,
1166 	.set_configfs_dev_params = tcmu_set_configfs_dev_params,
1167 	.show_configfs_dev_params = tcmu_show_configfs_dev_params,
1168 	.get_device_type	= sbc_get_device_type,
1169 	.get_blocks		= tcmu_get_blocks,
1170 };
1171 
1172 static int __init tcmu_module_init(void)
1173 {
1174 	struct target_backend_cits *tbc = &tcmu_template.tb_cits;
1175 	int ret;
1176 
1177 	BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0);
1178 
1179 	tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache",
1180 				sizeof(struct tcmu_cmd),
1181 				__alignof__(struct tcmu_cmd),
1182 				0, NULL);
1183 	if (!tcmu_cmd_cache)
1184 		return -ENOMEM;
1185 
1186 	tcmu_root_device = root_device_register("tcm_user");
1187 	if (IS_ERR(tcmu_root_device)) {
1188 		ret = PTR_ERR(tcmu_root_device);
1189 		goto out_free_cache;
1190 	}
1191 
1192 	ret = genl_register_family(&tcmu_genl_family);
1193 	if (ret < 0) {
1194 		goto out_unreg_device;
1195 	}
1196 
1197 	target_core_setup_sub_cits(&tcmu_template);
1198 	tbc->tb_dev_attrib_cit.ct_attrs = tcmu_backend_dev_attrs;
1199 
1200 	ret = transport_subsystem_register(&tcmu_template);
1201 	if (ret)
1202 		goto out_unreg_genl;
1203 
1204 	return 0;
1205 
1206 out_unreg_genl:
1207 	genl_unregister_family(&tcmu_genl_family);
1208 out_unreg_device:
1209 	root_device_unregister(tcmu_root_device);
1210 out_free_cache:
1211 	kmem_cache_destroy(tcmu_cmd_cache);
1212 
1213 	return ret;
1214 }
1215 
1216 static void __exit tcmu_module_exit(void)
1217 {
1218 	transport_subsystem_release(&tcmu_template);
1219 	genl_unregister_family(&tcmu_genl_family);
1220 	root_device_unregister(tcmu_root_device);
1221 	kmem_cache_destroy(tcmu_cmd_cache);
1222 }
1223 
1224 MODULE_DESCRIPTION("TCM USER subsystem plugin");
1225 MODULE_AUTHOR("Shaohua Li <shli@kernel.org>");
1226 MODULE_AUTHOR("Andy Grover <agrover@redhat.com>");
1227 MODULE_LICENSE("GPL");
1228 
1229 module_init(tcmu_module_init);
1230 module_exit(tcmu_module_exit);
1231