target: remove dead SCF_ flags
[deliverable/linux.git] / drivers / target / target_core_iblock.c
CommitLineData
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
52static struct se_subsystem_api iblock_template;
53
54static void iblock_bio_done(struct bio *, int);
55
56/* iblock_attach_hba(): (Part of se_subsystem_api_t template)
57 *
58 *
59 */
60static 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
68static void iblock_detach_hba(struct se_hba *hba)
69{
c66ac9db
NB
70}
71
72static 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
87static 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
176failed:
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
185static 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
196static 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
275static 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 299static 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
320static 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 329enum {
44bfd018 330 Opt_udev_path, Opt_readonly, Opt_force, Opt_err
c66ac9db
NB
331};
332
333static 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
340static 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
404out:
405 kfree(orig);
406 return (!ret) ? count : ret;
407}
408
409static 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
423static 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
454static 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
471static 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 479static struct bio *
5787cacd 480iblock_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
506static 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
5787cacd
CH
517static int iblock_execute_cmd(struct se_cmd *cmd, struct scatterlist *sgl,
518 u32 sgl_nents, enum dma_data_direction data_direction)
c66ac9db 519{
5951146d 520 struct se_device *dev = cmd->se_dev;
5787cacd 521 struct iblock_req *ibr;
dbbf3e94
CH
522 struct bio *bio;
523 struct bio_list list;
c66ac9db 524 struct scatterlist *sg;
5787cacd 525 u32 sg_num = sgl_nents;
c66ac9db 526 sector_t block_lba;
d5b4a21b 527 unsigned bio_cnt;
dbbf3e94 528 int rw;
5787cacd 529 int i;
dbbf3e94 530
5787cacd 531 if (data_direction == DMA_TO_DEVICE) {
dbbf3e94
CH
532 /*
533 * Force data to disk if we pretend to not have a volatile
534 * write cache, or the initiator set the Force Unit Access bit.
535 */
536 if (dev->se_sub_dev->se_dev_attrib.emulate_write_cache == 0 ||
537 (dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0 &&
2d3a4b51 538 (cmd->se_cmd_flags & SCF_FUA)))
dbbf3e94
CH
539 rw = WRITE_FUA;
540 else
541 rw = WRITE;
542 } else {
543 rw = READ;
544 }
545
c66ac9db 546 /*
5787cacd
CH
547 * Convert the blocksize advertised to the initiator to the 512 byte
548 * units unconditionally used by the Linux block layer.
c66ac9db 549 */
e3d6f909 550 if (dev->se_sub_dev->se_dev_attrib.block_size == 4096)
72a0e5e2 551 block_lba = (cmd->t_task_lba << 3);
e3d6f909 552 else if (dev->se_sub_dev->se_dev_attrib.block_size == 2048)
72a0e5e2 553 block_lba = (cmd->t_task_lba << 2);
e3d6f909 554 else if (dev->se_sub_dev->se_dev_attrib.block_size == 1024)
72a0e5e2 555 block_lba = (cmd->t_task_lba << 1);
e3d6f909 556 else if (dev->se_sub_dev->se_dev_attrib.block_size == 512)
72a0e5e2 557 block_lba = cmd->t_task_lba;
c66ac9db 558 else {
6708bb27 559 pr_err("Unsupported SCSI -> BLOCK LBA conversion:"
e3d6f909 560 " %u\n", dev->se_sub_dev->se_dev_attrib.block_size);
03e98c9e
NB
561 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
562 return -ENOSYS;
c66ac9db
NB
563 }
564
5787cacd
CH
565 ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
566 if (!ibr)
567 goto fail;
568 cmd->priv = ibr;
569
570 bio = iblock_get_bio(cmd, block_lba, sgl_nents);
571 if (!bio)
572 goto fail_free_ibr;
dbbf3e94
CH
573
574 bio_list_init(&list);
575 bio_list_add(&list, bio);
5787cacd
CH
576
577 atomic_set(&ibr->pending, 2);
d5b4a21b 578 bio_cnt = 1;
c66ac9db 579
5787cacd 580 for_each_sg(sgl, sg, sgl_nents, i) {
dbbf3e94
CH
581 /*
582 * XXX: if the length the device accepts is shorter than the
583 * length of the S/G list entry this will cause and
584 * endless loop. Better hope no driver uses huge pages.
585 */
586 while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
587 != sg->length) {
d5b4a21b
CH
588 if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) {
589 iblock_submit_bios(&list, rw);
590 bio_cnt = 0;
591 }
592
5787cacd 593 bio = iblock_get_bio(cmd, block_lba, sg_num);
6708bb27 594 if (!bio)
5787cacd
CH
595 goto fail_put_bios;
596
597 atomic_inc(&ibr->pending);
dbbf3e94 598 bio_list_add(&list, bio);
d5b4a21b 599 bio_cnt++;
c66ac9db 600 }
dbbf3e94 601
c66ac9db
NB
602 /* Always in 512 byte units for Linux/Block */
603 block_lba += sg->length >> IBLOCK_LBA_SHIFT;
604 sg_num--;
c66ac9db
NB
605 }
606
d5b4a21b 607 iblock_submit_bios(&list, rw);
5787cacd 608 iblock_complete_cmd(cmd);
03e98c9e 609 return 0;
dbbf3e94 610
5787cacd 611fail_put_bios:
dbbf3e94 612 while ((bio = bio_list_pop(&list)))
c66ac9db 613 bio_put(bio);
5787cacd
CH
614fail_free_ibr:
615 kfree(ibr);
03e98c9e 616 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
5787cacd 617fail:
03e98c9e 618 return -ENOMEM;
c66ac9db
NB
619}
620
c66ac9db
NB
621static u32 iblock_get_device_rev(struct se_device *dev)
622{
623 return SCSI_SPC_2; /* Returns SPC-3 in Initiator Data */
624}
625
626static u32 iblock_get_device_type(struct se_device *dev)
627{
628 return TYPE_DISK;
629}
630
631static sector_t iblock_get_blocks(struct se_device *dev)
632{
633 struct iblock_dev *ibd = dev->dev_ptr;
634 struct block_device *bd = ibd->ibd_bd;
635 struct request_queue *q = bdev_get_queue(bd);
636
637 return iblock_emulate_read_cap_with_block_size(dev, bd, q);
638}
639
640static void iblock_bio_done(struct bio *bio, int err)
641{
5787cacd
CH
642 struct se_cmd *cmd = bio->bi_private;
643 struct iblock_req *ibr = cmd->priv;
dbbf3e94 644
c66ac9db
NB
645 /*
646 * Set -EIO if !BIO_UPTODATE and the passed is still err=0
647 */
6708bb27 648 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && !err)
c66ac9db
NB
649 err = -EIO;
650
651 if (err != 0) {
6708bb27 652 pr_err("test_bit(BIO_UPTODATE) failed for bio: %p,"
c66ac9db
NB
653 " err: %d\n", bio, err);
654 /*
655 * Bump the ib_bio_err_cnt and release bio.
656 */
657 atomic_inc(&ibr->ib_bio_err_cnt);
658 smp_mb__after_atomic_inc();
c66ac9db 659 }
dbbf3e94 660
c66ac9db 661 bio_put(bio);
dbbf3e94 662
5787cacd 663 iblock_complete_cmd(cmd);
c66ac9db
NB
664}
665
666static struct se_subsystem_api iblock_template = {
667 .name = "iblock",
668 .owner = THIS_MODULE,
669 .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV,
f55918fa
CH
670 .write_cache_emulated = 1,
671 .fua_write_emulated = 1,
c66ac9db
NB
672 .attach_hba = iblock_attach_hba,
673 .detach_hba = iblock_detach_hba,
674 .allocate_virtdevice = iblock_allocate_virtdevice,
675 .create_virtdevice = iblock_create_virtdevice,
676 .free_device = iblock_free_device,
d6e0175c 677 .parse_cdb = sbc_parse_cdb,
5787cacd 678 .execute_cmd = iblock_execute_cmd,
c66ac9db
NB
679 .do_discard = iblock_do_discard,
680 .do_sync_cache = iblock_emulate_sync_cache,
c66ac9db
NB
681 .check_configfs_dev_params = iblock_check_configfs_dev_params,
682 .set_configfs_dev_params = iblock_set_configfs_dev_params,
683 .show_configfs_dev_params = iblock_show_configfs_dev_params,
c66ac9db
NB
684 .get_device_rev = iblock_get_device_rev,
685 .get_device_type = iblock_get_device_type,
686 .get_blocks = iblock_get_blocks,
687};
688
689static int __init iblock_module_init(void)
690{
691 return transport_subsystem_register(&iblock_template);
692}
693
694static void iblock_module_exit(void)
695{
696 transport_subsystem_release(&iblock_template);
697}
698
699MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin");
700MODULE_AUTHOR("nab@Linux-iSCSI.org");
701MODULE_LICENSE("GPL");
702
703module_init(iblock_module_init);
704module_exit(iblock_module_exit);
This page took 0.232505 seconds and 5 git commands to generate.