target_core_xcopy.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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. INIT_LIST_HEAD(&xcopy_pt_sess.sess_cmd_list);
  390. spin_lock_init(&xcopy_pt_sess.sess_cmd_lock);
  391. xcopy_pt_nacl.se_tpg = &xcopy_pt_tpg;
  392. xcopy_pt_nacl.nacl_sess = &xcopy_pt_sess;
  393. xcopy_pt_sess.se_tpg = &xcopy_pt_tpg;
  394. xcopy_pt_sess.se_node_acl = &xcopy_pt_nacl;
  395. return 0;
  396. }
  397. void target_xcopy_release_pt(void)
  398. {
  399. if (xcopy_wq)
  400. destroy_workqueue(xcopy_wq);
  401. }
  402. static void target_xcopy_setup_pt_port(
  403. struct xcopy_pt_cmd *xpt_cmd,
  404. struct xcopy_op *xop,
  405. bool remote_port)
  406. {
  407. struct se_cmd *ec_cmd = xop->xop_se_cmd;
  408. struct se_cmd *pt_cmd = &xpt_cmd->se_cmd;
  409. if (xop->op_origin == XCOL_SOURCE_RECV_OP) {
  410. /*
  411. * Honor destination port reservations for X-COPY PUSH emulation
  412. * when CDB is received on local source port, and READs blocks to
  413. * WRITE on remote destination port.
  414. */
  415. if (remote_port) {
  416. xpt_cmd->remote_port = remote_port;
  417. } else {
  418. pt_cmd->se_lun = ec_cmd->se_lun;
  419. pt_cmd->se_dev = ec_cmd->se_dev;
  420. pr_debug("Honoring local SRC port from ec_cmd->se_dev:"
  421. " %p\n", pt_cmd->se_dev);
  422. pt_cmd->se_lun = ec_cmd->se_lun;
  423. pr_debug("Honoring local SRC port from ec_cmd->se_lun: %p\n",
  424. pt_cmd->se_lun);
  425. }
  426. } else {
  427. /*
  428. * Honor source port reservation for X-COPY PULL emulation
  429. * when CDB is received on local desintation port, and READs
  430. * blocks from the remote source port to WRITE on local
  431. * destination port.
  432. */
  433. if (remote_port) {
  434. xpt_cmd->remote_port = remote_port;
  435. } else {
  436. pt_cmd->se_lun = ec_cmd->se_lun;
  437. pt_cmd->se_dev = ec_cmd->se_dev;
  438. pr_debug("Honoring local DST port from ec_cmd->se_dev:"
  439. " %p\n", pt_cmd->se_dev);
  440. pt_cmd->se_lun = ec_cmd->se_lun;
  441. pr_debug("Honoring local DST port from ec_cmd->se_lun: %p\n",
  442. pt_cmd->se_lun);
  443. }
  444. }
  445. }
  446. static void target_xcopy_init_pt_lun(struct se_device *se_dev,
  447. struct se_cmd *pt_cmd, bool remote_port)
  448. {
  449. /*
  450. * Don't allocate + init an pt_cmd->se_lun if honoring local port for
  451. * reservations. The pt_cmd->se_lun pointer will be setup from within
  452. * target_xcopy_setup_pt_port()
  453. */
  454. if (remote_port) {
  455. pr_debug("Setup emulated se_dev: %p from se_dev\n",
  456. pt_cmd->se_dev);
  457. pt_cmd->se_lun = &se_dev->xcopy_lun;
  458. pt_cmd->se_dev = se_dev;
  459. }
  460. pt_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  461. }
  462. static int target_xcopy_setup_pt_cmd(
  463. struct xcopy_pt_cmd *xpt_cmd,
  464. struct xcopy_op *xop,
  465. struct se_device *se_dev,
  466. unsigned char *cdb,
  467. bool remote_port,
  468. bool alloc_mem)
  469. {
  470. struct se_cmd *cmd = &xpt_cmd->se_cmd;
  471. sense_reason_t sense_rc;
  472. int ret = 0, rc;
  473. /*
  474. * Setup LUN+port to honor reservations based upon xop->op_origin for
  475. * X-COPY PUSH or X-COPY PULL based upon where the CDB was received.
  476. */
  477. target_xcopy_init_pt_lun(se_dev, cmd, remote_port);
  478. xpt_cmd->xcopy_op = xop;
  479. target_xcopy_setup_pt_port(xpt_cmd, xop, remote_port);
  480. cmd->tag = 0;
  481. sense_rc = target_setup_cmd_from_cdb(cmd, cdb);
  482. if (sense_rc) {
  483. ret = -EINVAL;
  484. goto out;
  485. }
  486. if (alloc_mem) {
  487. rc = target_alloc_sgl(&cmd->t_data_sg, &cmd->t_data_nents,
  488. cmd->data_length, false);
  489. if (rc < 0) {
  490. ret = rc;
  491. goto out;
  492. }
  493. /*
  494. * Set this bit so that transport_free_pages() allows the
  495. * caller to release SGLs + physical memory allocated by
  496. * transport_generic_get_mem()..
  497. */
  498. cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
  499. } else {
  500. /*
  501. * Here the previously allocated SGLs for the internal READ
  502. * are mapped zero-copy to the internal WRITE.
  503. */
  504. sense_rc = transport_generic_map_mem_to_cmd(cmd,
  505. xop->xop_data_sg, xop->xop_data_nents,
  506. NULL, 0);
  507. if (sense_rc) {
  508. ret = -EINVAL;
  509. goto out;
  510. }
  511. pr_debug("Setup PASSTHROUGH_NOALLOC t_data_sg: %p t_data_nents:"
  512. " %u\n", cmd->t_data_sg, cmd->t_data_nents);
  513. }
  514. return 0;
  515. out:
  516. return ret;
  517. }
  518. static int target_xcopy_issue_pt_cmd(struct xcopy_pt_cmd *xpt_cmd)
  519. {
  520. struct se_cmd *se_cmd = &xpt_cmd->se_cmd;
  521. sense_reason_t sense_rc;
  522. sense_rc = transport_generic_new_cmd(se_cmd);
  523. if (sense_rc)
  524. return -EINVAL;
  525. if (se_cmd->data_direction == DMA_TO_DEVICE)
  526. target_execute_cmd(se_cmd);
  527. wait_for_completion_interruptible(&xpt_cmd->xpt_passthrough_sem);
  528. pr_debug("target_xcopy_issue_pt_cmd(): SCSI status: 0x%02x\n",
  529. se_cmd->scsi_status);
  530. return (se_cmd->scsi_status) ? -EINVAL : 0;
  531. }
  532. static int target_xcopy_read_source(
  533. struct se_cmd *ec_cmd,
  534. struct xcopy_op *xop,
  535. struct se_device *src_dev,
  536. sector_t src_lba,
  537. u32 src_sectors)
  538. {
  539. struct xcopy_pt_cmd *xpt_cmd;
  540. struct se_cmd *se_cmd;
  541. u32 length = (src_sectors * src_dev->dev_attrib.block_size);
  542. int rc;
  543. unsigned char cdb[16];
  544. bool remote_port = (xop->op_origin == XCOL_DEST_RECV_OP);
  545. xpt_cmd = kzalloc(sizeof(struct xcopy_pt_cmd), GFP_KERNEL);
  546. if (!xpt_cmd) {
  547. pr_err("Unable to allocate xcopy_pt_cmd\n");
  548. return -ENOMEM;
  549. }
  550. init_completion(&xpt_cmd->xpt_passthrough_sem);
  551. se_cmd = &xpt_cmd->se_cmd;
  552. memset(&cdb[0], 0, 16);
  553. cdb[0] = READ_16;
  554. put_unaligned_be64(src_lba, &cdb[2]);
  555. put_unaligned_be32(src_sectors, &cdb[10]);
  556. pr_debug("XCOPY: Built READ_16: LBA: %llu Sectors: %u Length: %u\n",
  557. (unsigned long long)src_lba, src_sectors, length);
  558. transport_init_se_cmd(se_cmd, &xcopy_pt_tfo, &xcopy_pt_sess, length,
  559. DMA_FROM_DEVICE, 0, &xpt_cmd->sense_buffer[0]);
  560. xop->src_pt_cmd = xpt_cmd;
  561. rc = target_xcopy_setup_pt_cmd(xpt_cmd, xop, src_dev, &cdb[0],
  562. remote_port, true);
  563. if (rc < 0) {
  564. transport_generic_free_cmd(se_cmd, 0);
  565. return rc;
  566. }
  567. xop->xop_data_sg = se_cmd->t_data_sg;
  568. xop->xop_data_nents = se_cmd->t_data_nents;
  569. pr_debug("XCOPY-READ: Saved xop->xop_data_sg: %p, num: %u for READ"
  570. " memory\n", xop->xop_data_sg, xop->xop_data_nents);
  571. rc = target_xcopy_issue_pt_cmd(xpt_cmd);
  572. if (rc < 0) {
  573. transport_generic_free_cmd(se_cmd, 0);
  574. return rc;
  575. }
  576. /*
  577. * Clear off the allocated t_data_sg, that has been saved for
  578. * zero-copy WRITE submission reuse in struct xcopy_op..
  579. */
  580. se_cmd->t_data_sg = NULL;
  581. se_cmd->t_data_nents = 0;
  582. return 0;
  583. }
  584. static int target_xcopy_write_destination(
  585. struct se_cmd *ec_cmd,
  586. struct xcopy_op *xop,
  587. struct se_device *dst_dev,
  588. sector_t dst_lba,
  589. u32 dst_sectors)
  590. {
  591. struct xcopy_pt_cmd *xpt_cmd;
  592. struct se_cmd *se_cmd;
  593. u32 length = (dst_sectors * dst_dev->dev_attrib.block_size);
  594. int rc;
  595. unsigned char cdb[16];
  596. bool remote_port = (xop->op_origin == XCOL_SOURCE_RECV_OP);
  597. xpt_cmd = kzalloc(sizeof(struct xcopy_pt_cmd), GFP_KERNEL);
  598. if (!xpt_cmd) {
  599. pr_err("Unable to allocate xcopy_pt_cmd\n");
  600. return -ENOMEM;
  601. }
  602. init_completion(&xpt_cmd->xpt_passthrough_sem);
  603. se_cmd = &xpt_cmd->se_cmd;
  604. memset(&cdb[0], 0, 16);
  605. cdb[0] = WRITE_16;
  606. put_unaligned_be64(dst_lba, &cdb[2]);
  607. put_unaligned_be32(dst_sectors, &cdb[10]);
  608. pr_debug("XCOPY: Built WRITE_16: LBA: %llu Sectors: %u Length: %u\n",
  609. (unsigned long long)dst_lba, dst_sectors, length);
  610. transport_init_se_cmd(se_cmd, &xcopy_pt_tfo, &xcopy_pt_sess, length,
  611. DMA_TO_DEVICE, 0, &xpt_cmd->sense_buffer[0]);
  612. xop->dst_pt_cmd = xpt_cmd;
  613. rc = target_xcopy_setup_pt_cmd(xpt_cmd, xop, dst_dev, &cdb[0],
  614. remote_port, false);
  615. if (rc < 0) {
  616. struct se_cmd *src_cmd = &xop->src_pt_cmd->se_cmd;
  617. /*
  618. * If the failure happened before the t_mem_list hand-off in
  619. * target_xcopy_setup_pt_cmd(), Reset memory + clear flag so that
  620. * core releases this memory on error during X-COPY WRITE I/O.
  621. */
  622. src_cmd->se_cmd_flags &= ~SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
  623. src_cmd->t_data_sg = xop->xop_data_sg;
  624. src_cmd->t_data_nents = xop->xop_data_nents;
  625. transport_generic_free_cmd(se_cmd, 0);
  626. return rc;
  627. }
  628. rc = target_xcopy_issue_pt_cmd(xpt_cmd);
  629. if (rc < 0) {
  630. se_cmd->se_cmd_flags &= ~SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
  631. transport_generic_free_cmd(se_cmd, 0);
  632. return rc;
  633. }
  634. return 0;
  635. }
  636. static void target_xcopy_do_work(struct work_struct *work)
  637. {
  638. struct xcopy_op *xop = container_of(work, struct xcopy_op, xop_work);
  639. struct se_device *src_dev = xop->src_dev, *dst_dev = xop->dst_dev;
  640. struct se_cmd *ec_cmd = xop->xop_se_cmd;
  641. sector_t src_lba = xop->src_lba, dst_lba = xop->dst_lba, end_lba;
  642. unsigned int max_sectors;
  643. int rc;
  644. unsigned short nolb = xop->nolb, cur_nolb, max_nolb, copied_nolb = 0;
  645. end_lba = src_lba + nolb;
  646. /*
  647. * Break up XCOPY I/O into hw_max_sectors sized I/O based on the
  648. * smallest max_sectors between src_dev + dev_dev, or
  649. */
  650. max_sectors = min(src_dev->dev_attrib.hw_max_sectors,
  651. dst_dev->dev_attrib.hw_max_sectors);
  652. max_sectors = min_t(u32, max_sectors, XCOPY_MAX_SECTORS);
  653. max_nolb = min_t(u16, max_sectors, ((u16)(~0U)));
  654. pr_debug("target_xcopy_do_work: nolb: %hu, max_nolb: %hu end_lba: %llu\n",
  655. nolb, max_nolb, (unsigned long long)end_lba);
  656. pr_debug("target_xcopy_do_work: Starting src_lba: %llu, dst_lba: %llu\n",
  657. (unsigned long long)src_lba, (unsigned long long)dst_lba);
  658. while (src_lba < end_lba) {
  659. cur_nolb = min(nolb, max_nolb);
  660. pr_debug("target_xcopy_do_work: Calling read src_dev: %p src_lba: %llu,"
  661. " cur_nolb: %hu\n", src_dev, (unsigned long long)src_lba, cur_nolb);
  662. rc = target_xcopy_read_source(ec_cmd, xop, src_dev, src_lba, cur_nolb);
  663. if (rc < 0)
  664. goto out;
  665. src_lba += cur_nolb;
  666. pr_debug("target_xcopy_do_work: Incremented READ src_lba to %llu\n",
  667. (unsigned long long)src_lba);
  668. pr_debug("target_xcopy_do_work: Calling write dst_dev: %p dst_lba: %llu,"
  669. " cur_nolb: %hu\n", dst_dev, (unsigned long long)dst_lba, cur_nolb);
  670. rc = target_xcopy_write_destination(ec_cmd, xop, dst_dev,
  671. dst_lba, cur_nolb);
  672. if (rc < 0) {
  673. transport_generic_free_cmd(&xop->src_pt_cmd->se_cmd, 0);
  674. goto out;
  675. }
  676. dst_lba += cur_nolb;
  677. pr_debug("target_xcopy_do_work: Incremented WRITE dst_lba to %llu\n",
  678. (unsigned long long)dst_lba);
  679. copied_nolb += cur_nolb;
  680. nolb -= cur_nolb;
  681. transport_generic_free_cmd(&xop->src_pt_cmd->se_cmd, 0);
  682. xop->dst_pt_cmd->se_cmd.se_cmd_flags &= ~SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
  683. transport_generic_free_cmd(&xop->dst_pt_cmd->se_cmd, 0);
  684. }
  685. xcopy_pt_undepend_remotedev(xop);
  686. kfree(xop);
  687. pr_debug("target_xcopy_do_work: Final src_lba: %llu, dst_lba: %llu\n",
  688. (unsigned long long)src_lba, (unsigned long long)dst_lba);
  689. pr_debug("target_xcopy_do_work: Blocks copied: %hu, Bytes Copied: %u\n",
  690. copied_nolb, copied_nolb * dst_dev->dev_attrib.block_size);
  691. pr_debug("target_xcopy_do_work: Setting X-COPY GOOD status -> sending response\n");
  692. target_complete_cmd(ec_cmd, SAM_STAT_GOOD);
  693. return;
  694. out:
  695. xcopy_pt_undepend_remotedev(xop);
  696. kfree(xop);
  697. pr_warn("target_xcopy_do_work: Setting X-COPY CHECK_CONDITION -> sending response\n");
  698. ec_cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
  699. target_complete_cmd(ec_cmd, SAM_STAT_CHECK_CONDITION);
  700. }
  701. sense_reason_t target_do_xcopy(struct se_cmd *se_cmd)
  702. {
  703. struct se_device *dev = se_cmd->se_dev;
  704. struct xcopy_op *xop = NULL;
  705. unsigned char *p = NULL, *seg_desc;
  706. unsigned int list_id, list_id_usage, sdll, inline_dl, sa;
  707. sense_reason_t ret = TCM_INVALID_PARAMETER_LIST;
  708. int rc;
  709. unsigned short tdll;
  710. if (!dev->dev_attrib.emulate_3pc) {
  711. pr_err("EXTENDED_COPY operation explicitly disabled\n");
  712. return TCM_UNSUPPORTED_SCSI_OPCODE;
  713. }
  714. sa = se_cmd->t_task_cdb[1] & 0x1f;
  715. if (sa != 0x00) {
  716. pr_err("EXTENDED_COPY(LID4) not supported\n");
  717. return TCM_UNSUPPORTED_SCSI_OPCODE;
  718. }
  719. xop = kzalloc(sizeof(struct xcopy_op), GFP_KERNEL);
  720. if (!xop) {
  721. pr_err("Unable to allocate xcopy_op\n");
  722. return TCM_OUT_OF_RESOURCES;
  723. }
  724. xop->xop_se_cmd = se_cmd;
  725. p = transport_kmap_data_sg(se_cmd);
  726. if (!p) {
  727. pr_err("transport_kmap_data_sg() failed in target_do_xcopy\n");
  728. kfree(xop);
  729. return TCM_OUT_OF_RESOURCES;
  730. }
  731. list_id = p[0];
  732. list_id_usage = (p[1] & 0x18) >> 3;
  733. /*
  734. * Determine TARGET DESCRIPTOR LIST LENGTH + SEGMENT DESCRIPTOR LIST LENGTH
  735. */
  736. tdll = get_unaligned_be16(&p[2]);
  737. sdll = get_unaligned_be32(&p[8]);
  738. inline_dl = get_unaligned_be32(&p[12]);
  739. if (inline_dl != 0) {
  740. pr_err("XCOPY with non zero inline data length\n");
  741. goto out;
  742. }
  743. pr_debug("Processing XCOPY with list_id: 0x%02x list_id_usage: 0x%02x"
  744. " tdll: %hu sdll: %u inline_dl: %u\n", list_id, list_id_usage,
  745. tdll, sdll, inline_dl);
  746. rc = target_xcopy_parse_target_descriptors(se_cmd, xop, &p[16], tdll);
  747. if (rc <= 0)
  748. goto out;
  749. if (xop->src_dev->dev_attrib.block_size !=
  750. xop->dst_dev->dev_attrib.block_size) {
  751. pr_err("XCOPY: Non matching src_dev block_size: %u + dst_dev"
  752. " block_size: %u currently unsupported\n",
  753. xop->src_dev->dev_attrib.block_size,
  754. xop->dst_dev->dev_attrib.block_size);
  755. xcopy_pt_undepend_remotedev(xop);
  756. ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  757. goto out;
  758. }
  759. pr_debug("XCOPY: Processed %d target descriptors, length: %u\n", rc,
  760. rc * XCOPY_TARGET_DESC_LEN);
  761. seg_desc = &p[16];
  762. seg_desc += (rc * XCOPY_TARGET_DESC_LEN);
  763. rc = target_xcopy_parse_segment_descriptors(se_cmd, xop, seg_desc, sdll);
  764. if (rc <= 0) {
  765. xcopy_pt_undepend_remotedev(xop);
  766. goto out;
  767. }
  768. transport_kunmap_data_sg(se_cmd);
  769. pr_debug("XCOPY: Processed %d segment descriptors, length: %u\n", rc,
  770. rc * XCOPY_SEGMENT_DESC_LEN);
  771. INIT_WORK(&xop->xop_work, target_xcopy_do_work);
  772. queue_work(xcopy_wq, &xop->xop_work);
  773. return TCM_NO_SENSE;
  774. out:
  775. if (p)
  776. transport_kunmap_data_sg(se_cmd);
  777. kfree(xop);
  778. return ret;
  779. }
  780. static sense_reason_t target_rcr_operating_parameters(struct se_cmd *se_cmd)
  781. {
  782. unsigned char *p;
  783. p = transport_kmap_data_sg(se_cmd);
  784. if (!p) {
  785. pr_err("transport_kmap_data_sg failed in"
  786. " target_rcr_operating_parameters\n");
  787. return TCM_OUT_OF_RESOURCES;
  788. }
  789. if (se_cmd->data_length < 54) {
  790. pr_err("Receive Copy Results Op Parameters length"
  791. " too small: %u\n", se_cmd->data_length);
  792. transport_kunmap_data_sg(se_cmd);
  793. return TCM_INVALID_CDB_FIELD;
  794. }
  795. /*
  796. * Set SNLID=1 (Supports no List ID)
  797. */
  798. p[4] = 0x1;
  799. /*
  800. * MAXIMUM TARGET DESCRIPTOR COUNT
  801. */
  802. put_unaligned_be16(RCR_OP_MAX_TARGET_DESC_COUNT, &p[8]);
  803. /*
  804. * MAXIMUM SEGMENT DESCRIPTOR COUNT
  805. */
  806. put_unaligned_be16(RCR_OP_MAX_SG_DESC_COUNT, &p[10]);
  807. /*
  808. * MAXIMUM DESCRIPTOR LIST LENGTH
  809. */
  810. put_unaligned_be32(RCR_OP_MAX_DESC_LIST_LEN, &p[12]);
  811. /*
  812. * MAXIMUM SEGMENT LENGTH
  813. */
  814. put_unaligned_be32(RCR_OP_MAX_SEGMENT_LEN, &p[16]);
  815. /*
  816. * MAXIMUM INLINE DATA LENGTH for SA 0x04 (NOT SUPPORTED)
  817. */
  818. put_unaligned_be32(0x0, &p[20]);
  819. /*
  820. * HELD DATA LIMIT
  821. */
  822. put_unaligned_be32(0x0, &p[24]);
  823. /*
  824. * MAXIMUM STREAM DEVICE TRANSFER SIZE
  825. */
  826. put_unaligned_be32(0x0, &p[28]);
  827. /*
  828. * TOTAL CONCURRENT COPIES
  829. */
  830. put_unaligned_be16(RCR_OP_TOTAL_CONCURR_COPIES, &p[34]);
  831. /*
  832. * MAXIMUM CONCURRENT COPIES
  833. */
  834. p[36] = RCR_OP_MAX_CONCURR_COPIES;
  835. /*
  836. * DATA SEGMENT GRANULARITY (log 2)
  837. */
  838. p[37] = RCR_OP_DATA_SEG_GRAN_LOG2;
  839. /*
  840. * INLINE DATA GRANULARITY log 2)
  841. */
  842. p[38] = RCR_OP_INLINE_DATA_GRAN_LOG2;
  843. /*
  844. * HELD DATA GRANULARITY
  845. */
  846. p[39] = RCR_OP_HELD_DATA_GRAN_LOG2;
  847. /*
  848. * IMPLEMENTED DESCRIPTOR LIST LENGTH
  849. */
  850. p[43] = 0x2;
  851. /*
  852. * List of implemented descriptor type codes (ordered)
  853. */
  854. p[44] = 0x02; /* Copy Block to Block device */
  855. p[45] = 0xe4; /* Identification descriptor target descriptor */
  856. /*
  857. * AVAILABLE DATA (n-3)
  858. */
  859. put_unaligned_be32(42, &p[0]);
  860. transport_kunmap_data_sg(se_cmd);
  861. target_complete_cmd(se_cmd, GOOD);
  862. return TCM_NO_SENSE;
  863. }
  864. sense_reason_t target_do_receive_copy_results(struct se_cmd *se_cmd)
  865. {
  866. unsigned char *cdb = &se_cmd->t_task_cdb[0];
  867. int sa = (cdb[1] & 0x1f), list_id = cdb[2];
  868. sense_reason_t rc = TCM_NO_SENSE;
  869. pr_debug("Entering target_do_receive_copy_results: SA: 0x%02x, List ID:"
  870. " 0x%02x, AL: %u\n", sa, list_id, se_cmd->data_length);
  871. if (list_id != 0) {
  872. pr_err("Receive Copy Results with non zero list identifier"
  873. " not supported\n");
  874. return TCM_INVALID_CDB_FIELD;
  875. }
  876. switch (sa) {
  877. case RCR_SA_OPERATING_PARAMETERS:
  878. rc = target_rcr_operating_parameters(se_cmd);
  879. break;
  880. case RCR_SA_COPY_STATUS:
  881. case RCR_SA_RECEIVE_DATA:
  882. case RCR_SA_FAILED_SEGMENT_DETAILS:
  883. default:
  884. pr_err("Unsupported SA for receive copy results: 0x%02x\n", sa);
  885. return TCM_INVALID_CDB_FIELD;
  886. }
  887. return rc;
  888. }