target_core_iblock.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /*******************************************************************************
  2. * Filename: target_core_iblock.c
  3. *
  4. * This file contains the Storage Engine <-> Linux BlockIO 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/fs.h>
  34. #include <linux/blkdev.h>
  35. #include <linux/slab.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/bio.h>
  38. #include <linux/genhd.h>
  39. #include <linux/file.h>
  40. #include <scsi/scsi.h>
  41. #include <scsi/scsi_host.h>
  42. #include <target/target_core_base.h>
  43. #include <target/target_core_device.h>
  44. #include <target/target_core_transport.h>
  45. #include "target_core_iblock.h"
  46. #if 0
  47. #define DEBUG_IBLOCK(x...) printk(x)
  48. #else
  49. #define DEBUG_IBLOCK(x...)
  50. #endif
  51. static struct se_subsystem_api iblock_template;
  52. static void iblock_bio_done(struct bio *, int);
  53. /* iblock_attach_hba(): (Part of se_subsystem_api_t template)
  54. *
  55. *
  56. */
  57. static int iblock_attach_hba(struct se_hba *hba, u32 host_id)
  58. {
  59. struct iblock_hba *ib_host;
  60. ib_host = kzalloc(sizeof(struct iblock_hba), GFP_KERNEL);
  61. if (!(ib_host)) {
  62. printk(KERN_ERR "Unable to allocate memory for"
  63. " struct iblock_hba\n");
  64. return -ENOMEM;
  65. }
  66. ib_host->iblock_host_id = host_id;
  67. hba->hba_ptr = ib_host;
  68. printk(KERN_INFO "CORE_HBA[%d] - TCM iBlock HBA Driver %s on"
  69. " Generic Target Core Stack %s\n", hba->hba_id,
  70. IBLOCK_VERSION, TARGET_CORE_MOD_VERSION);
  71. printk(KERN_INFO "CORE_HBA[%d] - Attached iBlock HBA: %u to Generic\n",
  72. hba->hba_id, ib_host->iblock_host_id);
  73. return 0;
  74. }
  75. static void iblock_detach_hba(struct se_hba *hba)
  76. {
  77. struct iblock_hba *ib_host = hba->hba_ptr;
  78. printk(KERN_INFO "CORE_HBA[%d] - Detached iBlock HBA: %u from Generic"
  79. " Target Core\n", hba->hba_id, ib_host->iblock_host_id);
  80. kfree(ib_host);
  81. hba->hba_ptr = NULL;
  82. }
  83. static void *iblock_allocate_virtdevice(struct se_hba *hba, const char *name)
  84. {
  85. struct iblock_dev *ib_dev = NULL;
  86. struct iblock_hba *ib_host = hba->hba_ptr;
  87. ib_dev = kzalloc(sizeof(struct iblock_dev), GFP_KERNEL);
  88. if (!(ib_dev)) {
  89. printk(KERN_ERR "Unable to allocate struct iblock_dev\n");
  90. return NULL;
  91. }
  92. ib_dev->ibd_host = ib_host;
  93. printk(KERN_INFO "IBLOCK: Allocated ib_dev for %s\n", name);
  94. return ib_dev;
  95. }
  96. static struct se_device *iblock_create_virtdevice(
  97. struct se_hba *hba,
  98. struct se_subsystem_dev *se_dev,
  99. void *p)
  100. {
  101. struct iblock_dev *ib_dev = p;
  102. struct se_device *dev;
  103. struct se_dev_limits dev_limits;
  104. struct block_device *bd = NULL;
  105. struct request_queue *q;
  106. struct queue_limits *limits;
  107. u32 dev_flags = 0;
  108. int ret = -EINVAL;
  109. if (!(ib_dev)) {
  110. printk(KERN_ERR "Unable to locate struct iblock_dev parameter\n");
  111. return ERR_PTR(ret);
  112. }
  113. memset(&dev_limits, 0, sizeof(struct se_dev_limits));
  114. /*
  115. * These settings need to be made tunable..
  116. */
  117. ib_dev->ibd_bio_set = bioset_create(32, 64);
  118. if (!(ib_dev->ibd_bio_set)) {
  119. printk(KERN_ERR "IBLOCK: Unable to create bioset()\n");
  120. return ERR_PTR(-ENOMEM);
  121. }
  122. printk(KERN_INFO "IBLOCK: Created bio_set()\n");
  123. /*
  124. * iblock_check_configfs_dev_params() ensures that ib_dev->ibd_udev_path
  125. * must already have been set in order for echo 1 > $HBA/$DEV/enable to run.
  126. */
  127. printk(KERN_INFO "IBLOCK: Claiming struct block_device: %s\n",
  128. ib_dev->ibd_udev_path);
  129. bd = blkdev_get_by_path(ib_dev->ibd_udev_path,
  130. FMODE_WRITE|FMODE_READ|FMODE_EXCL, ib_dev);
  131. if (IS_ERR(bd)) {
  132. ret = PTR_ERR(bd);
  133. goto failed;
  134. }
  135. /*
  136. * Setup the local scope queue_limits from struct request_queue->limits
  137. * to pass into transport_add_device_to_core_hba() as struct se_dev_limits.
  138. */
  139. q = bdev_get_queue(bd);
  140. limits = &dev_limits.limits;
  141. limits->logical_block_size = bdev_logical_block_size(bd);
  142. limits->max_hw_sectors = queue_max_hw_sectors(q);
  143. limits->max_sectors = queue_max_sectors(q);
  144. dev_limits.hw_queue_depth = IBLOCK_MAX_DEVICE_QUEUE_DEPTH;
  145. dev_limits.queue_depth = IBLOCK_DEVICE_QUEUE_DEPTH;
  146. ib_dev->ibd_major = MAJOR(bd->bd_dev);
  147. ib_dev->ibd_minor = MINOR(bd->bd_dev);
  148. ib_dev->ibd_bd = bd;
  149. dev = transport_add_device_to_core_hba(hba,
  150. &iblock_template, se_dev, dev_flags, ib_dev,
  151. &dev_limits, "IBLOCK", IBLOCK_VERSION);
  152. if (!(dev))
  153. goto failed;
  154. ib_dev->ibd_depth = dev->queue_depth;
  155. /*
  156. * Check if the underlying struct block_device request_queue supports
  157. * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM
  158. * in ATA and we need to set TPE=1
  159. */
  160. if (blk_queue_discard(q)) {
  161. dev->se_sub_dev->se_dev_attrib.max_unmap_lba_count =
  162. q->limits.max_discard_sectors;
  163. /*
  164. * Currently hardcoded to 1 in Linux/SCSI code..
  165. */
  166. dev->se_sub_dev->se_dev_attrib.max_unmap_block_desc_count = 1;
  167. dev->se_sub_dev->se_dev_attrib.unmap_granularity =
  168. q->limits.discard_granularity;
  169. dev->se_sub_dev->se_dev_attrib.unmap_granularity_alignment =
  170. q->limits.discard_alignment;
  171. printk(KERN_INFO "IBLOCK: BLOCK Discard support available,"
  172. " disabled by default\n");
  173. }
  174. return dev;
  175. failed:
  176. if (ib_dev->ibd_bio_set) {
  177. bioset_free(ib_dev->ibd_bio_set);
  178. ib_dev->ibd_bio_set = NULL;
  179. }
  180. ib_dev->ibd_bd = NULL;
  181. ib_dev->ibd_major = 0;
  182. ib_dev->ibd_minor = 0;
  183. return ERR_PTR(ret);
  184. }
  185. static void iblock_free_device(void *p)
  186. {
  187. struct iblock_dev *ib_dev = p;
  188. if (ib_dev->ibd_bd != NULL)
  189. blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
  190. if (ib_dev->ibd_bio_set != NULL)
  191. bioset_free(ib_dev->ibd_bio_set);
  192. kfree(ib_dev);
  193. }
  194. static inline struct iblock_req *IBLOCK_REQ(struct se_task *task)
  195. {
  196. return container_of(task, struct iblock_req, ib_task);
  197. }
  198. static struct se_task *
  199. iblock_alloc_task(struct se_cmd *cmd)
  200. {
  201. struct iblock_req *ib_req;
  202. ib_req = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
  203. if (!(ib_req)) {
  204. printk(KERN_ERR "Unable to allocate memory for struct iblock_req\n");
  205. return NULL;
  206. }
  207. ib_req->ib_dev = cmd->se_dev->dev_ptr;
  208. atomic_set(&ib_req->ib_bio_cnt, 0);
  209. return &ib_req->ib_task;
  210. }
  211. static unsigned long long iblock_emulate_read_cap_with_block_size(
  212. struct se_device *dev,
  213. struct block_device *bd,
  214. struct request_queue *q)
  215. {
  216. unsigned long long blocks_long = (div_u64(i_size_read(bd->bd_inode),
  217. bdev_logical_block_size(bd)) - 1);
  218. u32 block_size = bdev_logical_block_size(bd);
  219. if (block_size == dev->se_sub_dev->se_dev_attrib.block_size)
  220. return blocks_long;
  221. switch (block_size) {
  222. case 4096:
  223. switch (dev->se_sub_dev->se_dev_attrib.block_size) {
  224. case 2048:
  225. blocks_long <<= 1;
  226. break;
  227. case 1024:
  228. blocks_long <<= 2;
  229. break;
  230. case 512:
  231. blocks_long <<= 3;
  232. default:
  233. break;
  234. }
  235. break;
  236. case 2048:
  237. switch (dev->se_sub_dev->se_dev_attrib.block_size) {
  238. case 4096:
  239. blocks_long >>= 1;
  240. break;
  241. case 1024:
  242. blocks_long <<= 1;
  243. break;
  244. case 512:
  245. blocks_long <<= 2;
  246. break;
  247. default:
  248. break;
  249. }
  250. break;
  251. case 1024:
  252. switch (dev->se_sub_dev->se_dev_attrib.block_size) {
  253. case 4096:
  254. blocks_long >>= 2;
  255. break;
  256. case 2048:
  257. blocks_long >>= 1;
  258. break;
  259. case 512:
  260. blocks_long <<= 1;
  261. break;
  262. default:
  263. break;
  264. }
  265. break;
  266. case 512:
  267. switch (dev->se_sub_dev->se_dev_attrib.block_size) {
  268. case 4096:
  269. blocks_long >>= 3;
  270. break;
  271. case 2048:
  272. blocks_long >>= 2;
  273. break;
  274. case 1024:
  275. blocks_long >>= 1;
  276. break;
  277. default:
  278. break;
  279. }
  280. break;
  281. default:
  282. break;
  283. }
  284. return blocks_long;
  285. }
  286. /*
  287. * Emulate SYCHRONIZE_CACHE_*
  288. */
  289. static void iblock_emulate_sync_cache(struct se_task *task)
  290. {
  291. struct se_cmd *cmd = task->task_se_cmd;
  292. struct iblock_dev *ib_dev = cmd->se_dev->dev_ptr;
  293. int immed = (cmd->t_task.t_task_cdb[1] & 0x2);
  294. sector_t error_sector;
  295. int ret;
  296. /*
  297. * If the Immediate bit is set, queue up the GOOD response
  298. * for this SYNCHRONIZE_CACHE op
  299. */
  300. if (immed)
  301. transport_complete_sync_cache(cmd, 1);
  302. /*
  303. * blkdev_issue_flush() does not support a specifying a range, so
  304. * we have to flush the entire cache.
  305. */
  306. ret = blkdev_issue_flush(ib_dev->ibd_bd, GFP_KERNEL, &error_sector);
  307. if (ret != 0) {
  308. printk(KERN_ERR "IBLOCK: block_issue_flush() failed: %d "
  309. " error_sector: %llu\n", ret,
  310. (unsigned long long)error_sector);
  311. }
  312. if (!immed)
  313. transport_complete_sync_cache(cmd, ret == 0);
  314. }
  315. /*
  316. * Tell TCM Core that we are capable of WriteCache emulation for
  317. * an underlying struct se_device.
  318. */
  319. static int iblock_emulated_write_cache(struct se_device *dev)
  320. {
  321. return 1;
  322. }
  323. static int iblock_emulated_dpo(struct se_device *dev)
  324. {
  325. return 0;
  326. }
  327. /*
  328. * Tell TCM Core that we will be emulating Forced Unit Access (FUA) for WRITEs
  329. * for TYPE_DISK.
  330. */
  331. static int iblock_emulated_fua_write(struct se_device *dev)
  332. {
  333. return 1;
  334. }
  335. static int iblock_emulated_fua_read(struct se_device *dev)
  336. {
  337. return 0;
  338. }
  339. static int iblock_do_task(struct se_task *task)
  340. {
  341. struct se_device *dev = task->task_se_cmd->se_dev;
  342. struct iblock_req *req = IBLOCK_REQ(task);
  343. struct bio *bio = req->ib_bio, *nbio = NULL;
  344. struct blk_plug plug;
  345. int rw;
  346. if (task->task_data_direction == DMA_TO_DEVICE) {
  347. /*
  348. * Force data to disk if we pretend to not have a volatile
  349. * write cache, or the initiator set the Force Unit Access bit.
  350. */
  351. if (dev->se_sub_dev->se_dev_attrib.emulate_write_cache == 0 ||
  352. (dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0 &&
  353. task->task_se_cmd->t_task.t_tasks_fua))
  354. rw = WRITE_FUA;
  355. else
  356. rw = WRITE;
  357. } else {
  358. rw = READ;
  359. }
  360. blk_start_plug(&plug);
  361. while (bio) {
  362. nbio = bio->bi_next;
  363. bio->bi_next = NULL;
  364. DEBUG_IBLOCK("Calling submit_bio() task: %p bio: %p"
  365. " bio->bi_sector: %llu\n", task, bio, bio->bi_sector);
  366. submit_bio(rw, bio);
  367. bio = nbio;
  368. }
  369. blk_finish_plug(&plug);
  370. return PYX_TRANSPORT_SENT_TO_TRANSPORT;
  371. }
  372. static int iblock_do_discard(struct se_device *dev, sector_t lba, u32 range)
  373. {
  374. struct iblock_dev *ibd = dev->dev_ptr;
  375. struct block_device *bd = ibd->ibd_bd;
  376. int barrier = 0;
  377. return blkdev_issue_discard(bd, lba, range, GFP_KERNEL, barrier);
  378. }
  379. static void iblock_free_task(struct se_task *task)
  380. {
  381. struct iblock_req *req = IBLOCK_REQ(task);
  382. struct bio *bio, *hbio = req->ib_bio;
  383. /*
  384. * We only release the bio(s) here if iblock_bio_done() has not called
  385. * bio_put() -> iblock_bio_destructor().
  386. */
  387. while (hbio != NULL) {
  388. bio = hbio;
  389. hbio = hbio->bi_next;
  390. bio->bi_next = NULL;
  391. bio_put(bio);
  392. }
  393. kfree(req);
  394. }
  395. enum {
  396. Opt_udev_path, Opt_force, Opt_err
  397. };
  398. static match_table_t tokens = {
  399. {Opt_udev_path, "udev_path=%s"},
  400. {Opt_force, "force=%d"},
  401. {Opt_err, NULL}
  402. };
  403. static ssize_t iblock_set_configfs_dev_params(struct se_hba *hba,
  404. struct se_subsystem_dev *se_dev,
  405. const char *page, ssize_t count)
  406. {
  407. struct iblock_dev *ib_dev = se_dev->se_dev_su_ptr;
  408. char *orig, *ptr, *arg_p, *opts;
  409. substring_t args[MAX_OPT_ARGS];
  410. int ret = 0, arg, token;
  411. opts = kstrdup(page, GFP_KERNEL);
  412. if (!opts)
  413. return -ENOMEM;
  414. orig = opts;
  415. while ((ptr = strsep(&opts, ",")) != NULL) {
  416. if (!*ptr)
  417. continue;
  418. token = match_token(ptr, tokens, args);
  419. switch (token) {
  420. case Opt_udev_path:
  421. if (ib_dev->ibd_bd) {
  422. printk(KERN_ERR "Unable to set udev_path= while"
  423. " ib_dev->ibd_bd exists\n");
  424. ret = -EEXIST;
  425. goto out;
  426. }
  427. arg_p = match_strdup(&args[0]);
  428. if (!arg_p) {
  429. ret = -ENOMEM;
  430. break;
  431. }
  432. snprintf(ib_dev->ibd_udev_path, SE_UDEV_PATH_LEN,
  433. "%s", arg_p);
  434. kfree(arg_p);
  435. printk(KERN_INFO "IBLOCK: Referencing UDEV path: %s\n",
  436. ib_dev->ibd_udev_path);
  437. ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH;
  438. break;
  439. case Opt_force:
  440. match_int(args, &arg);
  441. ib_dev->ibd_force = arg;
  442. printk(KERN_INFO "IBLOCK: Set force=%d\n",
  443. ib_dev->ibd_force);
  444. break;
  445. default:
  446. break;
  447. }
  448. }
  449. out:
  450. kfree(orig);
  451. return (!ret) ? count : ret;
  452. }
  453. static ssize_t iblock_check_configfs_dev_params(
  454. struct se_hba *hba,
  455. struct se_subsystem_dev *se_dev)
  456. {
  457. struct iblock_dev *ibd = se_dev->se_dev_su_ptr;
  458. if (!(ibd->ibd_flags & IBDF_HAS_UDEV_PATH)) {
  459. printk(KERN_ERR "Missing udev_path= parameters for IBLOCK\n");
  460. return -EINVAL;
  461. }
  462. return 0;
  463. }
  464. static ssize_t iblock_show_configfs_dev_params(
  465. struct se_hba *hba,
  466. struct se_subsystem_dev *se_dev,
  467. char *b)
  468. {
  469. struct iblock_dev *ibd = se_dev->se_dev_su_ptr;
  470. struct block_device *bd = ibd->ibd_bd;
  471. char buf[BDEVNAME_SIZE];
  472. ssize_t bl = 0;
  473. if (bd)
  474. bl += sprintf(b + bl, "iBlock device: %s",
  475. bdevname(bd, buf));
  476. if (ibd->ibd_flags & IBDF_HAS_UDEV_PATH) {
  477. bl += sprintf(b + bl, " UDEV PATH: %s\n",
  478. ibd->ibd_udev_path);
  479. } else
  480. bl += sprintf(b + bl, "\n");
  481. bl += sprintf(b + bl, " ");
  482. if (bd) {
  483. bl += sprintf(b + bl, "Major: %d Minor: %d %s\n",
  484. ibd->ibd_major, ibd->ibd_minor, (!bd->bd_contains) ?
  485. "" : (bd->bd_holder == (struct iblock_dev *)ibd) ?
  486. "CLAIMED: IBLOCK" : "CLAIMED: OS");
  487. } else {
  488. bl += sprintf(b + bl, "Major: %d Minor: %d\n",
  489. ibd->ibd_major, ibd->ibd_minor);
  490. }
  491. return bl;
  492. }
  493. static void iblock_bio_destructor(struct bio *bio)
  494. {
  495. struct se_task *task = bio->bi_private;
  496. struct iblock_dev *ib_dev = task->se_dev->dev_ptr;
  497. bio_free(bio, ib_dev->ibd_bio_set);
  498. }
  499. static struct bio *iblock_get_bio(
  500. struct se_task *task,
  501. struct iblock_req *ib_req,
  502. struct iblock_dev *ib_dev,
  503. int *ret,
  504. sector_t lba,
  505. u32 sg_num)
  506. {
  507. struct bio *bio;
  508. bio = bio_alloc_bioset(GFP_NOIO, sg_num, ib_dev->ibd_bio_set);
  509. if (!(bio)) {
  510. printk(KERN_ERR "Unable to allocate memory for bio\n");
  511. *ret = PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
  512. return NULL;
  513. }
  514. DEBUG_IBLOCK("Allocated bio: %p task_sg_num: %u using ibd_bio_set:"
  515. " %p\n", bio, task->task_sg_num, ib_dev->ibd_bio_set);
  516. DEBUG_IBLOCK("Allocated bio: %p task_size: %u\n", bio, task->task_size);
  517. bio->bi_bdev = ib_dev->ibd_bd;
  518. bio->bi_private = task;
  519. bio->bi_destructor = iblock_bio_destructor;
  520. bio->bi_end_io = &iblock_bio_done;
  521. bio->bi_sector = lba;
  522. atomic_inc(&ib_req->ib_bio_cnt);
  523. DEBUG_IBLOCK("Set bio->bi_sector: %llu\n", bio->bi_sector);
  524. DEBUG_IBLOCK("Set ib_req->ib_bio_cnt: %d\n",
  525. atomic_read(&ib_req->ib_bio_cnt));
  526. return bio;
  527. }
  528. static int iblock_map_task_SG(struct se_task *task)
  529. {
  530. struct se_cmd *cmd = task->task_se_cmd;
  531. struct se_device *dev = cmd->se_dev;
  532. struct iblock_dev *ib_dev = task->se_dev->dev_ptr;
  533. struct iblock_req *ib_req = IBLOCK_REQ(task);
  534. struct bio *bio = NULL, *hbio = NULL, *tbio = NULL;
  535. struct scatterlist *sg;
  536. int ret = 0;
  537. u32 i, sg_num = task->task_sg_num;
  538. sector_t block_lba;
  539. /*
  540. * Do starting conversion up from non 512-byte blocksize with
  541. * struct se_task SCSI blocksize into Linux/Block 512 units for BIO.
  542. */
  543. if (dev->se_sub_dev->se_dev_attrib.block_size == 4096)
  544. block_lba = (task->task_lba << 3);
  545. else if (dev->se_sub_dev->se_dev_attrib.block_size == 2048)
  546. block_lba = (task->task_lba << 2);
  547. else if (dev->se_sub_dev->se_dev_attrib.block_size == 1024)
  548. block_lba = (task->task_lba << 1);
  549. else if (dev->se_sub_dev->se_dev_attrib.block_size == 512)
  550. block_lba = task->task_lba;
  551. else {
  552. printk(KERN_ERR "Unsupported SCSI -> BLOCK LBA conversion:"
  553. " %u\n", dev->se_sub_dev->se_dev_attrib.block_size);
  554. return PYX_TRANSPORT_LU_COMM_FAILURE;
  555. }
  556. bio = iblock_get_bio(task, ib_req, ib_dev, &ret, block_lba, sg_num);
  557. if (!(bio))
  558. return ret;
  559. ib_req->ib_bio = bio;
  560. hbio = tbio = bio;
  561. /*
  562. * Use fs/bio.c:bio_add_pages() to setup the bio_vec maplist
  563. * from TCM struct se_mem -> task->task_sg -> struct scatterlist memory.
  564. */
  565. for_each_sg(task->task_sg, sg, task->task_sg_num, i) {
  566. DEBUG_IBLOCK("task: %p bio: %p Calling bio_add_page(): page:"
  567. " %p len: %u offset: %u\n", task, bio, sg_page(sg),
  568. sg->length, sg->offset);
  569. again:
  570. ret = bio_add_page(bio, sg_page(sg), sg->length, sg->offset);
  571. if (ret != sg->length) {
  572. DEBUG_IBLOCK("*** Set bio->bi_sector: %llu\n",
  573. bio->bi_sector);
  574. DEBUG_IBLOCK("** task->task_size: %u\n",
  575. task->task_size);
  576. DEBUG_IBLOCK("*** bio->bi_max_vecs: %u\n",
  577. bio->bi_max_vecs);
  578. DEBUG_IBLOCK("*** bio->bi_vcnt: %u\n",
  579. bio->bi_vcnt);
  580. bio = iblock_get_bio(task, ib_req, ib_dev, &ret,
  581. block_lba, sg_num);
  582. if (!(bio))
  583. goto fail;
  584. tbio = tbio->bi_next = bio;
  585. DEBUG_IBLOCK("-----------------> Added +1 bio: %p to"
  586. " list, Going to again\n", bio);
  587. goto again;
  588. }
  589. /* Always in 512 byte units for Linux/Block */
  590. block_lba += sg->length >> IBLOCK_LBA_SHIFT;
  591. sg_num--;
  592. DEBUG_IBLOCK("task: %p bio-add_page() passed!, decremented"
  593. " sg_num to %u\n", task, sg_num);
  594. DEBUG_IBLOCK("task: %p bio_add_page() passed!, increased lba"
  595. " to %llu\n", task, block_lba);
  596. DEBUG_IBLOCK("task: %p bio_add_page() passed!, bio->bi_vcnt:"
  597. " %u\n", task, bio->bi_vcnt);
  598. }
  599. return 0;
  600. fail:
  601. while (hbio) {
  602. bio = hbio;
  603. hbio = hbio->bi_next;
  604. bio->bi_next = NULL;
  605. bio_put(bio);
  606. }
  607. return ret;
  608. }
  609. static unsigned char *iblock_get_cdb(struct se_task *task)
  610. {
  611. return IBLOCK_REQ(task)->ib_scsi_cdb;
  612. }
  613. static u32 iblock_get_device_rev(struct se_device *dev)
  614. {
  615. return SCSI_SPC_2; /* Returns SPC-3 in Initiator Data */
  616. }
  617. static u32 iblock_get_device_type(struct se_device *dev)
  618. {
  619. return TYPE_DISK;
  620. }
  621. static sector_t iblock_get_blocks(struct se_device *dev)
  622. {
  623. struct iblock_dev *ibd = dev->dev_ptr;
  624. struct block_device *bd = ibd->ibd_bd;
  625. struct request_queue *q = bdev_get_queue(bd);
  626. return iblock_emulate_read_cap_with_block_size(dev, bd, q);
  627. }
  628. static void iblock_bio_done(struct bio *bio, int err)
  629. {
  630. struct se_task *task = bio->bi_private;
  631. struct iblock_req *ibr = IBLOCK_REQ(task);
  632. /*
  633. * Set -EIO if !BIO_UPTODATE and the passed is still err=0
  634. */
  635. if (!(test_bit(BIO_UPTODATE, &bio->bi_flags)) && !(err))
  636. err = -EIO;
  637. if (err != 0) {
  638. printk(KERN_ERR "test_bit(BIO_UPTODATE) failed for bio: %p,"
  639. " err: %d\n", bio, err);
  640. /*
  641. * Bump the ib_bio_err_cnt and release bio.
  642. */
  643. atomic_inc(&ibr->ib_bio_err_cnt);
  644. smp_mb__after_atomic_inc();
  645. bio_put(bio);
  646. /*
  647. * Wait to complete the task until the last bio as completed.
  648. */
  649. if (!(atomic_dec_and_test(&ibr->ib_bio_cnt)))
  650. return;
  651. ibr->ib_bio = NULL;
  652. transport_complete_task(task, 0);
  653. return;
  654. }
  655. DEBUG_IBLOCK("done[%p] bio: %p task_lba: %llu bio_lba: %llu err=%d\n",
  656. task, bio, task->task_lba, bio->bi_sector, err);
  657. /*
  658. * bio_put() will call iblock_bio_destructor() to release the bio back
  659. * to ibr->ib_bio_set.
  660. */
  661. bio_put(bio);
  662. /*
  663. * Wait to complete the task until the last bio as completed.
  664. */
  665. if (!(atomic_dec_and_test(&ibr->ib_bio_cnt)))
  666. return;
  667. /*
  668. * Return GOOD status for task if zero ib_bio_err_cnt exists.
  669. */
  670. ibr->ib_bio = NULL;
  671. transport_complete_task(task, (!atomic_read(&ibr->ib_bio_err_cnt)));
  672. }
  673. static struct se_subsystem_api iblock_template = {
  674. .name = "iblock",
  675. .owner = THIS_MODULE,
  676. .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV,
  677. .map_task_SG = iblock_map_task_SG,
  678. .attach_hba = iblock_attach_hba,
  679. .detach_hba = iblock_detach_hba,
  680. .allocate_virtdevice = iblock_allocate_virtdevice,
  681. .create_virtdevice = iblock_create_virtdevice,
  682. .free_device = iblock_free_device,
  683. .dpo_emulated = iblock_emulated_dpo,
  684. .fua_write_emulated = iblock_emulated_fua_write,
  685. .fua_read_emulated = iblock_emulated_fua_read,
  686. .write_cache_emulated = iblock_emulated_write_cache,
  687. .alloc_task = iblock_alloc_task,
  688. .do_task = iblock_do_task,
  689. .do_discard = iblock_do_discard,
  690. .do_sync_cache = iblock_emulate_sync_cache,
  691. .free_task = iblock_free_task,
  692. .check_configfs_dev_params = iblock_check_configfs_dev_params,
  693. .set_configfs_dev_params = iblock_set_configfs_dev_params,
  694. .show_configfs_dev_params = iblock_show_configfs_dev_params,
  695. .get_cdb = iblock_get_cdb,
  696. .get_device_rev = iblock_get_device_rev,
  697. .get_device_type = iblock_get_device_type,
  698. .get_blocks = iblock_get_blocks,
  699. };
  700. static int __init iblock_module_init(void)
  701. {
  702. return transport_subsystem_register(&iblock_template);
  703. }
  704. static void iblock_module_exit(void)
  705. {
  706. transport_subsystem_release(&iblock_template);
  707. }
  708. MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin");
  709. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  710. MODULE_LICENSE("GPL");
  711. module_init(iblock_module_init);
  712. module_exit(iblock_module_exit);