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