tfc_io.c 9.8 KB

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