tfc_io.c 9.7 KB

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