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