target_core_rd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /*******************************************************************************
  2. * Filename: target_core_rd.c
  3. *
  4. * This file contains the Storage Engine <-> Ramdisk 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. #include <linux/string.h>
  30. #include <linux/parser.h>
  31. #include <linux/timer.h>
  32. #include <linux/blkdev.h>
  33. #include <linux/slab.h>
  34. #include <linux/spinlock.h>
  35. #include <scsi/scsi.h>
  36. #include <scsi/scsi_host.h>
  37. #include <target/target_core_base.h>
  38. #include <target/target_core_backend.h>
  39. #include "target_core_rd.h"
  40. static struct se_subsystem_api rd_mcp_template;
  41. /* rd_attach_hba(): (Part of se_subsystem_api_t template)
  42. *
  43. *
  44. */
  45. static int rd_attach_hba(struct se_hba *hba, u32 host_id)
  46. {
  47. struct rd_host *rd_host;
  48. rd_host = kzalloc(sizeof(struct rd_host), GFP_KERNEL);
  49. if (!rd_host) {
  50. pr_err("Unable to allocate memory for struct rd_host\n");
  51. return -ENOMEM;
  52. }
  53. rd_host->rd_host_id = host_id;
  54. hba->hba_ptr = rd_host;
  55. pr_debug("CORE_HBA[%d] - TCM Ramdisk HBA Driver %s on"
  56. " Generic Target Core Stack %s\n", hba->hba_id,
  57. RD_HBA_VERSION, TARGET_CORE_MOD_VERSION);
  58. pr_debug("CORE_HBA[%d] - Attached Ramdisk HBA: %u to Generic"
  59. " MaxSectors: %u\n", hba->hba_id,
  60. rd_host->rd_host_id, RD_MAX_SECTORS);
  61. return 0;
  62. }
  63. static void rd_detach_hba(struct se_hba *hba)
  64. {
  65. struct rd_host *rd_host = hba->hba_ptr;
  66. pr_debug("CORE_HBA[%d] - Detached Ramdisk HBA: %u from"
  67. " Generic Target Core\n", hba->hba_id, rd_host->rd_host_id);
  68. kfree(rd_host);
  69. hba->hba_ptr = NULL;
  70. }
  71. static u32 rd_release_sgl_table(struct rd_dev *rd_dev, struct rd_dev_sg_table *sg_table,
  72. u32 sg_table_count)
  73. {
  74. struct page *pg;
  75. struct scatterlist *sg;
  76. u32 i, j, page_count = 0, sg_per_table;
  77. for (i = 0; i < sg_table_count; i++) {
  78. sg = sg_table[i].sg_table;
  79. sg_per_table = sg_table[i].rd_sg_count;
  80. for (j = 0; j < sg_per_table; j++) {
  81. pg = sg_page(&sg[j]);
  82. if (pg) {
  83. __free_page(pg);
  84. page_count++;
  85. }
  86. }
  87. kfree(sg);
  88. }
  89. kfree(sg_table);
  90. return page_count;
  91. }
  92. static void rd_release_device_space(struct rd_dev *rd_dev)
  93. {
  94. u32 page_count;
  95. if (!rd_dev->sg_table_array || !rd_dev->sg_table_count)
  96. return;
  97. page_count = rd_release_sgl_table(rd_dev, rd_dev->sg_table_array,
  98. rd_dev->sg_table_count);
  99. pr_debug("CORE_RD[%u] - Released device space for Ramdisk"
  100. " Device ID: %u, pages %u in %u tables total bytes %lu\n",
  101. rd_dev->rd_host->rd_host_id, rd_dev->rd_dev_id, page_count,
  102. rd_dev->sg_table_count, (unsigned long)page_count * PAGE_SIZE);
  103. rd_dev->sg_table_array = NULL;
  104. rd_dev->sg_table_count = 0;
  105. }
  106. /* rd_build_device_space():
  107. *
  108. *
  109. */
  110. static int rd_allocate_sgl_table(struct rd_dev *rd_dev, struct rd_dev_sg_table *sg_table,
  111. u32 total_sg_needed, unsigned char init_payload)
  112. {
  113. u32 i = 0, j, page_offset = 0, sg_per_table;
  114. u32 max_sg_per_table = (RD_MAX_ALLOCATION_SIZE /
  115. sizeof(struct scatterlist));
  116. struct page *pg;
  117. struct scatterlist *sg;
  118. unsigned char *p;
  119. while (total_sg_needed) {
  120. sg_per_table = (total_sg_needed > max_sg_per_table) ?
  121. max_sg_per_table : total_sg_needed;
  122. sg = kzalloc(sg_per_table * sizeof(struct scatterlist),
  123. GFP_KERNEL);
  124. if (!sg) {
  125. pr_err("Unable to allocate scatterlist array"
  126. " for struct rd_dev\n");
  127. return -ENOMEM;
  128. }
  129. sg_init_table(sg, sg_per_table);
  130. sg_table[i].sg_table = sg;
  131. sg_table[i].rd_sg_count = sg_per_table;
  132. sg_table[i].page_start_offset = page_offset;
  133. sg_table[i++].page_end_offset = (page_offset + sg_per_table)
  134. - 1;
  135. for (j = 0; j < sg_per_table; j++) {
  136. pg = alloc_pages(GFP_KERNEL | __GFP_ZERO, 0);
  137. if (!pg) {
  138. pr_err("Unable to allocate scatterlist"
  139. " pages for struct rd_dev_sg_table\n");
  140. return -ENOMEM;
  141. }
  142. sg_assign_page(&sg[j], pg);
  143. sg[j].length = PAGE_SIZE;
  144. p = kmap(pg);
  145. memset(p, init_payload, PAGE_SIZE);
  146. kunmap(pg);
  147. }
  148. page_offset += sg_per_table;
  149. total_sg_needed -= sg_per_table;
  150. }
  151. return 0;
  152. }
  153. static int rd_build_device_space(struct rd_dev *rd_dev)
  154. {
  155. struct rd_dev_sg_table *sg_table;
  156. u32 sg_tables, total_sg_needed;
  157. u32 max_sg_per_table = (RD_MAX_ALLOCATION_SIZE /
  158. sizeof(struct scatterlist));
  159. int rc;
  160. if (rd_dev->rd_page_count <= 0) {
  161. pr_err("Illegal page count: %u for Ramdisk device\n",
  162. rd_dev->rd_page_count);
  163. return -EINVAL;
  164. }
  165. /* Don't need backing pages for NULLIO */
  166. if (rd_dev->rd_flags & RDF_NULLIO)
  167. return 0;
  168. total_sg_needed = rd_dev->rd_page_count;
  169. sg_tables = (total_sg_needed / max_sg_per_table) + 1;
  170. sg_table = kzalloc(sg_tables * sizeof(struct rd_dev_sg_table), GFP_KERNEL);
  171. if (!sg_table) {
  172. pr_err("Unable to allocate memory for Ramdisk"
  173. " scatterlist tables\n");
  174. return -ENOMEM;
  175. }
  176. rd_dev->sg_table_array = sg_table;
  177. rd_dev->sg_table_count = sg_tables;
  178. rc = rd_allocate_sgl_table(rd_dev, sg_table, total_sg_needed, 0x00);
  179. if (rc)
  180. return rc;
  181. pr_debug("CORE_RD[%u] - Built Ramdisk Device ID: %u space of"
  182. " %u pages in %u tables\n", rd_dev->rd_host->rd_host_id,
  183. rd_dev->rd_dev_id, rd_dev->rd_page_count,
  184. rd_dev->sg_table_count);
  185. return 0;
  186. }
  187. static void *rd_allocate_virtdevice(
  188. struct se_hba *hba,
  189. const char *name,
  190. int rd_direct)
  191. {
  192. struct rd_dev *rd_dev;
  193. struct rd_host *rd_host = hba->hba_ptr;
  194. rd_dev = kzalloc(sizeof(struct rd_dev), GFP_KERNEL);
  195. if (!rd_dev) {
  196. pr_err("Unable to allocate memory for struct rd_dev\n");
  197. return NULL;
  198. }
  199. rd_dev->rd_host = rd_host;
  200. rd_dev->rd_direct = rd_direct;
  201. return rd_dev;
  202. }
  203. static void *rd_MEMCPY_allocate_virtdevice(struct se_hba *hba, const char *name)
  204. {
  205. return rd_allocate_virtdevice(hba, name, 0);
  206. }
  207. /* rd_create_virtdevice():
  208. *
  209. *
  210. */
  211. static struct se_device *rd_create_virtdevice(
  212. struct se_hba *hba,
  213. struct se_subsystem_dev *se_dev,
  214. void *p,
  215. int rd_direct)
  216. {
  217. struct se_device *dev;
  218. struct se_dev_limits dev_limits;
  219. struct rd_dev *rd_dev = p;
  220. struct rd_host *rd_host = hba->hba_ptr;
  221. int dev_flags = 0, ret;
  222. char prod[16], rev[4];
  223. memset(&dev_limits, 0, sizeof(struct se_dev_limits));
  224. ret = rd_build_device_space(rd_dev);
  225. if (ret < 0)
  226. goto fail;
  227. snprintf(prod, 16, "RAMDISK-%s", (rd_dev->rd_direct) ? "DR" : "MCP");
  228. snprintf(rev, 4, "%s", (rd_dev->rd_direct) ? RD_DR_VERSION :
  229. RD_MCP_VERSION);
  230. dev_limits.limits.logical_block_size = RD_BLOCKSIZE;
  231. dev_limits.limits.max_hw_sectors = RD_MAX_SECTORS;
  232. dev_limits.limits.max_sectors = RD_MAX_SECTORS;
  233. dev_limits.hw_queue_depth = RD_MAX_DEVICE_QUEUE_DEPTH;
  234. dev_limits.queue_depth = RD_DEVICE_QUEUE_DEPTH;
  235. dev = transport_add_device_to_core_hba(hba,
  236. &rd_mcp_template, se_dev, dev_flags, rd_dev,
  237. &dev_limits, prod, rev);
  238. if (!dev)
  239. goto fail;
  240. rd_dev->rd_dev_id = rd_host->rd_host_dev_id_count++;
  241. rd_dev->rd_queue_depth = dev->queue_depth;
  242. pr_debug("CORE_RD[%u] - Added TCM %s Ramdisk Device ID: %u of"
  243. " %u pages in %u tables, %lu total bytes\n",
  244. rd_host->rd_host_id, (!rd_dev->rd_direct) ? "MEMCPY" :
  245. "DIRECT", rd_dev->rd_dev_id, rd_dev->rd_page_count,
  246. rd_dev->sg_table_count,
  247. (unsigned long)(rd_dev->rd_page_count * PAGE_SIZE));
  248. return dev;
  249. fail:
  250. rd_release_device_space(rd_dev);
  251. return ERR_PTR(ret);
  252. }
  253. static struct se_device *rd_MEMCPY_create_virtdevice(
  254. struct se_hba *hba,
  255. struct se_subsystem_dev *se_dev,
  256. void *p)
  257. {
  258. return rd_create_virtdevice(hba, se_dev, p, 0);
  259. }
  260. /* rd_free_device(): (Part of se_subsystem_api_t template)
  261. *
  262. *
  263. */
  264. static void rd_free_device(void *p)
  265. {
  266. struct rd_dev *rd_dev = p;
  267. rd_release_device_space(rd_dev);
  268. kfree(rd_dev);
  269. }
  270. static inline struct rd_request *RD_REQ(struct se_task *task)
  271. {
  272. return container_of(task, struct rd_request, rd_task);
  273. }
  274. static struct se_task *
  275. rd_alloc_task(unsigned char *cdb)
  276. {
  277. struct rd_request *rd_req;
  278. rd_req = kzalloc(sizeof(struct rd_request), GFP_KERNEL);
  279. if (!rd_req) {
  280. pr_err("Unable to allocate struct rd_request\n");
  281. return NULL;
  282. }
  283. return &rd_req->rd_task;
  284. }
  285. /* rd_get_sg_table():
  286. *
  287. *
  288. */
  289. static struct rd_dev_sg_table *rd_get_sg_table(struct rd_dev *rd_dev, u32 page)
  290. {
  291. u32 i;
  292. struct rd_dev_sg_table *sg_table;
  293. for (i = 0; i < rd_dev->sg_table_count; i++) {
  294. sg_table = &rd_dev->sg_table_array[i];
  295. if ((sg_table->page_start_offset <= page) &&
  296. (sg_table->page_end_offset >= page))
  297. return sg_table;
  298. }
  299. pr_err("Unable to locate struct rd_dev_sg_table for page: %u\n",
  300. page);
  301. return NULL;
  302. }
  303. static int rd_MEMCPY(struct rd_request *req, u32 read_rd)
  304. {
  305. struct se_task *task = &req->rd_task;
  306. struct rd_dev *dev = req->rd_task.task_se_cmd->se_dev->dev_ptr;
  307. struct rd_dev_sg_table *table;
  308. struct scatterlist *rd_sg;
  309. struct sg_mapping_iter m;
  310. u32 rd_offset = req->rd_offset;
  311. u32 src_len;
  312. table = rd_get_sg_table(dev, req->rd_page);
  313. if (!table)
  314. return -EINVAL;
  315. rd_sg = &table->sg_table[req->rd_page - table->page_start_offset];
  316. pr_debug("RD[%u]: %s LBA: %llu, Size: %u Page: %u, Offset: %u\n",
  317. dev->rd_dev_id, read_rd ? "Read" : "Write",
  318. task->task_lba, req->rd_size, req->rd_page,
  319. rd_offset);
  320. src_len = PAGE_SIZE - rd_offset;
  321. sg_miter_start(&m, task->task_sg, task->task_sg_nents,
  322. read_rd ? SG_MITER_TO_SG : SG_MITER_FROM_SG);
  323. while (req->rd_size) {
  324. u32 len;
  325. void *rd_addr;
  326. sg_miter_next(&m);
  327. len = min((u32)m.length, src_len);
  328. m.consumed = len;
  329. rd_addr = sg_virt(rd_sg) + rd_offset;
  330. if (read_rd)
  331. memcpy(m.addr, rd_addr, len);
  332. else
  333. memcpy(rd_addr, m.addr, len);
  334. req->rd_size -= len;
  335. if (!req->rd_size)
  336. continue;
  337. src_len -= len;
  338. if (src_len) {
  339. rd_offset += len;
  340. continue;
  341. }
  342. /* rd page completed, next one please */
  343. req->rd_page++;
  344. rd_offset = 0;
  345. src_len = PAGE_SIZE;
  346. if (req->rd_page <= table->page_end_offset) {
  347. rd_sg++;
  348. continue;
  349. }
  350. table = rd_get_sg_table(dev, req->rd_page);
  351. if (!table) {
  352. sg_miter_stop(&m);
  353. return -EINVAL;
  354. }
  355. /* since we increment, the first sg entry is correct */
  356. rd_sg = table->sg_table;
  357. }
  358. sg_miter_stop(&m);
  359. return 0;
  360. }
  361. /* rd_MEMCPY_do_task(): (Part of se_subsystem_api_t template)
  362. *
  363. *
  364. */
  365. static int rd_MEMCPY_do_task(struct se_task *task)
  366. {
  367. struct se_device *dev = task->task_se_cmd->se_dev;
  368. struct rd_request *req = RD_REQ(task);
  369. u64 tmp;
  370. int ret;
  371. tmp = task->task_lba * dev->se_sub_dev->se_dev_attrib.block_size;
  372. req->rd_offset = do_div(tmp, PAGE_SIZE);
  373. req->rd_page = tmp;
  374. req->rd_size = task->task_size;
  375. ret = rd_MEMCPY(req, task->task_data_direction == DMA_FROM_DEVICE);
  376. if (ret != 0)
  377. return ret;
  378. task->task_scsi_status = GOOD;
  379. transport_complete_task(task, 1);
  380. return 0;
  381. }
  382. /* rd_free_task(): (Part of se_subsystem_api_t template)
  383. *
  384. *
  385. */
  386. static void rd_free_task(struct se_task *task)
  387. {
  388. kfree(RD_REQ(task));
  389. }
  390. enum {
  391. Opt_rd_pages, Opt_err
  392. };
  393. static match_table_t tokens = {
  394. {Opt_rd_pages, "rd_pages=%d"},
  395. {Opt_err, NULL}
  396. };
  397. static ssize_t rd_set_configfs_dev_params(
  398. struct se_hba *hba,
  399. struct se_subsystem_dev *se_dev,
  400. const char *page,
  401. ssize_t count)
  402. {
  403. struct rd_dev *rd_dev = se_dev->se_dev_su_ptr;
  404. char *orig, *ptr, *opts;
  405. substring_t args[MAX_OPT_ARGS];
  406. int ret = 0, arg, token;
  407. opts = kstrdup(page, GFP_KERNEL);
  408. if (!opts)
  409. return -ENOMEM;
  410. orig = opts;
  411. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  412. if (!*ptr)
  413. continue;
  414. token = match_token(ptr, tokens, args);
  415. switch (token) {
  416. case Opt_rd_pages:
  417. match_int(args, &arg);
  418. rd_dev->rd_page_count = arg;
  419. pr_debug("RAMDISK: Referencing Page"
  420. " Count: %u\n", rd_dev->rd_page_count);
  421. rd_dev->rd_flags |= RDF_HAS_PAGE_COUNT;
  422. break;
  423. default:
  424. break;
  425. }
  426. }
  427. kfree(orig);
  428. return (!ret) ? count : ret;
  429. }
  430. static ssize_t rd_check_configfs_dev_params(struct se_hba *hba, struct se_subsystem_dev *se_dev)
  431. {
  432. struct rd_dev *rd_dev = se_dev->se_dev_su_ptr;
  433. if (!(rd_dev->rd_flags & RDF_HAS_PAGE_COUNT)) {
  434. pr_debug("Missing rd_pages= parameter\n");
  435. return -EINVAL;
  436. }
  437. return 0;
  438. }
  439. static ssize_t rd_show_configfs_dev_params(
  440. struct se_hba *hba,
  441. struct se_subsystem_dev *se_dev,
  442. char *b)
  443. {
  444. struct rd_dev *rd_dev = se_dev->se_dev_su_ptr;
  445. ssize_t bl = sprintf(b, "TCM RamDisk ID: %u RamDisk Makeup: %s\n",
  446. rd_dev->rd_dev_id, (rd_dev->rd_direct) ?
  447. "rd_direct" : "rd_mcp");
  448. bl += sprintf(b + bl, " PAGES/PAGE_SIZE: %u*%lu"
  449. " SG_table_count: %u\n", rd_dev->rd_page_count,
  450. PAGE_SIZE, rd_dev->sg_table_count);
  451. return bl;
  452. }
  453. static u32 rd_get_device_rev(struct se_device *dev)
  454. {
  455. return SCSI_SPC_2; /* Returns SPC-3 in Initiator Data */
  456. }
  457. static u32 rd_get_device_type(struct se_device *dev)
  458. {
  459. return TYPE_DISK;
  460. }
  461. static sector_t rd_get_blocks(struct se_device *dev)
  462. {
  463. struct rd_dev *rd_dev = dev->dev_ptr;
  464. unsigned long long blocks_long = ((rd_dev->rd_page_count * PAGE_SIZE) /
  465. dev->se_sub_dev->se_dev_attrib.block_size) - 1;
  466. return blocks_long;
  467. }
  468. static struct se_subsystem_api rd_mcp_template = {
  469. .name = "rd_mcp",
  470. .transport_type = TRANSPORT_PLUGIN_VHBA_VDEV,
  471. .attach_hba = rd_attach_hba,
  472. .detach_hba = rd_detach_hba,
  473. .allocate_virtdevice = rd_MEMCPY_allocate_virtdevice,
  474. .create_virtdevice = rd_MEMCPY_create_virtdevice,
  475. .free_device = rd_free_device,
  476. .alloc_task = rd_alloc_task,
  477. .do_task = rd_MEMCPY_do_task,
  478. .free_task = rd_free_task,
  479. .check_configfs_dev_params = rd_check_configfs_dev_params,
  480. .set_configfs_dev_params = rd_set_configfs_dev_params,
  481. .show_configfs_dev_params = rd_show_configfs_dev_params,
  482. .get_device_rev = rd_get_device_rev,
  483. .get_device_type = rd_get_device_type,
  484. .get_blocks = rd_get_blocks,
  485. };
  486. int __init rd_module_init(void)
  487. {
  488. int ret;
  489. ret = transport_subsystem_register(&rd_mcp_template);
  490. if (ret < 0) {
  491. return ret;
  492. }
  493. return 0;
  494. }
  495. void rd_module_exit(void)
  496. {
  497. transport_subsystem_release(&rd_mcp_template);
  498. }