tfc_io.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Copyright (c) 2010 Cisco Systems, Inc.
  3. *
  4. * Portions based on tcm_loop_fabric_scsi.c and libfc/fc_fcp.c
  5. *
  6. * Copyright (c) 2007 Intel Corporation. All rights reserved.
  7. * Copyright (c) 2008 Red Hat, Inc. All rights reserved.
  8. * Copyright (c) 2008 Mike Christie
  9. * Copyright (c) 2009 Rising Tide, Inc.
  10. * Copyright (c) 2009 Linux-iSCSI.org
  11. * Copyright (c) 2009 Nicholas A. Bellinger <nab@linux-iscsi.org>
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms and conditions of the GNU General Public License,
  15. * version 2, as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope it will be useful, but WITHOUT
  18. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  20. * more details.
  21. *
  22. * You should have received a copy of the GNU General Public License along with
  23. * this program; if not, write to the Free Software Foundation, Inc.,
  24. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  25. */
  26. /* XXX TBD some includes may be extraneous */
  27. #include <linux/module.h>
  28. #include <linux/moduleparam.h>
  29. #include <generated/utsrelease.h>
  30. #include <linux/utsname.h>
  31. #include <linux/init.h>
  32. #include <linux/slab.h>
  33. #include <linux/kthread.h>
  34. #include <linux/types.h>
  35. #include <linux/string.h>
  36. #include <linux/configfs.h>
  37. #include <linux/ctype.h>
  38. #include <linux/hash.h>
  39. #include <linux/ratelimit.h>
  40. #include <asm/unaligned.h>
  41. #include <scsi/scsi.h>
  42. #include <scsi/scsi_host.h>
  43. #include <scsi/scsi_device.h>
  44. #include <scsi/scsi_cmnd.h>
  45. #include <scsi/libfc.h>
  46. #include <scsi/fc_encode.h>
  47. #include <target/target_core_base.h>
  48. #include <target/target_core_fabric.h>
  49. #include <target/target_core_configfs.h>
  50. #include <target/configfs_macros.h>
  51. #include "tcm_fc.h"
  52. /*
  53. * Deliver read data back to initiator.
  54. * XXX TBD handle resource problems later.
  55. */
  56. int ft_queue_data_in(struct se_cmd *se_cmd)
  57. {
  58. struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
  59. struct fc_frame *fp = NULL;
  60. struct fc_exch *ep;
  61. struct fc_lport *lport;
  62. struct scatterlist *sg = NULL;
  63. size_t remaining;
  64. u32 f_ctl = FC_FC_EX_CTX | FC_FC_REL_OFF;
  65. u32 mem_off = 0;
  66. u32 fh_off = 0;
  67. u32 frame_off = 0;
  68. size_t frame_len = 0;
  69. size_t mem_len = 0;
  70. size_t tlen;
  71. size_t off_in_page;
  72. struct page *page = NULL;
  73. int use_sg;
  74. int error;
  75. void *page_addr;
  76. void *from;
  77. void *to = NULL;
  78. if (cmd->aborted)
  79. return 0;
  80. ep = fc_seq_exch(cmd->seq);
  81. lport = ep->lp;
  82. cmd->seq = lport->tt.seq_start_next(cmd->seq);
  83. remaining = se_cmd->data_length;
  84. /*
  85. * Setup to use first mem list entry, unless no data.
  86. */
  87. BUG_ON(remaining && !se_cmd->t_data_sg);
  88. if (remaining) {
  89. sg = se_cmd->t_data_sg;
  90. mem_len = sg->length;
  91. mem_off = sg->offset;
  92. page = sg_page(sg);
  93. }
  94. /* no scatter/gather in skb for odd word length due to fc_seq_send() */
  95. use_sg = !(remaining % 4);
  96. while (remaining) {
  97. if (!mem_len) {
  98. sg = sg_next(sg);
  99. mem_len = min((size_t)sg->length, remaining);
  100. mem_off = sg->offset;
  101. page = sg_page(sg);
  102. }
  103. if (!frame_len) {
  104. /*
  105. * If lport's has capability of Large Send Offload LSO)
  106. * , then allow 'frame_len' to be as big as 'lso_max'
  107. * if indicated transfer length is >= lport->lso_max
  108. */
  109. frame_len = (lport->seq_offload) ? lport->lso_max :
  110. cmd->sess->max_frame;
  111. frame_len = min(frame_len, remaining);
  112. fp = fc_frame_alloc(lport, use_sg ? 0 : frame_len);
  113. if (!fp)
  114. return -ENOMEM;
  115. to = fc_frame_payload_get(fp, 0);
  116. fh_off = frame_off;
  117. frame_off += frame_len;
  118. /*
  119. * Setup the frame's max payload which is used by base
  120. * driver to indicate HW about max frame size, so that
  121. * HW can do fragmentation appropriately based on
  122. * "gso_max_size" of underline netdev.
  123. */
  124. fr_max_payload(fp) = cmd->sess->max_frame;
  125. }
  126. tlen = min(mem_len, frame_len);
  127. if (use_sg) {
  128. off_in_page = mem_off;
  129. BUG_ON(!page);
  130. get_page(page);
  131. skb_fill_page_desc(fp_skb(fp),
  132. skb_shinfo(fp_skb(fp))->nr_frags,
  133. page, off_in_page, tlen);
  134. fr_len(fp) += tlen;
  135. fp_skb(fp)->data_len += tlen;
  136. fp_skb(fp)->truesize +=
  137. PAGE_SIZE << compound_order(page);
  138. } else {
  139. BUG_ON(!page);
  140. from = kmap_atomic(page + (mem_off >> PAGE_SHIFT));
  141. page_addr = from;
  142. from += mem_off & ~PAGE_MASK;
  143. tlen = min(tlen, (size_t)(PAGE_SIZE -
  144. (mem_off & ~PAGE_MASK)));
  145. memcpy(to, from, tlen);
  146. kunmap_atomic(page_addr);
  147. to += tlen;
  148. }
  149. mem_off += tlen;
  150. mem_len -= tlen;
  151. frame_len -= tlen;
  152. remaining -= tlen;
  153. if (frame_len &&
  154. (skb_shinfo(fp_skb(fp))->nr_frags < FC_FRAME_SG_LEN))
  155. continue;
  156. if (!remaining)
  157. f_ctl |= FC_FC_END_SEQ;
  158. fc_fill_fc_hdr(fp, FC_RCTL_DD_SOL_DATA, ep->did, ep->sid,
  159. FC_TYPE_FCP, f_ctl, fh_off);
  160. error = lport->tt.seq_send(lport, cmd->seq, fp);
  161. if (error) {
  162. /* XXX For now, initiator will retry */
  163. pr_err_ratelimited("%s: Failed to send frame %p, "
  164. "xid <0x%x>, remaining %zu, "
  165. "lso_max <0x%x>\n",
  166. __func__, fp, ep->xid,
  167. remaining, lport->lso_max);
  168. }
  169. }
  170. return ft_queue_status(se_cmd);
  171. }
  172. static void ft_execute_work(struct work_struct *work)
  173. {
  174. struct ft_cmd *cmd = container_of(work, struct ft_cmd, work);
  175. target_execute_cmd(&cmd->se_cmd);
  176. }
  177. /*
  178. * Receive write data frame.
  179. */
  180. void ft_recv_write_data(struct ft_cmd *cmd, struct fc_frame *fp)
  181. {
  182. struct se_cmd *se_cmd = &cmd->se_cmd;
  183. struct fc_seq *seq = cmd->seq;
  184. struct fc_exch *ep;
  185. struct fc_lport *lport;
  186. struct fc_frame_header *fh;
  187. struct scatterlist *sg = NULL;
  188. u32 mem_off = 0;
  189. u32 rel_off;
  190. size_t frame_len;
  191. size_t mem_len = 0;
  192. size_t tlen;
  193. struct page *page = NULL;
  194. void *page_addr;
  195. void *from;
  196. void *to;
  197. u32 f_ctl;
  198. void *buf;
  199. fh = fc_frame_header_get(fp);
  200. if (!(ntoh24(fh->fh_f_ctl) & FC_FC_REL_OFF))
  201. goto drop;
  202. f_ctl = ntoh24(fh->fh_f_ctl);
  203. ep = fc_seq_exch(seq);
  204. lport = ep->lp;
  205. if (cmd->was_ddp_setup) {
  206. BUG_ON(!ep);
  207. BUG_ON(!lport);
  208. /*
  209. * Since DDP (Large Rx offload) was setup for this request,
  210. * payload is expected to be copied directly to user buffers.
  211. */
  212. buf = fc_frame_payload_get(fp, 1);
  213. if (buf)
  214. pr_err("%s: xid 0x%x, f_ctl 0x%x, cmd->sg %p, "
  215. "cmd->sg_cnt 0x%x. DDP was setup"
  216. " hence not expected to receive frame with "
  217. "payload, Frame will be dropped if"
  218. "'Sequence Initiative' bit in f_ctl is"
  219. "not set\n", __func__, ep->xid, f_ctl,
  220. se_cmd->t_data_sg, se_cmd->t_data_nents);
  221. /*
  222. * Invalidate HW DDP context if it was setup for respective
  223. * command. Invalidation of HW DDP context is requited in both
  224. * situation (success and error).
  225. */
  226. ft_invl_hw_context(cmd);
  227. /*
  228. * If "Sequence Initiative (TSI)" bit set in f_ctl, means last
  229. * write data frame is received successfully where payload is
  230. * posted directly to user buffer and only the last frame's
  231. * header is posted in receive queue.
  232. *
  233. * If "Sequence Initiative (TSI)" bit is not set, means error
  234. * condition w.r.t. DDP, hence drop the packet and let explict
  235. * ABORTS from other end of exchange timer trigger the recovery.
  236. */
  237. if (f_ctl & FC_FC_SEQ_INIT)
  238. goto last_frame;
  239. else
  240. goto drop;
  241. }
  242. rel_off = ntohl(fh->fh_parm_offset);
  243. frame_len = fr_len(fp);
  244. if (frame_len <= sizeof(*fh))
  245. goto drop;
  246. frame_len -= sizeof(*fh);
  247. from = fc_frame_payload_get(fp, 0);
  248. if (rel_off >= se_cmd->data_length)
  249. goto drop;
  250. if (frame_len + rel_off > se_cmd->data_length)
  251. frame_len = se_cmd->data_length - rel_off;
  252. /*
  253. * Setup to use first mem list entry, unless no data.
  254. */
  255. BUG_ON(frame_len && !se_cmd->t_data_sg);
  256. if (frame_len) {
  257. sg = se_cmd->t_data_sg;
  258. mem_len = sg->length;
  259. mem_off = sg->offset;
  260. page = sg_page(sg);
  261. }
  262. while (frame_len) {
  263. if (!mem_len) {
  264. sg = sg_next(sg);
  265. mem_len = sg->length;
  266. mem_off = sg->offset;
  267. page = sg_page(sg);
  268. }
  269. if (rel_off >= mem_len) {
  270. rel_off -= mem_len;
  271. mem_len = 0;
  272. continue;
  273. }
  274. mem_off += rel_off;
  275. mem_len -= rel_off;
  276. rel_off = 0;
  277. tlen = min(mem_len, frame_len);
  278. to = kmap_atomic(page + (mem_off >> PAGE_SHIFT));
  279. page_addr = to;
  280. to += mem_off & ~PAGE_MASK;
  281. tlen = min(tlen, (size_t)(PAGE_SIZE -
  282. (mem_off & ~PAGE_MASK)));
  283. memcpy(to, from, tlen);
  284. kunmap_atomic(page_addr);
  285. from += tlen;
  286. frame_len -= tlen;
  287. mem_off += tlen;
  288. mem_len -= tlen;
  289. cmd->write_data_len += tlen;
  290. }
  291. last_frame:
  292. if (cmd->write_data_len == se_cmd->data_length) {
  293. INIT_WORK(&cmd->work, ft_execute_work);
  294. queue_work(cmd->sess->tport->tpg->workqueue, &cmd->work);
  295. }
  296. drop:
  297. fc_frame_free(fp);
  298. }
  299. /*
  300. * Handle and cleanup any HW specific resources if
  301. * received ABORTS, errors, timeouts.
  302. */
  303. void ft_invl_hw_context(struct ft_cmd *cmd)
  304. {
  305. struct fc_seq *seq = cmd->seq;
  306. struct fc_exch *ep = NULL;
  307. struct fc_lport *lport = NULL;
  308. BUG_ON(!cmd);
  309. /* Cleanup the DDP context in HW if DDP was setup */
  310. if (cmd->was_ddp_setup && seq) {
  311. ep = fc_seq_exch(seq);
  312. if (ep) {
  313. lport = ep->lp;
  314. if (lport && (ep->xid <= lport->lro_xid))
  315. /*
  316. * "ddp_done" trigger invalidation of HW
  317. * specific DDP context
  318. */
  319. cmd->write_data_len = lport->tt.ddp_done(lport,
  320. ep->xid);
  321. /*
  322. * Resetting same variable to indicate HW's
  323. * DDP context has been invalidated to avoid
  324. * re_invalidation of same context (context is
  325. * identified using ep->xid)
  326. */
  327. cmd->was_ddp_setup = 0;
  328. }
  329. }
  330. }