tfc_io.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 <linux/utsname.h>
  30. #include <linux/init.h>
  31. #include <linux/slab.h>
  32. #include <linux/kthread.h>
  33. #include <linux/types.h>
  34. #include <linux/string.h>
  35. #include <linux/configfs.h>
  36. #include <linux/ctype.h>
  37. #include <linux/hash.h>
  38. #include <linux/ratelimit.h>
  39. #include <asm/unaligned.h>
  40. #include <scsi/libfc.h>
  41. #include <scsi/fc_encode.h>
  42. #include <target/target_core_base.h>
  43. #include <target/target_core_fabric.h>
  44. #include <target/configfs_macros.h>
  45. #include "tcm_fc.h"
  46. /*
  47. * Deliver read data back to initiator.
  48. * XXX TBD handle resource problems later.
  49. */
  50. int ft_queue_data_in(struct se_cmd *se_cmd)
  51. {
  52. struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
  53. struct fc_frame *fp = NULL;
  54. struct fc_exch *ep;
  55. struct fc_lport *lport;
  56. struct scatterlist *sg = NULL;
  57. size_t remaining;
  58. u32 f_ctl = FC_FC_EX_CTX | FC_FC_REL_OFF;
  59. u32 mem_off = 0;
  60. u32 fh_off = 0;
  61. u32 frame_off = 0;
  62. size_t frame_len = 0;
  63. size_t mem_len = 0;
  64. size_t tlen;
  65. size_t off_in_page;
  66. struct page *page = NULL;
  67. int use_sg;
  68. int error;
  69. void *page_addr;
  70. void *from;
  71. void *to = NULL;
  72. if (cmd->aborted)
  73. return 0;
  74. if (se_cmd->scsi_status == SAM_STAT_TASK_SET_FULL)
  75. goto queue_status;
  76. ep = fc_seq_exch(cmd->seq);
  77. lport = ep->lp;
  78. cmd->seq = lport->tt.seq_start_next(cmd->seq);
  79. remaining = se_cmd->data_length;
  80. /*
  81. * Setup to use first mem list entry, unless no data.
  82. */
  83. BUG_ON(remaining && !se_cmd->t_data_sg);
  84. if (remaining) {
  85. sg = se_cmd->t_data_sg;
  86. mem_len = sg->length;
  87. mem_off = sg->offset;
  88. page = sg_page(sg);
  89. }
  90. /* no scatter/gather in skb for odd word length due to fc_seq_send() */
  91. use_sg = !(remaining % 4);
  92. while (remaining) {
  93. struct fc_seq *seq = cmd->seq;
  94. if (!seq) {
  95. pr_debug("%s: Command aborted, xid 0x%x\n",
  96. __func__, ep->xid);
  97. break;
  98. }
  99. if (!mem_len) {
  100. sg = sg_next(sg);
  101. mem_len = min((size_t)sg->length, remaining);
  102. mem_off = sg->offset;
  103. page = sg_page(sg);
  104. }
  105. if (!frame_len) {
  106. /*
  107. * If lport's has capability of Large Send Offload LSO)
  108. * , then allow 'frame_len' to be as big as 'lso_max'
  109. * if indicated transfer length is >= lport->lso_max
  110. */
  111. frame_len = (lport->seq_offload) ? lport->lso_max :
  112. cmd->sess->max_frame;
  113. frame_len = min(frame_len, remaining);
  114. fp = fc_frame_alloc(lport, use_sg ? 0 : frame_len);
  115. if (!fp)
  116. return -ENOMEM;
  117. to = fc_frame_payload_get(fp, 0);
  118. fh_off = frame_off;
  119. frame_off += frame_len;
  120. /*
  121. * Setup the frame's max payload which is used by base
  122. * driver to indicate HW about max frame size, so that
  123. * HW can do fragmentation appropriately based on
  124. * "gso_max_size" of underline netdev.
  125. */
  126. fr_max_payload(fp) = cmd->sess->max_frame;
  127. }
  128. tlen = min(mem_len, frame_len);
  129. if (use_sg) {
  130. off_in_page = mem_off;
  131. BUG_ON(!page);
  132. get_page(page);
  133. skb_fill_page_desc(fp_skb(fp),
  134. skb_shinfo(fp_skb(fp))->nr_frags,
  135. page, off_in_page, tlen);
  136. fr_len(fp) += tlen;
  137. fp_skb(fp)->data_len += tlen;
  138. fp_skb(fp)->truesize +=
  139. PAGE_SIZE << compound_order(page);
  140. } else {
  141. BUG_ON(!page);
  142. from = kmap_atomic(page + (mem_off >> PAGE_SHIFT));
  143. page_addr = from;
  144. from += mem_off & ~PAGE_MASK;
  145. tlen = min(tlen, (size_t)(PAGE_SIZE -
  146. (mem_off & ~PAGE_MASK)));
  147. memcpy(to, from, tlen);
  148. kunmap_atomic(page_addr);
  149. to += tlen;
  150. }
  151. mem_off += tlen;
  152. mem_len -= tlen;
  153. frame_len -= tlen;
  154. remaining -= tlen;
  155. if (frame_len &&
  156. (skb_shinfo(fp_skb(fp))->nr_frags < FC_FRAME_SG_LEN))
  157. continue;
  158. if (!remaining)
  159. f_ctl |= FC_FC_END_SEQ;
  160. fc_fill_fc_hdr(fp, FC_RCTL_DD_SOL_DATA, ep->did, ep->sid,
  161. FC_TYPE_FCP, f_ctl, fh_off);
  162. error = lport->tt.seq_send(lport, seq, fp);
  163. if (error) {
  164. pr_info_ratelimited("%s: Failed to send frame %p, "
  165. "xid <0x%x>, remaining %zu, "
  166. "lso_max <0x%x>\n",
  167. __func__, fp, ep->xid,
  168. remaining, lport->lso_max);
  169. /*
  170. * Go ahead and set TASK_SET_FULL status ignoring the
  171. * rest of the DataIN, and immediately attempt to
  172. * send the response via ft_queue_status() in order
  173. * to notify the initiator that it should reduce it's
  174. * per LUN queue_depth.
  175. */
  176. se_cmd->scsi_status = SAM_STAT_TASK_SET_FULL;
  177. break;
  178. }
  179. }
  180. queue_status:
  181. return ft_queue_status(se_cmd);
  182. }
  183. static void ft_execute_work(struct work_struct *work)
  184. {
  185. struct ft_cmd *cmd = container_of(work, struct ft_cmd, work);
  186. target_execute_cmd(&cmd->se_cmd);
  187. }
  188. /*
  189. * Receive write data frame.
  190. */
  191. void ft_recv_write_data(struct ft_cmd *cmd, struct fc_frame *fp)
  192. {
  193. struct se_cmd *se_cmd = &cmd->se_cmd;
  194. struct fc_seq *seq = cmd->seq;
  195. struct fc_exch *ep;
  196. struct fc_lport *lport;
  197. struct fc_frame_header *fh;
  198. struct scatterlist *sg = NULL;
  199. u32 mem_off = 0;
  200. u32 rel_off;
  201. size_t frame_len;
  202. size_t mem_len = 0;
  203. size_t tlen;
  204. struct page *page = NULL;
  205. void *page_addr;
  206. void *from;
  207. void *to;
  208. u32 f_ctl;
  209. void *buf;
  210. fh = fc_frame_header_get(fp);
  211. if (!(ntoh24(fh->fh_f_ctl) & FC_FC_REL_OFF))
  212. goto drop;
  213. f_ctl = ntoh24(fh->fh_f_ctl);
  214. ep = fc_seq_exch(seq);
  215. lport = ep->lp;
  216. if (cmd->was_ddp_setup) {
  217. BUG_ON(!ep);
  218. BUG_ON(!lport);
  219. /*
  220. * Since DDP (Large Rx offload) was setup for this request,
  221. * payload is expected to be copied directly to user buffers.
  222. */
  223. buf = fc_frame_payload_get(fp, 1);
  224. if (buf)
  225. pr_err("%s: xid 0x%x, f_ctl 0x%x, cmd->sg %p, "
  226. "cmd->sg_cnt 0x%x. DDP was setup"
  227. " hence not expected to receive frame with "
  228. "payload, Frame will be dropped if"
  229. "'Sequence Initiative' bit in f_ctl is"
  230. "not set\n", __func__, ep->xid, f_ctl,
  231. se_cmd->t_data_sg, se_cmd->t_data_nents);
  232. /*
  233. * Invalidate HW DDP context if it was setup for respective
  234. * command. Invalidation of HW DDP context is requited in both
  235. * situation (success and error).
  236. */
  237. ft_invl_hw_context(cmd);
  238. /*
  239. * If "Sequence Initiative (TSI)" bit set in f_ctl, means last
  240. * write data frame is received successfully where payload is
  241. * posted directly to user buffer and only the last frame's
  242. * header is posted in receive queue.
  243. *
  244. * If "Sequence Initiative (TSI)" bit is not set, means error
  245. * condition w.r.t. DDP, hence drop the packet and let explict
  246. * ABORTS from other end of exchange timer trigger the recovery.
  247. */
  248. if (f_ctl & FC_FC_SEQ_INIT)
  249. goto last_frame;
  250. else
  251. goto drop;
  252. }
  253. rel_off = ntohl(fh->fh_parm_offset);
  254. frame_len = fr_len(fp);
  255. if (frame_len <= sizeof(*fh))
  256. goto drop;
  257. frame_len -= sizeof(*fh);
  258. from = fc_frame_payload_get(fp, 0);
  259. if (rel_off >= se_cmd->data_length)
  260. goto drop;
  261. if (frame_len + rel_off > se_cmd->data_length)
  262. frame_len = se_cmd->data_length - rel_off;
  263. /*
  264. * Setup to use first mem list entry, unless no data.
  265. */
  266. BUG_ON(frame_len && !se_cmd->t_data_sg);
  267. if (frame_len) {
  268. sg = se_cmd->t_data_sg;
  269. mem_len = sg->length;
  270. mem_off = sg->offset;
  271. page = sg_page(sg);
  272. }
  273. while (frame_len) {
  274. if (!mem_len) {
  275. sg = sg_next(sg);
  276. mem_len = sg->length;
  277. mem_off = sg->offset;
  278. page = sg_page(sg);
  279. }
  280. if (rel_off >= mem_len) {
  281. rel_off -= mem_len;
  282. mem_len = 0;
  283. continue;
  284. }
  285. mem_off += rel_off;
  286. mem_len -= rel_off;
  287. rel_off = 0;
  288. tlen = min(mem_len, frame_len);
  289. to = kmap_atomic(page + (mem_off >> PAGE_SHIFT));
  290. page_addr = to;
  291. to += mem_off & ~PAGE_MASK;
  292. tlen = min(tlen, (size_t)(PAGE_SIZE -
  293. (mem_off & ~PAGE_MASK)));
  294. memcpy(to, from, tlen);
  295. kunmap_atomic(page_addr);
  296. from += tlen;
  297. frame_len -= tlen;
  298. mem_off += tlen;
  299. mem_len -= tlen;
  300. cmd->write_data_len += tlen;
  301. }
  302. last_frame:
  303. if (cmd->write_data_len == se_cmd->data_length) {
  304. INIT_WORK(&cmd->work, ft_execute_work);
  305. queue_work(cmd->sess->tport->tpg->workqueue, &cmd->work);
  306. }
  307. drop:
  308. fc_frame_free(fp);
  309. }
  310. /*
  311. * Handle and cleanup any HW specific resources if
  312. * received ABORTS, errors, timeouts.
  313. */
  314. void ft_invl_hw_context(struct ft_cmd *cmd)
  315. {
  316. struct fc_seq *seq;
  317. struct fc_exch *ep = NULL;
  318. struct fc_lport *lport = NULL;
  319. BUG_ON(!cmd);
  320. seq = cmd->seq;
  321. /* Cleanup the DDP context in HW if DDP was setup */
  322. if (cmd->was_ddp_setup && seq) {
  323. ep = fc_seq_exch(seq);
  324. if (ep) {
  325. lport = ep->lp;
  326. if (lport && (ep->xid <= lport->lro_xid)) {
  327. /*
  328. * "ddp_done" trigger invalidation of HW
  329. * specific DDP context
  330. */
  331. cmd->write_data_len = lport->tt.ddp_done(lport,
  332. ep->xid);
  333. /*
  334. * Resetting same variable to indicate HW's
  335. * DDP context has been invalidated to avoid
  336. * re_invalidation of same context (context is
  337. * identified using ep->xid)
  338. */
  339. cmd->was_ddp_setup = 0;
  340. }
  341. }
  342. }
  343. }