target_core_xcopy.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. /*******************************************************************************
  2. * Filename: target_core_xcopy.c
  3. *
  4. * This file contains support for SPC-4 Extended-Copy offload with generic
  5. * TCM backends.
  6. *
  7. * Copyright (c) 2011-2013 Datera, Inc. All rights reserved.
  8. *
  9. * Author:
  10. * Nicholas A. Bellinger <nab@daterainc.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. ******************************************************************************/
  23. #include <linux/slab.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/list.h>
  26. #include <linux/configfs.h>
  27. #include <scsi/scsi.h>
  28. #include <scsi/scsi_cmnd.h>
  29. #include <asm/unaligned.h>
  30. #include <target/target_core_base.h>
  31. #include <target/target_core_backend.h>
  32. #include <target/target_core_fabric.h>
  33. #include "target_core_internal.h"
  34. #include "target_core_pr.h"
  35. #include "target_core_ua.h"
  36. #include "target_core_xcopy.h"
  37. static struct workqueue_struct *xcopy_wq = NULL;
  38. static int target_xcopy_gen_naa_ieee(struct se_device *dev, unsigned char *buf)
  39. {
  40. int off = 0;
  41. buf[off++] = (0x6 << 4);
  42. buf[off++] = 0x01;
  43. buf[off++] = 0x40;
  44. buf[off] = (0x5 << 4);
  45. spc_parse_naa_6h_vendor_specific(dev, &buf[off]);
  46. return 0;
  47. }
  48. static int target_xcopy_locate_se_dev_e4(struct se_cmd *se_cmd, struct xcopy_op *xop,
  49. bool src)
  50. {
  51. struct se_device *se_dev;
  52. unsigned char tmp_dev_wwn[XCOPY_NAA_IEEE_REGEX_LEN], *dev_wwn;
  53. int rc;
  54. if (src)
  55. dev_wwn = &xop->dst_tid_wwn[0];
  56. else
  57. dev_wwn = &xop->src_tid_wwn[0];
  58. mutex_lock(&g_device_mutex);
  59. list_for_each_entry(se_dev, &g_device_list, g_dev_node) {
  60. if (!se_dev->dev_attrib.emulate_3pc)
  61. continue;
  62. memset(&tmp_dev_wwn[0], 0, XCOPY_NAA_IEEE_REGEX_LEN);
  63. target_xcopy_gen_naa_ieee(se_dev, &tmp_dev_wwn[0]);
  64. rc = memcmp(&tmp_dev_wwn[0], dev_wwn, XCOPY_NAA_IEEE_REGEX_LEN);
  65. if (rc != 0)
  66. continue;
  67. if (src) {
  68. xop->dst_dev = se_dev;
  69. pr_debug("XCOPY 0xe4: Setting xop->dst_dev: %p from located"
  70. " se_dev\n", xop->dst_dev);
  71. } else {
  72. xop->src_dev = se_dev;
  73. pr_debug("XCOPY 0xe4: Setting xop->src_dev: %p from located"
  74. " se_dev\n", xop->src_dev);
  75. }
  76. rc = target_depend_item(&se_dev->dev_group.cg_item);
  77. if (rc != 0) {
  78. pr_err("configfs_depend_item attempt failed:"
  79. " %d for se_dev: %p\n", rc, se_dev);
  80. mutex_unlock(&g_device_mutex);
  81. return rc;
  82. }
  83. pr_debug("Called configfs_depend_item for se_dev: %p"
  84. " se_dev->se_dev_group: %p\n", se_dev,
  85. &se_dev->dev_group);
  86. mutex_unlock(&g_device_mutex);
  87. return 0;
  88. }
  89. mutex_unlock(&g_device_mutex);
  90. pr_err("Unable to locate 0xe4 descriptor for EXTENDED_COPY\n");
  91. return -EINVAL;
  92. }
  93. static int target_xcopy_parse_tiddesc_e4(struct se_cmd *se_cmd, struct xcopy_op *xop,
  94. unsigned char *p, bool src)
  95. {
  96. unsigned char *desc = p;
  97. unsigned short ript;
  98. u8 desig_len;
  99. /*
  100. * Extract RELATIVE INITIATOR PORT IDENTIFIER
  101. */
  102. ript = get_unaligned_be16(&desc[2]);
  103. pr_debug("XCOPY 0xe4: RELATIVE INITIATOR PORT IDENTIFIER: %hu\n", ript);
  104. /*
  105. * Check for supported code set, association, and designator type
  106. */
  107. if ((desc[4] & 0x0f) != 0x1) {
  108. pr_err("XCOPY 0xe4: code set of non binary type not supported\n");
  109. return -EINVAL;
  110. }
  111. if ((desc[5] & 0x30) != 0x00) {
  112. pr_err("XCOPY 0xe4: association other than LUN not supported\n");
  113. return -EINVAL;
  114. }
  115. if ((desc[5] & 0x0f) != 0x3) {
  116. pr_err("XCOPY 0xe4: designator type unsupported: 0x%02x\n",
  117. (desc[5] & 0x0f));
  118. return -EINVAL;
  119. }
  120. /*
  121. * Check for matching 16 byte length for NAA IEEE Registered Extended
  122. * Assigned designator
  123. */
  124. desig_len = desc[7];
  125. if (desig_len != 16) {
  126. pr_err("XCOPY 0xe4: invalid desig_len: %d\n", (int)desig_len);
  127. return -EINVAL;
  128. }
  129. pr_debug("XCOPY 0xe4: desig_len: %d\n", (int)desig_len);
  130. /*
  131. * Check for NAA IEEE Registered Extended Assigned header..
  132. */
  133. if ((desc[8] & 0xf0) != 0x60) {
  134. pr_err("XCOPY 0xe4: Unsupported DESIGNATOR TYPE: 0x%02x\n",
  135. (desc[8] & 0xf0));
  136. return -EINVAL;
  137. }
  138. if (src) {
  139. memcpy(&xop->src_tid_wwn[0], &desc[8], XCOPY_NAA_IEEE_REGEX_LEN);
  140. /*
  141. * Determine if the source designator matches the local device
  142. */
  143. if (!memcmp(&xop->local_dev_wwn[0], &xop->src_tid_wwn[0],
  144. XCOPY_NAA_IEEE_REGEX_LEN)) {
  145. xop->op_origin = XCOL_SOURCE_RECV_OP;
  146. xop->src_dev = se_cmd->se_dev;
  147. pr_debug("XCOPY 0xe4: Set xop->src_dev %p from source"
  148. " received xop\n", xop->src_dev);
  149. }
  150. } else {
  151. memcpy(&xop->dst_tid_wwn[0], &desc[8], XCOPY_NAA_IEEE_REGEX_LEN);
  152. /*
  153. * Determine if the destination designator matches the local device
  154. */
  155. if (!memcmp(&xop->local_dev_wwn[0], &xop->dst_tid_wwn[0],
  156. XCOPY_NAA_IEEE_REGEX_LEN)) {
  157. xop->op_origin = XCOL_DEST_RECV_OP;
  158. xop->dst_dev = se_cmd->se_dev;
  159. pr_debug("XCOPY 0xe4: Set xop->dst_dev: %p from destination"
  160. " received xop\n", xop->dst_dev);
  161. }
  162. }
  163. return 0;
  164. }
  165. static int target_xcopy_parse_target_descriptors(struct se_cmd *se_cmd,
  166. struct xcopy_op *xop, unsigned char *p,
  167. unsigned short tdll)
  168. {
  169. struct se_device *local_dev = se_cmd->se_dev;
  170. unsigned char *desc = p;
  171. int offset = tdll % XCOPY_TARGET_DESC_LEN, rc, ret = 0;
  172. unsigned short start = 0;
  173. bool src = true;
  174. if (offset != 0) {
  175. pr_err("XCOPY target descriptor list length is not"
  176. " multiple of %d\n", XCOPY_TARGET_DESC_LEN);
  177. return -EINVAL;
  178. }
  179. if (tdll > 64) {
  180. pr_err("XCOPY target descriptor supports a maximum"
  181. " two src/dest descriptors, tdll: %hu too large..\n", tdll);
  182. return -EINVAL;
  183. }
  184. /*
  185. * Generate an IEEE Registered Extended designator based upon the
  186. * se_device the XCOPY was received upon..
  187. */
  188. memset(&xop->local_dev_wwn[0], 0, XCOPY_NAA_IEEE_REGEX_LEN);
  189. target_xcopy_gen_naa_ieee(local_dev, &xop->local_dev_wwn[0]);
  190. while (start < tdll) {
  191. /*
  192. * Check target descriptor identification with 0xE4 type with
  193. * use VPD 0x83 WWPN matching ..
  194. */
  195. switch (desc[0]) {
  196. case 0xe4:
  197. rc = target_xcopy_parse_tiddesc_e4(se_cmd, xop,
  198. &desc[0], src);
  199. if (rc != 0)
  200. goto out;
  201. /*
  202. * Assume target descriptors are in source -> destination order..
  203. */
  204. if (src)
  205. src = false;
  206. else
  207. src = true;
  208. start += XCOPY_TARGET_DESC_LEN;
  209. desc += XCOPY_TARGET_DESC_LEN;
  210. ret++;
  211. break;
  212. default:
  213. pr_err("XCOPY unsupported descriptor type code:"
  214. " 0x%02x\n", desc[0]);
  215. goto out;
  216. }
  217. }
  218. if (xop->op_origin == XCOL_SOURCE_RECV_OP)
  219. rc = target_xcopy_locate_se_dev_e4(se_cmd, xop, true);
  220. else
  221. rc = target_xcopy_locate_se_dev_e4(se_cmd, xop, false);
  222. if (rc < 0)
  223. goto out;
  224. pr_debug("XCOPY TGT desc: Source dev: %p NAA IEEE WWN: 0x%16phN\n",
  225. xop->src_dev, &xop->src_tid_wwn[0]);
  226. pr_debug("XCOPY TGT desc: Dest dev: %p NAA IEEE WWN: 0x%16phN\n",
  227. xop->dst_dev, &xop->dst_tid_wwn[0]);
  228. return ret;
  229. out:
  230. return -EINVAL;
  231. }
  232. static int target_xcopy_parse_segdesc_02(struct se_cmd *se_cmd, struct xcopy_op *xop,
  233. unsigned char *p)
  234. {
  235. unsigned char *desc = p;
  236. int dc = (desc[1] & 0x02);
  237. unsigned short desc_len;
  238. desc_len = get_unaligned_be16(&desc[2]);
  239. if (desc_len != 0x18) {
  240. pr_err("XCOPY segment desc 0x02: Illegal desc_len:"
  241. " %hu\n", desc_len);
  242. return -EINVAL;
  243. }
  244. xop->stdi = get_unaligned_be16(&desc[4]);
  245. xop->dtdi = get_unaligned_be16(&desc[6]);
  246. pr_debug("XCOPY seg desc 0x02: desc_len: %hu stdi: %hu dtdi: %hu, DC: %d\n",
  247. desc_len, xop->stdi, xop->dtdi, dc);
  248. xop->nolb = get_unaligned_be16(&desc[10]);
  249. xop->src_lba = get_unaligned_be64(&desc[12]);
  250. xop->dst_lba = get_unaligned_be64(&desc[20]);
  251. pr_debug("XCOPY seg desc 0x02: nolb: %hu src_lba: %llu dst_lba: %llu\n",
  252. xop->nolb, (unsigned long long)xop->src_lba,
  253. (unsigned long long)xop->dst_lba);
  254. if (dc != 0) {
  255. xop->dbl = (desc[29] & 0xff) << 16;
  256. xop->dbl |= (desc[30] & 0xff) << 8;
  257. xop->dbl |= desc[31] & 0xff;
  258. pr_debug("XCOPY seg desc 0x02: DC=1 w/ dbl: %u\n", xop->dbl);
  259. }
  260. return 0;
  261. }
  262. static int target_xcopy_parse_segment_descriptors(struct se_cmd *se_cmd,
  263. struct xcopy_op *xop, unsigned char *p,
  264. unsigned int sdll)
  265. {
  266. unsigned char *desc = p;
  267. unsigned int start = 0;
  268. int offset = sdll % XCOPY_SEGMENT_DESC_LEN, rc, ret = 0;
  269. if (offset != 0) {
  270. pr_err("XCOPY segment descriptor list length is not"
  271. " multiple of %d\n", XCOPY_SEGMENT_DESC_LEN);
  272. return -EINVAL;
  273. }
  274. while (start < sdll) {
  275. /*
  276. * Check segment descriptor type code for block -> block
  277. */
  278. switch (desc[0]) {
  279. case 0x02:
  280. rc = target_xcopy_parse_segdesc_02(se_cmd, xop, desc);
  281. if (rc < 0)
  282. goto out;
  283. ret++;
  284. start += XCOPY_SEGMENT_DESC_LEN;
  285. desc += XCOPY_SEGMENT_DESC_LEN;
  286. break;
  287. default:
  288. pr_err("XCOPY unsupported segment descriptor"
  289. "type: 0x%02x\n", desc[0]);
  290. goto out;
  291. }
  292. }
  293. return ret;
  294. out:
  295. return -EINVAL;
  296. }
  297. /*
  298. * Start xcopy_pt ops
  299. */
  300. struct xcopy_pt_cmd {
  301. bool remote_port;
  302. struct se_cmd se_cmd;
  303. struct xcopy_op *xcopy_op;
  304. struct completion xpt_passthrough_sem;
  305. unsigned char sense_buffer[TRANSPORT_SENSE_BUFFER];
  306. };
  307. struct se_portal_group xcopy_pt_tpg;
  308. static struct se_session xcopy_pt_sess;
  309. static struct se_node_acl xcopy_pt_nacl;
  310. static char *xcopy_pt_get_fabric_name(void)
  311. {
  312. return "xcopy-pt";
  313. }
  314. static int xcopy_pt_get_cmd_state(struct se_cmd *se_cmd)
  315. {
  316. return 0;
  317. }
  318. static void xcopy_pt_undepend_remotedev(struct xcopy_op *xop)
  319. {
  320. struct se_device *remote_dev;
  321. if (xop->op_origin == XCOL_SOURCE_RECV_OP)
  322. remote_dev = xop->dst_dev;
  323. else
  324. remote_dev = xop->src_dev;
  325. pr_debug("Calling configfs_undepend_item for"
  326. " remote_dev: %p remote_dev->dev_group: %p\n",
  327. remote_dev, &remote_dev->dev_group.cg_item);
  328. target_undepend_item(&remote_dev->dev_group.cg_item);
  329. }
  330. static void xcopy_pt_release_cmd(struct se_cmd *se_cmd)
  331. {
  332. struct xcopy_pt_cmd *xpt_cmd = container_of(se_cmd,
  333. struct xcopy_pt_cmd, se_cmd);
  334. kfree(xpt_cmd);
  335. }
  336. static int xcopy_pt_check_stop_free(struct se_cmd *se_cmd)
  337. {
  338. struct xcopy_pt_cmd *xpt_cmd = container_of(se_cmd,
  339. struct xcopy_pt_cmd, se_cmd);
  340. complete(&xpt_cmd->xpt_passthrough_sem);
  341. return 0;
  342. }
  343. static int xcopy_pt_write_pending(struct se_cmd *se_cmd)
  344. {
  345. return 0;
  346. }
  347. static int xcopy_pt_write_pending_status(struct se_cmd *se_cmd)
  348. {
  349. return 0;
  350. }
  351. static int xcopy_pt_queue_data_in(struct se_cmd *se_cmd)
  352. {
  353. return 0;
  354. }
  355. static int xcopy_pt_queue_status(struct se_cmd *se_cmd)
  356. {
  357. return 0;
  358. }
  359. static const struct target_core_fabric_ops xcopy_pt_tfo = {
  360. .get_fabric_name = xcopy_pt_get_fabric_name,
  361. .get_cmd_state = xcopy_pt_get_cmd_state,
  362. .release_cmd = xcopy_pt_release_cmd,
  363. .check_stop_free = xcopy_pt_check_stop_free,
  364. .write_pending = xcopy_pt_write_pending,
  365. .write_pending_status = xcopy_pt_write_pending_status,
  366. .queue_data_in = xcopy_pt_queue_data_in,
  367. .queue_status = xcopy_pt_queue_status,
  368. };
  369. /*
  370. * End xcopy_pt_ops
  371. */
  372. int target_xcopy_setup_pt(void)
  373. {
  374. xcopy_wq = alloc_workqueue("xcopy_wq", WQ_MEM_RECLAIM, 0);
  375. if (!xcopy_wq) {
  376. pr_err("Unable to allocate xcopy_wq\n");
  377. return -ENOMEM;
  378. }
  379. memset(&xcopy_pt_tpg, 0, sizeof(struct se_portal_group));
  380. INIT_LIST_HEAD(&xcopy_pt_tpg.se_tpg_node);
  381. INIT_LIST_HEAD(&xcopy_pt_tpg.acl_node_list);
  382. INIT_LIST_HEAD(&xcopy_pt_tpg.tpg_sess_list);
  383. xcopy_pt_tpg.se_tpg_tfo = &xcopy_pt_tfo;
  384. memset(&xcopy_pt_nacl, 0, sizeof(struct se_node_acl));
  385. INIT_LIST_HEAD(&xcopy_pt_nacl.acl_list);
  386. INIT_LIST_HEAD(&xcopy_pt_nacl.acl_sess_list);
  387. memset(&xcopy_pt_sess, 0, sizeof(struct se_session));
  388. INIT_LIST_HEAD(&xcopy_pt_sess.sess_list);
  389. INIT_LIST_HEAD(&xcopy_pt_sess.sess_acl_list);
  390. xcopy_pt_nacl.se_tpg = &xcopy_pt_tpg;
  391. xcopy_pt_nacl.nacl_sess = &xcopy_pt_sess;
  392. xcopy_pt_sess.se_tpg = &xcopy_pt_tpg;
  393. xcopy_pt_sess.se_node_acl = &xcopy_pt_nacl;
  394. return 0;
  395. }
  396. void target_xcopy_release_pt(void)
  397. {
  398. if (xcopy_wq)
  399. destroy_workqueue(xcopy_wq);
  400. }
  401. static void target_xcopy_setup_pt_port(
  402. struct xcopy_pt_cmd *xpt_cmd,
  403. struct xcopy_op *xop,
  404. bool remote_port)
  405. {
  406. struct se_cmd *ec_cmd = xop->xop_se_cmd;
  407. struct se_cmd *pt_cmd = &xpt_cmd->se_cmd;
  408. if (xop->op_origin == XCOL_SOURCE_RECV_OP) {
  409. /*
  410. * Honor destination port reservations for X-COPY PUSH emulation
  411. * when CDB is received on local source port, and READs blocks to
  412. * WRITE on remote destination port.
  413. */
  414. if (remote_port) {
  415. xpt_cmd->remote_port = remote_port;
  416. } else {
  417. pt_cmd->se_lun = ec_cmd->se_lun;
  418. pt_cmd->se_dev = ec_cmd->se_dev;
  419. pr_debug("Honoring local SRC port from ec_cmd->se_dev:"
  420. " %p\n", pt_cmd->se_dev);
  421. pt_cmd->se_lun = ec_cmd->se_lun;
  422. pr_debug("Honoring local SRC port from ec_cmd->se_lun: %p\n",
  423. pt_cmd->se_lun);
  424. }
  425. } else {
  426. /*
  427. * Honor source port reservation for X-COPY PULL emulation
  428. * when CDB is received on local desintation port, and READs
  429. * blocks from the remote source port to WRITE on local
  430. * destination port.
  431. */
  432. if (remote_port) {
  433. xpt_cmd->remote_port = remote_port;
  434. } else {
  435. pt_cmd->se_lun = ec_cmd->se_lun;
  436. pt_cmd->se_dev = ec_cmd->se_dev;
  437. pr_debug("Honoring local DST port from ec_cmd->se_dev:"
  438. " %p\n", pt_cmd->se_dev);
  439. pt_cmd->se_lun = ec_cmd->se_lun;
  440. pr_debug("Honoring local DST port from ec_cmd->se_lun: %p\n",
  441. pt_cmd->se_lun);
  442. }
  443. }
  444. }
  445. static void target_xcopy_init_pt_lun(struct se_device *se_dev,
  446. struct se_cmd *pt_cmd, bool remote_port)
  447. {
  448. /*
  449. * Don't allocate + init an pt_cmd->se_lun if honoring local port for
  450. * reservations. The pt_cmd->se_lun pointer will be setup from within
  451. * target_xcopy_setup_pt_port()
  452. */
  453. if (remote_port) {
  454. pr_debug("Setup emulated se_dev: %p from se_dev\n",
  455. pt_cmd->se_dev);
  456. pt_cmd->se_lun = &se_dev->xcopy_lun;
  457. pt_cmd->se_dev = se_dev;
  458. }
  459. pt_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  460. }
  461. static int target_xcopy_setup_pt_cmd(
  462. struct xcopy_pt_cmd *xpt_cmd,
  463. struct xcopy_op *xop,
  464. struct se_device *se_dev,
  465. unsigned char *cdb,
  466. bool remote_port,
  467. bool alloc_mem)
  468. {
  469. struct se_cmd *cmd = &xpt_cmd->se_cmd;
  470. sense_reason_t sense_rc;
  471. int ret = 0, rc;
  472. /*
  473. * Setup LUN+port to honor reservations based upon xop->op_origin for
  474. * X-COPY PUSH or X-COPY PULL based upon where the CDB was received.
  475. */
  476. target_xcopy_init_pt_lun(se_dev, cmd, remote_port);
  477. xpt_cmd->xcopy_op = xop;
  478. target_xcopy_setup_pt_port(xpt_cmd, xop, remote_port);
  479. cmd->tag = 0;
  480. sense_rc = target_setup_cmd_from_cdb(cmd, cdb);
  481. if (sense_rc) {
  482. ret = -EINVAL;
  483. goto out;
  484. }
  485. if (alloc_mem) {
  486. rc = target_alloc_sgl(&cmd->t_data_sg, &cmd->t_data_nents,
  487. cmd->data_length, false);
  488. if (rc < 0) {
  489. ret = rc;
  490. goto out;
  491. }
  492. /*
  493. * Set this bit so that transport_free_pages() allows the
  494. * caller to release SGLs + physical memory allocated by
  495. * transport_generic_get_mem()..
  496. */
  497. cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
  498. } else {
  499. /*
  500. * Here the previously allocated SGLs for the internal READ
  501. * are mapped zero-copy to the internal WRITE.
  502. */
  503. sense_rc = transport_generic_map_mem_to_cmd(cmd,
  504. xop->xop_data_sg, xop->xop_data_nents,
  505. NULL, 0);
  506. if (sense_rc) {
  507. ret = -EINVAL;
  508. goto out;
  509. }
  510. pr_debug("Setup PASSTHROUGH_NOALLOC t_data_sg: %p t_data_nents:"
  511. " %u\n", cmd->t_data_sg, cmd->t_data_nents);
  512. }
  513. return 0;
  514. out:
  515. return ret;
  516. }
  517. static int target_xcopy_issue_pt_cmd(struct xcopy_pt_cmd *xpt_cmd)
  518. {
  519. struct se_cmd *se_cmd = &xpt_cmd->se_cmd;
  520. sense_reason_t sense_rc;
  521. sense_rc = transport_generic_new_cmd(se_cmd);
  522. if (sense_rc)
  523. return -EINVAL;
  524. if (se_cmd->data_direction == DMA_TO_DEVICE)
  525. target_execute_cmd(se_cmd);
  526. wait_for_completion_interruptible(&xpt_cmd->xpt_passthrough_sem);
  527. pr_debug("target_xcopy_issue_pt_cmd(): SCSI status: 0x%02x\n",
  528. se_cmd->scsi_status);
  529. return (se_cmd->scsi_status) ? -EINVAL : 0;
  530. }
  531. static int target_xcopy_read_source(
  532. struct se_cmd *ec_cmd,
  533. struct xcopy_op *xop,
  534. struct se_device *src_dev,
  535. sector_t src_lba,
  536. u32 src_sectors)
  537. {
  538. struct xcopy_pt_cmd *xpt_cmd;
  539. struct se_cmd *se_cmd;
  540. u32 length = (src_sectors * src_dev->dev_attrib.block_size);
  541. int rc;
  542. unsigned char cdb[16];
  543. bool remote_port = (xop->op_origin == XCOL_DEST_RECV_OP);
  544. xpt_cmd = kzalloc(sizeof(struct xcopy_pt_cmd), GFP_KERNEL);
  545. if (!xpt_cmd) {
  546. pr_err("Unable to allocate xcopy_pt_cmd\n");
  547. return -ENOMEM;
  548. }
  549. init_completion(&xpt_cmd->xpt_passthrough_sem);
  550. se_cmd = &xpt_cmd->se_cmd;
  551. memset(&cdb[0], 0, 16);
  552. cdb[0] = READ_16;
  553. put_unaligned_be64(src_lba, &cdb[2]);
  554. put_unaligned_be32(src_sectors, &cdb[10]);
  555. pr_debug("XCOPY: Built READ_16: LBA: %llu Sectors: %u Length: %u\n",
  556. (unsigned long long)src_lba, src_sectors, length);
  557. transport_init_se_cmd(se_cmd, &xcopy_pt_tfo, NULL, length,
  558. DMA_FROM_DEVICE, 0, &xpt_cmd->sense_buffer[0]);
  559. xop->src_pt_cmd = xpt_cmd;
  560. rc = target_xcopy_setup_pt_cmd(xpt_cmd, xop, src_dev, &cdb[0],
  561. remote_port, true);
  562. if (rc < 0) {
  563. transport_generic_free_cmd(se_cmd, 0);
  564. return rc;
  565. }
  566. xop->xop_data_sg = se_cmd->t_data_sg;
  567. xop->xop_data_nents = se_cmd->t_data_nents;
  568. pr_debug("XCOPY-READ: Saved xop->xop_data_sg: %p, num: %u for READ"
  569. " memory\n", xop->xop_data_sg, xop->xop_data_nents);
  570. rc = target_xcopy_issue_pt_cmd(xpt_cmd);
  571. if (rc < 0) {
  572. transport_generic_free_cmd(se_cmd, 0);
  573. return rc;
  574. }
  575. /*
  576. * Clear off the allocated t_data_sg, that has been saved for
  577. * zero-copy WRITE submission reuse in struct xcopy_op..
  578. */
  579. se_cmd->t_data_sg = NULL;
  580. se_cmd->t_data_nents = 0;
  581. return 0;
  582. }
  583. static int target_xcopy_write_destination(
  584. struct se_cmd *ec_cmd,
  585. struct xcopy_op *xop,
  586. struct se_device *dst_dev,
  587. sector_t dst_lba,
  588. u32 dst_sectors)
  589. {
  590. struct xcopy_pt_cmd *xpt_cmd;
  591. struct se_cmd *se_cmd;
  592. u32 length = (dst_sectors * dst_dev->dev_attrib.block_size);
  593. int rc;
  594. unsigned char cdb[16];
  595. bool remote_port = (xop->op_origin == XCOL_SOURCE_RECV_OP);
  596. xpt_cmd = kzalloc(sizeof(struct xcopy_pt_cmd), GFP_KERNEL);
  597. if (!xpt_cmd) {
  598. pr_err("Unable to allocate xcopy_pt_cmd\n");
  599. return -ENOMEM;
  600. }
  601. init_completion(&xpt_cmd->xpt_passthrough_sem);
  602. se_cmd = &xpt_cmd->se_cmd;
  603. memset(&cdb[0], 0, 16);
  604. cdb[0] = WRITE_16;
  605. put_unaligned_be64(dst_lba, &cdb[2]);
  606. put_unaligned_be32(dst_sectors, &cdb[10]);
  607. pr_debug("XCOPY: Built WRITE_16: LBA: %llu Sectors: %u Length: %u\n",
  608. (unsigned long long)dst_lba, dst_sectors, length);
  609. transport_init_se_cmd(se_cmd, &xcopy_pt_tfo, NULL, length,
  610. DMA_TO_DEVICE, 0, &xpt_cmd->sense_buffer[0]);
  611. xop->dst_pt_cmd = xpt_cmd;
  612. rc = target_xcopy_setup_pt_cmd(xpt_cmd, xop, dst_dev, &cdb[0],
  613. remote_port, false);
  614. if (rc < 0) {
  615. struct se_cmd *src_cmd = &xop->src_pt_cmd->se_cmd;
  616. /*
  617. * If the failure happened before the t_mem_list hand-off in
  618. * target_xcopy_setup_pt_cmd(), Reset memory + clear flag so that
  619. * core releases this memory on error during X-COPY WRITE I/O.
  620. */
  621. src_cmd->se_cmd_flags &= ~SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
  622. src_cmd->t_data_sg = xop->xop_data_sg;
  623. src_cmd->t_data_nents = xop->xop_data_nents;
  624. transport_generic_free_cmd(se_cmd, 0);
  625. return rc;
  626. }
  627. rc = target_xcopy_issue_pt_cmd(xpt_cmd);
  628. if (rc < 0) {
  629. se_cmd->se_cmd_flags &= ~SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
  630. transport_generic_free_cmd(se_cmd, 0);
  631. return rc;
  632. }
  633. return 0;
  634. }
  635. static void target_xcopy_do_work(struct work_struct *work)
  636. {
  637. struct xcopy_op *xop = container_of(work, struct xcopy_op, xop_work);
  638. struct se_device *src_dev = xop->src_dev, *dst_dev = xop->dst_dev;
  639. struct se_cmd *ec_cmd = xop->xop_se_cmd;
  640. sector_t src_lba = xop->src_lba, dst_lba = xop->dst_lba, end_lba;
  641. unsigned int max_sectors;
  642. int rc;
  643. unsigned short nolb = xop->nolb, cur_nolb, max_nolb, copied_nolb = 0;
  644. end_lba = src_lba + nolb;
  645. /*
  646. * Break up XCOPY I/O into hw_max_sectors sized I/O based on the
  647. * smallest max_sectors between src_dev + dev_dev, or
  648. */
  649. max_sectors = min(src_dev->dev_attrib.hw_max_sectors,
  650. dst_dev->dev_attrib.hw_max_sectors);
  651. max_sectors = min_t(u32, max_sectors, XCOPY_MAX_SECTORS);
  652. max_nolb = min_t(u16, max_sectors, ((u16)(~0U)));
  653. pr_debug("target_xcopy_do_work: nolb: %hu, max_nolb: %hu end_lba: %llu\n",
  654. nolb, max_nolb, (unsigned long long)end_lba);
  655. pr_debug("target_xcopy_do_work: Starting src_lba: %llu, dst_lba: %llu\n",
  656. (unsigned long long)src_lba, (unsigned long long)dst_lba);
  657. while (src_lba < end_lba) {
  658. cur_nolb = min(nolb, max_nolb);
  659. pr_debug("target_xcopy_do_work: Calling read src_dev: %p src_lba: %llu,"
  660. " cur_nolb: %hu\n", src_dev, (unsigned long long)src_lba, cur_nolb);
  661. rc = target_xcopy_read_source(ec_cmd, xop, src_dev, src_lba, cur_nolb);
  662. if (rc < 0)
  663. goto out;
  664. src_lba += cur_nolb;
  665. pr_debug("target_xcopy_do_work: Incremented READ src_lba to %llu\n",
  666. (unsigned long long)src_lba);
  667. pr_debug("target_xcopy_do_work: Calling write dst_dev: %p dst_lba: %llu,"
  668. " cur_nolb: %hu\n", dst_dev, (unsigned long long)dst_lba, cur_nolb);
  669. rc = target_xcopy_write_destination(ec_cmd, xop, dst_dev,
  670. dst_lba, cur_nolb);
  671. if (rc < 0) {
  672. transport_generic_free_cmd(&xop->src_pt_cmd->se_cmd, 0);
  673. goto out;
  674. }
  675. dst_lba += cur_nolb;
  676. pr_debug("target_xcopy_do_work: Incremented WRITE dst_lba to %llu\n",
  677. (unsigned long long)dst_lba);
  678. copied_nolb += cur_nolb;
  679. nolb -= cur_nolb;
  680. transport_generic_free_cmd(&xop->src_pt_cmd->se_cmd, 0);
  681. xop->dst_pt_cmd->se_cmd.se_cmd_flags &= ~SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
  682. transport_generic_free_cmd(&xop->dst_pt_cmd->se_cmd, 0);
  683. }
  684. xcopy_pt_undepend_remotedev(xop);
  685. kfree(xop);
  686. pr_debug("target_xcopy_do_work: Final src_lba: %llu, dst_lba: %llu\n",
  687. (unsigned long long)src_lba, (unsigned long long)dst_lba);
  688. pr_debug("target_xcopy_do_work: Blocks copied: %hu, Bytes Copied: %u\n",
  689. copied_nolb, copied_nolb * dst_dev->dev_attrib.block_size);
  690. pr_debug("target_xcopy_do_work: Setting X-COPY GOOD status -> sending response\n");
  691. target_complete_cmd(ec_cmd, SAM_STAT_GOOD);
  692. return;
  693. out:
  694. xcopy_pt_undepend_remotedev(xop);
  695. kfree(xop);
  696. pr_warn("target_xcopy_do_work: Setting X-COPY CHECK_CONDITION -> sending response\n");
  697. ec_cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
  698. target_complete_cmd(ec_cmd, SAM_STAT_CHECK_CONDITION);
  699. }
  700. sense_reason_t target_do_xcopy(struct se_cmd *se_cmd)
  701. {
  702. struct se_device *dev = se_cmd->se_dev;
  703. struct xcopy_op *xop = NULL;
  704. unsigned char *p = NULL, *seg_desc;
  705. unsigned int list_id, list_id_usage, sdll, inline_dl, sa;
  706. sense_reason_t ret = TCM_INVALID_PARAMETER_LIST;
  707. int rc;
  708. unsigned short tdll;
  709. if (!dev->dev_attrib.emulate_3pc) {
  710. pr_err("EXTENDED_COPY operation explicitly disabled\n");
  711. return TCM_UNSUPPORTED_SCSI_OPCODE;
  712. }
  713. sa = se_cmd->t_task_cdb[1] & 0x1f;
  714. if (sa != 0x00) {
  715. pr_err("EXTENDED_COPY(LID4) not supported\n");
  716. return TCM_UNSUPPORTED_SCSI_OPCODE;
  717. }
  718. xop = kzalloc(sizeof(struct xcopy_op), GFP_KERNEL);
  719. if (!xop) {
  720. pr_err("Unable to allocate xcopy_op\n");
  721. return TCM_OUT_OF_RESOURCES;
  722. }
  723. xop->xop_se_cmd = se_cmd;
  724. p = transport_kmap_data_sg(se_cmd);
  725. if (!p) {
  726. pr_err("transport_kmap_data_sg() failed in target_do_xcopy\n");
  727. kfree(xop);
  728. return TCM_OUT_OF_RESOURCES;
  729. }
  730. list_id = p[0];
  731. list_id_usage = (p[1] & 0x18) >> 3;
  732. /*
  733. * Determine TARGET DESCRIPTOR LIST LENGTH + SEGMENT DESCRIPTOR LIST LENGTH
  734. */
  735. tdll = get_unaligned_be16(&p[2]);
  736. sdll = get_unaligned_be32(&p[8]);
  737. inline_dl = get_unaligned_be32(&p[12]);
  738. if (inline_dl != 0) {
  739. pr_err("XCOPY with non zero inline data length\n");
  740. goto out;
  741. }
  742. pr_debug("Processing XCOPY with list_id: 0x%02x list_id_usage: 0x%02x"
  743. " tdll: %hu sdll: %u inline_dl: %u\n", list_id, list_id_usage,
  744. tdll, sdll, inline_dl);
  745. rc = target_xcopy_parse_target_descriptors(se_cmd, xop, &p[16], tdll);
  746. if (rc <= 0)
  747. goto out;
  748. if (xop->src_dev->dev_attrib.block_size !=
  749. xop->dst_dev->dev_attrib.block_size) {
  750. pr_err("XCOPY: Non matching src_dev block_size: %u + dst_dev"
  751. " block_size: %u currently unsupported\n",
  752. xop->src_dev->dev_attrib.block_size,
  753. xop->dst_dev->dev_attrib.block_size);
  754. xcopy_pt_undepend_remotedev(xop);
  755. ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  756. goto out;
  757. }
  758. pr_debug("XCOPY: Processed %d target descriptors, length: %u\n", rc,
  759. rc * XCOPY_TARGET_DESC_LEN);
  760. seg_desc = &p[16];
  761. seg_desc += (rc * XCOPY_TARGET_DESC_LEN);
  762. rc = target_xcopy_parse_segment_descriptors(se_cmd, xop, seg_desc, sdll);
  763. if (rc <= 0) {
  764. xcopy_pt_undepend_remotedev(xop);
  765. goto out;
  766. }
  767. transport_kunmap_data_sg(se_cmd);
  768. pr_debug("XCOPY: Processed %d segment descriptors, length: %u\n", rc,
  769. rc * XCOPY_SEGMENT_DESC_LEN);
  770. INIT_WORK(&xop->xop_work, target_xcopy_do_work);
  771. queue_work(xcopy_wq, &xop->xop_work);
  772. return TCM_NO_SENSE;
  773. out:
  774. if (p)
  775. transport_kunmap_data_sg(se_cmd);
  776. kfree(xop);
  777. return ret;
  778. }
  779. static sense_reason_t target_rcr_operating_parameters(struct se_cmd *se_cmd)
  780. {
  781. unsigned char *p;
  782. p = transport_kmap_data_sg(se_cmd);
  783. if (!p) {
  784. pr_err("transport_kmap_data_sg failed in"
  785. " target_rcr_operating_parameters\n");
  786. return TCM_OUT_OF_RESOURCES;
  787. }
  788. if (se_cmd->data_length < 54) {
  789. pr_err("Receive Copy Results Op Parameters length"
  790. " too small: %u\n", se_cmd->data_length);
  791. transport_kunmap_data_sg(se_cmd);
  792. return TCM_INVALID_CDB_FIELD;
  793. }
  794. /*
  795. * Set SNLID=1 (Supports no List ID)
  796. */
  797. p[4] = 0x1;
  798. /*
  799. * MAXIMUM TARGET DESCRIPTOR COUNT
  800. */
  801. put_unaligned_be16(RCR_OP_MAX_TARGET_DESC_COUNT, &p[8]);
  802. /*
  803. * MAXIMUM SEGMENT DESCRIPTOR COUNT
  804. */
  805. put_unaligned_be16(RCR_OP_MAX_SG_DESC_COUNT, &p[10]);
  806. /*
  807. * MAXIMUM DESCRIPTOR LIST LENGTH
  808. */
  809. put_unaligned_be32(RCR_OP_MAX_DESC_LIST_LEN, &p[12]);
  810. /*
  811. * MAXIMUM SEGMENT LENGTH
  812. */
  813. put_unaligned_be32(RCR_OP_MAX_SEGMENT_LEN, &p[16]);
  814. /*
  815. * MAXIMUM INLINE DATA LENGTH for SA 0x04 (NOT SUPPORTED)
  816. */
  817. put_unaligned_be32(0x0, &p[20]);
  818. /*
  819. * HELD DATA LIMIT
  820. */
  821. put_unaligned_be32(0x0, &p[24]);
  822. /*
  823. * MAXIMUM STREAM DEVICE TRANSFER SIZE
  824. */
  825. put_unaligned_be32(0x0, &p[28]);
  826. /*
  827. * TOTAL CONCURRENT COPIES
  828. */
  829. put_unaligned_be16(RCR_OP_TOTAL_CONCURR_COPIES, &p[34]);
  830. /*
  831. * MAXIMUM CONCURRENT COPIES
  832. */
  833. p[36] = RCR_OP_MAX_CONCURR_COPIES;
  834. /*
  835. * DATA SEGMENT GRANULARITY (log 2)
  836. */
  837. p[37] = RCR_OP_DATA_SEG_GRAN_LOG2;
  838. /*
  839. * INLINE DATA GRANULARITY log 2)
  840. */
  841. p[38] = RCR_OP_INLINE_DATA_GRAN_LOG2;
  842. /*
  843. * HELD DATA GRANULARITY
  844. */
  845. p[39] = RCR_OP_HELD_DATA_GRAN_LOG2;
  846. /*
  847. * IMPLEMENTED DESCRIPTOR LIST LENGTH
  848. */
  849. p[43] = 0x2;
  850. /*
  851. * List of implemented descriptor type codes (ordered)
  852. */
  853. p[44] = 0x02; /* Copy Block to Block device */
  854. p[45] = 0xe4; /* Identification descriptor target descriptor */
  855. /*
  856. * AVAILABLE DATA (n-3)
  857. */
  858. put_unaligned_be32(42, &p[0]);
  859. transport_kunmap_data_sg(se_cmd);
  860. target_complete_cmd(se_cmd, GOOD);
  861. return TCM_NO_SENSE;
  862. }
  863. sense_reason_t target_do_receive_copy_results(struct se_cmd *se_cmd)
  864. {
  865. unsigned char *cdb = &se_cmd->t_task_cdb[0];
  866. int sa = (cdb[1] & 0x1f), list_id = cdb[2];
  867. sense_reason_t rc = TCM_NO_SENSE;
  868. pr_debug("Entering target_do_receive_copy_results: SA: 0x%02x, List ID:"
  869. " 0x%02x, AL: %u\n", sa, list_id, se_cmd->data_length);
  870. if (list_id != 0) {
  871. pr_err("Receive Copy Results with non zero list identifier"
  872. " not supported\n");
  873. return TCM_INVALID_CDB_FIELD;
  874. }
  875. switch (sa) {
  876. case RCR_SA_OPERATING_PARAMETERS:
  877. rc = target_rcr_operating_parameters(se_cmd);
  878. break;
  879. case RCR_SA_COPY_STATUS:
  880. case RCR_SA_RECEIVE_DATA:
  881. case RCR_SA_FAILED_SEGMENT_DETAILS:
  882. default:
  883. pr_err("Unsupported SA for receive copy results: 0x%02x\n", sa);
  884. return TCM_INVALID_CDB_FIELD;
  885. }
  886. return rc;
  887. }