1 /*******************************************************************************
2 * Filename: target_core_rd.c
4 * This file contains the Storage Engine <-> Ramdisk transport
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
12 * Nicholas A. Bellinger <nab@kernel.org>
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.
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.
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.
28 ******************************************************************************/
30 #include <linux/version.h>
31 #include <linux/string.h>
32 #include <linux/parser.h>
33 #include <linux/timer.h>
34 #include <linux/blkdev.h>
35 #include <linux/slab.h>
36 #include <linux/spinlock.h>
37 #include <scsi/scsi.h>
38 #include <scsi/scsi_host.h>
40 #include <target/target_core_base.h>
41 #include <target/target_core_device.h>
42 #include <target/target_core_transport.h>
43 #include <target/target_core_fabric_ops.h>
45 #include "target_core_rd.h"
47 static struct se_subsystem_api rd_dr_template
;
48 static struct se_subsystem_api rd_mcp_template
;
50 /* #define DEBUG_RAMDISK_MCP */
51 /* #define DEBUG_RAMDISK_DR */
53 /* rd_attach_hba(): (Part of se_subsystem_api_t template)
57 static int rd_attach_hba(struct se_hba
*hba
, u32 host_id
)
59 struct rd_host
*rd_host
;
61 rd_host
= kzalloc(sizeof(struct rd_host
), GFP_KERNEL
);
63 printk(KERN_ERR
"Unable to allocate memory for struct rd_host\n");
67 rd_host
->rd_host_id
= host_id
;
69 atomic_set(&hba
->left_queue_depth
, RD_HBA_QUEUE_DEPTH
);
70 atomic_set(&hba
->max_queue_depth
, RD_HBA_QUEUE_DEPTH
);
71 hba
->hba_ptr
= (void *) rd_host
;
73 printk(KERN_INFO
"CORE_HBA[%d] - TCM Ramdisk HBA Driver %s on"
74 " Generic Target Core Stack %s\n", hba
->hba_id
,
75 RD_HBA_VERSION
, TARGET_CORE_MOD_VERSION
);
76 printk(KERN_INFO
"CORE_HBA[%d] - Attached Ramdisk HBA: %u to Generic"
77 " Target Core TCQ Depth: %d MaxSectors: %u\n", hba
->hba_id
,
78 rd_host
->rd_host_id
, atomic_read(&hba
->max_queue_depth
),
84 static void rd_detach_hba(struct se_hba
*hba
)
86 struct rd_host
*rd_host
= hba
->hba_ptr
;
88 printk(KERN_INFO
"CORE_HBA[%d] - Detached Ramdisk HBA: %u from"
89 " Generic Target Core\n", hba
->hba_id
, rd_host
->rd_host_id
);
95 /* rd_release_device_space():
99 static void rd_release_device_space(struct rd_dev
*rd_dev
)
101 u32 i
, j
, page_count
= 0, sg_per_table
;
102 struct rd_dev_sg_table
*sg_table
;
104 struct scatterlist
*sg
;
106 if (!rd_dev
->sg_table_array
|| !rd_dev
->sg_table_count
)
109 sg_table
= rd_dev
->sg_table_array
;
111 for (i
= 0; i
< rd_dev
->sg_table_count
; i
++) {
112 sg
= sg_table
[i
].sg_table
;
113 sg_per_table
= sg_table
[i
].rd_sg_count
;
115 for (j
= 0; j
< sg_per_table
; j
++) {
116 pg
= sg_page(&sg
[j
]);
126 printk(KERN_INFO
"CORE_RD[%u] - Released device space for Ramdisk"
127 " Device ID: %u, pages %u in %u tables total bytes %lu\n",
128 rd_dev
->rd_host
->rd_host_id
, rd_dev
->rd_dev_id
, page_count
,
129 rd_dev
->sg_table_count
, (unsigned long)page_count
* PAGE_SIZE
);
132 rd_dev
->sg_table_array
= NULL
;
133 rd_dev
->sg_table_count
= 0;
137 /* rd_build_device_space():
141 static int rd_build_device_space(struct rd_dev
*rd_dev
)
143 u32 i
= 0, j
, page_offset
= 0, sg_per_table
, sg_tables
, total_sg_needed
;
144 u32 max_sg_per_table
= (RD_MAX_ALLOCATION_SIZE
/
145 sizeof(struct scatterlist
));
146 struct rd_dev_sg_table
*sg_table
;
148 struct scatterlist
*sg
;
150 if (rd_dev
->rd_page_count
<= 0) {
151 printk(KERN_ERR
"Illegal page count: %u for Ramdisk device\n",
152 rd_dev
->rd_page_count
);
155 total_sg_needed
= rd_dev
->rd_page_count
;
157 sg_tables
= (total_sg_needed
/ max_sg_per_table
) + 1;
159 sg_table
= kzalloc(sg_tables
* sizeof(struct rd_dev_sg_table
), GFP_KERNEL
);
161 printk(KERN_ERR
"Unable to allocate memory for Ramdisk"
162 " scatterlist tables\n");
166 rd_dev
->sg_table_array
= sg_table
;
167 rd_dev
->sg_table_count
= sg_tables
;
169 while (total_sg_needed
) {
170 sg_per_table
= (total_sg_needed
> max_sg_per_table
) ?
171 max_sg_per_table
: total_sg_needed
;
173 sg
= kzalloc(sg_per_table
* sizeof(struct scatterlist
),
176 printk(KERN_ERR
"Unable to allocate scatterlist array"
177 " for struct rd_dev\n");
181 sg_init_table((struct scatterlist
*)&sg
[0], sg_per_table
);
183 sg_table
[i
].sg_table
= sg
;
184 sg_table
[i
].rd_sg_count
= sg_per_table
;
185 sg_table
[i
].page_start_offset
= page_offset
;
186 sg_table
[i
++].page_end_offset
= (page_offset
+ sg_per_table
)
189 for (j
= 0; j
< sg_per_table
; j
++) {
190 pg
= alloc_pages(GFP_KERNEL
, 0);
192 printk(KERN_ERR
"Unable to allocate scatterlist"
193 " pages for struct rd_dev_sg_table\n");
196 sg_assign_page(&sg
[j
], pg
);
197 sg
[j
].length
= PAGE_SIZE
;
200 page_offset
+= sg_per_table
;
201 total_sg_needed
-= sg_per_table
;
204 printk(KERN_INFO
"CORE_RD[%u] - Built Ramdisk Device ID: %u space of"
205 " %u pages in %u tables\n", rd_dev
->rd_host
->rd_host_id
,
206 rd_dev
->rd_dev_id
, rd_dev
->rd_page_count
,
207 rd_dev
->sg_table_count
);
212 static void *rd_allocate_virtdevice(
217 struct rd_dev
*rd_dev
;
218 struct rd_host
*rd_host
= hba
->hba_ptr
;
220 rd_dev
= kzalloc(sizeof(struct rd_dev
), GFP_KERNEL
);
222 printk(KERN_ERR
"Unable to allocate memory for struct rd_dev\n");
226 rd_dev
->rd_host
= rd_host
;
227 rd_dev
->rd_direct
= rd_direct
;
232 static void *rd_DIRECT_allocate_virtdevice(struct se_hba
*hba
, const char *name
)
234 return rd_allocate_virtdevice(hba
, name
, 1);
237 static void *rd_MEMCPY_allocate_virtdevice(struct se_hba
*hba
, const char *name
)
239 return rd_allocate_virtdevice(hba
, name
, 0);
242 /* rd_create_virtdevice():
246 static struct se_device
*rd_create_virtdevice(
248 struct se_subsystem_dev
*se_dev
,
252 struct se_device
*dev
;
253 struct se_dev_limits dev_limits
;
254 struct rd_dev
*rd_dev
= p
;
255 struct rd_host
*rd_host
= hba
->hba_ptr
;
257 char prod
[16], rev
[4];
259 memset(&dev_limits
, 0, sizeof(struct se_dev_limits
));
261 if (rd_build_device_space(rd_dev
) < 0)
264 snprintf(prod
, 16, "RAMDISK-%s", (rd_dev
->rd_direct
) ? "DR" : "MCP");
265 snprintf(rev
, 4, "%s", (rd_dev
->rd_direct
) ? RD_DR_VERSION
:
268 dev_limits
.limits
.logical_block_size
= RD_BLOCKSIZE
;
269 dev_limits
.limits
.max_hw_sectors
= RD_MAX_SECTORS
;
270 dev_limits
.limits
.max_sectors
= RD_MAX_SECTORS
;
271 dev_limits
.hw_queue_depth
= RD_MAX_DEVICE_QUEUE_DEPTH
;
272 dev_limits
.queue_depth
= RD_DEVICE_QUEUE_DEPTH
;
274 dev
= transport_add_device_to_core_hba(hba
,
275 (rd_dev
->rd_direct
) ? &rd_dr_template
:
276 &rd_mcp_template
, se_dev
, dev_flags
, (void *)rd_dev
,
277 &dev_limits
, prod
, rev
);
281 rd_dev
->rd_dev_id
= rd_host
->rd_host_dev_id_count
++;
282 rd_dev
->rd_queue_depth
= dev
->queue_depth
;
284 printk(KERN_INFO
"CORE_RD[%u] - Added TCM %s Ramdisk Device ID: %u of"
285 " %u pages in %u tables, %lu total bytes\n",
286 rd_host
->rd_host_id
, (!rd_dev
->rd_direct
) ? "MEMCPY" :
287 "DIRECT", rd_dev
->rd_dev_id
, rd_dev
->rd_page_count
,
288 rd_dev
->sg_table_count
,
289 (unsigned long)(rd_dev
->rd_page_count
* PAGE_SIZE
));
294 rd_release_device_space(rd_dev
);
298 static struct se_device
*rd_DIRECT_create_virtdevice(
300 struct se_subsystem_dev
*se_dev
,
303 return rd_create_virtdevice(hba
, se_dev
, p
, 1);
306 static struct se_device
*rd_MEMCPY_create_virtdevice(
308 struct se_subsystem_dev
*se_dev
,
311 return rd_create_virtdevice(hba
, se_dev
, p
, 0);
314 /* rd_free_device(): (Part of se_subsystem_api_t template)
318 static void rd_free_device(void *p
)
320 struct rd_dev
*rd_dev
= p
;
322 rd_release_device_space(rd_dev
);
326 static inline struct rd_request
*RD_REQ(struct se_task
*task
)
328 return container_of(task
, struct rd_request
, rd_task
);
331 static struct se_task
*
332 rd_alloc_task(struct se_cmd
*cmd
)
334 struct rd_request
*rd_req
;
336 rd_req
= kzalloc(sizeof(struct rd_request
), GFP_KERNEL
);
338 printk(KERN_ERR
"Unable to allocate struct rd_request\n");
341 rd_req
->rd_dev
= SE_DEV(cmd
)->dev_ptr
;
343 return &rd_req
->rd_task
;
346 /* rd_get_sg_table():
350 static struct rd_dev_sg_table
*rd_get_sg_table(struct rd_dev
*rd_dev
, u32 page
)
353 struct rd_dev_sg_table
*sg_table
;
355 for (i
= 0; i
< rd_dev
->sg_table_count
; i
++) {
356 sg_table
= &rd_dev
->sg_table_array
[i
];
357 if ((sg_table
->page_start_offset
<= page
) &&
358 (sg_table
->page_end_offset
>= page
))
362 printk(KERN_ERR
"Unable to locate struct rd_dev_sg_table for page: %u\n",
372 static int rd_MEMCPY_read(struct rd_request
*req
)
374 struct se_task
*task
= &req
->rd_task
;
375 struct rd_dev
*dev
= req
->rd_dev
;
376 struct rd_dev_sg_table
*table
;
377 struct scatterlist
*sg_d
, *sg_s
;
379 u32 i
= 0, j
= 0, dst_offset
= 0, src_offset
= 0;
380 u32 length
, page_end
= 0, table_sg_end
;
381 u32 rd_offset
= req
->rd_offset
;
383 table
= rd_get_sg_table(dev
, req
->rd_page
);
387 table_sg_end
= (table
->page_end_offset
- req
->rd_page
);
388 sg_d
= task
->task_sg
;
389 sg_s
= &table
->sg_table
[req
->rd_page
- table
->page_start_offset
];
390 #ifdef DEBUG_RAMDISK_MCP
391 printk(KERN_INFO
"RD[%u]: Read LBA: %llu, Size: %u Page: %u, Offset:"
392 " %u\n", dev
->rd_dev_id
, task
->task_lba
, req
->rd_size
,
393 req
->rd_page
, req
->rd_offset
);
395 src_offset
= rd_offset
;
397 while (req
->rd_size
) {
398 if ((sg_d
[i
].length
- dst_offset
) <
399 (sg_s
[j
].length
- src_offset
)) {
400 length
= (sg_d
[i
].length
- dst_offset
);
401 #ifdef DEBUG_RAMDISK_MCP
402 printk(KERN_INFO
"Step 1 - sg_d[%d]: %p length: %d"
403 " offset: %u sg_s[%d].length: %u\n", i
,
404 &sg_d
[i
], sg_d
[i
].length
, sg_d
[i
].offset
, j
,
406 printk(KERN_INFO
"Step 1 - length: %u dst_offset: %u"
407 " src_offset: %u\n", length
, dst_offset
,
410 if (length
> req
->rd_size
)
411 length
= req
->rd_size
;
413 dst
= sg_virt(&sg_d
[i
++]) + dst_offset
;
417 src
= sg_virt(&sg_s
[j
]) + src_offset
;
425 length
= (sg_s
[j
].length
- src_offset
);
426 #ifdef DEBUG_RAMDISK_MCP
427 printk(KERN_INFO
"Step 2 - sg_d[%d]: %p length: %d"
428 " offset: %u sg_s[%d].length: %u\n", i
,
429 &sg_d
[i
], sg_d
[i
].length
, sg_d
[i
].offset
,
431 printk(KERN_INFO
"Step 2 - length: %u dst_offset: %u"
432 " src_offset: %u\n", length
, dst_offset
,
435 if (length
> req
->rd_size
)
436 length
= req
->rd_size
;
438 dst
= sg_virt(&sg_d
[i
]) + dst_offset
;
442 if (sg_d
[i
].length
== length
) {
448 src
= sg_virt(&sg_s
[j
++]) + src_offset
;
456 memcpy(dst
, src
, length
);
458 #ifdef DEBUG_RAMDISK_MCP
459 printk(KERN_INFO
"page: %u, remaining size: %u, length: %u,"
460 " i: %u, j: %u\n", req
->rd_page
,
461 (req
->rd_size
- length
), length
, i
, j
);
463 req
->rd_size
-= length
;
470 if (++req
->rd_page
<= table
->page_end_offset
) {
471 #ifdef DEBUG_RAMDISK_MCP
472 printk(KERN_INFO
"page: %u in same page table\n",
477 #ifdef DEBUG_RAMDISK_MCP
478 printk(KERN_INFO
"getting new page table for page: %u\n",
481 table
= rd_get_sg_table(dev
, req
->rd_page
);
485 sg_s
= &table
->sg_table
[j
= 0];
491 /* rd_MEMCPY_write():
495 static int rd_MEMCPY_write(struct rd_request
*req
)
497 struct se_task
*task
= &req
->rd_task
;
498 struct rd_dev
*dev
= req
->rd_dev
;
499 struct rd_dev_sg_table
*table
;
500 struct scatterlist
*sg_d
, *sg_s
;
502 u32 i
= 0, j
= 0, dst_offset
= 0, src_offset
= 0;
503 u32 length
, page_end
= 0, table_sg_end
;
504 u32 rd_offset
= req
->rd_offset
;
506 table
= rd_get_sg_table(dev
, req
->rd_page
);
510 table_sg_end
= (table
->page_end_offset
- req
->rd_page
);
511 sg_d
= &table
->sg_table
[req
->rd_page
- table
->page_start_offset
];
512 sg_s
= task
->task_sg
;
513 #ifdef DEBUG_RAMDISK_MCP
514 printk(KERN_INFO
"RD[%d] Write LBA: %llu, Size: %u, Page: %u,"
515 " Offset: %u\n", dev
->rd_dev_id
, task
->task_lba
, req
->rd_size
,
516 req
->rd_page
, req
->rd_offset
);
518 dst_offset
= rd_offset
;
520 while (req
->rd_size
) {
521 if ((sg_s
[i
].length
- src_offset
) <
522 (sg_d
[j
].length
- dst_offset
)) {
523 length
= (sg_s
[i
].length
- src_offset
);
524 #ifdef DEBUG_RAMDISK_MCP
525 printk(KERN_INFO
"Step 1 - sg_s[%d]: %p length: %d"
526 " offset: %d sg_d[%d].length: %u\n", i
,
527 &sg_s
[i
], sg_s
[i
].length
, sg_s
[i
].offset
,
529 printk(KERN_INFO
"Step 1 - length: %u src_offset: %u"
530 " dst_offset: %u\n", length
, src_offset
,
533 if (length
> req
->rd_size
)
534 length
= req
->rd_size
;
536 src
= sg_virt(&sg_s
[i
++]) + src_offset
;
540 dst
= sg_virt(&sg_d
[j
]) + dst_offset
;
548 length
= (sg_d
[j
].length
- dst_offset
);
549 #ifdef DEBUG_RAMDISK_MCP
550 printk(KERN_INFO
"Step 2 - sg_s[%d]: %p length: %d"
551 " offset: %d sg_d[%d].length: %u\n", i
,
552 &sg_s
[i
], sg_s
[i
].length
, sg_s
[i
].offset
,
554 printk(KERN_INFO
"Step 2 - length: %u src_offset: %u"
555 " dst_offset: %u\n", length
, src_offset
,
558 if (length
> req
->rd_size
)
559 length
= req
->rd_size
;
561 src
= sg_virt(&sg_s
[i
]) + src_offset
;
565 if (sg_s
[i
].length
== length
) {
571 dst
= sg_virt(&sg_d
[j
++]) + dst_offset
;
579 memcpy(dst
, src
, length
);
581 #ifdef DEBUG_RAMDISK_MCP
582 printk(KERN_INFO
"page: %u, remaining size: %u, length: %u,"
583 " i: %u, j: %u\n", req
->rd_page
,
584 (req
->rd_size
- length
), length
, i
, j
);
586 req
->rd_size
-= length
;
593 if (++req
->rd_page
<= table
->page_end_offset
) {
594 #ifdef DEBUG_RAMDISK_MCP
595 printk(KERN_INFO
"page: %u in same page table\n",
600 #ifdef DEBUG_RAMDISK_MCP
601 printk(KERN_INFO
"getting new page table for page: %u\n",
604 table
= rd_get_sg_table(dev
, req
->rd_page
);
608 sg_d
= &table
->sg_table
[j
= 0];
614 /* rd_MEMCPY_do_task(): (Part of se_subsystem_api_t template)
618 static int rd_MEMCPY_do_task(struct se_task
*task
)
620 struct se_device
*dev
= task
->se_dev
;
621 struct rd_request
*req
= RD_REQ(task
);
622 unsigned long long lba
;
625 req
->rd_page
= (task
->task_lba
* DEV_ATTRIB(dev
)->block_size
) / PAGE_SIZE
;
626 lba
= task
->task_lba
;
627 req
->rd_offset
= (do_div(lba
,
628 (PAGE_SIZE
/ DEV_ATTRIB(dev
)->block_size
))) *
629 DEV_ATTRIB(dev
)->block_size
;
630 req
->rd_size
= task
->task_size
;
632 if (task
->task_data_direction
== DMA_FROM_DEVICE
)
633 ret
= rd_MEMCPY_read(req
);
635 ret
= rd_MEMCPY_write(req
);
640 task
->task_scsi_status
= GOOD
;
641 transport_complete_task(task
, 1);
643 return PYX_TRANSPORT_SENT_TO_TRANSPORT
;
646 /* rd_DIRECT_with_offset():
650 static int rd_DIRECT_with_offset(
651 struct se_task
*task
,
652 struct list_head
*se_mem_list
,
656 struct rd_request
*req
= RD_REQ(task
);
657 struct rd_dev
*dev
= req
->rd_dev
;
658 struct rd_dev_sg_table
*table
;
659 struct se_mem
*se_mem
;
660 struct scatterlist
*sg_s
;
661 u32 j
= 0, set_offset
= 1;
662 u32 get_next_table
= 0, offset_length
, table_sg_end
;
664 table
= rd_get_sg_table(dev
, req
->rd_page
);
668 table_sg_end
= (table
->page_end_offset
- req
->rd_page
);
669 sg_s
= &table
->sg_table
[req
->rd_page
- table
->page_start_offset
];
670 #ifdef DEBUG_RAMDISK_DR
671 printk(KERN_INFO
"%s DIRECT LBA: %llu, Size: %u Page: %u, Offset: %u\n",
672 (task
->task_data_direction
== DMA_TO_DEVICE
) ?
674 task
->task_lba
, req
->rd_size
, req
->rd_page
, req
->rd_offset
);
676 while (req
->rd_size
) {
677 se_mem
= kmem_cache_zalloc(se_mem_cache
, GFP_KERNEL
);
679 printk(KERN_ERR
"Unable to allocate struct se_mem\n");
682 INIT_LIST_HEAD(&se_mem
->se_list
);
685 offset_length
= sg_s
[j
].length
- req
->rd_offset
;
686 if (offset_length
> req
->rd_size
)
687 offset_length
= req
->rd_size
;
689 se_mem
->se_page
= sg_page(&sg_s
[j
++]);
690 se_mem
->se_off
= req
->rd_offset
;
691 se_mem
->se_len
= offset_length
;
694 get_next_table
= (j
> table_sg_end
);
698 offset_length
= (req
->rd_size
< req
->rd_offset
) ?
699 req
->rd_size
: req
->rd_offset
;
701 se_mem
->se_page
= sg_page(&sg_s
[j
]);
702 se_mem
->se_len
= offset_length
;
707 #ifdef DEBUG_RAMDISK_DR
708 printk(KERN_INFO
"page: %u, size: %u, offset_length: %u, j: %u"
709 " se_mem: %p, se_page: %p se_off: %u se_len: %u\n",
710 req
->rd_page
, req
->rd_size
, offset_length
, j
, se_mem
,
711 se_mem
->se_page
, se_mem
->se_off
, se_mem
->se_len
);
713 list_add_tail(&se_mem
->se_list
, se_mem_list
);
716 req
->rd_size
-= offset_length
;
720 if (!set_offset
&& !get_next_table
)
723 if (++req
->rd_page
<= table
->page_end_offset
) {
724 #ifdef DEBUG_RAMDISK_DR
725 printk(KERN_INFO
"page: %u in same page table\n",
730 #ifdef DEBUG_RAMDISK_DR
731 printk(KERN_INFO
"getting new page table for page: %u\n",
734 table
= rd_get_sg_table(dev
, req
->rd_page
);
738 sg_s
= &table
->sg_table
[j
= 0];
742 T_TASK(task
->task_se_cmd
)->t_tasks_se_num
+= *se_mem_cnt
;
743 #ifdef DEBUG_RAMDISK_DR
744 printk(KERN_INFO
"RD_DR - Allocated %u struct se_mem segments for task\n",
750 /* rd_DIRECT_without_offset():
754 static int rd_DIRECT_without_offset(
755 struct se_task
*task
,
756 struct list_head
*se_mem_list
,
760 struct rd_request
*req
= RD_REQ(task
);
761 struct rd_dev
*dev
= req
->rd_dev
;
762 struct rd_dev_sg_table
*table
;
763 struct se_mem
*se_mem
;
764 struct scatterlist
*sg_s
;
767 table
= rd_get_sg_table(dev
, req
->rd_page
);
771 sg_s
= &table
->sg_table
[req
->rd_page
- table
->page_start_offset
];
772 #ifdef DEBUG_RAMDISK_DR
773 printk(KERN_INFO
"%s DIRECT LBA: %llu, Size: %u, Page: %u\n",
774 (task
->task_data_direction
== DMA_TO_DEVICE
) ?
776 task
->task_lba
, req
->rd_size
, req
->rd_page
);
778 while (req
->rd_size
) {
779 se_mem
= kmem_cache_zalloc(se_mem_cache
, GFP_KERNEL
);
781 printk(KERN_ERR
"Unable to allocate struct se_mem\n");
784 INIT_LIST_HEAD(&se_mem
->se_list
);
786 length
= (req
->rd_size
< sg_s
[j
].length
) ?
787 req
->rd_size
: sg_s
[j
].length
;
789 se_mem
->se_page
= sg_page(&sg_s
[j
++]);
790 se_mem
->se_len
= length
;
792 #ifdef DEBUG_RAMDISK_DR
793 printk(KERN_INFO
"page: %u, size: %u, j: %u se_mem: %p,"
794 " se_page: %p se_off: %u se_len: %u\n", req
->rd_page
,
795 req
->rd_size
, j
, se_mem
, se_mem
->se_page
,
796 se_mem
->se_off
, se_mem
->se_len
);
798 list_add_tail(&se_mem
->se_list
, se_mem_list
);
801 req
->rd_size
-= length
;
805 if (++req
->rd_page
<= table
->page_end_offset
) {
806 #ifdef DEBUG_RAMDISK_DR
807 printk("page: %u in same page table\n",
812 #ifdef DEBUG_RAMDISK_DR
813 printk(KERN_INFO
"getting new page table for page: %u\n",
816 table
= rd_get_sg_table(dev
, req
->rd_page
);
820 sg_s
= &table
->sg_table
[j
= 0];
824 T_TASK(task
->task_se_cmd
)->t_tasks_se_num
+= *se_mem_cnt
;
825 #ifdef DEBUG_RAMDISK_DR
826 printk(KERN_INFO
"RD_DR - Allocated %u struct se_mem segments for task\n",
832 /* rd_DIRECT_do_se_mem_map():
836 static int rd_DIRECT_do_se_mem_map(
837 struct se_task
*task
,
838 struct list_head
*se_mem_list
,
840 struct se_mem
*in_se_mem
,
841 struct se_mem
**out_se_mem
,
845 struct se_cmd
*cmd
= task
->task_se_cmd
;
846 struct rd_request
*req
= RD_REQ(task
);
847 u32 task_offset
= *task_offset_in
;
848 unsigned long long lba
;
851 req
->rd_page
= ((task
->task_lba
* DEV_ATTRIB(task
->se_dev
)->block_size
) /
853 lba
= task
->task_lba
;
854 req
->rd_offset
= (do_div(lba
,
855 (PAGE_SIZE
/ DEV_ATTRIB(task
->se_dev
)->block_size
))) *
856 DEV_ATTRIB(task
->se_dev
)->block_size
;
857 req
->rd_size
= task
->task_size
;
860 ret
= rd_DIRECT_with_offset(task
, se_mem_list
, se_mem_cnt
,
863 ret
= rd_DIRECT_without_offset(task
, se_mem_list
, se_mem_cnt
,
869 if (CMD_TFO(cmd
)->task_sg_chaining
== 0)
872 * Currently prevent writers from multiple HW fabrics doing
873 * pci_map_sg() to RD_DR's internal scatterlist memory.
875 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
876 printk(KERN_ERR
"DMA_TO_DEVICE not supported for"
877 " RAMDISK_DR with task_sg_chaining=1\n");
881 * Special case for if task_sg_chaining is enabled, then
882 * we setup struct se_task->task_sg[], as it will be used by
883 * transport_do_task_sg_chain() for creating chainged SGLs
884 * across multiple struct se_task->task_sg[].
886 if (!(transport_calc_sg_num(task
,
887 list_entry(T_TASK(cmd
)->t_mem_list
->next
,
888 struct se_mem
, se_list
),
892 return transport_map_mem_to_sg(task
, se_mem_list
, task
->task_sg
,
893 list_entry(T_TASK(cmd
)->t_mem_list
->next
,
894 struct se_mem
, se_list
),
895 out_se_mem
, se_mem_cnt
, task_offset_in
);
898 /* rd_DIRECT_do_task(): (Part of se_subsystem_api_t template)
902 static int rd_DIRECT_do_task(struct se_task
*task
)
905 * At this point the locally allocated RD tables have been mapped
906 * to struct se_mem elements in rd_DIRECT_do_se_mem_map().
908 task
->task_scsi_status
= GOOD
;
909 transport_complete_task(task
, 1);
911 return PYX_TRANSPORT_SENT_TO_TRANSPORT
;
914 /* rd_free_task(): (Part of se_subsystem_api_t template)
918 static void rd_free_task(struct se_task
*task
)
924 Opt_rd_pages
, Opt_err
927 static match_table_t tokens
= {
928 {Opt_rd_pages
, "rd_pages=%d"},
932 static ssize_t
rd_set_configfs_dev_params(
934 struct se_subsystem_dev
*se_dev
,
938 struct rd_dev
*rd_dev
= se_dev
->se_dev_su_ptr
;
939 char *orig
, *ptr
, *opts
;
940 substring_t args
[MAX_OPT_ARGS
];
941 int ret
= 0, arg
, token
;
943 opts
= kstrdup(page
, GFP_KERNEL
);
949 while ((ptr
= strsep(&opts
, ",")) != NULL
) {
953 token
= match_token(ptr
, tokens
, args
);
956 match_int(args
, &arg
);
957 rd_dev
->rd_page_count
= arg
;
958 printk(KERN_INFO
"RAMDISK: Referencing Page"
959 " Count: %u\n", rd_dev
->rd_page_count
);
960 rd_dev
->rd_flags
|= RDF_HAS_PAGE_COUNT
;
968 return (!ret
) ? count
: ret
;
971 static ssize_t
rd_check_configfs_dev_params(struct se_hba
*hba
, struct se_subsystem_dev
*se_dev
)
973 struct rd_dev
*rd_dev
= se_dev
->se_dev_su_ptr
;
975 if (!(rd_dev
->rd_flags
& RDF_HAS_PAGE_COUNT
)) {
976 printk(KERN_INFO
"Missing rd_pages= parameter\n");
983 static ssize_t
rd_show_configfs_dev_params(
985 struct se_subsystem_dev
*se_dev
,
988 struct rd_dev
*rd_dev
= se_dev
->se_dev_su_ptr
;
989 ssize_t bl
= sprintf(b
, "TCM RamDisk ID: %u RamDisk Makeup: %s\n",
990 rd_dev
->rd_dev_id
, (rd_dev
->rd_direct
) ?
991 "rd_direct" : "rd_mcp");
992 bl
+= sprintf(b
+ bl
, " PAGES/PAGE_SIZE: %u*%lu"
993 " SG_table_count: %u\n", rd_dev
->rd_page_count
,
994 PAGE_SIZE
, rd_dev
->sg_table_count
);
998 /* rd_get_cdb(): (Part of se_subsystem_api_t template)
1002 static unsigned char *rd_get_cdb(struct se_task
*task
)
1004 struct rd_request
*req
= RD_REQ(task
);
1006 return req
->rd_scsi_cdb
;
1009 static u32
rd_get_device_rev(struct se_device
*dev
)
1011 return SCSI_SPC_2
; /* Returns SPC-3 in Initiator Data */
1014 static u32
rd_get_device_type(struct se_device
*dev
)
1019 static sector_t
rd_get_blocks(struct se_device
*dev
)
1021 struct rd_dev
*rd_dev
= dev
->dev_ptr
;
1022 unsigned long long blocks_long
= ((rd_dev
->rd_page_count
* PAGE_SIZE
) /
1023 DEV_ATTRIB(dev
)->block_size
) - 1;
1028 static struct se_subsystem_api rd_dr_template
= {
1030 .transport_type
= TRANSPORT_PLUGIN_VHBA_VDEV
,
1031 .attach_hba
= rd_attach_hba
,
1032 .detach_hba
= rd_detach_hba
,
1033 .allocate_virtdevice
= rd_DIRECT_allocate_virtdevice
,
1034 .create_virtdevice
= rd_DIRECT_create_virtdevice
,
1035 .free_device
= rd_free_device
,
1036 .alloc_task
= rd_alloc_task
,
1037 .do_task
= rd_DIRECT_do_task
,
1038 .free_task
= rd_free_task
,
1039 .check_configfs_dev_params
= rd_check_configfs_dev_params
,
1040 .set_configfs_dev_params
= rd_set_configfs_dev_params
,
1041 .show_configfs_dev_params
= rd_show_configfs_dev_params
,
1042 .get_cdb
= rd_get_cdb
,
1043 .get_device_rev
= rd_get_device_rev
,
1044 .get_device_type
= rd_get_device_type
,
1045 .get_blocks
= rd_get_blocks
,
1046 .do_se_mem_map
= rd_DIRECT_do_se_mem_map
,
1049 static struct se_subsystem_api rd_mcp_template
= {
1051 .transport_type
= TRANSPORT_PLUGIN_VHBA_VDEV
,
1052 .attach_hba
= rd_attach_hba
,
1053 .detach_hba
= rd_detach_hba
,
1054 .allocate_virtdevice
= rd_MEMCPY_allocate_virtdevice
,
1055 .create_virtdevice
= rd_MEMCPY_create_virtdevice
,
1056 .free_device
= rd_free_device
,
1057 .alloc_task
= rd_alloc_task
,
1058 .do_task
= rd_MEMCPY_do_task
,
1059 .free_task
= rd_free_task
,
1060 .check_configfs_dev_params
= rd_check_configfs_dev_params
,
1061 .set_configfs_dev_params
= rd_set_configfs_dev_params
,
1062 .show_configfs_dev_params
= rd_show_configfs_dev_params
,
1063 .get_cdb
= rd_get_cdb
,
1064 .get_device_rev
= rd_get_device_rev
,
1065 .get_device_type
= rd_get_device_type
,
1066 .get_blocks
= rd_get_blocks
,
1069 int __init
rd_module_init(void)
1073 ret
= transport_subsystem_register(&rd_dr_template
);
1077 ret
= transport_subsystem_register(&rd_mcp_template
);
1079 transport_subsystem_release(&rd_dr_template
);
1086 void rd_module_exit(void)
1088 transport_subsystem_release(&rd_dr_template
);
1089 transport_subsystem_release(&rd_mcp_template
);