tfc_io.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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_transport.h>
  49. #include <target/target_core_fabric_ops.h>
  50. #include <target/target_core_device.h>
  51. #include <target/target_core_tpg.h>
  52. #include <target/target_core_configfs.h>
  53. #include <target/configfs_macros.h>
  54. #include "tcm_fc.h"
  55. /*
  56. * Deliver read data back to initiator.
  57. * XXX TBD handle resource problems later.
  58. */
  59. int ft_queue_data_in(struct se_cmd *se_cmd)
  60. {
  61. struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
  62. struct fc_frame *fp = NULL;
  63. struct fc_exch *ep;
  64. struct fc_lport *lport;
  65. struct scatterlist *sg = NULL;
  66. size_t remaining;
  67. u32 f_ctl = FC_FC_EX_CTX | FC_FC_REL_OFF;
  68. u32 mem_off = 0;
  69. u32 fh_off = 0;
  70. u32 frame_off = 0;
  71. size_t frame_len = 0;
  72. size_t mem_len = 0;
  73. size_t tlen;
  74. size_t off_in_page;
  75. struct page *page = NULL;
  76. int use_sg;
  77. int error;
  78. void *page_addr;
  79. void *from;
  80. void *to = NULL;
  81. ep = fc_seq_exch(cmd->seq);
  82. lport = ep->lp;
  83. cmd->seq = lport->tt.seq_start_next(cmd->seq);
  84. remaining = se_cmd->data_length;
  85. /*
  86. * Setup to use first mem list entry, unless no data.
  87. */
  88. BUG_ON(remaining && !se_cmd->t_data_sg);
  89. if (remaining) {
  90. sg = se_cmd->t_data_sg;
  91. mem_len = sg->length;
  92. mem_off = sg->offset;
  93. page = sg_page(sg);
  94. }
  95. /* no scatter/gather in skb for odd word length due to fc_seq_send() */
  96. use_sg = !(remaining % 4);
  97. while (remaining) {
  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. KM_SOFTIRQ0);
  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, KM_SOFTIRQ0);
  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, cmd->seq, fp);
  163. if (error) {
  164. /* XXX For now, initiator will retry */
  165. pr_err_ratelimited("%s: Failed to send frame %p, "
  166. "xid <0x%x>, remaining %zu, "
  167. "lso_max <0x%x>\n",
  168. __func__, fp, ep->xid,
  169. remaining, lport->lso_max);
  170. }
  171. }
  172. return ft_queue_status(se_cmd);
  173. }
  174. /*
  175. * Receive write data frame.
  176. */
  177. void ft_recv_write_data(struct ft_cmd *cmd, struct fc_frame *fp)
  178. {
  179. struct se_cmd *se_cmd = &cmd->se_cmd;
  180. struct fc_seq *seq = cmd->seq;
  181. struct fc_exch *ep;
  182. struct fc_lport *lport;
  183. struct fc_frame_header *fh;
  184. struct scatterlist *sg = NULL;
  185. u32 mem_off = 0;
  186. u32 rel_off;
  187. size_t frame_len;
  188. size_t mem_len = 0;
  189. size_t tlen;
  190. struct page *page = NULL;
  191. void *page_addr;
  192. void *from;
  193. void *to;
  194. u32 f_ctl;
  195. void *buf;
  196. fh = fc_frame_header_get(fp);
  197. if (!(ntoh24(fh->fh_f_ctl) & FC_FC_REL_OFF))
  198. goto drop;
  199. /*
  200. * Doesn't expect even single byte of payload. Payload
  201. * is expected to be copied directly to user buffers
  202. * due to DDP (Large Rx offload) feature, hence
  203. * BUG_ON if BUF is non-NULL
  204. */
  205. buf = fc_frame_payload_get(fp, 1);
  206. if (cmd->was_ddp_setup && buf) {
  207. pr_debug("%s: When DDP was setup, not expected to"
  208. "receive frame with payload, Payload shall be"
  209. "copied directly to buffer instead of coming "
  210. "via. legacy receive queues\n", __func__);
  211. BUG_ON(buf);
  212. }
  213. /*
  214. * If ft_cmd indicated 'ddp_setup', in that case only the last frame
  215. * should come with 'TSI bit being set'. If 'TSI bit is not set and if
  216. * data frame appears here, means error condition. In both the cases
  217. * release the DDP context (ddp_put) and in error case, as well
  218. * initiate error recovery mechanism.
  219. */
  220. ep = fc_seq_exch(seq);
  221. if (cmd->was_ddp_setup) {
  222. BUG_ON(!ep);
  223. lport = ep->lp;
  224. BUG_ON(!lport);
  225. }
  226. if (cmd->was_ddp_setup && ep->xid != FC_XID_UNKNOWN) {
  227. f_ctl = ntoh24(fh->fh_f_ctl);
  228. /*
  229. * If TSI bit set in f_ctl, means last write data frame is
  230. * received successfully where payload is posted directly
  231. * to user buffer and only the last frame's header is posted
  232. * in legacy receive queue
  233. */
  234. if (f_ctl & FC_FC_SEQ_INIT) { /* TSI bit set in FC frame */
  235. cmd->write_data_len = lport->tt.ddp_done(lport,
  236. ep->xid);
  237. goto last_frame;
  238. } else {
  239. /*
  240. * Updating the write_data_len may be meaningless at
  241. * this point, but just in case if required in future
  242. * for debugging or any other purpose
  243. */
  244. pr_err("%s: Received frame with TSI bit not"
  245. " being SET, dropping the frame, "
  246. "cmd->sg <%p>, cmd->sg_cnt <0x%x>\n",
  247. __func__, cmd->sg, cmd->sg_cnt);
  248. cmd->write_data_len = lport->tt.ddp_done(lport,
  249. ep->xid);
  250. lport->tt.seq_exch_abort(cmd->seq, 0);
  251. goto drop;
  252. }
  253. }
  254. rel_off = ntohl(fh->fh_parm_offset);
  255. frame_len = fr_len(fp);
  256. if (frame_len <= sizeof(*fh))
  257. goto drop;
  258. frame_len -= sizeof(*fh);
  259. from = fc_frame_payload_get(fp, 0);
  260. if (rel_off >= se_cmd->data_length)
  261. goto drop;
  262. if (frame_len + rel_off > se_cmd->data_length)
  263. frame_len = se_cmd->data_length - rel_off;
  264. /*
  265. * Setup to use first mem list entry, unless no data.
  266. */
  267. BUG_ON(frame_len && !se_cmd->t_data_sg);
  268. if (frame_len) {
  269. sg = se_cmd->t_data_sg;
  270. mem_len = sg->length;
  271. mem_off = sg->offset;
  272. page = sg_page(sg);
  273. }
  274. while (frame_len) {
  275. if (!mem_len) {
  276. sg = sg_next(sg);
  277. mem_len = sg->length;
  278. mem_off = sg->offset;
  279. page = sg_page(sg);
  280. }
  281. if (rel_off >= mem_len) {
  282. rel_off -= mem_len;
  283. mem_len = 0;
  284. continue;
  285. }
  286. mem_off += rel_off;
  287. mem_len -= rel_off;
  288. rel_off = 0;
  289. tlen = min(mem_len, frame_len);
  290. to = kmap_atomic(page + (mem_off >> PAGE_SHIFT),
  291. KM_SOFTIRQ0);
  292. page_addr = to;
  293. to += mem_off & ~PAGE_MASK;
  294. tlen = min(tlen, (size_t)(PAGE_SIZE -
  295. (mem_off & ~PAGE_MASK)));
  296. memcpy(to, from, tlen);
  297. kunmap_atomic(page_addr, KM_SOFTIRQ0);
  298. from += tlen;
  299. frame_len -= tlen;
  300. mem_off += tlen;
  301. mem_len -= tlen;
  302. cmd->write_data_len += tlen;
  303. }
  304. last_frame:
  305. if (cmd->write_data_len == se_cmd->data_length)
  306. transport_generic_handle_data(se_cmd);
  307. drop:
  308. fc_frame_free(fp);
  309. }