blob: 372d744315f38e971da84d1f4eca26a66666a158 [file] [log] [blame]
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001/*******************************************************************************
2 * Filename: target_core_iblock.c
3 *
4 * This file contains the Storage Engine <-> Linux BlockIO transport
5 * specific functions.
6 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07007 * (c) Copyright 2003-2013 Datera, Inc.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08008 *
9 * Nicholas A. Bellinger <nab@kernel.org>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 *
25 ******************************************************************************/
26
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080027#include <linux/string.h>
28#include <linux/parser.h>
29#include <linux/timer.h>
30#include <linux/fs.h>
31#include <linux/blkdev.h>
32#include <linux/slab.h>
33#include <linux/spinlock.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080034#include <linux/bio.h>
35#include <linux/genhd.h>
36#include <linux/file.h>
Paul Gortmaker827509e2011-08-30 14:20:44 -040037#include <linux/module.h>
Bart Van Asscheba929992015-05-08 10:11:12 +020038#include <scsi/scsi_proto.h>
Christoph Hellwig14150a62012-06-17 18:40:55 -040039#include <asm/unaligned.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080040
41#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050042#include <target/target_core_backend.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080043
44#include "target_core_iblock.h"
45
Christoph Hellwigd5b4a212011-12-21 14:20:31 -050046#define IBLOCK_MAX_BIO_PER_TASK 32 /* max # of bios to submit at a time */
47#define IBLOCK_BIO_POOL_SIZE 128
48
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -040049static inline struct iblock_dev *IBLOCK_DEV(struct se_device *dev)
50{
51 return container_of(dev, struct iblock_dev, dev);
52}
53
54
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080055static int iblock_attach_hba(struct se_hba *hba, u32 host_id)
56{
Andy Grover6708bb22011-06-08 10:36:43 -070057 pr_debug("CORE_HBA[%d] - TCM iBlock HBA Driver %s on"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080058 " Generic Target Core Stack %s\n", hba->hba_id,
Christoph Hellwigce8dd252015-06-19 15:14:39 +020059 IBLOCK_VERSION, TARGET_CORE_VERSION);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080060 return 0;
61}
62
63static void iblock_detach_hba(struct se_hba *hba)
64{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080065}
66
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -040067static struct se_device *iblock_alloc_device(struct se_hba *hba, const char *name)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080068{
69 struct iblock_dev *ib_dev = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080070
71 ib_dev = kzalloc(sizeof(struct iblock_dev), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -070072 if (!ib_dev) {
73 pr_err("Unable to allocate struct iblock_dev\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080074 return NULL;
75 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080076
Andy Grover6708bb22011-06-08 10:36:43 -070077 pr_debug( "IBLOCK: Allocated ib_dev for %s\n", name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080078
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -040079 return &ib_dev->dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080080}
81
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -040082static int iblock_configure_device(struct se_device *dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080083{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -040084 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080085 struct request_queue *q;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -040086 struct block_device *bd = NULL;
Nicholas Bellingerecebbf62013-12-23 20:31:24 +000087 struct blk_integrity *bi;
Andy Grover44bfd012012-06-07 10:38:51 -070088 fmode_t mode;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -040089 int ret = -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080090
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -040091 if (!(ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)) {
92 pr_err("Missing udev_path= parameters for IBLOCK\n");
93 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080094 }
Christoph Hellwigd5b4a212011-12-21 14:20:31 -050095
96 ib_dev->ibd_bio_set = bioset_create(IBLOCK_BIO_POOL_SIZE, 0);
Andy Grover6708bb22011-06-08 10:36:43 -070097 if (!ib_dev->ibd_bio_set) {
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -040098 pr_err("IBLOCK: Unable to create bioset\n");
99 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800100 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400101
Andy Grover6708bb22011-06-08 10:36:43 -0700102 pr_debug( "IBLOCK: Claiming struct block_device: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800103 ib_dev->ibd_udev_path);
104
Andy Grover44bfd012012-06-07 10:38:51 -0700105 mode = FMODE_READ|FMODE_EXCL;
106 if (!ib_dev->ibd_readonly)
107 mode |= FMODE_WRITE;
Nicholas Bellingereeeb9522015-09-15 17:27:35 -0700108 else
109 dev->dev_flags |= DF_READ_ONLY;
Andy Grover44bfd012012-06-07 10:38:51 -0700110
111 bd = blkdev_get_by_path(ib_dev->ibd_udev_path, mode, ib_dev);
Nicholas Bellinger613640e2011-03-14 04:05:59 -0700112 if (IS_ERR(bd)) {
113 ret = PTR_ERR(bd);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400114 goto out_free_bioset;
Nicholas Bellinger613640e2011-03-14 04:05:59 -0700115 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800116 ib_dev->ibd_bd = bd;
117
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400118 q = bdev_get_queue(bd);
119
120 dev->dev_attrib.hw_block_size = bdev_logical_block_size(bd);
Nicholas Bellinger046ba642015-01-06 16:10:37 -0800121 dev->dev_attrib.hw_max_sectors = queue_max_hw_sectors(q);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400122 dev->dev_attrib.hw_queue_depth = q->nr_requests;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800123
Mike Christieea263c72016-06-02 20:12:37 -0500124 if (target_configure_unmap_from_queue(&dev->dev_attrib, q))
Andy Grover6708bb22011-06-08 10:36:43 -0700125 pr_debug("IBLOCK: BLOCK Discard support available,"
Mike Christie8a9ebe72016-01-18 14:09:27 -0600126 " disabled by default\n");
127
Nicholas Bellingerf6970ad2012-11-07 20:08:38 -0800128 /*
129 * Enable write same emulation for IBLOCK and use 0xFFFF as
130 * the smaller WRITE_SAME(10) only has a two-byte block count.
131 */
132 dev->dev_attrib.max_write_same_len = 0xFFFF;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800133
Roland Dreiere22a7f02011-07-05 13:34:52 -0700134 if (blk_queue_nonrot(q))
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400135 dev->dev_attrib.is_nonrot = 1;
Nicholas Bellingerd0c8b252013-01-29 22:10:06 -0800136
Nicholas Bellingerecebbf62013-12-23 20:31:24 +0000137 bi = bdev_get_integrity(bd);
138 if (bi) {
139 struct bio_set *bs = ib_dev->ibd_bio_set;
140
Martin K. Petersen0f8087e2015-10-21 13:19:33 -0400141 if (!strcmp(bi->profile->name, "T10-DIF-TYPE3-IP") ||
142 !strcmp(bi->profile->name, "T10-DIF-TYPE1-IP")) {
Nicholas Bellingerecebbf62013-12-23 20:31:24 +0000143 pr_err("IBLOCK export of blk_integrity: %s not"
Martin K. Petersen0f8087e2015-10-21 13:19:33 -0400144 " supported\n", bi->profile->name);
Nicholas Bellingerecebbf62013-12-23 20:31:24 +0000145 ret = -ENOSYS;
146 goto out_blkdev_put;
147 }
148
Martin K. Petersen0f8087e2015-10-21 13:19:33 -0400149 if (!strcmp(bi->profile->name, "T10-DIF-TYPE3-CRC")) {
Nicholas Bellingerecebbf62013-12-23 20:31:24 +0000150 dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE3_PROT;
Martin K. Petersen0f8087e2015-10-21 13:19:33 -0400151 } else if (!strcmp(bi->profile->name, "T10-DIF-TYPE1-CRC")) {
Nicholas Bellingerecebbf62013-12-23 20:31:24 +0000152 dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE1_PROT;
153 }
154
155 if (dev->dev_attrib.pi_prot_type) {
156 if (bioset_integrity_create(bs, IBLOCK_BIO_POOL_SIZE) < 0) {
157 pr_err("Unable to allocate bioset for PI\n");
158 ret = -ENOMEM;
159 goto out_blkdev_put;
160 }
161 pr_debug("IBLOCK setup BIP bs->bio_integrity_pool: %p\n",
162 bs->bio_integrity_pool);
163 }
164 dev->dev_attrib.hw_pi_prot_type = dev->dev_attrib.pi_prot_type;
165 }
166
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400167 return 0;
Roland Dreiere22a7f02011-07-05 13:34:52 -0700168
Nicholas Bellingerecebbf62013-12-23 20:31:24 +0000169out_blkdev_put:
170 blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400171out_free_bioset:
172 bioset_free(ib_dev->ibd_bio_set);
173 ib_dev->ibd_bio_set = NULL;
174out:
175 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800176}
177
Nicholas Bellinger4cc987e2015-05-19 00:03:07 -0700178static void iblock_dev_call_rcu(struct rcu_head *p)
179{
180 struct se_device *dev = container_of(p, struct se_device, rcu_head);
181 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
182
183 kfree(ib_dev);
184}
185
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400186static void iblock_free_device(struct se_device *dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800187{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400188 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800189
Nicholas Bellingerbc665522011-02-09 15:34:38 -0800190 if (ib_dev->ibd_bd != NULL)
191 blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
Nicholas Bellingerd84287b2014-04-03 03:35:02 +0000192 if (ib_dev->ibd_bio_set != NULL)
Nicholas Bellingerbc665522011-02-09 15:34:38 -0800193 bioset_free(ib_dev->ibd_bio_set);
Nicholas Bellingerd84287b2014-04-03 03:35:02 +0000194
Nicholas Bellinger4cc987e2015-05-19 00:03:07 -0700195 call_rcu(&dev->rcu_head, iblock_dev_call_rcu);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800196}
197
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800198static unsigned long long iblock_emulate_read_cap_with_block_size(
199 struct se_device *dev,
200 struct block_device *bd,
201 struct request_queue *q)
202{
203 unsigned long long blocks_long = (div_u64(i_size_read(bd->bd_inode),
204 bdev_logical_block_size(bd)) - 1);
205 u32 block_size = bdev_logical_block_size(bd);
206
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400207 if (block_size == dev->dev_attrib.block_size)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800208 return blocks_long;
209
210 switch (block_size) {
211 case 4096:
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400212 switch (dev->dev_attrib.block_size) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800213 case 2048:
214 blocks_long <<= 1;
215 break;
216 case 1024:
217 blocks_long <<= 2;
218 break;
219 case 512:
220 blocks_long <<= 3;
221 default:
222 break;
223 }
224 break;
225 case 2048:
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400226 switch (dev->dev_attrib.block_size) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800227 case 4096:
228 blocks_long >>= 1;
229 break;
230 case 1024:
231 blocks_long <<= 1;
232 break;
233 case 512:
234 blocks_long <<= 2;
235 break;
236 default:
237 break;
238 }
239 break;
240 case 1024:
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400241 switch (dev->dev_attrib.block_size) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800242 case 4096:
243 blocks_long >>= 2;
244 break;
245 case 2048:
246 blocks_long >>= 1;
247 break;
248 case 512:
249 blocks_long <<= 1;
250 break;
251 default:
252 break;
253 }
254 break;
255 case 512:
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400256 switch (dev->dev_attrib.block_size) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800257 case 4096:
258 blocks_long >>= 3;
259 break;
260 case 2048:
261 blocks_long >>= 2;
262 break;
263 case 1024:
264 blocks_long >>= 1;
265 break;
266 default:
267 break;
268 }
269 break;
270 default:
271 break;
272 }
273
274 return blocks_long;
275}
276
Nicholas Bellinger3a41d852012-11-17 14:27:55 -0800277static void iblock_complete_cmd(struct se_cmd *cmd)
278{
279 struct iblock_req *ibr = cmd->priv;
280 u8 status;
281
282 if (!atomic_dec_and_test(&ibr->pending))
283 return;
284
285 if (atomic_read(&ibr->ib_bio_err_cnt))
286 status = SAM_STAT_CHECK_CONDITION;
287 else
288 status = SAM_STAT_GOOD;
289
290 target_complete_cmd(cmd, status);
291 kfree(ibr);
292}
293
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200294static void iblock_bio_done(struct bio *bio)
Nicholas Bellinger3a41d852012-11-17 14:27:55 -0800295{
296 struct se_cmd *cmd = bio->bi_private;
297 struct iblock_req *ibr = cmd->priv;
298
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200299 if (bio->bi_error) {
300 pr_err("bio error: %p, err: %d\n", bio, bio->bi_error);
Nicholas Bellinger3a41d852012-11-17 14:27:55 -0800301 /*
302 * Bump the ib_bio_err_cnt and release bio.
303 */
304 atomic_inc(&ibr->ib_bio_err_cnt);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100305 smp_mb__after_atomic();
Nicholas Bellinger3a41d852012-11-17 14:27:55 -0800306 }
307
308 bio_put(bio);
309
310 iblock_complete_cmd(cmd);
311}
312
313static struct bio *
Mike Christiee742fc32016-06-05 14:32:08 -0500314iblock_get_bio(struct se_cmd *cmd, sector_t lba, u32 sg_num, int op,
315 int op_flags)
Nicholas Bellinger3a41d852012-11-17 14:27:55 -0800316{
317 struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
318 struct bio *bio;
319
320 /*
321 * Only allocate as many vector entries as the bio code allows us to,
322 * we'll loop later on until we have handled the whole request.
323 */
324 if (sg_num > BIO_MAX_PAGES)
325 sg_num = BIO_MAX_PAGES;
326
327 bio = bio_alloc_bioset(GFP_NOIO, sg_num, ib_dev->ibd_bio_set);
328 if (!bio) {
329 pr_err("Unable to allocate memory for bio\n");
330 return NULL;
331 }
332
333 bio->bi_bdev = ib_dev->ibd_bd;
334 bio->bi_private = cmd;
335 bio->bi_end_io = &iblock_bio_done;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700336 bio->bi_iter.bi_sector = lba;
Mike Christiee742fc32016-06-05 14:32:08 -0500337 bio_set_op_attrs(bio, op, op_flags);
Nicholas Bellinger3a41d852012-11-17 14:27:55 -0800338
339 return bio;
340}
341
Mike Christie4e49ea42016-06-05 14:31:41 -0500342static void iblock_submit_bios(struct bio_list *list)
Nicholas Bellinger3a41d852012-11-17 14:27:55 -0800343{
344 struct blk_plug plug;
345 struct bio *bio;
346
347 blk_start_plug(&plug);
348 while ((bio = bio_list_pop(list)))
Mike Christie4e49ea42016-06-05 14:31:41 -0500349 submit_bio(bio);
Nicholas Bellinger3a41d852012-11-17 14:27:55 -0800350 blk_finish_plug(&plug);
351}
352
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200353static void iblock_end_io_flush(struct bio *bio)
Christoph Hellwigdf5fa692011-10-14 07:29:58 -0400354{
355 struct se_cmd *cmd = bio->bi_private;
356
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200357 if (bio->bi_error)
358 pr_err("IBLOCK: cache flush failed: %d\n", bio->bi_error);
Christoph Hellwigdf5fa692011-10-14 07:29:58 -0400359
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400360 if (cmd) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200361 if (bio->bi_error)
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400362 target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800363 else
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400364 target_complete_cmd(cmd, SAM_STAT_GOOD);
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400365 }
366
Christoph Hellwigdf5fa692011-10-14 07:29:58 -0400367 bio_put(bio);
368}
369
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800370/*
Christoph Hellwigdf5fa692011-10-14 07:29:58 -0400371 * Implement SYCHRONIZE CACHE. Note that we can't handle lba ranges and must
372 * always flush the whole cache.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800373 */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800374static sense_reason_t
375iblock_execute_sync_cache(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800376{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400377 struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
Andy Grovera1d8b492011-05-02 17:12:10 -0700378 int immed = (cmd->t_task_cdb[1] & 0x2);
Christoph Hellwigdf5fa692011-10-14 07:29:58 -0400379 struct bio *bio;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800380
381 /*
382 * If the Immediate bit is set, queue up the GOOD response
Christoph Hellwigdf5fa692011-10-14 07:29:58 -0400383 * for this SYNCHRONIZE_CACHE op.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800384 */
385 if (immed)
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400386 target_complete_cmd(cmd, SAM_STAT_GOOD);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800387
Christoph Hellwigdf5fa692011-10-14 07:29:58 -0400388 bio = bio_alloc(GFP_KERNEL, 0);
389 bio->bi_end_io = iblock_end_io_flush;
390 bio->bi_bdev = ib_dev->ibd_bd;
Jens Axboe31c64f72016-08-01 09:39:23 -0600391 bio_set_op_attrs(bio, REQ_OP_WRITE, WRITE_FLUSH);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800392 if (!immed)
Christoph Hellwigdf5fa692011-10-14 07:29:58 -0400393 bio->bi_private = cmd;
Mike Christie4e49ea42016-06-05 14:31:41 -0500394 submit_bio(bio);
Christoph Hellwigad67f0d2012-06-17 18:40:53 -0400395 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800396}
397
Christoph Hellwigde103c92012-11-06 12:24:09 -0800398static sense_reason_t
Christoph Hellwig62e46942015-06-19 15:10:59 +0200399iblock_execute_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb)
Asias Hedbc21c52013-02-25 14:03:45 +0800400{
Christoph Hellwig62e46942015-06-19 15:10:59 +0200401 struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd;
Mike Christie8a9ebe72016-01-18 14:09:27 -0600402 struct se_device *dev = cmd->se_dev;
Asias Hedbc21c52013-02-25 14:03:45 +0800403 int ret;
404
Mike Christie8a9ebe72016-01-18 14:09:27 -0600405 ret = blkdev_issue_discard(bdev,
406 target_to_linux_sector(dev, lba),
407 target_to_linux_sector(dev, nolb),
408 GFP_KERNEL, 0);
Asias Hedbc21c52013-02-25 14:03:45 +0800409 if (ret < 0) {
410 pr_err("blkdev_issue_discard() failed: %d\n", ret);
411 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
412 }
413
414 return 0;
415}
416
417static sense_reason_t
Mike Christie07b63192016-02-24 13:56:33 -0600418iblock_execute_write_same_direct(struct block_device *bdev, struct se_cmd *cmd)
419{
420 struct se_device *dev = cmd->se_dev;
421 struct scatterlist *sg = &cmd->t_data_sg[0];
422 struct page *page = NULL;
423 int ret;
424
425 if (sg->offset) {
426 page = alloc_page(GFP_KERNEL);
427 if (!page)
428 return TCM_OUT_OF_RESOURCES;
429 sg_copy_to_buffer(sg, cmd->t_data_nents, page_address(page),
430 dev->dev_attrib.block_size);
431 }
432
433 ret = blkdev_issue_write_same(bdev,
434 target_to_linux_sector(dev, cmd->t_task_lba),
435 target_to_linux_sector(dev,
436 sbc_get_write_same_sectors(cmd)),
437 GFP_KERNEL, page ? page : sg_page(sg));
438 if (page)
439 __free_page(page);
440 if (ret)
441 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
442
443 target_complete_cmd(cmd, GOOD);
444 return 0;
445}
446
447static sense_reason_t
Nicholas Bellingerf6970ad2012-11-07 20:08:38 -0800448iblock_execute_write_same(struct se_cmd *cmd)
449{
Mike Christie07b63192016-02-24 13:56:33 -0600450 struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd;
Nicholas Bellingerf6970ad2012-11-07 20:08:38 -0800451 struct iblock_req *ibr;
452 struct scatterlist *sg;
453 struct bio *bio;
454 struct bio_list list;
Mike Christie8a9ebe72016-01-18 14:09:27 -0600455 struct se_device *dev = cmd->se_dev;
456 sector_t block_lba = target_to_linux_sector(dev, cmd->t_task_lba);
457 sector_t sectors = target_to_linux_sector(dev,
458 sbc_get_write_same_sectors(cmd));
Nicholas Bellingerf6970ad2012-11-07 20:08:38 -0800459
Nicholas Bellingerafd73f12015-02-14 01:32:11 +0000460 if (cmd->prot_op) {
461 pr_err("WRITE_SAME: Protection information with IBLOCK"
462 " backends not supported\n");
463 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
464 }
Nicholas Bellingerf6970ad2012-11-07 20:08:38 -0800465 sg = &cmd->t_data_sg[0];
466
467 if (cmd->t_data_nents > 1 ||
468 sg->length != cmd->se_dev->dev_attrib.block_size) {
469 pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u"
470 " block_size: %u\n", cmd->t_data_nents, sg->length,
471 cmd->se_dev->dev_attrib.block_size);
472 return TCM_INVALID_CDB_FIELD;
473 }
474
Mike Christie07b63192016-02-24 13:56:33 -0600475 if (bdev_write_same(bdev))
476 return iblock_execute_write_same_direct(bdev, cmd);
477
Nicholas Bellingerf6970ad2012-11-07 20:08:38 -0800478 ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
479 if (!ibr)
480 goto fail;
481 cmd->priv = ibr;
482
Mike Christiee742fc32016-06-05 14:32:08 -0500483 bio = iblock_get_bio(cmd, block_lba, 1, REQ_OP_WRITE, 0);
Nicholas Bellingerf6970ad2012-11-07 20:08:38 -0800484 if (!bio)
485 goto fail_free_ibr;
486
487 bio_list_init(&list);
488 bio_list_add(&list, bio);
489
490 atomic_set(&ibr->pending, 1);
491
492 while (sectors) {
493 while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
494 != sg->length) {
495
Mike Christiee742fc32016-06-05 14:32:08 -0500496 bio = iblock_get_bio(cmd, block_lba, 1, REQ_OP_WRITE,
497 0);
Nicholas Bellingerf6970ad2012-11-07 20:08:38 -0800498 if (!bio)
499 goto fail_put_bios;
500
501 atomic_inc(&ibr->pending);
502 bio_list_add(&list, bio);
503 }
504
505 /* Always in 512 byte units for Linux/Block */
506 block_lba += sg->length >> IBLOCK_LBA_SHIFT;
507 sectors -= 1;
508 }
509
Mike Christie4e49ea42016-06-05 14:31:41 -0500510 iblock_submit_bios(&list);
Nicholas Bellingerf6970ad2012-11-07 20:08:38 -0800511 return 0;
512
513fail_put_bios:
514 while ((bio = bio_list_pop(&list)))
515 bio_put(bio);
516fail_free_ibr:
517 kfree(ibr);
518fail:
519 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
520}
521
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800522enum {
Andy Grover44bfd012012-06-07 10:38:51 -0700523 Opt_udev_path, Opt_readonly, Opt_force, Opt_err
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800524};
525
526static match_table_t tokens = {
527 {Opt_udev_path, "udev_path=%s"},
Andy Grover44bfd012012-06-07 10:38:51 -0700528 {Opt_readonly, "readonly=%d"},
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800529 {Opt_force, "force=%d"},
530 {Opt_err, NULL}
531};
532
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400533static ssize_t iblock_set_configfs_dev_params(struct se_device *dev,
534 const char *page, ssize_t count)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800535{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400536 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
Jesper Juhl6d180252011-03-14 04:05:56 -0700537 char *orig, *ptr, *arg_p, *opts;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800538 substring_t args[MAX_OPT_ARGS];
Roland Dreier21bca312011-07-05 15:35:02 -0700539 int ret = 0, token;
Andy Grover44bfd012012-06-07 10:38:51 -0700540 unsigned long tmp_readonly;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800541
542 opts = kstrdup(page, GFP_KERNEL);
543 if (!opts)
544 return -ENOMEM;
545
546 orig = opts;
547
Sebastian Andrzej Siewior90c161b2011-11-23 20:53:17 +0100548 while ((ptr = strsep(&opts, ",\n")) != NULL) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800549 if (!*ptr)
550 continue;
551
552 token = match_token(ptr, tokens, args);
553 switch (token) {
554 case Opt_udev_path:
555 if (ib_dev->ibd_bd) {
Andy Grover6708bb22011-06-08 10:36:43 -0700556 pr_err("Unable to set udev_path= while"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800557 " ib_dev->ibd_bd exists\n");
558 ret = -EEXIST;
559 goto out;
560 }
Nicholas Bellinger852b6ed2012-08-22 18:45:11 -0700561 if (match_strlcpy(ib_dev->ibd_udev_path, &args[0],
562 SE_UDEV_PATH_LEN) == 0) {
563 ret = -EINVAL;
Jesper Juhl6d180252011-03-14 04:05:56 -0700564 break;
565 }
Andy Grover6708bb22011-06-08 10:36:43 -0700566 pr_debug("IBLOCK: Referencing UDEV path: %s\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800567 ib_dev->ibd_udev_path);
568 ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH;
569 break;
Andy Grover44bfd012012-06-07 10:38:51 -0700570 case Opt_readonly:
571 arg_p = match_strdup(&args[0]);
572 if (!arg_p) {
573 ret = -ENOMEM;
574 break;
575 }
Jingoo Han57103d72013-07-19 16:22:19 +0900576 ret = kstrtoul(arg_p, 0, &tmp_readonly);
Andy Grover44bfd012012-06-07 10:38:51 -0700577 kfree(arg_p);
578 if (ret < 0) {
Jingoo Han57103d72013-07-19 16:22:19 +0900579 pr_err("kstrtoul() failed for"
Andy Grover44bfd012012-06-07 10:38:51 -0700580 " readonly=\n");
581 goto out;
582 }
583 ib_dev->ibd_readonly = tmp_readonly;
584 pr_debug("IBLOCK: readonly: %d\n", ib_dev->ibd_readonly);
585 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800586 case Opt_force:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800587 break;
588 default:
589 break;
590 }
591 }
592
593out:
594 kfree(orig);
595 return (!ret) ? count : ret;
596}
597
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400598static ssize_t iblock_show_configfs_dev_params(struct se_device *dev, char *b)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800599{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400600 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
601 struct block_device *bd = ib_dev->ibd_bd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800602 char buf[BDEVNAME_SIZE];
603 ssize_t bl = 0;
604
605 if (bd)
606 bl += sprintf(b + bl, "iBlock device: %s",
607 bdevname(bd, buf));
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400608 if (ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)
Andy Grover44bfd012012-06-07 10:38:51 -0700609 bl += sprintf(b + bl, " UDEV PATH: %s",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400610 ib_dev->ibd_udev_path);
611 bl += sprintf(b + bl, " readonly: %d\n", ib_dev->ibd_readonly);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800612
613 bl += sprintf(b + bl, " ");
614 if (bd) {
615 bl += sprintf(b + bl, "Major: %d Minor: %d %s\n",
Roland Dreier21bca312011-07-05 15:35:02 -0700616 MAJOR(bd->bd_dev), MINOR(bd->bd_dev), (!bd->bd_contains) ?
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400617 "" : (bd->bd_holder == ib_dev) ?
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800618 "CLAIMED: IBLOCK" : "CLAIMED: OS");
619 } else {
Roland Dreier21bca312011-07-05 15:35:02 -0700620 bl += sprintf(b + bl, "Major: 0 Minor: 0\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800621 }
622
623 return bl;
624}
625
Nicholas Bellingerecebbf62013-12-23 20:31:24 +0000626static int
627iblock_alloc_bip(struct se_cmd *cmd, struct bio *bio)
628{
629 struct se_device *dev = cmd->se_dev;
630 struct blk_integrity *bi;
631 struct bio_integrity_payload *bip;
632 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
633 struct scatterlist *sg;
634 int i, rc;
635
636 bi = bdev_get_integrity(ib_dev->ibd_bd);
637 if (!bi) {
638 pr_err("Unable to locate bio_integrity\n");
639 return -ENODEV;
640 }
641
642 bip = bio_integrity_alloc(bio, GFP_NOIO, cmd->t_prot_nents);
Keith Busch06c1e392015-12-03 09:32:21 -0700643 if (IS_ERR(bip)) {
Nicholas Bellingerecebbf62013-12-23 20:31:24 +0000644 pr_err("Unable to allocate bio_integrity_payload\n");
Keith Busch06c1e392015-12-03 09:32:21 -0700645 return PTR_ERR(bip);
Nicholas Bellingerecebbf62013-12-23 20:31:24 +0000646 }
647
Linus Torvalds4e13c5d2014-01-31 15:31:23 -0800648 bip->bip_iter.bi_size = (cmd->data_length / dev->dev_attrib.block_size) *
Nicholas Bellingerecebbf62013-12-23 20:31:24 +0000649 dev->prot_length;
Linus Torvalds4e13c5d2014-01-31 15:31:23 -0800650 bip->bip_iter.bi_sector = bio->bi_iter.bi_sector;
Nicholas Bellingerecebbf62013-12-23 20:31:24 +0000651
Linus Torvalds4e13c5d2014-01-31 15:31:23 -0800652 pr_debug("IBLOCK BIP Size: %u Sector: %llu\n", bip->bip_iter.bi_size,
653 (unsigned long long)bip->bip_iter.bi_sector);
Nicholas Bellingerecebbf62013-12-23 20:31:24 +0000654
655 for_each_sg(cmd->t_prot_sg, sg, cmd->t_prot_nents, i) {
656
657 rc = bio_integrity_add_page(bio, sg_page(sg), sg->length,
658 sg->offset);
659 if (rc != sg->length) {
660 pr_err("bio_integrity_add_page() failed; %d\n", rc);
661 return -ENOMEM;
662 }
663
664 pr_debug("Added bio integrity page: %p length: %d offset; %d\n",
665 sg_page(sg), sg->length, sg->offset);
666 }
667
668 return 0;
669}
670
Christoph Hellwigde103c92012-11-06 12:24:09 -0800671static sense_reason_t
Nicholas Bellingera82a9532013-08-19 23:57:30 -0700672iblock_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
673 enum dma_data_direction data_direction)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800674{
Andy Grover5951146d2011-07-19 10:26:37 +0000675 struct se_device *dev = cmd->se_dev;
Mike Christie8a9ebe72016-01-18 14:09:27 -0600676 sector_t block_lba = target_to_linux_sector(dev, cmd->t_task_lba);
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400677 struct iblock_req *ibr;
Nicholas Bellingerecebbf62013-12-23 20:31:24 +0000678 struct bio *bio, *bio_start;
Christoph Hellwigdbbf3e92011-09-25 14:56:24 -0400679 struct bio_list list;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800680 struct scatterlist *sg;
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400681 u32 sg_num = sgl_nents;
Christoph Hellwigd5b4a212011-12-21 14:20:31 -0500682 unsigned bio_cnt;
Mike Christiee742fc32016-06-05 14:32:08 -0500683 int i, op, op_flags = 0;
Christoph Hellwigdbbf3e92011-09-25 14:56:24 -0400684
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400685 if (data_direction == DMA_TO_DEVICE) {
Nicholas Bellingerd0c8b252013-01-29 22:10:06 -0800686 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
687 struct request_queue *q = bdev_get_queue(ib_dev->ibd_bd);
Christoph Hellwigdbbf3e92011-09-25 14:56:24 -0400688 /*
Nicholas Bellingerd0c8b252013-01-29 22:10:06 -0800689 * Force writethrough using WRITE_FUA if a volatile write cache
690 * is not enabled, or if initiator set the Force Unit Access bit.
Christoph Hellwigdbbf3e92011-09-25 14:56:24 -0400691 */
Mike Christiee742fc32016-06-05 14:32:08 -0500692 op = REQ_OP_WRITE;
Jens Axboec888a8f2016-04-13 13:33:19 -0600693 if (test_bit(QUEUE_FLAG_FUA, &q->queue_flags)) {
Nicholas Bellingerd0c8b252013-01-29 22:10:06 -0800694 if (cmd->se_cmd_flags & SCF_FUA)
Mike Christiee742fc32016-06-05 14:32:08 -0500695 op_flags = WRITE_FUA;
Jens Axboec888a8f2016-04-13 13:33:19 -0600696 else if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags))
Mike Christiee742fc32016-06-05 14:32:08 -0500697 op_flags = WRITE_FUA;
Nicholas Bellingerd0c8b252013-01-29 22:10:06 -0800698 }
Christoph Hellwigdbbf3e92011-09-25 14:56:24 -0400699 } else {
Mike Christiee742fc32016-06-05 14:32:08 -0500700 op = REQ_OP_READ;
Christoph Hellwigdbbf3e92011-09-25 14:56:24 -0400701 }
702
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400703 ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
704 if (!ibr)
705 goto fail;
706 cmd->priv = ibr;
707
Paolo Bonzinie0de4452012-09-07 17:30:41 +0200708 if (!sgl_nents) {
709 atomic_set(&ibr->pending, 1);
710 iblock_complete_cmd(cmd);
711 return 0;
712 }
713
Mike Christiee742fc32016-06-05 14:32:08 -0500714 bio = iblock_get_bio(cmd, block_lba, sgl_nents, op, op_flags);
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400715 if (!bio)
716 goto fail_free_ibr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800717
Nicholas Bellingerecebbf62013-12-23 20:31:24 +0000718 bio_start = bio;
Christoph Hellwigdbbf3e92011-09-25 14:56:24 -0400719 bio_list_init(&list);
720 bio_list_add(&list, bio);
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400721
722 atomic_set(&ibr->pending, 2);
Christoph Hellwigd5b4a212011-12-21 14:20:31 -0500723 bio_cnt = 1;
Christoph Hellwigdbbf3e92011-09-25 14:56:24 -0400724
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400725 for_each_sg(sgl, sg, sgl_nents, i) {
Christoph Hellwigdbbf3e92011-09-25 14:56:24 -0400726 /*
727 * XXX: if the length the device accepts is shorter than the
728 * length of the S/G list entry this will cause and
729 * endless loop. Better hope no driver uses huge pages.
730 */
731 while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
732 != sg->length) {
Christoph Hellwigd5b4a212011-12-21 14:20:31 -0500733 if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) {
Mike Christie4e49ea42016-06-05 14:31:41 -0500734 iblock_submit_bios(&list);
Christoph Hellwigd5b4a212011-12-21 14:20:31 -0500735 bio_cnt = 0;
736 }
737
Mike Christiee742fc32016-06-05 14:32:08 -0500738 bio = iblock_get_bio(cmd, block_lba, sg_num, op,
739 op_flags);
Andy Grover6708bb22011-06-08 10:36:43 -0700740 if (!bio)
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400741 goto fail_put_bios;
742
743 atomic_inc(&ibr->pending);
Christoph Hellwigdbbf3e92011-09-25 14:56:24 -0400744 bio_list_add(&list, bio);
Christoph Hellwigd5b4a212011-12-21 14:20:31 -0500745 bio_cnt++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800746 }
Christoph Hellwigdbbf3e92011-09-25 14:56:24 -0400747
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800748 /* Always in 512 byte units for Linux/Block */
749 block_lba += sg->length >> IBLOCK_LBA_SHIFT;
750 sg_num--;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800751 }
752
Nicholas Bellinger6f16ec42015-03-27 23:14:16 -0700753 if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
Nicholas Bellingerecebbf62013-12-23 20:31:24 +0000754 int rc = iblock_alloc_bip(cmd, bio_start);
755 if (rc)
756 goto fail_put_bios;
757 }
758
Mike Christie4e49ea42016-06-05 14:31:41 -0500759 iblock_submit_bios(&list);
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400760 iblock_complete_cmd(cmd);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700761 return 0;
Christoph Hellwigdbbf3e92011-09-25 14:56:24 -0400762
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400763fail_put_bios:
Christoph Hellwigdbbf3e92011-09-25 14:56:24 -0400764 while ((bio = bio_list_pop(&list)))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800765 bio_put(bio);
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400766fail_free_ibr:
767 kfree(ibr);
Christoph Hellwig5787cac2012-04-24 00:25:06 -0400768fail:
Christoph Hellwigde103c92012-11-06 12:24:09 -0800769 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800770}
771
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800772static sector_t iblock_get_blocks(struct se_device *dev)
773{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400774 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
775 struct block_device *bd = ib_dev->ibd_bd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800776 struct request_queue *q = bdev_get_queue(bd);
777
778 return iblock_emulate_read_cap_with_block_size(dev, bd, q);
779}
780
Andy Grover7f7caf62013-11-11 08:59:17 -0800781static sector_t iblock_get_alignment_offset_lbas(struct se_device *dev)
782{
783 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
784 struct block_device *bd = ib_dev->ibd_bd;
785 int ret;
786
787 ret = bdev_alignment_offset(bd);
788 if (ret == -1)
789 return 0;
790
791 /* convert offset-bytes to offset-lbas */
792 return ret / bdev_logical_block_size(bd);
793}
794
795static unsigned int iblock_get_lbppbe(struct se_device *dev)
796{
797 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
798 struct block_device *bd = ib_dev->ibd_bd;
799 int logs_per_phys = bdev_physical_block_size(bd) / bdev_logical_block_size(bd);
800
801 return ilog2(logs_per_phys);
802}
803
804static unsigned int iblock_get_io_min(struct se_device *dev)
805{
806 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
807 struct block_device *bd = ib_dev->ibd_bd;
808
809 return bdev_io_min(bd);
810}
811
812static unsigned int iblock_get_io_opt(struct se_device *dev)
813{
814 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
815 struct block_device *bd = ib_dev->ibd_bd;
816
817 return bdev_io_opt(bd);
818}
819
Christoph Hellwig9e999a62012-10-07 10:55:50 -0400820static struct sbc_ops iblock_sbc_ops = {
Christoph Hellwig0c2ad7d2012-06-17 18:40:52 -0400821 .execute_rw = iblock_execute_rw,
Christoph Hellwigad67f0d2012-06-17 18:40:53 -0400822 .execute_sync_cache = iblock_execute_sync_cache,
Christoph Hellwig6f974e82012-06-17 18:40:54 -0400823 .execute_write_same = iblock_execute_write_same,
Christoph Hellwig14150a62012-06-17 18:40:55 -0400824 .execute_unmap = iblock_execute_unmap,
Christoph Hellwig0c2ad7d2012-06-17 18:40:52 -0400825};
826
Christoph Hellwigde103c92012-11-06 12:24:09 -0800827static sense_reason_t
828iblock_parse_cdb(struct se_cmd *cmd)
Christoph Hellwig0c2ad7d2012-06-17 18:40:52 -0400829{
Christoph Hellwig9e999a62012-10-07 10:55:50 -0400830 return sbc_parse_cdb(cmd, &iblock_sbc_ops);
Christoph Hellwig0c2ad7d2012-06-17 18:40:52 -0400831}
832
Rashika Kheria452e2012013-12-18 23:56:44 +0530833static bool iblock_get_write_cache(struct se_device *dev)
Nicholas Bellingerd0c8b252013-01-29 22:10:06 -0800834{
835 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
836 struct block_device *bd = ib_dev->ibd_bd;
837 struct request_queue *q = bdev_get_queue(bd);
838
Jens Axboec888a8f2016-04-13 13:33:19 -0600839 return test_bit(QUEUE_FLAG_WC, &q->queue_flags);
Nicholas Bellingerd0c8b252013-01-29 22:10:06 -0800840}
841
Christoph Hellwig0a06d432015-05-10 18:14:56 +0200842static const struct target_backend_ops iblock_ops = {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800843 .name = "iblock",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400844 .inquiry_prod = "IBLOCK",
845 .inquiry_rev = IBLOCK_VERSION,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800846 .owner = THIS_MODULE,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800847 .attach_hba = iblock_attach_hba,
848 .detach_hba = iblock_detach_hba,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400849 .alloc_device = iblock_alloc_device,
850 .configure_device = iblock_configure_device,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800851 .free_device = iblock_free_device,
Christoph Hellwig0c2ad7d2012-06-17 18:40:52 -0400852 .parse_cdb = iblock_parse_cdb,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800853 .set_configfs_dev_params = iblock_set_configfs_dev_params,
854 .show_configfs_dev_params = iblock_show_configfs_dev_params,
Christoph Hellwig6f23ac82012-10-07 10:55:53 -0400855 .get_device_type = sbc_get_device_type,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800856 .get_blocks = iblock_get_blocks,
Andy Grover7f7caf62013-11-11 08:59:17 -0800857 .get_alignment_offset_lbas = iblock_get_alignment_offset_lbas,
858 .get_lbppbe = iblock_get_lbppbe,
859 .get_io_min = iblock_get_io_min,
860 .get_io_opt = iblock_get_io_opt,
Nicholas Bellingerd0c8b252013-01-29 22:10:06 -0800861 .get_write_cache = iblock_get_write_cache,
Christoph Hellwig5873c4d2015-05-10 18:14:57 +0200862 .tb_dev_attrib_attrs = sbc_attrib_attrs,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800863};
864
865static int __init iblock_module_init(void)
866{
Christoph Hellwig0a06d432015-05-10 18:14:56 +0200867 return transport_backend_register(&iblock_ops);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800868}
869
Asias He63b91d52013-02-27 12:50:56 +0800870static void __exit iblock_module_exit(void)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800871{
Christoph Hellwig0a06d432015-05-10 18:14:56 +0200872 target_backend_unregister(&iblock_ops);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800873}
874
875MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin");
876MODULE_AUTHOR("nab@Linux-iSCSI.org");
877MODULE_LICENSE("GPL");
878
879module_init(iblock_module_init);
880module_exit(iblock_module_exit);