target_core_xcopy.c 28 KB

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