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>
26394f8118SMike Christie #include <linux/pr.h>
27ba929992SBart Van Assche #include <scsi/scsi_proto.h>
28394f8118SMike Christie #include <scsi/scsi_common.h>
2914150a6bSChristoph Hellwig #include <asm/unaligned.h>
30c66ac9dbSNicholas Bellinger 
31c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
32c4795fb2SChristoph Hellwig #include <target/target_core_backend.h>
33c66ac9dbSNicholas Bellinger 
34c66ac9dbSNicholas Bellinger #include "target_core_iblock.h"
35394f8118SMike Christie #include "target_core_pr.h"
36c66ac9dbSNicholas Bellinger 
37d5b4a21bSChristoph Hellwig #define IBLOCK_MAX_BIO_PER_TASK	 32	/* max # of bios to submit at a time */
38d5b4a21bSChristoph Hellwig #define IBLOCK_BIO_POOL_SIZE	128
39d5b4a21bSChristoph Hellwig 
IBLOCK_DEV(struct se_device * dev)400fd97ccfSChristoph Hellwig static inline struct iblock_dev *IBLOCK_DEV(struct se_device *dev)
410fd97ccfSChristoph Hellwig {
420fd97ccfSChristoph Hellwig 	return container_of(dev, struct iblock_dev, dev);
430fd97ccfSChristoph Hellwig }
440fd97ccfSChristoph Hellwig 
450fd97ccfSChristoph Hellwig 
iblock_attach_hba(struct se_hba * hba,u32 host_id)46c66ac9dbSNicholas Bellinger static int iblock_attach_hba(struct se_hba *hba, u32 host_id)
47c66ac9dbSNicholas Bellinger {
486708bb27SAndy Grover 	pr_debug("CORE_HBA[%d] - TCM iBlock HBA Driver %s on"
49c66ac9dbSNicholas Bellinger 		" Generic Target Core Stack %s\n", hba->hba_id,
50ce8dd25dSChristoph Hellwig 		IBLOCK_VERSION, TARGET_CORE_VERSION);
51c66ac9dbSNicholas Bellinger 	return 0;
52c66ac9dbSNicholas Bellinger }
53c66ac9dbSNicholas Bellinger 
iblock_detach_hba(struct se_hba * hba)54c66ac9dbSNicholas Bellinger static void iblock_detach_hba(struct se_hba *hba)
55c66ac9dbSNicholas Bellinger {
56c66ac9dbSNicholas Bellinger }
57c66ac9dbSNicholas Bellinger 
iblock_alloc_device(struct se_hba * hba,const char * name)580fd97ccfSChristoph Hellwig static struct se_device *iblock_alloc_device(struct se_hba *hba, const char *name)
59c66ac9dbSNicholas Bellinger {
60c66ac9dbSNicholas Bellinger 	struct iblock_dev *ib_dev = NULL;
61c66ac9dbSNicholas Bellinger 
62c66ac9dbSNicholas Bellinger 	ib_dev = kzalloc(sizeof(struct iblock_dev), GFP_KERNEL);
636708bb27SAndy Grover 	if (!ib_dev) {
646708bb27SAndy Grover 		pr_err("Unable to allocate struct iblock_dev\n");
65c66ac9dbSNicholas Bellinger 		return NULL;
66c66ac9dbSNicholas Bellinger 	}
67c66ac9dbSNicholas Bellinger 
68415ccd98SMike Christie 	ib_dev->ibd_plug = kcalloc(nr_cpu_ids, sizeof(*ib_dev->ibd_plug),
69415ccd98SMike Christie 				   GFP_KERNEL);
70415ccd98SMike Christie 	if (!ib_dev->ibd_plug)
71415ccd98SMike Christie 		goto free_dev;
72415ccd98SMike Christie 
736708bb27SAndy Grover 	pr_debug( "IBLOCK: Allocated ib_dev for %s\n", name);
74c66ac9dbSNicholas Bellinger 
750fd97ccfSChristoph Hellwig 	return &ib_dev->dev;
76415ccd98SMike Christie 
77415ccd98SMike Christie free_dev:
78415ccd98SMike Christie 	kfree(ib_dev);
79415ccd98SMike Christie 	return NULL;
80c66ac9dbSNicholas Bellinger }
81c66ac9dbSNicholas Bellinger 
iblock_configure_unmap(struct se_device * dev)82d7c382c5SMike Christie static bool iblock_configure_unmap(struct se_device *dev)
83d7c382c5SMike Christie {
84d7c382c5SMike Christie 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
85d7c382c5SMike Christie 
86d7c382c5SMike Christie 	return target_configure_unmap_from_queue(&dev->dev_attrib,
87d7c382c5SMike Christie 						 ib_dev->ibd_bd);
88d7c382c5SMike Christie }
89d7c382c5SMike Christie 
iblock_configure_device(struct se_device * dev)900fd97ccfSChristoph Hellwig static int iblock_configure_device(struct se_device *dev)
91c66ac9dbSNicholas Bellinger {
920fd97ccfSChristoph Hellwig 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
93c66ac9dbSNicholas Bellinger 	struct request_queue *q;
940fd97ccfSChristoph Hellwig 	struct block_device *bd = NULL;
95ecebbf6cSNicholas Bellinger 	struct blk_integrity *bi;
9605bdb996SChristoph Hellwig 	blk_mode_t mode = BLK_OPEN_READ;
972237498fSNicholas Bellinger 	unsigned int max_write_zeroes_sectors;
988f13142aSColin Ian King 	int ret;
99c66ac9dbSNicholas Bellinger 
1000fd97ccfSChristoph Hellwig 	if (!(ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)) {
1010fd97ccfSChristoph Hellwig 		pr_err("Missing udev_path= parameters for IBLOCK\n");
1020fd97ccfSChristoph Hellwig 		return -EINVAL;
103c66ac9dbSNicholas Bellinger 	}
104d5b4a21bSChristoph Hellwig 
105a47a28b7SKent Overstreet 	ret = bioset_init(&ib_dev->ibd_bio_set, IBLOCK_BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
106a47a28b7SKent Overstreet 	if (ret) {
1070fd97ccfSChristoph Hellwig 		pr_err("IBLOCK: Unable to create bioset\n");
1080fd97ccfSChristoph Hellwig 		goto out;
109c66ac9dbSNicholas Bellinger 	}
1100fd97ccfSChristoph Hellwig 
1116708bb27SAndy Grover 	pr_debug( "IBLOCK: Claiming struct block_device: %s\n",
112c66ac9dbSNicholas Bellinger 			ib_dev->ibd_udev_path);
113c66ac9dbSNicholas Bellinger 
11444bfd018SAndy Grover 	if (!ib_dev->ibd_readonly)
11505bdb996SChristoph Hellwig 		mode |= BLK_OPEN_WRITE;
116eeeb9522SNicholas Bellinger 	else
117eeeb9522SNicholas Bellinger 		dev->dev_flags |= DF_READ_ONLY;
11844bfd018SAndy Grover 
1190718afd4SChristoph Hellwig 	bd = blkdev_get_by_path(ib_dev->ibd_udev_path, mode, ib_dev, NULL);
120613640e4SNicholas Bellinger 	if (IS_ERR(bd)) {
121613640e4SNicholas Bellinger 		ret = PTR_ERR(bd);
1220fd97ccfSChristoph Hellwig 		goto out_free_bioset;
123613640e4SNicholas Bellinger 	}
124c66ac9dbSNicholas Bellinger 	ib_dev->ibd_bd = bd;
125c66ac9dbSNicholas Bellinger 
1260fd97ccfSChristoph Hellwig 	q = bdev_get_queue(bd);
1270fd97ccfSChristoph Hellwig 
1280fd97ccfSChristoph Hellwig 	dev->dev_attrib.hw_block_size = bdev_logical_block_size(bd);
1299375031eSAnastasia Kovaleva 	dev->dev_attrib.hw_max_sectors = mult_frac(queue_max_hw_sectors(q),
1309375031eSAnastasia Kovaleva 			SECTOR_SIZE,
1319375031eSAnastasia Kovaleva 			dev->dev_attrib.hw_block_size);
1320fd97ccfSChristoph Hellwig 	dev->dev_attrib.hw_queue_depth = q->nr_requests;
133c66ac9dbSNicholas Bellinger 
134f6970ad3SNicholas Bellinger 	/*
135f6970ad3SNicholas Bellinger 	 * Enable write same emulation for IBLOCK and use 0xFFFF as
136f6970ad3SNicholas Bellinger 	 * the smaller WRITE_SAME(10) only has a two-byte block count.
137f6970ad3SNicholas Bellinger 	 */
1382237498fSNicholas Bellinger 	max_write_zeroes_sectors = bdev_write_zeroes_sectors(bd);
1392237498fSNicholas Bellinger 	if (max_write_zeroes_sectors)
1402237498fSNicholas Bellinger 		dev->dev_attrib.max_write_same_len = max_write_zeroes_sectors;
1412237498fSNicholas Bellinger 	else
142f6970ad3SNicholas Bellinger 		dev->dev_attrib.max_write_same_len = 0xFFFF;
143c66ac9dbSNicholas Bellinger 
14410f0d2a5SChristoph Hellwig 	if (bdev_nonrot(bd))
1450fd97ccfSChristoph Hellwig 		dev->dev_attrib.is_nonrot = 1;
146d0c8b259SNicholas Bellinger 
147ecebbf6cSNicholas Bellinger 	bi = bdev_get_integrity(bd);
148ecebbf6cSNicholas Bellinger 	if (bi) {
149a47a28b7SKent Overstreet 		struct bio_set *bs = &ib_dev->ibd_bio_set;
150ecebbf6cSNicholas Bellinger 
1510f8087ecSMartin K. Petersen 		if (!strcmp(bi->profile->name, "T10-DIF-TYPE3-IP") ||
1520f8087ecSMartin K. Petersen 		    !strcmp(bi->profile->name, "T10-DIF-TYPE1-IP")) {
153ecebbf6cSNicholas Bellinger 			pr_err("IBLOCK export of blk_integrity: %s not"
1540f8087ecSMartin K. Petersen 			       " supported\n", bi->profile->name);
155ecebbf6cSNicholas Bellinger 			ret = -ENOSYS;
156ecebbf6cSNicholas Bellinger 			goto out_blkdev_put;
157ecebbf6cSNicholas Bellinger 		}
158ecebbf6cSNicholas Bellinger 
1590f8087ecSMartin K. Petersen 		if (!strcmp(bi->profile->name, "T10-DIF-TYPE3-CRC")) {
160ecebbf6cSNicholas Bellinger 			dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE3_PROT;
1610f8087ecSMartin K. Petersen 		} else if (!strcmp(bi->profile->name, "T10-DIF-TYPE1-CRC")) {
162ecebbf6cSNicholas Bellinger 			dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE1_PROT;
163ecebbf6cSNicholas Bellinger 		}
164ecebbf6cSNicholas Bellinger 
165ecebbf6cSNicholas Bellinger 		if (dev->dev_attrib.pi_prot_type) {
166ecebbf6cSNicholas Bellinger 			if (bioset_integrity_create(bs, IBLOCK_BIO_POOL_SIZE) < 0) {
167ecebbf6cSNicholas Bellinger 				pr_err("Unable to allocate bioset for PI\n");
168ecebbf6cSNicholas Bellinger 				ret = -ENOMEM;
169ecebbf6cSNicholas Bellinger 				goto out_blkdev_put;
170ecebbf6cSNicholas Bellinger 			}
171ecebbf6cSNicholas Bellinger 			pr_debug("IBLOCK setup BIP bs->bio_integrity_pool: %p\n",
172f4f8154aSKent Overstreet 				 &bs->bio_integrity_pool);
173ecebbf6cSNicholas Bellinger 		}
174ecebbf6cSNicholas Bellinger 		dev->dev_attrib.hw_pi_prot_type = dev->dev_attrib.pi_prot_type;
175ecebbf6cSNicholas Bellinger 	}
176ecebbf6cSNicholas Bellinger 
1770fd97ccfSChristoph Hellwig 	return 0;
178e22a7f07SRoland Dreier 
179ecebbf6cSNicholas Bellinger out_blkdev_put:
1802736e8eeSChristoph Hellwig 	blkdev_put(ib_dev->ibd_bd, ib_dev);
1810fd97ccfSChristoph Hellwig out_free_bioset:
182a47a28b7SKent Overstreet 	bioset_exit(&ib_dev->ibd_bio_set);
1830fd97ccfSChristoph Hellwig out:
1840fd97ccfSChristoph Hellwig 	return ret;
185c66ac9dbSNicholas Bellinger }
186c66ac9dbSNicholas Bellinger 
iblock_dev_call_rcu(struct rcu_head * p)1874cc987eaSNicholas Bellinger static void iblock_dev_call_rcu(struct rcu_head *p)
1884cc987eaSNicholas Bellinger {
1894cc987eaSNicholas Bellinger 	struct se_device *dev = container_of(p, struct se_device, rcu_head);
1904cc987eaSNicholas Bellinger 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
1914cc987eaSNicholas Bellinger 
192415ccd98SMike Christie 	kfree(ib_dev->ibd_plug);
1934cc987eaSNicholas Bellinger 	kfree(ib_dev);
1944cc987eaSNicholas Bellinger }
1954cc987eaSNicholas Bellinger 
iblock_free_device(struct se_device * dev)1960fd97ccfSChristoph Hellwig static void iblock_free_device(struct se_device *dev)
197c66ac9dbSNicholas Bellinger {
19892634706SMike Christie 	call_rcu(&dev->rcu_head, iblock_dev_call_rcu);
19992634706SMike Christie }
20092634706SMike Christie 
iblock_destroy_device(struct se_device * dev)20192634706SMike Christie static void iblock_destroy_device(struct se_device *dev)
20292634706SMike Christie {
2030fd97ccfSChristoph Hellwig 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
204c66ac9dbSNicholas Bellinger 
205bc665524SNicholas Bellinger 	if (ib_dev->ibd_bd != NULL)
2062736e8eeSChristoph Hellwig 		blkdev_put(ib_dev->ibd_bd, ib_dev);
207a47a28b7SKent Overstreet 	bioset_exit(&ib_dev->ibd_bio_set);
208c66ac9dbSNicholas Bellinger }
209c66ac9dbSNicholas Bellinger 
iblock_plug_device(struct se_device * se_dev)210415ccd98SMike Christie static struct se_dev_plug *iblock_plug_device(struct se_device *se_dev)
211415ccd98SMike Christie {
212415ccd98SMike Christie 	struct iblock_dev *ib_dev = IBLOCK_DEV(se_dev);
213415ccd98SMike Christie 	struct iblock_dev_plug *ib_dev_plug;
214415ccd98SMike Christie 
215415ccd98SMike Christie 	/*
2165aaeca25SMike Christie 	 * Each se_device has a per cpu work this can be run from. We
217415ccd98SMike Christie 	 * shouldn't have multiple threads on the same cpu calling this
218415ccd98SMike Christie 	 * at the same time.
219415ccd98SMike Christie 	 */
2205aaeca25SMike Christie 	ib_dev_plug = &ib_dev->ibd_plug[raw_smp_processor_id()];
221415ccd98SMike Christie 	if (test_and_set_bit(IBD_PLUGF_PLUGGED, &ib_dev_plug->flags))
222415ccd98SMike Christie 		return NULL;
223415ccd98SMike Christie 
224415ccd98SMike Christie 	blk_start_plug(&ib_dev_plug->blk_plug);
225415ccd98SMike Christie 	return &ib_dev_plug->se_plug;
226415ccd98SMike Christie }
227415ccd98SMike Christie 
iblock_unplug_device(struct se_dev_plug * se_plug)228415ccd98SMike Christie static void iblock_unplug_device(struct se_dev_plug *se_plug)
229415ccd98SMike Christie {
230415ccd98SMike Christie 	struct iblock_dev_plug *ib_dev_plug = container_of(se_plug,
231415ccd98SMike Christie 					struct iblock_dev_plug, se_plug);
232415ccd98SMike Christie 
233415ccd98SMike Christie 	blk_finish_plug(&ib_dev_plug->blk_plug);
234415ccd98SMike Christie 	clear_bit(IBD_PLUGF_PLUGGED, &ib_dev_plug->flags);
235415ccd98SMike Christie }
236415ccd98SMike Christie 
iblock_get_blocks(struct se_device * dev)237f915f58eSChristoph Hellwig static sector_t iblock_get_blocks(struct se_device *dev)
238c66ac9dbSNicholas Bellinger {
239f915f58eSChristoph Hellwig 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
240f915f58eSChristoph Hellwig 	u32 block_size = bdev_logical_block_size(ib_dev->ibd_bd);
24164f0f426SChristoph Hellwig 	unsigned long long blocks_long =
242f915f58eSChristoph Hellwig 		div_u64(bdev_nr_bytes(ib_dev->ibd_bd), block_size) - 1;
243c66ac9dbSNicholas Bellinger 
2440fd97ccfSChristoph Hellwig 	if (block_size == dev->dev_attrib.block_size)
245c66ac9dbSNicholas Bellinger 		return blocks_long;
246c66ac9dbSNicholas Bellinger 
247c66ac9dbSNicholas Bellinger 	switch (block_size) {
248c66ac9dbSNicholas Bellinger 	case 4096:
2490fd97ccfSChristoph Hellwig 		switch (dev->dev_attrib.block_size) {
250c66ac9dbSNicholas Bellinger 		case 2048:
251c66ac9dbSNicholas Bellinger 			blocks_long <<= 1;
252c66ac9dbSNicholas Bellinger 			break;
253c66ac9dbSNicholas Bellinger 		case 1024:
254c66ac9dbSNicholas Bellinger 			blocks_long <<= 2;
255c66ac9dbSNicholas Bellinger 			break;
256c66ac9dbSNicholas Bellinger 		case 512:
257c66ac9dbSNicholas Bellinger 			blocks_long <<= 3;
258492096ecSGustavo A. R. Silva 			break;
259c66ac9dbSNicholas Bellinger 		default:
260c66ac9dbSNicholas Bellinger 			break;
261c66ac9dbSNicholas Bellinger 		}
262c66ac9dbSNicholas Bellinger 		break;
263c66ac9dbSNicholas Bellinger 	case 2048:
2640fd97ccfSChristoph Hellwig 		switch (dev->dev_attrib.block_size) {
265c66ac9dbSNicholas Bellinger 		case 4096:
266c66ac9dbSNicholas Bellinger 			blocks_long >>= 1;
267c66ac9dbSNicholas Bellinger 			break;
268c66ac9dbSNicholas Bellinger 		case 1024:
269c66ac9dbSNicholas Bellinger 			blocks_long <<= 1;
270c66ac9dbSNicholas Bellinger 			break;
271c66ac9dbSNicholas Bellinger 		case 512:
272c66ac9dbSNicholas Bellinger 			blocks_long <<= 2;
273c66ac9dbSNicholas Bellinger 			break;
274c66ac9dbSNicholas Bellinger 		default:
275c66ac9dbSNicholas Bellinger 			break;
276c66ac9dbSNicholas Bellinger 		}
277c66ac9dbSNicholas Bellinger 		break;
278c66ac9dbSNicholas Bellinger 	case 1024:
2790fd97ccfSChristoph Hellwig 		switch (dev->dev_attrib.block_size) {
280c66ac9dbSNicholas Bellinger 		case 4096:
281c66ac9dbSNicholas Bellinger 			blocks_long >>= 2;
282c66ac9dbSNicholas Bellinger 			break;
283c66ac9dbSNicholas Bellinger 		case 2048:
284c66ac9dbSNicholas Bellinger 			blocks_long >>= 1;
285c66ac9dbSNicholas Bellinger 			break;
286c66ac9dbSNicholas Bellinger 		case 512:
287c66ac9dbSNicholas Bellinger 			blocks_long <<= 1;
288c66ac9dbSNicholas Bellinger 			break;
289c66ac9dbSNicholas Bellinger 		default:
290c66ac9dbSNicholas Bellinger 			break;
291c66ac9dbSNicholas Bellinger 		}
292c66ac9dbSNicholas Bellinger 		break;
293c66ac9dbSNicholas Bellinger 	case 512:
2940fd97ccfSChristoph Hellwig 		switch (dev->dev_attrib.block_size) {
295c66ac9dbSNicholas Bellinger 		case 4096:
296c66ac9dbSNicholas Bellinger 			blocks_long >>= 3;
297c66ac9dbSNicholas Bellinger 			break;
298c66ac9dbSNicholas Bellinger 		case 2048:
299c66ac9dbSNicholas Bellinger 			blocks_long >>= 2;
300c66ac9dbSNicholas Bellinger 			break;
301c66ac9dbSNicholas Bellinger 		case 1024:
302c66ac9dbSNicholas Bellinger 			blocks_long >>= 1;
303c66ac9dbSNicholas Bellinger 			break;
304c66ac9dbSNicholas Bellinger 		default:
305c66ac9dbSNicholas Bellinger 			break;
306c66ac9dbSNicholas Bellinger 		}
307c66ac9dbSNicholas Bellinger 		break;
308c66ac9dbSNicholas Bellinger 	default:
309c66ac9dbSNicholas Bellinger 		break;
310c66ac9dbSNicholas Bellinger 	}
311c66ac9dbSNicholas Bellinger 
312c66ac9dbSNicholas Bellinger 	return blocks_long;
313c66ac9dbSNicholas Bellinger }
314c66ac9dbSNicholas Bellinger 
iblock_complete_cmd(struct se_cmd * cmd,blk_status_t blk_status)315394f8118SMike Christie static void iblock_complete_cmd(struct se_cmd *cmd, blk_status_t blk_status)
3163a41d85fSNicholas Bellinger {
3173a41d85fSNicholas Bellinger 	struct iblock_req *ibr = cmd->priv;
3183a41d85fSNicholas Bellinger 	u8 status;
3193a41d85fSNicholas Bellinger 
3205981c245SElena Reshetova 	if (!refcount_dec_and_test(&ibr->pending))
3213a41d85fSNicholas Bellinger 		return;
3223a41d85fSNicholas Bellinger 
323394f8118SMike Christie 	if (blk_status == BLK_STS_RESV_CONFLICT)
324394f8118SMike Christie 		status = SAM_STAT_RESERVATION_CONFLICT;
325394f8118SMike Christie 	else if (atomic_read(&ibr->ib_bio_err_cnt))
3263a41d85fSNicholas Bellinger 		status = SAM_STAT_CHECK_CONDITION;
3273a41d85fSNicholas Bellinger 	else
3283a41d85fSNicholas Bellinger 		status = SAM_STAT_GOOD;
3293a41d85fSNicholas Bellinger 
3303a41d85fSNicholas Bellinger 	target_complete_cmd(cmd, status);
3313a41d85fSNicholas Bellinger 	kfree(ibr);
3323a41d85fSNicholas Bellinger }
3333a41d85fSNicholas Bellinger 
iblock_bio_done(struct bio * bio)3344246a0b6SChristoph Hellwig static void iblock_bio_done(struct bio *bio)
3353a41d85fSNicholas Bellinger {
3363a41d85fSNicholas Bellinger 	struct se_cmd *cmd = bio->bi_private;
3373a41d85fSNicholas Bellinger 	struct iblock_req *ibr = cmd->priv;
338394f8118SMike Christie 	blk_status_t blk_status = bio->bi_status;
3393a41d85fSNicholas Bellinger 
3404e4cbee9SChristoph Hellwig 	if (bio->bi_status) {
3414e4cbee9SChristoph Hellwig 		pr_err("bio error: %p,  err: %d\n", bio, bio->bi_status);
3423a41d85fSNicholas Bellinger 		/*
3433a41d85fSNicholas Bellinger 		 * Bump the ib_bio_err_cnt and release bio.
3443a41d85fSNicholas Bellinger 		 */
3453a41d85fSNicholas Bellinger 		atomic_inc(&ibr->ib_bio_err_cnt);
3464e857c58SPeter Zijlstra 		smp_mb__after_atomic();
3473a41d85fSNicholas Bellinger 	}
3483a41d85fSNicholas Bellinger 
3493a41d85fSNicholas Bellinger 	bio_put(bio);
3503a41d85fSNicholas Bellinger 
351394f8118SMike Christie 	iblock_complete_cmd(cmd, blk_status);
3523a41d85fSNicholas Bellinger }
3533a41d85fSNicholas Bellinger 
iblock_get_bio(struct se_cmd * cmd,sector_t lba,u32 sg_num,blk_opf_t opf)354bc9e0e36SChaitanya Kulkarni static struct bio *iblock_get_bio(struct se_cmd *cmd, sector_t lba, u32 sg_num,
35579fe9d7dSBart Van Assche 				  blk_opf_t opf)
3563a41d85fSNicholas Bellinger {
3573a41d85fSNicholas Bellinger 	struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
3583a41d85fSNicholas Bellinger 	struct bio *bio;
3593a41d85fSNicholas Bellinger 
3603a41d85fSNicholas Bellinger 	/*
3613a41d85fSNicholas Bellinger 	 * Only allocate as many vector entries as the bio code allows us to,
3623a41d85fSNicholas Bellinger 	 * we'll loop later on until we have handled the whole request.
3633a41d85fSNicholas Bellinger 	 */
364609be106SChristoph Hellwig 	bio = bio_alloc_bioset(ib_dev->ibd_bd, bio_max_segs(sg_num), opf,
365609be106SChristoph Hellwig 			       GFP_NOIO, &ib_dev->ibd_bio_set);
3663a41d85fSNicholas Bellinger 	if (!bio) {
3673a41d85fSNicholas Bellinger 		pr_err("Unable to allocate memory for bio\n");
3683a41d85fSNicholas Bellinger 		return NULL;
3693a41d85fSNicholas Bellinger 	}
3703a41d85fSNicholas Bellinger 
3713a41d85fSNicholas Bellinger 	bio->bi_private = cmd;
3723a41d85fSNicholas Bellinger 	bio->bi_end_io = &iblock_bio_done;
3734f024f37SKent Overstreet 	bio->bi_iter.bi_sector = lba;
3743a41d85fSNicholas Bellinger 
3753a41d85fSNicholas Bellinger 	return bio;
3763a41d85fSNicholas Bellinger }
3773a41d85fSNicholas Bellinger 
iblock_submit_bios(struct bio_list * list)3784e49ea4aSMike Christie static void iblock_submit_bios(struct bio_list *list)
3793a41d85fSNicholas Bellinger {
3803a41d85fSNicholas Bellinger 	struct blk_plug plug;
3813a41d85fSNicholas Bellinger 	struct bio *bio;
382415ccd98SMike Christie 	/*
383415ccd98SMike Christie 	 * The block layer handles nested plugs, so just plug/unplug to handle
384415ccd98SMike Christie 	 * fabric drivers that didn't support batching and multi bio cmds.
385415ccd98SMike Christie 	 */
3863a41d85fSNicholas Bellinger 	blk_start_plug(&plug);
3873a41d85fSNicholas Bellinger 	while ((bio = bio_list_pop(list)))
3884e49ea4aSMike Christie 		submit_bio(bio);
3893a41d85fSNicholas Bellinger 	blk_finish_plug(&plug);
3903a41d85fSNicholas Bellinger }
3913a41d85fSNicholas Bellinger 
iblock_end_io_flush(struct bio * bio)3924246a0b6SChristoph Hellwig static void iblock_end_io_flush(struct bio *bio)
393df5fa691SChristoph Hellwig {
394df5fa691SChristoph Hellwig 	struct se_cmd *cmd = bio->bi_private;
395df5fa691SChristoph Hellwig 
3964e4cbee9SChristoph Hellwig 	if (bio->bi_status)
3974e4cbee9SChristoph Hellwig 		pr_err("IBLOCK: cache flush failed: %d\n", bio->bi_status);
398df5fa691SChristoph Hellwig 
3995787cacdSChristoph Hellwig 	if (cmd) {
4004e4cbee9SChristoph Hellwig 		if (bio->bi_status)
4015787cacdSChristoph Hellwig 			target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
402de103c93SChristoph Hellwig 		else
4035787cacdSChristoph Hellwig 			target_complete_cmd(cmd, SAM_STAT_GOOD);
4045787cacdSChristoph Hellwig 	}
4055787cacdSChristoph Hellwig 
406df5fa691SChristoph Hellwig 	bio_put(bio);
407df5fa691SChristoph Hellwig }
408df5fa691SChristoph Hellwig 
409c66ac9dbSNicholas Bellinger /*
410df5fa691SChristoph Hellwig  * Implement SYCHRONIZE CACHE.  Note that we can't handle lba ranges and must
411df5fa691SChristoph Hellwig  * always flush the whole cache.
412c66ac9dbSNicholas Bellinger  */
413de103c93SChristoph Hellwig static sense_reason_t
iblock_execute_sync_cache(struct se_cmd * cmd)414de103c93SChristoph Hellwig iblock_execute_sync_cache(struct se_cmd *cmd)
415c66ac9dbSNicholas Bellinger {
4160fd97ccfSChristoph Hellwig 	struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
417a1d8b49aSAndy Grover 	int immed = (cmd->t_task_cdb[1] & 0x2);
418df5fa691SChristoph Hellwig 	struct bio *bio;
419c66ac9dbSNicholas Bellinger 
420c66ac9dbSNicholas Bellinger 	/*
421c66ac9dbSNicholas Bellinger 	 * If the Immediate bit is set, queue up the GOOD response
422df5fa691SChristoph Hellwig 	 * for this SYNCHRONIZE_CACHE op.
423c66ac9dbSNicholas Bellinger 	 */
424c66ac9dbSNicholas Bellinger 	if (immed)
4255787cacdSChristoph Hellwig 		target_complete_cmd(cmd, SAM_STAT_GOOD);
426c66ac9dbSNicholas Bellinger 
42707888c66SChristoph Hellwig 	bio = bio_alloc(ib_dev->ibd_bd, 0, REQ_OP_WRITE | REQ_PREFLUSH,
42807888c66SChristoph Hellwig 			GFP_KERNEL);
429df5fa691SChristoph Hellwig 	bio->bi_end_io = iblock_end_io_flush;
430c66ac9dbSNicholas Bellinger 	if (!immed)
431df5fa691SChristoph Hellwig 		bio->bi_private = cmd;
4324e49ea4aSMike Christie 	submit_bio(bio);
433ad67f0d9SChristoph Hellwig 	return 0;
434c66ac9dbSNicholas Bellinger }
435c66ac9dbSNicholas Bellinger 
436de103c93SChristoph Hellwig static sense_reason_t
iblock_execute_unmap(struct se_cmd * cmd,sector_t lba,sector_t nolb)43762e46942SChristoph Hellwig iblock_execute_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb)
438dbc21c5aSAsias He {
43962e46942SChristoph Hellwig 	struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd;
4408a9ebe71SMike Christie 	struct se_device *dev = cmd->se_dev;
441dbc21c5aSAsias He 	int ret;
442dbc21c5aSAsias He 
4438a9ebe71SMike Christie 	ret = blkdev_issue_discard(bdev,
4448a9ebe71SMike Christie 				   target_to_linux_sector(dev, lba),
4458a9ebe71SMike Christie 				   target_to_linux_sector(dev,  nolb),
44644abff2cSChristoph Hellwig 				   GFP_KERNEL);
447dbc21c5aSAsias He 	if (ret < 0) {
448dbc21c5aSAsias He 		pr_err("blkdev_issue_discard() failed: %d\n", ret);
449dbc21c5aSAsias He 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
450dbc21c5aSAsias He 	}
451dbc21c5aSAsias He 
452dbc21c5aSAsias He 	return 0;
453dbc21c5aSAsias He }
454dbc21c5aSAsias He 
455dbc21c5aSAsias He static sense_reason_t
iblock_execute_zero_out(struct block_device * bdev,struct se_cmd * cmd)4562237498fSNicholas Bellinger iblock_execute_zero_out(struct block_device *bdev, struct se_cmd *cmd)
45707b63196SMike Christie {
45807b63196SMike Christie 	struct se_device *dev = cmd->se_dev;
45907b63196SMike Christie 	struct scatterlist *sg = &cmd->t_data_sg[0];
460f5957dadSBryant G Ly 	unsigned char *buf, *not_zero;
461f5957dadSBryant G Ly 	int ret;
46207b63196SMike Christie 
4632237498fSNicholas Bellinger 	buf = kmap(sg_page(sg)) + sg->offset;
4642237498fSNicholas Bellinger 	if (!buf)
4652237498fSNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
4662237498fSNicholas Bellinger 	/*
4672237498fSNicholas Bellinger 	 * Fall back to block_execute_write_same() slow-path if
4682237498fSNicholas Bellinger 	 * incoming WRITE_SAME payload does not contain zeros.
4692237498fSNicholas Bellinger 	 */
470f5957dadSBryant G Ly 	not_zero = memchr_inv(buf, 0x00, cmd->data_length);
4712237498fSNicholas Bellinger 	kunmap(sg_page(sg));
47207b63196SMike Christie 
473f5957dadSBryant G Ly 	if (not_zero)
4742237498fSNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
4752237498fSNicholas Bellinger 
4762237498fSNicholas Bellinger 	ret = blkdev_issue_zeroout(bdev,
47707b63196SMike Christie 				target_to_linux_sector(dev, cmd->t_task_lba),
47807b63196SMike Christie 				target_to_linux_sector(dev,
47907b63196SMike Christie 					sbc_get_write_same_sectors(cmd)),
4801d2ff149SDavid Disseldorp 				GFP_KERNEL, BLKDEV_ZERO_NOUNMAP);
48107b63196SMike Christie 	if (ret)
48207b63196SMike Christie 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
48307b63196SMike Christie 
48414b40c1eSHannes Reinecke 	target_complete_cmd(cmd, SAM_STAT_GOOD);
48507b63196SMike Christie 	return 0;
48607b63196SMike Christie }
48707b63196SMike Christie 
48807b63196SMike Christie static sense_reason_t
iblock_execute_write_same(struct se_cmd * cmd)489f6970ad3SNicholas Bellinger iblock_execute_write_same(struct se_cmd *cmd)
490f6970ad3SNicholas Bellinger {
49107b63196SMike Christie 	struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd;
492f6970ad3SNicholas Bellinger 	struct iblock_req *ibr;
493f6970ad3SNicholas Bellinger 	struct scatterlist *sg;
494f6970ad3SNicholas Bellinger 	struct bio *bio;
495f6970ad3SNicholas Bellinger 	struct bio_list list;
4968a9ebe71SMike Christie 	struct se_device *dev = cmd->se_dev;
4978a9ebe71SMike Christie 	sector_t block_lba = target_to_linux_sector(dev, cmd->t_task_lba);
4988a9ebe71SMike Christie 	sector_t sectors = target_to_linux_sector(dev,
4998a9ebe71SMike Christie 					sbc_get_write_same_sectors(cmd));
500f6970ad3SNicholas Bellinger 
501afd73f1bSNicholas Bellinger 	if (cmd->prot_op) {
502afd73f1bSNicholas Bellinger 		pr_err("WRITE_SAME: Protection information with IBLOCK"
503afd73f1bSNicholas Bellinger 		       " backends not supported\n");
504afd73f1bSNicholas Bellinger 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
505afd73f1bSNicholas Bellinger 	}
506ccd3f449SMike Christie 
507ccd3f449SMike Christie 	if (!cmd->t_data_nents)
508ccd3f449SMike Christie 		return TCM_INVALID_CDB_FIELD;
509ccd3f449SMike Christie 
510f6970ad3SNicholas Bellinger 	sg = &cmd->t_data_sg[0];
511f6970ad3SNicholas Bellinger 
512f6970ad3SNicholas Bellinger 	if (cmd->t_data_nents > 1 ||
513f6970ad3SNicholas Bellinger 	    sg->length != cmd->se_dev->dev_attrib.block_size) {
514f6970ad3SNicholas Bellinger 		pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u"
515f6970ad3SNicholas Bellinger 			" block_size: %u\n", cmd->t_data_nents, sg->length,
516f6970ad3SNicholas Bellinger 			cmd->se_dev->dev_attrib.block_size);
517f6970ad3SNicholas Bellinger 		return TCM_INVALID_CDB_FIELD;
518f6970ad3SNicholas Bellinger 	}
519f6970ad3SNicholas Bellinger 
5202237498fSNicholas Bellinger 	if (bdev_write_zeroes_sectors(bdev)) {
5212237498fSNicholas Bellinger 		if (!iblock_execute_zero_out(bdev, cmd))
5222237498fSNicholas Bellinger 			return 0;
5232237498fSNicholas Bellinger 	}
52407b63196SMike Christie 
525f6970ad3SNicholas Bellinger 	ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
526f6970ad3SNicholas Bellinger 	if (!ibr)
527f6970ad3SNicholas Bellinger 		goto fail;
528f6970ad3SNicholas Bellinger 	cmd->priv = ibr;
529f6970ad3SNicholas Bellinger 
530bc9e0e36SChaitanya Kulkarni 	bio = iblock_get_bio(cmd, block_lba, 1, REQ_OP_WRITE);
531f6970ad3SNicholas Bellinger 	if (!bio)
532f6970ad3SNicholas Bellinger 		goto fail_free_ibr;
533f6970ad3SNicholas Bellinger 
534f6970ad3SNicholas Bellinger 	bio_list_init(&list);
535f6970ad3SNicholas Bellinger 	bio_list_add(&list, bio);
536f6970ad3SNicholas Bellinger 
5375981c245SElena Reshetova 	refcount_set(&ibr->pending, 1);
538f6970ad3SNicholas Bellinger 
539f6970ad3SNicholas Bellinger 	while (sectors) {
540f6970ad3SNicholas Bellinger 		while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
541f6970ad3SNicholas Bellinger 				!= sg->length) {
542f6970ad3SNicholas Bellinger 
543bc9e0e36SChaitanya Kulkarni 			bio = iblock_get_bio(cmd, block_lba, 1, REQ_OP_WRITE);
544f6970ad3SNicholas Bellinger 			if (!bio)
545f6970ad3SNicholas Bellinger 				goto fail_put_bios;
546f6970ad3SNicholas Bellinger 
5475981c245SElena Reshetova 			refcount_inc(&ibr->pending);
548f6970ad3SNicholas Bellinger 			bio_list_add(&list, bio);
549f6970ad3SNicholas Bellinger 		}
550f6970ad3SNicholas Bellinger 
551f6970ad3SNicholas Bellinger 		/* Always in 512 byte units for Linux/Block */
55280b045b3SBart Van Assche 		block_lba += sg->length >> SECTOR_SHIFT;
5535676234fSRoman Bolshakov 		sectors -= sg->length >> SECTOR_SHIFT;
554f6970ad3SNicholas Bellinger 	}
555f6970ad3SNicholas Bellinger 
5564e49ea4aSMike Christie 	iblock_submit_bios(&list);
557f6970ad3SNicholas Bellinger 	return 0;
558f6970ad3SNicholas Bellinger 
559f6970ad3SNicholas Bellinger fail_put_bios:
560f6970ad3SNicholas Bellinger 	while ((bio = bio_list_pop(&list)))
561f6970ad3SNicholas Bellinger 		bio_put(bio);
562f6970ad3SNicholas Bellinger fail_free_ibr:
563f6970ad3SNicholas Bellinger 	kfree(ibr);
564f6970ad3SNicholas Bellinger fail:
565f6970ad3SNicholas Bellinger 	return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
566f6970ad3SNicholas Bellinger }
567f6970ad3SNicholas Bellinger 
568c66ac9dbSNicholas Bellinger enum {
56944bfd018SAndy Grover 	Opt_udev_path, Opt_readonly, Opt_force, Opt_err
570c66ac9dbSNicholas Bellinger };
571c66ac9dbSNicholas Bellinger 
572c66ac9dbSNicholas Bellinger static match_table_t tokens = {
573c66ac9dbSNicholas Bellinger 	{Opt_udev_path, "udev_path=%s"},
57444bfd018SAndy Grover 	{Opt_readonly, "readonly=%d"},
575c66ac9dbSNicholas Bellinger 	{Opt_force, "force=%d"},
576c66ac9dbSNicholas Bellinger 	{Opt_err, NULL}
577c66ac9dbSNicholas Bellinger };
578c66ac9dbSNicholas Bellinger 
iblock_set_configfs_dev_params(struct se_device * dev,const char * page,ssize_t count)5790fd97ccfSChristoph Hellwig static ssize_t iblock_set_configfs_dev_params(struct se_device *dev,
580c66ac9dbSNicholas Bellinger 		const char *page, ssize_t count)
581c66ac9dbSNicholas Bellinger {
5820fd97ccfSChristoph Hellwig 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
5836d180253SJesper Juhl 	char *orig, *ptr, *arg_p, *opts;
584c66ac9dbSNicholas Bellinger 	substring_t args[MAX_OPT_ARGS];
58521bca31cSRoland Dreier 	int ret = 0, token;
58644bfd018SAndy Grover 	unsigned long tmp_readonly;
587c66ac9dbSNicholas Bellinger 
588c66ac9dbSNicholas Bellinger 	opts = kstrdup(page, GFP_KERNEL);
589c66ac9dbSNicholas Bellinger 	if (!opts)
590c66ac9dbSNicholas Bellinger 		return -ENOMEM;
591c66ac9dbSNicholas Bellinger 
592c66ac9dbSNicholas Bellinger 	orig = opts;
593c66ac9dbSNicholas Bellinger 
59490c161b6SSebastian Andrzej Siewior 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
595c66ac9dbSNicholas Bellinger 		if (!*ptr)
596c66ac9dbSNicholas Bellinger 			continue;
597c66ac9dbSNicholas Bellinger 
598c66ac9dbSNicholas Bellinger 		token = match_token(ptr, tokens, args);
599c66ac9dbSNicholas Bellinger 		switch (token) {
600c66ac9dbSNicholas Bellinger 		case Opt_udev_path:
601c66ac9dbSNicholas Bellinger 			if (ib_dev->ibd_bd) {
6026708bb27SAndy Grover 				pr_err("Unable to set udev_path= while"
603c66ac9dbSNicholas Bellinger 					" ib_dev->ibd_bd exists\n");
604c66ac9dbSNicholas Bellinger 				ret = -EEXIST;
605c66ac9dbSNicholas Bellinger 				goto out;
606c66ac9dbSNicholas Bellinger 			}
607852b6ed1SNicholas Bellinger 			if (match_strlcpy(ib_dev->ibd_udev_path, &args[0],
608852b6ed1SNicholas Bellinger 				SE_UDEV_PATH_LEN) == 0) {
609852b6ed1SNicholas Bellinger 				ret = -EINVAL;
6106d180253SJesper Juhl 				break;
6116d180253SJesper Juhl 			}
6126708bb27SAndy Grover 			pr_debug("IBLOCK: Referencing UDEV path: %s\n",
613c66ac9dbSNicholas Bellinger 					ib_dev->ibd_udev_path);
614c66ac9dbSNicholas Bellinger 			ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH;
615c66ac9dbSNicholas Bellinger 			break;
61644bfd018SAndy Grover 		case Opt_readonly:
61744bfd018SAndy Grover 			arg_p = match_strdup(&args[0]);
61844bfd018SAndy Grover 			if (!arg_p) {
61944bfd018SAndy Grover 				ret = -ENOMEM;
62044bfd018SAndy Grover 				break;
62144bfd018SAndy Grover 			}
62257103d7fSJingoo Han 			ret = kstrtoul(arg_p, 0, &tmp_readonly);
62344bfd018SAndy Grover 			kfree(arg_p);
62444bfd018SAndy Grover 			if (ret < 0) {
62557103d7fSJingoo Han 				pr_err("kstrtoul() failed for"
62644bfd018SAndy Grover 						" readonly=\n");
62744bfd018SAndy Grover 				goto out;
62844bfd018SAndy Grover 			}
62944bfd018SAndy Grover 			ib_dev->ibd_readonly = tmp_readonly;
63044bfd018SAndy Grover 			pr_debug("IBLOCK: readonly: %d\n", ib_dev->ibd_readonly);
63144bfd018SAndy Grover 			break;
632c66ac9dbSNicholas Bellinger 		case Opt_force:
633c66ac9dbSNicholas Bellinger 			break;
634c66ac9dbSNicholas Bellinger 		default:
635c66ac9dbSNicholas Bellinger 			break;
636c66ac9dbSNicholas Bellinger 		}
637c66ac9dbSNicholas Bellinger 	}
638c66ac9dbSNicholas Bellinger 
639c66ac9dbSNicholas Bellinger out:
640c66ac9dbSNicholas Bellinger 	kfree(orig);
641c66ac9dbSNicholas Bellinger 	return (!ret) ? count : ret;
642c66ac9dbSNicholas Bellinger }
643c66ac9dbSNicholas Bellinger 
iblock_show_configfs_dev_params(struct se_device * dev,char * b)6440fd97ccfSChristoph Hellwig static ssize_t iblock_show_configfs_dev_params(struct se_device *dev, char *b)
645c66ac9dbSNicholas Bellinger {
6460fd97ccfSChristoph Hellwig 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
6470fd97ccfSChristoph Hellwig 	struct block_device *bd = ib_dev->ibd_bd;
648c66ac9dbSNicholas Bellinger 	ssize_t bl = 0;
649c66ac9dbSNicholas Bellinger 
650c66ac9dbSNicholas Bellinger 	if (bd)
6511b74ab77SChristoph Hellwig 		bl += sprintf(b + bl, "iBlock device: %pg", bd);
6520fd97ccfSChristoph Hellwig 	if (ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)
65344bfd018SAndy Grover 		bl += sprintf(b + bl, "  UDEV PATH: %s",
6540fd97ccfSChristoph Hellwig 				ib_dev->ibd_udev_path);
6550fd97ccfSChristoph Hellwig 	bl += sprintf(b + bl, "  readonly: %d\n", ib_dev->ibd_readonly);
656c66ac9dbSNicholas Bellinger 
657c66ac9dbSNicholas Bellinger 	bl += sprintf(b + bl, "        ");
658c66ac9dbSNicholas Bellinger 	if (bd) {
659c66ac9dbSNicholas Bellinger 		bl += sprintf(b + bl, "Major: %d Minor: %d  %s\n",
66057ba1059SChristoph Hellwig 			MAJOR(bd->bd_dev), MINOR(bd->bd_dev),
66157ba1059SChristoph Hellwig 			"CLAIMED: IBLOCK");
662c66ac9dbSNicholas Bellinger 	} else {
66321bca31cSRoland Dreier 		bl += sprintf(b + bl, "Major: 0 Minor: 0\n");
664c66ac9dbSNicholas Bellinger 	}
665c66ac9dbSNicholas Bellinger 
666c66ac9dbSNicholas Bellinger 	return bl;
667c66ac9dbSNicholas Bellinger }
668c66ac9dbSNicholas Bellinger 
669ecebbf6cSNicholas Bellinger static int
iblock_alloc_bip(struct se_cmd * cmd,struct bio * bio,struct sg_mapping_iter * miter)670fed564f6SGreg Edwards iblock_alloc_bip(struct se_cmd *cmd, struct bio *bio,
671fed564f6SGreg Edwards 		 struct sg_mapping_iter *miter)
672ecebbf6cSNicholas Bellinger {
673ecebbf6cSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
674ecebbf6cSNicholas Bellinger 	struct blk_integrity *bi;
675ecebbf6cSNicholas Bellinger 	struct bio_integrity_payload *bip;
676ecebbf6cSNicholas Bellinger 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
677fed564f6SGreg Edwards 	int rc;
678fed564f6SGreg Edwards 	size_t resid, len;
679ecebbf6cSNicholas Bellinger 
680ecebbf6cSNicholas Bellinger 	bi = bdev_get_integrity(ib_dev->ibd_bd);
681ecebbf6cSNicholas Bellinger 	if (!bi) {
682ecebbf6cSNicholas Bellinger 		pr_err("Unable to locate bio_integrity\n");
683ecebbf6cSNicholas Bellinger 		return -ENODEV;
684ecebbf6cSNicholas Bellinger 	}
685ecebbf6cSNicholas Bellinger 
6865f7136dbSMatthew Wilcox (Oracle) 	bip = bio_integrity_alloc(bio, GFP_NOIO, bio_max_segs(cmd->t_prot_nents));
68706c1e390SKeith Busch 	if (IS_ERR(bip)) {
688ecebbf6cSNicholas Bellinger 		pr_err("Unable to allocate bio_integrity_payload\n");
68906c1e390SKeith Busch 		return PTR_ERR(bip);
690ecebbf6cSNicholas Bellinger 	}
691ecebbf6cSNicholas Bellinger 
692fed564f6SGreg Edwards 	/* virtual start sector must be in integrity interval units */
693e4dc9a4cSIsrael Rukshin 	bip_set_seed(bip, bio->bi_iter.bi_sector >>
694e4dc9a4cSIsrael Rukshin 				  (bi->interval_exp - SECTOR_SHIFT));
695e4dc9a4cSIsrael Rukshin 
696ecebbf6cSNicholas Bellinger 	pr_debug("IBLOCK BIP Size: %u Sector: %llu\n", bip->bip_iter.bi_size,
6974e13c5d0SLinus Torvalds 		 (unsigned long long)bip->bip_iter.bi_sector);
6984e13c5d0SLinus Torvalds 
699ecebbf6cSNicholas Bellinger 	resid = bio_integrity_bytes(bi, bio_sectors(bio));
700fed564f6SGreg Edwards 	while (resid > 0 && sg_miter_next(miter)) {
701fed564f6SGreg Edwards 
702ecebbf6cSNicholas Bellinger 		len = min_t(size_t, miter->length, resid);
703fed564f6SGreg Edwards 		rc = bio_integrity_add_page(bio, miter->page, len,
704fed564f6SGreg Edwards 					    offset_in_page(miter->addr));
705fed564f6SGreg Edwards 		if (rc != len) {
706fed564f6SGreg Edwards 			pr_err("bio_integrity_add_page() failed; %d\n", rc);
707ecebbf6cSNicholas Bellinger 			sg_miter_stop(miter);
708fed564f6SGreg Edwards 			return -ENOMEM;
709ecebbf6cSNicholas Bellinger 		}
710ecebbf6cSNicholas Bellinger 
711ecebbf6cSNicholas Bellinger 		pr_debug("Added bio integrity page: %p length: %zu offset: %lu\n",
712fed564f6SGreg Edwards 			  miter->page, len, offset_in_page(miter->addr));
713fed564f6SGreg Edwards 
714fed564f6SGreg Edwards 		resid -= len;
715fed564f6SGreg Edwards 		if (len < miter->length)
716fed564f6SGreg Edwards 			miter->consumed -= miter->length - len;
717fed564f6SGreg Edwards 	}
718ecebbf6cSNicholas Bellinger 	sg_miter_stop(miter);
719fed564f6SGreg Edwards 
720ecebbf6cSNicholas Bellinger 	return 0;
721ecebbf6cSNicholas Bellinger }
722ecebbf6cSNicholas Bellinger 
723ecebbf6cSNicholas Bellinger static sense_reason_t
iblock_execute_rw(struct se_cmd * cmd,struct scatterlist * sgl,u32 sgl_nents,enum dma_data_direction data_direction)724de103c93SChristoph Hellwig iblock_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
725a82a9538SNicholas Bellinger 		  enum dma_data_direction data_direction)
726a82a9538SNicholas Bellinger {
727c66ac9dbSNicholas Bellinger 	struct se_device *dev = cmd->se_dev;
7285951146dSAndy Grover 	sector_t block_lba = target_to_linux_sector(dev, cmd->t_task_lba);
7298a9ebe71SMike Christie 	struct iblock_req *ibr;
7305787cacdSChristoph Hellwig 	struct bio *bio;
731fed564f6SGreg Edwards 	struct bio_list list;
732dbbf3e94SChristoph Hellwig 	struct scatterlist *sg;
733c66ac9dbSNicholas Bellinger 	u32 sg_num = sgl_nents;
7345787cacdSChristoph Hellwig 	blk_opf_t opf;
73579fe9d7dSBart Van Assche 	unsigned bio_cnt;
736d5b4a21bSChristoph Hellwig 	int i, rc;
737bc9e0e36SChaitanya Kulkarni 	struct sg_mapping_iter prot_miter;
738fed564f6SGreg Edwards 	unsigned int miter_dir;
739bc9e0e36SChaitanya Kulkarni 
740dbbf3e94SChristoph Hellwig 	if (data_direction == DMA_TO_DEVICE) {
7415787cacdSChristoph Hellwig 		struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
742d0c8b259SNicholas Bellinger 
743*84c073fdSMike Christie 		/*
744*84c073fdSMike Christie 		 * Set bits to indicate WRITE_ODIRECT so we are not throttled
745*84c073fdSMike Christie 		 * by WBT.
746*84c073fdSMike Christie 		 */
747*84c073fdSMike Christie 		opf = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
748*84c073fdSMike Christie 		/*
749dbbf3e94SChristoph Hellwig 		 * Force writethrough using REQ_FUA if a volatile write cache
75070fd7614SChristoph Hellwig 		 * is not enabled, or if initiator set the Force Unit Access bit.
751d0c8b259SNicholas Bellinger 		 */
752dbbf3e94SChristoph Hellwig 		miter_dir = SG_MITER_TO_SG;
753bc9e0e36SChaitanya Kulkarni 		if (bdev_fua(ib_dev->ibd_bd)) {
754a557e82eSChristoph Hellwig 			if (cmd->se_cmd_flags & SCF_FUA)
755d0c8b259SNicholas Bellinger 				opf |= REQ_FUA;
756bc9e0e36SChaitanya Kulkarni 			else if (!bdev_write_cache(ib_dev->ibd_bd))
75708e688fdSChristoph Hellwig 				opf |= REQ_FUA;
758bc9e0e36SChaitanya Kulkarni 		}
759d0c8b259SNicholas Bellinger 	} else {
760dbbf3e94SChristoph Hellwig 		opf = REQ_OP_READ;
761bc9e0e36SChaitanya Kulkarni 		miter_dir = SG_MITER_FROM_SG;
762bc9e0e36SChaitanya Kulkarni 	}
763dbbf3e94SChristoph Hellwig 
764dbbf3e94SChristoph Hellwig 	ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
7655787cacdSChristoph Hellwig 	if (!ibr)
7665787cacdSChristoph Hellwig 		goto fail;
7675787cacdSChristoph Hellwig 	cmd->priv = ibr;
7685787cacdSChristoph Hellwig 
7695787cacdSChristoph Hellwig 	if (!sgl_nents) {
770e0de4457SPaolo Bonzini 		refcount_set(&ibr->pending, 1);
7715981c245SElena Reshetova 		iblock_complete_cmd(cmd, BLK_STS_OK);
772394f8118SMike Christie 		return 0;
773e0de4457SPaolo Bonzini 	}
774e0de4457SPaolo Bonzini 
775e0de4457SPaolo Bonzini 	bio = iblock_get_bio(cmd, block_lba, sgl_nents, opf);
776bc9e0e36SChaitanya Kulkarni 	if (!bio)
7775787cacdSChristoph Hellwig 		goto fail_free_ibr;
7785787cacdSChristoph Hellwig 
779c66ac9dbSNicholas Bellinger 	bio_list_init(&list);
780dbbf3e94SChristoph Hellwig 	bio_list_add(&list, bio);
781dbbf3e94SChristoph Hellwig 
7825787cacdSChristoph Hellwig 	refcount_set(&ibr->pending, 2);
7835981c245SElena Reshetova 	bio_cnt = 1;
784d5b4a21bSChristoph Hellwig 
785dbbf3e94SChristoph Hellwig 	if (cmd->prot_type && dev->dev_attrib.pi_prot_type)
786fed564f6SGreg Edwards 		sg_miter_start(&prot_miter, cmd->t_prot_sg, cmd->t_prot_nents,
787fed564f6SGreg Edwards 			       miter_dir);
788bc9e0e36SChaitanya Kulkarni 
789fed564f6SGreg Edwards 	for_each_sg(sgl, sg, sgl_nents, i) {
7905787cacdSChristoph Hellwig 		/*
791dbbf3e94SChristoph Hellwig 		 * XXX: if the length the device accepts is shorter than the
792dbbf3e94SChristoph Hellwig 		 *	length of the S/G list entry this will cause and
793dbbf3e94SChristoph Hellwig 		 *	endless loop.  Better hope no driver uses huge pages.
794dbbf3e94SChristoph Hellwig 		 */
795dbbf3e94SChristoph Hellwig 		while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
796dbbf3e94SChristoph Hellwig 				!= sg->length) {
797dbbf3e94SChristoph Hellwig 			if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
798fed564f6SGreg Edwards 				rc = iblock_alloc_bip(cmd, bio, &prot_miter);
799fed564f6SGreg Edwards 				if (rc)
800fed564f6SGreg Edwards 					goto fail_put_bios;
801fed564f6SGreg Edwards 			}
802fed564f6SGreg Edwards 
803fed564f6SGreg Edwards 			if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) {
804d5b4a21bSChristoph Hellwig 				iblock_submit_bios(&list);
8054e49ea4aSMike Christie 				bio_cnt = 0;
806d5b4a21bSChristoph Hellwig 			}
807d5b4a21bSChristoph Hellwig 
808d5b4a21bSChristoph Hellwig 			bio = iblock_get_bio(cmd, block_lba, sg_num, opf);
809bc9e0e36SChaitanya Kulkarni 			if (!bio)
8106708bb27SAndy Grover 				goto fail_put_bios;
8115787cacdSChristoph Hellwig 
8125787cacdSChristoph Hellwig 			refcount_inc(&ibr->pending);
8135981c245SElena Reshetova 			bio_list_add(&list, bio);
814dbbf3e94SChristoph Hellwig 			bio_cnt++;
815d5b4a21bSChristoph Hellwig 		}
816c66ac9dbSNicholas Bellinger 
817dbbf3e94SChristoph Hellwig 		/* Always in 512 byte units for Linux/Block */
818c66ac9dbSNicholas Bellinger 		block_lba += sg->length >> SECTOR_SHIFT;
81980b045b3SBart Van Assche 		sg_num--;
820c66ac9dbSNicholas Bellinger 	}
821c66ac9dbSNicholas Bellinger 
822c66ac9dbSNicholas Bellinger 	if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
8236f16ec43SNicholas Bellinger 		rc = iblock_alloc_bip(cmd, bio, &prot_miter);
824fed564f6SGreg Edwards 		if (rc)
825ecebbf6cSNicholas Bellinger 			goto fail_put_bios;
826ecebbf6cSNicholas Bellinger 	}
827ecebbf6cSNicholas Bellinger 
828ecebbf6cSNicholas Bellinger 	iblock_submit_bios(&list);
8294e49ea4aSMike Christie 	iblock_complete_cmd(cmd, BLK_STS_OK);
830394f8118SMike Christie 	return 0;
83103e98c9eSNicholas Bellinger 
832dbbf3e94SChristoph Hellwig fail_put_bios:
8335787cacdSChristoph Hellwig 	while ((bio = bio_list_pop(&list)))
834dbbf3e94SChristoph Hellwig 		bio_put(bio);
835c66ac9dbSNicholas Bellinger fail_free_ibr:
8365787cacdSChristoph Hellwig 	kfree(ibr);
8375787cacdSChristoph Hellwig fail:
8385787cacdSChristoph Hellwig 	return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
839de103c93SChristoph Hellwig }
840c66ac9dbSNicholas Bellinger 
iblock_execute_pr_out(struct se_cmd * cmd,u8 sa,u64 key,u64 sa_key,u8 type,bool aptpl)841c66ac9dbSNicholas Bellinger static sense_reason_t iblock_execute_pr_out(struct se_cmd *cmd, u8 sa, u64 key,
842394f8118SMike Christie 					    u64 sa_key, u8 type, bool aptpl)
843394f8118SMike Christie {
844394f8118SMike Christie 	struct se_device *dev = cmd->se_dev;
845394f8118SMike Christie 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
846394f8118SMike Christie 	struct block_device *bdev = ib_dev->ibd_bd;
847394f8118SMike Christie 	const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
848394f8118SMike Christie 	int ret;
849394f8118SMike Christie 
850394f8118SMike Christie 	if (!ops) {
851394f8118SMike Christie 		pr_err("Block device does not support pr_ops but iblock device has been configured for PR passthrough.\n");
852394f8118SMike Christie 		return TCM_UNSUPPORTED_SCSI_OPCODE;
853394f8118SMike Christie 	}
854394f8118SMike Christie 
855394f8118SMike Christie 	switch (sa) {
856394f8118SMike Christie 	case PRO_REGISTER:
857394f8118SMike Christie 	case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
858394f8118SMike Christie 		if (!ops->pr_register) {
859394f8118SMike Christie 			pr_err("block device does not support pr_register.\n");
860394f8118SMike Christie 			return TCM_UNSUPPORTED_SCSI_OPCODE;
861394f8118SMike Christie 		}
862394f8118SMike Christie 
863394f8118SMike Christie 		/* The block layer pr ops always enables aptpl */
864394f8118SMike Christie 		if (!aptpl)
865394f8118SMike Christie 			pr_info("APTPL not set by initiator, but will be used.\n");
866394f8118SMike Christie 
867394f8118SMike Christie 		ret = ops->pr_register(bdev, key, sa_key,
868394f8118SMike Christie 				sa == PRO_REGISTER ? 0 : PR_FL_IGNORE_KEY);
869394f8118SMike Christie 		break;
870394f8118SMike Christie 	case PRO_RESERVE:
871394f8118SMike Christie 		if (!ops->pr_reserve) {
872394f8118SMike Christie 			pr_err("block_device does not support pr_reserve.\n");
873394f8118SMike Christie 			return TCM_UNSUPPORTED_SCSI_OPCODE;
874394f8118SMike Christie 		}
875394f8118SMike Christie 
876394f8118SMike Christie 		ret = ops->pr_reserve(bdev, key, scsi_pr_type_to_block(type), 0);
877394f8118SMike Christie 		break;
878394f8118SMike Christie 	case PRO_CLEAR:
879394f8118SMike Christie 		if (!ops->pr_clear) {
880394f8118SMike Christie 			pr_err("block_device does not support pr_clear.\n");
881394f8118SMike Christie 			return TCM_UNSUPPORTED_SCSI_OPCODE;
882394f8118SMike Christie 		}
883394f8118SMike Christie 
884394f8118SMike Christie 		ret = ops->pr_clear(bdev, key);
885394f8118SMike Christie 		break;
886394f8118SMike Christie 	case PRO_PREEMPT:
887394f8118SMike Christie 	case PRO_PREEMPT_AND_ABORT:
888394f8118SMike Christie 		if (!ops->pr_clear) {
889394f8118SMike Christie 			pr_err("block_device does not support pr_preempt.\n");
890394f8118SMike Christie 			return TCM_UNSUPPORTED_SCSI_OPCODE;
891394f8118SMike Christie 		}
892394f8118SMike Christie 
893394f8118SMike Christie 		ret = ops->pr_preempt(bdev, key, sa_key,
894394f8118SMike Christie 				      scsi_pr_type_to_block(type),
895394f8118SMike Christie 				      sa == PRO_PREEMPT_AND_ABORT);
89640863cb9SMike Christie 		break;
897394f8118SMike Christie 	case PRO_RELEASE:
898394f8118SMike Christie 		if (!ops->pr_clear) {
899394f8118SMike Christie 			pr_err("block_device does not support pr_pclear.\n");
900394f8118SMike Christie 			return TCM_UNSUPPORTED_SCSI_OPCODE;
901394f8118SMike Christie 		}
902394f8118SMike Christie 
903394f8118SMike Christie 		ret = ops->pr_release(bdev, key, scsi_pr_type_to_block(type));
904394f8118SMike Christie 		break;
905394f8118SMike Christie 	default:
906394f8118SMike Christie 		pr_err("Unknown PERSISTENT_RESERVE_OUT SA: 0x%02x\n", sa);
907394f8118SMike Christie 		return TCM_UNSUPPORTED_SCSI_OPCODE;
908394f8118SMike Christie 	}
909394f8118SMike Christie 
910394f8118SMike Christie 	if (!ret)
911394f8118SMike Christie 		return TCM_NO_SENSE;
912394f8118SMike Christie 	else if (ret == PR_STS_RESERVATION_CONFLICT)
913394f8118SMike Christie 		return TCM_RESERVATION_CONFLICT;
914394f8118SMike Christie 	else
915394f8118SMike Christie 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
916394f8118SMike Christie }
917394f8118SMike Christie 
iblock_pr_report_caps(unsigned char * param_data)918394f8118SMike Christie static void iblock_pr_report_caps(unsigned char *param_data)
919394f8118SMike Christie {
920394f8118SMike Christie 	u16 len = 8;
921394f8118SMike Christie 
922394f8118SMike Christie 	put_unaligned_be16(len, &param_data[0]);
923394f8118SMike Christie 	/*
924394f8118SMike Christie 	 * When using the pr_ops passthrough method we only support exporting
925394f8118SMike Christie 	 * the device through one target port because from the backend module
926394f8118SMike Christie 	 * level we can't see the target port config. As a result we only
927394f8118SMike Christie 	 * support registration directly from the I_T nexus the cmd is sent
928394f8118SMike Christie 	 * through and do not set ATP_C here.
929394f8118SMike Christie 	 *
930394f8118SMike Christie 	 * The block layer pr_ops do not support passing in initiators so
931394f8118SMike Christie 	 * we don't set SIP_C here.
932394f8118SMike Christie 	 */
933394f8118SMike Christie 	/* PTPL_C: Persistence across Target Power Loss bit */
934394f8118SMike Christie 	param_data[2] |= 0x01;
935394f8118SMike Christie 	/*
936394f8118SMike Christie 	 * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
937394f8118SMike Christie 	 * set the TMV: Task Mask Valid bit.
938394f8118SMike Christie 	 */
939394f8118SMike Christie 	param_data[3] |= 0x80;
940394f8118SMike Christie 	/*
941394f8118SMike Christie 	 * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
942394f8118SMike Christie 	 */
943394f8118SMike Christie 	param_data[3] |= 0x10; /* ALLOW COMMANDs field 001b */
944394f8118SMike Christie 	/*
945394f8118SMike Christie 	 * PTPL_A: Persistence across Target Power Loss Active bit. The block
946394f8118SMike Christie 	 * layer pr ops always enables this so report it active.
947394f8118SMike Christie 	 */
948394f8118SMike Christie 	param_data[3] |= 0x01;
949394f8118SMike Christie 	/*
950394f8118SMike Christie 	 * Setup the PERSISTENT RESERVATION TYPE MASK from Table 212 spc4r37.
951394f8118SMike Christie 	 */
952394f8118SMike Christie 	param_data[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
953394f8118SMike Christie 	param_data[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
954394f8118SMike Christie 	param_data[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
955394f8118SMike Christie 	param_data[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
956394f8118SMike Christie 	param_data[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
957394f8118SMike Christie 	param_data[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
958394f8118SMike Christie }
959394f8118SMike Christie 
iblock_pr_read_keys(struct se_cmd * cmd,unsigned char * param_data)960394f8118SMike Christie static sense_reason_t iblock_pr_read_keys(struct se_cmd *cmd,
961394f8118SMike Christie 					  unsigned char *param_data)
962394f8118SMike Christie {
963394f8118SMike Christie 	struct se_device *dev = cmd->se_dev;
964394f8118SMike Christie 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
965394f8118SMike Christie 	struct block_device *bdev = ib_dev->ibd_bd;
966394f8118SMike Christie 	const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
967394f8118SMike Christie 	int i, len, paths, data_offset;
968394f8118SMike Christie 	struct pr_keys *keys;
969394f8118SMike Christie 	sense_reason_t ret;
970394f8118SMike Christie 
971394f8118SMike Christie 	if (!ops) {
972394f8118SMike Christie 		pr_err("Block device does not support pr_ops but iblock device has been configured for PR passthrough.\n");
973394f8118SMike Christie 		return TCM_UNSUPPORTED_SCSI_OPCODE;
974394f8118SMike Christie 	}
975394f8118SMike Christie 
976394f8118SMike Christie 	if (!ops->pr_read_keys) {
977394f8118SMike Christie 		pr_err("Block device does not support read_keys.\n");
978394f8118SMike Christie 		return TCM_UNSUPPORTED_SCSI_OPCODE;
979394f8118SMike Christie 	}
980394f8118SMike Christie 
981394f8118SMike Christie 	/*
982394f8118SMike Christie 	 * We don't know what's under us, but dm-multipath will register every
983394f8118SMike Christie 	 * path with the same key, so start off with enough space for 16 paths.
984394f8118SMike Christie 	 * which is not a lot of memory and should normally be enough.
985394f8118SMike Christie 	 */
986394f8118SMike Christie 	paths = 16;
987394f8118SMike Christie retry:
988394f8118SMike Christie 	len = 8 * paths;
989394f8118SMike Christie 	keys = kzalloc(sizeof(*keys) + len, GFP_KERNEL);
990394f8118SMike Christie 	if (!keys)
991394f8118SMike Christie 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
992394f8118SMike Christie 
993394f8118SMike Christie 	keys->num_keys = paths;
994394f8118SMike Christie 	if (!ops->pr_read_keys(bdev, keys)) {
995394f8118SMike Christie 		if (keys->num_keys > paths) {
996394f8118SMike Christie 			kfree(keys);
997394f8118SMike Christie 			paths *= 2;
998394f8118SMike Christie 			goto retry;
999394f8118SMike Christie 		}
1000394f8118SMike Christie 	} else {
1001394f8118SMike Christie 		ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1002394f8118SMike Christie 		goto free_keys;
1003394f8118SMike Christie 	}
1004394f8118SMike Christie 
1005394f8118SMike Christie 	ret = TCM_NO_SENSE;
1006394f8118SMike Christie 
1007394f8118SMike Christie 	put_unaligned_be32(keys->generation, &param_data[0]);
1008394f8118SMike Christie 	if (!keys->num_keys) {
1009394f8118SMike Christie 		put_unaligned_be32(0, &param_data[4]);
1010394f8118SMike Christie 		goto free_keys;
1011394f8118SMike Christie 	}
1012394f8118SMike Christie 
1013394f8118SMike Christie 	put_unaligned_be32(8 * keys->num_keys, &param_data[4]);
1014394f8118SMike Christie 
1015394f8118SMike Christie 	data_offset = 8;
1016394f8118SMike Christie 	for (i = 0; i < keys->num_keys; i++) {
1017394f8118SMike Christie 		if (data_offset + 8 > cmd->data_length)
1018394f8118SMike Christie 			break;
1019394f8118SMike Christie 
1020394f8118SMike Christie 		put_unaligned_be64(keys->keys[i], &param_data[data_offset]);
1021394f8118SMike Christie 		data_offset += 8;
1022394f8118SMike Christie 	}
1023394f8118SMike Christie 
1024394f8118SMike Christie free_keys:
1025394f8118SMike Christie 	kfree(keys);
1026394f8118SMike Christie 	return ret;
1027394f8118SMike Christie }
1028394f8118SMike Christie 
iblock_pr_read_reservation(struct se_cmd * cmd,unsigned char * param_data)1029394f8118SMike Christie static sense_reason_t iblock_pr_read_reservation(struct se_cmd *cmd,
1030394f8118SMike Christie 						 unsigned char *param_data)
1031394f8118SMike Christie {
1032394f8118SMike Christie 	struct se_device *dev = cmd->se_dev;
1033394f8118SMike Christie 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
1034394f8118SMike Christie 	struct block_device *bdev = ib_dev->ibd_bd;
1035394f8118SMike Christie 	const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
1036394f8118SMike Christie 	struct pr_held_reservation rsv = { };
1037394f8118SMike Christie 
1038394f8118SMike Christie 	if (!ops) {
1039394f8118SMike Christie 		pr_err("Block device does not support pr_ops but iblock device has been configured for PR passthrough.\n");
1040394f8118SMike Christie 		return TCM_UNSUPPORTED_SCSI_OPCODE;
1041394f8118SMike Christie 	}
1042394f8118SMike Christie 
1043394f8118SMike Christie 	if (!ops->pr_read_reservation) {
1044394f8118SMike Christie 		pr_err("Block device does not support read_keys.\n");
1045394f8118SMike Christie 		return TCM_UNSUPPORTED_SCSI_OPCODE;
1046394f8118SMike Christie 	}
1047394f8118SMike Christie 
1048394f8118SMike Christie 	if (ops->pr_read_reservation(bdev, &rsv))
1049394f8118SMike Christie 		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1050394f8118SMike Christie 
1051394f8118SMike Christie 	put_unaligned_be32(rsv.generation, &param_data[0]);
1052394f8118SMike Christie 	if (!block_pr_type_to_scsi(rsv.type)) {
1053394f8118SMike Christie 		put_unaligned_be32(0, &param_data[4]);
1054394f8118SMike Christie 		return TCM_NO_SENSE;
1055394f8118SMike Christie 	}
1056394f8118SMike Christie 
1057394f8118SMike Christie 	put_unaligned_be32(16, &param_data[4]);
1058394f8118SMike Christie 
1059394f8118SMike Christie 	if (cmd->data_length < 16)
1060394f8118SMike Christie 		return TCM_NO_SENSE;
1061394f8118SMike Christie 	put_unaligned_be64(rsv.key, &param_data[8]);
1062394f8118SMike Christie 
1063394f8118SMike Christie 	if (cmd->data_length < 22)
1064394f8118SMike Christie 		return TCM_NO_SENSE;
1065394f8118SMike Christie 	param_data[21] = block_pr_type_to_scsi(rsv.type);
1066394f8118SMike Christie 
1067394f8118SMike Christie 	return TCM_NO_SENSE;
1068394f8118SMike Christie }
1069394f8118SMike Christie 
iblock_execute_pr_in(struct se_cmd * cmd,u8 sa,unsigned char * param_data)1070394f8118SMike Christie static sense_reason_t iblock_execute_pr_in(struct se_cmd *cmd, u8 sa,
1071394f8118SMike Christie 					   unsigned char *param_data)
1072394f8118SMike Christie {
1073394f8118SMike Christie 	sense_reason_t ret = TCM_NO_SENSE;
1074394f8118SMike Christie 
1075394f8118SMike Christie 	switch (sa) {
1076394f8118SMike Christie 	case PRI_REPORT_CAPABILITIES:
1077394f8118SMike Christie 		iblock_pr_report_caps(param_data);
1078394f8118SMike Christie 		break;
1079394f8118SMike Christie 	case PRI_READ_KEYS:
1080394f8118SMike Christie 		ret = iblock_pr_read_keys(cmd, param_data);
1081394f8118SMike Christie 		break;
1082394f8118SMike Christie 	case PRI_READ_RESERVATION:
1083394f8118SMike Christie 		ret = iblock_pr_read_reservation(cmd, param_data);
1084394f8118SMike Christie 		break;
1085394f8118SMike Christie 	default:
1086394f8118SMike Christie 		pr_err("Unknown PERSISTENT_RESERVE_IN SA: 0x%02x\n", sa);
1087394f8118SMike Christie 		return TCM_UNSUPPORTED_SCSI_OPCODE;
1088394f8118SMike Christie 	}
1089394f8118SMike Christie 
1090394f8118SMike Christie 	return ret;
1091394f8118SMike Christie }
1092394f8118SMike Christie 
iblock_get_alignment_offset_lbas(struct se_device * dev)1093394f8118SMike Christie static sector_t iblock_get_alignment_offset_lbas(struct se_device *dev)
10947f7caf6aSAndy Grover {
10957f7caf6aSAndy Grover 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
10967f7caf6aSAndy Grover 	struct block_device *bd = ib_dev->ibd_bd;
10977f7caf6aSAndy Grover 	int ret;
10987f7caf6aSAndy Grover 
10997f7caf6aSAndy Grover 	ret = bdev_alignment_offset(bd);
11007f7caf6aSAndy Grover 	if (ret == -1)
11017f7caf6aSAndy Grover 		return 0;
11027f7caf6aSAndy Grover 
11037f7caf6aSAndy Grover 	/* convert offset-bytes to offset-lbas */
11047f7caf6aSAndy Grover 	return ret / bdev_logical_block_size(bd);
11057f7caf6aSAndy Grover }
11067f7caf6aSAndy Grover 
iblock_get_lbppbe(struct se_device * dev)11077f7caf6aSAndy Grover static unsigned int iblock_get_lbppbe(struct se_device *dev)
11087f7caf6aSAndy Grover {
11097f7caf6aSAndy Grover 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
11107f7caf6aSAndy Grover 	struct block_device *bd = ib_dev->ibd_bd;
11117f7caf6aSAndy Grover 	unsigned int logs_per_phys =
1112c151eddbSChaitanya Kulkarni 		bdev_physical_block_size(bd) / bdev_logical_block_size(bd);
1113a2c6c6a3SChaitanya Kulkarni 
11147f7caf6aSAndy Grover 	return ilog2(logs_per_phys);
11157f7caf6aSAndy Grover }
11167f7caf6aSAndy Grover 
iblock_get_io_min(struct se_device * dev)11177f7caf6aSAndy Grover static unsigned int iblock_get_io_min(struct se_device *dev)
11187f7caf6aSAndy Grover {
11197f7caf6aSAndy Grover 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
11207f7caf6aSAndy Grover 	struct block_device *bd = ib_dev->ibd_bd;
11217f7caf6aSAndy Grover 
11227f7caf6aSAndy Grover 	return bdev_io_min(bd);
11237f7caf6aSAndy Grover }
11247f7caf6aSAndy Grover 
iblock_get_io_opt(struct se_device * dev)11257f7caf6aSAndy Grover static unsigned int iblock_get_io_opt(struct se_device *dev)
11267f7caf6aSAndy Grover {
11277f7caf6aSAndy Grover 	struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
11287f7caf6aSAndy Grover 	struct block_device *bd = ib_dev->ibd_bd;
11297f7caf6aSAndy Grover 
11307f7caf6aSAndy Grover 	return bdev_io_opt(bd);
11317f7caf6aSAndy Grover }
11327f7caf6aSAndy Grover 
11337f7caf6aSAndy Grover static struct exec_cmd_ops iblock_exec_cmd_ops = {
11340217da08SMike Christie 	.execute_rw		= iblock_execute_rw,
11350c2ad7d1SChristoph Hellwig 	.execute_sync_cache	= iblock_execute_sync_cache,
1136ad67f0d9SChristoph Hellwig 	.execute_write_same	= iblock_execute_write_same,
11376f974e8cSChristoph Hellwig 	.execute_unmap		= iblock_execute_unmap,
113814150a6bSChristoph Hellwig 	.execute_pr_out		= iblock_execute_pr_out,
1139394f8118SMike Christie 	.execute_pr_in		= iblock_execute_pr_in,
1140394f8118SMike Christie };
11410c2ad7d1SChristoph Hellwig 
11420c2ad7d1SChristoph Hellwig static sense_reason_t
iblock_parse_cdb(struct se_cmd * cmd)1143de103c93SChristoph Hellwig iblock_parse_cdb(struct se_cmd *cmd)
1144de103c93SChristoph Hellwig {
11450c2ad7d1SChristoph Hellwig 	return sbc_parse_cdb(cmd, &iblock_exec_cmd_ops);
11460217da08SMike Christie }
11470c2ad7d1SChristoph Hellwig 
iblock_get_write_cache(struct se_device * dev)11480c2ad7d1SChristoph Hellwig static bool iblock_get_write_cache(struct se_device *dev)
1149452e2010SRashika Kheria {
1150d0c8b259SNicholas Bellinger 	return bdev_write_cache(IBLOCK_DEV(dev)->ibd_bd);
115108e688fdSChristoph Hellwig }
1152d0c8b259SNicholas Bellinger 
1153d0c8b259SNicholas Bellinger static const struct target_backend_ops iblock_ops = {
11540a06d430SChristoph Hellwig 	.name			= "iblock",
1155c66ac9dbSNicholas Bellinger 	.inquiry_prod		= "IBLOCK",
11560fd97ccfSChristoph Hellwig 	.transport_flags_changeable = TRANSPORT_FLAG_PASSTHROUGH_PGR,
1157394f8118SMike Christie 	.inquiry_rev		= IBLOCK_VERSION,
11580fd97ccfSChristoph Hellwig 	.owner			= THIS_MODULE,
1159c66ac9dbSNicholas Bellinger 	.attach_hba		= iblock_attach_hba,
1160c66ac9dbSNicholas Bellinger 	.detach_hba		= iblock_detach_hba,
1161c66ac9dbSNicholas Bellinger 	.alloc_device		= iblock_alloc_device,
11620fd97ccfSChristoph Hellwig 	.configure_device	= iblock_configure_device,
11630fd97ccfSChristoph Hellwig 	.destroy_device		= iblock_destroy_device,
116492634706SMike Christie 	.free_device		= iblock_free_device,
1165c66ac9dbSNicholas Bellinger 	.configure_unmap	= iblock_configure_unmap,
1166d7c382c5SMike Christie 	.plug_device		= iblock_plug_device,
1167415ccd98SMike Christie 	.unplug_device		= iblock_unplug_device,
1168415ccd98SMike Christie 	.parse_cdb		= iblock_parse_cdb,
11690c2ad7d1SChristoph Hellwig 	.set_configfs_dev_params = iblock_set_configfs_dev_params,
1170c66ac9dbSNicholas Bellinger 	.show_configfs_dev_params = iblock_show_configfs_dev_params,
1171c66ac9dbSNicholas Bellinger 	.get_device_type	= sbc_get_device_type,
11726f23ac8aSChristoph Hellwig 	.get_blocks		= iblock_get_blocks,
1173c66ac9dbSNicholas Bellinger 	.get_alignment_offset_lbas = iblock_get_alignment_offset_lbas,
11747f7caf6aSAndy Grover 	.get_lbppbe		= iblock_get_lbppbe,
11757f7caf6aSAndy Grover 	.get_io_min		= iblock_get_io_min,
11767f7caf6aSAndy Grover 	.get_io_opt		= iblock_get_io_opt,
11777f7caf6aSAndy Grover 	.get_write_cache	= iblock_get_write_cache,
1178d0c8b259SNicholas Bellinger 	.tb_dev_attrib_attrs	= sbc_attrib_attrs,
11795873c4d1SChristoph Hellwig };
1180c66ac9dbSNicholas Bellinger 
iblock_module_init(void)1181c66ac9dbSNicholas Bellinger static int __init iblock_module_init(void)
1182c66ac9dbSNicholas Bellinger {
1183c66ac9dbSNicholas Bellinger 	return transport_backend_register(&iblock_ops);
11840a06d430SChristoph Hellwig }
1185c66ac9dbSNicholas Bellinger 
iblock_module_exit(void)1186c66ac9dbSNicholas Bellinger static void __exit iblock_module_exit(void)
118763b91d5aSAsias He {
1188c66ac9dbSNicholas Bellinger 	target_backend_unregister(&iblock_ops);
11890a06d430SChristoph Hellwig }
1190c66ac9dbSNicholas Bellinger 
1191c66ac9dbSNicholas Bellinger MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin");
1192c66ac9dbSNicholas Bellinger MODULE_AUTHOR("nab@Linux-iSCSI.org");
1193c66ac9dbSNicholas Bellinger MODULE_LICENSE("GPL");
1194c66ac9dbSNicholas Bellinger 
1195c66ac9dbSNicholas Bellinger module_init(iblock_module_init);
1196c66ac9dbSNicholas Bellinger module_exit(iblock_module_exit);
1197c66ac9dbSNicholas Bellinger