target_core_xcopy.c 29 KB

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