Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / target / target_core_rd.c
blobe567e129c69746b197ab0c2c4e6addfc008a3630
1 /*******************************************************************************
2 * Filename: target_core_rd.c
4 * This file contains the Storage Engine <-> Ramdisk transport
5 * specific functions.
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_mcp_template;
49 /* rd_attach_hba(): (Part of se_subsystem_api_t template)
53 static int rd_attach_hba(struct se_hba *hba, u32 host_id)
55 struct rd_host *rd_host;
57 rd_host = kzalloc(sizeof(struct rd_host), GFP_KERNEL);
58 if (!rd_host) {
59 pr_err("Unable to allocate memory for struct rd_host\n");
60 return -ENOMEM;
63 rd_host->rd_host_id = host_id;
65 hba->hba_ptr = rd_host;
67 pr_debug("CORE_HBA[%d] - TCM Ramdisk HBA Driver %s on"
68 " Generic Target Core Stack %s\n", hba->hba_id,
69 RD_HBA_VERSION, TARGET_CORE_MOD_VERSION);
70 pr_debug("CORE_HBA[%d] - Attached Ramdisk HBA: %u to Generic"
71 " MaxSectors: %u\n", hba->hba_id,
72 rd_host->rd_host_id, RD_MAX_SECTORS);
74 return 0;
77 static void rd_detach_hba(struct se_hba *hba)
79 struct rd_host *rd_host = hba->hba_ptr;
81 pr_debug("CORE_HBA[%d] - Detached Ramdisk HBA: %u from"
82 " Generic Target Core\n", hba->hba_id, rd_host->rd_host_id);
84 kfree(rd_host);
85 hba->hba_ptr = NULL;
88 /* rd_release_device_space():
92 static void rd_release_device_space(struct rd_dev *rd_dev)
94 u32 i, j, page_count = 0, sg_per_table;
95 struct rd_dev_sg_table *sg_table;
96 struct page *pg;
97 struct scatterlist *sg;
99 if (!rd_dev->sg_table_array || !rd_dev->sg_table_count)
100 return;
102 sg_table = rd_dev->sg_table_array;
104 for (i = 0; i < rd_dev->sg_table_count; i++) {
105 sg = sg_table[i].sg_table;
106 sg_per_table = sg_table[i].rd_sg_count;
108 for (j = 0; j < sg_per_table; j++) {
109 pg = sg_page(&sg[j]);
110 if (pg) {
111 __free_page(pg);
112 page_count++;
116 kfree(sg);
119 pr_debug("CORE_RD[%u] - Released device space for Ramdisk"
120 " Device ID: %u, pages %u in %u tables total bytes %lu\n",
121 rd_dev->rd_host->rd_host_id, rd_dev->rd_dev_id, page_count,
122 rd_dev->sg_table_count, (unsigned long)page_count * PAGE_SIZE);
124 kfree(sg_table);
125 rd_dev->sg_table_array = NULL;
126 rd_dev->sg_table_count = 0;
130 /* rd_build_device_space():
134 static int rd_build_device_space(struct rd_dev *rd_dev)
136 u32 i = 0, j, page_offset = 0, sg_per_table, sg_tables, total_sg_needed;
137 u32 max_sg_per_table = (RD_MAX_ALLOCATION_SIZE /
138 sizeof(struct scatterlist));
139 struct rd_dev_sg_table *sg_table;
140 struct page *pg;
141 struct scatterlist *sg;
143 if (rd_dev->rd_page_count <= 0) {
144 pr_err("Illegal page count: %u for Ramdisk device\n",
145 rd_dev->rd_page_count);
146 return -EINVAL;
148 total_sg_needed = rd_dev->rd_page_count;
150 sg_tables = (total_sg_needed / max_sg_per_table) + 1;
152 sg_table = kzalloc(sg_tables * sizeof(struct rd_dev_sg_table), GFP_KERNEL);
153 if (!sg_table) {
154 pr_err("Unable to allocate memory for Ramdisk"
155 " scatterlist tables\n");
156 return -ENOMEM;
159 rd_dev->sg_table_array = sg_table;
160 rd_dev->sg_table_count = sg_tables;
162 while (total_sg_needed) {
163 sg_per_table = (total_sg_needed > max_sg_per_table) ?
164 max_sg_per_table : total_sg_needed;
166 sg = kzalloc(sg_per_table * sizeof(struct scatterlist),
167 GFP_KERNEL);
168 if (!sg) {
169 pr_err("Unable to allocate scatterlist array"
170 " for struct rd_dev\n");
171 return -ENOMEM;
174 sg_init_table(sg, sg_per_table);
176 sg_table[i].sg_table = sg;
177 sg_table[i].rd_sg_count = sg_per_table;
178 sg_table[i].page_start_offset = page_offset;
179 sg_table[i++].page_end_offset = (page_offset + sg_per_table)
180 - 1;
182 for (j = 0; j < sg_per_table; j++) {
183 pg = alloc_pages(GFP_KERNEL, 0);
184 if (!pg) {
185 pr_err("Unable to allocate scatterlist"
186 " pages for struct rd_dev_sg_table\n");
187 return -ENOMEM;
189 sg_assign_page(&sg[j], pg);
190 sg[j].length = PAGE_SIZE;
193 page_offset += sg_per_table;
194 total_sg_needed -= sg_per_table;
197 pr_debug("CORE_RD[%u] - Built Ramdisk Device ID: %u space of"
198 " %u pages in %u tables\n", rd_dev->rd_host->rd_host_id,
199 rd_dev->rd_dev_id, rd_dev->rd_page_count,
200 rd_dev->sg_table_count);
202 return 0;
205 static void *rd_allocate_virtdevice(
206 struct se_hba *hba,
207 const char *name,
208 int rd_direct)
210 struct rd_dev *rd_dev;
211 struct rd_host *rd_host = hba->hba_ptr;
213 rd_dev = kzalloc(sizeof(struct rd_dev), GFP_KERNEL);
214 if (!rd_dev) {
215 pr_err("Unable to allocate memory for struct rd_dev\n");
216 return NULL;
219 rd_dev->rd_host = rd_host;
220 rd_dev->rd_direct = rd_direct;
222 return rd_dev;
225 static void *rd_MEMCPY_allocate_virtdevice(struct se_hba *hba, const char *name)
227 return rd_allocate_virtdevice(hba, name, 0);
230 /* rd_create_virtdevice():
234 static struct se_device *rd_create_virtdevice(
235 struct se_hba *hba,
236 struct se_subsystem_dev *se_dev,
237 void *p,
238 int rd_direct)
240 struct se_device *dev;
241 struct se_dev_limits dev_limits;
242 struct rd_dev *rd_dev = p;
243 struct rd_host *rd_host = hba->hba_ptr;
244 int dev_flags = 0, ret;
245 char prod[16], rev[4];
247 memset(&dev_limits, 0, sizeof(struct se_dev_limits));
249 ret = rd_build_device_space(rd_dev);
250 if (ret < 0)
251 goto fail;
253 snprintf(prod, 16, "RAMDISK-%s", (rd_dev->rd_direct) ? "DR" : "MCP");
254 snprintf(rev, 4, "%s", (rd_dev->rd_direct) ? RD_DR_VERSION :
255 RD_MCP_VERSION);
257 dev_limits.limits.logical_block_size = RD_BLOCKSIZE;
258 dev_limits.limits.max_hw_sectors = RD_MAX_SECTORS;
259 dev_limits.limits.max_sectors = RD_MAX_SECTORS;
260 dev_limits.hw_queue_depth = RD_MAX_DEVICE_QUEUE_DEPTH;
261 dev_limits.queue_depth = RD_DEVICE_QUEUE_DEPTH;
263 dev = transport_add_device_to_core_hba(hba,
264 &rd_mcp_template, se_dev, dev_flags, rd_dev,
265 &dev_limits, prod, rev);
266 if (!dev)
267 goto fail;
269 rd_dev->rd_dev_id = rd_host->rd_host_dev_id_count++;
270 rd_dev->rd_queue_depth = dev->queue_depth;
272 pr_debug("CORE_RD[%u] - Added TCM %s Ramdisk Device ID: %u of"
273 " %u pages in %u tables, %lu total bytes\n",
274 rd_host->rd_host_id, (!rd_dev->rd_direct) ? "MEMCPY" :
275 "DIRECT", rd_dev->rd_dev_id, rd_dev->rd_page_count,
276 rd_dev->sg_table_count,
277 (unsigned long)(rd_dev->rd_page_count * PAGE_SIZE));
279 return dev;
281 fail:
282 rd_release_device_space(rd_dev);
283 return ERR_PTR(ret);
286 static struct se_device *rd_MEMCPY_create_virtdevice(
287 struct se_hba *hba,
288 struct se_subsystem_dev *se_dev,
289 void *p)
291 return rd_create_virtdevice(hba, se_dev, p, 0);
294 /* rd_free_device(): (Part of se_subsystem_api_t template)
298 static void rd_free_device(void *p)
300 struct rd_dev *rd_dev = p;
302 rd_release_device_space(rd_dev);
303 kfree(rd_dev);
306 static inline struct rd_request *RD_REQ(struct se_task *task)
308 return container_of(task, struct rd_request, rd_task);
311 static struct se_task *
312 rd_alloc_task(unsigned char *cdb)
314 struct rd_request *rd_req;
316 rd_req = kzalloc(sizeof(struct rd_request), GFP_KERNEL);
317 if (!rd_req) {
318 pr_err("Unable to allocate struct rd_request\n");
319 return NULL;
322 return &rd_req->rd_task;
325 /* rd_get_sg_table():
329 static struct rd_dev_sg_table *rd_get_sg_table(struct rd_dev *rd_dev, u32 page)
331 u32 i;
332 struct rd_dev_sg_table *sg_table;
334 for (i = 0; i < rd_dev->sg_table_count; i++) {
335 sg_table = &rd_dev->sg_table_array[i];
336 if ((sg_table->page_start_offset <= page) &&
337 (sg_table->page_end_offset >= page))
338 return sg_table;
341 pr_err("Unable to locate struct rd_dev_sg_table for page: %u\n",
342 page);
344 return NULL;
347 /* rd_MEMCPY_read():
351 static int rd_MEMCPY_read(struct rd_request *req)
353 struct se_task *task = &req->rd_task;
354 struct rd_dev *dev = req->rd_task.se_dev->dev_ptr;
355 struct rd_dev_sg_table *table;
356 struct scatterlist *sg_d, *sg_s;
357 void *dst, *src;
358 u32 i = 0, j = 0, dst_offset = 0, src_offset = 0;
359 u32 length, page_end = 0, table_sg_end;
360 u32 rd_offset = req->rd_offset;
362 table = rd_get_sg_table(dev, req->rd_page);
363 if (!table)
364 return -EINVAL;
366 table_sg_end = (table->page_end_offset - req->rd_page);
367 sg_d = task->task_sg;
368 sg_s = &table->sg_table[req->rd_page - table->page_start_offset];
370 pr_debug("RD[%u]: Read LBA: %llu, Size: %u Page: %u, Offset:"
371 " %u\n", dev->rd_dev_id, task->task_lba, req->rd_size,
372 req->rd_page, req->rd_offset);
374 src_offset = rd_offset;
376 while (req->rd_size) {
377 if ((sg_d[i].length - dst_offset) <
378 (sg_s[j].length - src_offset)) {
379 length = (sg_d[i].length - dst_offset);
381 pr_debug("Step 1 - sg_d[%d]: %p length: %d"
382 " offset: %u sg_s[%d].length: %u\n", i,
383 &sg_d[i], sg_d[i].length, sg_d[i].offset, j,
384 sg_s[j].length);
385 pr_debug("Step 1 - length: %u dst_offset: %u"
386 " src_offset: %u\n", length, dst_offset,
387 src_offset);
389 if (length > req->rd_size)
390 length = req->rd_size;
392 dst = sg_virt(&sg_d[i++]) + dst_offset;
393 BUG_ON(!dst);
395 src = sg_virt(&sg_s[j]) + src_offset;
396 BUG_ON(!src);
398 dst_offset = 0;
399 src_offset = length;
400 page_end = 0;
401 } else {
402 length = (sg_s[j].length - src_offset);
404 pr_debug("Step 2 - sg_d[%d]: %p length: %d"
405 " offset: %u sg_s[%d].length: %u\n", i,
406 &sg_d[i], sg_d[i].length, sg_d[i].offset,
407 j, sg_s[j].length);
408 pr_debug("Step 2 - length: %u dst_offset: %u"
409 " src_offset: %u\n", length, dst_offset,
410 src_offset);
412 if (length > req->rd_size)
413 length = req->rd_size;
415 dst = sg_virt(&sg_d[i]) + dst_offset;
416 BUG_ON(!dst);
418 if (sg_d[i].length == length) {
419 i++;
420 dst_offset = 0;
421 } else
422 dst_offset = length;
424 src = sg_virt(&sg_s[j++]) + src_offset;
425 BUG_ON(!src);
427 src_offset = 0;
428 page_end = 1;
431 memcpy(dst, src, length);
433 pr_debug("page: %u, remaining size: %u, length: %u,"
434 " i: %u, j: %u\n", req->rd_page,
435 (req->rd_size - length), length, i, j);
437 req->rd_size -= length;
438 if (!req->rd_size)
439 return 0;
441 if (!page_end)
442 continue;
444 if (++req->rd_page <= table->page_end_offset) {
445 pr_debug("page: %u in same page table\n",
446 req->rd_page);
447 continue;
450 pr_debug("getting new page table for page: %u\n",
451 req->rd_page);
453 table = rd_get_sg_table(dev, req->rd_page);
454 if (!table)
455 return -EINVAL;
457 sg_s = &table->sg_table[j = 0];
460 return 0;
463 /* rd_MEMCPY_write():
467 static int rd_MEMCPY_write(struct rd_request *req)
469 struct se_task *task = &req->rd_task;
470 struct rd_dev *dev = req->rd_task.se_dev->dev_ptr;
471 struct rd_dev_sg_table *table;
472 struct scatterlist *sg_d, *sg_s;
473 void *dst, *src;
474 u32 i = 0, j = 0, dst_offset = 0, src_offset = 0;
475 u32 length, page_end = 0, table_sg_end;
476 u32 rd_offset = req->rd_offset;
478 table = rd_get_sg_table(dev, req->rd_page);
479 if (!table)
480 return -EINVAL;
482 table_sg_end = (table->page_end_offset - req->rd_page);
483 sg_d = &table->sg_table[req->rd_page - table->page_start_offset];
484 sg_s = task->task_sg;
486 pr_debug("RD[%d] Write LBA: %llu, Size: %u, Page: %u,"
487 " Offset: %u\n", dev->rd_dev_id, task->task_lba, req->rd_size,
488 req->rd_page, req->rd_offset);
490 dst_offset = rd_offset;
492 while (req->rd_size) {
493 if ((sg_s[i].length - src_offset) <
494 (sg_d[j].length - dst_offset)) {
495 length = (sg_s[i].length - src_offset);
497 pr_debug("Step 1 - sg_s[%d]: %p length: %d"
498 " offset: %d sg_d[%d].length: %u\n", i,
499 &sg_s[i], sg_s[i].length, sg_s[i].offset,
500 j, sg_d[j].length);
501 pr_debug("Step 1 - length: %u src_offset: %u"
502 " dst_offset: %u\n", length, src_offset,
503 dst_offset);
505 if (length > req->rd_size)
506 length = req->rd_size;
508 src = sg_virt(&sg_s[i++]) + src_offset;
509 BUG_ON(!src);
511 dst = sg_virt(&sg_d[j]) + dst_offset;
512 BUG_ON(!dst);
514 src_offset = 0;
515 dst_offset = length;
516 page_end = 0;
517 } else {
518 length = (sg_d[j].length - dst_offset);
520 pr_debug("Step 2 - sg_s[%d]: %p length: %d"
521 " offset: %d sg_d[%d].length: %u\n", i,
522 &sg_s[i], sg_s[i].length, sg_s[i].offset,
523 j, sg_d[j].length);
524 pr_debug("Step 2 - length: %u src_offset: %u"
525 " dst_offset: %u\n", length, src_offset,
526 dst_offset);
528 if (length > req->rd_size)
529 length = req->rd_size;
531 src = sg_virt(&sg_s[i]) + src_offset;
532 BUG_ON(!src);
534 if (sg_s[i].length == length) {
535 i++;
536 src_offset = 0;
537 } else
538 src_offset = length;
540 dst = sg_virt(&sg_d[j++]) + dst_offset;
541 BUG_ON(!dst);
543 dst_offset = 0;
544 page_end = 1;
547 memcpy(dst, src, length);
549 pr_debug("page: %u, remaining size: %u, length: %u,"
550 " i: %u, j: %u\n", req->rd_page,
551 (req->rd_size - length), length, i, j);
553 req->rd_size -= length;
554 if (!req->rd_size)
555 return 0;
557 if (!page_end)
558 continue;
560 if (++req->rd_page <= table->page_end_offset) {
561 pr_debug("page: %u in same page table\n",
562 req->rd_page);
563 continue;
566 pr_debug("getting new page table for page: %u\n",
567 req->rd_page);
569 table = rd_get_sg_table(dev, req->rd_page);
570 if (!table)
571 return -EINVAL;
573 sg_d = &table->sg_table[j = 0];
576 return 0;
579 /* rd_MEMCPY_do_task(): (Part of se_subsystem_api_t template)
583 static int rd_MEMCPY_do_task(struct se_task *task)
585 struct se_device *dev = task->se_dev;
586 struct rd_request *req = RD_REQ(task);
587 unsigned long long lba;
588 int ret;
590 req->rd_page = (task->task_lba * dev->se_sub_dev->se_dev_attrib.block_size) / PAGE_SIZE;
591 lba = task->task_lba;
592 req->rd_offset = (do_div(lba,
593 (PAGE_SIZE / dev->se_sub_dev->se_dev_attrib.block_size))) *
594 dev->se_sub_dev->se_dev_attrib.block_size;
595 req->rd_size = task->task_size;
597 if (task->task_data_direction == DMA_FROM_DEVICE)
598 ret = rd_MEMCPY_read(req);
599 else
600 ret = rd_MEMCPY_write(req);
602 if (ret != 0)
603 return ret;
605 task->task_scsi_status = GOOD;
606 transport_complete_task(task, 1);
608 return PYX_TRANSPORT_SENT_TO_TRANSPORT;
611 /* rd_free_task(): (Part of se_subsystem_api_t template)
615 static void rd_free_task(struct se_task *task)
617 kfree(RD_REQ(task));
620 enum {
621 Opt_rd_pages, Opt_err
624 static match_table_t tokens = {
625 {Opt_rd_pages, "rd_pages=%d"},
626 {Opt_err, NULL}
629 static ssize_t rd_set_configfs_dev_params(
630 struct se_hba *hba,
631 struct se_subsystem_dev *se_dev,
632 const char *page,
633 ssize_t count)
635 struct rd_dev *rd_dev = se_dev->se_dev_su_ptr;
636 char *orig, *ptr, *opts;
637 substring_t args[MAX_OPT_ARGS];
638 int ret = 0, arg, token;
640 opts = kstrdup(page, GFP_KERNEL);
641 if (!opts)
642 return -ENOMEM;
644 orig = opts;
646 while ((ptr = strsep(&opts, ",")) != NULL) {
647 if (!*ptr)
648 continue;
650 token = match_token(ptr, tokens, args);
651 switch (token) {
652 case Opt_rd_pages:
653 match_int(args, &arg);
654 rd_dev->rd_page_count = arg;
655 pr_debug("RAMDISK: Referencing Page"
656 " Count: %u\n", rd_dev->rd_page_count);
657 rd_dev->rd_flags |= RDF_HAS_PAGE_COUNT;
658 break;
659 default:
660 break;
664 kfree(orig);
665 return (!ret) ? count : ret;
668 static ssize_t rd_check_configfs_dev_params(struct se_hba *hba, struct se_subsystem_dev *se_dev)
670 struct rd_dev *rd_dev = se_dev->se_dev_su_ptr;
672 if (!(rd_dev->rd_flags & RDF_HAS_PAGE_COUNT)) {
673 pr_debug("Missing rd_pages= parameter\n");
674 return -EINVAL;
677 return 0;
680 static ssize_t rd_show_configfs_dev_params(
681 struct se_hba *hba,
682 struct se_subsystem_dev *se_dev,
683 char *b)
685 struct rd_dev *rd_dev = se_dev->se_dev_su_ptr;
686 ssize_t bl = sprintf(b, "TCM RamDisk ID: %u RamDisk Makeup: %s\n",
687 rd_dev->rd_dev_id, (rd_dev->rd_direct) ?
688 "rd_direct" : "rd_mcp");
689 bl += sprintf(b + bl, " PAGES/PAGE_SIZE: %u*%lu"
690 " SG_table_count: %u\n", rd_dev->rd_page_count,
691 PAGE_SIZE, rd_dev->sg_table_count);
692 return bl;
695 /* rd_get_cdb(): (Part of se_subsystem_api_t template)
699 static unsigned char *rd_get_cdb(struct se_task *task)
701 struct rd_request *req = RD_REQ(task);
703 return req->rd_scsi_cdb;
706 static u32 rd_get_device_rev(struct se_device *dev)
708 return SCSI_SPC_2; /* Returns SPC-3 in Initiator Data */
711 static u32 rd_get_device_type(struct se_device *dev)
713 return TYPE_DISK;
716 static sector_t rd_get_blocks(struct se_device *dev)
718 struct rd_dev *rd_dev = dev->dev_ptr;
719 unsigned long long blocks_long = ((rd_dev->rd_page_count * PAGE_SIZE) /
720 dev->se_sub_dev->se_dev_attrib.block_size) - 1;
722 return blocks_long;
725 static struct se_subsystem_api rd_mcp_template = {
726 .name = "rd_mcp",
727 .transport_type = TRANSPORT_PLUGIN_VHBA_VDEV,
728 .attach_hba = rd_attach_hba,
729 .detach_hba = rd_detach_hba,
730 .allocate_virtdevice = rd_MEMCPY_allocate_virtdevice,
731 .create_virtdevice = rd_MEMCPY_create_virtdevice,
732 .free_device = rd_free_device,
733 .alloc_task = rd_alloc_task,
734 .do_task = rd_MEMCPY_do_task,
735 .free_task = rd_free_task,
736 .check_configfs_dev_params = rd_check_configfs_dev_params,
737 .set_configfs_dev_params = rd_set_configfs_dev_params,
738 .show_configfs_dev_params = rd_show_configfs_dev_params,
739 .get_cdb = rd_get_cdb,
740 .get_device_rev = rd_get_device_rev,
741 .get_device_type = rd_get_device_type,
742 .get_blocks = rd_get_blocks,
745 int __init rd_module_init(void)
747 int ret;
749 ret = transport_subsystem_register(&rd_mcp_template);
750 if (ret < 0) {
751 return ret;
754 return 0;
757 void rd_module_exit(void)
759 transport_subsystem_release(&rd_mcp_template);