tfc_io.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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/version.h>
  30. #include <generated/utsrelease.h>
  31. #include <linux/utsname.h>
  32. #include <linux/init.h>
  33. #include <linux/slab.h>
  34. #include <linux/kthread.h>
  35. #include <linux/types.h>
  36. #include <linux/string.h>
  37. #include <linux/configfs.h>
  38. #include <linux/ctype.h>
  39. #include <linux/hash.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/target_core_base.h>
  54. #include <target/configfs_macros.h>
  55. #include "tcm_fc.h"
  56. /*
  57. * Deliver read data back to initiator.
  58. * XXX TBD handle resource problems later.
  59. */
  60. int ft_queue_data_in(struct se_cmd *se_cmd)
  61. {
  62. struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
  63. struct fc_frame *fp = NULL;
  64. struct fc_exch *ep;
  65. struct fc_lport *lport;
  66. struct se_mem *mem;
  67. size_t remaining;
  68. u32 f_ctl = FC_FC_EX_CTX | FC_FC_REL_OFF;
  69. u32 mem_off;
  70. u32 fh_off = 0;
  71. u32 frame_off = 0;
  72. size_t frame_len = 0;
  73. size_t mem_len;
  74. size_t tlen;
  75. size_t off_in_page;
  76. struct page *page;
  77. int use_sg;
  78. int error;
  79. void *page_addr;
  80. void *from;
  81. void *to = NULL;
  82. ep = fc_seq_exch(cmd->seq);
  83. lport = ep->lp;
  84. cmd->seq = lport->tt.seq_start_next(cmd->seq);
  85. remaining = se_cmd->data_length;
  86. /*
  87. * Setup to use first mem list entry, unless no data.
  88. */
  89. BUG_ON(remaining && list_empty(&se_cmd->t_mem_list));
  90. if (remaining) {
  91. mem = list_first_entry(&se_cmd->t_mem_list,
  92. struct se_mem, se_list);
  93. mem_len = mem->se_len;
  94. mem_off = mem->se_off;
  95. page = mem->se_page;
  96. }
  97. /* no scatter/gather in skb for odd word length due to fc_seq_send() */
  98. use_sg = !(remaining % 4);
  99. while (remaining) {
  100. if (!mem_len) {
  101. BUG_ON(!mem);
  102. mem = list_entry(mem->se_list.next,
  103. struct se_mem, se_list);
  104. mem_len = min((size_t)mem->se_len, remaining);
  105. mem_off = mem->se_off;
  106. page = mem->se_page;
  107. }
  108. if (!frame_len) {
  109. /*
  110. * If lport's has capability of Large Send Offload LSO)
  111. * , then allow 'frame_len' to be as big as 'lso_max'
  112. * if indicated transfer length is >= lport->lso_max
  113. */
  114. frame_len = (lport->seq_offload) ? lport->lso_max :
  115. cmd->sess->max_frame;
  116. frame_len = min(frame_len, remaining);
  117. fp = fc_frame_alloc(lport, use_sg ? 0 : frame_len);
  118. if (!fp)
  119. return -ENOMEM;
  120. to = fc_frame_payload_get(fp, 0);
  121. fh_off = frame_off;
  122. frame_off += frame_len;
  123. /*
  124. * Setup the frame's max payload which is used by base
  125. * driver to indicate HW about max frame size, so that
  126. * HW can do fragmentation appropriately based on
  127. * "gso_max_size" of underline netdev.
  128. */
  129. fr_max_payload(fp) = cmd->sess->max_frame;
  130. }
  131. tlen = min(mem_len, frame_len);
  132. if (use_sg) {
  133. off_in_page = mem_off;
  134. BUG_ON(!page);
  135. get_page(page);
  136. skb_fill_page_desc(fp_skb(fp),
  137. skb_shinfo(fp_skb(fp))->nr_frags,
  138. page, off_in_page, tlen);
  139. fr_len(fp) += tlen;
  140. fp_skb(fp)->data_len += tlen;
  141. fp_skb(fp)->truesize +=
  142. PAGE_SIZE << compound_order(page);
  143. } else {
  144. BUG_ON(!page);
  145. from = kmap_atomic(page + (mem_off >> PAGE_SHIFT),
  146. KM_SOFTIRQ0);
  147. page_addr = from;
  148. from += mem_off & ~PAGE_MASK;
  149. tlen = min(tlen, (size_t)(PAGE_SIZE -
  150. (mem_off & ~PAGE_MASK)));
  151. memcpy(to, from, tlen);
  152. kunmap_atomic(page_addr, KM_SOFTIRQ0);
  153. to += tlen;
  154. }
  155. mem_off += tlen;
  156. mem_len -= tlen;
  157. frame_len -= tlen;
  158. remaining -= tlen;
  159. if (frame_len &&
  160. (skb_shinfo(fp_skb(fp))->nr_frags < FC_FRAME_SG_LEN))
  161. continue;
  162. if (!remaining)
  163. f_ctl |= FC_FC_END_SEQ;
  164. fc_fill_fc_hdr(fp, FC_RCTL_DD_SOL_DATA, ep->did, ep->sid,
  165. FC_TYPE_FCP, f_ctl, fh_off);
  166. error = lport->tt.seq_send(lport, cmd->seq, fp);
  167. if (error) {
  168. /* XXX For now, initiator will retry */
  169. if (printk_ratelimit())
  170. printk(KERN_ERR "%s: Failed to send frame %p, "
  171. "xid <0x%x>, remaining %zu, "
  172. "lso_max <0x%x>\n",
  173. __func__, fp, ep->xid,
  174. remaining, lport->lso_max);
  175. }
  176. }
  177. return ft_queue_status(se_cmd);
  178. }
  179. /*
  180. * Receive write data frame.
  181. */
  182. void ft_recv_write_data(struct ft_cmd *cmd, struct fc_frame *fp)
  183. {
  184. struct se_cmd *se_cmd = &cmd->se_cmd;
  185. struct fc_seq *seq = cmd->seq;
  186. struct fc_exch *ep;
  187. struct fc_lport *lport;
  188. struct fc_frame_header *fh;
  189. struct se_mem *mem;
  190. u32 mem_off;
  191. u32 rel_off;
  192. size_t frame_len;
  193. size_t mem_len;
  194. size_t tlen;
  195. struct page *page;
  196. void *page_addr;
  197. void *from;
  198. void *to;
  199. u32 f_ctl;
  200. void *buf;
  201. fh = fc_frame_header_get(fp);
  202. if (!(ntoh24(fh->fh_f_ctl) & FC_FC_REL_OFF))
  203. goto drop;
  204. /*
  205. * Doesn't expect even single byte of payload. Payload
  206. * is expected to be copied directly to user buffers
  207. * due to DDP (Large Rx offload) feature, hence
  208. * BUG_ON if BUF is non-NULL
  209. */
  210. buf = fc_frame_payload_get(fp, 1);
  211. if (cmd->was_ddp_setup && buf) {
  212. printk(KERN_INFO "%s: When DDP was setup, not expected to"
  213. "receive frame with payload, Payload shall be"
  214. "copied directly to buffer instead of coming "
  215. "via. legacy receive queues\n", __func__);
  216. BUG_ON(buf);
  217. }
  218. /*
  219. * If ft_cmd indicated 'ddp_setup', in that case only the last frame
  220. * should come with 'TSI bit being set'. If 'TSI bit is not set and if
  221. * data frame appears here, means error condition. In both the cases
  222. * release the DDP context (ddp_put) and in error case, as well
  223. * initiate error recovery mechanism.
  224. */
  225. ep = fc_seq_exch(seq);
  226. if (cmd->was_ddp_setup) {
  227. BUG_ON(!ep);
  228. lport = ep->lp;
  229. BUG_ON(!lport);
  230. }
  231. if (cmd->was_ddp_setup && ep->xid != FC_XID_UNKNOWN) {
  232. f_ctl = ntoh24(fh->fh_f_ctl);
  233. /*
  234. * If TSI bit set in f_ctl, means last write data frame is
  235. * received successfully where payload is posted directly
  236. * to user buffer and only the last frame's header is posted
  237. * in legacy receive queue
  238. */
  239. if (f_ctl & FC_FC_SEQ_INIT) { /* TSI bit set in FC frame */
  240. cmd->write_data_len = lport->tt.ddp_done(lport,
  241. ep->xid);
  242. goto last_frame;
  243. } else {
  244. /*
  245. * Updating the write_data_len may be meaningless at
  246. * this point, but just in case if required in future
  247. * for debugging or any other purpose
  248. */
  249. printk(KERN_ERR "%s: Received frame with TSI bit not"
  250. " being SET, dropping the frame, "
  251. "cmd->sg <%p>, cmd->sg_cnt <0x%x>\n",
  252. __func__, cmd->sg, cmd->sg_cnt);
  253. cmd->write_data_len = lport->tt.ddp_done(lport,
  254. ep->xid);
  255. lport->tt.seq_exch_abort(cmd->seq, 0);
  256. goto drop;
  257. }
  258. }
  259. rel_off = ntohl(fh->fh_parm_offset);
  260. frame_len = fr_len(fp);
  261. if (frame_len <= sizeof(*fh))
  262. goto drop;
  263. frame_len -= sizeof(*fh);
  264. from = fc_frame_payload_get(fp, 0);
  265. if (rel_off >= se_cmd->data_length)
  266. goto drop;
  267. if (frame_len + rel_off > se_cmd->data_length)
  268. frame_len = se_cmd->data_length - rel_off;
  269. /*
  270. * Setup to use first mem list entry, unless no data.
  271. */
  272. BUG_ON(frame_len && list_empty(&se_cmd->t_mem_list));
  273. if (frame_len) {
  274. mem = list_first_entry(&se_cmd->t_mem_list,
  275. struct se_mem, se_list);
  276. mem_len = mem->se_len;
  277. mem_off = mem->se_off;
  278. page = mem->se_page;
  279. }
  280. while (frame_len) {
  281. if (!mem_len) {
  282. BUG_ON(!mem);
  283. mem = list_entry(mem->se_list.next,
  284. struct se_mem, se_list);
  285. mem_len = mem->se_len;
  286. mem_off = mem->se_off;
  287. page = mem->se_page;
  288. }
  289. if (rel_off >= mem_len) {
  290. rel_off -= mem_len;
  291. mem_len = 0;
  292. continue;
  293. }
  294. mem_off += rel_off;
  295. mem_len -= rel_off;
  296. rel_off = 0;
  297. tlen = min(mem_len, frame_len);
  298. to = kmap_atomic(page + (mem_off >> PAGE_SHIFT),
  299. KM_SOFTIRQ0);
  300. page_addr = to;
  301. to += mem_off & ~PAGE_MASK;
  302. tlen = min(tlen, (size_t)(PAGE_SIZE -
  303. (mem_off & ~PAGE_MASK)));
  304. memcpy(to, from, tlen);
  305. kunmap_atomic(page_addr, KM_SOFTIRQ0);
  306. from += tlen;
  307. frame_len -= tlen;
  308. mem_off += tlen;
  309. mem_len -= tlen;
  310. cmd->write_data_len += tlen;
  311. }
  312. last_frame:
  313. if (cmd->write_data_len == se_cmd->data_length)
  314. transport_generic_handle_data(se_cmd);
  315. drop:
  316. fc_frame_free(fp);
  317. }