30 #include <linux/string.h>
35 #include <linux/slab.h>
40 #include <linux/module.h>
41 #include <scsi/scsi.h>
43 #include <asm/unaligned.h>
50 #define IBLOCK_MAX_BIO_PER_TASK 32
51 #define IBLOCK_BIO_POOL_SIZE 128
55 static void iblock_bio_done(
struct bio *,
int);
63 pr_debug(
"CORE_HBA[%d] - TCM iBlock HBA Driver %s on"
64 " Generic Target Core Stack %s\n", hba->
hba_id,
69 static void iblock_detach_hba(
struct se_hba *hba)
73 static void *iblock_allocate_virtdevice(
struct se_hba *hba,
const char *
name)
79 pr_err(
"Unable to allocate struct iblock_dev\n");
83 pr_debug(
"IBLOCK: Allocated ib_dev for %s\n", name);
88 static struct se_device *iblock_create_virtdevice(
98 struct queue_limits *limits;
104 pr_err(
"Unable to locate struct iblock_dev parameter\n");
111 pr_err(
"IBLOCK: Unable to create bioset()\n");
114 pr_debug(
"IBLOCK: Created bio_set()\n");
119 pr_debug(
"IBLOCK: Claiming struct block_device: %s\n",
135 q = bdev_get_queue(bd);
136 limits = &dev_limits.limits;
137 limits->logical_block_size = bdev_logical_block_size(bd);
140 dev_limits.hw_queue_depth = q->nr_requests;
141 dev_limits.queue_depth = q->nr_requests;
146 &iblock_template, se_dev, dev_flags, ib_dev,
156 if (blk_queue_discard(q)) {
157 dev->
se_sub_dev->se_dev_attrib.max_unmap_lba_count =
158 q->limits.max_discard_sectors;
162 dev->
se_sub_dev->se_dev_attrib.max_unmap_block_desc_count = 1;
163 dev->
se_sub_dev->se_dev_attrib.unmap_granularity =
164 q->limits.discard_granularity >> 9;
165 dev->
se_sub_dev->se_dev_attrib.unmap_granularity_alignment =
166 q->limits.discard_alignment;
168 pr_debug(
"IBLOCK: BLOCK Discard support available,"
169 " disabled by default\n");
172 if (blk_queue_nonrot(q))
186 static void iblock_free_device(
void *p)
197 static unsigned long long iblock_emulate_read_cap_with_block_size(
202 unsigned long long blocks_long = (div_u64(i_size_read(bd->
bd_inode),
203 bdev_logical_block_size(bd)) - 1);
206 if (block_size == dev->
se_sub_dev->se_dev_attrib.block_size)
209 switch (block_size) {
211 switch (dev->
se_sub_dev->se_dev_attrib.block_size) {
225 switch (dev->
se_sub_dev->se_dev_attrib.block_size) {
240 switch (dev->
se_sub_dev->se_dev_attrib.block_size) {
255 switch (dev->
se_sub_dev->se_dev_attrib.block_size) {
276 static void iblock_end_io_flush(
struct bio *bio,
int err)
281 pr_err(
"IBLOCK: cache flush failed: %d\n", err);
300 static int iblock_execute_sync_cache(
struct se_cmd *cmd)
314 bio->bi_end_io = iblock_end_io_flush;
315 bio->bi_bdev = ib_dev->
ibd_bd;
317 bio->bi_private =
cmd;
322 static int iblock_execute_unmap(
struct se_cmd *cmd)
334 pr_warn(
"UNMAP parameter list length %u too small\n",
342 dl = get_unaligned_be16(&buf[0]);
343 bd_dl = get_unaligned_be16(&buf[2]);
347 pr_warn(
"UNMAP parameter list length %u too small, ignoring bd_dl %u\n",
352 if (size / 16 > dev->
se_sub_dev->se_dev_attrib.max_unmap_block_desc_count) {
360 pr_debug(
"UNMAP: Sub: %s Using dl: %u bd_dl: %u size: %u"
361 " ptr: %p\n", dev->
transport->name, dl, bd_dl, size, ptr);
366 pr_debug(
"UNMAP: Using lba: %llu and range: %u\n",
367 (
unsigned long long)lba, range);
369 if (range > dev->
se_sub_dev->se_dev_attrib.max_unmap_lba_count) {
375 if (lba + range > dev->
transport->get_blocks(dev) + 1) {
384 pr_err(
"blkdev_issue_discard() failed: %d\n",
400 static int iblock_execute_write_same(
struct se_cmd *cmd)
409 pr_debug(
"blkdev_issue_discard() failed for WRITE_SAME\n");
428 static ssize_t iblock_set_configfs_dev_params(
struct se_hba *hba,
433 char *orig, *
ptr, *arg_p, *
opts;
436 unsigned long tmp_readonly;
444 while ((ptr =
strsep(&opts,
",\n")) !=
NULL) {
452 pr_err(
"Unable to set udev_path= while"
453 " ib_dev->ibd_bd exists\n");
462 pr_debug(
"IBLOCK: Referencing UDEV path: %s\n",
475 pr_err(
"strict_strtoul() failed for"
491 return (!ret) ? count :
ret;
494 static ssize_t iblock_check_configfs_dev_params(
501 pr_err(
"Missing udev_path= parameters for IBLOCK\n");
508 static ssize_t iblock_show_configfs_dev_params(
519 bl +=
sprintf(b + bl,
"iBlock device: %s",
522 bl +=
sprintf(b + bl,
" UDEV PATH: %s",
528 bl +=
sprintf(b + bl,
"Major: %d Minor: %d %s\n",
531 "CLAIMED: IBLOCK" :
"CLAIMED: OS");
533 bl +=
sprintf(b + bl,
"Major: 0 Minor: 0\n");
539 static void iblock_complete_cmd(
struct se_cmd *cmd)
566 if (sg_num > BIO_MAX_PAGES)
567 sg_num = BIO_MAX_PAGES;
571 pr_err(
"Unable to allocate memory for bio\n");
575 bio->bi_bdev = ib_dev->
ibd_bd;
576 bio->bi_private =
cmd;
577 bio->bi_end_io = &iblock_bio_done;
578 bio->bi_sector =
lba;
588 while ((bio = bio_list_pop(list)))
593 static int iblock_execute_rw(
struct se_cmd *cmd)
603 u32 sg_num = sgl_nents;
614 if (dev->
se_sub_dev->se_dev_attrib.emulate_write_cache == 0 ||
615 (dev->
se_sub_dev->se_dev_attrib.emulate_fua_write > 0 &&
628 if (dev->
se_sub_dev->se_dev_attrib.block_size == 4096)
630 else if (dev->
se_sub_dev->se_dev_attrib.block_size == 2048)
632 else if (dev->
se_sub_dev->se_dev_attrib.block_size == 1024)
634 else if (dev->
se_sub_dev->se_dev_attrib.block_size == 512)
637 pr_err(
"Unsupported SCSI -> BLOCK LBA conversion:"
638 " %u\n", dev->
se_sub_dev->se_dev_attrib.block_size);
650 iblock_complete_cmd(cmd);
654 bio = iblock_get_bio(cmd, block_lba, sgl_nents);
658 bio_list_init(&list);
659 bio_list_add(&list, bio);
673 iblock_submit_bios(&list, rw);
677 bio = iblock_get_bio(cmd, block_lba, sg_num);
682 bio_list_add(&list, bio);
691 iblock_submit_bios(&list, rw);
692 iblock_complete_cmd(cmd);
696 while ((bio = bio_list_pop(&list)))
710 static u32 iblock_get_device_type(
struct se_device *dev)
721 return iblock_emulate_read_cap_with_block_size(dev, bd, q);
724 static void iblock_bio_done(
struct bio *bio,
int err)
726 struct se_cmd *cmd = bio->bi_private;
732 if (!
test_bit(BIO_UPTODATE, &bio->bi_flags) && !err)
736 pr_err(
"test_bit(BIO_UPTODATE) failed for bio: %p,"
737 " err: %d\n", bio, err);
747 iblock_complete_cmd(cmd);
750 static struct spc_ops iblock_spc_ops = {
751 .execute_rw = iblock_execute_rw,
752 .execute_sync_cache = iblock_execute_sync_cache,
753 .execute_write_same = iblock_execute_write_same,
754 .execute_unmap = iblock_execute_unmap,
757 static int iblock_parse_cdb(
struct se_cmd *cmd)
766 .attach_hba = iblock_attach_hba,
767 .detach_hba = iblock_detach_hba,
768 .allocate_virtdevice = iblock_allocate_virtdevice,
769 .create_virtdevice = iblock_create_virtdevice,
770 .free_device = iblock_free_device,
771 .parse_cdb = iblock_parse_cdb,
772 .check_configfs_dev_params = iblock_check_configfs_dev_params,
773 .set_configfs_dev_params = iblock_set_configfs_dev_params,
774 .show_configfs_dev_params = iblock_show_configfs_dev_params,
775 .get_device_rev = iblock_get_device_rev,
776 .get_device_type = iblock_get_device_type,
777 .get_blocks = iblock_get_blocks,
780 static int __init iblock_module_init(
void)
785 static void iblock_module_exit(
void)