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