target_core_xcopy.c 29 KB

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