target_core_iblock.c 20 KB

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