target_core_xcopy.c 31 KB

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