tfc_io.c 9.5 KB

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