]>
Commit | Line | Data |
---|---|---|
c66ac9db NB |
1 | /******************************************************************************* |
2 | * Filename: target_core_iblock.c | |
3 | * | |
4 | * This file contains the Storage Engine <-> Linux BlockIO transport | |
5 | * specific functions. | |
6 | * | |
7 | * Copyright (c) 2003, 2004, 2005 PyX Technologies, Inc. | |
8 | * Copyright (c) 2005, 2006, 2007 SBE, Inc. | |
9 | * Copyright (c) 2007-2010 Rising Tide Systems | |
10 | * Copyright (c) 2008-2010 Linux-iSCSI.org | |
11 | * | |
12 | * Nicholas A. Bellinger <nab@kernel.org> | |
13 | * | |
14 | * This program is free software; you can redistribute it and/or modify | |
15 | * it under the terms of the GNU General Public License as published by | |
16 | * the Free Software Foundation; either version 2 of the License, or | |
17 | * (at your option) any later version. | |
18 | * | |
19 | * This program is distributed in the hope that it will be useful, | |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | * GNU General Public License for more details. | |
23 | * | |
24 | * You should have received a copy of the GNU General Public License | |
25 | * along with this program; if not, write to the Free Software | |
26 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
27 | * | |
28 | ******************************************************************************/ | |
29 | ||
c66ac9db NB |
30 | #include <linux/string.h> |
31 | #include <linux/parser.h> | |
32 | #include <linux/timer.h> | |
33 | #include <linux/fs.h> | |
34 | #include <linux/blkdev.h> | |
35 | #include <linux/slab.h> | |
36 | #include <linux/spinlock.h> | |
c66ac9db NB |
37 | #include <linux/bio.h> |
38 | #include <linux/genhd.h> | |
39 | #include <linux/file.h> | |
827509e3 | 40 | #include <linux/module.h> |
c66ac9db NB |
41 | #include <scsi/scsi.h> |
42 | #include <scsi/scsi_host.h> | |
43 | ||
44 | #include <target/target_core_base.h> | |
c4795fb2 | 45 | #include <target/target_core_backend.h> |
c66ac9db NB |
46 | |
47 | #include "target_core_iblock.h" | |
48 | ||
d5b4a21b CH |
49 | #define IBLOCK_MAX_BIO_PER_TASK 32 /* max # of bios to submit at a time */ |
50 | #define IBLOCK_BIO_POOL_SIZE 128 | |
51 | ||
c66ac9db NB |
52 | static struct se_subsystem_api iblock_template; |
53 | ||
54 | static void iblock_bio_done(struct bio *, int); | |
55 | ||
56 | /* iblock_attach_hba(): (Part of se_subsystem_api_t template) | |
57 | * | |
58 | * | |
59 | */ | |
60 | static int iblock_attach_hba(struct se_hba *hba, u32 host_id) | |
61 | { | |
6708bb27 | 62 | pr_debug("CORE_HBA[%d] - TCM iBlock HBA Driver %s on" |
c66ac9db NB |
63 | " Generic Target Core Stack %s\n", hba->hba_id, |
64 | IBLOCK_VERSION, TARGET_CORE_MOD_VERSION); | |
c66ac9db NB |
65 | return 0; |
66 | } | |
67 | ||
68 | static void iblock_detach_hba(struct se_hba *hba) | |
69 | { | |
c66ac9db NB |
70 | } |
71 | ||
72 | static void *iblock_allocate_virtdevice(struct se_hba *hba, const char *name) | |
73 | { | |
74 | struct iblock_dev *ib_dev = NULL; | |
c66ac9db NB |
75 | |
76 | ib_dev = kzalloc(sizeof(struct iblock_dev), GFP_KERNEL); | |
6708bb27 AG |
77 | if (!ib_dev) { |
78 | pr_err("Unable to allocate struct iblock_dev\n"); | |
c66ac9db NB |
79 | return NULL; |
80 | } | |
c66ac9db | 81 | |
6708bb27 | 82 | pr_debug( "IBLOCK: Allocated ib_dev for %s\n", name); |
c66ac9db NB |
83 | |
84 | return ib_dev; | |
85 | } | |
86 | ||
87 | static struct se_device *iblock_create_virtdevice( | |
88 | struct se_hba *hba, | |
89 | struct se_subsystem_dev *se_dev, | |
90 | void *p) | |
91 | { | |
92 | struct iblock_dev *ib_dev = p; | |
93 | struct se_device *dev; | |
94 | struct se_dev_limits dev_limits; | |
95 | struct block_device *bd = NULL; | |
96 | struct request_queue *q; | |
97 | struct queue_limits *limits; | |
98 | u32 dev_flags = 0; | |
44bfd018 | 99 | fmode_t mode; |
613640e4 | 100 | int ret = -EINVAL; |
c66ac9db | 101 | |
6708bb27 AG |
102 | if (!ib_dev) { |
103 | pr_err("Unable to locate struct iblock_dev parameter\n"); | |
613640e4 | 104 | return ERR_PTR(ret); |
c66ac9db NB |
105 | } |
106 | memset(&dev_limits, 0, sizeof(struct se_dev_limits)); | |
d5b4a21b CH |
107 | |
108 | ib_dev->ibd_bio_set = bioset_create(IBLOCK_BIO_POOL_SIZE, 0); | |
6708bb27 AG |
109 | if (!ib_dev->ibd_bio_set) { |
110 | pr_err("IBLOCK: Unable to create bioset()\n"); | |
613640e4 | 111 | return ERR_PTR(-ENOMEM); |
c66ac9db | 112 | } |
6708bb27 | 113 | pr_debug("IBLOCK: Created bio_set()\n"); |
c66ac9db NB |
114 | /* |
115 | * iblock_check_configfs_dev_params() ensures that ib_dev->ibd_udev_path | |
116 | * must already have been set in order for echo 1 > $HBA/$DEV/enable to run. | |
117 | */ | |
6708bb27 | 118 | pr_debug( "IBLOCK: Claiming struct block_device: %s\n", |
c66ac9db NB |
119 | ib_dev->ibd_udev_path); |
120 | ||
44bfd018 AG |
121 | mode = FMODE_READ|FMODE_EXCL; |
122 | if (!ib_dev->ibd_readonly) | |
123 | mode |= FMODE_WRITE; | |
124 | ||
125 | bd = blkdev_get_by_path(ib_dev->ibd_udev_path, mode, ib_dev); | |
613640e4 NB |
126 | if (IS_ERR(bd)) { |
127 | ret = PTR_ERR(bd); | |
c66ac9db | 128 | goto failed; |
613640e4 | 129 | } |
c66ac9db NB |
130 | /* |
131 | * Setup the local scope queue_limits from struct request_queue->limits | |
132 | * to pass into transport_add_device_to_core_hba() as struct se_dev_limits. | |
133 | */ | |
134 | q = bdev_get_queue(bd); | |
135 | limits = &dev_limits.limits; | |
136 | limits->logical_block_size = bdev_logical_block_size(bd); | |
d5b4a21b CH |
137 | limits->max_hw_sectors = UINT_MAX; |
138 | limits->max_sectors = UINT_MAX; | |
8f3d14e2 NB |
139 | dev_limits.hw_queue_depth = q->nr_requests; |
140 | dev_limits.queue_depth = q->nr_requests; | |
c66ac9db | 141 | |
c66ac9db NB |
142 | ib_dev->ibd_bd = bd; |
143 | ||
144 | dev = transport_add_device_to_core_hba(hba, | |
5951146d | 145 | &iblock_template, se_dev, dev_flags, ib_dev, |
c66ac9db | 146 | &dev_limits, "IBLOCK", IBLOCK_VERSION); |
6708bb27 | 147 | if (!dev) |
c66ac9db NB |
148 | goto failed; |
149 | ||
c66ac9db NB |
150 | /* |
151 | * Check if the underlying struct block_device request_queue supports | |
152 | * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM | |
153 | * in ATA and we need to set TPE=1 | |
154 | */ | |
613640e4 | 155 | if (blk_queue_discard(q)) { |
e3d6f909 | 156 | dev->se_sub_dev->se_dev_attrib.max_unmap_lba_count = |
c66ac9db NB |
157 | q->limits.max_discard_sectors; |
158 | /* | |
159 | * Currently hardcoded to 1 in Linux/SCSI code.. | |
160 | */ | |
e3d6f909 AG |
161 | dev->se_sub_dev->se_dev_attrib.max_unmap_block_desc_count = 1; |
162 | dev->se_sub_dev->se_dev_attrib.unmap_granularity = | |
7347b5ff | 163 | q->limits.discard_granularity >> 9; |
e3d6f909 | 164 | dev->se_sub_dev->se_dev_attrib.unmap_granularity_alignment = |
c66ac9db NB |
165 | q->limits.discard_alignment; |
166 | ||
6708bb27 | 167 | pr_debug("IBLOCK: BLOCK Discard support available," |
c66ac9db NB |
168 | " disabled by default\n"); |
169 | } | |
170 | ||
e22a7f07 RD |
171 | if (blk_queue_nonrot(q)) |
172 | dev->se_sub_dev->se_dev_attrib.is_nonrot = 1; | |
173 | ||
c66ac9db NB |
174 | return dev; |
175 | ||
176 | failed: | |
177 | if (ib_dev->ibd_bio_set) { | |
178 | bioset_free(ib_dev->ibd_bio_set); | |
179 | ib_dev->ibd_bio_set = NULL; | |
180 | } | |
181 | ib_dev->ibd_bd = NULL; | |
613640e4 | 182 | return ERR_PTR(ret); |
c66ac9db NB |
183 | } |
184 | ||
185 | static void iblock_free_device(void *p) | |
186 | { | |
187 | struct iblock_dev *ib_dev = p; | |
188 | ||
bc665524 NB |
189 | if (ib_dev->ibd_bd != NULL) |
190 | blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL); | |
191 | if (ib_dev->ibd_bio_set != NULL) | |
192 | bioset_free(ib_dev->ibd_bio_set); | |
c66ac9db NB |
193 | kfree(ib_dev); |
194 | } | |
195 | ||
c66ac9db NB |
196 | static unsigned long long iblock_emulate_read_cap_with_block_size( |
197 | struct se_device *dev, | |
198 | struct block_device *bd, | |
199 | struct request_queue *q) | |
200 | { | |
201 | unsigned long long blocks_long = (div_u64(i_size_read(bd->bd_inode), | |
202 | bdev_logical_block_size(bd)) - 1); | |
203 | u32 block_size = bdev_logical_block_size(bd); | |
204 | ||
e3d6f909 | 205 | if (block_size == dev->se_sub_dev->se_dev_attrib.block_size) |
c66ac9db NB |
206 | return blocks_long; |
207 | ||
208 | switch (block_size) { | |
209 | case 4096: | |
e3d6f909 | 210 | switch (dev->se_sub_dev->se_dev_attrib.block_size) { |
c66ac9db NB |
211 | case 2048: |
212 | blocks_long <<= 1; | |
213 | break; | |
214 | case 1024: | |
215 | blocks_long <<= 2; | |
216 | break; | |
217 | case 512: | |
218 | blocks_long <<= 3; | |
219 | default: | |
220 | break; | |
221 | } | |
222 | break; | |
223 | case 2048: | |
e3d6f909 | 224 | switch (dev->se_sub_dev->se_dev_attrib.block_size) { |
c66ac9db NB |
225 | case 4096: |
226 | blocks_long >>= 1; | |
227 | break; | |
228 | case 1024: | |
229 | blocks_long <<= 1; | |
230 | break; | |
231 | case 512: | |
232 | blocks_long <<= 2; | |
233 | break; | |
234 | default: | |
235 | break; | |
236 | } | |
237 | break; | |
238 | case 1024: | |
e3d6f909 | 239 | switch (dev->se_sub_dev->se_dev_attrib.block_size) { |
c66ac9db NB |
240 | case 4096: |
241 | blocks_long >>= 2; | |
242 | break; | |
243 | case 2048: | |
244 | blocks_long >>= 1; | |
245 | break; | |
246 | case 512: | |
247 | blocks_long <<= 1; | |
248 | break; | |
249 | default: | |
250 | break; | |
251 | } | |
252 | break; | |
253 | case 512: | |
e3d6f909 | 254 | switch (dev->se_sub_dev->se_dev_attrib.block_size) { |
c66ac9db NB |
255 | case 4096: |
256 | blocks_long >>= 3; | |
257 | break; | |
258 | case 2048: | |
259 | blocks_long >>= 2; | |
260 | break; | |
261 | case 1024: | |
262 | blocks_long >>= 1; | |
263 | break; | |
264 | default: | |
265 | break; | |
266 | } | |
267 | break; | |
268 | default: | |
269 | break; | |
270 | } | |
271 | ||
272 | return blocks_long; | |
273 | } | |
274 | ||
df5fa691 CH |
275 | static void iblock_end_io_flush(struct bio *bio, int err) |
276 | { | |
277 | struct se_cmd *cmd = bio->bi_private; | |
278 | ||
279 | if (err) | |
280 | pr_err("IBLOCK: cache flush failed: %d\n", err); | |
281 | ||
5787cacd CH |
282 | if (cmd) { |
283 | if (err) { | |
284 | cmd->scsi_sense_reason = | |
285 | TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; | |
286 | target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION); | |
287 | } else { | |
288 | target_complete_cmd(cmd, SAM_STAT_GOOD); | |
289 | } | |
290 | } | |
291 | ||
df5fa691 CH |
292 | bio_put(bio); |
293 | } | |
294 | ||
c66ac9db | 295 | /* |
df5fa691 CH |
296 | * Implement SYCHRONIZE CACHE. Note that we can't handle lba ranges and must |
297 | * always flush the whole cache. | |
c66ac9db | 298 | */ |
6bb35e00 | 299 | static void iblock_emulate_sync_cache(struct se_cmd *cmd) |
c66ac9db | 300 | { |
c66ac9db | 301 | struct iblock_dev *ib_dev = cmd->se_dev->dev_ptr; |
a1d8b49a | 302 | int immed = (cmd->t_task_cdb[1] & 0x2); |
df5fa691 | 303 | struct bio *bio; |
c66ac9db NB |
304 | |
305 | /* | |
306 | * If the Immediate bit is set, queue up the GOOD response | |
df5fa691 | 307 | * for this SYNCHRONIZE_CACHE op. |
c66ac9db NB |
308 | */ |
309 | if (immed) | |
5787cacd | 310 | target_complete_cmd(cmd, SAM_STAT_GOOD); |
c66ac9db | 311 | |
df5fa691 CH |
312 | bio = bio_alloc(GFP_KERNEL, 0); |
313 | bio->bi_end_io = iblock_end_io_flush; | |
314 | bio->bi_bdev = ib_dev->ibd_bd; | |
c66ac9db | 315 | if (!immed) |
df5fa691 CH |
316 | bio->bi_private = cmd; |
317 | submit_bio(WRITE_FLUSH, bio); | |
c66ac9db NB |
318 | } |
319 | ||
c66ac9db NB |
320 | static int iblock_do_discard(struct se_device *dev, sector_t lba, u32 range) |
321 | { | |
322 | struct iblock_dev *ibd = dev->dev_ptr; | |
323 | struct block_device *bd = ibd->ibd_bd; | |
324 | int barrier = 0; | |
325 | ||
326 | return blkdev_issue_discard(bd, lba, range, GFP_KERNEL, barrier); | |
327 | } | |
328 | ||
c66ac9db | 329 | enum { |
44bfd018 | 330 | Opt_udev_path, Opt_readonly, Opt_force, Opt_err |
c66ac9db NB |
331 | }; |
332 | ||
333 | static match_table_t tokens = { | |
334 | {Opt_udev_path, "udev_path=%s"}, | |
44bfd018 | 335 | {Opt_readonly, "readonly=%d"}, |
c66ac9db NB |
336 | {Opt_force, "force=%d"}, |
337 | {Opt_err, NULL} | |
338 | }; | |
339 | ||
340 | static ssize_t iblock_set_configfs_dev_params(struct se_hba *hba, | |
341 | struct se_subsystem_dev *se_dev, | |
342 | const char *page, ssize_t count) | |
343 | { | |
344 | struct iblock_dev *ib_dev = se_dev->se_dev_su_ptr; | |
6d180253 | 345 | char *orig, *ptr, *arg_p, *opts; |
c66ac9db | 346 | substring_t args[MAX_OPT_ARGS]; |
21bca31c | 347 | int ret = 0, token; |
44bfd018 | 348 | unsigned long tmp_readonly; |
c66ac9db NB |
349 | |
350 | opts = kstrdup(page, GFP_KERNEL); | |
351 | if (!opts) | |
352 | return -ENOMEM; | |
353 | ||
354 | orig = opts; | |
355 | ||
90c161b6 | 356 | while ((ptr = strsep(&opts, ",\n")) != NULL) { |
c66ac9db NB |
357 | if (!*ptr) |
358 | continue; | |
359 | ||
360 | token = match_token(ptr, tokens, args); | |
361 | switch (token) { | |
362 | case Opt_udev_path: | |
363 | if (ib_dev->ibd_bd) { | |
6708bb27 | 364 | pr_err("Unable to set udev_path= while" |
c66ac9db NB |
365 | " ib_dev->ibd_bd exists\n"); |
366 | ret = -EEXIST; | |
367 | goto out; | |
368 | } | |
6d180253 JJ |
369 | arg_p = match_strdup(&args[0]); |
370 | if (!arg_p) { | |
371 | ret = -ENOMEM; | |
372 | break; | |
373 | } | |
374 | snprintf(ib_dev->ibd_udev_path, SE_UDEV_PATH_LEN, | |
375 | "%s", arg_p); | |
376 | kfree(arg_p); | |
6708bb27 | 377 | pr_debug("IBLOCK: Referencing UDEV path: %s\n", |
c66ac9db NB |
378 | ib_dev->ibd_udev_path); |
379 | ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH; | |
380 | break; | |
44bfd018 AG |
381 | case Opt_readonly: |
382 | arg_p = match_strdup(&args[0]); | |
383 | if (!arg_p) { | |
384 | ret = -ENOMEM; | |
385 | break; | |
386 | } | |
387 | ret = strict_strtoul(arg_p, 0, &tmp_readonly); | |
388 | kfree(arg_p); | |
389 | if (ret < 0) { | |
390 | pr_err("strict_strtoul() failed for" | |
391 | " readonly=\n"); | |
392 | goto out; | |
393 | } | |
394 | ib_dev->ibd_readonly = tmp_readonly; | |
395 | pr_debug("IBLOCK: readonly: %d\n", ib_dev->ibd_readonly); | |
396 | break; | |
c66ac9db | 397 | case Opt_force: |
c66ac9db NB |
398 | break; |
399 | default: | |
400 | break; | |
401 | } | |
402 | } | |
403 | ||
404 | out: | |
405 | kfree(orig); | |
406 | return (!ret) ? count : ret; | |
407 | } | |
408 | ||
409 | static ssize_t iblock_check_configfs_dev_params( | |
410 | struct se_hba *hba, | |
411 | struct se_subsystem_dev *se_dev) | |
412 | { | |
413 | struct iblock_dev *ibd = se_dev->se_dev_su_ptr; | |
414 | ||
415 | if (!(ibd->ibd_flags & IBDF_HAS_UDEV_PATH)) { | |
6708bb27 | 416 | pr_err("Missing udev_path= parameters for IBLOCK\n"); |
e3d6f909 | 417 | return -EINVAL; |
c66ac9db NB |
418 | } |
419 | ||
420 | return 0; | |
421 | } | |
422 | ||
423 | static ssize_t iblock_show_configfs_dev_params( | |
424 | struct se_hba *hba, | |
425 | struct se_subsystem_dev *se_dev, | |
426 | char *b) | |
427 | { | |
428 | struct iblock_dev *ibd = se_dev->se_dev_su_ptr; | |
429 | struct block_device *bd = ibd->ibd_bd; | |
430 | char buf[BDEVNAME_SIZE]; | |
431 | ssize_t bl = 0; | |
432 | ||
433 | if (bd) | |
434 | bl += sprintf(b + bl, "iBlock device: %s", | |
435 | bdevname(bd, buf)); | |
44bfd018 AG |
436 | if (ibd->ibd_flags & IBDF_HAS_UDEV_PATH) |
437 | bl += sprintf(b + bl, " UDEV PATH: %s", | |
c66ac9db | 438 | ibd->ibd_udev_path); |
44bfd018 | 439 | bl += sprintf(b + bl, " readonly: %d\n", ibd->ibd_readonly); |
c66ac9db NB |
440 | |
441 | bl += sprintf(b + bl, " "); | |
442 | if (bd) { | |
443 | bl += sprintf(b + bl, "Major: %d Minor: %d %s\n", | |
21bca31c | 444 | MAJOR(bd->bd_dev), MINOR(bd->bd_dev), (!bd->bd_contains) ? |
8359cf43 | 445 | "" : (bd->bd_holder == ibd) ? |
c66ac9db NB |
446 | "CLAIMED: IBLOCK" : "CLAIMED: OS"); |
447 | } else { | |
21bca31c | 448 | bl += sprintf(b + bl, "Major: 0 Minor: 0\n"); |
c66ac9db NB |
449 | } |
450 | ||
451 | return bl; | |
452 | } | |
453 | ||
5787cacd CH |
454 | static void iblock_complete_cmd(struct se_cmd *cmd) |
455 | { | |
456 | struct iblock_req *ibr = cmd->priv; | |
457 | u8 status; | |
458 | ||
459 | if (!atomic_dec_and_test(&ibr->pending)) | |
460 | return; | |
461 | ||
462 | if (atomic_read(&ibr->ib_bio_err_cnt)) | |
463 | status = SAM_STAT_CHECK_CONDITION; | |
464 | else | |
465 | status = SAM_STAT_GOOD; | |
466 | ||
467 | target_complete_cmd(cmd, status); | |
468 | kfree(ibr); | |
469 | } | |
470 | ||
c66ac9db NB |
471 | static void iblock_bio_destructor(struct bio *bio) |
472 | { | |
5787cacd CH |
473 | struct se_cmd *cmd = bio->bi_private; |
474 | struct iblock_dev *ib_dev = cmd->se_dev->dev_ptr; | |
c66ac9db NB |
475 | |
476 | bio_free(bio, ib_dev->ibd_bio_set); | |
477 | } | |
478 | ||
dbbf3e94 | 479 | static struct bio * |
5787cacd | 480 | iblock_get_bio(struct se_cmd *cmd, sector_t lba, u32 sg_num) |
c66ac9db | 481 | { |
5787cacd | 482 | struct iblock_dev *ib_dev = cmd->se_dev->dev_ptr; |
c66ac9db NB |
483 | struct bio *bio; |
484 | ||
5c55125f CH |
485 | /* |
486 | * Only allocate as many vector entries as the bio code allows us to, | |
487 | * we'll loop later on until we have handled the whole request. | |
488 | */ | |
489 | if (sg_num > BIO_MAX_PAGES) | |
490 | sg_num = BIO_MAX_PAGES; | |
491 | ||
c66ac9db | 492 | bio = bio_alloc_bioset(GFP_NOIO, sg_num, ib_dev->ibd_bio_set); |
6708bb27 AG |
493 | if (!bio) { |
494 | pr_err("Unable to allocate memory for bio\n"); | |
c66ac9db NB |
495 | return NULL; |
496 | } | |
497 | ||
c66ac9db | 498 | bio->bi_bdev = ib_dev->ibd_bd; |
5787cacd | 499 | bio->bi_private = cmd; |
c66ac9db NB |
500 | bio->bi_destructor = iblock_bio_destructor; |
501 | bio->bi_end_io = &iblock_bio_done; | |
502 | bio->bi_sector = lba; | |
c66ac9db NB |
503 | return bio; |
504 | } | |
505 | ||
d5b4a21b CH |
506 | static void iblock_submit_bios(struct bio_list *list, int rw) |
507 | { | |
508 | struct blk_plug plug; | |
509 | struct bio *bio; | |
510 | ||
511 | blk_start_plug(&plug); | |
512 | while ((bio = bio_list_pop(list))) | |
513 | submit_bio(rw, bio); | |
514 | blk_finish_plug(&plug); | |
515 | } | |
516 | ||
0c2ad7d1 | 517 | static int iblock_execute_rw(struct se_cmd *cmd) |
c66ac9db | 518 | { |
0c2ad7d1 CH |
519 | struct scatterlist *sgl = cmd->t_data_sg; |
520 | u32 sgl_nents = cmd->t_data_nents; | |
521 | enum dma_data_direction data_direction = cmd->data_direction; | |
5951146d | 522 | struct se_device *dev = cmd->se_dev; |
5787cacd | 523 | struct iblock_req *ibr; |
dbbf3e94 CH |
524 | struct bio *bio; |
525 | struct bio_list list; | |
c66ac9db | 526 | struct scatterlist *sg; |
5787cacd | 527 | u32 sg_num = sgl_nents; |
c66ac9db | 528 | sector_t block_lba; |
d5b4a21b | 529 | unsigned bio_cnt; |
dbbf3e94 | 530 | int rw; |
5787cacd | 531 | int i; |
dbbf3e94 | 532 | |
5787cacd | 533 | if (data_direction == DMA_TO_DEVICE) { |
dbbf3e94 CH |
534 | /* |
535 | * Force data to disk if we pretend to not have a volatile | |
536 | * write cache, or the initiator set the Force Unit Access bit. | |
537 | */ | |
538 | if (dev->se_sub_dev->se_dev_attrib.emulate_write_cache == 0 || | |
539 | (dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0 && | |
2d3a4b51 | 540 | (cmd->se_cmd_flags & SCF_FUA))) |
dbbf3e94 CH |
541 | rw = WRITE_FUA; |
542 | else | |
543 | rw = WRITE; | |
544 | } else { | |
545 | rw = READ; | |
546 | } | |
547 | ||
c66ac9db | 548 | /* |
5787cacd CH |
549 | * Convert the blocksize advertised to the initiator to the 512 byte |
550 | * units unconditionally used by the Linux block layer. | |
c66ac9db | 551 | */ |
e3d6f909 | 552 | if (dev->se_sub_dev->se_dev_attrib.block_size == 4096) |
72a0e5e2 | 553 | block_lba = (cmd->t_task_lba << 3); |
e3d6f909 | 554 | else if (dev->se_sub_dev->se_dev_attrib.block_size == 2048) |
72a0e5e2 | 555 | block_lba = (cmd->t_task_lba << 2); |
e3d6f909 | 556 | else if (dev->se_sub_dev->se_dev_attrib.block_size == 1024) |
72a0e5e2 | 557 | block_lba = (cmd->t_task_lba << 1); |
e3d6f909 | 558 | else if (dev->se_sub_dev->se_dev_attrib.block_size == 512) |
72a0e5e2 | 559 | block_lba = cmd->t_task_lba; |
c66ac9db | 560 | else { |
6708bb27 | 561 | pr_err("Unsupported SCSI -> BLOCK LBA conversion:" |
e3d6f909 | 562 | " %u\n", dev->se_sub_dev->se_dev_attrib.block_size); |
03e98c9e NB |
563 | cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
564 | return -ENOSYS; | |
c66ac9db NB |
565 | } |
566 | ||
5787cacd CH |
567 | ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL); |
568 | if (!ibr) | |
569 | goto fail; | |
570 | cmd->priv = ibr; | |
571 | ||
572 | bio = iblock_get_bio(cmd, block_lba, sgl_nents); | |
573 | if (!bio) | |
574 | goto fail_free_ibr; | |
dbbf3e94 CH |
575 | |
576 | bio_list_init(&list); | |
577 | bio_list_add(&list, bio); | |
5787cacd CH |
578 | |
579 | atomic_set(&ibr->pending, 2); | |
d5b4a21b | 580 | bio_cnt = 1; |
c66ac9db | 581 | |
5787cacd | 582 | for_each_sg(sgl, sg, sgl_nents, i) { |
dbbf3e94 CH |
583 | /* |
584 | * XXX: if the length the device accepts is shorter than the | |
585 | * length of the S/G list entry this will cause and | |
586 | * endless loop. Better hope no driver uses huge pages. | |
587 | */ | |
588 | while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset) | |
589 | != sg->length) { | |
d5b4a21b CH |
590 | if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) { |
591 | iblock_submit_bios(&list, rw); | |
592 | bio_cnt = 0; | |
593 | } | |
594 | ||
5787cacd | 595 | bio = iblock_get_bio(cmd, block_lba, sg_num); |
6708bb27 | 596 | if (!bio) |
5787cacd CH |
597 | goto fail_put_bios; |
598 | ||
599 | atomic_inc(&ibr->pending); | |
dbbf3e94 | 600 | bio_list_add(&list, bio); |
d5b4a21b | 601 | bio_cnt++; |
c66ac9db | 602 | } |
dbbf3e94 | 603 | |
c66ac9db NB |
604 | /* Always in 512 byte units for Linux/Block */ |
605 | block_lba += sg->length >> IBLOCK_LBA_SHIFT; | |
606 | sg_num--; | |
c66ac9db NB |
607 | } |
608 | ||
d5b4a21b | 609 | iblock_submit_bios(&list, rw); |
5787cacd | 610 | iblock_complete_cmd(cmd); |
03e98c9e | 611 | return 0; |
dbbf3e94 | 612 | |
5787cacd | 613 | fail_put_bios: |
dbbf3e94 | 614 | while ((bio = bio_list_pop(&list))) |
c66ac9db | 615 | bio_put(bio); |
5787cacd CH |
616 | fail_free_ibr: |
617 | kfree(ibr); | |
03e98c9e | 618 | cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
5787cacd | 619 | fail: |
03e98c9e | 620 | return -ENOMEM; |
c66ac9db NB |
621 | } |
622 | ||
c66ac9db NB |
623 | static u32 iblock_get_device_rev(struct se_device *dev) |
624 | { | |
625 | return SCSI_SPC_2; /* Returns SPC-3 in Initiator Data */ | |
626 | } | |
627 | ||
628 | static u32 iblock_get_device_type(struct se_device *dev) | |
629 | { | |
630 | return TYPE_DISK; | |
631 | } | |
632 | ||
633 | static sector_t iblock_get_blocks(struct se_device *dev) | |
634 | { | |
635 | struct iblock_dev *ibd = dev->dev_ptr; | |
636 | struct block_device *bd = ibd->ibd_bd; | |
637 | struct request_queue *q = bdev_get_queue(bd); | |
638 | ||
639 | return iblock_emulate_read_cap_with_block_size(dev, bd, q); | |
640 | } | |
641 | ||
642 | static void iblock_bio_done(struct bio *bio, int err) | |
643 | { | |
5787cacd CH |
644 | struct se_cmd *cmd = bio->bi_private; |
645 | struct iblock_req *ibr = cmd->priv; | |
dbbf3e94 | 646 | |
c66ac9db NB |
647 | /* |
648 | * Set -EIO if !BIO_UPTODATE and the passed is still err=0 | |
649 | */ | |
6708bb27 | 650 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && !err) |
c66ac9db NB |
651 | err = -EIO; |
652 | ||
653 | if (err != 0) { | |
6708bb27 | 654 | pr_err("test_bit(BIO_UPTODATE) failed for bio: %p," |
c66ac9db NB |
655 | " err: %d\n", bio, err); |
656 | /* | |
657 | * Bump the ib_bio_err_cnt and release bio. | |
658 | */ | |
659 | atomic_inc(&ibr->ib_bio_err_cnt); | |
660 | smp_mb__after_atomic_inc(); | |
c66ac9db | 661 | } |
dbbf3e94 | 662 | |
c66ac9db | 663 | bio_put(bio); |
dbbf3e94 | 664 | |
5787cacd | 665 | iblock_complete_cmd(cmd); |
c66ac9db NB |
666 | } |
667 | ||
0c2ad7d1 CH |
668 | static struct spc_ops iblock_spc_ops = { |
669 | .execute_rw = iblock_execute_rw, | |
670 | }; | |
671 | ||
672 | static int iblock_parse_cdb(struct se_cmd *cmd) | |
673 | { | |
674 | return sbc_parse_cdb(cmd, &iblock_spc_ops); | |
675 | } | |
676 | ||
c66ac9db NB |
677 | static struct se_subsystem_api iblock_template = { |
678 | .name = "iblock", | |
679 | .owner = THIS_MODULE, | |
680 | .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV, | |
f55918fa CH |
681 | .write_cache_emulated = 1, |
682 | .fua_write_emulated = 1, | |
c66ac9db NB |
683 | .attach_hba = iblock_attach_hba, |
684 | .detach_hba = iblock_detach_hba, | |
685 | .allocate_virtdevice = iblock_allocate_virtdevice, | |
686 | .create_virtdevice = iblock_create_virtdevice, | |
687 | .free_device = iblock_free_device, | |
0c2ad7d1 | 688 | .parse_cdb = iblock_parse_cdb, |
c66ac9db NB |
689 | .do_discard = iblock_do_discard, |
690 | .do_sync_cache = iblock_emulate_sync_cache, | |
c66ac9db NB |
691 | .check_configfs_dev_params = iblock_check_configfs_dev_params, |
692 | .set_configfs_dev_params = iblock_set_configfs_dev_params, | |
693 | .show_configfs_dev_params = iblock_show_configfs_dev_params, | |
c66ac9db NB |
694 | .get_device_rev = iblock_get_device_rev, |
695 | .get_device_type = iblock_get_device_type, | |
696 | .get_blocks = iblock_get_blocks, | |
697 | }; | |
698 | ||
699 | static int __init iblock_module_init(void) | |
700 | { | |
701 | return transport_subsystem_register(&iblock_template); | |
702 | } | |
703 | ||
704 | static void iblock_module_exit(void) | |
705 | { | |
706 | transport_subsystem_release(&iblock_template); | |
707 | } | |
708 | ||
709 | MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin"); | |
710 | MODULE_AUTHOR("nab@Linux-iSCSI.org"); | |
711 | MODULE_LICENSE("GPL"); | |
712 | ||
713 | module_init(iblock_module_init); | |
714 | module_exit(iblock_module_exit); |