target: Fix reading of data length fields for UNMAP commands
[deliverable/linux.git] / drivers / target / target_core_iblock.c
CommitLineData
c66ac9db
NB
1/*******************************************************************************
2 * Filename: target_core_iblock.c
3 *
4 * This file contains the Storage Engine <-> Linux BlockIO transport
5 * specific functions.
6 *
7 * Copyright (c) 2003, 2004, 2005 PyX Technologies, Inc.
8 * Copyright (c) 2005, 2006, 2007 SBE, Inc.
9 * Copyright (c) 2007-2010 Rising Tide Systems
10 * Copyright (c) 2008-2010 Linux-iSCSI.org
11 *
12 * Nicholas A. Bellinger <nab@kernel.org>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 *
28 ******************************************************************************/
29
c66ac9db
NB
30#include <linux/string.h>
31#include <linux/parser.h>
32#include <linux/timer.h>
33#include <linux/fs.h>
34#include <linux/blkdev.h>
35#include <linux/slab.h>
36#include <linux/spinlock.h>
c66ac9db
NB
37#include <linux/bio.h>
38#include <linux/genhd.h>
39#include <linux/file.h>
827509e3 40#include <linux/module.h>
c66ac9db
NB
41#include <scsi/scsi.h>
42#include <scsi/scsi_host.h>
14150a6b 43#include <asm/unaligned.h>
c66ac9db
NB
44
45#include <target/target_core_base.h>
c4795fb2 46#include <target/target_core_backend.h>
c66ac9db
NB
47
48#include "target_core_iblock.h"
49
d5b4a21b
CH
50#define IBLOCK_MAX_BIO_PER_TASK 32 /* max # of bios to submit at a time */
51#define IBLOCK_BIO_POOL_SIZE 128
52
c66ac9db
NB
53static struct se_subsystem_api iblock_template;
54
55static void iblock_bio_done(struct bio *, int);
56
57/* iblock_attach_hba(): (Part of se_subsystem_api_t template)
58 *
59 *
60 */
61static int iblock_attach_hba(struct se_hba *hba, u32 host_id)
62{
6708bb27 63 pr_debug("CORE_HBA[%d] - TCM iBlock HBA Driver %s on"
c66ac9db
NB
64 " Generic Target Core Stack %s\n", hba->hba_id,
65 IBLOCK_VERSION, TARGET_CORE_MOD_VERSION);
c66ac9db
NB
66 return 0;
67}
68
69static void iblock_detach_hba(struct se_hba *hba)
70{
c66ac9db
NB
71}
72
73static void *iblock_allocate_virtdevice(struct se_hba *hba, const char *name)
74{
75 struct iblock_dev *ib_dev = NULL;
c66ac9db
NB
76
77 ib_dev = kzalloc(sizeof(struct iblock_dev), GFP_KERNEL);
6708bb27
AG
78 if (!ib_dev) {
79 pr_err("Unable to allocate struct iblock_dev\n");
c66ac9db
NB
80 return NULL;
81 }
c66ac9db 82
6708bb27 83 pr_debug( "IBLOCK: Allocated ib_dev for %s\n", name);
c66ac9db
NB
84
85 return ib_dev;
86}
87
88static struct se_device *iblock_create_virtdevice(
89 struct se_hba *hba,
90 struct se_subsystem_dev *se_dev,
91 void *p)
92{
93 struct iblock_dev *ib_dev = p;
94 struct se_device *dev;
95 struct se_dev_limits dev_limits;
96 struct block_device *bd = NULL;
97 struct request_queue *q;
98 struct queue_limits *limits;
99 u32 dev_flags = 0;
44bfd018 100 fmode_t mode;
613640e4 101 int ret = -EINVAL;
c66ac9db 102
6708bb27
AG
103 if (!ib_dev) {
104 pr_err("Unable to locate struct iblock_dev parameter\n");
613640e4 105 return ERR_PTR(ret);
c66ac9db
NB
106 }
107 memset(&dev_limits, 0, sizeof(struct se_dev_limits));
d5b4a21b
CH
108
109 ib_dev->ibd_bio_set = bioset_create(IBLOCK_BIO_POOL_SIZE, 0);
6708bb27
AG
110 if (!ib_dev->ibd_bio_set) {
111 pr_err("IBLOCK: Unable to create bioset()\n");
613640e4 112 return ERR_PTR(-ENOMEM);
c66ac9db 113 }
6708bb27 114 pr_debug("IBLOCK: Created bio_set()\n");
c66ac9db
NB
115 /*
116 * iblock_check_configfs_dev_params() ensures that ib_dev->ibd_udev_path
117 * must already have been set in order for echo 1 > $HBA/$DEV/enable to run.
118 */
6708bb27 119 pr_debug( "IBLOCK: Claiming struct block_device: %s\n",
c66ac9db
NB
120 ib_dev->ibd_udev_path);
121
44bfd018
AG
122 mode = FMODE_READ|FMODE_EXCL;
123 if (!ib_dev->ibd_readonly)
124 mode |= FMODE_WRITE;
125
126 bd = blkdev_get_by_path(ib_dev->ibd_udev_path, mode, ib_dev);
613640e4
NB
127 if (IS_ERR(bd)) {
128 ret = PTR_ERR(bd);
c66ac9db 129 goto failed;
613640e4 130 }
c66ac9db
NB
131 /*
132 * Setup the local scope queue_limits from struct request_queue->limits
133 * to pass into transport_add_device_to_core_hba() as struct se_dev_limits.
134 */
135 q = bdev_get_queue(bd);
136 limits = &dev_limits.limits;
137 limits->logical_block_size = bdev_logical_block_size(bd);
d5b4a21b
CH
138 limits->max_hw_sectors = UINT_MAX;
139 limits->max_sectors = UINT_MAX;
8f3d14e2
NB
140 dev_limits.hw_queue_depth = q->nr_requests;
141 dev_limits.queue_depth = q->nr_requests;
c66ac9db 142
c66ac9db
NB
143 ib_dev->ibd_bd = bd;
144
145 dev = transport_add_device_to_core_hba(hba,
5951146d 146 &iblock_template, se_dev, dev_flags, ib_dev,
c66ac9db 147 &dev_limits, "IBLOCK", IBLOCK_VERSION);
6708bb27 148 if (!dev)
c66ac9db
NB
149 goto failed;
150
c66ac9db
NB
151 /*
152 * Check if the underlying struct block_device request_queue supports
153 * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM
154 * in ATA and we need to set TPE=1
155 */
613640e4 156 if (blk_queue_discard(q)) {
e3d6f909 157 dev->se_sub_dev->se_dev_attrib.max_unmap_lba_count =
c66ac9db
NB
158 q->limits.max_discard_sectors;
159 /*
160 * Currently hardcoded to 1 in Linux/SCSI code..
161 */
e3d6f909
AG
162 dev->se_sub_dev->se_dev_attrib.max_unmap_block_desc_count = 1;
163 dev->se_sub_dev->se_dev_attrib.unmap_granularity =
7347b5ff 164 q->limits.discard_granularity >> 9;
e3d6f909 165 dev->se_sub_dev->se_dev_attrib.unmap_granularity_alignment =
c66ac9db
NB
166 q->limits.discard_alignment;
167
6708bb27 168 pr_debug("IBLOCK: BLOCK Discard support available,"
c66ac9db
NB
169 " disabled by default\n");
170 }
171
e22a7f07
RD
172 if (blk_queue_nonrot(q))
173 dev->se_sub_dev->se_dev_attrib.is_nonrot = 1;
174
c66ac9db
NB
175 return dev;
176
177failed:
178 if (ib_dev->ibd_bio_set) {
179 bioset_free(ib_dev->ibd_bio_set);
180 ib_dev->ibd_bio_set = NULL;
181 }
182 ib_dev->ibd_bd = NULL;
613640e4 183 return ERR_PTR(ret);
c66ac9db
NB
184}
185
186static void iblock_free_device(void *p)
187{
188 struct iblock_dev *ib_dev = p;
189
bc665524
NB
190 if (ib_dev->ibd_bd != NULL)
191 blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
192 if (ib_dev->ibd_bio_set != NULL)
193 bioset_free(ib_dev->ibd_bio_set);
c66ac9db
NB
194 kfree(ib_dev);
195}
196
c66ac9db
NB
197static unsigned long long iblock_emulate_read_cap_with_block_size(
198 struct se_device *dev,
199 struct block_device *bd,
200 struct request_queue *q)
201{
202 unsigned long long blocks_long = (div_u64(i_size_read(bd->bd_inode),
203 bdev_logical_block_size(bd)) - 1);
204 u32 block_size = bdev_logical_block_size(bd);
205
e3d6f909 206 if (block_size == dev->se_sub_dev->se_dev_attrib.block_size)
c66ac9db
NB
207 return blocks_long;
208
209 switch (block_size) {
210 case 4096:
e3d6f909 211 switch (dev->se_sub_dev->se_dev_attrib.block_size) {
c66ac9db
NB
212 case 2048:
213 blocks_long <<= 1;
214 break;
215 case 1024:
216 blocks_long <<= 2;
217 break;
218 case 512:
219 blocks_long <<= 3;
220 default:
221 break;
222 }
223 break;
224 case 2048:
e3d6f909 225 switch (dev->se_sub_dev->se_dev_attrib.block_size) {
c66ac9db
NB
226 case 4096:
227 blocks_long >>= 1;
228 break;
229 case 1024:
230 blocks_long <<= 1;
231 break;
232 case 512:
233 blocks_long <<= 2;
234 break;
235 default:
236 break;
237 }
238 break;
239 case 1024:
e3d6f909 240 switch (dev->se_sub_dev->se_dev_attrib.block_size) {
c66ac9db
NB
241 case 4096:
242 blocks_long >>= 2;
243 break;
244 case 2048:
245 blocks_long >>= 1;
246 break;
247 case 512:
248 blocks_long <<= 1;
249 break;
250 default:
251 break;
252 }
253 break;
254 case 512:
e3d6f909 255 switch (dev->se_sub_dev->se_dev_attrib.block_size) {
c66ac9db
NB
256 case 4096:
257 blocks_long >>= 3;
258 break;
259 case 2048:
260 blocks_long >>= 2;
261 break;
262 case 1024:
263 blocks_long >>= 1;
264 break;
265 default:
266 break;
267 }
268 break;
269 default:
270 break;
271 }
272
273 return blocks_long;
274}
275
df5fa691
CH
276static void iblock_end_io_flush(struct bio *bio, int err)
277{
278 struct se_cmd *cmd = bio->bi_private;
279
280 if (err)
281 pr_err("IBLOCK: cache flush failed: %d\n", err);
282
5787cacd
CH
283 if (cmd) {
284 if (err) {
285 cmd->scsi_sense_reason =
286 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
287 target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
288 } else {
289 target_complete_cmd(cmd, SAM_STAT_GOOD);
290 }
291 }
292
df5fa691
CH
293 bio_put(bio);
294}
295
c66ac9db 296/*
df5fa691
CH
297 * Implement SYCHRONIZE CACHE. Note that we can't handle lba ranges and must
298 * always flush the whole cache.
c66ac9db 299 */
ad67f0d9 300static int iblock_execute_sync_cache(struct se_cmd *cmd)
c66ac9db 301{
c66ac9db 302 struct iblock_dev *ib_dev = cmd->se_dev->dev_ptr;
a1d8b49a 303 int immed = (cmd->t_task_cdb[1] & 0x2);
df5fa691 304 struct bio *bio;
c66ac9db
NB
305
306 /*
307 * If the Immediate bit is set, queue up the GOOD response
df5fa691 308 * for this SYNCHRONIZE_CACHE op.
c66ac9db
NB
309 */
310 if (immed)
5787cacd 311 target_complete_cmd(cmd, SAM_STAT_GOOD);
c66ac9db 312
df5fa691
CH
313 bio = bio_alloc(GFP_KERNEL, 0);
314 bio->bi_end_io = iblock_end_io_flush;
315 bio->bi_bdev = ib_dev->ibd_bd;
c66ac9db 316 if (!immed)
df5fa691
CH
317 bio->bi_private = cmd;
318 submit_bio(WRITE_FLUSH, bio);
ad67f0d9 319 return 0;
c66ac9db
NB
320}
321
14150a6b 322static int iblock_execute_unmap(struct se_cmd *cmd)
c66ac9db 323{
14150a6b 324 struct se_device *dev = cmd->se_dev;
c66ac9db 325 struct iblock_dev *ibd = dev->dev_ptr;
14150a6b 326 unsigned char *buf, *ptr = NULL;
14150a6b
CH
327 sector_t lba;
328 unsigned int size = cmd->data_length, range;
329 int ret = 0, offset;
330 unsigned short dl, bd_dl;
331
332 /* First UNMAP block descriptor starts at 8 byte offset */
333 offset = 8;
334 size -= 8;
14150a6b
CH
335
336 buf = transport_kmap_data_sg(cmd);
337
1a5fa457
RD
338 dl = get_unaligned_be16(&buf[0]);
339 bd_dl = get_unaligned_be16(&buf[2]);
340
14150a6b
CH
341 ptr = &buf[offset];
342 pr_debug("UNMAP: Sub: %s Using dl: %hu bd_dl: %hu size: %hu"
343 " ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr);
344
345 while (size) {
346 lba = get_unaligned_be64(&ptr[0]);
347 range = get_unaligned_be32(&ptr[8]);
348 pr_debug("UNMAP: Using lba: %llu and range: %u\n",
349 (unsigned long long)lba, range);
350
2594e298
RD
351 if (range > dev->se_sub_dev->se_dev_attrib.max_unmap_lba_count) {
352 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
353 ret = -EINVAL;
354 goto err;
355 }
356
357 if (lba + range > dev->transport->get_blocks(dev) + 1) {
358 cmd->scsi_sense_reason = TCM_ADDRESS_OUT_OF_RANGE;
359 ret = -EINVAL;
360 goto err;
361 }
362
14150a6b
CH
363 ret = blkdev_issue_discard(ibd->ibd_bd, lba, range,
364 GFP_KERNEL, 0);
365 if (ret < 0) {
366 pr_err("blkdev_issue_discard() failed: %d\n",
367 ret);
368 goto err;
369 }
370
371 ptr += 16;
372 size -= 16;
373 }
c66ac9db 374
14150a6b
CH
375err:
376 transport_kunmap_data_sg(cmd);
377 if (!ret)
378 target_complete_cmd(cmd, GOOD);
379 return ret;
c66ac9db
NB
380}
381
6f974e8c
CH
382static int iblock_execute_write_same(struct se_cmd *cmd)
383{
384 struct iblock_dev *ibd = cmd->se_dev->dev_ptr;
385 int ret;
386
387 ret = blkdev_issue_discard(ibd->ibd_bd, cmd->t_task_lba,
388 spc_get_write_same_sectors(cmd), GFP_KERNEL,
389 0);
390 if (ret < 0) {
391 pr_debug("blkdev_issue_discard() failed for WRITE_SAME\n");
392 return ret;
393 }
394
395 target_complete_cmd(cmd, GOOD);
396 return 0;
397}
398
c66ac9db 399enum {
44bfd018 400 Opt_udev_path, Opt_readonly, Opt_force, Opt_err
c66ac9db
NB
401};
402
403static match_table_t tokens = {
404 {Opt_udev_path, "udev_path=%s"},
44bfd018 405 {Opt_readonly, "readonly=%d"},
c66ac9db
NB
406 {Opt_force, "force=%d"},
407 {Opt_err, NULL}
408};
409
410static ssize_t iblock_set_configfs_dev_params(struct se_hba *hba,
411 struct se_subsystem_dev *se_dev,
412 const char *page, ssize_t count)
413{
414 struct iblock_dev *ib_dev = se_dev->se_dev_su_ptr;
6d180253 415 char *orig, *ptr, *arg_p, *opts;
c66ac9db 416 substring_t args[MAX_OPT_ARGS];
21bca31c 417 int ret = 0, token;
44bfd018 418 unsigned long tmp_readonly;
c66ac9db
NB
419
420 opts = kstrdup(page, GFP_KERNEL);
421 if (!opts)
422 return -ENOMEM;
423
424 orig = opts;
425
90c161b6 426 while ((ptr = strsep(&opts, ",\n")) != NULL) {
c66ac9db
NB
427 if (!*ptr)
428 continue;
429
430 token = match_token(ptr, tokens, args);
431 switch (token) {
432 case Opt_udev_path:
433 if (ib_dev->ibd_bd) {
6708bb27 434 pr_err("Unable to set udev_path= while"
c66ac9db
NB
435 " ib_dev->ibd_bd exists\n");
436 ret = -EEXIST;
437 goto out;
438 }
6d180253
JJ
439 arg_p = match_strdup(&args[0]);
440 if (!arg_p) {
441 ret = -ENOMEM;
442 break;
443 }
444 snprintf(ib_dev->ibd_udev_path, SE_UDEV_PATH_LEN,
445 "%s", arg_p);
446 kfree(arg_p);
6708bb27 447 pr_debug("IBLOCK: Referencing UDEV path: %s\n",
c66ac9db
NB
448 ib_dev->ibd_udev_path);
449 ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH;
450 break;
44bfd018
AG
451 case Opt_readonly:
452 arg_p = match_strdup(&args[0]);
453 if (!arg_p) {
454 ret = -ENOMEM;
455 break;
456 }
457 ret = strict_strtoul(arg_p, 0, &tmp_readonly);
458 kfree(arg_p);
459 if (ret < 0) {
460 pr_err("strict_strtoul() failed for"
461 " readonly=\n");
462 goto out;
463 }
464 ib_dev->ibd_readonly = tmp_readonly;
465 pr_debug("IBLOCK: readonly: %d\n", ib_dev->ibd_readonly);
466 break;
c66ac9db 467 case Opt_force:
c66ac9db
NB
468 break;
469 default:
470 break;
471 }
472 }
473
474out:
475 kfree(orig);
476 return (!ret) ? count : ret;
477}
478
479static ssize_t iblock_check_configfs_dev_params(
480 struct se_hba *hba,
481 struct se_subsystem_dev *se_dev)
482{
483 struct iblock_dev *ibd = se_dev->se_dev_su_ptr;
484
485 if (!(ibd->ibd_flags & IBDF_HAS_UDEV_PATH)) {
6708bb27 486 pr_err("Missing udev_path= parameters for IBLOCK\n");
e3d6f909 487 return -EINVAL;
c66ac9db
NB
488 }
489
490 return 0;
491}
492
493static ssize_t iblock_show_configfs_dev_params(
494 struct se_hba *hba,
495 struct se_subsystem_dev *se_dev,
496 char *b)
497{
498 struct iblock_dev *ibd = se_dev->se_dev_su_ptr;
499 struct block_device *bd = ibd->ibd_bd;
500 char buf[BDEVNAME_SIZE];
501 ssize_t bl = 0;
502
503 if (bd)
504 bl += sprintf(b + bl, "iBlock device: %s",
505 bdevname(bd, buf));
44bfd018
AG
506 if (ibd->ibd_flags & IBDF_HAS_UDEV_PATH)
507 bl += sprintf(b + bl, " UDEV PATH: %s",
c66ac9db 508 ibd->ibd_udev_path);
44bfd018 509 bl += sprintf(b + bl, " readonly: %d\n", ibd->ibd_readonly);
c66ac9db
NB
510
511 bl += sprintf(b + bl, " ");
512 if (bd) {
513 bl += sprintf(b + bl, "Major: %d Minor: %d %s\n",
21bca31c 514 MAJOR(bd->bd_dev), MINOR(bd->bd_dev), (!bd->bd_contains) ?
8359cf43 515 "" : (bd->bd_holder == ibd) ?
c66ac9db
NB
516 "CLAIMED: IBLOCK" : "CLAIMED: OS");
517 } else {
21bca31c 518 bl += sprintf(b + bl, "Major: 0 Minor: 0\n");
c66ac9db
NB
519 }
520
521 return bl;
522}
523
5787cacd
CH
524static void iblock_complete_cmd(struct se_cmd *cmd)
525{
526 struct iblock_req *ibr = cmd->priv;
527 u8 status;
528
529 if (!atomic_dec_and_test(&ibr->pending))
530 return;
531
532 if (atomic_read(&ibr->ib_bio_err_cnt))
533 status = SAM_STAT_CHECK_CONDITION;
534 else
535 status = SAM_STAT_GOOD;
536
537 target_complete_cmd(cmd, status);
538 kfree(ibr);
539}
540
c66ac9db
NB
541static void iblock_bio_destructor(struct bio *bio)
542{
5787cacd
CH
543 struct se_cmd *cmd = bio->bi_private;
544 struct iblock_dev *ib_dev = cmd->se_dev->dev_ptr;
c66ac9db
NB
545
546 bio_free(bio, ib_dev->ibd_bio_set);
547}
548
dbbf3e94 549static struct bio *
5787cacd 550iblock_get_bio(struct se_cmd *cmd, sector_t lba, u32 sg_num)
c66ac9db 551{
5787cacd 552 struct iblock_dev *ib_dev = cmd->se_dev->dev_ptr;
c66ac9db
NB
553 struct bio *bio;
554
5c55125f
CH
555 /*
556 * Only allocate as many vector entries as the bio code allows us to,
557 * we'll loop later on until we have handled the whole request.
558 */
559 if (sg_num > BIO_MAX_PAGES)
560 sg_num = BIO_MAX_PAGES;
561
c66ac9db 562 bio = bio_alloc_bioset(GFP_NOIO, sg_num, ib_dev->ibd_bio_set);
6708bb27
AG
563 if (!bio) {
564 pr_err("Unable to allocate memory for bio\n");
c66ac9db
NB
565 return NULL;
566 }
567
c66ac9db 568 bio->bi_bdev = ib_dev->ibd_bd;
5787cacd 569 bio->bi_private = cmd;
c66ac9db
NB
570 bio->bi_destructor = iblock_bio_destructor;
571 bio->bi_end_io = &iblock_bio_done;
572 bio->bi_sector = lba;
c66ac9db
NB
573 return bio;
574}
575
d5b4a21b
CH
576static void iblock_submit_bios(struct bio_list *list, int rw)
577{
578 struct blk_plug plug;
579 struct bio *bio;
580
581 blk_start_plug(&plug);
582 while ((bio = bio_list_pop(list)))
583 submit_bio(rw, bio);
584 blk_finish_plug(&plug);
585}
586
0c2ad7d1 587static int iblock_execute_rw(struct se_cmd *cmd)
c66ac9db 588{
0c2ad7d1
CH
589 struct scatterlist *sgl = cmd->t_data_sg;
590 u32 sgl_nents = cmd->t_data_nents;
591 enum dma_data_direction data_direction = cmd->data_direction;
5951146d 592 struct se_device *dev = cmd->se_dev;
5787cacd 593 struct iblock_req *ibr;
dbbf3e94
CH
594 struct bio *bio;
595 struct bio_list list;
c66ac9db 596 struct scatterlist *sg;
5787cacd 597 u32 sg_num = sgl_nents;
c66ac9db 598 sector_t block_lba;
d5b4a21b 599 unsigned bio_cnt;
dbbf3e94 600 int rw;
5787cacd 601 int i;
dbbf3e94 602
5787cacd 603 if (data_direction == DMA_TO_DEVICE) {
dbbf3e94
CH
604 /*
605 * Force data to disk if we pretend to not have a volatile
606 * write cache, or the initiator set the Force Unit Access bit.
607 */
608 if (dev->se_sub_dev->se_dev_attrib.emulate_write_cache == 0 ||
609 (dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0 &&
2d3a4b51 610 (cmd->se_cmd_flags & SCF_FUA)))
dbbf3e94
CH
611 rw = WRITE_FUA;
612 else
613 rw = WRITE;
614 } else {
615 rw = READ;
616 }
617
c66ac9db 618 /*
5787cacd
CH
619 * Convert the blocksize advertised to the initiator to the 512 byte
620 * units unconditionally used by the Linux block layer.
c66ac9db 621 */
e3d6f909 622 if (dev->se_sub_dev->se_dev_attrib.block_size == 4096)
72a0e5e2 623 block_lba = (cmd->t_task_lba << 3);
e3d6f909 624 else if (dev->se_sub_dev->se_dev_attrib.block_size == 2048)
72a0e5e2 625 block_lba = (cmd->t_task_lba << 2);
e3d6f909 626 else if (dev->se_sub_dev->se_dev_attrib.block_size == 1024)
72a0e5e2 627 block_lba = (cmd->t_task_lba << 1);
e3d6f909 628 else if (dev->se_sub_dev->se_dev_attrib.block_size == 512)
72a0e5e2 629 block_lba = cmd->t_task_lba;
c66ac9db 630 else {
6708bb27 631 pr_err("Unsupported SCSI -> BLOCK LBA conversion:"
e3d6f909 632 " %u\n", dev->se_sub_dev->se_dev_attrib.block_size);
03e98c9e
NB
633 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
634 return -ENOSYS;
c66ac9db
NB
635 }
636
5787cacd
CH
637 ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
638 if (!ibr)
639 goto fail;
640 cmd->priv = ibr;
641
642 bio = iblock_get_bio(cmd, block_lba, sgl_nents);
643 if (!bio)
644 goto fail_free_ibr;
dbbf3e94
CH
645
646 bio_list_init(&list);
647 bio_list_add(&list, bio);
5787cacd
CH
648
649 atomic_set(&ibr->pending, 2);
d5b4a21b 650 bio_cnt = 1;
c66ac9db 651
5787cacd 652 for_each_sg(sgl, sg, sgl_nents, i) {
dbbf3e94
CH
653 /*
654 * XXX: if the length the device accepts is shorter than the
655 * length of the S/G list entry this will cause and
656 * endless loop. Better hope no driver uses huge pages.
657 */
658 while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
659 != sg->length) {
d5b4a21b
CH
660 if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) {
661 iblock_submit_bios(&list, rw);
662 bio_cnt = 0;
663 }
664
5787cacd 665 bio = iblock_get_bio(cmd, block_lba, sg_num);
6708bb27 666 if (!bio)
5787cacd
CH
667 goto fail_put_bios;
668
669 atomic_inc(&ibr->pending);
dbbf3e94 670 bio_list_add(&list, bio);
d5b4a21b 671 bio_cnt++;
c66ac9db 672 }
dbbf3e94 673
c66ac9db
NB
674 /* Always in 512 byte units for Linux/Block */
675 block_lba += sg->length >> IBLOCK_LBA_SHIFT;
676 sg_num--;
c66ac9db
NB
677 }
678
d5b4a21b 679 iblock_submit_bios(&list, rw);
5787cacd 680 iblock_complete_cmd(cmd);
03e98c9e 681 return 0;
dbbf3e94 682
5787cacd 683fail_put_bios:
dbbf3e94 684 while ((bio = bio_list_pop(&list)))
c66ac9db 685 bio_put(bio);
5787cacd
CH
686fail_free_ibr:
687 kfree(ibr);
03e98c9e 688 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
5787cacd 689fail:
03e98c9e 690 return -ENOMEM;
c66ac9db
NB
691}
692
c66ac9db
NB
693static u32 iblock_get_device_rev(struct se_device *dev)
694{
695 return SCSI_SPC_2; /* Returns SPC-3 in Initiator Data */
696}
697
698static u32 iblock_get_device_type(struct se_device *dev)
699{
700 return TYPE_DISK;
701}
702
703static sector_t iblock_get_blocks(struct se_device *dev)
704{
705 struct iblock_dev *ibd = dev->dev_ptr;
706 struct block_device *bd = ibd->ibd_bd;
707 struct request_queue *q = bdev_get_queue(bd);
708
709 return iblock_emulate_read_cap_with_block_size(dev, bd, q);
710}
711
712static void iblock_bio_done(struct bio *bio, int err)
713{
5787cacd
CH
714 struct se_cmd *cmd = bio->bi_private;
715 struct iblock_req *ibr = cmd->priv;
dbbf3e94 716
c66ac9db
NB
717 /*
718 * Set -EIO if !BIO_UPTODATE and the passed is still err=0
719 */
6708bb27 720 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && !err)
c66ac9db
NB
721 err = -EIO;
722
723 if (err != 0) {
6708bb27 724 pr_err("test_bit(BIO_UPTODATE) failed for bio: %p,"
c66ac9db
NB
725 " err: %d\n", bio, err);
726 /*
727 * Bump the ib_bio_err_cnt and release bio.
728 */
729 atomic_inc(&ibr->ib_bio_err_cnt);
730 smp_mb__after_atomic_inc();
c66ac9db 731 }
dbbf3e94 732
c66ac9db 733 bio_put(bio);
dbbf3e94 734
5787cacd 735 iblock_complete_cmd(cmd);
c66ac9db
NB
736}
737
0c2ad7d1
CH
738static struct spc_ops iblock_spc_ops = {
739 .execute_rw = iblock_execute_rw,
ad67f0d9 740 .execute_sync_cache = iblock_execute_sync_cache,
6f974e8c 741 .execute_write_same = iblock_execute_write_same,
14150a6b 742 .execute_unmap = iblock_execute_unmap,
0c2ad7d1
CH
743};
744
745static int iblock_parse_cdb(struct se_cmd *cmd)
746{
747 return sbc_parse_cdb(cmd, &iblock_spc_ops);
748}
749
c66ac9db
NB
750static struct se_subsystem_api iblock_template = {
751 .name = "iblock",
752 .owner = THIS_MODULE,
753 .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV,
f55918fa
CH
754 .write_cache_emulated = 1,
755 .fua_write_emulated = 1,
c66ac9db
NB
756 .attach_hba = iblock_attach_hba,
757 .detach_hba = iblock_detach_hba,
758 .allocate_virtdevice = iblock_allocate_virtdevice,
759 .create_virtdevice = iblock_create_virtdevice,
760 .free_device = iblock_free_device,
0c2ad7d1 761 .parse_cdb = iblock_parse_cdb,
c66ac9db
NB
762 .check_configfs_dev_params = iblock_check_configfs_dev_params,
763 .set_configfs_dev_params = iblock_set_configfs_dev_params,
764 .show_configfs_dev_params = iblock_show_configfs_dev_params,
c66ac9db
NB
765 .get_device_rev = iblock_get_device_rev,
766 .get_device_type = iblock_get_device_type,
767 .get_blocks = iblock_get_blocks,
768};
769
770static int __init iblock_module_init(void)
771{
772 return transport_subsystem_register(&iblock_template);
773}
774
775static void iblock_module_exit(void)
776{
777 transport_subsystem_release(&iblock_template);
778}
779
780MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin");
781MODULE_AUTHOR("nab@Linux-iSCSI.org");
782MODULE_LICENSE("GPL");
783
784module_init(iblock_module_init);
785module_exit(iblock_module_exit);
This page took 0.145871 seconds and 5 git commands to generate.