target: replace se_cmd->execute_rw with a protocol_data field
[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 *
4c76251e 7 * (c) Copyright 2003-2013 Datera, Inc.
c66ac9db
NB
8 *
9 * Nicholas A. Bellinger <nab@kernel.org>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 *
25 ******************************************************************************/
26
c66ac9db
NB
27#include <linux/string.h>
28#include <linux/parser.h>
29#include <linux/timer.h>
30#include <linux/fs.h>
31#include <linux/blkdev.h>
32#include <linux/slab.h>
33#include <linux/spinlock.h>
c66ac9db
NB
34#include <linux/bio.h>
35#include <linux/genhd.h>
36#include <linux/file.h>
827509e3 37#include <linux/module.h>
c66ac9db
NB
38#include <scsi/scsi.h>
39#include <scsi/scsi_host.h>
14150a6b 40#include <asm/unaligned.h>
c66ac9db
NB
41
42#include <target/target_core_base.h>
c4795fb2 43#include <target/target_core_backend.h>
c66ac9db
NB
44
45#include "target_core_iblock.h"
46
d5b4a21b
CH
47#define IBLOCK_MAX_BIO_PER_TASK 32 /* max # of bios to submit at a time */
48#define IBLOCK_BIO_POOL_SIZE 128
49
0fd97ccf
CH
50static inline struct iblock_dev *IBLOCK_DEV(struct se_device *dev)
51{
52 return container_of(dev, struct iblock_dev, dev);
53}
54
55
c66ac9db
NB
56static int iblock_attach_hba(struct se_hba *hba, u32 host_id)
57{
6708bb27 58 pr_debug("CORE_HBA[%d] - TCM iBlock HBA Driver %s on"
c66ac9db
NB
59 " Generic Target Core Stack %s\n", hba->hba_id,
60 IBLOCK_VERSION, TARGET_CORE_MOD_VERSION);
c66ac9db
NB
61 return 0;
62}
63
64static void iblock_detach_hba(struct se_hba *hba)
65{
c66ac9db
NB
66}
67
0fd97ccf 68static struct se_device *iblock_alloc_device(struct se_hba *hba, const char *name)
c66ac9db
NB
69{
70 struct iblock_dev *ib_dev = NULL;
c66ac9db
NB
71
72 ib_dev = kzalloc(sizeof(struct iblock_dev), GFP_KERNEL);
6708bb27
AG
73 if (!ib_dev) {
74 pr_err("Unable to allocate struct iblock_dev\n");
c66ac9db
NB
75 return NULL;
76 }
c66ac9db 77
6708bb27 78 pr_debug( "IBLOCK: Allocated ib_dev for %s\n", name);
c66ac9db 79
0fd97ccf 80 return &ib_dev->dev;
c66ac9db
NB
81}
82
0fd97ccf 83static int iblock_configure_device(struct se_device *dev)
c66ac9db 84{
0fd97ccf 85 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
c66ac9db 86 struct request_queue *q;
0fd97ccf 87 struct block_device *bd = NULL;
ecebbf6c 88 struct blk_integrity *bi;
44bfd018 89 fmode_t mode;
0fd97ccf 90 int ret = -ENOMEM;
c66ac9db 91
0fd97ccf
CH
92 if (!(ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)) {
93 pr_err("Missing udev_path= parameters for IBLOCK\n");
94 return -EINVAL;
c66ac9db 95 }
d5b4a21b
CH
96
97 ib_dev->ibd_bio_set = bioset_create(IBLOCK_BIO_POOL_SIZE, 0);
6708bb27 98 if (!ib_dev->ibd_bio_set) {
0fd97ccf
CH
99 pr_err("IBLOCK: Unable to create bioset\n");
100 goto out;
c66ac9db 101 }
0fd97ccf 102
6708bb27 103 pr_debug( "IBLOCK: Claiming struct block_device: %s\n",
c66ac9db
NB
104 ib_dev->ibd_udev_path);
105
44bfd018
AG
106 mode = FMODE_READ|FMODE_EXCL;
107 if (!ib_dev->ibd_readonly)
108 mode |= FMODE_WRITE;
109
110 bd = blkdev_get_by_path(ib_dev->ibd_udev_path, mode, ib_dev);
613640e4
NB
111 if (IS_ERR(bd)) {
112 ret = PTR_ERR(bd);
0fd97ccf 113 goto out_free_bioset;
613640e4 114 }
c66ac9db
NB
115 ib_dev->ibd_bd = bd;
116
0fd97ccf
CH
117 q = bdev_get_queue(bd);
118
119 dev->dev_attrib.hw_block_size = bdev_logical_block_size(bd);
046ba642 120 dev->dev_attrib.hw_max_sectors = queue_max_hw_sectors(q);
0fd97ccf 121 dev->dev_attrib.hw_queue_depth = q->nr_requests;
c66ac9db 122
c66ac9db
NB
123 /*
124 * Check if the underlying struct block_device request_queue supports
125 * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM
126 * in ATA and we need to set TPE=1
127 */
613640e4 128 if (blk_queue_discard(q)) {
0fd97ccf 129 dev->dev_attrib.max_unmap_lba_count =
c66ac9db 130 q->limits.max_discard_sectors;
0fd97ccf 131
c66ac9db
NB
132 /*
133 * Currently hardcoded to 1 in Linux/SCSI code..
134 */
0fd97ccf
CH
135 dev->dev_attrib.max_unmap_block_desc_count = 1;
136 dev->dev_attrib.unmap_granularity =
7347b5ff 137 q->limits.discard_granularity >> 9;
0fd97ccf 138 dev->dev_attrib.unmap_granularity_alignment =
c66ac9db
NB
139 q->limits.discard_alignment;
140
6708bb27 141 pr_debug("IBLOCK: BLOCK Discard support available,"
c66ac9db
NB
142 " disabled by default\n");
143 }
f6970ad3
NB
144 /*
145 * Enable write same emulation for IBLOCK and use 0xFFFF as
146 * the smaller WRITE_SAME(10) only has a two-byte block count.
147 */
148 dev->dev_attrib.max_write_same_len = 0xFFFF;
c66ac9db 149
e22a7f07 150 if (blk_queue_nonrot(q))
0fd97ccf 151 dev->dev_attrib.is_nonrot = 1;
d0c8b259 152
ecebbf6c
NB
153 bi = bdev_get_integrity(bd);
154 if (bi) {
155 struct bio_set *bs = ib_dev->ibd_bio_set;
156
157 if (!strcmp(bi->name, "T10-DIF-TYPE3-IP") ||
158 !strcmp(bi->name, "T10-DIF-TYPE1-IP")) {
159 pr_err("IBLOCK export of blk_integrity: %s not"
160 " supported\n", bi->name);
161 ret = -ENOSYS;
162 goto out_blkdev_put;
163 }
164
165 if (!strcmp(bi->name, "T10-DIF-TYPE3-CRC")) {
166 dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE3_PROT;
167 } else if (!strcmp(bi->name, "T10-DIF-TYPE1-CRC")) {
168 dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE1_PROT;
169 }
170
171 if (dev->dev_attrib.pi_prot_type) {
172 if (bioset_integrity_create(bs, IBLOCK_BIO_POOL_SIZE) < 0) {
173 pr_err("Unable to allocate bioset for PI\n");
174 ret = -ENOMEM;
175 goto out_blkdev_put;
176 }
177 pr_debug("IBLOCK setup BIP bs->bio_integrity_pool: %p\n",
178 bs->bio_integrity_pool);
179 }
180 dev->dev_attrib.hw_pi_prot_type = dev->dev_attrib.pi_prot_type;
181 }
182
0fd97ccf 183 return 0;
c66ac9db 184
ecebbf6c
NB
185out_blkdev_put:
186 blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
0fd97ccf
CH
187out_free_bioset:
188 bioset_free(ib_dev->ibd_bio_set);
189 ib_dev->ibd_bio_set = NULL;
190out:
191 return ret;
c66ac9db
NB
192}
193
4cc987ea
NB
194static void iblock_dev_call_rcu(struct rcu_head *p)
195{
196 struct se_device *dev = container_of(p, struct se_device, rcu_head);
197 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
198
199 kfree(ib_dev);
200}
201
0fd97ccf 202static void iblock_free_device(struct se_device *dev)
c66ac9db 203{
0fd97ccf 204 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
c66ac9db 205
bc665524
NB
206 if (ib_dev->ibd_bd != NULL)
207 blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
d84287bc 208 if (ib_dev->ibd_bio_set != NULL)
bc665524 209 bioset_free(ib_dev->ibd_bio_set);
d84287bc 210
4cc987ea 211 call_rcu(&dev->rcu_head, iblock_dev_call_rcu);
c66ac9db
NB
212}
213
c66ac9db
NB
214static unsigned long long iblock_emulate_read_cap_with_block_size(
215 struct se_device *dev,
216 struct block_device *bd,
217 struct request_queue *q)
218{
219 unsigned long long blocks_long = (div_u64(i_size_read(bd->bd_inode),
220 bdev_logical_block_size(bd)) - 1);
221 u32 block_size = bdev_logical_block_size(bd);
222
0fd97ccf 223 if (block_size == dev->dev_attrib.block_size)
c66ac9db
NB
224 return blocks_long;
225
226 switch (block_size) {
227 case 4096:
0fd97ccf 228 switch (dev->dev_attrib.block_size) {
c66ac9db
NB
229 case 2048:
230 blocks_long <<= 1;
231 break;
232 case 1024:
233 blocks_long <<= 2;
234 break;
235 case 512:
236 blocks_long <<= 3;
237 default:
238 break;
239 }
240 break;
241 case 2048:
0fd97ccf 242 switch (dev->dev_attrib.block_size) {
c66ac9db
NB
243 case 4096:
244 blocks_long >>= 1;
245 break;
246 case 1024:
247 blocks_long <<= 1;
248 break;
249 case 512:
250 blocks_long <<= 2;
251 break;
252 default:
253 break;
254 }
255 break;
256 case 1024:
0fd97ccf 257 switch (dev->dev_attrib.block_size) {
c66ac9db
NB
258 case 4096:
259 blocks_long >>= 2;
260 break;
261 case 2048:
262 blocks_long >>= 1;
263 break;
264 case 512:
265 blocks_long <<= 1;
266 break;
267 default:
268 break;
269 }
270 break;
271 case 512:
0fd97ccf 272 switch (dev->dev_attrib.block_size) {
c66ac9db
NB
273 case 4096:
274 blocks_long >>= 3;
275 break;
276 case 2048:
277 blocks_long >>= 2;
278 break;
279 case 1024:
280 blocks_long >>= 1;
281 break;
282 default:
283 break;
284 }
285 break;
286 default:
287 break;
288 }
289
290 return blocks_long;
291}
292
3a41d85f
NB
293static void iblock_complete_cmd(struct se_cmd *cmd)
294{
295 struct iblock_req *ibr = cmd->priv;
296 u8 status;
297
298 if (!atomic_dec_and_test(&ibr->pending))
299 return;
300
301 if (atomic_read(&ibr->ib_bio_err_cnt))
302 status = SAM_STAT_CHECK_CONDITION;
303 else
304 status = SAM_STAT_GOOD;
305
306 target_complete_cmd(cmd, status);
307 kfree(ibr);
308}
309
310static void iblock_bio_done(struct bio *bio, int err)
311{
312 struct se_cmd *cmd = bio->bi_private;
313 struct iblock_req *ibr = cmd->priv;
314
315 /*
316 * Set -EIO if !BIO_UPTODATE and the passed is still err=0
317 */
318 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && !err)
319 err = -EIO;
320
321 if (err != 0) {
322 pr_err("test_bit(BIO_UPTODATE) failed for bio: %p,"
323 " err: %d\n", bio, err);
324 /*
325 * Bump the ib_bio_err_cnt and release bio.
326 */
327 atomic_inc(&ibr->ib_bio_err_cnt);
4e857c58 328 smp_mb__after_atomic();
3a41d85f
NB
329 }
330
331 bio_put(bio);
332
333 iblock_complete_cmd(cmd);
334}
335
336static struct bio *
337iblock_get_bio(struct se_cmd *cmd, sector_t lba, u32 sg_num)
338{
339 struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
340 struct bio *bio;
341
342 /*
343 * Only allocate as many vector entries as the bio code allows us to,
344 * we'll loop later on until we have handled the whole request.
345 */
346 if (sg_num > BIO_MAX_PAGES)
347 sg_num = BIO_MAX_PAGES;
348
349 bio = bio_alloc_bioset(GFP_NOIO, sg_num, ib_dev->ibd_bio_set);
350 if (!bio) {
351 pr_err("Unable to allocate memory for bio\n");
352 return NULL;
353 }
354
355 bio->bi_bdev = ib_dev->ibd_bd;
356 bio->bi_private = cmd;
357 bio->bi_end_io = &iblock_bio_done;
4f024f37 358 bio->bi_iter.bi_sector = lba;
3a41d85f
NB
359
360 return bio;
361}
362
363static void iblock_submit_bios(struct bio_list *list, int rw)
364{
365 struct blk_plug plug;
366 struct bio *bio;
367
368 blk_start_plug(&plug);
369 while ((bio = bio_list_pop(list)))
370 submit_bio(rw, bio);
371 blk_finish_plug(&plug);
372}
373
df5fa691
CH
374static void iblock_end_io_flush(struct bio *bio, int err)
375{
376 struct se_cmd *cmd = bio->bi_private;
377
378 if (err)
379 pr_err("IBLOCK: cache flush failed: %d\n", err);
380
5787cacd 381 if (cmd) {
de103c93 382 if (err)
5787cacd 383 target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
de103c93 384 else
5787cacd 385 target_complete_cmd(cmd, SAM_STAT_GOOD);
5787cacd
CH
386 }
387
df5fa691
CH
388 bio_put(bio);
389}
390
c66ac9db 391/*
df5fa691
CH
392 * Implement SYCHRONIZE CACHE. Note that we can't handle lba ranges and must
393 * always flush the whole cache.
c66ac9db 394 */
de103c93
CH
395static sense_reason_t
396iblock_execute_sync_cache(struct se_cmd *cmd)
c66ac9db 397{
0fd97ccf 398 struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
a1d8b49a 399 int immed = (cmd->t_task_cdb[1] & 0x2);
df5fa691 400 struct bio *bio;
c66ac9db
NB
401
402 /*
403 * If the Immediate bit is set, queue up the GOOD response
df5fa691 404 * for this SYNCHRONIZE_CACHE op.
c66ac9db
NB
405 */
406 if (immed)
5787cacd 407 target_complete_cmd(cmd, SAM_STAT_GOOD);
c66ac9db 408
df5fa691
CH
409 bio = bio_alloc(GFP_KERNEL, 0);
410 bio->bi_end_io = iblock_end_io_flush;
411 bio->bi_bdev = ib_dev->ibd_bd;
c66ac9db 412 if (!immed)
df5fa691
CH
413 bio->bi_private = cmd;
414 submit_bio(WRITE_FLUSH, bio);
ad67f0d9 415 return 0;
c66ac9db
NB
416}
417
dbc21c5a 418static sense_reason_t
86d71829 419iblock_do_unmap(struct se_cmd *cmd, void *priv,
dbc21c5a
AH
420 sector_t lba, sector_t nolb)
421{
86d71829 422 struct block_device *bdev = priv;
dbc21c5a
AH
423 int ret;
424
425 ret = blkdev_issue_discard(bdev, lba, nolb, GFP_KERNEL, 0);
426 if (ret < 0) {
427 pr_err("blkdev_issue_discard() failed: %d\n", ret);
428 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
429 }
430
431 return 0;
432}
433
de103c93
CH
434static sense_reason_t
435iblock_execute_unmap(struct se_cmd *cmd)
c66ac9db 436{
86d71829 437 struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd;
c66ac9db 438
86d71829 439 return sbc_execute_unmap(cmd, iblock_do_unmap, bdev);
c66ac9db
NB
440}
441
de103c93 442static sense_reason_t
f6970ad3 443iblock_execute_write_same_unmap(struct se_cmd *cmd)
6f974e8c 444{
dbc21c5a
AH
445 struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd;
446 sector_t lba = cmd->t_task_lba;
447 sector_t nolb = sbc_get_write_same_sectors(cmd);
3abff1e5 448 sense_reason_t ret;
dbc21c5a
AH
449
450 ret = iblock_do_unmap(cmd, bdev, lba, nolb);
451 if (ret)
452 return ret;
6f974e8c
CH
453
454 target_complete_cmd(cmd, GOOD);
455 return 0;
456}
457
f6970ad3
NB
458static sense_reason_t
459iblock_execute_write_same(struct se_cmd *cmd)
460{
461 struct iblock_req *ibr;
462 struct scatterlist *sg;
463 struct bio *bio;
464 struct bio_list list;
465 sector_t block_lba = cmd->t_task_lba;
972b29c8 466 sector_t sectors = sbc_get_write_same_sectors(cmd);
f6970ad3 467
afd73f1b
NB
468 if (cmd->prot_op) {
469 pr_err("WRITE_SAME: Protection information with IBLOCK"
470 " backends not supported\n");
471 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
472 }
f6970ad3
NB
473 sg = &cmd->t_data_sg[0];
474
475 if (cmd->t_data_nents > 1 ||
476 sg->length != cmd->se_dev->dev_attrib.block_size) {
477 pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u"
478 " block_size: %u\n", cmd->t_data_nents, sg->length,
479 cmd->se_dev->dev_attrib.block_size);
480 return TCM_INVALID_CDB_FIELD;
481 }
482
483 ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
484 if (!ibr)
485 goto fail;
486 cmd->priv = ibr;
487
488 bio = iblock_get_bio(cmd, block_lba, 1);
489 if (!bio)
490 goto fail_free_ibr;
491
492 bio_list_init(&list);
493 bio_list_add(&list, bio);
494
495 atomic_set(&ibr->pending, 1);
496
497 while (sectors) {
498 while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
499 != sg->length) {
500
501 bio = iblock_get_bio(cmd, block_lba, 1);
502 if (!bio)
503 goto fail_put_bios;
504
505 atomic_inc(&ibr->pending);
506 bio_list_add(&list, bio);
507 }
508
509 /* Always in 512 byte units for Linux/Block */
510 block_lba += sg->length >> IBLOCK_LBA_SHIFT;
511 sectors -= 1;
512 }
513
514 iblock_submit_bios(&list, WRITE);
515 return 0;
516
517fail_put_bios:
518 while ((bio = bio_list_pop(&list)))
519 bio_put(bio);
520fail_free_ibr:
521 kfree(ibr);
522fail:
523 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
524}
525
c66ac9db 526enum {
44bfd018 527 Opt_udev_path, Opt_readonly, Opt_force, Opt_err
c66ac9db
NB
528};
529
530static match_table_t tokens = {
531 {Opt_udev_path, "udev_path=%s"},
44bfd018 532 {Opt_readonly, "readonly=%d"},
c66ac9db
NB
533 {Opt_force, "force=%d"},
534 {Opt_err, NULL}
535};
536
0fd97ccf
CH
537static ssize_t iblock_set_configfs_dev_params(struct se_device *dev,
538 const char *page, ssize_t count)
c66ac9db 539{
0fd97ccf 540 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
6d180253 541 char *orig, *ptr, *arg_p, *opts;
c66ac9db 542 substring_t args[MAX_OPT_ARGS];
21bca31c 543 int ret = 0, token;
44bfd018 544 unsigned long tmp_readonly;
c66ac9db
NB
545
546 opts = kstrdup(page, GFP_KERNEL);
547 if (!opts)
548 return -ENOMEM;
549
550 orig = opts;
551
90c161b6 552 while ((ptr = strsep(&opts, ",\n")) != NULL) {
c66ac9db
NB
553 if (!*ptr)
554 continue;
555
556 token = match_token(ptr, tokens, args);
557 switch (token) {
558 case Opt_udev_path:
559 if (ib_dev->ibd_bd) {
6708bb27 560 pr_err("Unable to set udev_path= while"
c66ac9db
NB
561 " ib_dev->ibd_bd exists\n");
562 ret = -EEXIST;
563 goto out;
564 }
852b6ed1
NB
565 if (match_strlcpy(ib_dev->ibd_udev_path, &args[0],
566 SE_UDEV_PATH_LEN) == 0) {
567 ret = -EINVAL;
6d180253
JJ
568 break;
569 }
6708bb27 570 pr_debug("IBLOCK: Referencing UDEV path: %s\n",
c66ac9db
NB
571 ib_dev->ibd_udev_path);
572 ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH;
573 break;
44bfd018
AG
574 case Opt_readonly:
575 arg_p = match_strdup(&args[0]);
576 if (!arg_p) {
577 ret = -ENOMEM;
578 break;
579 }
57103d7f 580 ret = kstrtoul(arg_p, 0, &tmp_readonly);
44bfd018
AG
581 kfree(arg_p);
582 if (ret < 0) {
57103d7f 583 pr_err("kstrtoul() failed for"
44bfd018
AG
584 " readonly=\n");
585 goto out;
586 }
587 ib_dev->ibd_readonly = tmp_readonly;
588 pr_debug("IBLOCK: readonly: %d\n", ib_dev->ibd_readonly);
589 break;
c66ac9db 590 case Opt_force:
c66ac9db
NB
591 break;
592 default:
593 break;
594 }
595 }
596
597out:
598 kfree(orig);
599 return (!ret) ? count : ret;
600}
601
0fd97ccf 602static ssize_t iblock_show_configfs_dev_params(struct se_device *dev, char *b)
c66ac9db 603{
0fd97ccf
CH
604 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
605 struct block_device *bd = ib_dev->ibd_bd;
c66ac9db
NB
606 char buf[BDEVNAME_SIZE];
607 ssize_t bl = 0;
608
609 if (bd)
610 bl += sprintf(b + bl, "iBlock device: %s",
611 bdevname(bd, buf));
0fd97ccf 612 if (ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)
44bfd018 613 bl += sprintf(b + bl, " UDEV PATH: %s",
0fd97ccf
CH
614 ib_dev->ibd_udev_path);
615 bl += sprintf(b + bl, " readonly: %d\n", ib_dev->ibd_readonly);
c66ac9db
NB
616
617 bl += sprintf(b + bl, " ");
618 if (bd) {
619 bl += sprintf(b + bl, "Major: %d Minor: %d %s\n",
21bca31c 620 MAJOR(bd->bd_dev), MINOR(bd->bd_dev), (!bd->bd_contains) ?
0fd97ccf 621 "" : (bd->bd_holder == ib_dev) ?
c66ac9db
NB
622 "CLAIMED: IBLOCK" : "CLAIMED: OS");
623 } else {
21bca31c 624 bl += sprintf(b + bl, "Major: 0 Minor: 0\n");
c66ac9db
NB
625 }
626
627 return bl;
628}
629
ecebbf6c
NB
630static int
631iblock_alloc_bip(struct se_cmd *cmd, struct bio *bio)
632{
633 struct se_device *dev = cmd->se_dev;
634 struct blk_integrity *bi;
635 struct bio_integrity_payload *bip;
636 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
637 struct scatterlist *sg;
638 int i, rc;
639
640 bi = bdev_get_integrity(ib_dev->ibd_bd);
641 if (!bi) {
642 pr_err("Unable to locate bio_integrity\n");
643 return -ENODEV;
644 }
645
646 bip = bio_integrity_alloc(bio, GFP_NOIO, cmd->t_prot_nents);
647 if (!bip) {
648 pr_err("Unable to allocate bio_integrity_payload\n");
649 return -ENOMEM;
650 }
651
4e13c5d0 652 bip->bip_iter.bi_size = (cmd->data_length / dev->dev_attrib.block_size) *
ecebbf6c 653 dev->prot_length;
4e13c5d0 654 bip->bip_iter.bi_sector = bio->bi_iter.bi_sector;
ecebbf6c 655
4e13c5d0
LT
656 pr_debug("IBLOCK BIP Size: %u Sector: %llu\n", bip->bip_iter.bi_size,
657 (unsigned long long)bip->bip_iter.bi_sector);
ecebbf6c
NB
658
659 for_each_sg(cmd->t_prot_sg, sg, cmd->t_prot_nents, i) {
660
661 rc = bio_integrity_add_page(bio, sg_page(sg), sg->length,
662 sg->offset);
663 if (rc != sg->length) {
664 pr_err("bio_integrity_add_page() failed; %d\n", rc);
665 return -ENOMEM;
666 }
667
668 pr_debug("Added bio integrity page: %p length: %d offset; %d\n",
669 sg_page(sg), sg->length, sg->offset);
670 }
671
672 return 0;
673}
674
de103c93 675static sense_reason_t
a82a9538
NB
676iblock_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
677 enum dma_data_direction data_direction)
c66ac9db 678{
5951146d 679 struct se_device *dev = cmd->se_dev;
5787cacd 680 struct iblock_req *ibr;
ecebbf6c 681 struct bio *bio, *bio_start;
dbbf3e94 682 struct bio_list list;
c66ac9db 683 struct scatterlist *sg;
5787cacd 684 u32 sg_num = sgl_nents;
c66ac9db 685 sector_t block_lba;
d5b4a21b 686 unsigned bio_cnt;
d0c8b259 687 int rw = 0;
5787cacd 688 int i;
dbbf3e94 689
5787cacd 690 if (data_direction == DMA_TO_DEVICE) {
d0c8b259
NB
691 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
692 struct request_queue *q = bdev_get_queue(ib_dev->ibd_bd);
dbbf3e94 693 /*
d0c8b259
NB
694 * Force writethrough using WRITE_FUA if a volatile write cache
695 * is not enabled, or if initiator set the Force Unit Access bit.
dbbf3e94 696 */
d0c8b259
NB
697 if (q->flush_flags & REQ_FUA) {
698 if (cmd->se_cmd_flags & SCF_FUA)
699 rw = WRITE_FUA;
700 else if (!(q->flush_flags & REQ_FLUSH))
701 rw = WRITE_FUA;
d2bdbee0
NB
702 else
703 rw = WRITE;
d0c8b259 704 } else {
dbbf3e94 705 rw = WRITE;
d0c8b259 706 }
dbbf3e94
CH
707 } else {
708 rw = READ;
709 }
710
c66ac9db 711 /*
5787cacd
CH
712 * Convert the blocksize advertised to the initiator to the 512 byte
713 * units unconditionally used by the Linux block layer.
c66ac9db 714 */
0fd97ccf 715 if (dev->dev_attrib.block_size == 4096)
72a0e5e2 716 block_lba = (cmd->t_task_lba << 3);
0fd97ccf 717 else if (dev->dev_attrib.block_size == 2048)
72a0e5e2 718 block_lba = (cmd->t_task_lba << 2);
0fd97ccf 719 else if (dev->dev_attrib.block_size == 1024)
72a0e5e2 720 block_lba = (cmd->t_task_lba << 1);
0fd97ccf 721 else if (dev->dev_attrib.block_size == 512)
72a0e5e2 722 block_lba = cmd->t_task_lba;
c66ac9db 723 else {
6708bb27 724 pr_err("Unsupported SCSI -> BLOCK LBA conversion:"
0fd97ccf 725 " %u\n", dev->dev_attrib.block_size);
de103c93 726 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
c66ac9db
NB
727 }
728
5787cacd
CH
729 ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
730 if (!ibr)
731 goto fail;
732 cmd->priv = ibr;
733
e0de4457
PB
734 if (!sgl_nents) {
735 atomic_set(&ibr->pending, 1);
736 iblock_complete_cmd(cmd);
737 return 0;
738 }
739
5787cacd
CH
740 bio = iblock_get_bio(cmd, block_lba, sgl_nents);
741 if (!bio)
742 goto fail_free_ibr;
dbbf3e94 743
ecebbf6c 744 bio_start = bio;
dbbf3e94
CH
745 bio_list_init(&list);
746 bio_list_add(&list, bio);
5787cacd
CH
747
748 atomic_set(&ibr->pending, 2);
d5b4a21b 749 bio_cnt = 1;
c66ac9db 750
5787cacd 751 for_each_sg(sgl, sg, sgl_nents, i) {
dbbf3e94
CH
752 /*
753 * XXX: if the length the device accepts is shorter than the
754 * length of the S/G list entry this will cause and
755 * endless loop. Better hope no driver uses huge pages.
756 */
757 while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
758 != sg->length) {
d5b4a21b
CH
759 if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) {
760 iblock_submit_bios(&list, rw);
761 bio_cnt = 0;
762 }
763
5787cacd 764 bio = iblock_get_bio(cmd, block_lba, sg_num);
6708bb27 765 if (!bio)
5787cacd
CH
766 goto fail_put_bios;
767
768 atomic_inc(&ibr->pending);
dbbf3e94 769 bio_list_add(&list, bio);
d5b4a21b 770 bio_cnt++;
c66ac9db 771 }
dbbf3e94 772
c66ac9db
NB
773 /* Always in 512 byte units for Linux/Block */
774 block_lba += sg->length >> IBLOCK_LBA_SHIFT;
775 sg_num--;
c66ac9db
NB
776 }
777
6f16ec43 778 if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
ecebbf6c
NB
779 int rc = iblock_alloc_bip(cmd, bio_start);
780 if (rc)
781 goto fail_put_bios;
782 }
783
d5b4a21b 784 iblock_submit_bios(&list, rw);
5787cacd 785 iblock_complete_cmd(cmd);
03e98c9e 786 return 0;
dbbf3e94 787
5787cacd 788fail_put_bios:
dbbf3e94 789 while ((bio = bio_list_pop(&list)))
c66ac9db 790 bio_put(bio);
5787cacd
CH
791fail_free_ibr:
792 kfree(ibr);
5787cacd 793fail:
de103c93 794 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
c66ac9db
NB
795}
796
c66ac9db
NB
797static sector_t iblock_get_blocks(struct se_device *dev)
798{
0fd97ccf
CH
799 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
800 struct block_device *bd = ib_dev->ibd_bd;
c66ac9db
NB
801 struct request_queue *q = bdev_get_queue(bd);
802
803 return iblock_emulate_read_cap_with_block_size(dev, bd, q);
804}
805
7f7caf6a
AG
806static sector_t iblock_get_alignment_offset_lbas(struct se_device *dev)
807{
808 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
809 struct block_device *bd = ib_dev->ibd_bd;
810 int ret;
811
812 ret = bdev_alignment_offset(bd);
813 if (ret == -1)
814 return 0;
815
816 /* convert offset-bytes to offset-lbas */
817 return ret / bdev_logical_block_size(bd);
818}
819
820static unsigned int iblock_get_lbppbe(struct se_device *dev)
821{
822 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
823 struct block_device *bd = ib_dev->ibd_bd;
824 int logs_per_phys = bdev_physical_block_size(bd) / bdev_logical_block_size(bd);
825
826 return ilog2(logs_per_phys);
827}
828
829static unsigned int iblock_get_io_min(struct se_device *dev)
830{
831 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
832 struct block_device *bd = ib_dev->ibd_bd;
833
834 return bdev_io_min(bd);
835}
836
837static unsigned int iblock_get_io_opt(struct se_device *dev)
838{
839 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
840 struct block_device *bd = ib_dev->ibd_bd;
841
842 return bdev_io_opt(bd);
843}
844
9e999a6c 845static struct sbc_ops iblock_sbc_ops = {
0c2ad7d1 846 .execute_rw = iblock_execute_rw,
ad67f0d9 847 .execute_sync_cache = iblock_execute_sync_cache,
6f974e8c 848 .execute_write_same = iblock_execute_write_same,
f6970ad3 849 .execute_write_same_unmap = iblock_execute_write_same_unmap,
14150a6b 850 .execute_unmap = iblock_execute_unmap,
0c2ad7d1
CH
851};
852
de103c93
CH
853static sense_reason_t
854iblock_parse_cdb(struct se_cmd *cmd)
0c2ad7d1 855{
9e999a6c 856 return sbc_parse_cdb(cmd, &iblock_sbc_ops);
0c2ad7d1
CH
857}
858
452e2010 859static bool iblock_get_write_cache(struct se_device *dev)
d0c8b259
NB
860{
861 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
862 struct block_device *bd = ib_dev->ibd_bd;
863 struct request_queue *q = bdev_get_queue(bd);
864
865 return q->flush_flags & REQ_FLUSH;
866}
867
0a06d430 868static const struct target_backend_ops iblock_ops = {
c66ac9db 869 .name = "iblock",
0fd97ccf
CH
870 .inquiry_prod = "IBLOCK",
871 .inquiry_rev = IBLOCK_VERSION,
c66ac9db 872 .owner = THIS_MODULE,
c66ac9db
NB
873 .attach_hba = iblock_attach_hba,
874 .detach_hba = iblock_detach_hba,
0fd97ccf
CH
875 .alloc_device = iblock_alloc_device,
876 .configure_device = iblock_configure_device,
c66ac9db 877 .free_device = iblock_free_device,
0c2ad7d1 878 .parse_cdb = iblock_parse_cdb,
c66ac9db
NB
879 .set_configfs_dev_params = iblock_set_configfs_dev_params,
880 .show_configfs_dev_params = iblock_show_configfs_dev_params,
6f23ac8a 881 .get_device_type = sbc_get_device_type,
c66ac9db 882 .get_blocks = iblock_get_blocks,
7f7caf6a
AG
883 .get_alignment_offset_lbas = iblock_get_alignment_offset_lbas,
884 .get_lbppbe = iblock_get_lbppbe,
885 .get_io_min = iblock_get_io_min,
886 .get_io_opt = iblock_get_io_opt,
d0c8b259 887 .get_write_cache = iblock_get_write_cache,
5873c4d1 888 .tb_dev_attrib_attrs = sbc_attrib_attrs,
c66ac9db
NB
889};
890
891static int __init iblock_module_init(void)
892{
0a06d430 893 return transport_backend_register(&iblock_ops);
c66ac9db
NB
894}
895
63b91d5a 896static void __exit iblock_module_exit(void)
c66ac9db 897{
0a06d430 898 target_backend_unregister(&iblock_ops);
c66ac9db
NB
899}
900
901MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin");
902MODULE_AUTHOR("nab@Linux-iSCSI.org");
903MODULE_LICENSE("GPL");
904
905module_init(iblock_module_init);
906module_exit(iblock_module_exit);
This page took 0.249348 seconds and 5 git commands to generate.