target_core_xcopy.c 28 KB

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