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 | ||
c66ac9db | 330 | enum { |
44bfd018 | 331 | Opt_udev_path, Opt_readonly, Opt_force, Opt_err |
c66ac9db NB |
332 | }; |
333 | ||
334 | static match_table_t tokens = { | |
335 | {Opt_udev_path, "udev_path=%s"}, | |
44bfd018 | 336 | {Opt_readonly, "readonly=%d"}, |
c66ac9db NB |
337 | {Opt_force, "force=%d"}, |
338 | {Opt_err, NULL} | |
339 | }; | |
340 | ||
341 | static ssize_t iblock_set_configfs_dev_params(struct se_hba *hba, | |
342 | struct se_subsystem_dev *se_dev, | |
343 | const char *page, ssize_t count) | |
344 | { | |
345 | struct iblock_dev *ib_dev = se_dev->se_dev_su_ptr; | |
6d180253 | 346 | char *orig, *ptr, *arg_p, *opts; |
c66ac9db | 347 | substring_t args[MAX_OPT_ARGS]; |
21bca31c | 348 | int ret = 0, token; |
44bfd018 | 349 | unsigned long tmp_readonly; |
c66ac9db NB |
350 | |
351 | opts = kstrdup(page, GFP_KERNEL); | |
352 | if (!opts) | |
353 | return -ENOMEM; | |
354 | ||
355 | orig = opts; | |
356 | ||
90c161b6 | 357 | while ((ptr = strsep(&opts, ",\n")) != NULL) { |
c66ac9db NB |
358 | if (!*ptr) |
359 | continue; | |
360 | ||
361 | token = match_token(ptr, tokens, args); | |
362 | switch (token) { | |
363 | case Opt_udev_path: | |
364 | if (ib_dev->ibd_bd) { | |
6708bb27 | 365 | pr_err("Unable to set udev_path= while" |
c66ac9db NB |
366 | " ib_dev->ibd_bd exists\n"); |
367 | ret = -EEXIST; | |
368 | goto out; | |
369 | } | |
6d180253 JJ |
370 | arg_p = match_strdup(&args[0]); |
371 | if (!arg_p) { | |
372 | ret = -ENOMEM; | |
373 | break; | |
374 | } | |
375 | snprintf(ib_dev->ibd_udev_path, SE_UDEV_PATH_LEN, | |
376 | "%s", arg_p); | |
377 | kfree(arg_p); | |
6708bb27 | 378 | pr_debug("IBLOCK: Referencing UDEV path: %s\n", |
c66ac9db NB |
379 | ib_dev->ibd_udev_path); |
380 | ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH; | |
381 | break; | |
44bfd018 AG |
382 | case Opt_readonly: |
383 | arg_p = match_strdup(&args[0]); | |
384 | if (!arg_p) { | |
385 | ret = -ENOMEM; | |
386 | break; | |
387 | } | |
388 | ret = strict_strtoul(arg_p, 0, &tmp_readonly); | |
389 | kfree(arg_p); | |
390 | if (ret < 0) { | |
391 | pr_err("strict_strtoul() failed for" | |
392 | " readonly=\n"); | |
393 | goto out; | |
394 | } | |
395 | ib_dev->ibd_readonly = tmp_readonly; | |
396 | pr_debug("IBLOCK: readonly: %d\n", ib_dev->ibd_readonly); | |
397 | break; | |
c66ac9db | 398 | case Opt_force: |
c66ac9db NB |
399 | break; |
400 | default: | |
401 | break; | |
402 | } | |
403 | } | |
404 | ||
405 | out: | |
406 | kfree(orig); | |
407 | return (!ret) ? count : ret; | |
408 | } | |
409 | ||
410 | static ssize_t iblock_check_configfs_dev_params( | |
411 | struct se_hba *hba, | |
412 | struct se_subsystem_dev *se_dev) | |
413 | { | |
414 | struct iblock_dev *ibd = se_dev->se_dev_su_ptr; | |
415 | ||
416 | if (!(ibd->ibd_flags & IBDF_HAS_UDEV_PATH)) { | |
6708bb27 | 417 | pr_err("Missing udev_path= parameters for IBLOCK\n"); |
e3d6f909 | 418 | return -EINVAL; |
c66ac9db NB |
419 | } |
420 | ||
421 | return 0; | |
422 | } | |
423 | ||
424 | static ssize_t iblock_show_configfs_dev_params( | |
425 | struct se_hba *hba, | |
426 | struct se_subsystem_dev *se_dev, | |
427 | char *b) | |
428 | { | |
429 | struct iblock_dev *ibd = se_dev->se_dev_su_ptr; | |
430 | struct block_device *bd = ibd->ibd_bd; | |
431 | char buf[BDEVNAME_SIZE]; | |
432 | ssize_t bl = 0; | |
433 | ||
434 | if (bd) | |
435 | bl += sprintf(b + bl, "iBlock device: %s", | |
436 | bdevname(bd, buf)); | |
44bfd018 AG |
437 | if (ibd->ibd_flags & IBDF_HAS_UDEV_PATH) |
438 | bl += sprintf(b + bl, " UDEV PATH: %s", | |
c66ac9db | 439 | ibd->ibd_udev_path); |
44bfd018 | 440 | bl += sprintf(b + bl, " readonly: %d\n", ibd->ibd_readonly); |
c66ac9db NB |
441 | |
442 | bl += sprintf(b + bl, " "); | |
443 | if (bd) { | |
444 | bl += sprintf(b + bl, "Major: %d Minor: %d %s\n", | |
21bca31c | 445 | MAJOR(bd->bd_dev), MINOR(bd->bd_dev), (!bd->bd_contains) ? |
8359cf43 | 446 | "" : (bd->bd_holder == ibd) ? |
c66ac9db NB |
447 | "CLAIMED: IBLOCK" : "CLAIMED: OS"); |
448 | } else { | |
21bca31c | 449 | bl += sprintf(b + bl, "Major: 0 Minor: 0\n"); |
c66ac9db NB |
450 | } |
451 | ||
452 | return bl; | |
453 | } | |
454 | ||
5787cacd CH |
455 | static void iblock_complete_cmd(struct se_cmd *cmd) |
456 | { | |
457 | struct iblock_req *ibr = cmd->priv; | |
458 | u8 status; | |
459 | ||
460 | if (!atomic_dec_and_test(&ibr->pending)) | |
461 | return; | |
462 | ||
463 | if (atomic_read(&ibr->ib_bio_err_cnt)) | |
464 | status = SAM_STAT_CHECK_CONDITION; | |
465 | else | |
466 | status = SAM_STAT_GOOD; | |
467 | ||
468 | target_complete_cmd(cmd, status); | |
469 | kfree(ibr); | |
470 | } | |
471 | ||
c66ac9db NB |
472 | static void iblock_bio_destructor(struct bio *bio) |
473 | { | |
5787cacd CH |
474 | struct se_cmd *cmd = bio->bi_private; |
475 | struct iblock_dev *ib_dev = cmd->se_dev->dev_ptr; | |
c66ac9db NB |
476 | |
477 | bio_free(bio, ib_dev->ibd_bio_set); | |
478 | } | |
479 | ||
dbbf3e94 | 480 | static struct bio * |
5787cacd | 481 | iblock_get_bio(struct se_cmd *cmd, sector_t lba, u32 sg_num) |
c66ac9db | 482 | { |
5787cacd | 483 | struct iblock_dev *ib_dev = cmd->se_dev->dev_ptr; |
c66ac9db NB |
484 | struct bio *bio; |
485 | ||
5c55125f CH |
486 | /* |
487 | * Only allocate as many vector entries as the bio code allows us to, | |
488 | * we'll loop later on until we have handled the whole request. | |
489 | */ | |
490 | if (sg_num > BIO_MAX_PAGES) | |
491 | sg_num = BIO_MAX_PAGES; | |
492 | ||
c66ac9db | 493 | bio = bio_alloc_bioset(GFP_NOIO, sg_num, ib_dev->ibd_bio_set); |
6708bb27 AG |
494 | if (!bio) { |
495 | pr_err("Unable to allocate memory for bio\n"); | |
c66ac9db NB |
496 | return NULL; |
497 | } | |
498 | ||
c66ac9db | 499 | bio->bi_bdev = ib_dev->ibd_bd; |
5787cacd | 500 | bio->bi_private = cmd; |
c66ac9db NB |
501 | bio->bi_destructor = iblock_bio_destructor; |
502 | bio->bi_end_io = &iblock_bio_done; | |
503 | bio->bi_sector = lba; | |
c66ac9db NB |
504 | return bio; |
505 | } | |
506 | ||
d5b4a21b CH |
507 | static void iblock_submit_bios(struct bio_list *list, int rw) |
508 | { | |
509 | struct blk_plug plug; | |
510 | struct bio *bio; | |
511 | ||
512 | blk_start_plug(&plug); | |
513 | while ((bio = bio_list_pop(list))) | |
514 | submit_bio(rw, bio); | |
515 | blk_finish_plug(&plug); | |
516 | } | |
517 | ||
0c2ad7d1 | 518 | static int iblock_execute_rw(struct se_cmd *cmd) |
c66ac9db | 519 | { |
0c2ad7d1 CH |
520 | struct scatterlist *sgl = cmd->t_data_sg; |
521 | u32 sgl_nents = cmd->t_data_nents; | |
522 | enum dma_data_direction data_direction = cmd->data_direction; | |
5951146d | 523 | struct se_device *dev = cmd->se_dev; |
5787cacd | 524 | struct iblock_req *ibr; |
dbbf3e94 CH |
525 | struct bio *bio; |
526 | struct bio_list list; | |
c66ac9db | 527 | struct scatterlist *sg; |
5787cacd | 528 | u32 sg_num = sgl_nents; |
c66ac9db | 529 | sector_t block_lba; |
d5b4a21b | 530 | unsigned bio_cnt; |
dbbf3e94 | 531 | int rw; |
5787cacd | 532 | int i; |
dbbf3e94 | 533 | |
5787cacd | 534 | if (data_direction == DMA_TO_DEVICE) { |
dbbf3e94 CH |
535 | /* |
536 | * Force data to disk if we pretend to not have a volatile | |
537 | * write cache, or the initiator set the Force Unit Access bit. | |
538 | */ | |
539 | if (dev->se_sub_dev->se_dev_attrib.emulate_write_cache == 0 || | |
540 | (dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0 && | |
2d3a4b51 | 541 | (cmd->se_cmd_flags & SCF_FUA))) |
dbbf3e94 CH |
542 | rw = WRITE_FUA; |
543 | else | |
544 | rw = WRITE; | |
545 | } else { | |
546 | rw = READ; | |
547 | } | |
548 | ||
c66ac9db | 549 | /* |
5787cacd CH |
550 | * Convert the blocksize advertised to the initiator to the 512 byte |
551 | * units unconditionally used by the Linux block layer. | |
c66ac9db | 552 | */ |
e3d6f909 | 553 | if (dev->se_sub_dev->se_dev_attrib.block_size == 4096) |
72a0e5e2 | 554 | block_lba = (cmd->t_task_lba << 3); |
e3d6f909 | 555 | else if (dev->se_sub_dev->se_dev_attrib.block_size == 2048) |
72a0e5e2 | 556 | block_lba = (cmd->t_task_lba << 2); |
e3d6f909 | 557 | else if (dev->se_sub_dev->se_dev_attrib.block_size == 1024) |
72a0e5e2 | 558 | block_lba = (cmd->t_task_lba << 1); |
e3d6f909 | 559 | else if (dev->se_sub_dev->se_dev_attrib.block_size == 512) |
72a0e5e2 | 560 | block_lba = cmd->t_task_lba; |
c66ac9db | 561 | else { |
6708bb27 | 562 | pr_err("Unsupported SCSI -> BLOCK LBA conversion:" |
e3d6f909 | 563 | " %u\n", dev->se_sub_dev->se_dev_attrib.block_size); |
03e98c9e NB |
564 | cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
565 | return -ENOSYS; | |
c66ac9db NB |
566 | } |
567 | ||
5787cacd CH |
568 | ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL); |
569 | if (!ibr) | |
570 | goto fail; | |
571 | cmd->priv = ibr; | |
572 | ||
573 | bio = iblock_get_bio(cmd, block_lba, sgl_nents); | |
574 | if (!bio) | |
575 | goto fail_free_ibr; | |
dbbf3e94 CH |
576 | |
577 | bio_list_init(&list); | |
578 | bio_list_add(&list, bio); | |
5787cacd CH |
579 | |
580 | atomic_set(&ibr->pending, 2); | |
d5b4a21b | 581 | bio_cnt = 1; |
c66ac9db | 582 | |
5787cacd | 583 | for_each_sg(sgl, sg, sgl_nents, i) { |
dbbf3e94 CH |
584 | /* |
585 | * XXX: if the length the device accepts is shorter than the | |
586 | * length of the S/G list entry this will cause and | |
587 | * endless loop. Better hope no driver uses huge pages. | |
588 | */ | |
589 | while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset) | |
590 | != sg->length) { | |
d5b4a21b CH |
591 | if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) { |
592 | iblock_submit_bios(&list, rw); | |
593 | bio_cnt = 0; | |
594 | } | |
595 | ||
5787cacd | 596 | bio = iblock_get_bio(cmd, block_lba, sg_num); |
6708bb27 | 597 | if (!bio) |
5787cacd CH |
598 | goto fail_put_bios; |
599 | ||
600 | atomic_inc(&ibr->pending); | |
dbbf3e94 | 601 | bio_list_add(&list, bio); |
d5b4a21b | 602 | bio_cnt++; |
c66ac9db | 603 | } |
dbbf3e94 | 604 | |
c66ac9db NB |
605 | /* Always in 512 byte units for Linux/Block */ |
606 | block_lba += sg->length >> IBLOCK_LBA_SHIFT; | |
607 | sg_num--; | |
c66ac9db NB |
608 | } |
609 | ||
d5b4a21b | 610 | iblock_submit_bios(&list, rw); |
5787cacd | 611 | iblock_complete_cmd(cmd); |
03e98c9e | 612 | return 0; |
dbbf3e94 | 613 | |
5787cacd | 614 | fail_put_bios: |
dbbf3e94 | 615 | while ((bio = bio_list_pop(&list))) |
c66ac9db | 616 | bio_put(bio); |
5787cacd CH |
617 | fail_free_ibr: |
618 | kfree(ibr); | |
03e98c9e | 619 | cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
5787cacd | 620 | fail: |
03e98c9e | 621 | return -ENOMEM; |
c66ac9db NB |
622 | } |
623 | ||
c66ac9db NB |
624 | static u32 iblock_get_device_rev(struct se_device *dev) |
625 | { | |
626 | return SCSI_SPC_2; /* Returns SPC-3 in Initiator Data */ | |
627 | } | |
628 | ||
629 | static u32 iblock_get_device_type(struct se_device *dev) | |
630 | { | |
631 | return TYPE_DISK; | |
632 | } | |
633 | ||
634 | static sector_t iblock_get_blocks(struct se_device *dev) | |
635 | { | |
636 | struct iblock_dev *ibd = dev->dev_ptr; | |
637 | struct block_device *bd = ibd->ibd_bd; | |
638 | struct request_queue *q = bdev_get_queue(bd); | |
639 | ||
640 | return iblock_emulate_read_cap_with_block_size(dev, bd, q); | |
641 | } | |
642 | ||
643 | static void iblock_bio_done(struct bio *bio, int err) | |
644 | { | |
5787cacd CH |
645 | struct se_cmd *cmd = bio->bi_private; |
646 | struct iblock_req *ibr = cmd->priv; | |
dbbf3e94 | 647 | |
c66ac9db NB |
648 | /* |
649 | * Set -EIO if !BIO_UPTODATE and the passed is still err=0 | |
650 | */ | |
6708bb27 | 651 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && !err) |
c66ac9db NB |
652 | err = -EIO; |
653 | ||
654 | if (err != 0) { | |
6708bb27 | 655 | pr_err("test_bit(BIO_UPTODATE) failed for bio: %p," |
c66ac9db NB |
656 | " err: %d\n", bio, err); |
657 | /* | |
658 | * Bump the ib_bio_err_cnt and release bio. | |
659 | */ | |
660 | atomic_inc(&ibr->ib_bio_err_cnt); | |
661 | smp_mb__after_atomic_inc(); | |
c66ac9db | 662 | } |
dbbf3e94 | 663 | |
c66ac9db | 664 | bio_put(bio); |
dbbf3e94 | 665 | |
5787cacd | 666 | iblock_complete_cmd(cmd); |
c66ac9db NB |
667 | } |
668 | ||
0c2ad7d1 CH |
669 | static struct spc_ops iblock_spc_ops = { |
670 | .execute_rw = iblock_execute_rw, | |
ad67f0d9 | 671 | .execute_sync_cache = iblock_execute_sync_cache, |
0c2ad7d1 CH |
672 | }; |
673 | ||
674 | static int iblock_parse_cdb(struct se_cmd *cmd) | |
675 | { | |
676 | return sbc_parse_cdb(cmd, &iblock_spc_ops); | |
677 | } | |
678 | ||
c66ac9db NB |
679 | static struct se_subsystem_api iblock_template = { |
680 | .name = "iblock", | |
681 | .owner = THIS_MODULE, | |
682 | .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV, | |
f55918fa CH |
683 | .write_cache_emulated = 1, |
684 | .fua_write_emulated = 1, | |
c66ac9db NB |
685 | .attach_hba = iblock_attach_hba, |
686 | .detach_hba = iblock_detach_hba, | |
687 | .allocate_virtdevice = iblock_allocate_virtdevice, | |
688 | .create_virtdevice = iblock_create_virtdevice, | |
689 | .free_device = iblock_free_device, | |
0c2ad7d1 | 690 | .parse_cdb = iblock_parse_cdb, |
c66ac9db | 691 | .do_discard = iblock_do_discard, |
c66ac9db NB |
692 | .check_configfs_dev_params = iblock_check_configfs_dev_params, |
693 | .set_configfs_dev_params = iblock_set_configfs_dev_params, | |
694 | .show_configfs_dev_params = iblock_show_configfs_dev_params, | |
c66ac9db NB |
695 | .get_device_rev = iblock_get_device_rev, |
696 | .get_device_type = iblock_get_device_type, | |
697 | .get_blocks = iblock_get_blocks, | |
698 | }; | |
699 | ||
700 | static int __init iblock_module_init(void) | |
701 | { | |
702 | return transport_subsystem_register(&iblock_template); | |
703 | } | |
704 | ||
705 | static void iblock_module_exit(void) | |
706 | { | |
707 | transport_subsystem_release(&iblock_template); | |
708 | } | |
709 | ||
710 | MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin"); | |
711 | MODULE_AUTHOR("nab@Linux-iSCSI.org"); | |
712 | MODULE_LICENSE("GPL"); | |
713 | ||
714 | module_init(iblock_module_init); | |
715 | module_exit(iblock_module_exit); |