target_core_rd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*******************************************************************************
  2. * Filename: target_core_rd.c
  3. *
  4. * This file contains the Storage Engine <-> Ramdisk transport
  5. * specific functions.
  6. *
  7. * (c) Copyright 2003-2012 RisingTide Systems LLC.
  8. *
  9. * Nicholas A. Bellinger <nab@kernel.org>
  10. *
  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.
  15. *
  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.
  20. *
  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.
  24. *
  25. ******************************************************************************/
  26. #include <linux/string.h>
  27. #include <linux/parser.h>
  28. #include <linux/timer.h>
  29. #include <linux/blkdev.h>
  30. #include <linux/slab.h>
  31. #include <linux/spinlock.h>
  32. #include <scsi/scsi.h>
  33. #include <scsi/scsi_host.h>
  34. #include <target/target_core_base.h>
  35. #include <target/target_core_backend.h>
  36. #include "target_core_rd.h"
  37. static inline struct rd_dev *RD_DEV(struct se_device *dev)
  38. {
  39. return container_of(dev, struct rd_dev, dev);
  40. }
  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. return 0;
  59. }
  60. static void rd_detach_hba(struct se_hba *hba)
  61. {
  62. struct rd_host *rd_host = hba->hba_ptr;
  63. pr_debug("CORE_HBA[%d] - Detached Ramdisk HBA: %u from"
  64. " Generic Target Core\n", hba->hba_id, rd_host->rd_host_id);
  65. kfree(rd_host);
  66. hba->hba_ptr = NULL;
  67. }
  68. /* rd_release_device_space():
  69. *
  70. *
  71. */
  72. static void rd_release_device_space(struct rd_dev *rd_dev)
  73. {
  74. u32 i, j, page_count = 0, sg_per_table;
  75. struct rd_dev_sg_table *sg_table;
  76. struct page *pg;
  77. struct scatterlist *sg;
  78. if (!rd_dev->sg_table_array || !rd_dev->sg_table_count)
  79. return;
  80. sg_table = rd_dev->sg_table_array;
  81. for (i = 0; i < rd_dev->sg_table_count; i++) {
  82. sg = sg_table[i].sg_table;
  83. sg_per_table = sg_table[i].rd_sg_count;
  84. for (j = 0; j < sg_per_table; j++) {
  85. pg = sg_page(&sg[j]);
  86. if (pg) {
  87. __free_page(pg);
  88. page_count++;
  89. }
  90. }
  91. kfree(sg);
  92. }
  93. pr_debug("CORE_RD[%u] - Released device space for Ramdisk"
  94. " Device ID: %u, pages %u in %u tables total bytes %lu\n",
  95. rd_dev->rd_host->rd_host_id, rd_dev->rd_dev_id, page_count,
  96. rd_dev->sg_table_count, (unsigned long)page_count * PAGE_SIZE);
  97. kfree(sg_table);
  98. rd_dev->sg_table_array = NULL;
  99. rd_dev->sg_table_count = 0;
  100. }
  101. /* rd_build_device_space():
  102. *
  103. *
  104. */
  105. static int rd_build_device_space(struct rd_dev *rd_dev)
  106. {
  107. u32 i = 0, j, page_offset = 0, sg_per_table, sg_tables, total_sg_needed;
  108. u32 max_sg_per_table = (RD_MAX_ALLOCATION_SIZE /
  109. sizeof(struct scatterlist));
  110. struct rd_dev_sg_table *sg_table;
  111. struct page *pg;
  112. struct scatterlist *sg;
  113. if (rd_dev->rd_page_count <= 0) {
  114. pr_err("Illegal page count: %u for Ramdisk device\n",
  115. rd_dev->rd_page_count);
  116. return -EINVAL;
  117. }
  118. /* Don't need backing pages for NULLIO */
  119. if (rd_dev->rd_flags & RDF_NULLIO)
  120. return 0;
  121. total_sg_needed = rd_dev->rd_page_count;
  122. sg_tables = (total_sg_needed / max_sg_per_table) + 1;
  123. sg_table = kzalloc(sg_tables * sizeof(struct rd_dev_sg_table), GFP_KERNEL);
  124. if (!sg_table) {
  125. pr_err("Unable to allocate memory for Ramdisk"
  126. " scatterlist tables\n");
  127. return -ENOMEM;
  128. }
  129. rd_dev->sg_table_array = sg_table;
  130. rd_dev->sg_table_count = sg_tables;
  131. while (total_sg_needed) {
  132. sg_per_table = (total_sg_needed > max_sg_per_table) ?
  133. max_sg_per_table : total_sg_needed;
  134. sg = kzalloc(sg_per_table * sizeof(struct scatterlist),
  135. GFP_KERNEL);
  136. if (!sg) {
  137. pr_err("Unable to allocate scatterlist array"
  138. " for struct rd_dev\n");
  139. return -ENOMEM;
  140. }
  141. sg_init_table(sg, sg_per_table);
  142. sg_table[i].sg_table = sg;
  143. sg_table[i].rd_sg_count = sg_per_table;
  144. sg_table[i].page_start_offset = page_offset;
  145. sg_table[i++].page_end_offset = (page_offset + sg_per_table)
  146. - 1;
  147. for (j = 0; j < sg_per_table; j++) {
  148. pg = alloc_pages(GFP_KERNEL, 0);
  149. if (!pg) {
  150. pr_err("Unable to allocate scatterlist"
  151. " pages for struct rd_dev_sg_table\n");
  152. return -ENOMEM;
  153. }
  154. sg_assign_page(&sg[j], pg);
  155. sg[j].length = PAGE_SIZE;
  156. }
  157. page_offset += sg_per_table;
  158. total_sg_needed -= sg_per_table;
  159. }
  160. pr_debug("CORE_RD[%u] - Built Ramdisk Device ID: %u space of"
  161. " %u pages in %u tables\n", rd_dev->rd_host->rd_host_id,
  162. rd_dev->rd_dev_id, rd_dev->rd_page_count,
  163. rd_dev->sg_table_count);
  164. return 0;
  165. }
  166. static struct se_device *rd_alloc_device(struct se_hba *hba, const char *name)
  167. {
  168. struct rd_dev *rd_dev;
  169. struct rd_host *rd_host = hba->hba_ptr;
  170. rd_dev = kzalloc(sizeof(struct rd_dev), GFP_KERNEL);
  171. if (!rd_dev) {
  172. pr_err("Unable to allocate memory for struct rd_dev\n");
  173. return NULL;
  174. }
  175. rd_dev->rd_host = rd_host;
  176. return &rd_dev->dev;
  177. }
  178. static int rd_configure_device(struct se_device *dev)
  179. {
  180. struct rd_dev *rd_dev = RD_DEV(dev);
  181. struct rd_host *rd_host = dev->se_hba->hba_ptr;
  182. int ret;
  183. if (!(rd_dev->rd_flags & RDF_HAS_PAGE_COUNT)) {
  184. pr_debug("Missing rd_pages= parameter\n");
  185. return -EINVAL;
  186. }
  187. ret = rd_build_device_space(rd_dev);
  188. if (ret < 0)
  189. goto fail;
  190. dev->dev_attrib.hw_block_size = RD_BLOCKSIZE;
  191. dev->dev_attrib.hw_max_sectors = UINT_MAX;
  192. dev->dev_attrib.hw_queue_depth = RD_MAX_DEVICE_QUEUE_DEPTH;
  193. rd_dev->rd_dev_id = rd_host->rd_host_dev_id_count++;
  194. pr_debug("CORE_RD[%u] - Added TCM MEMCPY Ramdisk Device ID: %u of"
  195. " %u pages in %u tables, %lu total bytes\n",
  196. rd_host->rd_host_id, rd_dev->rd_dev_id, rd_dev->rd_page_count,
  197. rd_dev->sg_table_count,
  198. (unsigned long)(rd_dev->rd_page_count * PAGE_SIZE));
  199. return 0;
  200. fail:
  201. rd_release_device_space(rd_dev);
  202. return ret;
  203. }
  204. static void rd_free_device(struct se_device *dev)
  205. {
  206. struct rd_dev *rd_dev = RD_DEV(dev);
  207. rd_release_device_space(rd_dev);
  208. kfree(rd_dev);
  209. }
  210. static struct rd_dev_sg_table *rd_get_sg_table(struct rd_dev *rd_dev, u32 page)
  211. {
  212. struct rd_dev_sg_table *sg_table;
  213. u32 i, sg_per_table = (RD_MAX_ALLOCATION_SIZE /
  214. sizeof(struct scatterlist));
  215. i = page / sg_per_table;
  216. if (i < rd_dev->sg_table_count) {
  217. sg_table = &rd_dev->sg_table_array[i];
  218. if ((sg_table->page_start_offset <= page) &&
  219. (sg_table->page_end_offset >= page))
  220. return sg_table;
  221. }
  222. pr_err("Unable to locate struct rd_dev_sg_table for page: %u\n",
  223. page);
  224. return NULL;
  225. }
  226. static sense_reason_t
  227. rd_execute_rw(struct se_cmd *cmd)
  228. {
  229. struct scatterlist *sgl = cmd->t_data_sg;
  230. u32 sgl_nents = cmd->t_data_nents;
  231. enum dma_data_direction data_direction = cmd->data_direction;
  232. struct se_device *se_dev = cmd->se_dev;
  233. struct rd_dev *dev = RD_DEV(se_dev);
  234. struct rd_dev_sg_table *table;
  235. struct scatterlist *rd_sg;
  236. struct sg_mapping_iter m;
  237. u32 rd_offset;
  238. u32 rd_size;
  239. u32 rd_page;
  240. u32 src_len;
  241. u64 tmp;
  242. if (dev->rd_flags & RDF_NULLIO) {
  243. target_complete_cmd(cmd, SAM_STAT_GOOD);
  244. return 0;
  245. }
  246. tmp = cmd->t_task_lba * se_dev->dev_attrib.block_size;
  247. rd_offset = do_div(tmp, PAGE_SIZE);
  248. rd_page = tmp;
  249. rd_size = cmd->data_length;
  250. table = rd_get_sg_table(dev, rd_page);
  251. if (!table)
  252. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  253. rd_sg = &table->sg_table[rd_page - table->page_start_offset];
  254. pr_debug("RD[%u]: %s LBA: %llu, Size: %u Page: %u, Offset: %u\n",
  255. dev->rd_dev_id,
  256. data_direction == DMA_FROM_DEVICE ? "Read" : "Write",
  257. cmd->t_task_lba, rd_size, rd_page, rd_offset);
  258. src_len = PAGE_SIZE - rd_offset;
  259. sg_miter_start(&m, sgl, sgl_nents,
  260. data_direction == DMA_FROM_DEVICE ?
  261. SG_MITER_TO_SG : SG_MITER_FROM_SG);
  262. while (rd_size) {
  263. u32 len;
  264. void *rd_addr;
  265. sg_miter_next(&m);
  266. if (!(u32)m.length) {
  267. pr_debug("RD[%u]: invalid sgl %p len %zu\n",
  268. dev->rd_dev_id, m.addr, m.length);
  269. sg_miter_stop(&m);
  270. return TCM_INCORRECT_AMOUNT_OF_DATA;
  271. }
  272. len = min((u32)m.length, src_len);
  273. if (len > rd_size) {
  274. pr_debug("RD[%u]: size underrun page %d offset %d "
  275. "size %d\n", dev->rd_dev_id,
  276. rd_page, rd_offset, rd_size);
  277. len = rd_size;
  278. }
  279. m.consumed = len;
  280. rd_addr = sg_virt(rd_sg) + rd_offset;
  281. if (data_direction == DMA_FROM_DEVICE)
  282. memcpy(m.addr, rd_addr, len);
  283. else
  284. memcpy(rd_addr, m.addr, len);
  285. rd_size -= len;
  286. if (!rd_size)
  287. continue;
  288. src_len -= len;
  289. if (src_len) {
  290. rd_offset += len;
  291. continue;
  292. }
  293. /* rd page completed, next one please */
  294. rd_page++;
  295. rd_offset = 0;
  296. src_len = PAGE_SIZE;
  297. if (rd_page <= table->page_end_offset) {
  298. rd_sg++;
  299. continue;
  300. }
  301. table = rd_get_sg_table(dev, rd_page);
  302. if (!table) {
  303. sg_miter_stop(&m);
  304. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  305. }
  306. /* since we increment, the first sg entry is correct */
  307. rd_sg = table->sg_table;
  308. }
  309. sg_miter_stop(&m);
  310. target_complete_cmd(cmd, SAM_STAT_GOOD);
  311. return 0;
  312. }
  313. enum {
  314. Opt_rd_pages, Opt_rd_nullio, Opt_err
  315. };
  316. static match_table_t tokens = {
  317. {Opt_rd_pages, "rd_pages=%d"},
  318. {Opt_rd_nullio, "rd_nullio=%d"},
  319. {Opt_err, NULL}
  320. };
  321. static ssize_t rd_set_configfs_dev_params(struct se_device *dev,
  322. const char *page, ssize_t count)
  323. {
  324. struct rd_dev *rd_dev = RD_DEV(dev);
  325. char *orig, *ptr, *opts;
  326. substring_t args[MAX_OPT_ARGS];
  327. int ret = 0, arg, token;
  328. opts = kstrdup(page, GFP_KERNEL);
  329. if (!opts)
  330. return -ENOMEM;
  331. orig = opts;
  332. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  333. if (!*ptr)
  334. continue;
  335. token = match_token(ptr, tokens, args);
  336. switch (token) {
  337. case Opt_rd_pages:
  338. match_int(args, &arg);
  339. rd_dev->rd_page_count = arg;
  340. pr_debug("RAMDISK: Referencing Page"
  341. " Count: %u\n", rd_dev->rd_page_count);
  342. rd_dev->rd_flags |= RDF_HAS_PAGE_COUNT;
  343. break;
  344. case Opt_rd_nullio:
  345. match_int(args, &arg);
  346. if (arg != 1)
  347. break;
  348. pr_debug("RAMDISK: Setting NULLIO flag: %d\n", arg);
  349. rd_dev->rd_flags |= RDF_NULLIO;
  350. break;
  351. default:
  352. break;
  353. }
  354. }
  355. kfree(orig);
  356. return (!ret) ? count : ret;
  357. }
  358. static ssize_t rd_show_configfs_dev_params(struct se_device *dev, char *b)
  359. {
  360. struct rd_dev *rd_dev = RD_DEV(dev);
  361. ssize_t bl = sprintf(b, "TCM RamDisk ID: %u RamDisk Makeup: rd_mcp\n",
  362. rd_dev->rd_dev_id);
  363. bl += sprintf(b + bl, " PAGES/PAGE_SIZE: %u*%lu"
  364. " SG_table_count: %u nullio: %d\n", rd_dev->rd_page_count,
  365. PAGE_SIZE, rd_dev->sg_table_count,
  366. !!(rd_dev->rd_flags & RDF_NULLIO));
  367. return bl;
  368. }
  369. static sector_t rd_get_blocks(struct se_device *dev)
  370. {
  371. struct rd_dev *rd_dev = RD_DEV(dev);
  372. unsigned long long blocks_long = ((rd_dev->rd_page_count * PAGE_SIZE) /
  373. dev->dev_attrib.block_size) - 1;
  374. return blocks_long;
  375. }
  376. static struct sbc_ops rd_sbc_ops = {
  377. .execute_rw = rd_execute_rw,
  378. };
  379. static sense_reason_t
  380. rd_parse_cdb(struct se_cmd *cmd)
  381. {
  382. return sbc_parse_cdb(cmd, &rd_sbc_ops);
  383. }
  384. static struct se_subsystem_api rd_mcp_template = {
  385. .name = "rd_mcp",
  386. .inquiry_prod = "RAMDISK-MCP",
  387. .inquiry_rev = RD_MCP_VERSION,
  388. .transport_type = TRANSPORT_PLUGIN_VHBA_VDEV,
  389. .attach_hba = rd_attach_hba,
  390. .detach_hba = rd_detach_hba,
  391. .alloc_device = rd_alloc_device,
  392. .configure_device = rd_configure_device,
  393. .free_device = rd_free_device,
  394. .parse_cdb = rd_parse_cdb,
  395. .set_configfs_dev_params = rd_set_configfs_dev_params,
  396. .show_configfs_dev_params = rd_show_configfs_dev_params,
  397. .get_device_type = sbc_get_device_type,
  398. .get_blocks = rd_get_blocks,
  399. };
  400. int __init rd_module_init(void)
  401. {
  402. int ret;
  403. ret = transport_subsystem_register(&rd_mcp_template);
  404. if (ret < 0) {
  405. return ret;
  406. }
  407. return 0;
  408. }
  409. void rd_module_exit(void)
  410. {
  411. transport_subsystem_release(&rd_mcp_template);
  412. }