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