target_core_rd.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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-2013 Datera, Inc.
  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/highmem.h>
  29. #include <linux/timer.h>
  30. #include <linux/scatterlist.h>
  31. #include <linux/slab.h>
  32. #include <linux/spinlock.h>
  33. #include <scsi/scsi_proto.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. static int rd_attach_hba(struct se_hba *hba, u32 host_id)
  42. {
  43. struct rd_host *rd_host;
  44. rd_host = kzalloc(sizeof(*rd_host), GFP_KERNEL);
  45. if (!rd_host)
  46. return -ENOMEM;
  47. rd_host->rd_host_id = host_id;
  48. hba->hba_ptr = rd_host;
  49. pr_debug("CORE_HBA[%d] - TCM Ramdisk HBA Driver %s on"
  50. " Generic Target Core Stack %s\n", hba->hba_id,
  51. RD_HBA_VERSION, TARGET_CORE_VERSION);
  52. return 0;
  53. }
  54. static void rd_detach_hba(struct se_hba *hba)
  55. {
  56. struct rd_host *rd_host = hba->hba_ptr;
  57. pr_debug("CORE_HBA[%d] - Detached Ramdisk HBA: %u from"
  58. " Generic Target Core\n", hba->hba_id, rd_host->rd_host_id);
  59. kfree(rd_host);
  60. hba->hba_ptr = NULL;
  61. }
  62. static u32 rd_release_sgl_table(struct rd_dev *rd_dev, struct rd_dev_sg_table *sg_table,
  63. u32 sg_table_count)
  64. {
  65. struct page *pg;
  66. struct scatterlist *sg;
  67. u32 i, j, page_count = 0, sg_per_table;
  68. for (i = 0; i < sg_table_count; i++) {
  69. sg = sg_table[i].sg_table;
  70. sg_per_table = sg_table[i].rd_sg_count;
  71. for (j = 0; j < sg_per_table; j++) {
  72. pg = sg_page(&sg[j]);
  73. if (pg) {
  74. __free_page(pg);
  75. page_count++;
  76. }
  77. }
  78. kfree(sg);
  79. }
  80. kfree(sg_table);
  81. return page_count;
  82. }
  83. static void rd_release_device_space(struct rd_dev *rd_dev)
  84. {
  85. u32 page_count;
  86. if (!rd_dev->sg_table_array || !rd_dev->sg_table_count)
  87. return;
  88. page_count = rd_release_sgl_table(rd_dev, rd_dev->sg_table_array,
  89. rd_dev->sg_table_count);
  90. pr_debug("CORE_RD[%u] - Released device space for Ramdisk"
  91. " Device ID: %u, pages %u in %u tables total bytes %lu\n",
  92. rd_dev->rd_host->rd_host_id, rd_dev->rd_dev_id, page_count,
  93. rd_dev->sg_table_count, (unsigned long)page_count * PAGE_SIZE);
  94. rd_dev->sg_table_array = NULL;
  95. rd_dev->sg_table_count = 0;
  96. }
  97. /* rd_build_device_space():
  98. *
  99. *
  100. */
  101. static int rd_allocate_sgl_table(struct rd_dev *rd_dev, struct rd_dev_sg_table *sg_table,
  102. u32 total_sg_needed, unsigned char init_payload)
  103. {
  104. u32 i = 0, j, page_offset = 0, sg_per_table;
  105. u32 max_sg_per_table = (RD_MAX_ALLOCATION_SIZE /
  106. sizeof(struct scatterlist));
  107. struct page *pg;
  108. struct scatterlist *sg;
  109. unsigned char *p;
  110. while (total_sg_needed) {
  111. unsigned int chain_entry = 0;
  112. sg_per_table = (total_sg_needed > max_sg_per_table) ?
  113. max_sg_per_table : total_sg_needed;
  114. /*
  115. * Reserve extra element for chain entry
  116. */
  117. if (sg_per_table < total_sg_needed)
  118. chain_entry = 1;
  119. sg = kcalloc(sg_per_table + chain_entry, sizeof(*sg),
  120. GFP_KERNEL);
  121. if (!sg)
  122. return -ENOMEM;
  123. sg_init_table(sg, sg_per_table + chain_entry);
  124. if (i > 0) {
  125. sg_chain(sg_table[i - 1].sg_table,
  126. max_sg_per_table + 1, sg);
  127. }
  128. sg_table[i].sg_table = sg;
  129. sg_table[i].rd_sg_count = sg_per_table;
  130. sg_table[i].page_start_offset = page_offset;
  131. sg_table[i++].page_end_offset = (page_offset + sg_per_table)
  132. - 1;
  133. for (j = 0; j < sg_per_table; j++) {
  134. pg = alloc_pages(GFP_KERNEL, 0);
  135. if (!pg) {
  136. pr_err("Unable to allocate scatterlist"
  137. " pages for struct rd_dev_sg_table\n");
  138. return -ENOMEM;
  139. }
  140. sg_assign_page(&sg[j], pg);
  141. sg[j].length = PAGE_SIZE;
  142. p = kmap(pg);
  143. memset(p, init_payload, PAGE_SIZE);
  144. kunmap(pg);
  145. }
  146. page_offset += sg_per_table;
  147. total_sg_needed -= sg_per_table;
  148. }
  149. return 0;
  150. }
  151. static int rd_build_device_space(struct rd_dev *rd_dev)
  152. {
  153. struct rd_dev_sg_table *sg_table;
  154. u32 sg_tables, total_sg_needed;
  155. u32 max_sg_per_table = (RD_MAX_ALLOCATION_SIZE /
  156. sizeof(struct scatterlist));
  157. int rc;
  158. if (rd_dev->rd_page_count <= 0) {
  159. pr_err("Illegal page count: %u for Ramdisk device\n",
  160. rd_dev->rd_page_count);
  161. return -EINVAL;
  162. }
  163. /* Don't need backing pages for NULLIO */
  164. if (rd_dev->rd_flags & RDF_NULLIO)
  165. return 0;
  166. total_sg_needed = rd_dev->rd_page_count;
  167. sg_tables = (total_sg_needed / max_sg_per_table) + 1;
  168. sg_table = kcalloc(sg_tables, sizeof(*sg_table), GFP_KERNEL);
  169. if (!sg_table)
  170. return -ENOMEM;
  171. rd_dev->sg_table_array = sg_table;
  172. rd_dev->sg_table_count = sg_tables;
  173. rc = rd_allocate_sgl_table(rd_dev, sg_table, total_sg_needed, 0x00);
  174. if (rc)
  175. return rc;
  176. pr_debug("CORE_RD[%u] - Built Ramdisk Device ID: %u space of"
  177. " %u pages in %u tables\n", rd_dev->rd_host->rd_host_id,
  178. rd_dev->rd_dev_id, rd_dev->rd_page_count,
  179. rd_dev->sg_table_count);
  180. return 0;
  181. }
  182. static void rd_release_prot_space(struct rd_dev *rd_dev)
  183. {
  184. u32 page_count;
  185. if (!rd_dev->sg_prot_array || !rd_dev->sg_prot_count)
  186. return;
  187. page_count = rd_release_sgl_table(rd_dev, rd_dev->sg_prot_array,
  188. rd_dev->sg_prot_count);
  189. pr_debug("CORE_RD[%u] - Released protection space for Ramdisk"
  190. " Device ID: %u, pages %u in %u tables total bytes %lu\n",
  191. rd_dev->rd_host->rd_host_id, rd_dev->rd_dev_id, page_count,
  192. rd_dev->sg_table_count, (unsigned long)page_count * PAGE_SIZE);
  193. rd_dev->sg_prot_array = NULL;
  194. rd_dev->sg_prot_count = 0;
  195. }
  196. static int rd_build_prot_space(struct rd_dev *rd_dev, int prot_length, int block_size)
  197. {
  198. struct rd_dev_sg_table *sg_table;
  199. u32 total_sg_needed, sg_tables;
  200. u32 max_sg_per_table = (RD_MAX_ALLOCATION_SIZE /
  201. sizeof(struct scatterlist));
  202. int rc;
  203. if (rd_dev->rd_flags & RDF_NULLIO)
  204. return 0;
  205. /*
  206. * prot_length=8byte dif data
  207. * tot sg needed = rd_page_count * (PGSZ/block_size) *
  208. * (prot_length/block_size) + pad
  209. * PGSZ canceled each other.
  210. */
  211. total_sg_needed = (rd_dev->rd_page_count * prot_length / block_size) + 1;
  212. sg_tables = (total_sg_needed / max_sg_per_table) + 1;
  213. sg_table = kcalloc(sg_tables, sizeof(*sg_table), GFP_KERNEL);
  214. if (!sg_table)
  215. return -ENOMEM;
  216. rd_dev->sg_prot_array = sg_table;
  217. rd_dev->sg_prot_count = sg_tables;
  218. rc = rd_allocate_sgl_table(rd_dev, sg_table, total_sg_needed, 0xff);
  219. if (rc)
  220. return rc;
  221. pr_debug("CORE_RD[%u] - Built Ramdisk Device ID: %u prot space of"
  222. " %u pages in %u tables\n", rd_dev->rd_host->rd_host_id,
  223. rd_dev->rd_dev_id, total_sg_needed, rd_dev->sg_prot_count);
  224. return 0;
  225. }
  226. static struct se_device *rd_alloc_device(struct se_hba *hba, const char *name)
  227. {
  228. struct rd_dev *rd_dev;
  229. struct rd_host *rd_host = hba->hba_ptr;
  230. rd_dev = kzalloc(sizeof(*rd_dev), GFP_KERNEL);
  231. if (!rd_dev)
  232. return NULL;
  233. rd_dev->rd_host = rd_host;
  234. return &rd_dev->dev;
  235. }
  236. static int rd_configure_device(struct se_device *dev)
  237. {
  238. struct rd_dev *rd_dev = RD_DEV(dev);
  239. struct rd_host *rd_host = dev->se_hba->hba_ptr;
  240. int ret;
  241. if (!(rd_dev->rd_flags & RDF_HAS_PAGE_COUNT)) {
  242. pr_debug("Missing rd_pages= parameter\n");
  243. return -EINVAL;
  244. }
  245. ret = rd_build_device_space(rd_dev);
  246. if (ret < 0)
  247. goto fail;
  248. dev->dev_attrib.hw_block_size = RD_BLOCKSIZE;
  249. dev->dev_attrib.hw_max_sectors = UINT_MAX;
  250. dev->dev_attrib.hw_queue_depth = RD_MAX_DEVICE_QUEUE_DEPTH;
  251. dev->dev_attrib.is_nonrot = 1;
  252. rd_dev->rd_dev_id = rd_host->rd_host_dev_id_count++;
  253. pr_debug("CORE_RD[%u] - Added TCM MEMCPY Ramdisk Device ID: %u of"
  254. " %u pages in %u tables, %lu total bytes\n",
  255. rd_host->rd_host_id, rd_dev->rd_dev_id, rd_dev->rd_page_count,
  256. rd_dev->sg_table_count,
  257. (unsigned long)(rd_dev->rd_page_count * PAGE_SIZE));
  258. return 0;
  259. fail:
  260. rd_release_device_space(rd_dev);
  261. return ret;
  262. }
  263. static void rd_dev_call_rcu(struct rcu_head *p)
  264. {
  265. struct se_device *dev = container_of(p, struct se_device, rcu_head);
  266. struct rd_dev *rd_dev = RD_DEV(dev);
  267. kfree(rd_dev);
  268. }
  269. static void rd_free_device(struct se_device *dev)
  270. {
  271. call_rcu(&dev->rcu_head, rd_dev_call_rcu);
  272. }
  273. static void rd_destroy_device(struct se_device *dev)
  274. {
  275. struct rd_dev *rd_dev = RD_DEV(dev);
  276. rd_release_device_space(rd_dev);
  277. }
  278. static struct rd_dev_sg_table *rd_get_sg_table(struct rd_dev *rd_dev, u32 page)
  279. {
  280. struct rd_dev_sg_table *sg_table;
  281. u32 i, sg_per_table = (RD_MAX_ALLOCATION_SIZE /
  282. sizeof(struct scatterlist));
  283. i = page / sg_per_table;
  284. if (i < rd_dev->sg_table_count) {
  285. sg_table = &rd_dev->sg_table_array[i];
  286. if ((sg_table->page_start_offset <= page) &&
  287. (sg_table->page_end_offset >= page))
  288. return sg_table;
  289. }
  290. pr_err("Unable to locate struct rd_dev_sg_table for page: %u\n",
  291. page);
  292. return NULL;
  293. }
  294. static struct rd_dev_sg_table *rd_get_prot_table(struct rd_dev *rd_dev, u32 page)
  295. {
  296. struct rd_dev_sg_table *sg_table;
  297. u32 i, sg_per_table = (RD_MAX_ALLOCATION_SIZE /
  298. sizeof(struct scatterlist));
  299. i = page / sg_per_table;
  300. if (i < rd_dev->sg_prot_count) {
  301. sg_table = &rd_dev->sg_prot_array[i];
  302. if ((sg_table->page_start_offset <= page) &&
  303. (sg_table->page_end_offset >= page))
  304. return sg_table;
  305. }
  306. pr_err("Unable to locate struct prot rd_dev_sg_table for page: %u\n",
  307. page);
  308. return NULL;
  309. }
  310. static sense_reason_t rd_do_prot_rw(struct se_cmd *cmd, bool is_read)
  311. {
  312. struct se_device *se_dev = cmd->se_dev;
  313. struct rd_dev *dev = RD_DEV(se_dev);
  314. struct rd_dev_sg_table *prot_table;
  315. struct scatterlist *prot_sg;
  316. u32 sectors = cmd->data_length / se_dev->dev_attrib.block_size;
  317. u32 prot_offset, prot_page;
  318. u32 prot_npages __maybe_unused;
  319. u64 tmp;
  320. sense_reason_t rc = 0;
  321. tmp = cmd->t_task_lba * se_dev->prot_length;
  322. prot_offset = do_div(tmp, PAGE_SIZE);
  323. prot_page = tmp;
  324. prot_table = rd_get_prot_table(dev, prot_page);
  325. if (!prot_table)
  326. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  327. prot_sg = &prot_table->sg_table[prot_page -
  328. prot_table->page_start_offset];
  329. if (se_dev->dev_attrib.pi_prot_verify) {
  330. if (is_read)
  331. rc = sbc_dif_verify(cmd, cmd->t_task_lba, sectors, 0,
  332. prot_sg, prot_offset);
  333. else
  334. rc = sbc_dif_verify(cmd, cmd->t_task_lba, sectors, 0,
  335. cmd->t_prot_sg, 0);
  336. }
  337. if (!rc)
  338. sbc_dif_copy_prot(cmd, sectors, is_read, prot_sg, prot_offset);
  339. return rc;
  340. }
  341. static sense_reason_t
  342. rd_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
  343. enum dma_data_direction data_direction)
  344. {
  345. struct se_device *se_dev = cmd->se_dev;
  346. struct rd_dev *dev = RD_DEV(se_dev);
  347. struct rd_dev_sg_table *table;
  348. struct scatterlist *rd_sg;
  349. struct sg_mapping_iter m;
  350. u32 rd_offset;
  351. u32 rd_size;
  352. u32 rd_page;
  353. u32 src_len;
  354. u64 tmp;
  355. sense_reason_t rc;
  356. if (dev->rd_flags & RDF_NULLIO) {
  357. target_complete_cmd(cmd, SAM_STAT_GOOD);
  358. return 0;
  359. }
  360. tmp = cmd->t_task_lba * se_dev->dev_attrib.block_size;
  361. rd_offset = do_div(tmp, PAGE_SIZE);
  362. rd_page = tmp;
  363. rd_size = cmd->data_length;
  364. table = rd_get_sg_table(dev, rd_page);
  365. if (!table)
  366. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  367. rd_sg = &table->sg_table[rd_page - table->page_start_offset];
  368. pr_debug("RD[%u]: %s LBA: %llu, Size: %u Page: %u, Offset: %u\n",
  369. dev->rd_dev_id,
  370. data_direction == DMA_FROM_DEVICE ? "Read" : "Write",
  371. cmd->t_task_lba, rd_size, rd_page, rd_offset);
  372. if (cmd->prot_type && se_dev->dev_attrib.pi_prot_type &&
  373. data_direction == DMA_TO_DEVICE) {
  374. rc = rd_do_prot_rw(cmd, false);
  375. if (rc)
  376. return rc;
  377. }
  378. src_len = PAGE_SIZE - rd_offset;
  379. sg_miter_start(&m, sgl, sgl_nents,
  380. data_direction == DMA_FROM_DEVICE ?
  381. SG_MITER_TO_SG : SG_MITER_FROM_SG);
  382. while (rd_size) {
  383. u32 len;
  384. void *rd_addr;
  385. sg_miter_next(&m);
  386. if (!(u32)m.length) {
  387. pr_debug("RD[%u]: invalid sgl %p len %zu\n",
  388. dev->rd_dev_id, m.addr, m.length);
  389. sg_miter_stop(&m);
  390. return TCM_INCORRECT_AMOUNT_OF_DATA;
  391. }
  392. len = min((u32)m.length, src_len);
  393. if (len > rd_size) {
  394. pr_debug("RD[%u]: size underrun page %d offset %d "
  395. "size %d\n", dev->rd_dev_id,
  396. rd_page, rd_offset, rd_size);
  397. len = rd_size;
  398. }
  399. m.consumed = len;
  400. rd_addr = sg_virt(rd_sg) + rd_offset;
  401. if (data_direction == DMA_FROM_DEVICE)
  402. memcpy(m.addr, rd_addr, len);
  403. else
  404. memcpy(rd_addr, m.addr, len);
  405. rd_size -= len;
  406. if (!rd_size)
  407. continue;
  408. src_len -= len;
  409. if (src_len) {
  410. rd_offset += len;
  411. continue;
  412. }
  413. /* rd page completed, next one please */
  414. rd_page++;
  415. rd_offset = 0;
  416. src_len = PAGE_SIZE;
  417. if (rd_page <= table->page_end_offset) {
  418. rd_sg++;
  419. continue;
  420. }
  421. table = rd_get_sg_table(dev, rd_page);
  422. if (!table) {
  423. sg_miter_stop(&m);
  424. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  425. }
  426. /* since we increment, the first sg entry is correct */
  427. rd_sg = table->sg_table;
  428. }
  429. sg_miter_stop(&m);
  430. if (cmd->prot_type && se_dev->dev_attrib.pi_prot_type &&
  431. data_direction == DMA_FROM_DEVICE) {
  432. rc = rd_do_prot_rw(cmd, true);
  433. if (rc)
  434. return rc;
  435. }
  436. target_complete_cmd(cmd, SAM_STAT_GOOD);
  437. return 0;
  438. }
  439. enum {
  440. Opt_rd_pages, Opt_rd_nullio, Opt_err
  441. };
  442. static match_table_t tokens = {
  443. {Opt_rd_pages, "rd_pages=%d"},
  444. {Opt_rd_nullio, "rd_nullio=%d"},
  445. {Opt_err, NULL}
  446. };
  447. static ssize_t rd_set_configfs_dev_params(struct se_device *dev,
  448. const char *page, ssize_t count)
  449. {
  450. struct rd_dev *rd_dev = RD_DEV(dev);
  451. char *orig, *ptr, *opts;
  452. substring_t args[MAX_OPT_ARGS];
  453. int arg, token;
  454. opts = kstrdup(page, GFP_KERNEL);
  455. if (!opts)
  456. return -ENOMEM;
  457. orig = opts;
  458. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  459. if (!*ptr)
  460. continue;
  461. token = match_token(ptr, tokens, args);
  462. switch (token) {
  463. case Opt_rd_pages:
  464. match_int(args, &arg);
  465. rd_dev->rd_page_count = arg;
  466. pr_debug("RAMDISK: Referencing Page"
  467. " Count: %u\n", rd_dev->rd_page_count);
  468. rd_dev->rd_flags |= RDF_HAS_PAGE_COUNT;
  469. break;
  470. case Opt_rd_nullio:
  471. match_int(args, &arg);
  472. if (arg != 1)
  473. break;
  474. pr_debug("RAMDISK: Setting NULLIO flag: %d\n", arg);
  475. rd_dev->rd_flags |= RDF_NULLIO;
  476. break;
  477. default:
  478. break;
  479. }
  480. }
  481. kfree(orig);
  482. return count;
  483. }
  484. static ssize_t rd_show_configfs_dev_params(struct se_device *dev, char *b)
  485. {
  486. struct rd_dev *rd_dev = RD_DEV(dev);
  487. ssize_t bl = sprintf(b, "TCM RamDisk ID: %u RamDisk Makeup: rd_mcp\n",
  488. rd_dev->rd_dev_id);
  489. bl += sprintf(b + bl, " PAGES/PAGE_SIZE: %u*%lu"
  490. " SG_table_count: %u nullio: %d\n", rd_dev->rd_page_count,
  491. PAGE_SIZE, rd_dev->sg_table_count,
  492. !!(rd_dev->rd_flags & RDF_NULLIO));
  493. return bl;
  494. }
  495. static sector_t rd_get_blocks(struct se_device *dev)
  496. {
  497. struct rd_dev *rd_dev = RD_DEV(dev);
  498. unsigned long long blocks_long = ((rd_dev->rd_page_count * PAGE_SIZE) /
  499. dev->dev_attrib.block_size) - 1;
  500. return blocks_long;
  501. }
  502. static int rd_init_prot(struct se_device *dev)
  503. {
  504. struct rd_dev *rd_dev = RD_DEV(dev);
  505. if (!dev->dev_attrib.pi_prot_type)
  506. return 0;
  507. return rd_build_prot_space(rd_dev, dev->prot_length,
  508. dev->dev_attrib.block_size);
  509. }
  510. static void rd_free_prot(struct se_device *dev)
  511. {
  512. struct rd_dev *rd_dev = RD_DEV(dev);
  513. rd_release_prot_space(rd_dev);
  514. }
  515. static struct sbc_ops rd_sbc_ops = {
  516. .execute_rw = rd_execute_rw,
  517. };
  518. static sense_reason_t
  519. rd_parse_cdb(struct se_cmd *cmd)
  520. {
  521. return sbc_parse_cdb(cmd, &rd_sbc_ops);
  522. }
  523. static const struct target_backend_ops rd_mcp_ops = {
  524. .name = "rd_mcp",
  525. .inquiry_prod = "RAMDISK-MCP",
  526. .inquiry_rev = RD_MCP_VERSION,
  527. .attach_hba = rd_attach_hba,
  528. .detach_hba = rd_detach_hba,
  529. .alloc_device = rd_alloc_device,
  530. .configure_device = rd_configure_device,
  531. .destroy_device = rd_destroy_device,
  532. .free_device = rd_free_device,
  533. .parse_cdb = rd_parse_cdb,
  534. .set_configfs_dev_params = rd_set_configfs_dev_params,
  535. .show_configfs_dev_params = rd_show_configfs_dev_params,
  536. .get_device_type = sbc_get_device_type,
  537. .get_blocks = rd_get_blocks,
  538. .init_prot = rd_init_prot,
  539. .free_prot = rd_free_prot,
  540. .tb_dev_attrib_attrs = sbc_attrib_attrs,
  541. };
  542. int __init rd_module_init(void)
  543. {
  544. return transport_backend_register(&rd_mcp_ops);
  545. }
  546. void rd_module_exit(void)
  547. {
  548. target_backend_unregister(&rd_mcp_ops);
  549. }