11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2c66ac9dbSNicholas Bellinger /******************************************************************************* 3c66ac9dbSNicholas Bellinger * Filename: target_core_iblock.c 4c66ac9dbSNicholas Bellinger * 5c66ac9dbSNicholas Bellinger * This file contains the Storage Engine <-> Linux BlockIO transport 6c66ac9dbSNicholas Bellinger * specific functions. 7c66ac9dbSNicholas Bellinger * 84c76251eSNicholas Bellinger * (c) Copyright 2003-2013 Datera, Inc. 9c66ac9dbSNicholas Bellinger * 10c66ac9dbSNicholas Bellinger * Nicholas A. Bellinger <nab@kernel.org> 11c66ac9dbSNicholas Bellinger * 12c66ac9dbSNicholas Bellinger ******************************************************************************/ 13c66ac9dbSNicholas Bellinger 14c66ac9dbSNicholas Bellinger #include <linux/string.h> 15c66ac9dbSNicholas Bellinger #include <linux/parser.h> 16c66ac9dbSNicholas Bellinger #include <linux/timer.h> 17c66ac9dbSNicholas Bellinger #include <linux/fs.h> 18c66ac9dbSNicholas Bellinger #include <linux/blkdev.h> 19fe45e630SChristoph Hellwig #include <linux/blk-integrity.h> 20c66ac9dbSNicholas Bellinger #include <linux/slab.h> 21c66ac9dbSNicholas Bellinger #include <linux/spinlock.h> 22c66ac9dbSNicholas Bellinger #include <linux/bio.h> 23c66ac9dbSNicholas Bellinger #include <linux/file.h> 24827509e3SPaul Gortmaker #include <linux/module.h> 2524b83debSChristoph Hellwig #include <linux/scatterlist.h> 26ba929992SBart Van Assche #include <scsi/scsi_proto.h> 2714150a6bSChristoph Hellwig #include <asm/unaligned.h> 28c66ac9dbSNicholas Bellinger 29c66ac9dbSNicholas Bellinger #include <target/target_core_base.h> 30c4795fb2SChristoph Hellwig #include <target/target_core_backend.h> 31c66ac9dbSNicholas Bellinger 32c66ac9dbSNicholas Bellinger #include "target_core_iblock.h" 33c66ac9dbSNicholas Bellinger 34d5b4a21bSChristoph Hellwig #define IBLOCK_MAX_BIO_PER_TASK 32 /* max # of bios to submit at a time */ 35d5b4a21bSChristoph Hellwig #define IBLOCK_BIO_POOL_SIZE 128 36d5b4a21bSChristoph Hellwig 370fd97ccfSChristoph Hellwig static inline struct iblock_dev *IBLOCK_DEV(struct se_device *dev) 380fd97ccfSChristoph Hellwig { 390fd97ccfSChristoph Hellwig return container_of(dev, struct iblock_dev, dev); 400fd97ccfSChristoph Hellwig } 410fd97ccfSChristoph Hellwig 420fd97ccfSChristoph Hellwig 43c66ac9dbSNicholas Bellinger static int iblock_attach_hba(struct se_hba *hba, u32 host_id) 44c66ac9dbSNicholas Bellinger { 456708bb27SAndy Grover pr_debug("CORE_HBA[%d] - TCM iBlock HBA Driver %s on" 46c66ac9dbSNicholas Bellinger " Generic Target Core Stack %s\n", hba->hba_id, 47ce8dd25dSChristoph Hellwig IBLOCK_VERSION, TARGET_CORE_VERSION); 48c66ac9dbSNicholas Bellinger return 0; 49c66ac9dbSNicholas Bellinger } 50c66ac9dbSNicholas Bellinger 51c66ac9dbSNicholas Bellinger static void iblock_detach_hba(struct se_hba *hba) 52c66ac9dbSNicholas Bellinger { 53c66ac9dbSNicholas Bellinger } 54c66ac9dbSNicholas Bellinger 550fd97ccfSChristoph Hellwig static struct se_device *iblock_alloc_device(struct se_hba *hba, const char *name) 56c66ac9dbSNicholas Bellinger { 57c66ac9dbSNicholas Bellinger struct iblock_dev *ib_dev = NULL; 58c66ac9dbSNicholas Bellinger 59c66ac9dbSNicholas Bellinger ib_dev = kzalloc(sizeof(struct iblock_dev), GFP_KERNEL); 606708bb27SAndy Grover if (!ib_dev) { 616708bb27SAndy Grover pr_err("Unable to allocate struct iblock_dev\n"); 62c66ac9dbSNicholas Bellinger return NULL; 63c66ac9dbSNicholas Bellinger } 64c66ac9dbSNicholas Bellinger 65415ccd98SMike Christie ib_dev->ibd_plug = kcalloc(nr_cpu_ids, sizeof(*ib_dev->ibd_plug), 66415ccd98SMike Christie GFP_KERNEL); 67415ccd98SMike Christie if (!ib_dev->ibd_plug) 68415ccd98SMike Christie goto free_dev; 69415ccd98SMike Christie 706708bb27SAndy Grover pr_debug( "IBLOCK: Allocated ib_dev for %s\n", name); 71c66ac9dbSNicholas Bellinger 720fd97ccfSChristoph Hellwig return &ib_dev->dev; 73415ccd98SMike Christie 74415ccd98SMike Christie free_dev: 75415ccd98SMike Christie kfree(ib_dev); 76415ccd98SMike Christie return NULL; 77c66ac9dbSNicholas Bellinger } 78c66ac9dbSNicholas Bellinger 79d7c382c5SMike Christie static bool iblock_configure_unmap(struct se_device *dev) 80d7c382c5SMike Christie { 81d7c382c5SMike Christie struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 82d7c382c5SMike Christie 83d7c382c5SMike Christie return target_configure_unmap_from_queue(&dev->dev_attrib, 84d7c382c5SMike Christie ib_dev->ibd_bd); 85d7c382c5SMike Christie } 86d7c382c5SMike Christie 870fd97ccfSChristoph Hellwig static int iblock_configure_device(struct se_device *dev) 88c66ac9dbSNicholas Bellinger { 890fd97ccfSChristoph Hellwig struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 90c66ac9dbSNicholas Bellinger struct request_queue *q; 910fd97ccfSChristoph Hellwig struct block_device *bd = NULL; 92ecebbf6cSNicholas Bellinger struct blk_integrity *bi; 93*05bdb996SChristoph Hellwig blk_mode_t mode = BLK_OPEN_READ; 942237498fSNicholas Bellinger unsigned int max_write_zeroes_sectors; 958f13142aSColin Ian King int ret; 96c66ac9dbSNicholas Bellinger 970fd97ccfSChristoph Hellwig if (!(ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)) { 980fd97ccfSChristoph Hellwig pr_err("Missing udev_path= parameters for IBLOCK\n"); 990fd97ccfSChristoph Hellwig return -EINVAL; 100c66ac9dbSNicholas Bellinger } 101d5b4a21bSChristoph Hellwig 102a47a28b7SKent Overstreet ret = bioset_init(&ib_dev->ibd_bio_set, IBLOCK_BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS); 103a47a28b7SKent Overstreet if (ret) { 1040fd97ccfSChristoph Hellwig pr_err("IBLOCK: Unable to create bioset\n"); 1050fd97ccfSChristoph Hellwig goto out; 106c66ac9dbSNicholas Bellinger } 1070fd97ccfSChristoph Hellwig 1086708bb27SAndy Grover pr_debug( "IBLOCK: Claiming struct block_device: %s\n", 109c66ac9dbSNicholas Bellinger ib_dev->ibd_udev_path); 110c66ac9dbSNicholas Bellinger 11144bfd018SAndy Grover if (!ib_dev->ibd_readonly) 112*05bdb996SChristoph Hellwig mode |= BLK_OPEN_WRITE; 113eeeb9522SNicholas Bellinger else 114eeeb9522SNicholas Bellinger dev->dev_flags |= DF_READ_ONLY; 11544bfd018SAndy Grover 1160718afd4SChristoph Hellwig bd = blkdev_get_by_path(ib_dev->ibd_udev_path, mode, ib_dev, NULL); 117613640e4SNicholas Bellinger if (IS_ERR(bd)) { 118613640e4SNicholas Bellinger ret = PTR_ERR(bd); 1190fd97ccfSChristoph Hellwig goto out_free_bioset; 120613640e4SNicholas Bellinger } 121c66ac9dbSNicholas Bellinger ib_dev->ibd_bd = bd; 122c66ac9dbSNicholas Bellinger 1230fd97ccfSChristoph Hellwig q = bdev_get_queue(bd); 1240fd97ccfSChristoph Hellwig 1250fd97ccfSChristoph Hellwig dev->dev_attrib.hw_block_size = bdev_logical_block_size(bd); 1269375031eSAnastasia Kovaleva dev->dev_attrib.hw_max_sectors = mult_frac(queue_max_hw_sectors(q), 1279375031eSAnastasia Kovaleva SECTOR_SIZE, 1289375031eSAnastasia Kovaleva dev->dev_attrib.hw_block_size); 1290fd97ccfSChristoph Hellwig dev->dev_attrib.hw_queue_depth = q->nr_requests; 130c66ac9dbSNicholas Bellinger 131f6970ad3SNicholas Bellinger /* 132f6970ad3SNicholas Bellinger * Enable write same emulation for IBLOCK and use 0xFFFF as 133f6970ad3SNicholas Bellinger * the smaller WRITE_SAME(10) only has a two-byte block count. 134f6970ad3SNicholas Bellinger */ 1352237498fSNicholas Bellinger max_write_zeroes_sectors = bdev_write_zeroes_sectors(bd); 1362237498fSNicholas Bellinger if (max_write_zeroes_sectors) 1372237498fSNicholas Bellinger dev->dev_attrib.max_write_same_len = max_write_zeroes_sectors; 1382237498fSNicholas Bellinger else 139f6970ad3SNicholas Bellinger dev->dev_attrib.max_write_same_len = 0xFFFF; 140c66ac9dbSNicholas Bellinger 14110f0d2a5SChristoph Hellwig if (bdev_nonrot(bd)) 1420fd97ccfSChristoph Hellwig dev->dev_attrib.is_nonrot = 1; 143d0c8b259SNicholas Bellinger 144ecebbf6cSNicholas Bellinger bi = bdev_get_integrity(bd); 145ecebbf6cSNicholas Bellinger if (bi) { 146a47a28b7SKent Overstreet struct bio_set *bs = &ib_dev->ibd_bio_set; 147ecebbf6cSNicholas Bellinger 1480f8087ecSMartin K. Petersen if (!strcmp(bi->profile->name, "T10-DIF-TYPE3-IP") || 1490f8087ecSMartin K. Petersen !strcmp(bi->profile->name, "T10-DIF-TYPE1-IP")) { 150ecebbf6cSNicholas Bellinger pr_err("IBLOCK export of blk_integrity: %s not" 1510f8087ecSMartin K. Petersen " supported\n", bi->profile->name); 152ecebbf6cSNicholas Bellinger ret = -ENOSYS; 153ecebbf6cSNicholas Bellinger goto out_blkdev_put; 154ecebbf6cSNicholas Bellinger } 155ecebbf6cSNicholas Bellinger 1560f8087ecSMartin K. Petersen if (!strcmp(bi->profile->name, "T10-DIF-TYPE3-CRC")) { 157ecebbf6cSNicholas Bellinger dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE3_PROT; 1580f8087ecSMartin K. Petersen } else if (!strcmp(bi->profile->name, "T10-DIF-TYPE1-CRC")) { 159ecebbf6cSNicholas Bellinger dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE1_PROT; 160ecebbf6cSNicholas Bellinger } 161ecebbf6cSNicholas Bellinger 162ecebbf6cSNicholas Bellinger if (dev->dev_attrib.pi_prot_type) { 163ecebbf6cSNicholas Bellinger if (bioset_integrity_create(bs, IBLOCK_BIO_POOL_SIZE) < 0) { 164ecebbf6cSNicholas Bellinger pr_err("Unable to allocate bioset for PI\n"); 165ecebbf6cSNicholas Bellinger ret = -ENOMEM; 166ecebbf6cSNicholas Bellinger goto out_blkdev_put; 167ecebbf6cSNicholas Bellinger } 168ecebbf6cSNicholas Bellinger pr_debug("IBLOCK setup BIP bs->bio_integrity_pool: %p\n", 169f4f8154aSKent Overstreet &bs->bio_integrity_pool); 170ecebbf6cSNicholas Bellinger } 171ecebbf6cSNicholas Bellinger dev->dev_attrib.hw_pi_prot_type = dev->dev_attrib.pi_prot_type; 172ecebbf6cSNicholas Bellinger } 173ecebbf6cSNicholas Bellinger 1740fd97ccfSChristoph Hellwig return 0; 175e22a7f07SRoland Dreier 176ecebbf6cSNicholas Bellinger out_blkdev_put: 1772736e8eeSChristoph Hellwig blkdev_put(ib_dev->ibd_bd, ib_dev); 1780fd97ccfSChristoph Hellwig out_free_bioset: 179a47a28b7SKent Overstreet bioset_exit(&ib_dev->ibd_bio_set); 1800fd97ccfSChristoph Hellwig out: 1810fd97ccfSChristoph Hellwig return ret; 182c66ac9dbSNicholas Bellinger } 183c66ac9dbSNicholas Bellinger 1844cc987eaSNicholas Bellinger static void iblock_dev_call_rcu(struct rcu_head *p) 1854cc987eaSNicholas Bellinger { 1864cc987eaSNicholas Bellinger struct se_device *dev = container_of(p, struct se_device, rcu_head); 1874cc987eaSNicholas Bellinger struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 1884cc987eaSNicholas Bellinger 189415ccd98SMike Christie kfree(ib_dev->ibd_plug); 1904cc987eaSNicholas Bellinger kfree(ib_dev); 1914cc987eaSNicholas Bellinger } 1924cc987eaSNicholas Bellinger 1930fd97ccfSChristoph Hellwig static void iblock_free_device(struct se_device *dev) 194c66ac9dbSNicholas Bellinger { 19592634706SMike Christie call_rcu(&dev->rcu_head, iblock_dev_call_rcu); 19692634706SMike Christie } 19792634706SMike Christie 19892634706SMike Christie static void iblock_destroy_device(struct se_device *dev) 19992634706SMike Christie { 2000fd97ccfSChristoph Hellwig struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 201c66ac9dbSNicholas Bellinger 202bc665524SNicholas Bellinger if (ib_dev->ibd_bd != NULL) 2032736e8eeSChristoph Hellwig blkdev_put(ib_dev->ibd_bd, ib_dev); 204a47a28b7SKent Overstreet bioset_exit(&ib_dev->ibd_bio_set); 205c66ac9dbSNicholas Bellinger } 206c66ac9dbSNicholas Bellinger 207415ccd98SMike Christie static struct se_dev_plug *iblock_plug_device(struct se_device *se_dev) 208415ccd98SMike Christie { 209415ccd98SMike Christie struct iblock_dev *ib_dev = IBLOCK_DEV(se_dev); 210415ccd98SMike Christie struct iblock_dev_plug *ib_dev_plug; 211415ccd98SMike Christie 212415ccd98SMike Christie /* 2135aaeca25SMike Christie * Each se_device has a per cpu work this can be run from. We 214415ccd98SMike Christie * shouldn't have multiple threads on the same cpu calling this 215415ccd98SMike Christie * at the same time. 216415ccd98SMike Christie */ 2175aaeca25SMike Christie ib_dev_plug = &ib_dev->ibd_plug[raw_smp_processor_id()]; 218415ccd98SMike Christie if (test_and_set_bit(IBD_PLUGF_PLUGGED, &ib_dev_plug->flags)) 219415ccd98SMike Christie return NULL; 220415ccd98SMike Christie 221415ccd98SMike Christie blk_start_plug(&ib_dev_plug->blk_plug); 222415ccd98SMike Christie return &ib_dev_plug->se_plug; 223415ccd98SMike Christie } 224415ccd98SMike Christie 225415ccd98SMike Christie static void iblock_unplug_device(struct se_dev_plug *se_plug) 226415ccd98SMike Christie { 227415ccd98SMike Christie struct iblock_dev_plug *ib_dev_plug = container_of(se_plug, 228415ccd98SMike Christie struct iblock_dev_plug, se_plug); 229415ccd98SMike Christie 230415ccd98SMike Christie blk_finish_plug(&ib_dev_plug->blk_plug); 231415ccd98SMike Christie clear_bit(IBD_PLUGF_PLUGGED, &ib_dev_plug->flags); 232415ccd98SMike Christie } 233415ccd98SMike Christie 234f915f58eSChristoph Hellwig static sector_t iblock_get_blocks(struct se_device *dev) 235c66ac9dbSNicholas Bellinger { 236f915f58eSChristoph Hellwig struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 237f915f58eSChristoph Hellwig u32 block_size = bdev_logical_block_size(ib_dev->ibd_bd); 23864f0f426SChristoph Hellwig unsigned long long blocks_long = 239f915f58eSChristoph Hellwig div_u64(bdev_nr_bytes(ib_dev->ibd_bd), block_size) - 1; 240c66ac9dbSNicholas Bellinger 2410fd97ccfSChristoph Hellwig if (block_size == dev->dev_attrib.block_size) 242c66ac9dbSNicholas Bellinger return blocks_long; 243c66ac9dbSNicholas Bellinger 244c66ac9dbSNicholas Bellinger switch (block_size) { 245c66ac9dbSNicholas Bellinger case 4096: 2460fd97ccfSChristoph Hellwig switch (dev->dev_attrib.block_size) { 247c66ac9dbSNicholas Bellinger case 2048: 248c66ac9dbSNicholas Bellinger blocks_long <<= 1; 249c66ac9dbSNicholas Bellinger break; 250c66ac9dbSNicholas Bellinger case 1024: 251c66ac9dbSNicholas Bellinger blocks_long <<= 2; 252c66ac9dbSNicholas Bellinger break; 253c66ac9dbSNicholas Bellinger case 512: 254c66ac9dbSNicholas Bellinger blocks_long <<= 3; 255492096ecSGustavo A. R. Silva break; 256c66ac9dbSNicholas Bellinger default: 257c66ac9dbSNicholas Bellinger break; 258c66ac9dbSNicholas Bellinger } 259c66ac9dbSNicholas Bellinger break; 260c66ac9dbSNicholas Bellinger case 2048: 2610fd97ccfSChristoph Hellwig switch (dev->dev_attrib.block_size) { 262c66ac9dbSNicholas Bellinger case 4096: 263c66ac9dbSNicholas Bellinger blocks_long >>= 1; 264c66ac9dbSNicholas Bellinger break; 265c66ac9dbSNicholas Bellinger case 1024: 266c66ac9dbSNicholas Bellinger blocks_long <<= 1; 267c66ac9dbSNicholas Bellinger break; 268c66ac9dbSNicholas Bellinger case 512: 269c66ac9dbSNicholas Bellinger blocks_long <<= 2; 270c66ac9dbSNicholas Bellinger break; 271c66ac9dbSNicholas Bellinger default: 272c66ac9dbSNicholas Bellinger break; 273c66ac9dbSNicholas Bellinger } 274c66ac9dbSNicholas Bellinger break; 275c66ac9dbSNicholas Bellinger case 1024: 2760fd97ccfSChristoph Hellwig switch (dev->dev_attrib.block_size) { 277c66ac9dbSNicholas Bellinger case 4096: 278c66ac9dbSNicholas Bellinger blocks_long >>= 2; 279c66ac9dbSNicholas Bellinger break; 280c66ac9dbSNicholas Bellinger case 2048: 281c66ac9dbSNicholas Bellinger blocks_long >>= 1; 282c66ac9dbSNicholas Bellinger break; 283c66ac9dbSNicholas Bellinger case 512: 284c66ac9dbSNicholas Bellinger blocks_long <<= 1; 285c66ac9dbSNicholas Bellinger break; 286c66ac9dbSNicholas Bellinger default: 287c66ac9dbSNicholas Bellinger break; 288c66ac9dbSNicholas Bellinger } 289c66ac9dbSNicholas Bellinger break; 290c66ac9dbSNicholas Bellinger case 512: 2910fd97ccfSChristoph Hellwig switch (dev->dev_attrib.block_size) { 292c66ac9dbSNicholas Bellinger case 4096: 293c66ac9dbSNicholas Bellinger blocks_long >>= 3; 294c66ac9dbSNicholas Bellinger break; 295c66ac9dbSNicholas Bellinger case 2048: 296c66ac9dbSNicholas Bellinger blocks_long >>= 2; 297c66ac9dbSNicholas Bellinger break; 298c66ac9dbSNicholas Bellinger case 1024: 299c66ac9dbSNicholas Bellinger blocks_long >>= 1; 300c66ac9dbSNicholas Bellinger break; 301c66ac9dbSNicholas Bellinger default: 302c66ac9dbSNicholas Bellinger break; 303c66ac9dbSNicholas Bellinger } 304c66ac9dbSNicholas Bellinger break; 305c66ac9dbSNicholas Bellinger default: 306c66ac9dbSNicholas Bellinger break; 307c66ac9dbSNicholas Bellinger } 308c66ac9dbSNicholas Bellinger 309c66ac9dbSNicholas Bellinger return blocks_long; 310c66ac9dbSNicholas Bellinger } 311c66ac9dbSNicholas Bellinger 3123a41d85fSNicholas Bellinger static void iblock_complete_cmd(struct se_cmd *cmd) 3133a41d85fSNicholas Bellinger { 3143a41d85fSNicholas Bellinger struct iblock_req *ibr = cmd->priv; 3153a41d85fSNicholas Bellinger u8 status; 3163a41d85fSNicholas Bellinger 3175981c245SElena Reshetova if (!refcount_dec_and_test(&ibr->pending)) 3183a41d85fSNicholas Bellinger return; 3193a41d85fSNicholas Bellinger 3203a41d85fSNicholas Bellinger if (atomic_read(&ibr->ib_bio_err_cnt)) 3213a41d85fSNicholas Bellinger status = SAM_STAT_CHECK_CONDITION; 3223a41d85fSNicholas Bellinger else 3233a41d85fSNicholas Bellinger status = SAM_STAT_GOOD; 3243a41d85fSNicholas Bellinger 3253a41d85fSNicholas Bellinger target_complete_cmd(cmd, status); 3263a41d85fSNicholas Bellinger kfree(ibr); 3273a41d85fSNicholas Bellinger } 3283a41d85fSNicholas Bellinger 3294246a0b6SChristoph Hellwig static void iblock_bio_done(struct bio *bio) 3303a41d85fSNicholas Bellinger { 3313a41d85fSNicholas Bellinger struct se_cmd *cmd = bio->bi_private; 3323a41d85fSNicholas Bellinger struct iblock_req *ibr = cmd->priv; 3333a41d85fSNicholas Bellinger 3344e4cbee9SChristoph Hellwig if (bio->bi_status) { 3354e4cbee9SChristoph Hellwig pr_err("bio error: %p, err: %d\n", bio, bio->bi_status); 3363a41d85fSNicholas Bellinger /* 3373a41d85fSNicholas Bellinger * Bump the ib_bio_err_cnt and release bio. 3383a41d85fSNicholas Bellinger */ 3393a41d85fSNicholas Bellinger atomic_inc(&ibr->ib_bio_err_cnt); 3404e857c58SPeter Zijlstra smp_mb__after_atomic(); 3413a41d85fSNicholas Bellinger } 3423a41d85fSNicholas Bellinger 3433a41d85fSNicholas Bellinger bio_put(bio); 3443a41d85fSNicholas Bellinger 3453a41d85fSNicholas Bellinger iblock_complete_cmd(cmd); 3463a41d85fSNicholas Bellinger } 3473a41d85fSNicholas Bellinger 348bc9e0e36SChaitanya Kulkarni static struct bio *iblock_get_bio(struct se_cmd *cmd, sector_t lba, u32 sg_num, 34979fe9d7dSBart Van Assche blk_opf_t opf) 3503a41d85fSNicholas Bellinger { 3513a41d85fSNicholas Bellinger struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev); 3523a41d85fSNicholas Bellinger struct bio *bio; 3533a41d85fSNicholas Bellinger 3543a41d85fSNicholas Bellinger /* 3553a41d85fSNicholas Bellinger * Only allocate as many vector entries as the bio code allows us to, 3563a41d85fSNicholas Bellinger * we'll loop later on until we have handled the whole request. 3573a41d85fSNicholas Bellinger */ 358609be106SChristoph Hellwig bio = bio_alloc_bioset(ib_dev->ibd_bd, bio_max_segs(sg_num), opf, 359609be106SChristoph Hellwig GFP_NOIO, &ib_dev->ibd_bio_set); 3603a41d85fSNicholas Bellinger if (!bio) { 3613a41d85fSNicholas Bellinger pr_err("Unable to allocate memory for bio\n"); 3623a41d85fSNicholas Bellinger return NULL; 3633a41d85fSNicholas Bellinger } 3643a41d85fSNicholas Bellinger 3653a41d85fSNicholas Bellinger bio->bi_private = cmd; 3663a41d85fSNicholas Bellinger bio->bi_end_io = &iblock_bio_done; 3674f024f37SKent Overstreet bio->bi_iter.bi_sector = lba; 3683a41d85fSNicholas Bellinger 3693a41d85fSNicholas Bellinger return bio; 3703a41d85fSNicholas Bellinger } 3713a41d85fSNicholas Bellinger 3724e49ea4aSMike Christie static void iblock_submit_bios(struct bio_list *list) 3733a41d85fSNicholas Bellinger { 3743a41d85fSNicholas Bellinger struct blk_plug plug; 3753a41d85fSNicholas Bellinger struct bio *bio; 376415ccd98SMike Christie /* 377415ccd98SMike Christie * The block layer handles nested plugs, so just plug/unplug to handle 378415ccd98SMike Christie * fabric drivers that didn't support batching and multi bio cmds. 379415ccd98SMike Christie */ 3803a41d85fSNicholas Bellinger blk_start_plug(&plug); 3813a41d85fSNicholas Bellinger while ((bio = bio_list_pop(list))) 3824e49ea4aSMike Christie submit_bio(bio); 3833a41d85fSNicholas Bellinger blk_finish_plug(&plug); 3843a41d85fSNicholas Bellinger } 3853a41d85fSNicholas Bellinger 3864246a0b6SChristoph Hellwig static void iblock_end_io_flush(struct bio *bio) 387df5fa691SChristoph Hellwig { 388df5fa691SChristoph Hellwig struct se_cmd *cmd = bio->bi_private; 389df5fa691SChristoph Hellwig 3904e4cbee9SChristoph Hellwig if (bio->bi_status) 3914e4cbee9SChristoph Hellwig pr_err("IBLOCK: cache flush failed: %d\n", bio->bi_status); 392df5fa691SChristoph Hellwig 3935787cacdSChristoph Hellwig if (cmd) { 3944e4cbee9SChristoph Hellwig if (bio->bi_status) 3955787cacdSChristoph Hellwig target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION); 396de103c93SChristoph Hellwig else 3975787cacdSChristoph Hellwig target_complete_cmd(cmd, SAM_STAT_GOOD); 3985787cacdSChristoph Hellwig } 3995787cacdSChristoph Hellwig 400df5fa691SChristoph Hellwig bio_put(bio); 401df5fa691SChristoph Hellwig } 402df5fa691SChristoph Hellwig 403c66ac9dbSNicholas Bellinger /* 404df5fa691SChristoph Hellwig * Implement SYCHRONIZE CACHE. Note that we can't handle lba ranges and must 405df5fa691SChristoph Hellwig * always flush the whole cache. 406c66ac9dbSNicholas Bellinger */ 407de103c93SChristoph Hellwig static sense_reason_t 408de103c93SChristoph Hellwig iblock_execute_sync_cache(struct se_cmd *cmd) 409c66ac9dbSNicholas Bellinger { 4100fd97ccfSChristoph Hellwig struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev); 411a1d8b49aSAndy Grover int immed = (cmd->t_task_cdb[1] & 0x2); 412df5fa691SChristoph Hellwig struct bio *bio; 413c66ac9dbSNicholas Bellinger 414c66ac9dbSNicholas Bellinger /* 415c66ac9dbSNicholas Bellinger * If the Immediate bit is set, queue up the GOOD response 416df5fa691SChristoph Hellwig * for this SYNCHRONIZE_CACHE op. 417c66ac9dbSNicholas Bellinger */ 418c66ac9dbSNicholas Bellinger if (immed) 4195787cacdSChristoph Hellwig target_complete_cmd(cmd, SAM_STAT_GOOD); 420c66ac9dbSNicholas Bellinger 42107888c66SChristoph Hellwig bio = bio_alloc(ib_dev->ibd_bd, 0, REQ_OP_WRITE | REQ_PREFLUSH, 42207888c66SChristoph Hellwig GFP_KERNEL); 423df5fa691SChristoph Hellwig bio->bi_end_io = iblock_end_io_flush; 424c66ac9dbSNicholas Bellinger if (!immed) 425df5fa691SChristoph Hellwig bio->bi_private = cmd; 4264e49ea4aSMike Christie submit_bio(bio); 427ad67f0d9SChristoph Hellwig return 0; 428c66ac9dbSNicholas Bellinger } 429c66ac9dbSNicholas Bellinger 430de103c93SChristoph Hellwig static sense_reason_t 43162e46942SChristoph Hellwig iblock_execute_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb) 432dbc21c5aSAsias He { 43362e46942SChristoph Hellwig struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd; 4348a9ebe71SMike Christie struct se_device *dev = cmd->se_dev; 435dbc21c5aSAsias He int ret; 436dbc21c5aSAsias He 4378a9ebe71SMike Christie ret = blkdev_issue_discard(bdev, 4388a9ebe71SMike Christie target_to_linux_sector(dev, lba), 4398a9ebe71SMike Christie target_to_linux_sector(dev, nolb), 44044abff2cSChristoph Hellwig GFP_KERNEL); 441dbc21c5aSAsias He if (ret < 0) { 442dbc21c5aSAsias He pr_err("blkdev_issue_discard() failed: %d\n", ret); 443dbc21c5aSAsias He return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 444dbc21c5aSAsias He } 445dbc21c5aSAsias He 446dbc21c5aSAsias He return 0; 447dbc21c5aSAsias He } 448dbc21c5aSAsias He 449dbc21c5aSAsias He static sense_reason_t 4502237498fSNicholas Bellinger iblock_execute_zero_out(struct block_device *bdev, struct se_cmd *cmd) 45107b63196SMike Christie { 45207b63196SMike Christie struct se_device *dev = cmd->se_dev; 45307b63196SMike Christie struct scatterlist *sg = &cmd->t_data_sg[0]; 454f5957dadSBryant G Ly unsigned char *buf, *not_zero; 455f5957dadSBryant G Ly int ret; 45607b63196SMike Christie 4572237498fSNicholas Bellinger buf = kmap(sg_page(sg)) + sg->offset; 4582237498fSNicholas Bellinger if (!buf) 4592237498fSNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 4602237498fSNicholas Bellinger /* 4612237498fSNicholas Bellinger * Fall back to block_execute_write_same() slow-path if 4622237498fSNicholas Bellinger * incoming WRITE_SAME payload does not contain zeros. 4632237498fSNicholas Bellinger */ 464f5957dadSBryant G Ly not_zero = memchr_inv(buf, 0x00, cmd->data_length); 4652237498fSNicholas Bellinger kunmap(sg_page(sg)); 46607b63196SMike Christie 467f5957dadSBryant G Ly if (not_zero) 4682237498fSNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 4692237498fSNicholas Bellinger 4702237498fSNicholas Bellinger ret = blkdev_issue_zeroout(bdev, 47107b63196SMike Christie target_to_linux_sector(dev, cmd->t_task_lba), 47207b63196SMike Christie target_to_linux_sector(dev, 47307b63196SMike Christie sbc_get_write_same_sectors(cmd)), 4741d2ff149SDavid Disseldorp GFP_KERNEL, BLKDEV_ZERO_NOUNMAP); 47507b63196SMike Christie if (ret) 47607b63196SMike Christie return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 47707b63196SMike Christie 47814b40c1eSHannes Reinecke target_complete_cmd(cmd, SAM_STAT_GOOD); 47907b63196SMike Christie return 0; 48007b63196SMike Christie } 48107b63196SMike Christie 48207b63196SMike Christie static sense_reason_t 483f6970ad3SNicholas Bellinger iblock_execute_write_same(struct se_cmd *cmd) 484f6970ad3SNicholas Bellinger { 48507b63196SMike Christie struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd; 486f6970ad3SNicholas Bellinger struct iblock_req *ibr; 487f6970ad3SNicholas Bellinger struct scatterlist *sg; 488f6970ad3SNicholas Bellinger struct bio *bio; 489f6970ad3SNicholas Bellinger struct bio_list list; 4908a9ebe71SMike Christie struct se_device *dev = cmd->se_dev; 4918a9ebe71SMike Christie sector_t block_lba = target_to_linux_sector(dev, cmd->t_task_lba); 4928a9ebe71SMike Christie sector_t sectors = target_to_linux_sector(dev, 4938a9ebe71SMike Christie sbc_get_write_same_sectors(cmd)); 494f6970ad3SNicholas Bellinger 495afd73f1bSNicholas Bellinger if (cmd->prot_op) { 496afd73f1bSNicholas Bellinger pr_err("WRITE_SAME: Protection information with IBLOCK" 497afd73f1bSNicholas Bellinger " backends not supported\n"); 498afd73f1bSNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 499afd73f1bSNicholas Bellinger } 500ccd3f449SMike Christie 501ccd3f449SMike Christie if (!cmd->t_data_nents) 502ccd3f449SMike Christie return TCM_INVALID_CDB_FIELD; 503ccd3f449SMike Christie 504f6970ad3SNicholas Bellinger sg = &cmd->t_data_sg[0]; 505f6970ad3SNicholas Bellinger 506f6970ad3SNicholas Bellinger if (cmd->t_data_nents > 1 || 507f6970ad3SNicholas Bellinger sg->length != cmd->se_dev->dev_attrib.block_size) { 508f6970ad3SNicholas Bellinger pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u" 509f6970ad3SNicholas Bellinger " block_size: %u\n", cmd->t_data_nents, sg->length, 510f6970ad3SNicholas Bellinger cmd->se_dev->dev_attrib.block_size); 511f6970ad3SNicholas Bellinger return TCM_INVALID_CDB_FIELD; 512f6970ad3SNicholas Bellinger } 513f6970ad3SNicholas Bellinger 5142237498fSNicholas Bellinger if (bdev_write_zeroes_sectors(bdev)) { 5152237498fSNicholas Bellinger if (!iblock_execute_zero_out(bdev, cmd)) 5162237498fSNicholas Bellinger return 0; 5172237498fSNicholas Bellinger } 51807b63196SMike Christie 519f6970ad3SNicholas Bellinger ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL); 520f6970ad3SNicholas Bellinger if (!ibr) 521f6970ad3SNicholas Bellinger goto fail; 522f6970ad3SNicholas Bellinger cmd->priv = ibr; 523f6970ad3SNicholas Bellinger 524bc9e0e36SChaitanya Kulkarni bio = iblock_get_bio(cmd, block_lba, 1, REQ_OP_WRITE); 525f6970ad3SNicholas Bellinger if (!bio) 526f6970ad3SNicholas Bellinger goto fail_free_ibr; 527f6970ad3SNicholas Bellinger 528f6970ad3SNicholas Bellinger bio_list_init(&list); 529f6970ad3SNicholas Bellinger bio_list_add(&list, bio); 530f6970ad3SNicholas Bellinger 5315981c245SElena Reshetova refcount_set(&ibr->pending, 1); 532f6970ad3SNicholas Bellinger 533f6970ad3SNicholas Bellinger while (sectors) { 534f6970ad3SNicholas Bellinger while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset) 535f6970ad3SNicholas Bellinger != sg->length) { 536f6970ad3SNicholas Bellinger 537bc9e0e36SChaitanya Kulkarni bio = iblock_get_bio(cmd, block_lba, 1, REQ_OP_WRITE); 538f6970ad3SNicholas Bellinger if (!bio) 539f6970ad3SNicholas Bellinger goto fail_put_bios; 540f6970ad3SNicholas Bellinger 5415981c245SElena Reshetova refcount_inc(&ibr->pending); 542f6970ad3SNicholas Bellinger bio_list_add(&list, bio); 543f6970ad3SNicholas Bellinger } 544f6970ad3SNicholas Bellinger 545f6970ad3SNicholas Bellinger /* Always in 512 byte units for Linux/Block */ 54680b045b3SBart Van Assche block_lba += sg->length >> SECTOR_SHIFT; 5475676234fSRoman Bolshakov sectors -= sg->length >> SECTOR_SHIFT; 548f6970ad3SNicholas Bellinger } 549f6970ad3SNicholas Bellinger 5504e49ea4aSMike Christie iblock_submit_bios(&list); 551f6970ad3SNicholas Bellinger return 0; 552f6970ad3SNicholas Bellinger 553f6970ad3SNicholas Bellinger fail_put_bios: 554f6970ad3SNicholas Bellinger while ((bio = bio_list_pop(&list))) 555f6970ad3SNicholas Bellinger bio_put(bio); 556f6970ad3SNicholas Bellinger fail_free_ibr: 557f6970ad3SNicholas Bellinger kfree(ibr); 558f6970ad3SNicholas Bellinger fail: 559f6970ad3SNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 560f6970ad3SNicholas Bellinger } 561f6970ad3SNicholas Bellinger 562c66ac9dbSNicholas Bellinger enum { 56344bfd018SAndy Grover Opt_udev_path, Opt_readonly, Opt_force, Opt_err 564c66ac9dbSNicholas Bellinger }; 565c66ac9dbSNicholas Bellinger 566c66ac9dbSNicholas Bellinger static match_table_t tokens = { 567c66ac9dbSNicholas Bellinger {Opt_udev_path, "udev_path=%s"}, 56844bfd018SAndy Grover {Opt_readonly, "readonly=%d"}, 569c66ac9dbSNicholas Bellinger {Opt_force, "force=%d"}, 570c66ac9dbSNicholas Bellinger {Opt_err, NULL} 571c66ac9dbSNicholas Bellinger }; 572c66ac9dbSNicholas Bellinger 5730fd97ccfSChristoph Hellwig static ssize_t iblock_set_configfs_dev_params(struct se_device *dev, 574c66ac9dbSNicholas Bellinger const char *page, ssize_t count) 575c66ac9dbSNicholas Bellinger { 5760fd97ccfSChristoph Hellwig struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 5776d180253SJesper Juhl char *orig, *ptr, *arg_p, *opts; 578c66ac9dbSNicholas Bellinger substring_t args[MAX_OPT_ARGS]; 57921bca31cSRoland Dreier int ret = 0, token; 58044bfd018SAndy Grover unsigned long tmp_readonly; 581c66ac9dbSNicholas Bellinger 582c66ac9dbSNicholas Bellinger opts = kstrdup(page, GFP_KERNEL); 583c66ac9dbSNicholas Bellinger if (!opts) 584c66ac9dbSNicholas Bellinger return -ENOMEM; 585c66ac9dbSNicholas Bellinger 586c66ac9dbSNicholas Bellinger orig = opts; 587c66ac9dbSNicholas Bellinger 58890c161b6SSebastian Andrzej Siewior while ((ptr = strsep(&opts, ",\n")) != NULL) { 589c66ac9dbSNicholas Bellinger if (!*ptr) 590c66ac9dbSNicholas Bellinger continue; 591c66ac9dbSNicholas Bellinger 592c66ac9dbSNicholas Bellinger token = match_token(ptr, tokens, args); 593c66ac9dbSNicholas Bellinger switch (token) { 594c66ac9dbSNicholas Bellinger case Opt_udev_path: 595c66ac9dbSNicholas Bellinger if (ib_dev->ibd_bd) { 5966708bb27SAndy Grover pr_err("Unable to set udev_path= while" 597c66ac9dbSNicholas Bellinger " ib_dev->ibd_bd exists\n"); 598c66ac9dbSNicholas Bellinger ret = -EEXIST; 599c66ac9dbSNicholas Bellinger goto out; 600c66ac9dbSNicholas Bellinger } 601852b6ed1SNicholas Bellinger if (match_strlcpy(ib_dev->ibd_udev_path, &args[0], 602852b6ed1SNicholas Bellinger SE_UDEV_PATH_LEN) == 0) { 603852b6ed1SNicholas Bellinger ret = -EINVAL; 6046d180253SJesper Juhl break; 6056d180253SJesper Juhl } 6066708bb27SAndy Grover pr_debug("IBLOCK: Referencing UDEV path: %s\n", 607c66ac9dbSNicholas Bellinger ib_dev->ibd_udev_path); 608c66ac9dbSNicholas Bellinger ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH; 609c66ac9dbSNicholas Bellinger break; 61044bfd018SAndy Grover case Opt_readonly: 61144bfd018SAndy Grover arg_p = match_strdup(&args[0]); 61244bfd018SAndy Grover if (!arg_p) { 61344bfd018SAndy Grover ret = -ENOMEM; 61444bfd018SAndy Grover break; 61544bfd018SAndy Grover } 61657103d7fSJingoo Han ret = kstrtoul(arg_p, 0, &tmp_readonly); 61744bfd018SAndy Grover kfree(arg_p); 61844bfd018SAndy Grover if (ret < 0) { 61957103d7fSJingoo Han pr_err("kstrtoul() failed for" 62044bfd018SAndy Grover " readonly=\n"); 62144bfd018SAndy Grover goto out; 62244bfd018SAndy Grover } 62344bfd018SAndy Grover ib_dev->ibd_readonly = tmp_readonly; 62444bfd018SAndy Grover pr_debug("IBLOCK: readonly: %d\n", ib_dev->ibd_readonly); 62544bfd018SAndy Grover break; 626c66ac9dbSNicholas Bellinger case Opt_force: 627c66ac9dbSNicholas Bellinger break; 628c66ac9dbSNicholas Bellinger default: 629c66ac9dbSNicholas Bellinger break; 630c66ac9dbSNicholas Bellinger } 631c66ac9dbSNicholas Bellinger } 632c66ac9dbSNicholas Bellinger 633c66ac9dbSNicholas Bellinger out: 634c66ac9dbSNicholas Bellinger kfree(orig); 635c66ac9dbSNicholas Bellinger return (!ret) ? count : ret; 636c66ac9dbSNicholas Bellinger } 637c66ac9dbSNicholas Bellinger 6380fd97ccfSChristoph Hellwig static ssize_t iblock_show_configfs_dev_params(struct se_device *dev, char *b) 639c66ac9dbSNicholas Bellinger { 6400fd97ccfSChristoph Hellwig struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 6410fd97ccfSChristoph Hellwig struct block_device *bd = ib_dev->ibd_bd; 642c66ac9dbSNicholas Bellinger ssize_t bl = 0; 643c66ac9dbSNicholas Bellinger 644c66ac9dbSNicholas Bellinger if (bd) 6451b74ab77SChristoph Hellwig bl += sprintf(b + bl, "iBlock device: %pg", bd); 6460fd97ccfSChristoph Hellwig if (ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH) 64744bfd018SAndy Grover bl += sprintf(b + bl, " UDEV PATH: %s", 6480fd97ccfSChristoph Hellwig ib_dev->ibd_udev_path); 6490fd97ccfSChristoph Hellwig bl += sprintf(b + bl, " readonly: %d\n", ib_dev->ibd_readonly); 650c66ac9dbSNicholas Bellinger 651c66ac9dbSNicholas Bellinger bl += sprintf(b + bl, " "); 652c66ac9dbSNicholas Bellinger if (bd) { 653c66ac9dbSNicholas Bellinger bl += sprintf(b + bl, "Major: %d Minor: %d %s\n", 65457ba1059SChristoph Hellwig MAJOR(bd->bd_dev), MINOR(bd->bd_dev), 65557ba1059SChristoph Hellwig "CLAIMED: IBLOCK"); 656c66ac9dbSNicholas Bellinger } else { 65721bca31cSRoland Dreier bl += sprintf(b + bl, "Major: 0 Minor: 0\n"); 658c66ac9dbSNicholas Bellinger } 659c66ac9dbSNicholas Bellinger 660c66ac9dbSNicholas Bellinger return bl; 661c66ac9dbSNicholas Bellinger } 662c66ac9dbSNicholas Bellinger 663ecebbf6cSNicholas Bellinger static int 664fed564f6SGreg Edwards iblock_alloc_bip(struct se_cmd *cmd, struct bio *bio, 665fed564f6SGreg Edwards struct sg_mapping_iter *miter) 666ecebbf6cSNicholas Bellinger { 667ecebbf6cSNicholas Bellinger struct se_device *dev = cmd->se_dev; 668ecebbf6cSNicholas Bellinger struct blk_integrity *bi; 669ecebbf6cSNicholas Bellinger struct bio_integrity_payload *bip; 670ecebbf6cSNicholas Bellinger struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 671fed564f6SGreg Edwards int rc; 672fed564f6SGreg Edwards size_t resid, len; 673ecebbf6cSNicholas Bellinger 674ecebbf6cSNicholas Bellinger bi = bdev_get_integrity(ib_dev->ibd_bd); 675ecebbf6cSNicholas Bellinger if (!bi) { 676ecebbf6cSNicholas Bellinger pr_err("Unable to locate bio_integrity\n"); 677ecebbf6cSNicholas Bellinger return -ENODEV; 678ecebbf6cSNicholas Bellinger } 679ecebbf6cSNicholas Bellinger 6805f7136dbSMatthew Wilcox (Oracle) bip = bio_integrity_alloc(bio, GFP_NOIO, bio_max_segs(cmd->t_prot_nents)); 68106c1e390SKeith Busch if (IS_ERR(bip)) { 682ecebbf6cSNicholas Bellinger pr_err("Unable to allocate bio_integrity_payload\n"); 68306c1e390SKeith Busch return PTR_ERR(bip); 684ecebbf6cSNicholas Bellinger } 685ecebbf6cSNicholas Bellinger 686fed564f6SGreg Edwards bip->bip_iter.bi_size = bio_integrity_bytes(bi, bio_sectors(bio)); 687e4dc9a4cSIsrael Rukshin /* virtual start sector must be in integrity interval units */ 688e4dc9a4cSIsrael Rukshin bip_set_seed(bip, bio->bi_iter.bi_sector >> 689e4dc9a4cSIsrael Rukshin (bi->interval_exp - SECTOR_SHIFT)); 690ecebbf6cSNicholas Bellinger 6914e13c5d0SLinus Torvalds pr_debug("IBLOCK BIP Size: %u Sector: %llu\n", bip->bip_iter.bi_size, 6924e13c5d0SLinus Torvalds (unsigned long long)bip->bip_iter.bi_sector); 693ecebbf6cSNicholas Bellinger 694fed564f6SGreg Edwards resid = bip->bip_iter.bi_size; 695fed564f6SGreg Edwards while (resid > 0 && sg_miter_next(miter)) { 696ecebbf6cSNicholas Bellinger 697fed564f6SGreg Edwards len = min_t(size_t, miter->length, resid); 698fed564f6SGreg Edwards rc = bio_integrity_add_page(bio, miter->page, len, 699fed564f6SGreg Edwards offset_in_page(miter->addr)); 700fed564f6SGreg Edwards if (rc != len) { 701ecebbf6cSNicholas Bellinger pr_err("bio_integrity_add_page() failed; %d\n", rc); 702fed564f6SGreg Edwards sg_miter_stop(miter); 703ecebbf6cSNicholas Bellinger return -ENOMEM; 704ecebbf6cSNicholas Bellinger } 705ecebbf6cSNicholas Bellinger 706fed564f6SGreg Edwards pr_debug("Added bio integrity page: %p length: %zu offset: %lu\n", 707fed564f6SGreg Edwards miter->page, len, offset_in_page(miter->addr)); 708fed564f6SGreg Edwards 709fed564f6SGreg Edwards resid -= len; 710fed564f6SGreg Edwards if (len < miter->length) 711fed564f6SGreg Edwards miter->consumed -= miter->length - len; 712ecebbf6cSNicholas Bellinger } 713fed564f6SGreg Edwards sg_miter_stop(miter); 714ecebbf6cSNicholas Bellinger 715ecebbf6cSNicholas Bellinger return 0; 716ecebbf6cSNicholas Bellinger } 717ecebbf6cSNicholas Bellinger 718de103c93SChristoph Hellwig static sense_reason_t 719a82a9538SNicholas Bellinger iblock_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents, 720a82a9538SNicholas Bellinger enum dma_data_direction data_direction) 721c66ac9dbSNicholas Bellinger { 7225951146dSAndy Grover struct se_device *dev = cmd->se_dev; 7238a9ebe71SMike Christie sector_t block_lba = target_to_linux_sector(dev, cmd->t_task_lba); 7245787cacdSChristoph Hellwig struct iblock_req *ibr; 725fed564f6SGreg Edwards struct bio *bio; 726dbbf3e94SChristoph Hellwig struct bio_list list; 727c66ac9dbSNicholas Bellinger struct scatterlist *sg; 7285787cacdSChristoph Hellwig u32 sg_num = sgl_nents; 72979fe9d7dSBart Van Assche blk_opf_t opf; 730d5b4a21bSChristoph Hellwig unsigned bio_cnt; 731bc9e0e36SChaitanya Kulkarni int i, rc; 732fed564f6SGreg Edwards struct sg_mapping_iter prot_miter; 733bc9e0e36SChaitanya Kulkarni unsigned int miter_dir; 734dbbf3e94SChristoph Hellwig 7355787cacdSChristoph Hellwig if (data_direction == DMA_TO_DEVICE) { 736d0c8b259SNicholas Bellinger struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 737dbbf3e94SChristoph Hellwig /* 73870fd7614SChristoph Hellwig * Force writethrough using REQ_FUA if a volatile write cache 739d0c8b259SNicholas Bellinger * is not enabled, or if initiator set the Force Unit Access bit. 740dbbf3e94SChristoph Hellwig */ 741bc9e0e36SChaitanya Kulkarni opf = REQ_OP_WRITE; 742bc9e0e36SChaitanya Kulkarni miter_dir = SG_MITER_TO_SG; 743a557e82eSChristoph Hellwig if (bdev_fua(ib_dev->ibd_bd)) { 744d0c8b259SNicholas Bellinger if (cmd->se_cmd_flags & SCF_FUA) 745bc9e0e36SChaitanya Kulkarni opf |= REQ_FUA; 74608e688fdSChristoph Hellwig else if (!bdev_write_cache(ib_dev->ibd_bd)) 747bc9e0e36SChaitanya Kulkarni opf |= REQ_FUA; 748d0c8b259SNicholas Bellinger } 749dbbf3e94SChristoph Hellwig } else { 750bc9e0e36SChaitanya Kulkarni opf = REQ_OP_READ; 751bc9e0e36SChaitanya Kulkarni miter_dir = SG_MITER_FROM_SG; 752dbbf3e94SChristoph Hellwig } 753dbbf3e94SChristoph Hellwig 7545787cacdSChristoph Hellwig ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL); 7555787cacdSChristoph Hellwig if (!ibr) 7565787cacdSChristoph Hellwig goto fail; 7575787cacdSChristoph Hellwig cmd->priv = ibr; 7585787cacdSChristoph Hellwig 759e0de4457SPaolo Bonzini if (!sgl_nents) { 7605981c245SElena Reshetova refcount_set(&ibr->pending, 1); 761e0de4457SPaolo Bonzini iblock_complete_cmd(cmd); 762e0de4457SPaolo Bonzini return 0; 763e0de4457SPaolo Bonzini } 764e0de4457SPaolo Bonzini 765bc9e0e36SChaitanya Kulkarni bio = iblock_get_bio(cmd, block_lba, sgl_nents, opf); 7665787cacdSChristoph Hellwig if (!bio) 7675787cacdSChristoph Hellwig goto fail_free_ibr; 768c66ac9dbSNicholas Bellinger 769dbbf3e94SChristoph Hellwig bio_list_init(&list); 770dbbf3e94SChristoph Hellwig bio_list_add(&list, bio); 7715787cacdSChristoph Hellwig 7725981c245SElena Reshetova refcount_set(&ibr->pending, 2); 773d5b4a21bSChristoph Hellwig bio_cnt = 1; 774dbbf3e94SChristoph Hellwig 775fed564f6SGreg Edwards if (cmd->prot_type && dev->dev_attrib.pi_prot_type) 776fed564f6SGreg Edwards sg_miter_start(&prot_miter, cmd->t_prot_sg, cmd->t_prot_nents, 777bc9e0e36SChaitanya Kulkarni miter_dir); 778fed564f6SGreg Edwards 7795787cacdSChristoph Hellwig for_each_sg(sgl, sg, sgl_nents, i) { 780dbbf3e94SChristoph Hellwig /* 781dbbf3e94SChristoph Hellwig * XXX: if the length the device accepts is shorter than the 782dbbf3e94SChristoph Hellwig * length of the S/G list entry this will cause and 783dbbf3e94SChristoph Hellwig * endless loop. Better hope no driver uses huge pages. 784dbbf3e94SChristoph Hellwig */ 785dbbf3e94SChristoph Hellwig while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset) 786dbbf3e94SChristoph Hellwig != sg->length) { 787fed564f6SGreg Edwards if (cmd->prot_type && dev->dev_attrib.pi_prot_type) { 788fed564f6SGreg Edwards rc = iblock_alloc_bip(cmd, bio, &prot_miter); 789fed564f6SGreg Edwards if (rc) 790fed564f6SGreg Edwards goto fail_put_bios; 791fed564f6SGreg Edwards } 792fed564f6SGreg Edwards 793d5b4a21bSChristoph Hellwig if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) { 7944e49ea4aSMike Christie iblock_submit_bios(&list); 795d5b4a21bSChristoph Hellwig bio_cnt = 0; 796d5b4a21bSChristoph Hellwig } 797d5b4a21bSChristoph Hellwig 798bc9e0e36SChaitanya Kulkarni bio = iblock_get_bio(cmd, block_lba, sg_num, opf); 7996708bb27SAndy Grover if (!bio) 8005787cacdSChristoph Hellwig goto fail_put_bios; 8015787cacdSChristoph Hellwig 8025981c245SElena Reshetova refcount_inc(&ibr->pending); 803dbbf3e94SChristoph Hellwig bio_list_add(&list, bio); 804d5b4a21bSChristoph Hellwig bio_cnt++; 805c66ac9dbSNicholas Bellinger } 806dbbf3e94SChristoph Hellwig 807c66ac9dbSNicholas Bellinger /* Always in 512 byte units for Linux/Block */ 80880b045b3SBart Van Assche block_lba += sg->length >> SECTOR_SHIFT; 809c66ac9dbSNicholas Bellinger sg_num--; 810c66ac9dbSNicholas Bellinger } 811c66ac9dbSNicholas Bellinger 8126f16ec43SNicholas Bellinger if (cmd->prot_type && dev->dev_attrib.pi_prot_type) { 813fed564f6SGreg Edwards rc = iblock_alloc_bip(cmd, bio, &prot_miter); 814ecebbf6cSNicholas Bellinger if (rc) 815ecebbf6cSNicholas Bellinger goto fail_put_bios; 816ecebbf6cSNicholas Bellinger } 817ecebbf6cSNicholas Bellinger 8184e49ea4aSMike Christie iblock_submit_bios(&list); 8195787cacdSChristoph Hellwig iblock_complete_cmd(cmd); 82003e98c9eSNicholas Bellinger return 0; 821dbbf3e94SChristoph Hellwig 8225787cacdSChristoph Hellwig fail_put_bios: 823dbbf3e94SChristoph Hellwig while ((bio = bio_list_pop(&list))) 824c66ac9dbSNicholas Bellinger bio_put(bio); 8255787cacdSChristoph Hellwig fail_free_ibr: 8265787cacdSChristoph Hellwig kfree(ibr); 8275787cacdSChristoph Hellwig fail: 828de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 829c66ac9dbSNicholas Bellinger } 830c66ac9dbSNicholas Bellinger 8317f7caf6aSAndy Grover static sector_t iblock_get_alignment_offset_lbas(struct se_device *dev) 8327f7caf6aSAndy Grover { 8337f7caf6aSAndy Grover struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 8347f7caf6aSAndy Grover struct block_device *bd = ib_dev->ibd_bd; 8357f7caf6aSAndy Grover int ret; 8367f7caf6aSAndy Grover 8377f7caf6aSAndy Grover ret = bdev_alignment_offset(bd); 8387f7caf6aSAndy Grover if (ret == -1) 8397f7caf6aSAndy Grover return 0; 8407f7caf6aSAndy Grover 8417f7caf6aSAndy Grover /* convert offset-bytes to offset-lbas */ 8427f7caf6aSAndy Grover return ret / bdev_logical_block_size(bd); 8437f7caf6aSAndy Grover } 8447f7caf6aSAndy Grover 8457f7caf6aSAndy Grover static unsigned int iblock_get_lbppbe(struct se_device *dev) 8467f7caf6aSAndy Grover { 8477f7caf6aSAndy Grover struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 8487f7caf6aSAndy Grover struct block_device *bd = ib_dev->ibd_bd; 849c151eddbSChaitanya Kulkarni unsigned int logs_per_phys = 850a2c6c6a3SChaitanya Kulkarni bdev_physical_block_size(bd) / bdev_logical_block_size(bd); 8517f7caf6aSAndy Grover 8527f7caf6aSAndy Grover return ilog2(logs_per_phys); 8537f7caf6aSAndy Grover } 8547f7caf6aSAndy Grover 8557f7caf6aSAndy Grover static unsigned int iblock_get_io_min(struct se_device *dev) 8567f7caf6aSAndy Grover { 8577f7caf6aSAndy Grover struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 8587f7caf6aSAndy Grover struct block_device *bd = ib_dev->ibd_bd; 8597f7caf6aSAndy Grover 8607f7caf6aSAndy Grover return bdev_io_min(bd); 8617f7caf6aSAndy Grover } 8627f7caf6aSAndy Grover 8637f7caf6aSAndy Grover static unsigned int iblock_get_io_opt(struct se_device *dev) 8647f7caf6aSAndy Grover { 8657f7caf6aSAndy Grover struct iblock_dev *ib_dev = IBLOCK_DEV(dev); 8667f7caf6aSAndy Grover struct block_device *bd = ib_dev->ibd_bd; 8677f7caf6aSAndy Grover 8687f7caf6aSAndy Grover return bdev_io_opt(bd); 8697f7caf6aSAndy Grover } 8707f7caf6aSAndy Grover 8719e999a6cSChristoph Hellwig static struct sbc_ops iblock_sbc_ops = { 8720c2ad7d1SChristoph Hellwig .execute_rw = iblock_execute_rw, 873ad67f0d9SChristoph Hellwig .execute_sync_cache = iblock_execute_sync_cache, 8746f974e8cSChristoph Hellwig .execute_write_same = iblock_execute_write_same, 87514150a6bSChristoph Hellwig .execute_unmap = iblock_execute_unmap, 8760c2ad7d1SChristoph Hellwig }; 8770c2ad7d1SChristoph Hellwig 878de103c93SChristoph Hellwig static sense_reason_t 879de103c93SChristoph Hellwig iblock_parse_cdb(struct se_cmd *cmd) 8800c2ad7d1SChristoph Hellwig { 8819e999a6cSChristoph Hellwig return sbc_parse_cdb(cmd, &iblock_sbc_ops); 8820c2ad7d1SChristoph Hellwig } 8830c2ad7d1SChristoph Hellwig 884452e2010SRashika Kheria static bool iblock_get_write_cache(struct se_device *dev) 885d0c8b259SNicholas Bellinger { 88608e688fdSChristoph Hellwig return bdev_write_cache(IBLOCK_DEV(dev)->ibd_bd); 887d0c8b259SNicholas Bellinger } 888d0c8b259SNicholas Bellinger 8890a06d430SChristoph Hellwig static const struct target_backend_ops iblock_ops = { 890c66ac9dbSNicholas Bellinger .name = "iblock", 8910fd97ccfSChristoph Hellwig .inquiry_prod = "IBLOCK", 8920fd97ccfSChristoph Hellwig .inquiry_rev = IBLOCK_VERSION, 893c66ac9dbSNicholas Bellinger .owner = THIS_MODULE, 894c66ac9dbSNicholas Bellinger .attach_hba = iblock_attach_hba, 895c66ac9dbSNicholas Bellinger .detach_hba = iblock_detach_hba, 8960fd97ccfSChristoph Hellwig .alloc_device = iblock_alloc_device, 8970fd97ccfSChristoph Hellwig .configure_device = iblock_configure_device, 89892634706SMike Christie .destroy_device = iblock_destroy_device, 899c66ac9dbSNicholas Bellinger .free_device = iblock_free_device, 900d7c382c5SMike Christie .configure_unmap = iblock_configure_unmap, 901415ccd98SMike Christie .plug_device = iblock_plug_device, 902415ccd98SMike Christie .unplug_device = iblock_unplug_device, 9030c2ad7d1SChristoph Hellwig .parse_cdb = iblock_parse_cdb, 904c66ac9dbSNicholas Bellinger .set_configfs_dev_params = iblock_set_configfs_dev_params, 905c66ac9dbSNicholas Bellinger .show_configfs_dev_params = iblock_show_configfs_dev_params, 9066f23ac8aSChristoph Hellwig .get_device_type = sbc_get_device_type, 907c66ac9dbSNicholas Bellinger .get_blocks = iblock_get_blocks, 9087f7caf6aSAndy Grover .get_alignment_offset_lbas = iblock_get_alignment_offset_lbas, 9097f7caf6aSAndy Grover .get_lbppbe = iblock_get_lbppbe, 9107f7caf6aSAndy Grover .get_io_min = iblock_get_io_min, 9117f7caf6aSAndy Grover .get_io_opt = iblock_get_io_opt, 912d0c8b259SNicholas Bellinger .get_write_cache = iblock_get_write_cache, 9135873c4d1SChristoph Hellwig .tb_dev_attrib_attrs = sbc_attrib_attrs, 914c66ac9dbSNicholas Bellinger }; 915c66ac9dbSNicholas Bellinger 916c66ac9dbSNicholas Bellinger static int __init iblock_module_init(void) 917c66ac9dbSNicholas Bellinger { 9180a06d430SChristoph Hellwig return transport_backend_register(&iblock_ops); 919c66ac9dbSNicholas Bellinger } 920c66ac9dbSNicholas Bellinger 92163b91d5aSAsias He static void __exit iblock_module_exit(void) 922c66ac9dbSNicholas Bellinger { 9230a06d430SChristoph Hellwig target_backend_unregister(&iblock_ops); 924c66ac9dbSNicholas Bellinger } 925c66ac9dbSNicholas Bellinger 926c66ac9dbSNicholas Bellinger MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin"); 927c66ac9dbSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org"); 928c66ac9dbSNicholas Bellinger MODULE_LICENSE("GPL"); 929c66ac9dbSNicholas Bellinger 930c66ac9dbSNicholas Bellinger module_init(iblock_module_init); 931c66ac9dbSNicholas Bellinger module_exit(iblock_module_exit); 932