]>
Commit | Line | Data |
---|---|---|
7c9e7a6f AG |
1 | /* |
2 | * Copyright (C) 2013 Shaohua Li <shli@kernel.org> | |
3 | * Copyright (C) 2014 Red Hat, Inc. | |
f97ec7db | 4 | * Copyright (C) 2015 Arrikto, Inc. |
7c9e7a6f AG |
5 | * |
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> | |
ba929992 | 23 | #include <linux/kernel.h> |
7c9e7a6f AG |
24 | #include <linux/timer.h> |
25 | #include <linux/parser.h> | |
5538d294 | 26 | #include <linux/vmalloc.h> |
7c9e7a6f | 27 | #include <linux/uio_driver.h> |
ac64a2ce | 28 | #include <linux/stringify.h> |
26418649 | 29 | #include <linux/bitops.h> |
f5045724 | 30 | #include <linux/highmem.h> |
7c9e7a6f | 31 | #include <net/genetlink.h> |
ba929992 BVA |
32 | #include <scsi/scsi_common.h> |
33 | #include <scsi/scsi_proto.h> | |
7c9e7a6f AG |
34 | #include <target/target_core_base.h> |
35 | #include <target/target_core_fabric.h> | |
36 | #include <target/target_core_backend.h> | |
e9f720d6 | 37 | |
7c9e7a6f AG |
38 | #include <linux/target_core_user.h> |
39 | ||
40 | /* | |
41 | * Define a shared-memory interface for LIO to pass SCSI commands and | |
42 | * data to userspace for processing. This is to allow backends that | |
43 | * are too complex for in-kernel support to be possible. | |
44 | * | |
45 | * It uses the UIO framework to do a lot of the device-creation and | |
46 | * introspection work for us. | |
47 | * | |
48 | * See the .h file for how the ring is laid out. Note that while the | |
49 | * command ring is defined, the particulars of the data area are | |
50 | * not. Offset values in the command entry point to other locations | |
51 | * internal to the mmap()ed area. There is separate space outside the | |
52 | * command ring for data buffers. This leaves maximum flexibility for | |
53 | * moving buffer allocations, or even page flipping or other | |
54 | * allocation techniques, without altering the command ring layout. | |
55 | * | |
56 | * SECURITY: | |
57 | * The user process must be assumed to be malicious. There's no way to | |
58 | * prevent it breaking the command ring protocol if it wants, but in | |
59 | * order to prevent other issues we must only ever read *data* from | |
60 | * the shared memory area, not offsets or sizes. This applies to | |
61 | * command ring entries as well as the mailbox. Extra code needed for | |
62 | * this may have a 'UAM' comment. | |
63 | */ | |
64 | ||
65 | ||
66 | #define TCMU_TIME_OUT (30 * MSEC_PER_SEC) | |
67 | ||
26418649 SY |
68 | #define DATA_BLOCK_BITS 256 |
69 | #define DATA_BLOCK_SIZE 4096 | |
70 | ||
7c9e7a6f | 71 | #define CMDR_SIZE (16 * 4096) |
26418649 | 72 | #define DATA_SIZE (DATA_BLOCK_BITS * DATA_BLOCK_SIZE) |
7c9e7a6f AG |
73 | |
74 | #define TCMU_RING_SIZE (CMDR_SIZE + DATA_SIZE) | |
75 | ||
76 | static struct device *tcmu_root_device; | |
77 | ||
78 | struct tcmu_hba { | |
79 | u32 host_id; | |
80 | }; | |
81 | ||
7c9e7a6f AG |
82 | #define TCMU_CONFIG_LEN 256 |
83 | ||
84 | struct tcmu_dev { | |
85 | struct se_device se_dev; | |
86 | ||
87 | char *name; | |
88 | struct se_hba *hba; | |
89 | ||
90 | #define TCMU_DEV_BIT_OPEN 0 | |
91 | #define TCMU_DEV_BIT_BROKEN 1 | |
92 | unsigned long flags; | |
7c9e7a6f AG |
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; | |
3d9b9555 | 100 | /* Offset of data area from start of mb */ |
26418649 | 101 | /* Must add data_off and mb_addr to get the address */ |
7c9e7a6f AG |
102 | size_t data_off; |
103 | size_t data_size; | |
26418649 SY |
104 | |
105 | DECLARE_BITMAP(data_bitmap, DATA_BLOCK_BITS); | |
7c9e7a6f AG |
106 | |
107 | wait_queue_head_t wait_cmdr; | |
108 | /* TODO should this be a mutex? */ | |
109 | spinlock_t cmdr_lock; | |
110 | ||
111 | struct idr commands; | |
112 | spinlock_t commands_lock; | |
113 | ||
114 | struct timer_list timeout; | |
115 | ||
116 | char dev_config[TCMU_CONFIG_LEN]; | |
117 | }; | |
118 | ||
119 | #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev) | |
120 | ||
121 | #define CMDR_OFF sizeof(struct tcmu_mailbox) | |
122 | ||
123 | struct tcmu_cmd { | |
124 | struct se_cmd *se_cmd; | |
125 | struct tcmu_dev *tcmu_dev; | |
126 | ||
127 | uint16_t cmd_id; | |
128 | ||
26418649 | 129 | /* Can't use se_cmd when cleaning up expired cmds, because if |
7c9e7a6f | 130 | cmd has been completed then accessing se_cmd is off limits */ |
26418649 | 131 | DECLARE_BITMAP(data_bitmap, DATA_BLOCK_BITS); |
7c9e7a6f AG |
132 | |
133 | unsigned long deadline; | |
134 | ||
135 | #define TCMU_CMD_BIT_EXPIRED 0 | |
136 | unsigned long flags; | |
137 | }; | |
138 | ||
139 | static struct kmem_cache *tcmu_cmd_cache; | |
140 | ||
141 | /* multicast group */ | |
142 | enum tcmu_multicast_groups { | |
143 | TCMU_MCGRP_CONFIG, | |
144 | }; | |
145 | ||
146 | static const struct genl_multicast_group tcmu_mcgrps[] = { | |
147 | [TCMU_MCGRP_CONFIG] = { .name = "config", }, | |
148 | }; | |
149 | ||
150 | /* Our generic netlink family */ | |
56989f6d | 151 | static struct genl_family tcmu_genl_family __ro_after_init = { |
489111e5 | 152 | .module = THIS_MODULE, |
7c9e7a6f AG |
153 | .hdrsize = 0, |
154 | .name = "TCM-USER", | |
155 | .version = 1, | |
156 | .maxattr = TCMU_ATTR_MAX, | |
157 | .mcgrps = tcmu_mcgrps, | |
158 | .n_mcgrps = ARRAY_SIZE(tcmu_mcgrps), | |
20c08b36 | 159 | .netnsok = true, |
7c9e7a6f AG |
160 | }; |
161 | ||
ade9e091 MC |
162 | /* Sense Key = 2 (Not Ready) |
163 | * ASC/ASCQ = 0x0800 (Logical Unit Communication Failure) | |
164 | */ | |
165 | static const char lu_comm_failure_sense[18] = { | |
166 | 0x70, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x0a, | |
167 | 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, | |
168 | 0x00, 0x00 }; | |
169 | ||
7c9e7a6f AG |
170 | static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd) |
171 | { | |
172 | struct se_device *se_dev = se_cmd->se_dev; | |
173 | struct tcmu_dev *udev = TCMU_DEV(se_dev); | |
174 | struct tcmu_cmd *tcmu_cmd; | |
175 | int cmd_id; | |
176 | ||
177 | tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_KERNEL); | |
178 | if (!tcmu_cmd) | |
179 | return NULL; | |
180 | ||
181 | tcmu_cmd->se_cmd = se_cmd; | |
182 | tcmu_cmd->tcmu_dev = udev; | |
7c9e7a6f AG |
183 | tcmu_cmd->deadline = jiffies + msecs_to_jiffies(TCMU_TIME_OUT); |
184 | ||
185 | idr_preload(GFP_KERNEL); | |
186 | spin_lock_irq(&udev->commands_lock); | |
187 | cmd_id = idr_alloc(&udev->commands, tcmu_cmd, 0, | |
188 | USHRT_MAX, GFP_NOWAIT); | |
189 | spin_unlock_irq(&udev->commands_lock); | |
190 | idr_preload_end(); | |
191 | ||
192 | if (cmd_id < 0) { | |
193 | kmem_cache_free(tcmu_cmd_cache, tcmu_cmd); | |
194 | return NULL; | |
195 | } | |
196 | tcmu_cmd->cmd_id = cmd_id; | |
197 | ||
198 | return tcmu_cmd; | |
199 | } | |
200 | ||
201 | static inline void tcmu_flush_dcache_range(void *vaddr, size_t size) | |
202 | { | |
b75d8063 | 203 | unsigned long offset = offset_in_page(vaddr); |
7c9e7a6f AG |
204 | |
205 | size = round_up(size+offset, PAGE_SIZE); | |
206 | vaddr -= offset; | |
207 | ||
208 | while (size) { | |
209 | flush_dcache_page(virt_to_page(vaddr)); | |
210 | size -= PAGE_SIZE; | |
211 | } | |
212 | } | |
213 | ||
214 | /* | |
215 | * Some ring helper functions. We don't assume size is a power of 2 so | |
216 | * we can't use circ_buf.h. | |
217 | */ | |
218 | static inline size_t spc_used(size_t head, size_t tail, size_t size) | |
219 | { | |
220 | int diff = head - tail; | |
221 | ||
222 | if (diff >= 0) | |
223 | return diff; | |
224 | else | |
225 | return size + diff; | |
226 | } | |
227 | ||
228 | static inline size_t spc_free(size_t head, size_t tail, size_t size) | |
229 | { | |
230 | /* Keep 1 byte unused or we can't tell full from empty */ | |
231 | return (size - spc_used(head, tail, size) - 1); | |
232 | } | |
233 | ||
234 | static inline size_t head_to_end(size_t head, size_t size) | |
235 | { | |
236 | return size - head; | |
237 | } | |
238 | ||
f1dbd087 SY |
239 | static inline void new_iov(struct iovec **iov, int *iov_cnt, |
240 | struct tcmu_dev *udev) | |
241 | { | |
242 | struct iovec *iovec; | |
243 | ||
244 | if (*iov_cnt != 0) | |
245 | (*iov)++; | |
246 | (*iov_cnt)++; | |
247 | ||
248 | iovec = *iov; | |
249 | memset(iovec, 0, sizeof(struct iovec)); | |
f1dbd087 SY |
250 | } |
251 | ||
7c9e7a6f AG |
252 | #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size) |
253 | ||
26418649 SY |
254 | /* offset is relative to mb_addr */ |
255 | static inline size_t get_block_offset(struct tcmu_dev *dev, | |
256 | int block, int remaining) | |
257 | { | |
258 | return dev->data_off + block * DATA_BLOCK_SIZE + | |
259 | DATA_BLOCK_SIZE - remaining; | |
260 | } | |
261 | ||
262 | static inline size_t iov_tail(struct tcmu_dev *udev, struct iovec *iov) | |
263 | { | |
264 | return (size_t)iov->iov_base + iov->iov_len; | |
265 | } | |
266 | ||
f97ec7db IT |
267 | static void alloc_and_scatter_data_area(struct tcmu_dev *udev, |
268 | struct scatterlist *data_sg, unsigned int data_nents, | |
269 | struct iovec **iov, int *iov_cnt, bool copy_data) | |
270 | { | |
26418649 SY |
271 | int i, block; |
272 | int block_remaining = 0; | |
f97ec7db | 273 | void *from, *to; |
26418649 | 274 | size_t copy_bytes, to_offset; |
f97ec7db IT |
275 | struct scatterlist *sg; |
276 | ||
277 | for_each_sg(data_sg, sg, data_nents, i) { | |
26418649 | 278 | int sg_remaining = sg->length; |
f97ec7db | 279 | from = kmap_atomic(sg_page(sg)) + sg->offset; |
26418649 SY |
280 | while (sg_remaining > 0) { |
281 | if (block_remaining == 0) { | |
282 | block = find_first_zero_bit(udev->data_bitmap, | |
283 | DATA_BLOCK_BITS); | |
284 | block_remaining = DATA_BLOCK_SIZE; | |
285 | set_bit(block, udev->data_bitmap); | |
286 | } | |
287 | copy_bytes = min_t(size_t, sg_remaining, | |
288 | block_remaining); | |
289 | to_offset = get_block_offset(udev, block, | |
290 | block_remaining); | |
291 | to = (void *)udev->mb_addr + to_offset; | |
292 | if (*iov_cnt != 0 && | |
293 | to_offset == iov_tail(udev, *iov)) { | |
294 | (*iov)->iov_len += copy_bytes; | |
295 | } else { | |
296 | new_iov(iov, iov_cnt, udev); | |
297 | (*iov)->iov_base = (void __user *) to_offset; | |
298 | (*iov)->iov_len = copy_bytes; | |
299 | } | |
f97ec7db | 300 | if (copy_data) { |
26418649 SY |
301 | memcpy(to, from + sg->length - sg_remaining, |
302 | copy_bytes); | |
f97ec7db IT |
303 | tcmu_flush_dcache_range(to, copy_bytes); |
304 | } | |
26418649 SY |
305 | sg_remaining -= copy_bytes; |
306 | block_remaining -= copy_bytes; | |
f97ec7db | 307 | } |
e2e21bd8 | 308 | kunmap_atomic(from - sg->offset); |
f97ec7db IT |
309 | } |
310 | } | |
311 | ||
26418649 | 312 | static void free_data_area(struct tcmu_dev *udev, struct tcmu_cmd *cmd) |
0c28481f | 313 | { |
26418649 SY |
314 | bitmap_xor(udev->data_bitmap, udev->data_bitmap, cmd->data_bitmap, |
315 | DATA_BLOCK_BITS); | |
0c28481f SY |
316 | } |
317 | ||
2bcbe7c9 XL |
318 | static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *cmd, |
319 | bool bidi) | |
f97ec7db | 320 | { |
2bcbe7c9 | 321 | struct se_cmd *se_cmd = cmd->se_cmd; |
26418649 SY |
322 | int i, block; |
323 | int block_remaining = 0; | |
f97ec7db | 324 | void *from, *to; |
26418649 | 325 | size_t copy_bytes, from_offset; |
2bcbe7c9 XL |
326 | struct scatterlist *sg, *data_sg; |
327 | unsigned int data_nents; | |
328 | DECLARE_BITMAP(bitmap, DATA_BLOCK_BITS); | |
329 | ||
330 | bitmap_copy(bitmap, cmd->data_bitmap, DATA_BLOCK_BITS); | |
331 | ||
332 | if (!bidi) { | |
333 | data_sg = se_cmd->t_data_sg; | |
334 | data_nents = se_cmd->t_data_nents; | |
335 | } else { | |
336 | uint32_t count; | |
337 | ||
338 | /* | |
339 | * For bidi case, the first count blocks are for Data-Out | |
340 | * buffer blocks, and before gathering the Data-In buffer | |
341 | * the Data-Out buffer blocks should be discarded. | |
342 | */ | |
343 | count = DIV_ROUND_UP(se_cmd->data_length, DATA_BLOCK_SIZE); | |
344 | while (count--) { | |
345 | block = find_first_bit(bitmap, DATA_BLOCK_BITS); | |
346 | clear_bit(block, bitmap); | |
347 | } | |
348 | ||
349 | data_sg = se_cmd->t_bidi_data_sg; | |
350 | data_nents = se_cmd->t_bidi_data_nents; | |
351 | } | |
f97ec7db | 352 | |
f97ec7db | 353 | for_each_sg(data_sg, sg, data_nents, i) { |
26418649 | 354 | int sg_remaining = sg->length; |
f97ec7db | 355 | to = kmap_atomic(sg_page(sg)) + sg->offset; |
26418649 SY |
356 | while (sg_remaining > 0) { |
357 | if (block_remaining == 0) { | |
2bcbe7c9 | 358 | block = find_first_bit(bitmap, |
26418649 SY |
359 | DATA_BLOCK_BITS); |
360 | block_remaining = DATA_BLOCK_SIZE; | |
2bcbe7c9 | 361 | clear_bit(block, bitmap); |
26418649 SY |
362 | } |
363 | copy_bytes = min_t(size_t, sg_remaining, | |
364 | block_remaining); | |
365 | from_offset = get_block_offset(udev, block, | |
366 | block_remaining); | |
367 | from = (void *) udev->mb_addr + from_offset; | |
f97ec7db | 368 | tcmu_flush_dcache_range(from, copy_bytes); |
26418649 SY |
369 | memcpy(to + sg->length - sg_remaining, from, |
370 | copy_bytes); | |
f97ec7db | 371 | |
26418649 SY |
372 | sg_remaining -= copy_bytes; |
373 | block_remaining -= copy_bytes; | |
f97ec7db | 374 | } |
e2e21bd8 | 375 | kunmap_atomic(to - sg->offset); |
f97ec7db IT |
376 | } |
377 | } | |
378 | ||
26418649 SY |
379 | static inline size_t spc_bitmap_free(unsigned long *bitmap) |
380 | { | |
381 | return DATA_BLOCK_SIZE * (DATA_BLOCK_BITS - | |
382 | bitmap_weight(bitmap, DATA_BLOCK_BITS)); | |
383 | } | |
384 | ||
7c9e7a6f | 385 | /* |
f97ec7db | 386 | * We can't queue a command until we have space available on the cmd ring *and* |
3d9b9555 | 387 | * space available on the data area. |
7c9e7a6f AG |
388 | * |
389 | * Called with ring lock held. | |
390 | */ | |
f56574a2 | 391 | static bool is_ring_space_avail(struct tcmu_dev *udev, size_t cmd_size, size_t data_needed) |
7c9e7a6f AG |
392 | { |
393 | struct tcmu_mailbox *mb = udev->mb_addr; | |
0241fd39 | 394 | size_t space, cmd_needed; |
7c9e7a6f AG |
395 | u32 cmd_head; |
396 | ||
397 | tcmu_flush_dcache_range(mb, sizeof(*mb)); | |
398 | ||
399 | cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ | |
400 | ||
f56574a2 AG |
401 | /* |
402 | * If cmd end-of-ring space is too small then we need space for a NOP plus | |
403 | * original cmd - cmds are internally contiguous. | |
404 | */ | |
405 | if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size) | |
406 | cmd_needed = cmd_size; | |
407 | else | |
408 | cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size); | |
409 | ||
7c9e7a6f AG |
410 | space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size); |
411 | if (space < cmd_needed) { | |
412 | pr_debug("no cmd space: %u %u %u\n", cmd_head, | |
413 | udev->cmdr_last_cleaned, udev->cmdr_size); | |
414 | return false; | |
415 | } | |
416 | ||
26418649 | 417 | space = spc_bitmap_free(udev->data_bitmap); |
7c9e7a6f | 418 | if (space < data_needed) { |
0241fd39 | 419 | pr_debug("no data space: only %zu available, but ask for %zu\n", |
26418649 | 420 | space, data_needed); |
7c9e7a6f AG |
421 | return false; |
422 | } | |
423 | ||
424 | return true; | |
425 | } | |
426 | ||
646f4da0 XL |
427 | static inline size_t tcmu_cmd_get_data_length(struct tcmu_cmd *tcmu_cmd) |
428 | { | |
429 | struct se_cmd *se_cmd = tcmu_cmd->se_cmd; | |
430 | size_t data_length = round_up(se_cmd->data_length, DATA_BLOCK_SIZE); | |
431 | ||
432 | if (se_cmd->se_cmd_flags & SCF_BIDI) { | |
433 | BUG_ON(!(se_cmd->t_bidi_data_sg && se_cmd->t_bidi_data_nents)); | |
434 | data_length += round_up(se_cmd->t_bidi_data_sg->length, | |
435 | DATA_BLOCK_SIZE); | |
436 | } | |
437 | ||
438 | return data_length; | |
439 | } | |
440 | ||
ced06cd9 XL |
441 | static inline uint32_t tcmu_cmd_get_block_cnt(struct tcmu_cmd *tcmu_cmd) |
442 | { | |
443 | size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd); | |
444 | ||
445 | return data_length / DATA_BLOCK_SIZE; | |
446 | } | |
447 | ||
02eb924f AG |
448 | static sense_reason_t |
449 | tcmu_queue_cmd_ring(struct tcmu_cmd *tcmu_cmd) | |
7c9e7a6f AG |
450 | { |
451 | struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; | |
452 | struct se_cmd *se_cmd = tcmu_cmd->se_cmd; | |
453 | size_t base_command_size, command_size; | |
7c9e7a6f | 454 | struct tcmu_mailbox *mb; |
7c9e7a6f | 455 | struct tcmu_cmd_entry *entry; |
7c9e7a6f | 456 | struct iovec *iov; |
f97ec7db | 457 | int iov_cnt; |
7c9e7a6f AG |
458 | uint32_t cmd_head; |
459 | uint64_t cdb_off; | |
f97ec7db | 460 | bool copy_to_data_area; |
646f4da0 | 461 | size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd); |
26418649 | 462 | DECLARE_BITMAP(old_bitmap, DATA_BLOCK_BITS); |
7c9e7a6f AG |
463 | |
464 | if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) | |
02eb924f | 465 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
7c9e7a6f AG |
466 | |
467 | /* | |
468 | * Must be a certain minimum size for response sense info, but | |
469 | * also may be larger if the iov array is large. | |
470 | * | |
26418649 SY |
471 | * We prepare way too many iovs for potential uses here, because it's |
472 | * expensive to tell how many regions are freed in the bitmap | |
7c9e7a6f | 473 | */ |
26418649 | 474 | base_command_size = max(offsetof(struct tcmu_cmd_entry, |
ced06cd9 | 475 | req.iov[tcmu_cmd_get_block_cnt(tcmu_cmd)]), |
7c9e7a6f AG |
476 | sizeof(struct tcmu_cmd_entry)); |
477 | command_size = base_command_size | |
478 | + round_up(scsi_command_size(se_cmd->t_task_cdb), TCMU_OP_ALIGN_SIZE); | |
479 | ||
480 | WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1)); | |
481 | ||
482 | spin_lock_irq(&udev->cmdr_lock); | |
483 | ||
484 | mb = udev->mb_addr; | |
485 | cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ | |
554617b2 AG |
486 | if ((command_size > (udev->cmdr_size / 2)) || |
487 | data_length > udev->data_size) { | |
488 | pr_warn("TCMU: Request of size %zu/%zu is too big for %u/%zu " | |
3d9b9555 | 489 | "cmd ring/data area\n", command_size, data_length, |
7c9e7a6f | 490 | udev->cmdr_size, udev->data_size); |
554617b2 AG |
491 | spin_unlock_irq(&udev->cmdr_lock); |
492 | return TCM_INVALID_CDB_FIELD; | |
493 | } | |
7c9e7a6f | 494 | |
26418649 | 495 | while (!is_ring_space_avail(udev, command_size, data_length)) { |
7c9e7a6f AG |
496 | int ret; |
497 | DEFINE_WAIT(__wait); | |
498 | ||
499 | prepare_to_wait(&udev->wait_cmdr, &__wait, TASK_INTERRUPTIBLE); | |
500 | ||
501 | pr_debug("sleeping for ring space\n"); | |
502 | spin_unlock_irq(&udev->cmdr_lock); | |
503 | ret = schedule_timeout(msecs_to_jiffies(TCMU_TIME_OUT)); | |
504 | finish_wait(&udev->wait_cmdr, &__wait); | |
505 | if (!ret) { | |
506 | pr_warn("tcmu: command timed out\n"); | |
02eb924f | 507 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
7c9e7a6f AG |
508 | } |
509 | ||
510 | spin_lock_irq(&udev->cmdr_lock); | |
511 | ||
512 | /* We dropped cmdr_lock, cmd_head is stale */ | |
513 | cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ | |
514 | } | |
515 | ||
f56574a2 AG |
516 | /* Insert a PAD if end-of-ring space is too small */ |
517 | if (head_to_end(cmd_head, udev->cmdr_size) < command_size) { | |
518 | size_t pad_size = head_to_end(cmd_head, udev->cmdr_size); | |
519 | ||
7c9e7a6f AG |
520 | entry = (void *) mb + CMDR_OFF + cmd_head; |
521 | tcmu_flush_dcache_range(entry, sizeof(*entry)); | |
0ad46af8 AG |
522 | tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_PAD); |
523 | tcmu_hdr_set_len(&entry->hdr.len_op, pad_size); | |
524 | entry->hdr.cmd_id = 0; /* not used for PAD */ | |
525 | entry->hdr.kflags = 0; | |
526 | entry->hdr.uflags = 0; | |
7c9e7a6f AG |
527 | |
528 | UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size); | |
529 | ||
530 | cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ | |
531 | WARN_ON(cmd_head != 0); | |
532 | } | |
533 | ||
534 | entry = (void *) mb + CMDR_OFF + cmd_head; | |
535 | tcmu_flush_dcache_range(entry, sizeof(*entry)); | |
0ad46af8 AG |
536 | tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD); |
537 | tcmu_hdr_set_len(&entry->hdr.len_op, command_size); | |
538 | entry->hdr.cmd_id = tcmu_cmd->cmd_id; | |
539 | entry->hdr.kflags = 0; | |
540 | entry->hdr.uflags = 0; | |
7c9e7a6f | 541 | |
26418649 SY |
542 | bitmap_copy(old_bitmap, udev->data_bitmap, DATA_BLOCK_BITS); |
543 | ||
3d9b9555 | 544 | /* Handle allocating space from the data area */ |
7c9e7a6f | 545 | iov = &entry->req.iov[0]; |
f97ec7db | 546 | iov_cnt = 0; |
e4648b01 IT |
547 | copy_to_data_area = (se_cmd->data_direction == DMA_TO_DEVICE |
548 | || se_cmd->se_cmd_flags & SCF_BIDI); | |
f97ec7db IT |
549 | alloc_and_scatter_data_area(udev, se_cmd->t_data_sg, |
550 | se_cmd->t_data_nents, &iov, &iov_cnt, copy_to_data_area); | |
7c9e7a6f | 551 | entry->req.iov_cnt = iov_cnt; |
0ad46af8 | 552 | entry->req.iov_dif_cnt = 0; |
7c9e7a6f | 553 | |
e4648b01 | 554 | /* Handle BIDI commands */ |
646f4da0 XL |
555 | if (se_cmd->se_cmd_flags & SCF_BIDI) { |
556 | iov_cnt = 0; | |
557 | iov++; | |
558 | alloc_and_scatter_data_area(udev, se_cmd->t_bidi_data_sg, | |
559 | se_cmd->t_bidi_data_nents, &iov, &iov_cnt, | |
560 | false); | |
561 | entry->req.iov_bidi_cnt = iov_cnt; | |
562 | } | |
26418649 SY |
563 | /* cmd's data_bitmap is what changed in process */ |
564 | bitmap_xor(tcmu_cmd->data_bitmap, old_bitmap, udev->data_bitmap, | |
565 | DATA_BLOCK_BITS); | |
566 | ||
7c9e7a6f AG |
567 | /* All offsets relative to mb_addr, not start of entry! */ |
568 | cdb_off = CMDR_OFF + cmd_head + base_command_size; | |
569 | memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb)); | |
570 | entry->req.cdb_off = cdb_off; | |
571 | tcmu_flush_dcache_range(entry, sizeof(*entry)); | |
572 | ||
573 | UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size); | |
574 | tcmu_flush_dcache_range(mb, sizeof(*mb)); | |
575 | ||
576 | spin_unlock_irq(&udev->cmdr_lock); | |
577 | ||
578 | /* TODO: only if FLUSH and FUA? */ | |
579 | uio_event_notify(&udev->uio_info); | |
580 | ||
581 | mod_timer(&udev->timeout, | |
582 | round_jiffies_up(jiffies + msecs_to_jiffies(TCMU_TIME_OUT))); | |
583 | ||
02eb924f | 584 | return TCM_NO_SENSE; |
7c9e7a6f AG |
585 | } |
586 | ||
02eb924f AG |
587 | static sense_reason_t |
588 | tcmu_queue_cmd(struct se_cmd *se_cmd) | |
7c9e7a6f AG |
589 | { |
590 | struct se_device *se_dev = se_cmd->se_dev; | |
591 | struct tcmu_dev *udev = TCMU_DEV(se_dev); | |
592 | struct tcmu_cmd *tcmu_cmd; | |
ecaf597b | 593 | sense_reason_t ret; |
7c9e7a6f AG |
594 | |
595 | tcmu_cmd = tcmu_alloc_cmd(se_cmd); | |
596 | if (!tcmu_cmd) | |
02eb924f | 597 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
7c9e7a6f AG |
598 | |
599 | ret = tcmu_queue_cmd_ring(tcmu_cmd); | |
02eb924f | 600 | if (ret != TCM_NO_SENSE) { |
7c9e7a6f AG |
601 | pr_err("TCMU: Could not queue command\n"); |
602 | spin_lock_irq(&udev->commands_lock); | |
603 | idr_remove(&udev->commands, tcmu_cmd->cmd_id); | |
604 | spin_unlock_irq(&udev->commands_lock); | |
605 | ||
606 | kmem_cache_free(tcmu_cmd_cache, tcmu_cmd); | |
607 | } | |
608 | ||
609 | return ret; | |
610 | } | |
611 | ||
612 | static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *entry) | |
613 | { | |
614 | struct se_cmd *se_cmd = cmd->se_cmd; | |
615 | struct tcmu_dev *udev = cmd->tcmu_dev; | |
616 | ||
617 | if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) { | |
b25c7863 SY |
618 | /* |
619 | * cmd has been completed already from timeout, just reclaim | |
3d9b9555 | 620 | * data area space and free cmd |
b25c7863 | 621 | */ |
26418649 | 622 | free_data_area(udev, cmd); |
b25c7863 SY |
623 | |
624 | kmem_cache_free(tcmu_cmd_cache, cmd); | |
7c9e7a6f AG |
625 | return; |
626 | } | |
627 | ||
0ad46af8 | 628 | if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) { |
26418649 | 629 | free_data_area(udev, cmd); |
0ad46af8 AG |
630 | pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n", |
631 | cmd->se_cmd); | |
ed97d0cd | 632 | entry->rsp.scsi_status = SAM_STAT_CHECK_CONDITION; |
ade9e091 MC |
633 | memcpy(se_cmd->sense_buffer, lu_comm_failure_sense, |
634 | sizeof(lu_comm_failure_sense)); | |
ed97d0cd | 635 | } else if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) { |
7c9e7a6f | 636 | memcpy(se_cmd->sense_buffer, entry->rsp.sense_buffer, |
ade9e091 | 637 | TRANSPORT_SENSE_BUFFER); |
26418649 | 638 | free_data_area(udev, cmd); |
e4648b01 | 639 | } else if (se_cmd->se_cmd_flags & SCF_BIDI) { |
26418649 | 640 | /* Get Data-In buffer before clean up */ |
2bcbe7c9 | 641 | gather_data_area(udev, cmd, true); |
26418649 | 642 | free_data_area(udev, cmd); |
e4648b01 | 643 | } else if (se_cmd->data_direction == DMA_FROM_DEVICE) { |
2bcbe7c9 | 644 | gather_data_area(udev, cmd, false); |
26418649 | 645 | free_data_area(udev, cmd); |
7c9e7a6f | 646 | } else if (se_cmd->data_direction == DMA_TO_DEVICE) { |
26418649 | 647 | free_data_area(udev, cmd); |
2bc396a2 IT |
648 | } else if (se_cmd->data_direction != DMA_NONE) { |
649 | pr_warn("TCMU: data direction was %d!\n", | |
650 | se_cmd->data_direction); | |
7c9e7a6f AG |
651 | } |
652 | ||
653 | target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status); | |
654 | cmd->se_cmd = NULL; | |
655 | ||
656 | kmem_cache_free(tcmu_cmd_cache, cmd); | |
657 | } | |
658 | ||
659 | static unsigned int tcmu_handle_completions(struct tcmu_dev *udev) | |
660 | { | |
661 | struct tcmu_mailbox *mb; | |
7c9e7a6f AG |
662 | unsigned long flags; |
663 | int handled = 0; | |
664 | ||
665 | if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) { | |
666 | pr_err("ring broken, not handling completions\n"); | |
667 | return 0; | |
668 | } | |
669 | ||
670 | spin_lock_irqsave(&udev->cmdr_lock, flags); | |
671 | ||
672 | mb = udev->mb_addr; | |
673 | tcmu_flush_dcache_range(mb, sizeof(*mb)); | |
674 | ||
675 | while (udev->cmdr_last_cleaned != ACCESS_ONCE(mb->cmd_tail)) { | |
676 | ||
677 | struct tcmu_cmd_entry *entry = (void *) mb + CMDR_OFF + udev->cmdr_last_cleaned; | |
678 | struct tcmu_cmd *cmd; | |
679 | ||
680 | tcmu_flush_dcache_range(entry, sizeof(*entry)); | |
681 | ||
0ad46af8 AG |
682 | if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD) { |
683 | UPDATE_HEAD(udev->cmdr_last_cleaned, | |
684 | tcmu_hdr_get_len(entry->hdr.len_op), | |
685 | udev->cmdr_size); | |
7c9e7a6f AG |
686 | continue; |
687 | } | |
0ad46af8 | 688 | WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD); |
7c9e7a6f AG |
689 | |
690 | spin_lock(&udev->commands_lock); | |
0ad46af8 | 691 | cmd = idr_find(&udev->commands, entry->hdr.cmd_id); |
7c9e7a6f AG |
692 | if (cmd) |
693 | idr_remove(&udev->commands, cmd->cmd_id); | |
694 | spin_unlock(&udev->commands_lock); | |
695 | ||
696 | if (!cmd) { | |
697 | pr_err("cmd_id not found, ring is broken\n"); | |
698 | set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags); | |
699 | break; | |
700 | } | |
701 | ||
702 | tcmu_handle_completion(cmd, entry); | |
703 | ||
0ad46af8 AG |
704 | UPDATE_HEAD(udev->cmdr_last_cleaned, |
705 | tcmu_hdr_get_len(entry->hdr.len_op), | |
706 | udev->cmdr_size); | |
7c9e7a6f AG |
707 | |
708 | handled++; | |
709 | } | |
710 | ||
711 | if (mb->cmd_tail == mb->cmd_head) | |
712 | del_timer(&udev->timeout); /* no more pending cmds */ | |
713 | ||
714 | spin_unlock_irqrestore(&udev->cmdr_lock, flags); | |
715 | ||
716 | wake_up(&udev->wait_cmdr); | |
717 | ||
718 | return handled; | |
719 | } | |
720 | ||
721 | static int tcmu_check_expired_cmd(int id, void *p, void *data) | |
722 | { | |
723 | struct tcmu_cmd *cmd = p; | |
724 | ||
725 | if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) | |
726 | return 0; | |
727 | ||
611e2267 | 728 | if (!time_after(jiffies, cmd->deadline)) |
7c9e7a6f AG |
729 | return 0; |
730 | ||
731 | set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags); | |
ade9e091 MC |
732 | memcpy(cmd->se_cmd->sense_buffer, lu_comm_failure_sense, |
733 | sizeof(lu_comm_failure_sense)); | |
7c9e7a6f AG |
734 | target_complete_cmd(cmd->se_cmd, SAM_STAT_CHECK_CONDITION); |
735 | cmd->se_cmd = NULL; | |
736 | ||
7c9e7a6f AG |
737 | return 0; |
738 | } | |
739 | ||
740 | static void tcmu_device_timedout(unsigned long data) | |
741 | { | |
742 | struct tcmu_dev *udev = (struct tcmu_dev *)data; | |
743 | unsigned long flags; | |
744 | int handled; | |
745 | ||
746 | handled = tcmu_handle_completions(udev); | |
747 | ||
748 | pr_warn("%d completions handled from timeout\n", handled); | |
749 | ||
750 | spin_lock_irqsave(&udev->commands_lock, flags); | |
751 | idr_for_each(&udev->commands, tcmu_check_expired_cmd, NULL); | |
752 | spin_unlock_irqrestore(&udev->commands_lock, flags); | |
753 | ||
754 | /* | |
755 | * We don't need to wakeup threads on wait_cmdr since they have their | |
756 | * own timeout. | |
757 | */ | |
758 | } | |
759 | ||
760 | static int tcmu_attach_hba(struct se_hba *hba, u32 host_id) | |
761 | { | |
762 | struct tcmu_hba *tcmu_hba; | |
763 | ||
764 | tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL); | |
765 | if (!tcmu_hba) | |
766 | return -ENOMEM; | |
767 | ||
768 | tcmu_hba->host_id = host_id; | |
769 | hba->hba_ptr = tcmu_hba; | |
770 | ||
771 | return 0; | |
772 | } | |
773 | ||
774 | static void tcmu_detach_hba(struct se_hba *hba) | |
775 | { | |
776 | kfree(hba->hba_ptr); | |
777 | hba->hba_ptr = NULL; | |
778 | } | |
779 | ||
780 | static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name) | |
781 | { | |
782 | struct tcmu_dev *udev; | |
783 | ||
784 | udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL); | |
785 | if (!udev) | |
786 | return NULL; | |
787 | ||
788 | udev->name = kstrdup(name, GFP_KERNEL); | |
789 | if (!udev->name) { | |
790 | kfree(udev); | |
791 | return NULL; | |
792 | } | |
793 | ||
794 | udev->hba = hba; | |
795 | ||
796 | init_waitqueue_head(&udev->wait_cmdr); | |
797 | spin_lock_init(&udev->cmdr_lock); | |
798 | ||
799 | idr_init(&udev->commands); | |
800 | spin_lock_init(&udev->commands_lock); | |
801 | ||
802 | setup_timer(&udev->timeout, tcmu_device_timedout, | |
803 | (unsigned long)udev); | |
804 | ||
7c9e7a6f AG |
805 | return &udev->se_dev; |
806 | } | |
807 | ||
808 | static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on) | |
809 | { | |
810 | struct tcmu_dev *tcmu_dev = container_of(info, struct tcmu_dev, uio_info); | |
811 | ||
812 | tcmu_handle_completions(tcmu_dev); | |
813 | ||
814 | return 0; | |
815 | } | |
816 | ||
817 | /* | |
818 | * mmap code from uio.c. Copied here because we want to hook mmap() | |
819 | * and this stuff must come along. | |
820 | */ | |
821 | static int tcmu_find_mem_index(struct vm_area_struct *vma) | |
822 | { | |
823 | struct tcmu_dev *udev = vma->vm_private_data; | |
824 | struct uio_info *info = &udev->uio_info; | |
825 | ||
826 | if (vma->vm_pgoff < MAX_UIO_MAPS) { | |
827 | if (info->mem[vma->vm_pgoff].size == 0) | |
828 | return -1; | |
829 | return (int)vma->vm_pgoff; | |
830 | } | |
831 | return -1; | |
832 | } | |
833 | ||
834 | static int tcmu_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf) | |
835 | { | |
836 | struct tcmu_dev *udev = vma->vm_private_data; | |
837 | struct uio_info *info = &udev->uio_info; | |
838 | struct page *page; | |
839 | unsigned long offset; | |
840 | void *addr; | |
841 | ||
842 | int mi = tcmu_find_mem_index(vma); | |
843 | if (mi < 0) | |
844 | return VM_FAULT_SIGBUS; | |
845 | ||
846 | /* | |
847 | * We need to subtract mi because userspace uses offset = N*PAGE_SIZE | |
848 | * to use mem[N]. | |
849 | */ | |
850 | offset = (vmf->pgoff - mi) << PAGE_SHIFT; | |
851 | ||
852 | addr = (void *)(unsigned long)info->mem[mi].addr + offset; | |
853 | if (info->mem[mi].memtype == UIO_MEM_LOGICAL) | |
854 | page = virt_to_page(addr); | |
855 | else | |
856 | page = vmalloc_to_page(addr); | |
857 | get_page(page); | |
858 | vmf->page = page; | |
859 | return 0; | |
860 | } | |
861 | ||
862 | static const struct vm_operations_struct tcmu_vm_ops = { | |
863 | .fault = tcmu_vma_fault, | |
864 | }; | |
865 | ||
866 | static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma) | |
867 | { | |
868 | struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); | |
869 | ||
870 | vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; | |
871 | vma->vm_ops = &tcmu_vm_ops; | |
872 | ||
873 | vma->vm_private_data = udev; | |
874 | ||
875 | /* Ensure the mmap is exactly the right size */ | |
876 | if (vma_pages(vma) != (TCMU_RING_SIZE >> PAGE_SHIFT)) | |
877 | return -EINVAL; | |
878 | ||
879 | return 0; | |
880 | } | |
881 | ||
882 | static int tcmu_open(struct uio_info *info, struct inode *inode) | |
883 | { | |
884 | struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); | |
885 | ||
886 | /* O_EXCL not supported for char devs, so fake it? */ | |
887 | if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags)) | |
888 | return -EBUSY; | |
889 | ||
890 | pr_debug("open\n"); | |
891 | ||
892 | return 0; | |
893 | } | |
894 | ||
895 | static int tcmu_release(struct uio_info *info, struct inode *inode) | |
896 | { | |
897 | struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); | |
898 | ||
899 | clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags); | |
900 | ||
901 | pr_debug("close\n"); | |
902 | ||
903 | return 0; | |
904 | } | |
905 | ||
906 | static int tcmu_netlink_event(enum tcmu_genl_cmd cmd, const char *name, int minor) | |
907 | { | |
908 | struct sk_buff *skb; | |
909 | void *msg_header; | |
6e14eab9 | 910 | int ret = -ENOMEM; |
7c9e7a6f AG |
911 | |
912 | skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
913 | if (!skb) | |
6e14eab9 | 914 | return ret; |
7c9e7a6f AG |
915 | |
916 | msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd); | |
6e14eab9 NB |
917 | if (!msg_header) |
918 | goto free_skb; | |
7c9e7a6f AG |
919 | |
920 | ret = nla_put_string(skb, TCMU_ATTR_DEVICE, name); | |
6e14eab9 NB |
921 | if (ret < 0) |
922 | goto free_skb; | |
7c9e7a6f AG |
923 | |
924 | ret = nla_put_u32(skb, TCMU_ATTR_MINOR, minor); | |
6e14eab9 NB |
925 | if (ret < 0) |
926 | goto free_skb; | |
7c9e7a6f | 927 | |
053c095a | 928 | genlmsg_end(skb, msg_header); |
7c9e7a6f | 929 | |
20c08b36 | 930 | ret = genlmsg_multicast_allns(&tcmu_genl_family, skb, 0, |
7c9e7a6f AG |
931 | TCMU_MCGRP_CONFIG, GFP_KERNEL); |
932 | ||
933 | /* We don't care if no one is listening */ | |
934 | if (ret == -ESRCH) | |
935 | ret = 0; | |
936 | ||
937 | return ret; | |
6e14eab9 NB |
938 | free_skb: |
939 | nlmsg_free(skb); | |
940 | return ret; | |
7c9e7a6f AG |
941 | } |
942 | ||
943 | static int tcmu_configure_device(struct se_device *dev) | |
944 | { | |
945 | struct tcmu_dev *udev = TCMU_DEV(dev); | |
946 | struct tcmu_hba *hba = udev->hba->hba_ptr; | |
947 | struct uio_info *info; | |
948 | struct tcmu_mailbox *mb; | |
949 | size_t size; | |
950 | size_t used; | |
951 | int ret = 0; | |
952 | char *str; | |
953 | ||
954 | info = &udev->uio_info; | |
955 | ||
956 | size = snprintf(NULL, 0, "tcm-user/%u/%s/%s", hba->host_id, udev->name, | |
957 | udev->dev_config); | |
958 | size += 1; /* for \0 */ | |
959 | str = kmalloc(size, GFP_KERNEL); | |
960 | if (!str) | |
961 | return -ENOMEM; | |
962 | ||
963 | used = snprintf(str, size, "tcm-user/%u/%s", hba->host_id, udev->name); | |
964 | ||
965 | if (udev->dev_config[0]) | |
966 | snprintf(str + used, size - used, "/%s", udev->dev_config); | |
967 | ||
968 | info->name = str; | |
969 | ||
970 | udev->mb_addr = vzalloc(TCMU_RING_SIZE); | |
971 | if (!udev->mb_addr) { | |
972 | ret = -ENOMEM; | |
973 | goto err_vzalloc; | |
974 | } | |
975 | ||
976 | /* mailbox fits in first part of CMDR space */ | |
977 | udev->cmdr_size = CMDR_SIZE - CMDR_OFF; | |
978 | udev->data_off = CMDR_SIZE; | |
979 | udev->data_size = TCMU_RING_SIZE - CMDR_SIZE; | |
980 | ||
981 | mb = udev->mb_addr; | |
0ad46af8 | 982 | mb->version = TCMU_MAILBOX_VERSION; |
32c76de3 | 983 | mb->flags = TCMU_MAILBOX_FLAG_CAP_OOOC; |
7c9e7a6f AG |
984 | mb->cmdr_off = CMDR_OFF; |
985 | mb->cmdr_size = udev->cmdr_size; | |
986 | ||
987 | WARN_ON(!PAGE_ALIGNED(udev->data_off)); | |
988 | WARN_ON(udev->data_size % PAGE_SIZE); | |
26418649 | 989 | WARN_ON(udev->data_size % DATA_BLOCK_SIZE); |
7c9e7a6f | 990 | |
ac64a2ce | 991 | info->version = __stringify(TCMU_MAILBOX_VERSION); |
7c9e7a6f AG |
992 | |
993 | info->mem[0].name = "tcm-user command & data buffer"; | |
0633e123 | 994 | info->mem[0].addr = (phys_addr_t)(uintptr_t)udev->mb_addr; |
7c9e7a6f AG |
995 | info->mem[0].size = TCMU_RING_SIZE; |
996 | info->mem[0].memtype = UIO_MEM_VIRTUAL; | |
997 | ||
998 | info->irqcontrol = tcmu_irqcontrol; | |
999 | info->irq = UIO_IRQ_CUSTOM; | |
1000 | ||
1001 | info->mmap = tcmu_mmap; | |
1002 | info->open = tcmu_open; | |
1003 | info->release = tcmu_release; | |
1004 | ||
1005 | ret = uio_register_device(tcmu_root_device, info); | |
1006 | if (ret) | |
1007 | goto err_register; | |
1008 | ||
81ee28de SY |
1009 | /* User can set hw_block_size before enable the device */ |
1010 | if (dev->dev_attrib.hw_block_size == 0) | |
1011 | dev->dev_attrib.hw_block_size = 512; | |
7c9e7a6f | 1012 | /* Other attributes can be configured in userspace */ |
7c9e7a6f AG |
1013 | dev->dev_attrib.hw_max_sectors = 128; |
1014 | dev->dev_attrib.hw_queue_depth = 128; | |
1015 | ||
1016 | ret = tcmu_netlink_event(TCMU_CMD_ADDED_DEVICE, udev->uio_info.name, | |
1017 | udev->uio_info.uio_dev->minor); | |
1018 | if (ret) | |
1019 | goto err_netlink; | |
1020 | ||
1021 | return 0; | |
1022 | ||
1023 | err_netlink: | |
1024 | uio_unregister_device(&udev->uio_info); | |
1025 | err_register: | |
1026 | vfree(udev->mb_addr); | |
1027 | err_vzalloc: | |
1028 | kfree(info->name); | |
1029 | ||
1030 | return ret; | |
1031 | } | |
1032 | ||
b25c7863 | 1033 | static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd) |
7c9e7a6f | 1034 | { |
b25c7863 SY |
1035 | if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) { |
1036 | kmem_cache_free(tcmu_cmd_cache, cmd); | |
7c9e7a6f | 1037 | return 0; |
b25c7863 | 1038 | } |
7c9e7a6f AG |
1039 | return -EINVAL; |
1040 | } | |
1041 | ||
4cc987ea NB |
1042 | static void tcmu_dev_call_rcu(struct rcu_head *p) |
1043 | { | |
1044 | struct se_device *dev = container_of(p, struct se_device, rcu_head); | |
1045 | struct tcmu_dev *udev = TCMU_DEV(dev); | |
1046 | ||
1047 | kfree(udev); | |
1048 | } | |
1049 | ||
7c9e7a6f AG |
1050 | static void tcmu_free_device(struct se_device *dev) |
1051 | { | |
1052 | struct tcmu_dev *udev = TCMU_DEV(dev); | |
b25c7863 SY |
1053 | struct tcmu_cmd *cmd; |
1054 | bool all_expired = true; | |
7c9e7a6f AG |
1055 | int i; |
1056 | ||
1057 | del_timer_sync(&udev->timeout); | |
1058 | ||
1059 | vfree(udev->mb_addr); | |
1060 | ||
1061 | /* Upper layer should drain all requests before calling this */ | |
1062 | spin_lock_irq(&udev->commands_lock); | |
b25c7863 SY |
1063 | idr_for_each_entry(&udev->commands, cmd, i) { |
1064 | if (tcmu_check_and_free_pending_cmd(cmd) != 0) | |
1065 | all_expired = false; | |
1066 | } | |
7c9e7a6f AG |
1067 | idr_destroy(&udev->commands); |
1068 | spin_unlock_irq(&udev->commands_lock); | |
b25c7863 | 1069 | WARN_ON(!all_expired); |
7c9e7a6f AG |
1070 | |
1071 | /* Device was configured */ | |
1072 | if (udev->uio_info.uio_dev) { | |
1073 | tcmu_netlink_event(TCMU_CMD_REMOVED_DEVICE, udev->uio_info.name, | |
1074 | udev->uio_info.uio_dev->minor); | |
1075 | ||
1076 | uio_unregister_device(&udev->uio_info); | |
1077 | kfree(udev->uio_info.name); | |
1078 | kfree(udev->name); | |
1079 | } | |
4cc987ea | 1080 | call_rcu(&dev->rcu_head, tcmu_dev_call_rcu); |
7c9e7a6f AG |
1081 | } |
1082 | ||
1083 | enum { | |
9c1cd1b6 | 1084 | Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_err, |
7c9e7a6f AG |
1085 | }; |
1086 | ||
1087 | static match_table_t tokens = { | |
1088 | {Opt_dev_config, "dev_config=%s"}, | |
1089 | {Opt_dev_size, "dev_size=%u"}, | |
9c1cd1b6 | 1090 | {Opt_hw_block_size, "hw_block_size=%u"}, |
7c9e7a6f AG |
1091 | {Opt_err, NULL} |
1092 | }; | |
1093 | ||
1094 | static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev, | |
1095 | const char *page, ssize_t count) | |
1096 | { | |
1097 | struct tcmu_dev *udev = TCMU_DEV(dev); | |
1098 | char *orig, *ptr, *opts, *arg_p; | |
1099 | substring_t args[MAX_OPT_ARGS]; | |
1100 | int ret = 0, token; | |
9c1cd1b6 | 1101 | unsigned long tmp_ul; |
7c9e7a6f AG |
1102 | |
1103 | opts = kstrdup(page, GFP_KERNEL); | |
1104 | if (!opts) | |
1105 | return -ENOMEM; | |
1106 | ||
1107 | orig = opts; | |
1108 | ||
1109 | while ((ptr = strsep(&opts, ",\n")) != NULL) { | |
1110 | if (!*ptr) | |
1111 | continue; | |
1112 | ||
1113 | token = match_token(ptr, tokens, args); | |
1114 | switch (token) { | |
1115 | case Opt_dev_config: | |
1116 | if (match_strlcpy(udev->dev_config, &args[0], | |
1117 | TCMU_CONFIG_LEN) == 0) { | |
1118 | ret = -EINVAL; | |
1119 | break; | |
1120 | } | |
1121 | pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config); | |
1122 | break; | |
1123 | case Opt_dev_size: | |
1124 | arg_p = match_strdup(&args[0]); | |
1125 | if (!arg_p) { | |
1126 | ret = -ENOMEM; | |
1127 | break; | |
1128 | } | |
1129 | ret = kstrtoul(arg_p, 0, (unsigned long *) &udev->dev_size); | |
1130 | kfree(arg_p); | |
1131 | if (ret < 0) | |
1132 | pr_err("kstrtoul() failed for dev_size=\n"); | |
1133 | break; | |
9c1cd1b6 AG |
1134 | case Opt_hw_block_size: |
1135 | arg_p = match_strdup(&args[0]); | |
1136 | if (!arg_p) { | |
1137 | ret = -ENOMEM; | |
1138 | break; | |
1139 | } | |
1140 | ret = kstrtoul(arg_p, 0, &tmp_ul); | |
1141 | kfree(arg_p); | |
1142 | if (ret < 0) { | |
1143 | pr_err("kstrtoul() failed for hw_block_size=\n"); | |
1144 | break; | |
1145 | } | |
1146 | if (!tmp_ul) { | |
1147 | pr_err("hw_block_size must be nonzero\n"); | |
1148 | break; | |
1149 | } | |
1150 | dev->dev_attrib.hw_block_size = tmp_ul; | |
1151 | break; | |
7c9e7a6f AG |
1152 | default: |
1153 | break; | |
1154 | } | |
1155 | } | |
1156 | ||
1157 | kfree(orig); | |
1158 | return (!ret) ? count : ret; | |
1159 | } | |
1160 | ||
1161 | static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b) | |
1162 | { | |
1163 | struct tcmu_dev *udev = TCMU_DEV(dev); | |
1164 | ssize_t bl = 0; | |
1165 | ||
1166 | bl = sprintf(b + bl, "Config: %s ", | |
1167 | udev->dev_config[0] ? udev->dev_config : "NULL"); | |
8ee83a74 | 1168 | bl += sprintf(b + bl, "Size: %zu\n", udev->dev_size); |
7c9e7a6f AG |
1169 | |
1170 | return bl; | |
1171 | } | |
1172 | ||
1173 | static sector_t tcmu_get_blocks(struct se_device *dev) | |
1174 | { | |
1175 | struct tcmu_dev *udev = TCMU_DEV(dev); | |
1176 | ||
1177 | return div_u64(udev->dev_size - dev->dev_attrib.block_size, | |
1178 | dev->dev_attrib.block_size); | |
1179 | } | |
1180 | ||
7c9e7a6f | 1181 | static sense_reason_t |
9c1cd1b6 | 1182 | tcmu_parse_cdb(struct se_cmd *cmd) |
7c9e7a6f | 1183 | { |
02eb924f | 1184 | return passthrough_parse_cdb(cmd, tcmu_queue_cmd); |
7c9e7a6f AG |
1185 | } |
1186 | ||
ade9e091 MC |
1187 | static void tcmu_transport_complete(struct se_cmd *cmd, struct scatterlist *sg, |
1188 | unsigned char *sense_buffer) | |
1189 | { | |
1190 | if (cmd->scsi_status == SAM_STAT_CHECK_CONDITION) | |
1191 | /* Setting this flag will prevent target_complete_cmd from | |
1192 | * calling target_complete_failure_work, which would overwrite | |
1193 | * the sense data we already set. | |
1194 | */ | |
1195 | cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE; | |
1196 | } | |
1197 | ||
0a06d430 | 1198 | static const struct target_backend_ops tcmu_ops = { |
7c9e7a6f | 1199 | .name = "user", |
7c9e7a6f | 1200 | .owner = THIS_MODULE, |
a3541703 | 1201 | .transport_flags = TRANSPORT_FLAG_PASSTHROUGH, |
7c9e7a6f AG |
1202 | .attach_hba = tcmu_attach_hba, |
1203 | .detach_hba = tcmu_detach_hba, | |
1204 | .alloc_device = tcmu_alloc_device, | |
1205 | .configure_device = tcmu_configure_device, | |
1206 | .free_device = tcmu_free_device, | |
1207 | .parse_cdb = tcmu_parse_cdb, | |
ade9e091 | 1208 | .transport_complete = tcmu_transport_complete, |
7c9e7a6f AG |
1209 | .set_configfs_dev_params = tcmu_set_configfs_dev_params, |
1210 | .show_configfs_dev_params = tcmu_show_configfs_dev_params, | |
1211 | .get_device_type = sbc_get_device_type, | |
1212 | .get_blocks = tcmu_get_blocks, | |
5873c4d1 | 1213 | .tb_dev_attrib_attrs = passthrough_attrib_attrs, |
7c9e7a6f AG |
1214 | }; |
1215 | ||
1216 | static int __init tcmu_module_init(void) | |
1217 | { | |
1218 | int ret; | |
1219 | ||
1220 | BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0); | |
1221 | ||
1222 | tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache", | |
1223 | sizeof(struct tcmu_cmd), | |
1224 | __alignof__(struct tcmu_cmd), | |
1225 | 0, NULL); | |
1226 | if (!tcmu_cmd_cache) | |
1227 | return -ENOMEM; | |
1228 | ||
1229 | tcmu_root_device = root_device_register("tcm_user"); | |
1230 | if (IS_ERR(tcmu_root_device)) { | |
1231 | ret = PTR_ERR(tcmu_root_device); | |
1232 | goto out_free_cache; | |
1233 | } | |
1234 | ||
1235 | ret = genl_register_family(&tcmu_genl_family); | |
1236 | if (ret < 0) { | |
1237 | goto out_unreg_device; | |
1238 | } | |
1239 | ||
0a06d430 | 1240 | ret = transport_backend_register(&tcmu_ops); |
7c9e7a6f AG |
1241 | if (ret) |
1242 | goto out_unreg_genl; | |
1243 | ||
1244 | return 0; | |
1245 | ||
1246 | out_unreg_genl: | |
1247 | genl_unregister_family(&tcmu_genl_family); | |
1248 | out_unreg_device: | |
1249 | root_device_unregister(tcmu_root_device); | |
1250 | out_free_cache: | |
1251 | kmem_cache_destroy(tcmu_cmd_cache); | |
1252 | ||
1253 | return ret; | |
1254 | } | |
1255 | ||
1256 | static void __exit tcmu_module_exit(void) | |
1257 | { | |
0a06d430 | 1258 | target_backend_unregister(&tcmu_ops); |
7c9e7a6f AG |
1259 | genl_unregister_family(&tcmu_genl_family); |
1260 | root_device_unregister(tcmu_root_device); | |
1261 | kmem_cache_destroy(tcmu_cmd_cache); | |
1262 | } | |
1263 | ||
1264 | MODULE_DESCRIPTION("TCM USER subsystem plugin"); | |
1265 | MODULE_AUTHOR("Shaohua Li <shli@kernel.org>"); | |
1266 | MODULE_AUTHOR("Andy Grover <agrover@redhat.com>"); | |
1267 | MODULE_LICENSE("GPL"); | |
1268 | ||
1269 | module_init(tcmu_module_init); | |
1270 | module_exit(tcmu_module_exit); |