target_core_rd.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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/version.h>
  30. #include <linux/string.h>
  31. #include <linux/parser.h>
  32. #include <linux/timer.h>
  33. #include <linux/blkdev.h>
  34. #include <linux/slab.h>
  35. #include <linux/spinlock.h>
  36. #include <scsi/scsi.h>
  37. #include <scsi/scsi_host.h>
  38. #include <target/target_core_base.h>
  39. #include <target/target_core_device.h>
  40. #include <target/target_core_transport.h>
  41. #include <target/target_core_fabric_ops.h>
  42. #include "target_core_rd.h"
  43. static struct se_subsystem_api rd_dr_template;
  44. static struct se_subsystem_api rd_mcp_template;
  45. /* #define DEBUG_RAMDISK_MCP */
  46. /* #define DEBUG_RAMDISK_DR */
  47. /* rd_attach_hba(): (Part of se_subsystem_api_t template)
  48. *
  49. *
  50. */
  51. static int rd_attach_hba(struct se_hba *hba, u32 host_id)
  52. {
  53. struct rd_host *rd_host;
  54. rd_host = kzalloc(sizeof(struct rd_host), GFP_KERNEL);
  55. if (!(rd_host)) {
  56. printk(KERN_ERR "Unable to allocate memory for struct rd_host\n");
  57. return -ENOMEM;
  58. }
  59. rd_host->rd_host_id = host_id;
  60. atomic_set(&hba->left_queue_depth, RD_HBA_QUEUE_DEPTH);
  61. atomic_set(&hba->max_queue_depth, RD_HBA_QUEUE_DEPTH);
  62. hba->hba_ptr = (void *) rd_host;
  63. printk(KERN_INFO "CORE_HBA[%d] - TCM Ramdisk HBA Driver %s on"
  64. " Generic Target Core Stack %s\n", hba->hba_id,
  65. RD_HBA_VERSION, TARGET_CORE_MOD_VERSION);
  66. printk(KERN_INFO "CORE_HBA[%d] - Attached Ramdisk HBA: %u to Generic"
  67. " Target Core TCQ Depth: %d MaxSectors: %u\n", hba->hba_id,
  68. rd_host->rd_host_id, atomic_read(&hba->max_queue_depth),
  69. RD_MAX_SECTORS);
  70. return 0;
  71. }
  72. static void rd_detach_hba(struct se_hba *hba)
  73. {
  74. struct rd_host *rd_host = hba->hba_ptr;
  75. printk(KERN_INFO "CORE_HBA[%d] - Detached Ramdisk HBA: %u from"
  76. " Generic Target Core\n", hba->hba_id, rd_host->rd_host_id);
  77. kfree(rd_host);
  78. hba->hba_ptr = NULL;
  79. }
  80. /* rd_release_device_space():
  81. *
  82. *
  83. */
  84. static void rd_release_device_space(struct rd_dev *rd_dev)
  85. {
  86. u32 i, j, page_count = 0, sg_per_table;
  87. struct rd_dev_sg_table *sg_table;
  88. struct page *pg;
  89. struct scatterlist *sg;
  90. if (!rd_dev->sg_table_array || !rd_dev->sg_table_count)
  91. return;
  92. sg_table = rd_dev->sg_table_array;
  93. for (i = 0; i < rd_dev->sg_table_count; i++) {
  94. sg = sg_table[i].sg_table;
  95. sg_per_table = sg_table[i].rd_sg_count;
  96. for (j = 0; j < sg_per_table; j++) {
  97. pg = sg_page(&sg[j]);
  98. if ((pg)) {
  99. __free_page(pg);
  100. page_count++;
  101. }
  102. }
  103. kfree(sg);
  104. }
  105. printk(KERN_INFO "CORE_RD[%u] - Released device space for Ramdisk"
  106. " Device ID: %u, pages %u in %u tables total bytes %lu\n",
  107. rd_dev->rd_host->rd_host_id, rd_dev->rd_dev_id, page_count,
  108. rd_dev->sg_table_count, (unsigned long)page_count * PAGE_SIZE);
  109. kfree(sg_table);
  110. rd_dev->sg_table_array = NULL;
  111. rd_dev->sg_table_count = 0;
  112. }
  113. /* rd_build_device_space():
  114. *
  115. *
  116. */
  117. static int rd_build_device_space(struct rd_dev *rd_dev)
  118. {
  119. u32 i = 0, j, page_offset = 0, sg_per_table, sg_tables, total_sg_needed;
  120. u32 max_sg_per_table = (RD_MAX_ALLOCATION_SIZE /
  121. sizeof(struct scatterlist));
  122. struct rd_dev_sg_table *sg_table;
  123. struct page *pg;
  124. struct scatterlist *sg;
  125. if (rd_dev->rd_page_count <= 0) {
  126. printk(KERN_ERR "Illegal page count: %u for Ramdisk device\n",
  127. rd_dev->rd_page_count);
  128. return -EINVAL;
  129. }
  130. total_sg_needed = rd_dev->rd_page_count;
  131. sg_tables = (total_sg_needed / max_sg_per_table) + 1;
  132. sg_table = kzalloc(sg_tables * sizeof(struct rd_dev_sg_table), GFP_KERNEL);
  133. if (!(sg_table)) {
  134. printk(KERN_ERR "Unable to allocate memory for Ramdisk"
  135. " scatterlist tables\n");
  136. return -ENOMEM;
  137. }
  138. rd_dev->sg_table_array = sg_table;
  139. rd_dev->sg_table_count = sg_tables;
  140. while (total_sg_needed) {
  141. sg_per_table = (total_sg_needed > max_sg_per_table) ?
  142. max_sg_per_table : total_sg_needed;
  143. sg = kzalloc(sg_per_table * sizeof(struct scatterlist),
  144. GFP_KERNEL);
  145. if (!(sg)) {
  146. printk(KERN_ERR "Unable to allocate scatterlist array"
  147. " for struct rd_dev\n");
  148. return -ENOMEM;
  149. }
  150. sg_init_table((struct scatterlist *)&sg[0], sg_per_table);
  151. sg_table[i].sg_table = sg;
  152. sg_table[i].rd_sg_count = sg_per_table;
  153. sg_table[i].page_start_offset = page_offset;
  154. sg_table[i++].page_end_offset = (page_offset + sg_per_table)
  155. - 1;
  156. for (j = 0; j < sg_per_table; j++) {
  157. pg = alloc_pages(GFP_KERNEL, 0);
  158. if (!(pg)) {
  159. printk(KERN_ERR "Unable to allocate scatterlist"
  160. " pages for struct rd_dev_sg_table\n");
  161. return -ENOMEM;
  162. }
  163. sg_assign_page(&sg[j], pg);
  164. sg[j].length = PAGE_SIZE;
  165. }
  166. page_offset += sg_per_table;
  167. total_sg_needed -= sg_per_table;
  168. }
  169. printk(KERN_INFO "CORE_RD[%u] - Built Ramdisk Device ID: %u space of"
  170. " %u pages in %u tables\n", rd_dev->rd_host->rd_host_id,
  171. rd_dev->rd_dev_id, rd_dev->rd_page_count,
  172. rd_dev->sg_table_count);
  173. return 0;
  174. }
  175. static void *rd_allocate_virtdevice(
  176. struct se_hba *hba,
  177. const char *name,
  178. int rd_direct)
  179. {
  180. struct rd_dev *rd_dev;
  181. struct rd_host *rd_host = hba->hba_ptr;
  182. rd_dev = kzalloc(sizeof(struct rd_dev), GFP_KERNEL);
  183. if (!(rd_dev)) {
  184. printk(KERN_ERR "Unable to allocate memory for struct rd_dev\n");
  185. return NULL;
  186. }
  187. rd_dev->rd_host = rd_host;
  188. rd_dev->rd_direct = rd_direct;
  189. return rd_dev;
  190. }
  191. static void *rd_DIRECT_allocate_virtdevice(struct se_hba *hba, const char *name)
  192. {
  193. return rd_allocate_virtdevice(hba, name, 1);
  194. }
  195. static void *rd_MEMCPY_allocate_virtdevice(struct se_hba *hba, const char *name)
  196. {
  197. return rd_allocate_virtdevice(hba, name, 0);
  198. }
  199. /* rd_create_virtdevice():
  200. *
  201. *
  202. */
  203. static struct se_device *rd_create_virtdevice(
  204. struct se_hba *hba,
  205. struct se_subsystem_dev *se_dev,
  206. void *p,
  207. int rd_direct)
  208. {
  209. struct se_device *dev;
  210. struct se_dev_limits dev_limits;
  211. struct rd_dev *rd_dev = p;
  212. struct rd_host *rd_host = hba->hba_ptr;
  213. int dev_flags = 0, ret;
  214. char prod[16], rev[4];
  215. memset(&dev_limits, 0, sizeof(struct se_dev_limits));
  216. ret = rd_build_device_space(rd_dev);
  217. if (ret < 0)
  218. goto fail;
  219. snprintf(prod, 16, "RAMDISK-%s", (rd_dev->rd_direct) ? "DR" : "MCP");
  220. snprintf(rev, 4, "%s", (rd_dev->rd_direct) ? RD_DR_VERSION :
  221. RD_MCP_VERSION);
  222. dev_limits.limits.logical_block_size = RD_BLOCKSIZE;
  223. dev_limits.limits.max_hw_sectors = RD_MAX_SECTORS;
  224. dev_limits.limits.max_sectors = RD_MAX_SECTORS;
  225. dev_limits.hw_queue_depth = RD_MAX_DEVICE_QUEUE_DEPTH;
  226. dev_limits.queue_depth = RD_DEVICE_QUEUE_DEPTH;
  227. dev = transport_add_device_to_core_hba(hba,
  228. (rd_dev->rd_direct) ? &rd_dr_template :
  229. &rd_mcp_template, se_dev, dev_flags, (void *)rd_dev,
  230. &dev_limits, prod, rev);
  231. if (!(dev))
  232. goto fail;
  233. rd_dev->rd_dev_id = rd_host->rd_host_dev_id_count++;
  234. rd_dev->rd_queue_depth = dev->queue_depth;
  235. printk(KERN_INFO "CORE_RD[%u] - Added TCM %s Ramdisk Device ID: %u of"
  236. " %u pages in %u tables, %lu total bytes\n",
  237. rd_host->rd_host_id, (!rd_dev->rd_direct) ? "MEMCPY" :
  238. "DIRECT", rd_dev->rd_dev_id, rd_dev->rd_page_count,
  239. rd_dev->sg_table_count,
  240. (unsigned long)(rd_dev->rd_page_count * PAGE_SIZE));
  241. return dev;
  242. fail:
  243. rd_release_device_space(rd_dev);
  244. return ERR_PTR(ret);
  245. }
  246. static struct se_device *rd_DIRECT_create_virtdevice(
  247. struct se_hba *hba,
  248. struct se_subsystem_dev *se_dev,
  249. void *p)
  250. {
  251. return rd_create_virtdevice(hba, se_dev, p, 1);
  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(struct se_cmd *cmd)
  276. {
  277. struct rd_request *rd_req;
  278. rd_req = kzalloc(sizeof(struct rd_request), GFP_KERNEL);
  279. if (!rd_req) {
  280. printk(KERN_ERR "Unable to allocate struct rd_request\n");
  281. return NULL;
  282. }
  283. rd_req->rd_dev = SE_DEV(cmd)->dev_ptr;
  284. return &rd_req->rd_task;
  285. }
  286. /* rd_get_sg_table():
  287. *
  288. *
  289. */
  290. static struct rd_dev_sg_table *rd_get_sg_table(struct rd_dev *rd_dev, u32 page)
  291. {
  292. u32 i;
  293. struct rd_dev_sg_table *sg_table;
  294. for (i = 0; i < rd_dev->sg_table_count; i++) {
  295. sg_table = &rd_dev->sg_table_array[i];
  296. if ((sg_table->page_start_offset <= page) &&
  297. (sg_table->page_end_offset >= page))
  298. return sg_table;
  299. }
  300. printk(KERN_ERR "Unable to locate struct rd_dev_sg_table for page: %u\n",
  301. page);
  302. return NULL;
  303. }
  304. /* rd_MEMCPY_read():
  305. *
  306. *
  307. */
  308. static int rd_MEMCPY_read(struct rd_request *req)
  309. {
  310. struct se_task *task = &req->rd_task;
  311. struct rd_dev *dev = req->rd_dev;
  312. struct rd_dev_sg_table *table;
  313. struct scatterlist *sg_d, *sg_s;
  314. void *dst, *src;
  315. u32 i = 0, j = 0, dst_offset = 0, src_offset = 0;
  316. u32 length, page_end = 0, table_sg_end;
  317. u32 rd_offset = req->rd_offset;
  318. table = rd_get_sg_table(dev, req->rd_page);
  319. if (!(table))
  320. return -1;
  321. table_sg_end = (table->page_end_offset - req->rd_page);
  322. sg_d = task->task_sg;
  323. sg_s = &table->sg_table[req->rd_page - table->page_start_offset];
  324. #ifdef DEBUG_RAMDISK_MCP
  325. printk(KERN_INFO "RD[%u]: Read LBA: %llu, Size: %u Page: %u, Offset:"
  326. " %u\n", dev->rd_dev_id, task->task_lba, req->rd_size,
  327. req->rd_page, req->rd_offset);
  328. #endif
  329. src_offset = rd_offset;
  330. while (req->rd_size) {
  331. if ((sg_d[i].length - dst_offset) <
  332. (sg_s[j].length - src_offset)) {
  333. length = (sg_d[i].length - dst_offset);
  334. #ifdef DEBUG_RAMDISK_MCP
  335. printk(KERN_INFO "Step 1 - sg_d[%d]: %p length: %d"
  336. " offset: %u sg_s[%d].length: %u\n", i,
  337. &sg_d[i], sg_d[i].length, sg_d[i].offset, j,
  338. sg_s[j].length);
  339. printk(KERN_INFO "Step 1 - length: %u dst_offset: %u"
  340. " src_offset: %u\n", length, dst_offset,
  341. src_offset);
  342. #endif
  343. if (length > req->rd_size)
  344. length = req->rd_size;
  345. dst = sg_virt(&sg_d[i++]) + dst_offset;
  346. if (!dst)
  347. BUG();
  348. src = sg_virt(&sg_s[j]) + src_offset;
  349. if (!src)
  350. BUG();
  351. dst_offset = 0;
  352. src_offset = length;
  353. page_end = 0;
  354. } else {
  355. length = (sg_s[j].length - src_offset);
  356. #ifdef DEBUG_RAMDISK_MCP
  357. printk(KERN_INFO "Step 2 - sg_d[%d]: %p length: %d"
  358. " offset: %u sg_s[%d].length: %u\n", i,
  359. &sg_d[i], sg_d[i].length, sg_d[i].offset,
  360. j, sg_s[j].length);
  361. printk(KERN_INFO "Step 2 - length: %u dst_offset: %u"
  362. " src_offset: %u\n", length, dst_offset,
  363. src_offset);
  364. #endif
  365. if (length > req->rd_size)
  366. length = req->rd_size;
  367. dst = sg_virt(&sg_d[i]) + dst_offset;
  368. if (!dst)
  369. BUG();
  370. if (sg_d[i].length == length) {
  371. i++;
  372. dst_offset = 0;
  373. } else
  374. dst_offset = length;
  375. src = sg_virt(&sg_s[j++]) + src_offset;
  376. if (!src)
  377. BUG();
  378. src_offset = 0;
  379. page_end = 1;
  380. }
  381. memcpy(dst, src, length);
  382. #ifdef DEBUG_RAMDISK_MCP
  383. printk(KERN_INFO "page: %u, remaining size: %u, length: %u,"
  384. " i: %u, j: %u\n", req->rd_page,
  385. (req->rd_size - length), length, i, j);
  386. #endif
  387. req->rd_size -= length;
  388. if (!(req->rd_size))
  389. return 0;
  390. if (!page_end)
  391. continue;
  392. if (++req->rd_page <= table->page_end_offset) {
  393. #ifdef DEBUG_RAMDISK_MCP
  394. printk(KERN_INFO "page: %u in same page table\n",
  395. req->rd_page);
  396. #endif
  397. continue;
  398. }
  399. #ifdef DEBUG_RAMDISK_MCP
  400. printk(KERN_INFO "getting new page table for page: %u\n",
  401. req->rd_page);
  402. #endif
  403. table = rd_get_sg_table(dev, req->rd_page);
  404. if (!(table))
  405. return -1;
  406. sg_s = &table->sg_table[j = 0];
  407. }
  408. return 0;
  409. }
  410. /* rd_MEMCPY_write():
  411. *
  412. *
  413. */
  414. static int rd_MEMCPY_write(struct rd_request *req)
  415. {
  416. struct se_task *task = &req->rd_task;
  417. struct rd_dev *dev = req->rd_dev;
  418. struct rd_dev_sg_table *table;
  419. struct scatterlist *sg_d, *sg_s;
  420. void *dst, *src;
  421. u32 i = 0, j = 0, dst_offset = 0, src_offset = 0;
  422. u32 length, page_end = 0, table_sg_end;
  423. u32 rd_offset = req->rd_offset;
  424. table = rd_get_sg_table(dev, req->rd_page);
  425. if (!(table))
  426. return -1;
  427. table_sg_end = (table->page_end_offset - req->rd_page);
  428. sg_d = &table->sg_table[req->rd_page - table->page_start_offset];
  429. sg_s = task->task_sg;
  430. #ifdef DEBUG_RAMDISK_MCP
  431. printk(KERN_INFO "RD[%d] Write LBA: %llu, Size: %u, Page: %u,"
  432. " Offset: %u\n", dev->rd_dev_id, task->task_lba, req->rd_size,
  433. req->rd_page, req->rd_offset);
  434. #endif
  435. dst_offset = rd_offset;
  436. while (req->rd_size) {
  437. if ((sg_s[i].length - src_offset) <
  438. (sg_d[j].length - dst_offset)) {
  439. length = (sg_s[i].length - src_offset);
  440. #ifdef DEBUG_RAMDISK_MCP
  441. printk(KERN_INFO "Step 1 - sg_s[%d]: %p length: %d"
  442. " offset: %d sg_d[%d].length: %u\n", i,
  443. &sg_s[i], sg_s[i].length, sg_s[i].offset,
  444. j, sg_d[j].length);
  445. printk(KERN_INFO "Step 1 - length: %u src_offset: %u"
  446. " dst_offset: %u\n", length, src_offset,
  447. dst_offset);
  448. #endif
  449. if (length > req->rd_size)
  450. length = req->rd_size;
  451. src = sg_virt(&sg_s[i++]) + src_offset;
  452. if (!src)
  453. BUG();
  454. dst = sg_virt(&sg_d[j]) + dst_offset;
  455. if (!dst)
  456. BUG();
  457. src_offset = 0;
  458. dst_offset = length;
  459. page_end = 0;
  460. } else {
  461. length = (sg_d[j].length - dst_offset);
  462. #ifdef DEBUG_RAMDISK_MCP
  463. printk(KERN_INFO "Step 2 - sg_s[%d]: %p length: %d"
  464. " offset: %d sg_d[%d].length: %u\n", i,
  465. &sg_s[i], sg_s[i].length, sg_s[i].offset,
  466. j, sg_d[j].length);
  467. printk(KERN_INFO "Step 2 - length: %u src_offset: %u"
  468. " dst_offset: %u\n", length, src_offset,
  469. dst_offset);
  470. #endif
  471. if (length > req->rd_size)
  472. length = req->rd_size;
  473. src = sg_virt(&sg_s[i]) + src_offset;
  474. if (!src)
  475. BUG();
  476. if (sg_s[i].length == length) {
  477. i++;
  478. src_offset = 0;
  479. } else
  480. src_offset = length;
  481. dst = sg_virt(&sg_d[j++]) + dst_offset;
  482. if (!dst)
  483. BUG();
  484. dst_offset = 0;
  485. page_end = 1;
  486. }
  487. memcpy(dst, src, length);
  488. #ifdef DEBUG_RAMDISK_MCP
  489. printk(KERN_INFO "page: %u, remaining size: %u, length: %u,"
  490. " i: %u, j: %u\n", req->rd_page,
  491. (req->rd_size - length), length, i, j);
  492. #endif
  493. req->rd_size -= length;
  494. if (!(req->rd_size))
  495. return 0;
  496. if (!page_end)
  497. continue;
  498. if (++req->rd_page <= table->page_end_offset) {
  499. #ifdef DEBUG_RAMDISK_MCP
  500. printk(KERN_INFO "page: %u in same page table\n",
  501. req->rd_page);
  502. #endif
  503. continue;
  504. }
  505. #ifdef DEBUG_RAMDISK_MCP
  506. printk(KERN_INFO "getting new page table for page: %u\n",
  507. req->rd_page);
  508. #endif
  509. table = rd_get_sg_table(dev, req->rd_page);
  510. if (!(table))
  511. return -1;
  512. sg_d = &table->sg_table[j = 0];
  513. }
  514. return 0;
  515. }
  516. /* rd_MEMCPY_do_task(): (Part of se_subsystem_api_t template)
  517. *
  518. *
  519. */
  520. static int rd_MEMCPY_do_task(struct se_task *task)
  521. {
  522. struct se_device *dev = task->se_dev;
  523. struct rd_request *req = RD_REQ(task);
  524. unsigned long long lba;
  525. int ret;
  526. req->rd_page = (task->task_lba * DEV_ATTRIB(dev)->block_size) / PAGE_SIZE;
  527. lba = task->task_lba;
  528. req->rd_offset = (do_div(lba,
  529. (PAGE_SIZE / DEV_ATTRIB(dev)->block_size))) *
  530. DEV_ATTRIB(dev)->block_size;
  531. req->rd_size = task->task_size;
  532. if (task->task_data_direction == DMA_FROM_DEVICE)
  533. ret = rd_MEMCPY_read(req);
  534. else
  535. ret = rd_MEMCPY_write(req);
  536. if (ret != 0)
  537. return ret;
  538. task->task_scsi_status = GOOD;
  539. transport_complete_task(task, 1);
  540. return PYX_TRANSPORT_SENT_TO_TRANSPORT;
  541. }
  542. /* rd_DIRECT_with_offset():
  543. *
  544. *
  545. */
  546. static int rd_DIRECT_with_offset(
  547. struct se_task *task,
  548. struct list_head *se_mem_list,
  549. u32 *se_mem_cnt,
  550. u32 *task_offset)
  551. {
  552. struct rd_request *req = RD_REQ(task);
  553. struct rd_dev *dev = req->rd_dev;
  554. struct rd_dev_sg_table *table;
  555. struct se_mem *se_mem;
  556. struct scatterlist *sg_s;
  557. u32 j = 0, set_offset = 1;
  558. u32 get_next_table = 0, offset_length, table_sg_end;
  559. table = rd_get_sg_table(dev, req->rd_page);
  560. if (!(table))
  561. return -1;
  562. table_sg_end = (table->page_end_offset - req->rd_page);
  563. sg_s = &table->sg_table[req->rd_page - table->page_start_offset];
  564. #ifdef DEBUG_RAMDISK_DR
  565. printk(KERN_INFO "%s DIRECT LBA: %llu, Size: %u Page: %u, Offset: %u\n",
  566. (task->task_data_direction == DMA_TO_DEVICE) ?
  567. "Write" : "Read",
  568. task->task_lba, req->rd_size, req->rd_page, req->rd_offset);
  569. #endif
  570. while (req->rd_size) {
  571. se_mem = kmem_cache_zalloc(se_mem_cache, GFP_KERNEL);
  572. if (!(se_mem)) {
  573. printk(KERN_ERR "Unable to allocate struct se_mem\n");
  574. return -1;
  575. }
  576. INIT_LIST_HEAD(&se_mem->se_list);
  577. if (set_offset) {
  578. offset_length = sg_s[j].length - req->rd_offset;
  579. if (offset_length > req->rd_size)
  580. offset_length = req->rd_size;
  581. se_mem->se_page = sg_page(&sg_s[j++]);
  582. se_mem->se_off = req->rd_offset;
  583. se_mem->se_len = offset_length;
  584. set_offset = 0;
  585. get_next_table = (j > table_sg_end);
  586. goto check_eot;
  587. }
  588. offset_length = (req->rd_size < req->rd_offset) ?
  589. req->rd_size : req->rd_offset;
  590. se_mem->se_page = sg_page(&sg_s[j]);
  591. se_mem->se_len = offset_length;
  592. set_offset = 1;
  593. check_eot:
  594. #ifdef DEBUG_RAMDISK_DR
  595. printk(KERN_INFO "page: %u, size: %u, offset_length: %u, j: %u"
  596. " se_mem: %p, se_page: %p se_off: %u se_len: %u\n",
  597. req->rd_page, req->rd_size, offset_length, j, se_mem,
  598. se_mem->se_page, se_mem->se_off, se_mem->se_len);
  599. #endif
  600. list_add_tail(&se_mem->se_list, se_mem_list);
  601. (*se_mem_cnt)++;
  602. req->rd_size -= offset_length;
  603. if (!(req->rd_size))
  604. goto out;
  605. if (!set_offset && !get_next_table)
  606. continue;
  607. if (++req->rd_page <= table->page_end_offset) {
  608. #ifdef DEBUG_RAMDISK_DR
  609. printk(KERN_INFO "page: %u in same page table\n",
  610. req->rd_page);
  611. #endif
  612. continue;
  613. }
  614. #ifdef DEBUG_RAMDISK_DR
  615. printk(KERN_INFO "getting new page table for page: %u\n",
  616. req->rd_page);
  617. #endif
  618. table = rd_get_sg_table(dev, req->rd_page);
  619. if (!(table))
  620. return -1;
  621. sg_s = &table->sg_table[j = 0];
  622. }
  623. out:
  624. T_TASK(task->task_se_cmd)->t_tasks_se_num += *se_mem_cnt;
  625. #ifdef DEBUG_RAMDISK_DR
  626. printk(KERN_INFO "RD_DR - Allocated %u struct se_mem segments for task\n",
  627. *se_mem_cnt);
  628. #endif
  629. return 0;
  630. }
  631. /* rd_DIRECT_without_offset():
  632. *
  633. *
  634. */
  635. static int rd_DIRECT_without_offset(
  636. struct se_task *task,
  637. struct list_head *se_mem_list,
  638. u32 *se_mem_cnt,
  639. u32 *task_offset)
  640. {
  641. struct rd_request *req = RD_REQ(task);
  642. struct rd_dev *dev = req->rd_dev;
  643. struct rd_dev_sg_table *table;
  644. struct se_mem *se_mem;
  645. struct scatterlist *sg_s;
  646. u32 length, j = 0;
  647. table = rd_get_sg_table(dev, req->rd_page);
  648. if (!(table))
  649. return -1;
  650. sg_s = &table->sg_table[req->rd_page - table->page_start_offset];
  651. #ifdef DEBUG_RAMDISK_DR
  652. printk(KERN_INFO "%s DIRECT LBA: %llu, Size: %u, Page: %u\n",
  653. (task->task_data_direction == DMA_TO_DEVICE) ?
  654. "Write" : "Read",
  655. task->task_lba, req->rd_size, req->rd_page);
  656. #endif
  657. while (req->rd_size) {
  658. se_mem = kmem_cache_zalloc(se_mem_cache, GFP_KERNEL);
  659. if (!(se_mem)) {
  660. printk(KERN_ERR "Unable to allocate struct se_mem\n");
  661. return -1;
  662. }
  663. INIT_LIST_HEAD(&se_mem->se_list);
  664. length = (req->rd_size < sg_s[j].length) ?
  665. req->rd_size : sg_s[j].length;
  666. se_mem->se_page = sg_page(&sg_s[j++]);
  667. se_mem->se_len = length;
  668. #ifdef DEBUG_RAMDISK_DR
  669. printk(KERN_INFO "page: %u, size: %u, j: %u se_mem: %p,"
  670. " se_page: %p se_off: %u se_len: %u\n", req->rd_page,
  671. req->rd_size, j, se_mem, se_mem->se_page,
  672. se_mem->se_off, se_mem->se_len);
  673. #endif
  674. list_add_tail(&se_mem->se_list, se_mem_list);
  675. (*se_mem_cnt)++;
  676. req->rd_size -= length;
  677. if (!(req->rd_size))
  678. goto out;
  679. if (++req->rd_page <= table->page_end_offset) {
  680. #ifdef DEBUG_RAMDISK_DR
  681. printk("page: %u in same page table\n",
  682. req->rd_page);
  683. #endif
  684. continue;
  685. }
  686. #ifdef DEBUG_RAMDISK_DR
  687. printk(KERN_INFO "getting new page table for page: %u\n",
  688. req->rd_page);
  689. #endif
  690. table = rd_get_sg_table(dev, req->rd_page);
  691. if (!(table))
  692. return -1;
  693. sg_s = &table->sg_table[j = 0];
  694. }
  695. out:
  696. T_TASK(task->task_se_cmd)->t_tasks_se_num += *se_mem_cnt;
  697. #ifdef DEBUG_RAMDISK_DR
  698. printk(KERN_INFO "RD_DR - Allocated %u struct se_mem segments for task\n",
  699. *se_mem_cnt);
  700. #endif
  701. return 0;
  702. }
  703. /* rd_DIRECT_do_se_mem_map():
  704. *
  705. *
  706. */
  707. static int rd_DIRECT_do_se_mem_map(
  708. struct se_task *task,
  709. struct list_head *se_mem_list,
  710. void *in_mem,
  711. struct se_mem *in_se_mem,
  712. struct se_mem **out_se_mem,
  713. u32 *se_mem_cnt,
  714. u32 *task_offset_in)
  715. {
  716. struct se_cmd *cmd = task->task_se_cmd;
  717. struct rd_request *req = RD_REQ(task);
  718. u32 task_offset = *task_offset_in;
  719. unsigned long long lba;
  720. int ret;
  721. req->rd_page = ((task->task_lba * DEV_ATTRIB(task->se_dev)->block_size) /
  722. PAGE_SIZE);
  723. lba = task->task_lba;
  724. req->rd_offset = (do_div(lba,
  725. (PAGE_SIZE / DEV_ATTRIB(task->se_dev)->block_size))) *
  726. DEV_ATTRIB(task->se_dev)->block_size;
  727. req->rd_size = task->task_size;
  728. if (req->rd_offset)
  729. ret = rd_DIRECT_with_offset(task, se_mem_list, se_mem_cnt,
  730. task_offset_in);
  731. else
  732. ret = rd_DIRECT_without_offset(task, se_mem_list, se_mem_cnt,
  733. task_offset_in);
  734. if (ret < 0)
  735. return ret;
  736. if (CMD_TFO(cmd)->task_sg_chaining == 0)
  737. return 0;
  738. /*
  739. * Currently prevent writers from multiple HW fabrics doing
  740. * pci_map_sg() to RD_DR's internal scatterlist memory.
  741. */
  742. if (cmd->data_direction == DMA_TO_DEVICE) {
  743. printk(KERN_ERR "DMA_TO_DEVICE not supported for"
  744. " RAMDISK_DR with task_sg_chaining=1\n");
  745. return -1;
  746. }
  747. /*
  748. * Special case for if task_sg_chaining is enabled, then
  749. * we setup struct se_task->task_sg[], as it will be used by
  750. * transport_do_task_sg_chain() for creating chainged SGLs
  751. * across multiple struct se_task->task_sg[].
  752. */
  753. if (!(transport_calc_sg_num(task,
  754. list_entry(T_TASK(cmd)->t_mem_list->next,
  755. struct se_mem, se_list),
  756. task_offset)))
  757. return -1;
  758. return transport_map_mem_to_sg(task, se_mem_list, task->task_sg,
  759. list_entry(T_TASK(cmd)->t_mem_list->next,
  760. struct se_mem, se_list),
  761. out_se_mem, se_mem_cnt, task_offset_in);
  762. }
  763. /* rd_DIRECT_do_task(): (Part of se_subsystem_api_t template)
  764. *
  765. *
  766. */
  767. static int rd_DIRECT_do_task(struct se_task *task)
  768. {
  769. /*
  770. * At this point the locally allocated RD tables have been mapped
  771. * to struct se_mem elements in rd_DIRECT_do_se_mem_map().
  772. */
  773. task->task_scsi_status = GOOD;
  774. transport_complete_task(task, 1);
  775. return PYX_TRANSPORT_SENT_TO_TRANSPORT;
  776. }
  777. /* rd_free_task(): (Part of se_subsystem_api_t template)
  778. *
  779. *
  780. */
  781. static void rd_free_task(struct se_task *task)
  782. {
  783. kfree(RD_REQ(task));
  784. }
  785. enum {
  786. Opt_rd_pages, Opt_err
  787. };
  788. static match_table_t tokens = {
  789. {Opt_rd_pages, "rd_pages=%d"},
  790. {Opt_err, NULL}
  791. };
  792. static ssize_t rd_set_configfs_dev_params(
  793. struct se_hba *hba,
  794. struct se_subsystem_dev *se_dev,
  795. const char *page,
  796. ssize_t count)
  797. {
  798. struct rd_dev *rd_dev = se_dev->se_dev_su_ptr;
  799. char *orig, *ptr, *opts;
  800. substring_t args[MAX_OPT_ARGS];
  801. int ret = 0, arg, token;
  802. opts = kstrdup(page, GFP_KERNEL);
  803. if (!opts)
  804. return -ENOMEM;
  805. orig = opts;
  806. while ((ptr = strsep(&opts, ",")) != NULL) {
  807. if (!*ptr)
  808. continue;
  809. token = match_token(ptr, tokens, args);
  810. switch (token) {
  811. case Opt_rd_pages:
  812. match_int(args, &arg);
  813. rd_dev->rd_page_count = arg;
  814. printk(KERN_INFO "RAMDISK: Referencing Page"
  815. " Count: %u\n", rd_dev->rd_page_count);
  816. rd_dev->rd_flags |= RDF_HAS_PAGE_COUNT;
  817. break;
  818. default:
  819. break;
  820. }
  821. }
  822. kfree(orig);
  823. return (!ret) ? count : ret;
  824. }
  825. static ssize_t rd_check_configfs_dev_params(struct se_hba *hba, struct se_subsystem_dev *se_dev)
  826. {
  827. struct rd_dev *rd_dev = se_dev->se_dev_su_ptr;
  828. if (!(rd_dev->rd_flags & RDF_HAS_PAGE_COUNT)) {
  829. printk(KERN_INFO "Missing rd_pages= parameter\n");
  830. return -1;
  831. }
  832. return 0;
  833. }
  834. static ssize_t rd_show_configfs_dev_params(
  835. struct se_hba *hba,
  836. struct se_subsystem_dev *se_dev,
  837. char *b)
  838. {
  839. struct rd_dev *rd_dev = se_dev->se_dev_su_ptr;
  840. ssize_t bl = sprintf(b, "TCM RamDisk ID: %u RamDisk Makeup: %s\n",
  841. rd_dev->rd_dev_id, (rd_dev->rd_direct) ?
  842. "rd_direct" : "rd_mcp");
  843. bl += sprintf(b + bl, " PAGES/PAGE_SIZE: %u*%lu"
  844. " SG_table_count: %u\n", rd_dev->rd_page_count,
  845. PAGE_SIZE, rd_dev->sg_table_count);
  846. return bl;
  847. }
  848. /* rd_get_cdb(): (Part of se_subsystem_api_t template)
  849. *
  850. *
  851. */
  852. static unsigned char *rd_get_cdb(struct se_task *task)
  853. {
  854. struct rd_request *req = RD_REQ(task);
  855. return req->rd_scsi_cdb;
  856. }
  857. static u32 rd_get_device_rev(struct se_device *dev)
  858. {
  859. return SCSI_SPC_2; /* Returns SPC-3 in Initiator Data */
  860. }
  861. static u32 rd_get_device_type(struct se_device *dev)
  862. {
  863. return TYPE_DISK;
  864. }
  865. static sector_t rd_get_blocks(struct se_device *dev)
  866. {
  867. struct rd_dev *rd_dev = dev->dev_ptr;
  868. unsigned long long blocks_long = ((rd_dev->rd_page_count * PAGE_SIZE) /
  869. DEV_ATTRIB(dev)->block_size) - 1;
  870. return blocks_long;
  871. }
  872. static struct se_subsystem_api rd_dr_template = {
  873. .name = "rd_dr",
  874. .transport_type = TRANSPORT_PLUGIN_VHBA_VDEV,
  875. .attach_hba = rd_attach_hba,
  876. .detach_hba = rd_detach_hba,
  877. .allocate_virtdevice = rd_DIRECT_allocate_virtdevice,
  878. .create_virtdevice = rd_DIRECT_create_virtdevice,
  879. .free_device = rd_free_device,
  880. .alloc_task = rd_alloc_task,
  881. .do_task = rd_DIRECT_do_task,
  882. .free_task = rd_free_task,
  883. .check_configfs_dev_params = rd_check_configfs_dev_params,
  884. .set_configfs_dev_params = rd_set_configfs_dev_params,
  885. .show_configfs_dev_params = rd_show_configfs_dev_params,
  886. .get_cdb = rd_get_cdb,
  887. .get_device_rev = rd_get_device_rev,
  888. .get_device_type = rd_get_device_type,
  889. .get_blocks = rd_get_blocks,
  890. .do_se_mem_map = rd_DIRECT_do_se_mem_map,
  891. };
  892. static struct se_subsystem_api rd_mcp_template = {
  893. .name = "rd_mcp",
  894. .transport_type = TRANSPORT_PLUGIN_VHBA_VDEV,
  895. .attach_hba = rd_attach_hba,
  896. .detach_hba = rd_detach_hba,
  897. .allocate_virtdevice = rd_MEMCPY_allocate_virtdevice,
  898. .create_virtdevice = rd_MEMCPY_create_virtdevice,
  899. .free_device = rd_free_device,
  900. .alloc_task = rd_alloc_task,
  901. .do_task = rd_MEMCPY_do_task,
  902. .free_task = rd_free_task,
  903. .check_configfs_dev_params = rd_check_configfs_dev_params,
  904. .set_configfs_dev_params = rd_set_configfs_dev_params,
  905. .show_configfs_dev_params = rd_show_configfs_dev_params,
  906. .get_cdb = rd_get_cdb,
  907. .get_device_rev = rd_get_device_rev,
  908. .get_device_type = rd_get_device_type,
  909. .get_blocks = rd_get_blocks,
  910. };
  911. int __init rd_module_init(void)
  912. {
  913. int ret;
  914. ret = transport_subsystem_register(&rd_dr_template);
  915. if (ret < 0)
  916. return ret;
  917. ret = transport_subsystem_register(&rd_mcp_template);
  918. if (ret < 0) {
  919. transport_subsystem_release(&rd_dr_template);
  920. return ret;
  921. }
  922. return 0;
  923. }
  924. void rd_module_exit(void)
  925. {
  926. transport_subsystem_release(&rd_dr_template);
  927. transport_subsystem_release(&rd_mcp_template);
  928. }