target_core_rd.c 17 KB

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