1 /*******************************************************************************
2 * Filename: target_core_rd.c
4 * This file contains the Storage Engine <-> Ramdisk transport
7 * (c) Copyright 2003-2013 Datera, Inc.
9 * Nicholas A. Bellinger <nab@kernel.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 ******************************************************************************/
27 #include <linux/string.h>
28 #include <linux/parser.h>
29 #include <linux/highmem.h>
30 #include <linux/timer.h>
31 #include <linux/scatterlist.h>
32 #include <linux/slab.h>
33 #include <linux/spinlock.h>
34 #include <scsi/scsi_proto.h>
36 #include <target/target_core_base.h>
37 #include <target/target_core_backend.h>
39 #include "target_core_rd.h"
41 static inline struct rd_dev
*RD_DEV(struct se_device
*dev
)
43 return container_of(dev
, struct rd_dev
, dev
);
46 static int rd_attach_hba(struct se_hba
*hba
, u32 host_id
)
48 struct rd_host
*rd_host
;
50 rd_host
= kzalloc(sizeof(struct rd_host
), GFP_KERNEL
);
52 pr_err("Unable to allocate memory for struct rd_host\n");
56 rd_host
->rd_host_id
= host_id
;
58 hba
->hba_ptr
= rd_host
;
60 pr_debug("CORE_HBA[%d] - TCM Ramdisk HBA Driver %s on"
61 " Generic Target Core Stack %s\n", hba
->hba_id
,
62 RD_HBA_VERSION
, TARGET_CORE_VERSION
);
67 static void rd_detach_hba(struct se_hba
*hba
)
69 struct rd_host
*rd_host
= hba
->hba_ptr
;
71 pr_debug("CORE_HBA[%d] - Detached Ramdisk HBA: %u from"
72 " Generic Target Core\n", hba
->hba_id
, rd_host
->rd_host_id
);
78 static u32
rd_release_sgl_table(struct rd_dev
*rd_dev
, struct rd_dev_sg_table
*sg_table
,
82 struct scatterlist
*sg
;
83 u32 i
, j
, page_count
= 0, sg_per_table
;
85 for (i
= 0; i
< sg_table_count
; i
++) {
86 sg
= sg_table
[i
].sg_table
;
87 sg_per_table
= sg_table
[i
].rd_sg_count
;
89 for (j
= 0; j
< sg_per_table
; j
++) {
103 static void rd_release_device_space(struct rd_dev
*rd_dev
)
107 if (!rd_dev
->sg_table_array
|| !rd_dev
->sg_table_count
)
110 page_count
= rd_release_sgl_table(rd_dev
, rd_dev
->sg_table_array
,
111 rd_dev
->sg_table_count
);
113 pr_debug("CORE_RD[%u] - Released device space for Ramdisk"
114 " Device ID: %u, pages %u in %u tables total bytes %lu\n",
115 rd_dev
->rd_host
->rd_host_id
, rd_dev
->rd_dev_id
, page_count
,
116 rd_dev
->sg_table_count
, (unsigned long)page_count
* PAGE_SIZE
);
118 rd_dev
->sg_table_array
= NULL
;
119 rd_dev
->sg_table_count
= 0;
123 /* rd_build_device_space():
127 static int rd_allocate_sgl_table(struct rd_dev
*rd_dev
, struct rd_dev_sg_table
*sg_table
,
128 u32 total_sg_needed
, unsigned char init_payload
)
130 u32 i
= 0, j
, page_offset
= 0, sg_per_table
;
131 u32 max_sg_per_table
= (RD_MAX_ALLOCATION_SIZE
/
132 sizeof(struct scatterlist
));
134 struct scatterlist
*sg
;
137 while (total_sg_needed
) {
138 unsigned int chain_entry
= 0;
140 sg_per_table
= (total_sg_needed
> max_sg_per_table
) ?
141 max_sg_per_table
: total_sg_needed
;
144 * Reserve extra element for chain entry
146 if (sg_per_table
< total_sg_needed
)
149 sg
= kcalloc(sg_per_table
+ chain_entry
, sizeof(*sg
),
152 pr_err("Unable to allocate scatterlist array"
153 " for struct rd_dev\n");
157 sg_init_table(sg
, sg_per_table
+ chain_entry
);
160 sg_chain(sg_table
[i
- 1].sg_table
,
161 max_sg_per_table
+ 1, sg
);
164 sg_table
[i
].sg_table
= sg
;
165 sg_table
[i
].rd_sg_count
= sg_per_table
;
166 sg_table
[i
].page_start_offset
= page_offset
;
167 sg_table
[i
++].page_end_offset
= (page_offset
+ sg_per_table
)
170 for (j
= 0; j
< sg_per_table
; j
++) {
171 pg
= alloc_pages(GFP_KERNEL
, 0);
173 pr_err("Unable to allocate scatterlist"
174 " pages for struct rd_dev_sg_table\n");
177 sg_assign_page(&sg
[j
], pg
);
178 sg
[j
].length
= PAGE_SIZE
;
181 memset(p
, init_payload
, PAGE_SIZE
);
185 page_offset
+= sg_per_table
;
186 total_sg_needed
-= sg_per_table
;
192 static int rd_build_device_space(struct rd_dev
*rd_dev
)
194 struct rd_dev_sg_table
*sg_table
;
195 u32 sg_tables
, total_sg_needed
;
196 u32 max_sg_per_table
= (RD_MAX_ALLOCATION_SIZE
/
197 sizeof(struct scatterlist
));
200 if (rd_dev
->rd_page_count
<= 0) {
201 pr_err("Illegal page count: %u for Ramdisk device\n",
202 rd_dev
->rd_page_count
);
206 /* Don't need backing pages for NULLIO */
207 if (rd_dev
->rd_flags
& RDF_NULLIO
)
210 total_sg_needed
= rd_dev
->rd_page_count
;
212 sg_tables
= (total_sg_needed
/ max_sg_per_table
) + 1;
214 sg_table
= kzalloc(sg_tables
* sizeof(struct rd_dev_sg_table
), GFP_KERNEL
);
216 pr_err("Unable to allocate memory for Ramdisk"
217 " scatterlist tables\n");
221 rd_dev
->sg_table_array
= sg_table
;
222 rd_dev
->sg_table_count
= sg_tables
;
224 rc
= rd_allocate_sgl_table(rd_dev
, sg_table
, total_sg_needed
, 0x00);
228 pr_debug("CORE_RD[%u] - Built Ramdisk Device ID: %u space of"
229 " %u pages in %u tables\n", rd_dev
->rd_host
->rd_host_id
,
230 rd_dev
->rd_dev_id
, rd_dev
->rd_page_count
,
231 rd_dev
->sg_table_count
);
236 static void rd_release_prot_space(struct rd_dev
*rd_dev
)
240 if (!rd_dev
->sg_prot_array
|| !rd_dev
->sg_prot_count
)
243 page_count
= rd_release_sgl_table(rd_dev
, rd_dev
->sg_prot_array
,
244 rd_dev
->sg_prot_count
);
246 pr_debug("CORE_RD[%u] - Released protection space for Ramdisk"
247 " Device ID: %u, pages %u in %u tables total bytes %lu\n",
248 rd_dev
->rd_host
->rd_host_id
, rd_dev
->rd_dev_id
, page_count
,
249 rd_dev
->sg_table_count
, (unsigned long)page_count
* PAGE_SIZE
);
251 rd_dev
->sg_prot_array
= NULL
;
252 rd_dev
->sg_prot_count
= 0;
255 static int rd_build_prot_space(struct rd_dev
*rd_dev
, int prot_length
, int block_size
)
257 struct rd_dev_sg_table
*sg_table
;
258 u32 total_sg_needed
, sg_tables
;
259 u32 max_sg_per_table
= (RD_MAX_ALLOCATION_SIZE
/
260 sizeof(struct scatterlist
));
263 if (rd_dev
->rd_flags
& RDF_NULLIO
)
266 * prot_length=8byte dif data
267 * tot sg needed = rd_page_count * (PGSZ/block_size) *
268 * (prot_length/block_size) + pad
269 * PGSZ canceled each other.
271 total_sg_needed
= (rd_dev
->rd_page_count
* prot_length
/ block_size
) + 1;
273 sg_tables
= (total_sg_needed
/ max_sg_per_table
) + 1;
275 sg_table
= kzalloc(sg_tables
* sizeof(struct rd_dev_sg_table
), GFP_KERNEL
);
277 pr_err("Unable to allocate memory for Ramdisk protection"
278 " scatterlist tables\n");
282 rd_dev
->sg_prot_array
= sg_table
;
283 rd_dev
->sg_prot_count
= sg_tables
;
285 rc
= rd_allocate_sgl_table(rd_dev
, sg_table
, total_sg_needed
, 0xff);
289 pr_debug("CORE_RD[%u] - Built Ramdisk Device ID: %u prot space of"
290 " %u pages in %u tables\n", rd_dev
->rd_host
->rd_host_id
,
291 rd_dev
->rd_dev_id
, total_sg_needed
, rd_dev
->sg_prot_count
);
296 static struct se_device
*rd_alloc_device(struct se_hba
*hba
, const char *name
)
298 struct rd_dev
*rd_dev
;
299 struct rd_host
*rd_host
= hba
->hba_ptr
;
301 rd_dev
= kzalloc(sizeof(struct rd_dev
), GFP_KERNEL
);
303 pr_err("Unable to allocate memory for struct rd_dev\n");
307 rd_dev
->rd_host
= rd_host
;
312 static int rd_configure_device(struct se_device
*dev
)
314 struct rd_dev
*rd_dev
= RD_DEV(dev
);
315 struct rd_host
*rd_host
= dev
->se_hba
->hba_ptr
;
318 if (!(rd_dev
->rd_flags
& RDF_HAS_PAGE_COUNT
)) {
319 pr_debug("Missing rd_pages= parameter\n");
323 ret
= rd_build_device_space(rd_dev
);
327 dev
->dev_attrib
.hw_block_size
= RD_BLOCKSIZE
;
328 dev
->dev_attrib
.hw_max_sectors
= UINT_MAX
;
329 dev
->dev_attrib
.hw_queue_depth
= RD_MAX_DEVICE_QUEUE_DEPTH
;
330 dev
->dev_attrib
.is_nonrot
= 1;
332 rd_dev
->rd_dev_id
= rd_host
->rd_host_dev_id_count
++;
334 pr_debug("CORE_RD[%u] - Added TCM MEMCPY Ramdisk Device ID: %u of"
335 " %u pages in %u tables, %lu total bytes\n",
336 rd_host
->rd_host_id
, rd_dev
->rd_dev_id
, rd_dev
->rd_page_count
,
337 rd_dev
->sg_table_count
,
338 (unsigned long)(rd_dev
->rd_page_count
* PAGE_SIZE
));
343 rd_release_device_space(rd_dev
);
347 static void rd_dev_call_rcu(struct rcu_head
*p
)
349 struct se_device
*dev
= container_of(p
, struct se_device
, rcu_head
);
350 struct rd_dev
*rd_dev
= RD_DEV(dev
);
355 static void rd_free_device(struct se_device
*dev
)
357 struct rd_dev
*rd_dev
= RD_DEV(dev
);
359 rd_release_device_space(rd_dev
);
360 call_rcu(&dev
->rcu_head
, rd_dev_call_rcu
);
363 static struct rd_dev_sg_table
*rd_get_sg_table(struct rd_dev
*rd_dev
, u32 page
)
365 struct rd_dev_sg_table
*sg_table
;
366 u32 i
, sg_per_table
= (RD_MAX_ALLOCATION_SIZE
/
367 sizeof(struct scatterlist
));
369 i
= page
/ sg_per_table
;
370 if (i
< rd_dev
->sg_table_count
) {
371 sg_table
= &rd_dev
->sg_table_array
[i
];
372 if ((sg_table
->page_start_offset
<= page
) &&
373 (sg_table
->page_end_offset
>= page
))
377 pr_err("Unable to locate struct rd_dev_sg_table for page: %u\n",
383 static struct rd_dev_sg_table
*rd_get_prot_table(struct rd_dev
*rd_dev
, u32 page
)
385 struct rd_dev_sg_table
*sg_table
;
386 u32 i
, sg_per_table
= (RD_MAX_ALLOCATION_SIZE
/
387 sizeof(struct scatterlist
));
389 i
= page
/ sg_per_table
;
390 if (i
< rd_dev
->sg_prot_count
) {
391 sg_table
= &rd_dev
->sg_prot_array
[i
];
392 if ((sg_table
->page_start_offset
<= page
) &&
393 (sg_table
->page_end_offset
>= page
))
397 pr_err("Unable to locate struct prot rd_dev_sg_table for page: %u\n",
403 static sense_reason_t
rd_do_prot_rw(struct se_cmd
*cmd
, bool is_read
)
405 struct se_device
*se_dev
= cmd
->se_dev
;
406 struct rd_dev
*dev
= RD_DEV(se_dev
);
407 struct rd_dev_sg_table
*prot_table
;
408 struct scatterlist
*prot_sg
;
409 u32 sectors
= cmd
->data_length
/ se_dev
->dev_attrib
.block_size
;
410 u32 prot_offset
, prot_page
;
411 u32 prot_npages __maybe_unused
;
413 sense_reason_t rc
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
415 tmp
= cmd
->t_task_lba
* se_dev
->prot_length
;
416 prot_offset
= do_div(tmp
, PAGE_SIZE
);
419 prot_table
= rd_get_prot_table(dev
, prot_page
);
421 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
423 prot_sg
= &prot_table
->sg_table
[prot_page
-
424 prot_table
->page_start_offset
];
427 rc
= sbc_dif_verify(cmd
, cmd
->t_task_lba
, sectors
, 0,
428 prot_sg
, prot_offset
);
430 rc
= sbc_dif_verify(cmd
, cmd
->t_task_lba
, sectors
, 0,
434 sbc_dif_copy_prot(cmd
, sectors
, is_read
, prot_sg
, prot_offset
);
439 static sense_reason_t
440 rd_execute_rw(struct se_cmd
*cmd
, struct scatterlist
*sgl
, u32 sgl_nents
,
441 enum dma_data_direction data_direction
)
443 struct se_device
*se_dev
= cmd
->se_dev
;
444 struct rd_dev
*dev
= RD_DEV(se_dev
);
445 struct rd_dev_sg_table
*table
;
446 struct scatterlist
*rd_sg
;
447 struct sg_mapping_iter m
;
455 if (dev
->rd_flags
& RDF_NULLIO
) {
456 target_complete_cmd(cmd
, SAM_STAT_GOOD
);
460 tmp
= cmd
->t_task_lba
* se_dev
->dev_attrib
.block_size
;
461 rd_offset
= do_div(tmp
, PAGE_SIZE
);
463 rd_size
= cmd
->data_length
;
465 table
= rd_get_sg_table(dev
, rd_page
);
467 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
469 rd_sg
= &table
->sg_table
[rd_page
- table
->page_start_offset
];
471 pr_debug("RD[%u]: %s LBA: %llu, Size: %u Page: %u, Offset: %u\n",
473 data_direction
== DMA_FROM_DEVICE
? "Read" : "Write",
474 cmd
->t_task_lba
, rd_size
, rd_page
, rd_offset
);
476 if (cmd
->prot_type
&& se_dev
->dev_attrib
.pi_prot_type
&&
477 data_direction
== DMA_TO_DEVICE
) {
478 rc
= rd_do_prot_rw(cmd
, false);
483 src_len
= PAGE_SIZE
- rd_offset
;
484 sg_miter_start(&m
, sgl
, sgl_nents
,
485 data_direction
== DMA_FROM_DEVICE
?
486 SG_MITER_TO_SG
: SG_MITER_FROM_SG
);
492 if (!(u32
)m
.length
) {
493 pr_debug("RD[%u]: invalid sgl %p len %zu\n",
494 dev
->rd_dev_id
, m
.addr
, m
.length
);
496 return TCM_INCORRECT_AMOUNT_OF_DATA
;
498 len
= min((u32
)m
.length
, src_len
);
500 pr_debug("RD[%u]: size underrun page %d offset %d "
501 "size %d\n", dev
->rd_dev_id
,
502 rd_page
, rd_offset
, rd_size
);
507 rd_addr
= sg_virt(rd_sg
) + rd_offset
;
509 if (data_direction
== DMA_FROM_DEVICE
)
510 memcpy(m
.addr
, rd_addr
, len
);
512 memcpy(rd_addr
, m
.addr
, len
);
524 /* rd page completed, next one please */
528 if (rd_page
<= table
->page_end_offset
) {
533 table
= rd_get_sg_table(dev
, rd_page
);
536 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
539 /* since we increment, the first sg entry is correct */
540 rd_sg
= table
->sg_table
;
544 if (cmd
->prot_type
&& se_dev
->dev_attrib
.pi_prot_type
&&
545 data_direction
== DMA_FROM_DEVICE
) {
546 rc
= rd_do_prot_rw(cmd
, true);
551 target_complete_cmd(cmd
, SAM_STAT_GOOD
);
556 Opt_rd_pages
, Opt_rd_nullio
, Opt_err
559 static match_table_t tokens
= {
560 {Opt_rd_pages
, "rd_pages=%d"},
561 {Opt_rd_nullio
, "rd_nullio=%d"},
565 static ssize_t
rd_set_configfs_dev_params(struct se_device
*dev
,
566 const char *page
, ssize_t count
)
568 struct rd_dev
*rd_dev
= RD_DEV(dev
);
569 char *orig
, *ptr
, *opts
;
570 substring_t args
[MAX_OPT_ARGS
];
571 int ret
= 0, arg
, token
;
573 opts
= kstrdup(page
, GFP_KERNEL
);
579 while ((ptr
= strsep(&opts
, ",\n")) != NULL
) {
583 token
= match_token(ptr
, tokens
, args
);
586 match_int(args
, &arg
);
587 rd_dev
->rd_page_count
= arg
;
588 pr_debug("RAMDISK: Referencing Page"
589 " Count: %u\n", rd_dev
->rd_page_count
);
590 rd_dev
->rd_flags
|= RDF_HAS_PAGE_COUNT
;
593 match_int(args
, &arg
);
597 pr_debug("RAMDISK: Setting NULLIO flag: %d\n", arg
);
598 rd_dev
->rd_flags
|= RDF_NULLIO
;
606 return (!ret
) ? count
: ret
;
609 static ssize_t
rd_show_configfs_dev_params(struct se_device
*dev
, char *b
)
611 struct rd_dev
*rd_dev
= RD_DEV(dev
);
613 ssize_t bl
= sprintf(b
, "TCM RamDisk ID: %u RamDisk Makeup: rd_mcp\n",
615 bl
+= sprintf(b
+ bl
, " PAGES/PAGE_SIZE: %u*%lu"
616 " SG_table_count: %u nullio: %d\n", rd_dev
->rd_page_count
,
617 PAGE_SIZE
, rd_dev
->sg_table_count
,
618 !!(rd_dev
->rd_flags
& RDF_NULLIO
));
622 static sector_t
rd_get_blocks(struct se_device
*dev
)
624 struct rd_dev
*rd_dev
= RD_DEV(dev
);
626 unsigned long long blocks_long
= ((rd_dev
->rd_page_count
* PAGE_SIZE
) /
627 dev
->dev_attrib
.block_size
) - 1;
632 static int rd_init_prot(struct se_device
*dev
)
634 struct rd_dev
*rd_dev
= RD_DEV(dev
);
636 if (!dev
->dev_attrib
.pi_prot_type
)
639 return rd_build_prot_space(rd_dev
, dev
->prot_length
,
640 dev
->dev_attrib
.block_size
);
643 static void rd_free_prot(struct se_device
*dev
)
645 struct rd_dev
*rd_dev
= RD_DEV(dev
);
647 rd_release_prot_space(rd_dev
);
650 static struct sbc_ops rd_sbc_ops
= {
651 .execute_rw
= rd_execute_rw
,
654 static sense_reason_t
655 rd_parse_cdb(struct se_cmd
*cmd
)
657 return sbc_parse_cdb(cmd
, &rd_sbc_ops
);
660 static const struct target_backend_ops rd_mcp_ops
= {
662 .inquiry_prod
= "RAMDISK-MCP",
663 .inquiry_rev
= RD_MCP_VERSION
,
664 .attach_hba
= rd_attach_hba
,
665 .detach_hba
= rd_detach_hba
,
666 .alloc_device
= rd_alloc_device
,
667 .configure_device
= rd_configure_device
,
668 .free_device
= rd_free_device
,
669 .parse_cdb
= rd_parse_cdb
,
670 .set_configfs_dev_params
= rd_set_configfs_dev_params
,
671 .show_configfs_dev_params
= rd_show_configfs_dev_params
,
672 .get_device_type
= sbc_get_device_type
,
673 .get_blocks
= rd_get_blocks
,
674 .init_prot
= rd_init_prot
,
675 .free_prot
= rd_free_prot
,
676 .tb_dev_attrib_attrs
= sbc_attrib_attrs
,
679 int __init
rd_module_init(void)
681 return transport_backend_register(&rd_mcp_ops
);
684 void rd_module_exit(void)
686 target_backend_unregister(&rd_mcp_ops
);