target_core_xcopy.c 28 KB

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