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