target_core_xcopy.c 28 KB

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