target_core_iblock.c 20 KB

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