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