]>
Commit | Line | Data |
---|---|---|
a61127c2 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
7c9e7a6f AG |
2 | /* |
3 | * Copyright (C) 2013 Shaohua Li <shli@kernel.org> | |
4 | * Copyright (C) 2014 Red Hat, Inc. | |
f97ec7db | 5 | * Copyright (C) 2015 Arrikto, Inc. |
141685a3 | 6 | * Copyright (C) 2017 Chinamobile, Inc. |
7c9e7a6f AG |
7 | */ |
8 | ||
9 | #include <linux/spinlock.h> | |
10 | #include <linux/module.h> | |
11 | #include <linux/idr.h> | |
ba929992 | 12 | #include <linux/kernel.h> |
7c9e7a6f AG |
13 | #include <linux/timer.h> |
14 | #include <linux/parser.h> | |
5538d294 | 15 | #include <linux/vmalloc.h> |
7c9e7a6f | 16 | #include <linux/uio_driver.h> |
141685a3 | 17 | #include <linux/radix-tree.h> |
ac64a2ce | 18 | #include <linux/stringify.h> |
26418649 | 19 | #include <linux/bitops.h> |
f5045724 | 20 | #include <linux/highmem.h> |
7d7a7435 | 21 | #include <linux/configfs.h> |
b6df4b79 | 22 | #include <linux/mutex.h> |
9972cebb | 23 | #include <linux/workqueue.h> |
7c9e7a6f | 24 | #include <net/genetlink.h> |
ba929992 BVA |
25 | #include <scsi/scsi_common.h> |
26 | #include <scsi/scsi_proto.h> | |
7c9e7a6f AG |
27 | #include <target/target_core_base.h> |
28 | #include <target/target_core_fabric.h> | |
29 | #include <target/target_core_backend.h> | |
e9f720d6 | 30 | |
7c9e7a6f AG |
31 | #include <linux/target_core_user.h> |
32 | ||
572ccdab RD |
33 | /** |
34 | * DOC: Userspace I/O | |
35 | * Userspace I/O | |
36 | * ------------- | |
37 | * | |
7c9e7a6f AG |
38 | * Define a shared-memory interface for LIO to pass SCSI commands and |
39 | * data to userspace for processing. This is to allow backends that | |
40 | * are too complex for in-kernel support to be possible. | |
41 | * | |
42 | * It uses the UIO framework to do a lot of the device-creation and | |
43 | * introspection work for us. | |
44 | * | |
45 | * See the .h file for how the ring is laid out. Note that while the | |
46 | * command ring is defined, the particulars of the data area are | |
47 | * not. Offset values in the command entry point to other locations | |
572ccdab | 48 | * internal to the mmap-ed area. There is separate space outside the |
7c9e7a6f AG |
49 | * command ring for data buffers. This leaves maximum flexibility for |
50 | * moving buffer allocations, or even page flipping or other | |
51 | * allocation techniques, without altering the command ring layout. | |
52 | * | |
53 | * SECURITY: | |
54 | * The user process must be assumed to be malicious. There's no way to | |
55 | * prevent it breaking the command ring protocol if it wants, but in | |
56 | * order to prevent other issues we must only ever read *data* from | |
57 | * the shared memory area, not offsets or sizes. This applies to | |
58 | * command ring entries as well as the mailbox. Extra code needed for | |
59 | * this may have a 'UAM' comment. | |
60 | */ | |
61 | ||
7c9e7a6f AG |
62 | #define TCMU_TIME_OUT (30 * MSEC_PER_SEC) |
63 | ||
b6df4b79 XL |
64 | /* For cmd area, the size is fixed 8MB */ |
65 | #define CMDR_SIZE (8 * 1024 * 1024) | |
26418649 | 66 | |
b6df4b79 XL |
67 | /* |
68 | * For data area, the block size is PAGE_SIZE and | |
69 | * the total size is 256K * PAGE_SIZE. | |
70 | */ | |
71 | #define DATA_BLOCK_SIZE PAGE_SIZE | |
80eb8761 MC |
72 | #define DATA_BLOCK_SHIFT PAGE_SHIFT |
73 | #define DATA_BLOCK_BITS_DEF (256 * 1024) | |
7c9e7a6f | 74 | |
80eb8761 MC |
75 | #define TCMU_MBS_TO_BLOCKS(_mbs) (_mbs << (20 - DATA_BLOCK_SHIFT)) |
76 | #define TCMU_BLOCKS_TO_MBS(_blocks) (_blocks >> (20 - DATA_BLOCK_SHIFT)) | |
77 | ||
af1dd7ff MC |
78 | /* |
79 | * Default number of global data blocks(512K * PAGE_SIZE) | |
80 | * when the unmap thread will be started. | |
81 | */ | |
80eb8761 | 82 | #define TCMU_GLOBAL_MAX_BLOCKS_DEF (512 * 1024) |
b6df4b79 | 83 | |
b3af66e2 | 84 | static u8 tcmu_kern_cmd_reply_supported; |
bdaeedc1 | 85 | static u8 tcmu_netlink_blocked; |
b3af66e2 | 86 | |
7c9e7a6f AG |
87 | static struct device *tcmu_root_device; |
88 | ||
89 | struct tcmu_hba { | |
90 | u32 host_id; | |
91 | }; | |
92 | ||
7c9e7a6f AG |
93 | #define TCMU_CONFIG_LEN 256 |
94 | ||
3228691f MC |
95 | static DEFINE_MUTEX(tcmu_nl_cmd_mutex); |
96 | static LIST_HEAD(tcmu_nl_cmd_list); | |
97 | ||
98 | struct tcmu_dev; | |
99 | ||
b3af66e2 MC |
100 | struct tcmu_nl_cmd { |
101 | /* wake up thread waiting for reply */ | |
102 | struct completion complete; | |
3228691f MC |
103 | struct list_head nl_list; |
104 | struct tcmu_dev *udev; | |
b3af66e2 MC |
105 | int cmd; |
106 | int status; | |
107 | }; | |
108 | ||
7c9e7a6f | 109 | struct tcmu_dev { |
b6df4b79 | 110 | struct list_head node; |
f3cdbe39 | 111 | struct kref kref; |
af1dd7ff | 112 | |
7c9e7a6f AG |
113 | struct se_device se_dev; |
114 | ||
115 | char *name; | |
116 | struct se_hba *hba; | |
117 | ||
118 | #define TCMU_DEV_BIT_OPEN 0 | |
119 | #define TCMU_DEV_BIT_BROKEN 1 | |
892782ca | 120 | #define TCMU_DEV_BIT_BLOCKED 2 |
7c9e7a6f | 121 | unsigned long flags; |
7c9e7a6f AG |
122 | |
123 | struct uio_info uio_info; | |
124 | ||
b6df4b79 XL |
125 | struct inode *inode; |
126 | ||
7c9e7a6f | 127 | struct tcmu_mailbox *mb_addr; |
0e0d7526 | 128 | uint64_t dev_size; |
7c9e7a6f AG |
129 | u32 cmdr_size; |
130 | u32 cmdr_last_cleaned; | |
3d9b9555 | 131 | /* Offset of data area from start of mb */ |
26418649 | 132 | /* Must add data_off and mb_addr to get the address */ |
7c9e7a6f AG |
133 | size_t data_off; |
134 | size_t data_size; | |
80eb8761 MC |
135 | uint32_t max_blocks; |
136 | size_t ring_size; | |
26418649 | 137 | |
b6df4b79 | 138 | struct mutex cmdr_lock; |
a94a2572 | 139 | struct list_head qfull_queue; |
7c9e7a6f | 140 | |
141685a3 | 141 | uint32_t dbi_max; |
b6df4b79 | 142 | uint32_t dbi_thresh; |
80eb8761 | 143 | unsigned long *data_bitmap; |
141685a3 XL |
144 | struct radix_tree_root data_blocks; |
145 | ||
7c9e7a6f | 146 | struct idr commands; |
7c9e7a6f | 147 | |
9103575a | 148 | struct timer_list cmd_timer; |
af980e46 | 149 | unsigned int cmd_time_out; |
a94a2572 | 150 | struct list_head inflight_queue; |
9103575a MC |
151 | |
152 | struct timer_list qfull_timer; | |
153 | int qfull_time_out; | |
154 | ||
488ebe4c | 155 | struct list_head timedout_entry; |
7c9e7a6f | 156 | |
b3af66e2 | 157 | struct tcmu_nl_cmd curr_nl_cmd; |
b3af66e2 | 158 | |
7c9e7a6f | 159 | char dev_config[TCMU_CONFIG_LEN]; |
b849b456 KN |
160 | |
161 | int nl_reply_supported; | |
7c9e7a6f AG |
162 | }; |
163 | ||
164 | #define TCMU_DEV(_se_dev) container_of(_se_dev, struct tcmu_dev, se_dev) | |
165 | ||
166 | #define CMDR_OFF sizeof(struct tcmu_mailbox) | |
167 | ||
168 | struct tcmu_cmd { | |
169 | struct se_cmd *se_cmd; | |
170 | struct tcmu_dev *tcmu_dev; | |
a94a2572 | 171 | struct list_head queue_entry; |
7c9e7a6f AG |
172 | |
173 | uint16_t cmd_id; | |
174 | ||
26418649 | 175 | /* Can't use se_cmd when cleaning up expired cmds, because if |
7c9e7a6f | 176 | cmd has been completed then accessing se_cmd is off limits */ |
141685a3 XL |
177 | uint32_t dbi_cnt; |
178 | uint32_t dbi_cur; | |
179 | uint32_t *dbi; | |
7c9e7a6f AG |
180 | |
181 | unsigned long deadline; | |
182 | ||
183 | #define TCMU_CMD_BIT_EXPIRED 0 | |
a94a2572 | 184 | #define TCMU_CMD_BIT_INFLIGHT 1 |
7c9e7a6f AG |
185 | unsigned long flags; |
186 | }; | |
af1dd7ff MC |
187 | /* |
188 | * To avoid dead lock the mutex lock order should always be: | |
189 | * | |
190 | * mutex_lock(&root_udev_mutex); | |
191 | * ... | |
192 | * mutex_lock(&tcmu_dev->cmdr_lock); | |
193 | * mutex_unlock(&tcmu_dev->cmdr_lock); | |
194 | * ... | |
195 | * mutex_unlock(&root_udev_mutex); | |
196 | */ | |
b6df4b79 XL |
197 | static DEFINE_MUTEX(root_udev_mutex); |
198 | static LIST_HEAD(root_udev); | |
199 | ||
488ebe4c MC |
200 | static DEFINE_SPINLOCK(timed_out_udevs_lock); |
201 | static LIST_HEAD(timed_out_udevs); | |
202 | ||
80eb8761 MC |
203 | static struct kmem_cache *tcmu_cmd_cache; |
204 | ||
b6df4b79 | 205 | static atomic_t global_db_count = ATOMIC_INIT(0); |
af1dd7ff | 206 | static struct delayed_work tcmu_unmap_work; |
80eb8761 | 207 | static int tcmu_global_max_blocks = TCMU_GLOBAL_MAX_BLOCKS_DEF; |
b6df4b79 | 208 | |
80eb8761 MC |
209 | static int tcmu_set_global_max_data_area(const char *str, |
210 | const struct kernel_param *kp) | |
211 | { | |
212 | int ret, max_area_mb; | |
213 | ||
214 | ret = kstrtoint(str, 10, &max_area_mb); | |
215 | if (ret) | |
216 | return -EINVAL; | |
217 | ||
218 | if (max_area_mb <= 0) { | |
219 | pr_err("global_max_data_area must be larger than 0.\n"); | |
220 | return -EINVAL; | |
221 | } | |
222 | ||
223 | tcmu_global_max_blocks = TCMU_MBS_TO_BLOCKS(max_area_mb); | |
224 | if (atomic_read(&global_db_count) > tcmu_global_max_blocks) | |
225 | schedule_delayed_work(&tcmu_unmap_work, 0); | |
226 | else | |
227 | cancel_delayed_work_sync(&tcmu_unmap_work); | |
228 | ||
229 | return 0; | |
230 | } | |
231 | ||
232 | static int tcmu_get_global_max_data_area(char *buffer, | |
233 | const struct kernel_param *kp) | |
234 | { | |
235 | return sprintf(buffer, "%d", TCMU_BLOCKS_TO_MBS(tcmu_global_max_blocks)); | |
236 | } | |
237 | ||
238 | static const struct kernel_param_ops tcmu_global_max_data_area_op = { | |
239 | .set = tcmu_set_global_max_data_area, | |
240 | .get = tcmu_get_global_max_data_area, | |
241 | }; | |
242 | ||
243 | module_param_cb(global_max_data_area_mb, &tcmu_global_max_data_area_op, NULL, | |
244 | S_IWUSR | S_IRUGO); | |
245 | MODULE_PARM_DESC(global_max_data_area_mb, | |
246 | "Max MBs allowed to be allocated to all the tcmu device's " | |
247 | "data areas."); | |
7c9e7a6f | 248 | |
bdaeedc1 MC |
249 | static int tcmu_get_block_netlink(char *buffer, |
250 | const struct kernel_param *kp) | |
251 | { | |
252 | return sprintf(buffer, "%s\n", tcmu_netlink_blocked ? | |
253 | "blocked" : "unblocked"); | |
254 | } | |
255 | ||
256 | static int tcmu_set_block_netlink(const char *str, | |
257 | const struct kernel_param *kp) | |
258 | { | |
259 | int ret; | |
260 | u8 val; | |
261 | ||
262 | ret = kstrtou8(str, 0, &val); | |
263 | if (ret < 0) | |
264 | return ret; | |
265 | ||
266 | if (val > 1) { | |
267 | pr_err("Invalid block netlink value %u\n", val); | |
268 | return -EINVAL; | |
269 | } | |
270 | ||
271 | tcmu_netlink_blocked = val; | |
272 | return 0; | |
273 | } | |
274 | ||
275 | static const struct kernel_param_ops tcmu_block_netlink_op = { | |
276 | .set = tcmu_set_block_netlink, | |
277 | .get = tcmu_get_block_netlink, | |
278 | }; | |
279 | ||
280 | module_param_cb(block_netlink, &tcmu_block_netlink_op, NULL, S_IWUSR | S_IRUGO); | |
281 | MODULE_PARM_DESC(block_netlink, "Block new netlink commands."); | |
282 | ||
283 | static int tcmu_fail_netlink_cmd(struct tcmu_nl_cmd *nl_cmd) | |
284 | { | |
285 | struct tcmu_dev *udev = nl_cmd->udev; | |
286 | ||
287 | if (!tcmu_netlink_blocked) { | |
288 | pr_err("Could not reset device's netlink interface. Netlink is not blocked.\n"); | |
289 | return -EBUSY; | |
290 | } | |
291 | ||
292 | if (nl_cmd->cmd != TCMU_CMD_UNSPEC) { | |
293 | pr_debug("Aborting nl cmd %d on %s\n", nl_cmd->cmd, udev->name); | |
294 | nl_cmd->status = -EINTR; | |
295 | list_del(&nl_cmd->nl_list); | |
296 | complete(&nl_cmd->complete); | |
297 | } | |
298 | return 0; | |
299 | } | |
300 | ||
301 | static int tcmu_set_reset_netlink(const char *str, | |
302 | const struct kernel_param *kp) | |
303 | { | |
304 | struct tcmu_nl_cmd *nl_cmd, *tmp_cmd; | |
305 | int ret; | |
306 | u8 val; | |
307 | ||
308 | ret = kstrtou8(str, 0, &val); | |
309 | if (ret < 0) | |
310 | return ret; | |
311 | ||
312 | if (val != 1) { | |
313 | pr_err("Invalid reset netlink value %u\n", val); | |
314 | return -EINVAL; | |
315 | } | |
316 | ||
317 | mutex_lock(&tcmu_nl_cmd_mutex); | |
318 | list_for_each_entry_safe(nl_cmd, tmp_cmd, &tcmu_nl_cmd_list, nl_list) { | |
319 | ret = tcmu_fail_netlink_cmd(nl_cmd); | |
320 | if (ret) | |
321 | break; | |
322 | } | |
323 | mutex_unlock(&tcmu_nl_cmd_mutex); | |
324 | ||
325 | return ret; | |
326 | } | |
327 | ||
328 | static const struct kernel_param_ops tcmu_reset_netlink_op = { | |
329 | .set = tcmu_set_reset_netlink, | |
330 | }; | |
331 | ||
332 | module_param_cb(reset_netlink, &tcmu_reset_netlink_op, NULL, S_IWUSR); | |
333 | MODULE_PARM_DESC(reset_netlink, "Reset netlink commands."); | |
334 | ||
7c9e7a6f AG |
335 | /* multicast group */ |
336 | enum tcmu_multicast_groups { | |
337 | TCMU_MCGRP_CONFIG, | |
338 | }; | |
339 | ||
340 | static const struct genl_multicast_group tcmu_mcgrps[] = { | |
341 | [TCMU_MCGRP_CONFIG] = { .name = "config", }, | |
342 | }; | |
343 | ||
b3af66e2 MC |
344 | static struct nla_policy tcmu_attr_policy[TCMU_ATTR_MAX+1] = { |
345 | [TCMU_ATTR_DEVICE] = { .type = NLA_STRING }, | |
346 | [TCMU_ATTR_MINOR] = { .type = NLA_U32 }, | |
347 | [TCMU_ATTR_CMD_STATUS] = { .type = NLA_S32 }, | |
348 | [TCMU_ATTR_DEVICE_ID] = { .type = NLA_U32 }, | |
349 | [TCMU_ATTR_SUPP_KERN_CMD_REPLY] = { .type = NLA_U8 }, | |
350 | }; | |
351 | ||
352 | static int tcmu_genl_cmd_done(struct genl_info *info, int completed_cmd) | |
353 | { | |
3228691f | 354 | struct tcmu_dev *udev = NULL; |
b3af66e2 MC |
355 | struct tcmu_nl_cmd *nl_cmd; |
356 | int dev_id, rc, ret = 0; | |
b3af66e2 MC |
357 | |
358 | if (!info->attrs[TCMU_ATTR_CMD_STATUS] || | |
359 | !info->attrs[TCMU_ATTR_DEVICE_ID]) { | |
360 | printk(KERN_ERR "TCMU_ATTR_CMD_STATUS or TCMU_ATTR_DEVICE_ID not set, doing nothing\n"); | |
9554c1be | 361 | return -EINVAL; |
b3af66e2 MC |
362 | } |
363 | ||
364 | dev_id = nla_get_u32(info->attrs[TCMU_ATTR_DEVICE_ID]); | |
365 | rc = nla_get_s32(info->attrs[TCMU_ATTR_CMD_STATUS]); | |
366 | ||
3228691f MC |
367 | mutex_lock(&tcmu_nl_cmd_mutex); |
368 | list_for_each_entry(nl_cmd, &tcmu_nl_cmd_list, nl_list) { | |
369 | if (nl_cmd->udev->se_dev.dev_index == dev_id) { | |
370 | udev = nl_cmd->udev; | |
371 | break; | |
372 | } | |
b3af66e2 | 373 | } |
b3af66e2 | 374 | |
3228691f | 375 | if (!udev) { |
0c218e16 | 376 | pr_err("tcmu nl cmd %u/%d completion could not find device with dev id %u.\n", |
3228691f MC |
377 | completed_cmd, rc, dev_id); |
378 | ret = -ENODEV; | |
379 | goto unlock; | |
380 | } | |
381 | list_del(&nl_cmd->nl_list); | |
b3af66e2 | 382 | |
bdaeedc1 MC |
383 | pr_debug("%s genl cmd done got id %d curr %d done %d rc %d stat %d\n", |
384 | udev->name, dev_id, nl_cmd->cmd, completed_cmd, rc, | |
385 | nl_cmd->status); | |
b3af66e2 MC |
386 | |
387 | if (nl_cmd->cmd != completed_cmd) { | |
3228691f MC |
388 | pr_err("Mismatched commands on %s (Expecting reply for %d. Current %d).\n", |
389 | udev->name, completed_cmd, nl_cmd->cmd); | |
b3af66e2 | 390 | ret = -EINVAL; |
3228691f | 391 | goto unlock; |
b3af66e2 MC |
392 | } |
393 | ||
3228691f MC |
394 | nl_cmd->status = rc; |
395 | complete(&nl_cmd->complete); | |
396 | unlock: | |
397 | mutex_unlock(&tcmu_nl_cmd_mutex); | |
b3af66e2 MC |
398 | return ret; |
399 | } | |
400 | ||
401 | static int tcmu_genl_rm_dev_done(struct sk_buff *skb, struct genl_info *info) | |
402 | { | |
403 | return tcmu_genl_cmd_done(info, TCMU_CMD_REMOVED_DEVICE); | |
404 | } | |
405 | ||
406 | static int tcmu_genl_add_dev_done(struct sk_buff *skb, struct genl_info *info) | |
407 | { | |
408 | return tcmu_genl_cmd_done(info, TCMU_CMD_ADDED_DEVICE); | |
409 | } | |
410 | ||
411 | static int tcmu_genl_reconfig_dev_done(struct sk_buff *skb, | |
412 | struct genl_info *info) | |
413 | { | |
414 | return tcmu_genl_cmd_done(info, TCMU_CMD_RECONFIG_DEVICE); | |
415 | } | |
416 | ||
417 | static int tcmu_genl_set_features(struct sk_buff *skb, struct genl_info *info) | |
418 | { | |
419 | if (info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]) { | |
420 | tcmu_kern_cmd_reply_supported = | |
421 | nla_get_u8(info->attrs[TCMU_ATTR_SUPP_KERN_CMD_REPLY]); | |
422 | printk(KERN_INFO "tcmu daemon: command reply support %u.\n", | |
423 | tcmu_kern_cmd_reply_supported); | |
424 | } | |
425 | ||
426 | return 0; | |
427 | } | |
428 | ||
429 | static const struct genl_ops tcmu_genl_ops[] = { | |
430 | { | |
431 | .cmd = TCMU_CMD_SET_FEATURES, | |
ef6243ac | 432 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
b3af66e2 | 433 | .flags = GENL_ADMIN_PERM, |
b3af66e2 MC |
434 | .doit = tcmu_genl_set_features, |
435 | }, | |
436 | { | |
437 | .cmd = TCMU_CMD_ADDED_DEVICE_DONE, | |
ef6243ac | 438 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
b3af66e2 | 439 | .flags = GENL_ADMIN_PERM, |
b3af66e2 MC |
440 | .doit = tcmu_genl_add_dev_done, |
441 | }, | |
442 | { | |
443 | .cmd = TCMU_CMD_REMOVED_DEVICE_DONE, | |
ef6243ac | 444 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
b3af66e2 | 445 | .flags = GENL_ADMIN_PERM, |
b3af66e2 MC |
446 | .doit = tcmu_genl_rm_dev_done, |
447 | }, | |
448 | { | |
449 | .cmd = TCMU_CMD_RECONFIG_DEVICE_DONE, | |
ef6243ac | 450 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
b3af66e2 | 451 | .flags = GENL_ADMIN_PERM, |
b3af66e2 MC |
452 | .doit = tcmu_genl_reconfig_dev_done, |
453 | }, | |
454 | }; | |
455 | ||
7c9e7a6f | 456 | /* Our generic netlink family */ |
56989f6d | 457 | static struct genl_family tcmu_genl_family __ro_after_init = { |
489111e5 | 458 | .module = THIS_MODULE, |
7c9e7a6f AG |
459 | .hdrsize = 0, |
460 | .name = "TCM-USER", | |
b3af66e2 | 461 | .version = 2, |
7c9e7a6f | 462 | .maxattr = TCMU_ATTR_MAX, |
3b0f31f2 | 463 | .policy = tcmu_attr_policy, |
7c9e7a6f AG |
464 | .mcgrps = tcmu_mcgrps, |
465 | .n_mcgrps = ARRAY_SIZE(tcmu_mcgrps), | |
20c08b36 | 466 | .netnsok = true, |
b3af66e2 MC |
467 | .ops = tcmu_genl_ops, |
468 | .n_ops = ARRAY_SIZE(tcmu_genl_ops), | |
7c9e7a6f AG |
469 | }; |
470 | ||
141685a3 XL |
471 | #define tcmu_cmd_set_dbi_cur(cmd, index) ((cmd)->dbi_cur = (index)) |
472 | #define tcmu_cmd_reset_dbi_cur(cmd) tcmu_cmd_set_dbi_cur(cmd, 0) | |
473 | #define tcmu_cmd_set_dbi(cmd, index) ((cmd)->dbi[(cmd)->dbi_cur++] = (index)) | |
474 | #define tcmu_cmd_get_dbi(cmd) ((cmd)->dbi[(cmd)->dbi_cur++]) | |
475 | ||
b6df4b79 | 476 | static void tcmu_cmd_free_data(struct tcmu_cmd *tcmu_cmd, uint32_t len) |
141685a3 XL |
477 | { |
478 | struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; | |
479 | uint32_t i; | |
480 | ||
b6df4b79 | 481 | for (i = 0; i < len; i++) |
141685a3 XL |
482 | clear_bit(tcmu_cmd->dbi[i], udev->data_bitmap); |
483 | } | |
484 | ||
b6df4b79 XL |
485 | static inline bool tcmu_get_empty_block(struct tcmu_dev *udev, |
486 | struct tcmu_cmd *tcmu_cmd) | |
141685a3 | 487 | { |
b6df4b79 XL |
488 | struct page *page; |
489 | int ret, dbi; | |
141685a3 | 490 | |
b6df4b79 XL |
491 | dbi = find_first_zero_bit(udev->data_bitmap, udev->dbi_thresh); |
492 | if (dbi == udev->dbi_thresh) | |
493 | return false; | |
141685a3 | 494 | |
b6df4b79 XL |
495 | page = radix_tree_lookup(&udev->data_blocks, dbi); |
496 | if (!page) { | |
b6df4b79 | 497 | if (atomic_add_return(1, &global_db_count) > |
80eb8761 | 498 | tcmu_global_max_blocks) |
af1dd7ff | 499 | schedule_delayed_work(&tcmu_unmap_work, 0); |
141685a3 | 500 | |
b6df4b79 | 501 | /* try to get new page from the mm */ |
0eccce86 | 502 | page = alloc_page(GFP_NOIO); |
b6df4b79 | 503 | if (!page) |
daf78c30 | 504 | goto err_alloc; |
b6df4b79 XL |
505 | |
506 | ret = radix_tree_insert(&udev->data_blocks, dbi, page); | |
daf78c30 XL |
507 | if (ret) |
508 | goto err_insert; | |
141685a3 XL |
509 | } |
510 | ||
b6df4b79 XL |
511 | if (dbi > udev->dbi_max) |
512 | udev->dbi_max = dbi; | |
513 | ||
514 | set_bit(dbi, udev->data_bitmap); | |
515 | tcmu_cmd_set_dbi(tcmu_cmd, dbi); | |
516 | ||
517 | return true; | |
daf78c30 XL |
518 | err_insert: |
519 | __free_page(page); | |
520 | err_alloc: | |
521 | atomic_dec(&global_db_count); | |
522 | return false; | |
141685a3 XL |
523 | } |
524 | ||
b6df4b79 XL |
525 | static bool tcmu_get_empty_blocks(struct tcmu_dev *udev, |
526 | struct tcmu_cmd *tcmu_cmd) | |
527 | { | |
528 | int i; | |
529 | ||
b6df4b79 XL |
530 | for (i = tcmu_cmd->dbi_cur; i < tcmu_cmd->dbi_cnt; i++) { |
531 | if (!tcmu_get_empty_block(udev, tcmu_cmd)) | |
af1dd7ff | 532 | return false; |
b6df4b79 XL |
533 | } |
534 | return true; | |
b6df4b79 XL |
535 | } |
536 | ||
537 | static inline struct page * | |
538 | tcmu_get_block_page(struct tcmu_dev *udev, uint32_t dbi) | |
141685a3 XL |
539 | { |
540 | return radix_tree_lookup(&udev->data_blocks, dbi); | |
541 | } | |
542 | ||
543 | static inline void tcmu_free_cmd(struct tcmu_cmd *tcmu_cmd) | |
544 | { | |
545 | kfree(tcmu_cmd->dbi); | |
546 | kmem_cache_free(tcmu_cmd_cache, tcmu_cmd); | |
547 | } | |
548 | ||
549 | static inline size_t tcmu_cmd_get_data_length(struct tcmu_cmd *tcmu_cmd) | |
550 | { | |
551 | struct se_cmd *se_cmd = tcmu_cmd->se_cmd; | |
552 | size_t data_length = round_up(se_cmd->data_length, DATA_BLOCK_SIZE); | |
553 | ||
554 | if (se_cmd->se_cmd_flags & SCF_BIDI) { | |
555 | BUG_ON(!(se_cmd->t_bidi_data_sg && se_cmd->t_bidi_data_nents)); | |
556 | data_length += round_up(se_cmd->t_bidi_data_sg->length, | |
557 | DATA_BLOCK_SIZE); | |
558 | } | |
559 | ||
560 | return data_length; | |
561 | } | |
562 | ||
563 | static inline uint32_t tcmu_cmd_get_block_cnt(struct tcmu_cmd *tcmu_cmd) | |
564 | { | |
565 | size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd); | |
566 | ||
567 | return data_length / DATA_BLOCK_SIZE; | |
568 | } | |
569 | ||
7c9e7a6f AG |
570 | static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd) |
571 | { | |
572 | struct se_device *se_dev = se_cmd->se_dev; | |
573 | struct tcmu_dev *udev = TCMU_DEV(se_dev); | |
574 | struct tcmu_cmd *tcmu_cmd; | |
7c9e7a6f | 575 | |
0eccce86 | 576 | tcmu_cmd = kmem_cache_zalloc(tcmu_cmd_cache, GFP_NOIO); |
7c9e7a6f AG |
577 | if (!tcmu_cmd) |
578 | return NULL; | |
579 | ||
a94a2572 | 580 | INIT_LIST_HEAD(&tcmu_cmd->queue_entry); |
7c9e7a6f AG |
581 | tcmu_cmd->se_cmd = se_cmd; |
582 | tcmu_cmd->tcmu_dev = udev; | |
7c9e7a6f | 583 | |
141685a3 XL |
584 | tcmu_cmd_reset_dbi_cur(tcmu_cmd); |
585 | tcmu_cmd->dbi_cnt = tcmu_cmd_get_block_cnt(tcmu_cmd); | |
586 | tcmu_cmd->dbi = kcalloc(tcmu_cmd->dbi_cnt, sizeof(uint32_t), | |
0eccce86 | 587 | GFP_NOIO); |
141685a3 XL |
588 | if (!tcmu_cmd->dbi) { |
589 | kmem_cache_free(tcmu_cmd_cache, tcmu_cmd); | |
590 | return NULL; | |
591 | } | |
592 | ||
7c9e7a6f AG |
593 | return tcmu_cmd; |
594 | } | |
595 | ||
596 | static inline void tcmu_flush_dcache_range(void *vaddr, size_t size) | |
597 | { | |
b75d8063 | 598 | unsigned long offset = offset_in_page(vaddr); |
26d2b310 | 599 | void *start = vaddr - offset; |
7c9e7a6f AG |
600 | |
601 | size = round_up(size+offset, PAGE_SIZE); | |
7c9e7a6f AG |
602 | |
603 | while (size) { | |
26d2b310 | 604 | flush_dcache_page(virt_to_page(start)); |
605 | start += PAGE_SIZE; | |
7c9e7a6f AG |
606 | size -= PAGE_SIZE; |
607 | } | |
608 | } | |
609 | ||
610 | /* | |
611 | * Some ring helper functions. We don't assume size is a power of 2 so | |
612 | * we can't use circ_buf.h. | |
613 | */ | |
614 | static inline size_t spc_used(size_t head, size_t tail, size_t size) | |
615 | { | |
616 | int diff = head - tail; | |
617 | ||
618 | if (diff >= 0) | |
619 | return diff; | |
620 | else | |
621 | return size + diff; | |
622 | } | |
623 | ||
624 | static inline size_t spc_free(size_t head, size_t tail, size_t size) | |
625 | { | |
626 | /* Keep 1 byte unused or we can't tell full from empty */ | |
627 | return (size - spc_used(head, tail, size) - 1); | |
628 | } | |
629 | ||
630 | static inline size_t head_to_end(size_t head, size_t size) | |
631 | { | |
632 | return size - head; | |
633 | } | |
634 | ||
3e609135 | 635 | static inline void new_iov(struct iovec **iov, int *iov_cnt) |
f1dbd087 SY |
636 | { |
637 | struct iovec *iovec; | |
638 | ||
639 | if (*iov_cnt != 0) | |
640 | (*iov)++; | |
641 | (*iov_cnt)++; | |
642 | ||
643 | iovec = *iov; | |
644 | memset(iovec, 0, sizeof(struct iovec)); | |
f1dbd087 SY |
645 | } |
646 | ||
7c9e7a6f AG |
647 | #define UPDATE_HEAD(head, used, size) smp_store_release(&head, ((head % size) + used) % size) |
648 | ||
26418649 | 649 | /* offset is relative to mb_addr */ |
141685a3 XL |
650 | static inline size_t get_block_offset_user(struct tcmu_dev *dev, |
651 | int dbi, int remaining) | |
26418649 | 652 | { |
141685a3 | 653 | return dev->data_off + dbi * DATA_BLOCK_SIZE + |
26418649 SY |
654 | DATA_BLOCK_SIZE - remaining; |
655 | } | |
656 | ||
daf78c30 | 657 | static inline size_t iov_tail(struct iovec *iov) |
26418649 SY |
658 | { |
659 | return (size_t)iov->iov_base + iov->iov_len; | |
660 | } | |
661 | ||
1a1fc0b8 | 662 | static void scatter_data_area(struct tcmu_dev *udev, |
141685a3 XL |
663 | struct tcmu_cmd *tcmu_cmd, struct scatterlist *data_sg, |
664 | unsigned int data_nents, struct iovec **iov, | |
665 | int *iov_cnt, bool copy_data) | |
f97ec7db | 666 | { |
141685a3 | 667 | int i, dbi; |
26418649 | 668 | int block_remaining = 0; |
141685a3 XL |
669 | void *from, *to = NULL; |
670 | size_t copy_bytes, to_offset, offset; | |
f97ec7db | 671 | struct scatterlist *sg; |
b6df4b79 | 672 | struct page *page; |
f97ec7db IT |
673 | |
674 | for_each_sg(data_sg, sg, data_nents, i) { | |
26418649 | 675 | int sg_remaining = sg->length; |
f97ec7db | 676 | from = kmap_atomic(sg_page(sg)) + sg->offset; |
26418649 SY |
677 | while (sg_remaining > 0) { |
678 | if (block_remaining == 0) { | |
b6df4b79 XL |
679 | if (to) |
680 | kunmap_atomic(to); | |
681 | ||
26418649 | 682 | block_remaining = DATA_BLOCK_SIZE; |
b6df4b79 XL |
683 | dbi = tcmu_cmd_get_dbi(tcmu_cmd); |
684 | page = tcmu_get_block_page(udev, dbi); | |
685 | to = kmap_atomic(page); | |
26418649 | 686 | } |
141685a3 | 687 | |
3e609135 XL |
688 | /* |
689 | * Covert to virtual offset of the ring data area. | |
690 | */ | |
141685a3 | 691 | to_offset = get_block_offset_user(udev, dbi, |
26418649 | 692 | block_remaining); |
141685a3 | 693 | |
3e609135 XL |
694 | /* |
695 | * The following code will gather and map the blocks | |
696 | * to the same iovec when the blocks are all next to | |
697 | * each other. | |
698 | */ | |
699 | copy_bytes = min_t(size_t, sg_remaining, | |
700 | block_remaining); | |
26418649 | 701 | if (*iov_cnt != 0 && |
daf78c30 | 702 | to_offset == iov_tail(*iov)) { |
3e609135 XL |
703 | /* |
704 | * Will append to the current iovec, because | |
705 | * the current block page is next to the | |
706 | * previous one. | |
707 | */ | |
26418649 SY |
708 | (*iov)->iov_len += copy_bytes; |
709 | } else { | |
3e609135 XL |
710 | /* |
711 | * Will allocate a new iovec because we are | |
712 | * first time here or the current block page | |
713 | * is not next to the previous one. | |
714 | */ | |
715 | new_iov(iov, iov_cnt); | |
141685a3 | 716 | (*iov)->iov_base = (void __user *)to_offset; |
26418649 SY |
717 | (*iov)->iov_len = copy_bytes; |
718 | } | |
3e609135 | 719 | |
f97ec7db | 720 | if (copy_data) { |
c542942c XL |
721 | offset = DATA_BLOCK_SIZE - block_remaining; |
722 | memcpy(to + offset, | |
723 | from + sg->length - sg_remaining, | |
724 | copy_bytes); | |
f97ec7db IT |
725 | tcmu_flush_dcache_range(to, copy_bytes); |
726 | } | |
3e609135 | 727 | |
26418649 SY |
728 | sg_remaining -= copy_bytes; |
729 | block_remaining -= copy_bytes; | |
f97ec7db | 730 | } |
e2e21bd8 | 731 | kunmap_atomic(from - sg->offset); |
f97ec7db | 732 | } |
3e609135 | 733 | |
b6df4b79 XL |
734 | if (to) |
735 | kunmap_atomic(to); | |
0c28481f SY |
736 | } |
737 | ||
a5d68ba8 | 738 | static void gather_data_area(struct tcmu_dev *udev, struct tcmu_cmd *cmd, |
6c3796d1 | 739 | bool bidi, uint32_t read_len) |
f97ec7db | 740 | { |
a5d68ba8 | 741 | struct se_cmd *se_cmd = cmd->se_cmd; |
141685a3 | 742 | int i, dbi; |
26418649 | 743 | int block_remaining = 0; |
b6df4b79 | 744 | void *from = NULL, *to; |
141685a3 | 745 | size_t copy_bytes, offset; |
a5d68ba8 | 746 | struct scatterlist *sg, *data_sg; |
b6df4b79 | 747 | struct page *page; |
a5d68ba8 | 748 | unsigned int data_nents; |
141685a3 | 749 | uint32_t count = 0; |
a5d68ba8 XL |
750 | |
751 | if (!bidi) { | |
752 | data_sg = se_cmd->t_data_sg; | |
753 | data_nents = se_cmd->t_data_nents; | |
754 | } else { | |
a5d68ba8 XL |
755 | |
756 | /* | |
757 | * For bidi case, the first count blocks are for Data-Out | |
758 | * buffer blocks, and before gathering the Data-In buffer | |
759 | * the Data-Out buffer blocks should be discarded. | |
760 | */ | |
761 | count = DIV_ROUND_UP(se_cmd->data_length, DATA_BLOCK_SIZE); | |
a5d68ba8 XL |
762 | |
763 | data_sg = se_cmd->t_bidi_data_sg; | |
764 | data_nents = se_cmd->t_bidi_data_nents; | |
765 | } | |
f97ec7db | 766 | |
141685a3 XL |
767 | tcmu_cmd_set_dbi_cur(cmd, count); |
768 | ||
f97ec7db | 769 | for_each_sg(data_sg, sg, data_nents, i) { |
26418649 | 770 | int sg_remaining = sg->length; |
f97ec7db | 771 | to = kmap_atomic(sg_page(sg)) + sg->offset; |
6c3796d1 | 772 | while (sg_remaining > 0 && read_len > 0) { |
26418649 | 773 | if (block_remaining == 0) { |
b6df4b79 XL |
774 | if (from) |
775 | kunmap_atomic(from); | |
776 | ||
26418649 | 777 | block_remaining = DATA_BLOCK_SIZE; |
141685a3 | 778 | dbi = tcmu_cmd_get_dbi(cmd); |
b6df4b79 XL |
779 | page = tcmu_get_block_page(udev, dbi); |
780 | from = kmap_atomic(page); | |
26418649 SY |
781 | } |
782 | copy_bytes = min_t(size_t, sg_remaining, | |
783 | block_remaining); | |
6c3796d1 | 784 | if (read_len < copy_bytes) |
785 | copy_bytes = read_len; | |
141685a3 | 786 | offset = DATA_BLOCK_SIZE - block_remaining; |
f97ec7db | 787 | tcmu_flush_dcache_range(from, copy_bytes); |
c542942c | 788 | memcpy(to + sg->length - sg_remaining, from + offset, |
26418649 | 789 | copy_bytes); |
f97ec7db | 790 | |
26418649 SY |
791 | sg_remaining -= copy_bytes; |
792 | block_remaining -= copy_bytes; | |
6c3796d1 | 793 | read_len -= copy_bytes; |
f97ec7db | 794 | } |
e2e21bd8 | 795 | kunmap_atomic(to - sg->offset); |
6c3796d1 | 796 | if (read_len == 0) |
797 | break; | |
f97ec7db | 798 | } |
b6df4b79 XL |
799 | if (from) |
800 | kunmap_atomic(from); | |
f97ec7db IT |
801 | } |
802 | ||
b6df4b79 | 803 | static inline size_t spc_bitmap_free(unsigned long *bitmap, uint32_t thresh) |
26418649 | 804 | { |
3c0f26ff | 805 | return thresh - bitmap_weight(bitmap, thresh); |
26418649 SY |
806 | } |
807 | ||
7c9e7a6f | 808 | /* |
f97ec7db | 809 | * We can't queue a command until we have space available on the cmd ring *and* |
3d9b9555 | 810 | * space available on the data area. |
7c9e7a6f AG |
811 | * |
812 | * Called with ring lock held. | |
813 | */ | |
b6df4b79 XL |
814 | static bool is_ring_space_avail(struct tcmu_dev *udev, struct tcmu_cmd *cmd, |
815 | size_t cmd_size, size_t data_needed) | |
7c9e7a6f AG |
816 | { |
817 | struct tcmu_mailbox *mb = udev->mb_addr; | |
b6df4b79 XL |
818 | uint32_t blocks_needed = (data_needed + DATA_BLOCK_SIZE - 1) |
819 | / DATA_BLOCK_SIZE; | |
0241fd39 | 820 | size_t space, cmd_needed; |
7c9e7a6f AG |
821 | u32 cmd_head; |
822 | ||
823 | tcmu_flush_dcache_range(mb, sizeof(*mb)); | |
824 | ||
825 | cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ | |
826 | ||
f56574a2 AG |
827 | /* |
828 | * If cmd end-of-ring space is too small then we need space for a NOP plus | |
829 | * original cmd - cmds are internally contiguous. | |
830 | */ | |
831 | if (head_to_end(cmd_head, udev->cmdr_size) >= cmd_size) | |
832 | cmd_needed = cmd_size; | |
833 | else | |
834 | cmd_needed = cmd_size + head_to_end(cmd_head, udev->cmdr_size); | |
835 | ||
7c9e7a6f AG |
836 | space = spc_free(cmd_head, udev->cmdr_last_cleaned, udev->cmdr_size); |
837 | if (space < cmd_needed) { | |
838 | pr_debug("no cmd space: %u %u %u\n", cmd_head, | |
839 | udev->cmdr_last_cleaned, udev->cmdr_size); | |
840 | return false; | |
841 | } | |
842 | ||
b6df4b79 XL |
843 | /* try to check and get the data blocks as needed */ |
844 | space = spc_bitmap_free(udev->data_bitmap, udev->dbi_thresh); | |
3c0f26ff | 845 | if ((space * DATA_BLOCK_SIZE) < data_needed) { |
80eb8761 MC |
846 | unsigned long blocks_left = |
847 | (udev->max_blocks - udev->dbi_thresh) + space; | |
b6df4b79 XL |
848 | |
849 | if (blocks_left < blocks_needed) { | |
850 | pr_debug("no data space: only %lu available, but ask for %zu\n", | |
851 | blocks_left * DATA_BLOCK_SIZE, | |
852 | data_needed); | |
853 | return false; | |
854 | } | |
855 | ||
f890f579 | 856 | udev->dbi_thresh += blocks_needed; |
80eb8761 MC |
857 | if (udev->dbi_thresh > udev->max_blocks) |
858 | udev->dbi_thresh = udev->max_blocks; | |
7c9e7a6f AG |
859 | } |
860 | ||
daf78c30 | 861 | return tcmu_get_empty_blocks(udev, cmd); |
7c9e7a6f AG |
862 | } |
863 | ||
fe25cc34 XL |
864 | static inline size_t tcmu_cmd_get_base_cmd_size(size_t iov_cnt) |
865 | { | |
866 | return max(offsetof(struct tcmu_cmd_entry, req.iov[iov_cnt]), | |
867 | sizeof(struct tcmu_cmd_entry)); | |
868 | } | |
869 | ||
870 | static inline size_t tcmu_cmd_get_cmd_size(struct tcmu_cmd *tcmu_cmd, | |
871 | size_t base_command_size) | |
872 | { | |
873 | struct se_cmd *se_cmd = tcmu_cmd->se_cmd; | |
874 | size_t command_size; | |
875 | ||
876 | command_size = base_command_size + | |
877 | round_up(scsi_command_size(se_cmd->t_task_cdb), | |
878 | TCMU_OP_ALIGN_SIZE); | |
879 | ||
880 | WARN_ON(command_size & (TCMU_OP_ALIGN_SIZE-1)); | |
881 | ||
882 | return command_size; | |
883 | } | |
884 | ||
9103575a MC |
885 | static int tcmu_setup_cmd_timer(struct tcmu_cmd *tcmu_cmd, unsigned int tmo, |
886 | struct timer_list *timer) | |
0d44374c MC |
887 | { |
888 | struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; | |
0d44374c MC |
889 | int cmd_id; |
890 | ||
891 | if (tcmu_cmd->cmd_id) | |
9103575a | 892 | goto setup_timer; |
0d44374c MC |
893 | |
894 | cmd_id = idr_alloc(&udev->commands, tcmu_cmd, 1, USHRT_MAX, GFP_NOWAIT); | |
895 | if (cmd_id < 0) { | |
896 | pr_err("tcmu: Could not allocate cmd id.\n"); | |
897 | return cmd_id; | |
898 | } | |
899 | tcmu_cmd->cmd_id = cmd_id; | |
900 | ||
af1dd7ff MC |
901 | pr_debug("allocated cmd %u for dev %s tmo %lu\n", tcmu_cmd->cmd_id, |
902 | udev->name, tmo / MSEC_PER_SEC); | |
0d44374c | 903 | |
9103575a MC |
904 | setup_timer: |
905 | if (!tmo) | |
906 | return 0; | |
907 | ||
0d44374c | 908 | tcmu_cmd->deadline = round_jiffies_up(jiffies + msecs_to_jiffies(tmo)); |
a94a2572 XL |
909 | if (!timer_pending(timer)) |
910 | mod_timer(timer, tcmu_cmd->deadline); | |
911 | ||
0d44374c MC |
912 | return 0; |
913 | } | |
914 | ||
a94a2572 | 915 | static int add_to_qfull_queue(struct tcmu_cmd *tcmu_cmd) |
af1dd7ff MC |
916 | { |
917 | struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; | |
9103575a | 918 | unsigned int tmo; |
af1dd7ff MC |
919 | int ret; |
920 | ||
9103575a MC |
921 | /* |
922 | * For backwards compat if qfull_time_out is not set use | |
923 | * cmd_time_out and if that's not set use the default time out. | |
924 | */ | |
925 | if (!udev->qfull_time_out) | |
926 | return -ETIMEDOUT; | |
927 | else if (udev->qfull_time_out > 0) | |
928 | tmo = udev->qfull_time_out; | |
929 | else if (udev->cmd_time_out) | |
930 | tmo = udev->cmd_time_out; | |
931 | else | |
932 | tmo = TCMU_TIME_OUT; | |
933 | ||
934 | ret = tcmu_setup_cmd_timer(tcmu_cmd, tmo, &udev->qfull_timer); | |
af1dd7ff MC |
935 | if (ret) |
936 | return ret; | |
937 | ||
a94a2572 | 938 | list_add_tail(&tcmu_cmd->queue_entry, &udev->qfull_queue); |
af1dd7ff MC |
939 | pr_debug("adding cmd %u on dev %s to ring space wait queue\n", |
940 | tcmu_cmd->cmd_id, udev->name); | |
941 | return 0; | |
942 | } | |
943 | ||
6fd0ce79 MC |
944 | /** |
945 | * queue_cmd_ring - queue cmd to ring or internally | |
946 | * @tcmu_cmd: cmd to queue | |
947 | * @scsi_err: TCM error code if failure (-1) returned. | |
948 | * | |
949 | * Returns: | |
950 | * -1 we cannot queue internally or to the ring. | |
951 | * 0 success | |
af1dd7ff | 952 | * 1 internally queued to wait for ring memory to free. |
6fd0ce79 | 953 | */ |
e7f41104 | 954 | static int queue_cmd_ring(struct tcmu_cmd *tcmu_cmd, sense_reason_t *scsi_err) |
7c9e7a6f AG |
955 | { |
956 | struct tcmu_dev *udev = tcmu_cmd->tcmu_dev; | |
957 | struct se_cmd *se_cmd = tcmu_cmd->se_cmd; | |
958 | size_t base_command_size, command_size; | |
7c9e7a6f | 959 | struct tcmu_mailbox *mb; |
7c9e7a6f | 960 | struct tcmu_cmd_entry *entry; |
7c9e7a6f | 961 | struct iovec *iov; |
141685a3 | 962 | int iov_cnt, ret; |
7c9e7a6f AG |
963 | uint32_t cmd_head; |
964 | uint64_t cdb_off; | |
f97ec7db | 965 | bool copy_to_data_area; |
ab22d260 | 966 | size_t data_length = tcmu_cmd_get_data_length(tcmu_cmd); |
7c9e7a6f | 967 | |
6fd0ce79 MC |
968 | *scsi_err = TCM_NO_SENSE; |
969 | ||
892782ca MC |
970 | if (test_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags)) { |
971 | *scsi_err = TCM_LUN_BUSY; | |
972 | return -1; | |
973 | } | |
974 | ||
6fd0ce79 MC |
975 | if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) { |
976 | *scsi_err = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; | |
977 | return -1; | |
978 | } | |
7c9e7a6f AG |
979 | |
980 | /* | |
981 | * Must be a certain minimum size for response sense info, but | |
982 | * also may be larger if the iov array is large. | |
983 | * | |
fe25cc34 XL |
984 | * We prepare as many iovs as possbile for potential uses here, |
985 | * because it's expensive to tell how many regions are freed in | |
986 | * the bitmap & global data pool, as the size calculated here | |
987 | * will only be used to do the checks. | |
988 | * | |
989 | * The size will be recalculated later as actually needed to save | |
990 | * cmd area memories. | |
991 | */ | |
992 | base_command_size = tcmu_cmd_get_base_cmd_size(tcmu_cmd->dbi_cnt); | |
993 | command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size); | |
7c9e7a6f | 994 | |
a94a2572 | 995 | if (!list_empty(&udev->qfull_queue)) |
af1dd7ff | 996 | goto queue; |
7c9e7a6f AG |
997 | |
998 | mb = udev->mb_addr; | |
999 | cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ | |
554617b2 AG |
1000 | if ((command_size > (udev->cmdr_size / 2)) || |
1001 | data_length > udev->data_size) { | |
1002 | pr_warn("TCMU: Request of size %zu/%zu is too big for %u/%zu " | |
3d9b9555 | 1003 | "cmd ring/data area\n", command_size, data_length, |
7c9e7a6f | 1004 | udev->cmdr_size, udev->data_size); |
6fd0ce79 MC |
1005 | *scsi_err = TCM_INVALID_CDB_FIELD; |
1006 | return -1; | |
554617b2 | 1007 | } |
7c9e7a6f | 1008 | |
af1dd7ff | 1009 | if (!is_ring_space_avail(udev, tcmu_cmd, command_size, data_length)) { |
810b8153 MC |
1010 | /* |
1011 | * Don't leave commands partially setup because the unmap | |
1012 | * thread might need the blocks to make forward progress. | |
1013 | */ | |
1014 | tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cur); | |
1015 | tcmu_cmd_reset_dbi_cur(tcmu_cmd); | |
af1dd7ff | 1016 | goto queue; |
7c9e7a6f AG |
1017 | } |
1018 | ||
f56574a2 AG |
1019 | /* Insert a PAD if end-of-ring space is too small */ |
1020 | if (head_to_end(cmd_head, udev->cmdr_size) < command_size) { | |
1021 | size_t pad_size = head_to_end(cmd_head, udev->cmdr_size); | |
1022 | ||
7c9e7a6f | 1023 | entry = (void *) mb + CMDR_OFF + cmd_head; |
0ad46af8 AG |
1024 | tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_PAD); |
1025 | tcmu_hdr_set_len(&entry->hdr.len_op, pad_size); | |
1026 | entry->hdr.cmd_id = 0; /* not used for PAD */ | |
1027 | entry->hdr.kflags = 0; | |
1028 | entry->hdr.uflags = 0; | |
9d62bc0e | 1029 | tcmu_flush_dcache_range(entry, sizeof(*entry)); |
7c9e7a6f AG |
1030 | |
1031 | UPDATE_HEAD(mb->cmd_head, pad_size, udev->cmdr_size); | |
9d62bc0e | 1032 | tcmu_flush_dcache_range(mb, sizeof(*mb)); |
7c9e7a6f AG |
1033 | |
1034 | cmd_head = mb->cmd_head % udev->cmdr_size; /* UAM */ | |
1035 | WARN_ON(cmd_head != 0); | |
1036 | } | |
1037 | ||
1038 | entry = (void *) mb + CMDR_OFF + cmd_head; | |
b3743c71 | 1039 | memset(entry, 0, command_size); |
0ad46af8 | 1040 | tcmu_hdr_set_op(&entry->hdr.len_op, TCMU_OP_CMD); |
7c9e7a6f | 1041 | |
3d9b9555 | 1042 | /* Handle allocating space from the data area */ |
b6df4b79 | 1043 | tcmu_cmd_reset_dbi_cur(tcmu_cmd); |
7c9e7a6f | 1044 | iov = &entry->req.iov[0]; |
f97ec7db | 1045 | iov_cnt = 0; |
e4648b01 IT |
1046 | copy_to_data_area = (se_cmd->data_direction == DMA_TO_DEVICE |
1047 | || se_cmd->se_cmd_flags & SCF_BIDI); | |
1a1fc0b8 MC |
1048 | scatter_data_area(udev, tcmu_cmd, se_cmd->t_data_sg, |
1049 | se_cmd->t_data_nents, &iov, &iov_cnt, | |
1050 | copy_to_data_area); | |
7c9e7a6f AG |
1051 | entry->req.iov_cnt = iov_cnt; |
1052 | ||
e4648b01 | 1053 | /* Handle BIDI commands */ |
b3743c71 | 1054 | iov_cnt = 0; |
ab22d260 | 1055 | if (se_cmd->se_cmd_flags & SCF_BIDI) { |
ab22d260 | 1056 | iov++; |
1a1fc0b8 MC |
1057 | scatter_data_area(udev, tcmu_cmd, se_cmd->t_bidi_data_sg, |
1058 | se_cmd->t_bidi_data_nents, &iov, &iov_cnt, | |
1059 | false); | |
ab22d260 | 1060 | } |
b3743c71 | 1061 | entry->req.iov_bidi_cnt = iov_cnt; |
26418649 | 1062 | |
9103575a MC |
1063 | ret = tcmu_setup_cmd_timer(tcmu_cmd, udev->cmd_time_out, |
1064 | &udev->cmd_timer); | |
0d44374c MC |
1065 | if (ret) { |
1066 | tcmu_cmd_free_data(tcmu_cmd, tcmu_cmd->dbi_cnt); | |
6fd0ce79 MC |
1067 | |
1068 | *scsi_err = TCM_OUT_OF_RESOURCES; | |
1069 | return -1; | |
0d44374c MC |
1070 | } |
1071 | entry->hdr.cmd_id = tcmu_cmd->cmd_id; | |
1072 | ||
fe25cc34 XL |
1073 | /* |
1074 | * Recalaulate the command's base size and size according | |
1075 | * to the actual needs | |
1076 | */ | |
1077 | base_command_size = tcmu_cmd_get_base_cmd_size(entry->req.iov_cnt + | |
1078 | entry->req.iov_bidi_cnt); | |
1079 | command_size = tcmu_cmd_get_cmd_size(tcmu_cmd, base_command_size); | |
1080 | ||
1081 | tcmu_hdr_set_len(&entry->hdr.len_op, command_size); | |
1082 | ||
7c9e7a6f AG |
1083 | /* All offsets relative to mb_addr, not start of entry! */ |
1084 | cdb_off = CMDR_OFF + cmd_head + base_command_size; | |
1085 | memcpy((void *) mb + cdb_off, se_cmd->t_task_cdb, scsi_command_size(se_cmd->t_task_cdb)); | |
1086 | entry->req.cdb_off = cdb_off; | |
1087 | tcmu_flush_dcache_range(entry, sizeof(*entry)); | |
1088 | ||
1089 | UPDATE_HEAD(mb->cmd_head, command_size, udev->cmdr_size); | |
1090 | tcmu_flush_dcache_range(mb, sizeof(*mb)); | |
7c9e7a6f | 1091 | |
a94a2572 XL |
1092 | list_add_tail(&tcmu_cmd->queue_entry, &udev->inflight_queue); |
1093 | set_bit(TCMU_CMD_BIT_INFLIGHT, &tcmu_cmd->flags); | |
1094 | ||
7c9e7a6f AG |
1095 | /* TODO: only if FLUSH and FUA? */ |
1096 | uio_event_notify(&udev->uio_info); | |
1097 | ||
6fd0ce79 | 1098 | return 0; |
af1dd7ff MC |
1099 | |
1100 | queue: | |
a94a2572 | 1101 | if (add_to_qfull_queue(tcmu_cmd)) { |
af1dd7ff MC |
1102 | *scsi_err = TCM_OUT_OF_RESOURCES; |
1103 | return -1; | |
1104 | } | |
1105 | ||
1106 | return 1; | |
7c9e7a6f AG |
1107 | } |
1108 | ||
02eb924f AG |
1109 | static sense_reason_t |
1110 | tcmu_queue_cmd(struct se_cmd *se_cmd) | |
7c9e7a6f | 1111 | { |
af1dd7ff MC |
1112 | struct se_device *se_dev = se_cmd->se_dev; |
1113 | struct tcmu_dev *udev = TCMU_DEV(se_dev); | |
7c9e7a6f | 1114 | struct tcmu_cmd *tcmu_cmd; |
6fd0ce79 | 1115 | sense_reason_t scsi_ret; |
af1dd7ff | 1116 | int ret; |
7c9e7a6f AG |
1117 | |
1118 | tcmu_cmd = tcmu_alloc_cmd(se_cmd); | |
1119 | if (!tcmu_cmd) | |
02eb924f | 1120 | return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
7c9e7a6f | 1121 | |
af1dd7ff MC |
1122 | mutex_lock(&udev->cmdr_lock); |
1123 | ret = queue_cmd_ring(tcmu_cmd, &scsi_ret); | |
1124 | mutex_unlock(&udev->cmdr_lock); | |
1125 | if (ret < 0) | |
141685a3 | 1126 | tcmu_free_cmd(tcmu_cmd); |
6fd0ce79 | 1127 | return scsi_ret; |
7c9e7a6f AG |
1128 | } |
1129 | ||
1130 | static void tcmu_handle_completion(struct tcmu_cmd *cmd, struct tcmu_cmd_entry *entry) | |
1131 | { | |
1132 | struct se_cmd *se_cmd = cmd->se_cmd; | |
1133 | struct tcmu_dev *udev = cmd->tcmu_dev; | |
6c3796d1 | 1134 | bool read_len_valid = false; |
a86a7586 | 1135 | uint32_t read_len; |
7c9e7a6f | 1136 | |
141685a3 XL |
1137 | /* |
1138 | * cmd has been completed already from timeout, just reclaim | |
1139 | * data area space and free cmd | |
1140 | */ | |
a86a7586 DF |
1141 | if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) { |
1142 | WARN_ON_ONCE(se_cmd); | |
141685a3 | 1143 | goto out; |
a86a7586 | 1144 | } |
b25c7863 | 1145 | |
a94a2572 XL |
1146 | list_del_init(&cmd->queue_entry); |
1147 | ||
141685a3 | 1148 | tcmu_cmd_reset_dbi_cur(cmd); |
7c9e7a6f | 1149 | |
0ad46af8 | 1150 | if (entry->hdr.uflags & TCMU_UFLAG_UNKNOWN_OP) { |
0ad46af8 AG |
1151 | pr_warn("TCMU: Userspace set UNKNOWN_OP flag on se_cmd %p\n", |
1152 | cmd->se_cmd); | |
ed97d0cd | 1153 | entry->rsp.scsi_status = SAM_STAT_CHECK_CONDITION; |
6c3796d1 | 1154 | goto done; |
1155 | } | |
1156 | ||
a86a7586 | 1157 | read_len = se_cmd->data_length; |
6c3796d1 | 1158 | if (se_cmd->data_direction == DMA_FROM_DEVICE && |
1159 | (entry->hdr.uflags & TCMU_UFLAG_READ_LEN) && entry->rsp.read_len) { | |
1160 | read_len_valid = true; | |
1161 | if (entry->rsp.read_len < read_len) | |
1162 | read_len = entry->rsp.read_len; | |
1163 | } | |
1164 | ||
1165 | if (entry->rsp.scsi_status == SAM_STAT_CHECK_CONDITION) { | |
406f74c2 | 1166 | transport_copy_sense_to_cmd(se_cmd, entry->rsp.sense_buffer); |
6c3796d1 | 1167 | if (!read_len_valid ) |
1168 | goto done; | |
1169 | else | |
1170 | se_cmd->se_cmd_flags |= SCF_TREAT_READ_AS_NORMAL; | |
1171 | } | |
1172 | if (se_cmd->se_cmd_flags & SCF_BIDI) { | |
26418649 | 1173 | /* Get Data-In buffer before clean up */ |
6c3796d1 | 1174 | gather_data_area(udev, cmd, true, read_len); |
e4648b01 | 1175 | } else if (se_cmd->data_direction == DMA_FROM_DEVICE) { |
6c3796d1 | 1176 | gather_data_area(udev, cmd, false, read_len); |
7c9e7a6f | 1177 | } else if (se_cmd->data_direction == DMA_TO_DEVICE) { |
141685a3 | 1178 | /* TODO: */ |
2bc396a2 IT |
1179 | } else if (se_cmd->data_direction != DMA_NONE) { |
1180 | pr_warn("TCMU: data direction was %d!\n", | |
1181 | se_cmd->data_direction); | |
7c9e7a6f AG |
1182 | } |
1183 | ||
6c3796d1 | 1184 | done: |
1185 | if (read_len_valid) { | |
1186 | pr_debug("read_len = %d\n", read_len); | |
1187 | target_complete_cmd_with_length(cmd->se_cmd, | |
1188 | entry->rsp.scsi_status, read_len); | |
1189 | } else | |
1190 | target_complete_cmd(cmd->se_cmd, entry->rsp.scsi_status); | |
7c9e7a6f | 1191 | |
141685a3 XL |
1192 | out: |
1193 | cmd->se_cmd = NULL; | |
b6df4b79 | 1194 | tcmu_cmd_free_data(cmd, cmd->dbi_cnt); |
141685a3 | 1195 | tcmu_free_cmd(cmd); |
7c9e7a6f AG |
1196 | } |
1197 | ||
a94a2572 XL |
1198 | static void tcmu_set_next_deadline(struct list_head *queue, |
1199 | struct timer_list *timer) | |
1200 | { | |
1201 | struct tcmu_cmd *tcmu_cmd, *tmp_cmd; | |
1202 | unsigned long deadline = 0; | |
1203 | ||
1204 | list_for_each_entry_safe(tcmu_cmd, tmp_cmd, queue, queue_entry) { | |
1205 | if (!time_after(jiffies, tcmu_cmd->deadline)) { | |
1206 | deadline = tcmu_cmd->deadline; | |
1207 | break; | |
1208 | } | |
1209 | } | |
1210 | ||
1211 | if (deadline) | |
1212 | mod_timer(timer, deadline); | |
1213 | else | |
1214 | del_timer(timer); | |
1215 | } | |
1216 | ||
7c9e7a6f AG |
1217 | static unsigned int tcmu_handle_completions(struct tcmu_dev *udev) |
1218 | { | |
1219 | struct tcmu_mailbox *mb; | |
a94a2572 | 1220 | struct tcmu_cmd *cmd; |
7c9e7a6f AG |
1221 | int handled = 0; |
1222 | ||
1223 | if (test_bit(TCMU_DEV_BIT_BROKEN, &udev->flags)) { | |
1224 | pr_err("ring broken, not handling completions\n"); | |
1225 | return 0; | |
1226 | } | |
1227 | ||
7c9e7a6f AG |
1228 | mb = udev->mb_addr; |
1229 | tcmu_flush_dcache_range(mb, sizeof(*mb)); | |
1230 | ||
6aa7de05 | 1231 | while (udev->cmdr_last_cleaned != READ_ONCE(mb->cmd_tail)) { |
7c9e7a6f AG |
1232 | |
1233 | struct tcmu_cmd_entry *entry = (void *) mb + CMDR_OFF + udev->cmdr_last_cleaned; | |
7c9e7a6f AG |
1234 | |
1235 | tcmu_flush_dcache_range(entry, sizeof(*entry)); | |
1236 | ||
0ad46af8 AG |
1237 | if (tcmu_hdr_get_op(entry->hdr.len_op) == TCMU_OP_PAD) { |
1238 | UPDATE_HEAD(udev->cmdr_last_cleaned, | |
1239 | tcmu_hdr_get_len(entry->hdr.len_op), | |
1240 | udev->cmdr_size); | |
7c9e7a6f AG |
1241 | continue; |
1242 | } | |
0ad46af8 | 1243 | WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD); |
7c9e7a6f | 1244 | |
d3e709e6 | 1245 | cmd = idr_remove(&udev->commands, entry->hdr.cmd_id); |
7c9e7a6f | 1246 | if (!cmd) { |
88cf1073 MC |
1247 | pr_err("cmd_id %u not found, ring is broken\n", |
1248 | entry->hdr.cmd_id); | |
7c9e7a6f AG |
1249 | set_bit(TCMU_DEV_BIT_BROKEN, &udev->flags); |
1250 | break; | |
1251 | } | |
1252 | ||
1253 | tcmu_handle_completion(cmd, entry); | |
1254 | ||
0ad46af8 AG |
1255 | UPDATE_HEAD(udev->cmdr_last_cleaned, |
1256 | tcmu_hdr_get_len(entry->hdr.len_op), | |
1257 | udev->cmdr_size); | |
7c9e7a6f AG |
1258 | |
1259 | handled++; | |
1260 | } | |
1261 | ||
9103575a MC |
1262 | if (mb->cmd_tail == mb->cmd_head) { |
1263 | /* no more pending commands */ | |
1264 | del_timer(&udev->cmd_timer); | |
1265 | ||
a94a2572 | 1266 | if (list_empty(&udev->qfull_queue)) { |
9103575a MC |
1267 | /* |
1268 | * no more pending or waiting commands so try to | |
1269 | * reclaim blocks if needed. | |
1270 | */ | |
1271 | if (atomic_read(&global_db_count) > | |
80eb8761 | 1272 | tcmu_global_max_blocks) |
9103575a MC |
1273 | schedule_delayed_work(&tcmu_unmap_work, 0); |
1274 | } | |
a94a2572 XL |
1275 | } else if (udev->cmd_time_out) { |
1276 | tcmu_set_next_deadline(&udev->inflight_queue, &udev->cmd_timer); | |
af1dd7ff | 1277 | } |
7c9e7a6f AG |
1278 | |
1279 | return handled; | |
1280 | } | |
1281 | ||
1282 | static int tcmu_check_expired_cmd(int id, void *p, void *data) | |
1283 | { | |
1284 | struct tcmu_cmd *cmd = p; | |
af1dd7ff MC |
1285 | struct tcmu_dev *udev = cmd->tcmu_dev; |
1286 | u8 scsi_status; | |
1287 | struct se_cmd *se_cmd; | |
1288 | bool is_running; | |
7c9e7a6f AG |
1289 | |
1290 | if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) | |
1291 | return 0; | |
1292 | ||
611e2267 | 1293 | if (!time_after(jiffies, cmd->deadline)) |
7c9e7a6f AG |
1294 | return 0; |
1295 | ||
a94a2572 | 1296 | is_running = test_bit(TCMU_CMD_BIT_INFLIGHT, &cmd->flags); |
45dc488c | 1297 | se_cmd = cmd->se_cmd; |
7c9e7a6f | 1298 | |
af1dd7ff | 1299 | if (is_running) { |
9103575a MC |
1300 | /* |
1301 | * If cmd_time_out is disabled but qfull is set deadline | |
1302 | * will only reflect the qfull timeout. Ignore it. | |
1303 | */ | |
1304 | if (!udev->cmd_time_out) | |
1305 | return 0; | |
1306 | ||
af1dd7ff MC |
1307 | set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags); |
1308 | /* | |
1309 | * target_complete_cmd will translate this to LUN COMM FAILURE | |
1310 | */ | |
1311 | scsi_status = SAM_STAT_CHECK_CONDITION; | |
40d883b0 | 1312 | list_del_init(&cmd->queue_entry); |
a86a7586 | 1313 | cmd->se_cmd = NULL; |
af1dd7ff | 1314 | } else { |
40d883b0 | 1315 | list_del_init(&cmd->queue_entry); |
af1dd7ff MC |
1316 | idr_remove(&udev->commands, id); |
1317 | tcmu_free_cmd(cmd); | |
1318 | scsi_status = SAM_STAT_TASK_SET_FULL; | |
1319 | } | |
9103575a MC |
1320 | |
1321 | pr_debug("Timing out cmd %u on dev %s that is %s.\n", | |
1322 | id, udev->name, is_running ? "inflight" : "queued"); | |
1323 | ||
af1dd7ff | 1324 | target_complete_cmd(se_cmd, scsi_status); |
7c9e7a6f AG |
1325 | return 0; |
1326 | } | |
1327 | ||
9103575a | 1328 | static void tcmu_device_timedout(struct tcmu_dev *udev) |
7c9e7a6f | 1329 | { |
488ebe4c MC |
1330 | spin_lock(&timed_out_udevs_lock); |
1331 | if (list_empty(&udev->timedout_entry)) | |
1332 | list_add_tail(&udev->timedout_entry, &timed_out_udevs); | |
1333 | spin_unlock(&timed_out_udevs_lock); | |
b6df4b79 | 1334 | |
af1dd7ff | 1335 | schedule_delayed_work(&tcmu_unmap_work, 0); |
7c9e7a6f AG |
1336 | } |
1337 | ||
9103575a MC |
1338 | static void tcmu_cmd_timedout(struct timer_list *t) |
1339 | { | |
1340 | struct tcmu_dev *udev = from_timer(udev, t, cmd_timer); | |
1341 | ||
1342 | pr_debug("%s cmd timeout has expired\n", udev->name); | |
1343 | tcmu_device_timedout(udev); | |
1344 | } | |
1345 | ||
1346 | static void tcmu_qfull_timedout(struct timer_list *t) | |
1347 | { | |
1348 | struct tcmu_dev *udev = from_timer(udev, t, qfull_timer); | |
1349 | ||
1350 | pr_debug("%s qfull timeout has expired\n", udev->name); | |
1351 | tcmu_device_timedout(udev); | |
1352 | } | |
1353 | ||
7c9e7a6f AG |
1354 | static int tcmu_attach_hba(struct se_hba *hba, u32 host_id) |
1355 | { | |
1356 | struct tcmu_hba *tcmu_hba; | |
1357 | ||
1358 | tcmu_hba = kzalloc(sizeof(struct tcmu_hba), GFP_KERNEL); | |
1359 | if (!tcmu_hba) | |
1360 | return -ENOMEM; | |
1361 | ||
1362 | tcmu_hba->host_id = host_id; | |
1363 | hba->hba_ptr = tcmu_hba; | |
1364 | ||
1365 | return 0; | |
1366 | } | |
1367 | ||
1368 | static void tcmu_detach_hba(struct se_hba *hba) | |
1369 | { | |
1370 | kfree(hba->hba_ptr); | |
1371 | hba->hba_ptr = NULL; | |
1372 | } | |
1373 | ||
1374 | static struct se_device *tcmu_alloc_device(struct se_hba *hba, const char *name) | |
1375 | { | |
1376 | struct tcmu_dev *udev; | |
1377 | ||
1378 | udev = kzalloc(sizeof(struct tcmu_dev), GFP_KERNEL); | |
1379 | if (!udev) | |
1380 | return NULL; | |
f3cdbe39 | 1381 | kref_init(&udev->kref); |
7c9e7a6f AG |
1382 | |
1383 | udev->name = kstrdup(name, GFP_KERNEL); | |
1384 | if (!udev->name) { | |
1385 | kfree(udev); | |
1386 | return NULL; | |
1387 | } | |
1388 | ||
1389 | udev->hba = hba; | |
af980e46 | 1390 | udev->cmd_time_out = TCMU_TIME_OUT; |
9103575a | 1391 | udev->qfull_time_out = -1; |
7c9e7a6f | 1392 | |
80eb8761 | 1393 | udev->max_blocks = DATA_BLOCK_BITS_DEF; |
b6df4b79 | 1394 | mutex_init(&udev->cmdr_lock); |
7c9e7a6f | 1395 | |
ff07e4a4 | 1396 | INIT_LIST_HEAD(&udev->node); |
488ebe4c | 1397 | INIT_LIST_HEAD(&udev->timedout_entry); |
a94a2572 XL |
1398 | INIT_LIST_HEAD(&udev->qfull_queue); |
1399 | INIT_LIST_HEAD(&udev->inflight_queue); | |
7c9e7a6f | 1400 | idr_init(&udev->commands); |
7c9e7a6f | 1401 | |
9103575a MC |
1402 | timer_setup(&udev->qfull_timer, tcmu_qfull_timedout, 0); |
1403 | timer_setup(&udev->cmd_timer, tcmu_cmd_timedout, 0); | |
7c9e7a6f | 1404 | |
c22adc0b XL |
1405 | INIT_RADIX_TREE(&udev->data_blocks, GFP_KERNEL); |
1406 | ||
7c9e7a6f AG |
1407 | return &udev->se_dev; |
1408 | } | |
1409 | ||
a94a2572 | 1410 | static bool run_qfull_queue(struct tcmu_dev *udev, bool fail) |
af1dd7ff MC |
1411 | { |
1412 | struct tcmu_cmd *tcmu_cmd, *tmp_cmd; | |
1413 | LIST_HEAD(cmds); | |
1414 | bool drained = true; | |
1415 | sense_reason_t scsi_ret; | |
1416 | int ret; | |
1417 | ||
a94a2572 | 1418 | if (list_empty(&udev->qfull_queue)) |
af1dd7ff MC |
1419 | return true; |
1420 | ||
892782ca | 1421 | pr_debug("running %s's cmdr queue forcefail %d\n", udev->name, fail); |
af1dd7ff | 1422 | |
a94a2572 | 1423 | list_splice_init(&udev->qfull_queue, &cmds); |
af1dd7ff | 1424 | |
a94a2572 XL |
1425 | list_for_each_entry_safe(tcmu_cmd, tmp_cmd, &cmds, queue_entry) { |
1426 | list_del_init(&tcmu_cmd->queue_entry); | |
af1dd7ff MC |
1427 | |
1428 | pr_debug("removing cmd %u on dev %s from queue\n", | |
1429 | tcmu_cmd->cmd_id, udev->name); | |
1430 | ||
892782ca MC |
1431 | if (fail) { |
1432 | idr_remove(&udev->commands, tcmu_cmd->cmd_id); | |
1433 | /* | |
1434 | * We were not able to even start the command, so | |
1435 | * fail with busy to allow a retry in case runner | |
1436 | * was only temporarily down. If the device is being | |
1437 | * removed then LIO core will do the right thing and | |
1438 | * fail the retry. | |
1439 | */ | |
1440 | target_complete_cmd(tcmu_cmd->se_cmd, SAM_STAT_BUSY); | |
1441 | tcmu_free_cmd(tcmu_cmd); | |
1442 | continue; | |
1443 | } | |
1444 | ||
af1dd7ff MC |
1445 | ret = queue_cmd_ring(tcmu_cmd, &scsi_ret); |
1446 | if (ret < 0) { | |
1447 | pr_debug("cmd %u on dev %s failed with %u\n", | |
1448 | tcmu_cmd->cmd_id, udev->name, scsi_ret); | |
1449 | ||
1450 | idr_remove(&udev->commands, tcmu_cmd->cmd_id); | |
1451 | /* | |
1452 | * Ignore scsi_ret for now. target_complete_cmd | |
1453 | * drops it. | |
1454 | */ | |
1455 | target_complete_cmd(tcmu_cmd->se_cmd, | |
1456 | SAM_STAT_CHECK_CONDITION); | |
1457 | tcmu_free_cmd(tcmu_cmd); | |
1458 | } else if (ret > 0) { | |
1459 | pr_debug("ran out of space during cmdr queue run\n"); | |
1460 | /* | |
1461 | * cmd was requeued, so just put all cmds back in | |
1462 | * the queue | |
1463 | */ | |
a94a2572 | 1464 | list_splice_tail(&cmds, &udev->qfull_queue); |
af1dd7ff | 1465 | drained = false; |
a94a2572 | 1466 | break; |
af1dd7ff MC |
1467 | } |
1468 | } | |
a94a2572 XL |
1469 | |
1470 | tcmu_set_next_deadline(&udev->qfull_queue, &udev->qfull_timer); | |
af1dd7ff MC |
1471 | return drained; |
1472 | } | |
1473 | ||
7c9e7a6f AG |
1474 | static int tcmu_irqcontrol(struct uio_info *info, s32 irq_on) |
1475 | { | |
af1dd7ff | 1476 | struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); |
7c9e7a6f | 1477 | |
af1dd7ff MC |
1478 | mutex_lock(&udev->cmdr_lock); |
1479 | tcmu_handle_completions(udev); | |
a94a2572 | 1480 | run_qfull_queue(udev, false); |
af1dd7ff | 1481 | mutex_unlock(&udev->cmdr_lock); |
7c9e7a6f AG |
1482 | |
1483 | return 0; | |
1484 | } | |
1485 | ||
1486 | /* | |
1487 | * mmap code from uio.c. Copied here because we want to hook mmap() | |
1488 | * and this stuff must come along. | |
1489 | */ | |
1490 | static int tcmu_find_mem_index(struct vm_area_struct *vma) | |
1491 | { | |
1492 | struct tcmu_dev *udev = vma->vm_private_data; | |
1493 | struct uio_info *info = &udev->uio_info; | |
1494 | ||
1495 | if (vma->vm_pgoff < MAX_UIO_MAPS) { | |
1496 | if (info->mem[vma->vm_pgoff].size == 0) | |
1497 | return -1; | |
1498 | return (int)vma->vm_pgoff; | |
1499 | } | |
1500 | return -1; | |
1501 | } | |
1502 | ||
b6df4b79 XL |
1503 | static struct page *tcmu_try_get_block_page(struct tcmu_dev *udev, uint32_t dbi) |
1504 | { | |
1505 | struct page *page; | |
b6df4b79 XL |
1506 | |
1507 | mutex_lock(&udev->cmdr_lock); | |
1508 | page = tcmu_get_block_page(udev, dbi); | |
1509 | if (likely(page)) { | |
1510 | mutex_unlock(&udev->cmdr_lock); | |
1511 | return page; | |
1512 | } | |
1513 | ||
1514 | /* | |
c1c390ba MC |
1515 | * Userspace messed up and passed in a address not in the |
1516 | * data iov passed to it. | |
b6df4b79 | 1517 | */ |
c1c390ba MC |
1518 | pr_err("Invalid addr to data block mapping (dbi %u) on device %s\n", |
1519 | dbi, udev->name); | |
1520 | page = NULL; | |
b6df4b79 XL |
1521 | mutex_unlock(&udev->cmdr_lock); |
1522 | ||
1523 | return page; | |
1524 | } | |
1525 | ||
69589c9b | 1526 | static vm_fault_t tcmu_vma_fault(struct vm_fault *vmf) |
7c9e7a6f | 1527 | { |
11bac800 | 1528 | struct tcmu_dev *udev = vmf->vma->vm_private_data; |
7c9e7a6f AG |
1529 | struct uio_info *info = &udev->uio_info; |
1530 | struct page *page; | |
1531 | unsigned long offset; | |
1532 | void *addr; | |
1533 | ||
11bac800 | 1534 | int mi = tcmu_find_mem_index(vmf->vma); |
7c9e7a6f AG |
1535 | if (mi < 0) |
1536 | return VM_FAULT_SIGBUS; | |
1537 | ||
1538 | /* | |
1539 | * We need to subtract mi because userspace uses offset = N*PAGE_SIZE | |
1540 | * to use mem[N]. | |
1541 | */ | |
1542 | offset = (vmf->pgoff - mi) << PAGE_SHIFT; | |
1543 | ||
141685a3 XL |
1544 | if (offset < udev->data_off) { |
1545 | /* For the vmalloc()ed cmd area pages */ | |
1546 | addr = (void *)(unsigned long)info->mem[mi].addr + offset; | |
7c9e7a6f | 1547 | page = vmalloc_to_page(addr); |
141685a3 | 1548 | } else { |
141685a3 XL |
1549 | uint32_t dbi; |
1550 | ||
b6df4b79 | 1551 | /* For the dynamically growing data area pages */ |
141685a3 | 1552 | dbi = (offset - udev->data_off) / DATA_BLOCK_SIZE; |
b6df4b79 XL |
1553 | page = tcmu_try_get_block_page(udev, dbi); |
1554 | if (!page) | |
c1c390ba | 1555 | return VM_FAULT_SIGBUS; |
141685a3 XL |
1556 | } |
1557 | ||
7c9e7a6f AG |
1558 | get_page(page); |
1559 | vmf->page = page; | |
1560 | return 0; | |
1561 | } | |
1562 | ||
1563 | static const struct vm_operations_struct tcmu_vm_ops = { | |
1564 | .fault = tcmu_vma_fault, | |
1565 | }; | |
1566 | ||
1567 | static int tcmu_mmap(struct uio_info *info, struct vm_area_struct *vma) | |
1568 | { | |
1569 | struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); | |
1570 | ||
1571 | vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; | |
1572 | vma->vm_ops = &tcmu_vm_ops; | |
1573 | ||
1574 | vma->vm_private_data = udev; | |
1575 | ||
1576 | /* Ensure the mmap is exactly the right size */ | |
80eb8761 | 1577 | if (vma_pages(vma) != (udev->ring_size >> PAGE_SHIFT)) |
7c9e7a6f AG |
1578 | return -EINVAL; |
1579 | ||
1580 | return 0; | |
1581 | } | |
1582 | ||
1583 | static int tcmu_open(struct uio_info *info, struct inode *inode) | |
1584 | { | |
1585 | struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); | |
1586 | ||
1587 | /* O_EXCL not supported for char devs, so fake it? */ | |
1588 | if (test_and_set_bit(TCMU_DEV_BIT_OPEN, &udev->flags)) | |
1589 | return -EBUSY; | |
1590 | ||
b6df4b79 | 1591 | udev->inode = inode; |
9260695d | 1592 | kref_get(&udev->kref); |
b6df4b79 | 1593 | |
7c9e7a6f AG |
1594 | pr_debug("open\n"); |
1595 | ||
1596 | return 0; | |
1597 | } | |
1598 | ||
f3cdbe39 MC |
1599 | static void tcmu_dev_call_rcu(struct rcu_head *p) |
1600 | { | |
1601 | struct se_device *dev = container_of(p, struct se_device, rcu_head); | |
1602 | struct tcmu_dev *udev = TCMU_DEV(dev); | |
1603 | ||
1604 | kfree(udev->uio_info.name); | |
1605 | kfree(udev->name); | |
1606 | kfree(udev); | |
1607 | } | |
1608 | ||
c22adc0b XL |
1609 | static int tcmu_check_and_free_pending_cmd(struct tcmu_cmd *cmd) |
1610 | { | |
1611 | if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) { | |
1612 | kmem_cache_free(tcmu_cmd_cache, cmd); | |
1613 | return 0; | |
1614 | } | |
1615 | return -EINVAL; | |
1616 | } | |
1617 | ||
bf99ec13 MC |
1618 | static void tcmu_blocks_release(struct radix_tree_root *blocks, |
1619 | int start, int end) | |
c22adc0b XL |
1620 | { |
1621 | int i; | |
1622 | struct page *page; | |
1623 | ||
bf99ec13 MC |
1624 | for (i = start; i < end; i++) { |
1625 | page = radix_tree_delete(blocks, i); | |
c22adc0b XL |
1626 | if (page) { |
1627 | __free_page(page); | |
1628 | atomic_dec(&global_db_count); | |
1629 | } | |
1630 | } | |
c22adc0b XL |
1631 | } |
1632 | ||
f3cdbe39 MC |
1633 | static void tcmu_dev_kref_release(struct kref *kref) |
1634 | { | |
1635 | struct tcmu_dev *udev = container_of(kref, struct tcmu_dev, kref); | |
1636 | struct se_device *dev = &udev->se_dev; | |
c22adc0b XL |
1637 | struct tcmu_cmd *cmd; |
1638 | bool all_expired = true; | |
1639 | int i; | |
1640 | ||
1641 | vfree(udev->mb_addr); | |
1642 | udev->mb_addr = NULL; | |
1643 | ||
488ebe4c MC |
1644 | spin_lock_bh(&timed_out_udevs_lock); |
1645 | if (!list_empty(&udev->timedout_entry)) | |
1646 | list_del(&udev->timedout_entry); | |
1647 | spin_unlock_bh(&timed_out_udevs_lock); | |
1648 | ||
c22adc0b | 1649 | /* Upper layer should drain all requests before calling this */ |
6fddcb77 | 1650 | mutex_lock(&udev->cmdr_lock); |
c22adc0b XL |
1651 | idr_for_each_entry(&udev->commands, cmd, i) { |
1652 | if (tcmu_check_and_free_pending_cmd(cmd) != 0) | |
1653 | all_expired = false; | |
1654 | } | |
1655 | idr_destroy(&udev->commands); | |
c22adc0b XL |
1656 | WARN_ON(!all_expired); |
1657 | ||
bf99ec13 | 1658 | tcmu_blocks_release(&udev->data_blocks, 0, udev->dbi_max + 1); |
98effe47 | 1659 | bitmap_free(udev->data_bitmap); |
bf99ec13 | 1660 | mutex_unlock(&udev->cmdr_lock); |
f3cdbe39 MC |
1661 | |
1662 | call_rcu(&dev->rcu_head, tcmu_dev_call_rcu); | |
1663 | } | |
1664 | ||
7c9e7a6f AG |
1665 | static int tcmu_release(struct uio_info *info, struct inode *inode) |
1666 | { | |
1667 | struct tcmu_dev *udev = container_of(info, struct tcmu_dev, uio_info); | |
1668 | ||
1669 | clear_bit(TCMU_DEV_BIT_OPEN, &udev->flags); | |
1670 | ||
1671 | pr_debug("close\n"); | |
9260695d | 1672 | /* release ref from open */ |
f3cdbe39 | 1673 | kref_put(&udev->kref, tcmu_dev_kref_release); |
7c9e7a6f AG |
1674 | return 0; |
1675 | } | |
1676 | ||
9de3a1ef | 1677 | static int tcmu_init_genl_cmd_reply(struct tcmu_dev *udev, int cmd) |
b3af66e2 MC |
1678 | { |
1679 | struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd; | |
1680 | ||
1681 | if (!tcmu_kern_cmd_reply_supported) | |
9de3a1ef | 1682 | return 0; |
b849b456 KN |
1683 | |
1684 | if (udev->nl_reply_supported <= 0) | |
9de3a1ef | 1685 | return 0; |
b849b456 | 1686 | |
3228691f | 1687 | mutex_lock(&tcmu_nl_cmd_mutex); |
b849b456 | 1688 | |
bdaeedc1 MC |
1689 | if (tcmu_netlink_blocked) { |
1690 | mutex_unlock(&tcmu_nl_cmd_mutex); | |
1691 | pr_warn("Failing nl cmd %d on %s. Interface is blocked.\n", cmd, | |
1692 | udev->name); | |
1693 | return -EAGAIN; | |
1694 | } | |
b3af66e2 MC |
1695 | |
1696 | if (nl_cmd->cmd != TCMU_CMD_UNSPEC) { | |
3228691f | 1697 | mutex_unlock(&tcmu_nl_cmd_mutex); |
9de3a1ef MC |
1698 | pr_warn("netlink cmd %d already executing on %s\n", |
1699 | nl_cmd->cmd, udev->name); | |
1700 | return -EBUSY; | |
b3af66e2 MC |
1701 | } |
1702 | ||
1703 | memset(nl_cmd, 0, sizeof(*nl_cmd)); | |
1704 | nl_cmd->cmd = cmd; | |
3228691f | 1705 | nl_cmd->udev = udev; |
b3af66e2 | 1706 | init_completion(&nl_cmd->complete); |
3228691f MC |
1707 | INIT_LIST_HEAD(&nl_cmd->nl_list); |
1708 | ||
1709 | list_add_tail(&nl_cmd->nl_list, &tcmu_nl_cmd_list); | |
b3af66e2 | 1710 | |
3228691f | 1711 | mutex_unlock(&tcmu_nl_cmd_mutex); |
9de3a1ef | 1712 | return 0; |
b3af66e2 MC |
1713 | } |
1714 | ||
7d894862 LZ |
1715 | static void tcmu_destroy_genl_cmd_reply(struct tcmu_dev *udev) |
1716 | { | |
1717 | struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd; | |
1718 | ||
1719 | if (!tcmu_kern_cmd_reply_supported) | |
1720 | return; | |
1721 | ||
1722 | if (udev->nl_reply_supported <= 0) | |
1723 | return; | |
1724 | ||
1725 | mutex_lock(&tcmu_nl_cmd_mutex); | |
1726 | ||
1727 | list_del(&nl_cmd->nl_list); | |
1728 | memset(nl_cmd, 0, sizeof(*nl_cmd)); | |
1729 | ||
1730 | mutex_unlock(&tcmu_nl_cmd_mutex); | |
1731 | } | |
1732 | ||
b3af66e2 MC |
1733 | static int tcmu_wait_genl_cmd_reply(struct tcmu_dev *udev) |
1734 | { | |
1735 | struct tcmu_nl_cmd *nl_cmd = &udev->curr_nl_cmd; | |
1736 | int ret; | |
b3af66e2 MC |
1737 | |
1738 | if (!tcmu_kern_cmd_reply_supported) | |
1739 | return 0; | |
1740 | ||
b849b456 KN |
1741 | if (udev->nl_reply_supported <= 0) |
1742 | return 0; | |
1743 | ||
b3af66e2 MC |
1744 | pr_debug("sleeping for nl reply\n"); |
1745 | wait_for_completion(&nl_cmd->complete); | |
1746 | ||
3228691f | 1747 | mutex_lock(&tcmu_nl_cmd_mutex); |
b3af66e2 MC |
1748 | nl_cmd->cmd = TCMU_CMD_UNSPEC; |
1749 | ret = nl_cmd->status; | |
3228691f | 1750 | mutex_unlock(&tcmu_nl_cmd_mutex); |
b3af66e2 | 1751 | |
85fae482 | 1752 | return ret; |
b3af66e2 MC |
1753 | } |
1754 | ||
0e5aee39 ZL |
1755 | static int tcmu_netlink_event_init(struct tcmu_dev *udev, |
1756 | enum tcmu_genl_cmd cmd, | |
1757 | struct sk_buff **buf, void **hdr) | |
7c9e7a6f AG |
1758 | { |
1759 | struct sk_buff *skb; | |
1760 | void *msg_header; | |
6e14eab9 | 1761 | int ret = -ENOMEM; |
7c9e7a6f AG |
1762 | |
1763 | skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
1764 | if (!skb) | |
6e14eab9 | 1765 | return ret; |
7c9e7a6f AG |
1766 | |
1767 | msg_header = genlmsg_put(skb, 0, 0, &tcmu_genl_family, 0, cmd); | |
6e14eab9 NB |
1768 | if (!msg_header) |
1769 | goto free_skb; | |
7c9e7a6f | 1770 | |
b3af66e2 | 1771 | ret = nla_put_string(skb, TCMU_ATTR_DEVICE, udev->uio_info.name); |
6e14eab9 NB |
1772 | if (ret < 0) |
1773 | goto free_skb; | |
7c9e7a6f | 1774 | |
b3af66e2 MC |
1775 | ret = nla_put_u32(skb, TCMU_ATTR_MINOR, udev->uio_info.uio_dev->minor); |
1776 | if (ret < 0) | |
1777 | goto free_skb; | |
1778 | ||
1779 | ret = nla_put_u32(skb, TCMU_ATTR_DEVICE_ID, udev->se_dev.dev_index); | |
6e14eab9 NB |
1780 | if (ret < 0) |
1781 | goto free_skb; | |
7c9e7a6f | 1782 | |
0e5aee39 ZL |
1783 | *buf = skb; |
1784 | *hdr = msg_header; | |
1785 | return ret; | |
2d76443e | 1786 | |
0e5aee39 ZL |
1787 | free_skb: |
1788 | nlmsg_free(skb); | |
1789 | return ret; | |
1790 | } | |
1791 | ||
1792 | static int tcmu_netlink_event_send(struct tcmu_dev *udev, | |
1793 | enum tcmu_genl_cmd cmd, | |
06add777 | 1794 | struct sk_buff *skb, void *msg_header) |
0e5aee39 | 1795 | { |
06add777 | 1796 | int ret; |
8a45885c | 1797 | |
053c095a | 1798 | genlmsg_end(skb, msg_header); |
7c9e7a6f | 1799 | |
9de3a1ef MC |
1800 | ret = tcmu_init_genl_cmd_reply(udev, cmd); |
1801 | if (ret) { | |
1802 | nlmsg_free(skb); | |
1803 | return ret; | |
1804 | } | |
b3af66e2 | 1805 | |
20c08b36 | 1806 | ret = genlmsg_multicast_allns(&tcmu_genl_family, skb, 0, |
0e5aee39 | 1807 | TCMU_MCGRP_CONFIG, GFP_KERNEL); |
2ff717cd CA |
1808 | |
1809 | /* Wait during an add as the listener may not be up yet */ | |
1810 | if (ret == 0 || | |
1811 | (ret == -ESRCH && cmd == TCMU_CMD_ADDED_DEVICE)) | |
1812 | return tcmu_wait_genl_cmd_reply(udev); | |
7d894862 LZ |
1813 | else |
1814 | tcmu_destroy_genl_cmd_reply(udev); | |
2ff717cd | 1815 | |
6e14eab9 | 1816 | return ret; |
7c9e7a6f AG |
1817 | } |
1818 | ||
e0c240ac ZL |
1819 | static int tcmu_send_dev_add_event(struct tcmu_dev *udev) |
1820 | { | |
1821 | struct sk_buff *skb = NULL; | |
1822 | void *msg_header = NULL; | |
1823 | int ret = 0; | |
1824 | ||
1825 | ret = tcmu_netlink_event_init(udev, TCMU_CMD_ADDED_DEVICE, &skb, | |
1826 | &msg_header); | |
1827 | if (ret < 0) | |
1828 | return ret; | |
06add777 MC |
1829 | return tcmu_netlink_event_send(udev, TCMU_CMD_ADDED_DEVICE, skb, |
1830 | msg_header); | |
e0c240ac ZL |
1831 | } |
1832 | ||
f892bd8e ZL |
1833 | static int tcmu_send_dev_remove_event(struct tcmu_dev *udev) |
1834 | { | |
1835 | struct sk_buff *skb = NULL; | |
1836 | void *msg_header = NULL; | |
1837 | int ret = 0; | |
1838 | ||
1839 | ret = tcmu_netlink_event_init(udev, TCMU_CMD_REMOVED_DEVICE, | |
1840 | &skb, &msg_header); | |
1841 | if (ret < 0) | |
1842 | return ret; | |
1843 | return tcmu_netlink_event_send(udev, TCMU_CMD_REMOVED_DEVICE, | |
06add777 | 1844 | skb, msg_header); |
f892bd8e ZL |
1845 | } |
1846 | ||
de8c5221 | 1847 | static int tcmu_update_uio_info(struct tcmu_dev *udev) |
7c9e7a6f | 1848 | { |
7c9e7a6f AG |
1849 | struct tcmu_hba *hba = udev->hba->hba_ptr; |
1850 | struct uio_info *info; | |
7c9e7a6f AG |
1851 | char *str; |
1852 | ||
1853 | info = &udev->uio_info; | |
7c9e7a6f | 1854 | |
7c9e7a6f | 1855 | if (udev->dev_config[0]) |
22c2f35f CJ |
1856 | str = kasprintf(GFP_KERNEL, "tcm-user/%u/%s/%s", hba->host_id, |
1857 | udev->name, udev->dev_config); | |
1858 | else | |
1859 | str = kasprintf(GFP_KERNEL, "tcm-user/%u/%s", hba->host_id, | |
1860 | udev->name); | |
1861 | if (!str) | |
1862 | return -ENOMEM; | |
7c9e7a6f | 1863 | |
ededd039 BL |
1864 | /* If the old string exists, free it */ |
1865 | kfree(info->name); | |
7c9e7a6f AG |
1866 | info->name = str; |
1867 | ||
de8c5221 BL |
1868 | return 0; |
1869 | } | |
1870 | ||
1871 | static int tcmu_configure_device(struct se_device *dev) | |
1872 | { | |
1873 | struct tcmu_dev *udev = TCMU_DEV(dev); | |
1874 | struct uio_info *info; | |
1875 | struct tcmu_mailbox *mb; | |
1876 | int ret = 0; | |
1877 | ||
1878 | ret = tcmu_update_uio_info(udev); | |
1879 | if (ret) | |
1880 | return ret; | |
1881 | ||
1882 | info = &udev->uio_info; | |
1883 | ||
c97840c8 | 1884 | mutex_lock(&udev->cmdr_lock); |
98effe47 | 1885 | udev->data_bitmap = bitmap_zalloc(udev->max_blocks, GFP_KERNEL); |
c97840c8 | 1886 | mutex_unlock(&udev->cmdr_lock); |
a24e7917 WY |
1887 | if (!udev->data_bitmap) { |
1888 | ret = -ENOMEM; | |
80eb8761 | 1889 | goto err_bitmap_alloc; |
a24e7917 | 1890 | } |
80eb8761 | 1891 | |
141685a3 | 1892 | udev->mb_addr = vzalloc(CMDR_SIZE); |
7c9e7a6f AG |
1893 | if (!udev->mb_addr) { |
1894 | ret = -ENOMEM; | |
1895 | goto err_vzalloc; | |
1896 | } | |
1897 | ||
1898 | /* mailbox fits in first part of CMDR space */ | |
1899 | udev->cmdr_size = CMDR_SIZE - CMDR_OFF; | |
1900 | udev->data_off = CMDR_SIZE; | |
80eb8761 | 1901 | udev->data_size = udev->max_blocks * DATA_BLOCK_SIZE; |
b6df4b79 | 1902 | udev->dbi_thresh = 0; /* Default in Idle state */ |
7c9e7a6f | 1903 | |
141685a3 | 1904 | /* Initialise the mailbox of the ring buffer */ |
7c9e7a6f | 1905 | mb = udev->mb_addr; |
0ad46af8 | 1906 | mb->version = TCMU_MAILBOX_VERSION; |
6c3796d1 | 1907 | mb->flags = TCMU_MAILBOX_FLAG_CAP_OOOC | TCMU_MAILBOX_FLAG_CAP_READ_LEN; |
7c9e7a6f AG |
1908 | mb->cmdr_off = CMDR_OFF; |
1909 | mb->cmdr_size = udev->cmdr_size; | |
1910 | ||
1911 | WARN_ON(!PAGE_ALIGNED(udev->data_off)); | |
1912 | WARN_ON(udev->data_size % PAGE_SIZE); | |
26418649 | 1913 | WARN_ON(udev->data_size % DATA_BLOCK_SIZE); |
7c9e7a6f | 1914 | |
ac64a2ce | 1915 | info->version = __stringify(TCMU_MAILBOX_VERSION); |
7c9e7a6f AG |
1916 | |
1917 | info->mem[0].name = "tcm-user command & data buffer"; | |
0633e123 | 1918 | info->mem[0].addr = (phys_addr_t)(uintptr_t)udev->mb_addr; |
80eb8761 | 1919 | info->mem[0].size = udev->ring_size = udev->data_size + CMDR_SIZE; |
141685a3 | 1920 | info->mem[0].memtype = UIO_MEM_NONE; |
7c9e7a6f AG |
1921 | |
1922 | info->irqcontrol = tcmu_irqcontrol; | |
1923 | info->irq = UIO_IRQ_CUSTOM; | |
1924 | ||
1925 | info->mmap = tcmu_mmap; | |
1926 | info->open = tcmu_open; | |
1927 | info->release = tcmu_release; | |
1928 | ||
1929 | ret = uio_register_device(tcmu_root_device, info); | |
1930 | if (ret) | |
1931 | goto err_register; | |
1932 | ||
81ee28de SY |
1933 | /* User can set hw_block_size before enable the device */ |
1934 | if (dev->dev_attrib.hw_block_size == 0) | |
1935 | dev->dev_attrib.hw_block_size = 512; | |
7c9e7a6f | 1936 | /* Other attributes can be configured in userspace */ |
3abaa2bf MC |
1937 | if (!dev->dev_attrib.hw_max_sectors) |
1938 | dev->dev_attrib.hw_max_sectors = 128; | |
9a8bb606 BL |
1939 | if (!dev->dev_attrib.emulate_write_cache) |
1940 | dev->dev_attrib.emulate_write_cache = 0; | |
7c9e7a6f AG |
1941 | dev->dev_attrib.hw_queue_depth = 128; |
1942 | ||
b849b456 KN |
1943 | /* If user didn't explicitly disable netlink reply support, use |
1944 | * module scope setting. | |
1945 | */ | |
1946 | if (udev->nl_reply_supported >= 0) | |
1947 | udev->nl_reply_supported = tcmu_kern_cmd_reply_supported; | |
1948 | ||
f3cdbe39 MC |
1949 | /* |
1950 | * Get a ref incase userspace does a close on the uio device before | |
1951 | * LIO has initiated tcmu_free_device. | |
1952 | */ | |
1953 | kref_get(&udev->kref); | |
1954 | ||
e0c240ac | 1955 | ret = tcmu_send_dev_add_event(udev); |
7c9e7a6f AG |
1956 | if (ret) |
1957 | goto err_netlink; | |
1958 | ||
b6df4b79 XL |
1959 | mutex_lock(&root_udev_mutex); |
1960 | list_add(&udev->node, &root_udev); | |
1961 | mutex_unlock(&root_udev_mutex); | |
1962 | ||
7c9e7a6f AG |
1963 | return 0; |
1964 | ||
1965 | err_netlink: | |
f3cdbe39 | 1966 | kref_put(&udev->kref, tcmu_dev_kref_release); |
7c9e7a6f AG |
1967 | uio_unregister_device(&udev->uio_info); |
1968 | err_register: | |
1969 | vfree(udev->mb_addr); | |
c22adc0b | 1970 | udev->mb_addr = NULL; |
7c9e7a6f | 1971 | err_vzalloc: |
98effe47 | 1972 | bitmap_free(udev->data_bitmap); |
80eb8761 MC |
1973 | udev->data_bitmap = NULL; |
1974 | err_bitmap_alloc: | |
7c9e7a6f | 1975 | kfree(info->name); |
f3cdbe39 | 1976 | info->name = NULL; |
7c9e7a6f AG |
1977 | |
1978 | return ret; | |
1979 | } | |
1980 | ||
7c9e7a6f | 1981 | static void tcmu_free_device(struct se_device *dev) |
92634706 MC |
1982 | { |
1983 | struct tcmu_dev *udev = TCMU_DEV(dev); | |
1984 | ||
1985 | /* release ref from init */ | |
1986 | kref_put(&udev->kref, tcmu_dev_kref_release); | |
1987 | } | |
1988 | ||
1989 | static void tcmu_destroy_device(struct se_device *dev) | |
7c9e7a6f AG |
1990 | { |
1991 | struct tcmu_dev *udev = TCMU_DEV(dev); | |
7c9e7a6f | 1992 | |
9103575a MC |
1993 | del_timer_sync(&udev->cmd_timer); |
1994 | del_timer_sync(&udev->qfull_timer); | |
7c9e7a6f | 1995 | |
b6df4b79 XL |
1996 | mutex_lock(&root_udev_mutex); |
1997 | list_del(&udev->node); | |
1998 | mutex_unlock(&root_udev_mutex); | |
1999 | ||
f892bd8e | 2000 | tcmu_send_dev_remove_event(udev); |
7c9e7a6f | 2001 | |
531283ff | 2002 | uio_unregister_device(&udev->uio_info); |
9260695d MC |
2003 | |
2004 | /* release ref from configure */ | |
2005 | kref_put(&udev->kref, tcmu_dev_kref_release); | |
7c9e7a6f AG |
2006 | } |
2007 | ||
892782ca MC |
2008 | static void tcmu_unblock_dev(struct tcmu_dev *udev) |
2009 | { | |
2010 | mutex_lock(&udev->cmdr_lock); | |
2011 | clear_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags); | |
2012 | mutex_unlock(&udev->cmdr_lock); | |
2013 | } | |
2014 | ||
2015 | static void tcmu_block_dev(struct tcmu_dev *udev) | |
2016 | { | |
2017 | mutex_lock(&udev->cmdr_lock); | |
2018 | ||
2019 | if (test_and_set_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags)) | |
2020 | goto unlock; | |
2021 | ||
2022 | /* complete IO that has executed successfully */ | |
2023 | tcmu_handle_completions(udev); | |
2024 | /* fail IO waiting to be queued */ | |
a94a2572 | 2025 | run_qfull_queue(udev, true); |
892782ca MC |
2026 | |
2027 | unlock: | |
2028 | mutex_unlock(&udev->cmdr_lock); | |
2029 | } | |
2030 | ||
2031 | static void tcmu_reset_ring(struct tcmu_dev *udev, u8 err_level) | |
2032 | { | |
2033 | struct tcmu_mailbox *mb; | |
2034 | struct tcmu_cmd *cmd; | |
2035 | int i; | |
2036 | ||
2037 | mutex_lock(&udev->cmdr_lock); | |
2038 | ||
2039 | idr_for_each_entry(&udev->commands, cmd, i) { | |
a94a2572 | 2040 | if (!test_bit(TCMU_CMD_BIT_INFLIGHT, &cmd->flags)) |
892782ca MC |
2041 | continue; |
2042 | ||
2043 | pr_debug("removing cmd %u on dev %s from ring (is expired %d)\n", | |
2044 | cmd->cmd_id, udev->name, | |
2045 | test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)); | |
2046 | ||
2047 | idr_remove(&udev->commands, i); | |
2048 | if (!test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags)) { | |
a86a7586 | 2049 | WARN_ON(!cmd->se_cmd); |
a94a2572 | 2050 | list_del_init(&cmd->queue_entry); |
892782ca MC |
2051 | if (err_level == 1) { |
2052 | /* | |
2053 | * Userspace was not able to start the | |
2054 | * command or it is retryable. | |
2055 | */ | |
2056 | target_complete_cmd(cmd->se_cmd, SAM_STAT_BUSY); | |
2057 | } else { | |
2058 | /* hard failure */ | |
2059 | target_complete_cmd(cmd->se_cmd, | |
2060 | SAM_STAT_CHECK_CONDITION); | |
2061 | } | |
2062 | } | |
2063 | tcmu_cmd_free_data(cmd, cmd->dbi_cnt); | |
2064 | tcmu_free_cmd(cmd); | |
2065 | } | |
2066 | ||
2067 | mb = udev->mb_addr; | |
2068 | tcmu_flush_dcache_range(mb, sizeof(*mb)); | |
2069 | pr_debug("mb last %u head %u tail %u\n", udev->cmdr_last_cleaned, | |
2070 | mb->cmd_tail, mb->cmd_head); | |
2071 | ||
2072 | udev->cmdr_last_cleaned = 0; | |
2073 | mb->cmd_tail = 0; | |
2074 | mb->cmd_head = 0; | |
2075 | tcmu_flush_dcache_range(mb, sizeof(*mb)); | |
066f79a5 | 2076 | clear_bit(TCMU_DEV_BIT_BROKEN, &udev->flags); |
892782ca MC |
2077 | |
2078 | del_timer(&udev->cmd_timer); | |
2079 | ||
2080 | mutex_unlock(&udev->cmdr_lock); | |
2081 | } | |
2082 | ||
7c9e7a6f | 2083 | enum { |
3abaa2bf | 2084 | Opt_dev_config, Opt_dev_size, Opt_hw_block_size, Opt_hw_max_sectors, |
80eb8761 | 2085 | Opt_nl_reply_supported, Opt_max_data_area_mb, Opt_err, |
7c9e7a6f AG |
2086 | }; |
2087 | ||
2088 | static match_table_t tokens = { | |
2089 | {Opt_dev_config, "dev_config=%s"}, | |
0e0d7526 | 2090 | {Opt_dev_size, "dev_size=%s"}, |
b60cb1f8 MC |
2091 | {Opt_hw_block_size, "hw_block_size=%d"}, |
2092 | {Opt_hw_max_sectors, "hw_max_sectors=%d"}, | |
b849b456 | 2093 | {Opt_nl_reply_supported, "nl_reply_supported=%d"}, |
c97840c8 | 2094 | {Opt_max_data_area_mb, "max_data_area_mb=%d"}, |
7c9e7a6f AG |
2095 | {Opt_err, NULL} |
2096 | }; | |
2097 | ||
3abaa2bf MC |
2098 | static int tcmu_set_dev_attrib(substring_t *arg, u32 *dev_attrib) |
2099 | { | |
b60cb1f8 | 2100 | int val, ret; |
3abaa2bf | 2101 | |
b60cb1f8 | 2102 | ret = match_int(arg, &val); |
3abaa2bf | 2103 | if (ret < 0) { |
b60cb1f8 MC |
2104 | pr_err("match_int() failed for dev attrib. Error %d.\n", |
2105 | ret); | |
3abaa2bf MC |
2106 | return ret; |
2107 | } | |
b60cb1f8 MC |
2108 | |
2109 | if (val <= 0) { | |
2110 | pr_err("Invalid dev attrib value %d. Must be greater than zero.\n", | |
2111 | val); | |
3abaa2bf MC |
2112 | return -EINVAL; |
2113 | } | |
b60cb1f8 | 2114 | *dev_attrib = val; |
3abaa2bf MC |
2115 | return 0; |
2116 | } | |
2117 | ||
c97840c8 MC |
2118 | static int tcmu_set_max_blocks_param(struct tcmu_dev *udev, substring_t *arg) |
2119 | { | |
2120 | int val, ret; | |
2121 | ||
2122 | ret = match_int(arg, &val); | |
2123 | if (ret < 0) { | |
2124 | pr_err("match_int() failed for max_data_area_mb=. Error %d.\n", | |
2125 | ret); | |
2126 | return ret; | |
2127 | } | |
2128 | ||
2129 | if (val <= 0) { | |
2130 | pr_err("Invalid max_data_area %d.\n", val); | |
2131 | return -EINVAL; | |
2132 | } | |
2133 | ||
2134 | mutex_lock(&udev->cmdr_lock); | |
2135 | if (udev->data_bitmap) { | |
2136 | pr_err("Cannot set max_data_area_mb after it has been enabled.\n"); | |
2137 | ret = -EINVAL; | |
2138 | goto unlock; | |
2139 | } | |
2140 | ||
2141 | udev->max_blocks = TCMU_MBS_TO_BLOCKS(val); | |
2142 | if (udev->max_blocks > tcmu_global_max_blocks) { | |
2143 | pr_err("%d is too large. Adjusting max_data_area_mb to global limit of %u\n", | |
2144 | val, TCMU_BLOCKS_TO_MBS(tcmu_global_max_blocks)); | |
2145 | udev->max_blocks = tcmu_global_max_blocks; | |
2146 | } | |
2147 | ||
2148 | unlock: | |
2149 | mutex_unlock(&udev->cmdr_lock); | |
2150 | return ret; | |
2151 | } | |
2152 | ||
7c9e7a6f AG |
2153 | static ssize_t tcmu_set_configfs_dev_params(struct se_device *dev, |
2154 | const char *page, ssize_t count) | |
2155 | { | |
2156 | struct tcmu_dev *udev = TCMU_DEV(dev); | |
0e0d7526 | 2157 | char *orig, *ptr, *opts; |
7c9e7a6f | 2158 | substring_t args[MAX_OPT_ARGS]; |
c97840c8 | 2159 | int ret = 0, token; |
7c9e7a6f AG |
2160 | |
2161 | opts = kstrdup(page, GFP_KERNEL); | |
2162 | if (!opts) | |
2163 | return -ENOMEM; | |
2164 | ||
2165 | orig = opts; | |
2166 | ||
2167 | while ((ptr = strsep(&opts, ",\n")) != NULL) { | |
2168 | if (!*ptr) | |
2169 | continue; | |
2170 | ||
2171 | token = match_token(ptr, tokens, args); | |
2172 | switch (token) { | |
2173 | case Opt_dev_config: | |
2174 | if (match_strlcpy(udev->dev_config, &args[0], | |
2175 | TCMU_CONFIG_LEN) == 0) { | |
2176 | ret = -EINVAL; | |
2177 | break; | |
2178 | } | |
2179 | pr_debug("TCMU: Referencing Path: %s\n", udev->dev_config); | |
2180 | break; | |
2181 | case Opt_dev_size: | |
0e0d7526 | 2182 | ret = match_u64(&args[0], &udev->dev_size); |
7c9e7a6f | 2183 | if (ret < 0) |
0e0d7526 MC |
2184 | pr_err("match_u64() failed for dev_size=. Error %d.\n", |
2185 | ret); | |
7c9e7a6f | 2186 | break; |
9c1cd1b6 | 2187 | case Opt_hw_block_size: |
3abaa2bf MC |
2188 | ret = tcmu_set_dev_attrib(&args[0], |
2189 | &(dev->dev_attrib.hw_block_size)); | |
2190 | break; | |
2191 | case Opt_hw_max_sectors: | |
2192 | ret = tcmu_set_dev_attrib(&args[0], | |
2193 | &(dev->dev_attrib.hw_max_sectors)); | |
9c1cd1b6 | 2194 | break; |
b849b456 | 2195 | case Opt_nl_reply_supported: |
b60cb1f8 | 2196 | ret = match_int(&args[0], &udev->nl_reply_supported); |
b849b456 | 2197 | if (ret < 0) |
b60cb1f8 MC |
2198 | pr_err("match_int() failed for nl_reply_supported=. Error %d.\n", |
2199 | ret); | |
b849b456 | 2200 | break; |
80eb8761 | 2201 | case Opt_max_data_area_mb: |
c97840c8 | 2202 | ret = tcmu_set_max_blocks_param(udev, &args[0]); |
80eb8761 | 2203 | break; |
7c9e7a6f AG |
2204 | default: |
2205 | break; | |
2206 | } | |
2579325c MC |
2207 | |
2208 | if (ret) | |
2209 | break; | |
7c9e7a6f AG |
2210 | } |
2211 | ||
2212 | kfree(orig); | |
2213 | return (!ret) ? count : ret; | |
2214 | } | |
2215 | ||
2216 | static ssize_t tcmu_show_configfs_dev_params(struct se_device *dev, char *b) | |
2217 | { | |
2218 | struct tcmu_dev *udev = TCMU_DEV(dev); | |
2219 | ssize_t bl = 0; | |
2220 | ||
2221 | bl = sprintf(b + bl, "Config: %s ", | |
2222 | udev->dev_config[0] ? udev->dev_config : "NULL"); | |
0e0d7526 | 2223 | bl += sprintf(b + bl, "Size: %llu ", udev->dev_size); |
80eb8761 MC |
2224 | bl += sprintf(b + bl, "MaxDataAreaMB: %u\n", |
2225 | TCMU_BLOCKS_TO_MBS(udev->max_blocks)); | |
7c9e7a6f AG |
2226 | |
2227 | return bl; | |
2228 | } | |
2229 | ||
2230 | static sector_t tcmu_get_blocks(struct se_device *dev) | |
2231 | { | |
2232 | struct tcmu_dev *udev = TCMU_DEV(dev); | |
2233 | ||
2234 | return div_u64(udev->dev_size - dev->dev_attrib.block_size, | |
2235 | dev->dev_attrib.block_size); | |
2236 | } | |
2237 | ||
7c9e7a6f | 2238 | static sense_reason_t |
9c1cd1b6 | 2239 | tcmu_parse_cdb(struct se_cmd *cmd) |
7c9e7a6f | 2240 | { |
02eb924f | 2241 | return passthrough_parse_cdb(cmd, tcmu_queue_cmd); |
7c9e7a6f AG |
2242 | } |
2243 | ||
7d7a7435 NB |
2244 | static ssize_t tcmu_cmd_time_out_show(struct config_item *item, char *page) |
2245 | { | |
2246 | struct se_dev_attrib *da = container_of(to_config_group(item), | |
2247 | struct se_dev_attrib, da_group); | |
b5ab697c | 2248 | struct tcmu_dev *udev = TCMU_DEV(da->da_dev); |
7d7a7435 NB |
2249 | |
2250 | return snprintf(page, PAGE_SIZE, "%lu\n", udev->cmd_time_out / MSEC_PER_SEC); | |
2251 | } | |
2252 | ||
2253 | static ssize_t tcmu_cmd_time_out_store(struct config_item *item, const char *page, | |
2254 | size_t count) | |
2255 | { | |
2256 | struct se_dev_attrib *da = container_of(to_config_group(item), | |
2257 | struct se_dev_attrib, da_group); | |
2258 | struct tcmu_dev *udev = container_of(da->da_dev, | |
2259 | struct tcmu_dev, se_dev); | |
2260 | u32 val; | |
2261 | int ret; | |
2262 | ||
2263 | if (da->da_dev->export_count) { | |
2264 | pr_err("Unable to set tcmu cmd_time_out while exports exist\n"); | |
2265 | return -EINVAL; | |
2266 | } | |
2267 | ||
2268 | ret = kstrtou32(page, 0, &val); | |
2269 | if (ret < 0) | |
2270 | return ret; | |
2271 | ||
7d7a7435 NB |
2272 | udev->cmd_time_out = val * MSEC_PER_SEC; |
2273 | return count; | |
2274 | } | |
2275 | CONFIGFS_ATTR(tcmu_, cmd_time_out); | |
2276 | ||
9103575a MC |
2277 | static ssize_t tcmu_qfull_time_out_show(struct config_item *item, char *page) |
2278 | { | |
2279 | struct se_dev_attrib *da = container_of(to_config_group(item), | |
2280 | struct se_dev_attrib, da_group); | |
2281 | struct tcmu_dev *udev = TCMU_DEV(da->da_dev); | |
2282 | ||
2283 | return snprintf(page, PAGE_SIZE, "%ld\n", udev->qfull_time_out <= 0 ? | |
2284 | udev->qfull_time_out : | |
2285 | udev->qfull_time_out / MSEC_PER_SEC); | |
2286 | } | |
2287 | ||
2288 | static ssize_t tcmu_qfull_time_out_store(struct config_item *item, | |
2289 | const char *page, size_t count) | |
2290 | { | |
2291 | struct se_dev_attrib *da = container_of(to_config_group(item), | |
2292 | struct se_dev_attrib, da_group); | |
2293 | struct tcmu_dev *udev = TCMU_DEV(da->da_dev); | |
2294 | s32 val; | |
2295 | int ret; | |
2296 | ||
2297 | ret = kstrtos32(page, 0, &val); | |
2298 | if (ret < 0) | |
2299 | return ret; | |
2300 | ||
2301 | if (val >= 0) { | |
2302 | udev->qfull_time_out = val * MSEC_PER_SEC; | |
125966db PKK |
2303 | } else if (val == -1) { |
2304 | udev->qfull_time_out = val; | |
9103575a MC |
2305 | } else { |
2306 | printk(KERN_ERR "Invalid qfull timeout value %d\n", val); | |
2307 | return -EINVAL; | |
2308 | } | |
2309 | return count; | |
2310 | } | |
2311 | CONFIGFS_ATTR(tcmu_, qfull_time_out); | |
2312 | ||
80eb8761 MC |
2313 | static ssize_t tcmu_max_data_area_mb_show(struct config_item *item, char *page) |
2314 | { | |
2315 | struct se_dev_attrib *da = container_of(to_config_group(item), | |
2316 | struct se_dev_attrib, da_group); | |
2317 | struct tcmu_dev *udev = TCMU_DEV(da->da_dev); | |
2318 | ||
2319 | return snprintf(page, PAGE_SIZE, "%u\n", | |
2320 | TCMU_BLOCKS_TO_MBS(udev->max_blocks)); | |
2321 | } | |
2322 | CONFIGFS_ATTR_RO(tcmu_, max_data_area_mb); | |
2323 | ||
2d76443e | 2324 | static ssize_t tcmu_dev_config_show(struct config_item *item, char *page) |
ee018252 BL |
2325 | { |
2326 | struct se_dev_attrib *da = container_of(to_config_group(item), | |
2327 | struct se_dev_attrib, da_group); | |
2328 | struct tcmu_dev *udev = TCMU_DEV(da->da_dev); | |
2329 | ||
2330 | return snprintf(page, PAGE_SIZE, "%s\n", udev->dev_config); | |
2331 | } | |
2332 | ||
02ccfb54 ZL |
2333 | static int tcmu_send_dev_config_event(struct tcmu_dev *udev, |
2334 | const char *reconfig_data) | |
2335 | { | |
2336 | struct sk_buff *skb = NULL; | |
2337 | void *msg_header = NULL; | |
2338 | int ret = 0; | |
2339 | ||
2340 | ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE, | |
2341 | &skb, &msg_header); | |
2342 | if (ret < 0) | |
2343 | return ret; | |
2344 | ret = nla_put_string(skb, TCMU_ATTR_DEV_CFG, reconfig_data); | |
2345 | if (ret < 0) { | |
2346 | nlmsg_free(skb); | |
2347 | return ret; | |
2348 | } | |
2349 | return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE, | |
06add777 | 2350 | skb, msg_header); |
02ccfb54 ZL |
2351 | } |
2352 | ||
2353 | ||
2d76443e MC |
2354 | static ssize_t tcmu_dev_config_store(struct config_item *item, const char *page, |
2355 | size_t count) | |
ee018252 BL |
2356 | { |
2357 | struct se_dev_attrib *da = container_of(to_config_group(item), | |
2358 | struct se_dev_attrib, da_group); | |
2359 | struct tcmu_dev *udev = TCMU_DEV(da->da_dev); | |
2d76443e | 2360 | int ret, len; |
ee018252 | 2361 | |
2d76443e MC |
2362 | len = strlen(page); |
2363 | if (!len || len > TCMU_CONFIG_LEN - 1) | |
ee018252 | 2364 | return -EINVAL; |
ee018252 BL |
2365 | |
2366 | /* Check if device has been configured before */ | |
63d5be0f | 2367 | if (target_dev_configured(&udev->se_dev)) { |
02ccfb54 | 2368 | ret = tcmu_send_dev_config_event(udev, page); |
ee018252 BL |
2369 | if (ret) { |
2370 | pr_err("Unable to reconfigure device\n"); | |
2371 | return ret; | |
2372 | } | |
de8c5221 BL |
2373 | strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN); |
2374 | ||
2375 | ret = tcmu_update_uio_info(udev); | |
2376 | if (ret) | |
2377 | return ret; | |
2378 | return count; | |
ee018252 | 2379 | } |
2d76443e | 2380 | strlcpy(udev->dev_config, page, TCMU_CONFIG_LEN); |
ee018252 BL |
2381 | |
2382 | return count; | |
2383 | } | |
2d76443e | 2384 | CONFIGFS_ATTR(tcmu_, dev_config); |
ee018252 | 2385 | |
801fc54d BL |
2386 | static ssize_t tcmu_dev_size_show(struct config_item *item, char *page) |
2387 | { | |
2388 | struct se_dev_attrib *da = container_of(to_config_group(item), | |
2389 | struct se_dev_attrib, da_group); | |
2390 | struct tcmu_dev *udev = TCMU_DEV(da->da_dev); | |
2391 | ||
0e0d7526 | 2392 | return snprintf(page, PAGE_SIZE, "%llu\n", udev->dev_size); |
801fc54d BL |
2393 | } |
2394 | ||
84e28506 ZL |
2395 | static int tcmu_send_dev_size_event(struct tcmu_dev *udev, u64 size) |
2396 | { | |
2397 | struct sk_buff *skb = NULL; | |
2398 | void *msg_header = NULL; | |
2399 | int ret = 0; | |
2400 | ||
2401 | ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE, | |
2402 | &skb, &msg_header); | |
2403 | if (ret < 0) | |
2404 | return ret; | |
2405 | ret = nla_put_u64_64bit(skb, TCMU_ATTR_DEV_SIZE, | |
2406 | size, TCMU_ATTR_PAD); | |
2407 | if (ret < 0) { | |
2408 | nlmsg_free(skb); | |
2409 | return ret; | |
2410 | } | |
2411 | return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE, | |
06add777 | 2412 | skb, msg_header); |
84e28506 ZL |
2413 | } |
2414 | ||
801fc54d BL |
2415 | static ssize_t tcmu_dev_size_store(struct config_item *item, const char *page, |
2416 | size_t count) | |
2417 | { | |
2418 | struct se_dev_attrib *da = container_of(to_config_group(item), | |
2419 | struct se_dev_attrib, da_group); | |
2420 | struct tcmu_dev *udev = TCMU_DEV(da->da_dev); | |
2d76443e | 2421 | u64 val; |
801fc54d BL |
2422 | int ret; |
2423 | ||
2d76443e | 2424 | ret = kstrtou64(page, 0, &val); |
801fc54d BL |
2425 | if (ret < 0) |
2426 | return ret; | |
801fc54d BL |
2427 | |
2428 | /* Check if device has been configured before */ | |
63d5be0f | 2429 | if (target_dev_configured(&udev->se_dev)) { |
84e28506 | 2430 | ret = tcmu_send_dev_size_event(udev, val); |
801fc54d BL |
2431 | if (ret) { |
2432 | pr_err("Unable to reconfigure device\n"); | |
2433 | return ret; | |
2434 | } | |
2435 | } | |
2d76443e | 2436 | udev->dev_size = val; |
801fc54d BL |
2437 | return count; |
2438 | } | |
2439 | CONFIGFS_ATTR(tcmu_, dev_size); | |
2440 | ||
b849b456 KN |
2441 | static ssize_t tcmu_nl_reply_supported_show(struct config_item *item, |
2442 | char *page) | |
2443 | { | |
2444 | struct se_dev_attrib *da = container_of(to_config_group(item), | |
2445 | struct se_dev_attrib, da_group); | |
2446 | struct tcmu_dev *udev = TCMU_DEV(da->da_dev); | |
2447 | ||
2448 | return snprintf(page, PAGE_SIZE, "%d\n", udev->nl_reply_supported); | |
2449 | } | |
2450 | ||
2451 | static ssize_t tcmu_nl_reply_supported_store(struct config_item *item, | |
2452 | const char *page, size_t count) | |
2453 | { | |
2454 | struct se_dev_attrib *da = container_of(to_config_group(item), | |
2455 | struct se_dev_attrib, da_group); | |
2456 | struct tcmu_dev *udev = TCMU_DEV(da->da_dev); | |
2457 | s8 val; | |
2458 | int ret; | |
2459 | ||
2460 | ret = kstrtos8(page, 0, &val); | |
2461 | if (ret < 0) | |
2462 | return ret; | |
2463 | ||
2464 | udev->nl_reply_supported = val; | |
2465 | return count; | |
2466 | } | |
2467 | CONFIGFS_ATTR(tcmu_, nl_reply_supported); | |
2468 | ||
9a8bb606 BL |
2469 | static ssize_t tcmu_emulate_write_cache_show(struct config_item *item, |
2470 | char *page) | |
2471 | { | |
2472 | struct se_dev_attrib *da = container_of(to_config_group(item), | |
2473 | struct se_dev_attrib, da_group); | |
2474 | ||
2475 | return snprintf(page, PAGE_SIZE, "%i\n", da->emulate_write_cache); | |
2476 | } | |
2477 | ||
33d065cc ZL |
2478 | static int tcmu_send_emulate_write_cache(struct tcmu_dev *udev, u8 val) |
2479 | { | |
2480 | struct sk_buff *skb = NULL; | |
2481 | void *msg_header = NULL; | |
2482 | int ret = 0; | |
2483 | ||
2484 | ret = tcmu_netlink_event_init(udev, TCMU_CMD_RECONFIG_DEVICE, | |
2485 | &skb, &msg_header); | |
2486 | if (ret < 0) | |
2487 | return ret; | |
2488 | ret = nla_put_u8(skb, TCMU_ATTR_WRITECACHE, val); | |
2489 | if (ret < 0) { | |
2490 | nlmsg_free(skb); | |
2491 | return ret; | |
2492 | } | |
2493 | return tcmu_netlink_event_send(udev, TCMU_CMD_RECONFIG_DEVICE, | |
06add777 | 2494 | skb, msg_header); |
33d065cc ZL |
2495 | } |
2496 | ||
9a8bb606 BL |
2497 | static ssize_t tcmu_emulate_write_cache_store(struct config_item *item, |
2498 | const char *page, size_t count) | |
2499 | { | |
2500 | struct se_dev_attrib *da = container_of(to_config_group(item), | |
2501 | struct se_dev_attrib, da_group); | |
1068be7b | 2502 | struct tcmu_dev *udev = TCMU_DEV(da->da_dev); |
2d76443e | 2503 | u8 val; |
9a8bb606 BL |
2504 | int ret; |
2505 | ||
2d76443e | 2506 | ret = kstrtou8(page, 0, &val); |
9a8bb606 BL |
2507 | if (ret < 0) |
2508 | return ret; | |
2509 | ||
1068be7b | 2510 | /* Check if device has been configured before */ |
63d5be0f | 2511 | if (target_dev_configured(&udev->se_dev)) { |
33d065cc | 2512 | ret = tcmu_send_emulate_write_cache(udev, val); |
1068be7b BL |
2513 | if (ret) { |
2514 | pr_err("Unable to reconfigure device\n"); | |
2515 | return ret; | |
2516 | } | |
2517 | } | |
2d76443e MC |
2518 | |
2519 | da->emulate_write_cache = val; | |
9a8bb606 BL |
2520 | return count; |
2521 | } | |
2522 | CONFIGFS_ATTR(tcmu_, emulate_write_cache); | |
2523 | ||
892782ca MC |
2524 | static ssize_t tcmu_block_dev_show(struct config_item *item, char *page) |
2525 | { | |
2526 | struct se_device *se_dev = container_of(to_config_group(item), | |
2527 | struct se_device, | |
2528 | dev_action_group); | |
2529 | struct tcmu_dev *udev = TCMU_DEV(se_dev); | |
2530 | ||
2531 | if (test_bit(TCMU_DEV_BIT_BLOCKED, &udev->flags)) | |
2532 | return snprintf(page, PAGE_SIZE, "%s\n", "blocked"); | |
2533 | else | |
2534 | return snprintf(page, PAGE_SIZE, "%s\n", "unblocked"); | |
2535 | } | |
2536 | ||
2537 | static ssize_t tcmu_block_dev_store(struct config_item *item, const char *page, | |
2538 | size_t count) | |
2539 | { | |
2540 | struct se_device *se_dev = container_of(to_config_group(item), | |
2541 | struct se_device, | |
2542 | dev_action_group); | |
2543 | struct tcmu_dev *udev = TCMU_DEV(se_dev); | |
2544 | u8 val; | |
2545 | int ret; | |
2546 | ||
a30b0473 MC |
2547 | if (!target_dev_configured(&udev->se_dev)) { |
2548 | pr_err("Device is not configured.\n"); | |
2549 | return -EINVAL; | |
2550 | } | |
2551 | ||
892782ca MC |
2552 | ret = kstrtou8(page, 0, &val); |
2553 | if (ret < 0) | |
2554 | return ret; | |
2555 | ||
2556 | if (val > 1) { | |
2557 | pr_err("Invalid block value %d\n", val); | |
2558 | return -EINVAL; | |
2559 | } | |
2560 | ||
2561 | if (!val) | |
2562 | tcmu_unblock_dev(udev); | |
2563 | else | |
2564 | tcmu_block_dev(udev); | |
2565 | return count; | |
2566 | } | |
2567 | CONFIGFS_ATTR(tcmu_, block_dev); | |
2568 | ||
2569 | static ssize_t tcmu_reset_ring_store(struct config_item *item, const char *page, | |
2570 | size_t count) | |
2571 | { | |
2572 | struct se_device *se_dev = container_of(to_config_group(item), | |
2573 | struct se_device, | |
2574 | dev_action_group); | |
2575 | struct tcmu_dev *udev = TCMU_DEV(se_dev); | |
2576 | u8 val; | |
2577 | int ret; | |
2578 | ||
a30b0473 MC |
2579 | if (!target_dev_configured(&udev->se_dev)) { |
2580 | pr_err("Device is not configured.\n"); | |
2581 | return -EINVAL; | |
2582 | } | |
2583 | ||
892782ca MC |
2584 | ret = kstrtou8(page, 0, &val); |
2585 | if (ret < 0) | |
2586 | return ret; | |
2587 | ||
2588 | if (val != 1 && val != 2) { | |
2589 | pr_err("Invalid reset ring value %d\n", val); | |
2590 | return -EINVAL; | |
2591 | } | |
2592 | ||
2593 | tcmu_reset_ring(udev, val); | |
2594 | return count; | |
2595 | } | |
2596 | CONFIGFS_ATTR_WO(tcmu_, reset_ring); | |
2597 | ||
5821783b | 2598 | static struct configfs_attribute *tcmu_attrib_attrs[] = { |
801fc54d | 2599 | &tcmu_attr_cmd_time_out, |
9103575a | 2600 | &tcmu_attr_qfull_time_out, |
80eb8761 | 2601 | &tcmu_attr_max_data_area_mb, |
2d76443e | 2602 | &tcmu_attr_dev_config, |
801fc54d BL |
2603 | &tcmu_attr_dev_size, |
2604 | &tcmu_attr_emulate_write_cache, | |
b849b456 | 2605 | &tcmu_attr_nl_reply_supported, |
801fc54d BL |
2606 | NULL, |
2607 | }; | |
2608 | ||
7d7a7435 NB |
2609 | static struct configfs_attribute **tcmu_attrs; |
2610 | ||
892782ca MC |
2611 | static struct configfs_attribute *tcmu_action_attrs[] = { |
2612 | &tcmu_attr_block_dev, | |
2613 | &tcmu_attr_reset_ring, | |
2614 | NULL, | |
2615 | }; | |
2616 | ||
7d7a7435 | 2617 | static struct target_backend_ops tcmu_ops = { |
7c9e7a6f | 2618 | .name = "user", |
7c9e7a6f | 2619 | .owner = THIS_MODULE, |
a3541703 | 2620 | .transport_flags = TRANSPORT_FLAG_PASSTHROUGH, |
7c9e7a6f AG |
2621 | .attach_hba = tcmu_attach_hba, |
2622 | .detach_hba = tcmu_detach_hba, | |
2623 | .alloc_device = tcmu_alloc_device, | |
2624 | .configure_device = tcmu_configure_device, | |
92634706 | 2625 | .destroy_device = tcmu_destroy_device, |
7c9e7a6f AG |
2626 | .free_device = tcmu_free_device, |
2627 | .parse_cdb = tcmu_parse_cdb, | |
2628 | .set_configfs_dev_params = tcmu_set_configfs_dev_params, | |
2629 | .show_configfs_dev_params = tcmu_show_configfs_dev_params, | |
2630 | .get_device_type = sbc_get_device_type, | |
2631 | .get_blocks = tcmu_get_blocks, | |
892782ca | 2632 | .tb_dev_action_attrs = tcmu_action_attrs, |
7c9e7a6f AG |
2633 | }; |
2634 | ||
89ec9cfd | 2635 | static void find_free_blocks(void) |
b6df4b79 XL |
2636 | { |
2637 | struct tcmu_dev *udev; | |
2638 | loff_t off; | |
af1dd7ff MC |
2639 | u32 start, end, block, total_freed = 0; |
2640 | ||
80eb8761 | 2641 | if (atomic_read(&global_db_count) <= tcmu_global_max_blocks) |
af1dd7ff | 2642 | return; |
b6df4b79 | 2643 | |
89ec9cfd MC |
2644 | mutex_lock(&root_udev_mutex); |
2645 | list_for_each_entry(udev, &root_udev, node) { | |
2646 | mutex_lock(&udev->cmdr_lock); | |
b6df4b79 | 2647 | |
dc335a99 MC |
2648 | if (!target_dev_configured(&udev->se_dev)) { |
2649 | mutex_unlock(&udev->cmdr_lock); | |
2650 | continue; | |
2651 | } | |
2652 | ||
89ec9cfd MC |
2653 | /* Try to complete the finished commands first */ |
2654 | tcmu_handle_completions(udev); | |
d906d8af | 2655 | |
af1dd7ff MC |
2656 | /* Skip the udevs in idle */ |
2657 | if (!udev->dbi_thresh) { | |
89ec9cfd MC |
2658 | mutex_unlock(&udev->cmdr_lock); |
2659 | continue; | |
2660 | } | |
b6df4b79 | 2661 | |
89ec9cfd MC |
2662 | end = udev->dbi_max + 1; |
2663 | block = find_last_bit(udev->data_bitmap, end); | |
2664 | if (block == udev->dbi_max) { | |
2665 | /* | |
af1dd7ff MC |
2666 | * The last bit is dbi_max, so it is not possible |
2667 | * reclaim any blocks. | |
89ec9cfd MC |
2668 | */ |
2669 | mutex_unlock(&udev->cmdr_lock); | |
2670 | continue; | |
2671 | } else if (block == end) { | |
2672 | /* The current udev will goto idle state */ | |
2673 | udev->dbi_thresh = start = 0; | |
2674 | udev->dbi_max = 0; | |
2675 | } else { | |
2676 | udev->dbi_thresh = start = block + 1; | |
2677 | udev->dbi_max = block; | |
2678 | } | |
b6df4b79 | 2679 | |
89ec9cfd MC |
2680 | /* Here will truncate the data area from off */ |
2681 | off = udev->data_off + start * DATA_BLOCK_SIZE; | |
2682 | unmap_mapping_range(udev->inode->i_mapping, off, 0, 1); | |
b6df4b79 | 2683 | |
89ec9cfd MC |
2684 | /* Release the block pages */ |
2685 | tcmu_blocks_release(&udev->data_blocks, start, end); | |
2686 | mutex_unlock(&udev->cmdr_lock); | |
b6df4b79 | 2687 | |
af1dd7ff MC |
2688 | total_freed += end - start; |
2689 | pr_debug("Freed %u blocks (total %u) from %s.\n", end - start, | |
2690 | total_freed, udev->name); | |
89ec9cfd MC |
2691 | } |
2692 | mutex_unlock(&root_udev_mutex); | |
af1dd7ff | 2693 | |
80eb8761 | 2694 | if (atomic_read(&global_db_count) > tcmu_global_max_blocks) |
af1dd7ff | 2695 | schedule_delayed_work(&tcmu_unmap_work, msecs_to_jiffies(5000)); |
89ec9cfd MC |
2696 | } |
2697 | ||
488ebe4c MC |
2698 | static void check_timedout_devices(void) |
2699 | { | |
2700 | struct tcmu_dev *udev, *tmp_dev; | |
2701 | LIST_HEAD(devs); | |
2702 | ||
2703 | spin_lock_bh(&timed_out_udevs_lock); | |
2704 | list_splice_init(&timed_out_udevs, &devs); | |
2705 | ||
2706 | list_for_each_entry_safe(udev, tmp_dev, &devs, timedout_entry) { | |
2707 | list_del_init(&udev->timedout_entry); | |
2708 | spin_unlock_bh(&timed_out_udevs_lock); | |
2709 | ||
6fddcb77 | 2710 | mutex_lock(&udev->cmdr_lock); |
488ebe4c | 2711 | idr_for_each(&udev->commands, tcmu_check_expired_cmd, NULL); |
a94a2572 XL |
2712 | |
2713 | tcmu_set_next_deadline(&udev->inflight_queue, &udev->cmd_timer); | |
2714 | tcmu_set_next_deadline(&udev->qfull_queue, &udev->qfull_timer); | |
2715 | ||
6fddcb77 | 2716 | mutex_unlock(&udev->cmdr_lock); |
488ebe4c MC |
2717 | |
2718 | spin_lock_bh(&timed_out_udevs_lock); | |
2719 | } | |
2720 | ||
2721 | spin_unlock_bh(&timed_out_udevs_lock); | |
2722 | } | |
2723 | ||
9972cebb | 2724 | static void tcmu_unmap_work_fn(struct work_struct *work) |
89ec9cfd | 2725 | { |
488ebe4c | 2726 | check_timedout_devices(); |
9972cebb | 2727 | find_free_blocks(); |
b6df4b79 XL |
2728 | } |
2729 | ||
7c9e7a6f AG |
2730 | static int __init tcmu_module_init(void) |
2731 | { | |
801fc54d | 2732 | int ret, i, k, len = 0; |
7c9e7a6f AG |
2733 | |
2734 | BUILD_BUG_ON((sizeof(struct tcmu_cmd_entry) % TCMU_OP_ALIGN_SIZE) != 0); | |
2735 | ||
af1dd7ff | 2736 | INIT_DELAYED_WORK(&tcmu_unmap_work, tcmu_unmap_work_fn); |
9972cebb | 2737 | |
7c9e7a6f AG |
2738 | tcmu_cmd_cache = kmem_cache_create("tcmu_cmd_cache", |
2739 | sizeof(struct tcmu_cmd), | |
2740 | __alignof__(struct tcmu_cmd), | |
2741 | 0, NULL); | |
2742 | if (!tcmu_cmd_cache) | |
2743 | return -ENOMEM; | |
2744 | ||
2745 | tcmu_root_device = root_device_register("tcm_user"); | |
2746 | if (IS_ERR(tcmu_root_device)) { | |
2747 | ret = PTR_ERR(tcmu_root_device); | |
2748 | goto out_free_cache; | |
2749 | } | |
2750 | ||
2751 | ret = genl_register_family(&tcmu_genl_family); | |
2752 | if (ret < 0) { | |
2753 | goto out_unreg_device; | |
2754 | } | |
2755 | ||
7d7a7435 NB |
2756 | for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) { |
2757 | len += sizeof(struct configfs_attribute *); | |
2758 | } | |
801fc54d BL |
2759 | for (i = 0; tcmu_attrib_attrs[i] != NULL; i++) { |
2760 | len += sizeof(struct configfs_attribute *); | |
2761 | } | |
2762 | len += sizeof(struct configfs_attribute *); | |
7d7a7435 NB |
2763 | |
2764 | tcmu_attrs = kzalloc(len, GFP_KERNEL); | |
2765 | if (!tcmu_attrs) { | |
2766 | ret = -ENOMEM; | |
2767 | goto out_unreg_genl; | |
2768 | } | |
2769 | ||
2770 | for (i = 0; passthrough_attrib_attrs[i] != NULL; i++) { | |
2771 | tcmu_attrs[i] = passthrough_attrib_attrs[i]; | |
2772 | } | |
801fc54d BL |
2773 | for (k = 0; tcmu_attrib_attrs[k] != NULL; k++) { |
2774 | tcmu_attrs[i] = tcmu_attrib_attrs[k]; | |
2775 | i++; | |
2776 | } | |
7d7a7435 NB |
2777 | tcmu_ops.tb_dev_attrib_attrs = tcmu_attrs; |
2778 | ||
0a06d430 | 2779 | ret = transport_backend_register(&tcmu_ops); |
7c9e7a6f | 2780 | if (ret) |
7d7a7435 | 2781 | goto out_attrs; |
7c9e7a6f AG |
2782 | |
2783 | return 0; | |
2784 | ||
7d7a7435 NB |
2785 | out_attrs: |
2786 | kfree(tcmu_attrs); | |
7c9e7a6f AG |
2787 | out_unreg_genl: |
2788 | genl_unregister_family(&tcmu_genl_family); | |
2789 | out_unreg_device: | |
2790 | root_device_unregister(tcmu_root_device); | |
2791 | out_free_cache: | |
2792 | kmem_cache_destroy(tcmu_cmd_cache); | |
2793 | ||
2794 | return ret; | |
2795 | } | |
2796 | ||
2797 | static void __exit tcmu_module_exit(void) | |
2798 | { | |
af1dd7ff | 2799 | cancel_delayed_work_sync(&tcmu_unmap_work); |
0a06d430 | 2800 | target_backend_unregister(&tcmu_ops); |
7d7a7435 | 2801 | kfree(tcmu_attrs); |
7c9e7a6f AG |
2802 | genl_unregister_family(&tcmu_genl_family); |
2803 | root_device_unregister(tcmu_root_device); | |
2804 | kmem_cache_destroy(tcmu_cmd_cache); | |
2805 | } | |
2806 | ||
2807 | MODULE_DESCRIPTION("TCM USER subsystem plugin"); | |
2808 | MODULE_AUTHOR("Shaohua Li <shli@kernel.org>"); | |
2809 | MODULE_AUTHOR("Andy Grover <agrover@redhat.com>"); | |
2810 | MODULE_LICENSE("GPL"); | |
2811 | ||
2812 | module_init(tcmu_module_init); | |
2813 | module_exit(tcmu_module_exit); |