target_core_xcopy.c 28 KB

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