target_core_xcopy.c 31 KB

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