blob: a254792d882cc1541578436f5588bfeda59ccf15 [file] [log] [blame]
Thomas Gleixnera61127c2019-05-29 16:57:49 -07001// SPDX-License-Identifier: GPL-2.0-only
Kiran Patil3699d922011-04-18 16:24:14 -07002/*
3 * Copyright (c) 2010 Cisco Systems, Inc.
4 *
5 * Portions based on tcm_loop_fabric_scsi.c and libfc/fc_fcp.c
6 *
7 * Copyright (c) 2007 Intel Corporation. All rights reserved.
8 * Copyright (c) 2008 Red Hat, Inc. All rights reserved.
9 * Copyright (c) 2008 Mike Christie
10 * Copyright (c) 2009 Rising Tide, Inc.
11 * Copyright (c) 2009 Linux-iSCSI.org
12 * Copyright (c) 2009 Nicholas A. Bellinger <nab@linux-iscsi.org>
Kiran Patil3699d922011-04-18 16:24:14 -070013 */
14
15/* XXX TBD some includes may be extraneous */
16
17#include <linux/module.h>
18#include <linux/moduleparam.h>
Kiran Patil3699d922011-04-18 16:24:14 -070019#include <linux/utsname.h>
20#include <linux/init.h>
21#include <linux/slab.h>
22#include <linux/kthread.h>
23#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/configfs.h>
26#include <linux/ctype.h>
27#include <linux/hash.h>
Andy Grover6708bb22011-06-08 10:36:43 -070028#include <linux/ratelimit.h>
Kiran Patil3699d922011-04-18 16:24:14 -070029#include <asm/unaligned.h>
Kiran Patil3699d922011-04-18 16:24:14 -070030#include <scsi/libfc.h>
31#include <scsi/fc_encode.h>
32
33#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050034#include <target/target_core_fabric.h>
Kiran Patil3699d922011-04-18 16:24:14 -070035
36#include "tcm_fc.h"
37
38/*
39 * Deliver read data back to initiator.
40 * XXX TBD handle resource problems later.
41 */
42int ft_queue_data_in(struct se_cmd *se_cmd)
43{
44 struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
Kiran Patil3699d922011-04-18 16:24:14 -070045 struct fc_frame *fp = NULL;
46 struct fc_exch *ep;
47 struct fc_lport *lport;
Andy Groverec98f782011-07-20 19:28:46 +000048 struct scatterlist *sg = NULL;
Kiran Patil3699d922011-04-18 16:24:14 -070049 size_t remaining;
50 u32 f_ctl = FC_FC_EX_CTX | FC_FC_REL_OFF;
Andy Groverec98f782011-07-20 19:28:46 +000051 u32 mem_off = 0;
Kiran Patil3699d922011-04-18 16:24:14 -070052 u32 fh_off = 0;
53 u32 frame_off = 0;
54 size_t frame_len = 0;
Andy Groverec98f782011-07-20 19:28:46 +000055 size_t mem_len = 0;
Kiran Patil3699d922011-04-18 16:24:14 -070056 size_t tlen;
57 size_t off_in_page;
Andy Groverec98f782011-07-20 19:28:46 +000058 struct page *page = NULL;
Kiran Patil3699d922011-04-18 16:24:14 -070059 int use_sg;
60 int error;
61 void *page_addr;
62 void *from;
63 void *to = NULL;
64
Mark Rustade1c40382012-04-03 10:24:41 -070065 if (cmd->aborted)
66 return 0;
Nicholas Bellingerb3e5fe12014-06-05 16:27:53 -070067
68 if (se_cmd->scsi_status == SAM_STAT_TASK_SET_FULL)
69 goto queue_status;
70
Kiran Patil3699d922011-04-18 16:24:14 -070071 ep = fc_seq_exch(cmd->seq);
72 lport = ep->lp;
Hannes Reineckec6865b32016-10-18 10:01:51 +020073 cmd->seq = fc_seq_start_next(cmd->seq);
Kiran Patil3699d922011-04-18 16:24:14 -070074
Kiran Patil3699d922011-04-18 16:24:14 -070075 remaining = se_cmd->data_length;
76
77 /*
Andy Grover05d1c7c2011-07-20 19:13:28 +000078 * Setup to use first mem list entry, unless no data.
Kiran Patil3699d922011-04-18 16:24:14 -070079 */
Andy Groverec98f782011-07-20 19:28:46 +000080 BUG_ON(remaining && !se_cmd->t_data_sg);
Andy Grover05d1c7c2011-07-20 19:13:28 +000081 if (remaining) {
Andy Groverec98f782011-07-20 19:28:46 +000082 sg = se_cmd->t_data_sg;
83 mem_len = sg->length;
84 mem_off = sg->offset;
85 page = sg_page(sg);
Kiran Patil3699d922011-04-18 16:24:14 -070086 }
87
88 /* no scatter/gather in skb for odd word length due to fc_seq_send() */
89 use_sg = !(remaining % 4);
90
91 while (remaining) {
Mark Rustadd3682b12013-04-22 09:49:55 -070092 struct fc_seq *seq = cmd->seq;
93
94 if (!seq) {
95 pr_debug("%s: Command aborted, xid 0x%x\n",
96 __func__, ep->xid);
97 break;
98 }
Kiran Patil3699d922011-04-18 16:24:14 -070099 if (!mem_len) {
Andy Groverec98f782011-07-20 19:28:46 +0000100 sg = sg_next(sg);
101 mem_len = min((size_t)sg->length, remaining);
102 mem_off = sg->offset;
103 page = sg_page(sg);
Kiran Patil3699d922011-04-18 16:24:14 -0700104 }
105 if (!frame_len) {
106 /*
107 * If lport's has capability of Large Send Offload LSO)
108 * , then allow 'frame_len' to be as big as 'lso_max'
109 * if indicated transfer length is >= lport->lso_max
110 */
111 frame_len = (lport->seq_offload) ? lport->lso_max :
112 cmd->sess->max_frame;
113 frame_len = min(frame_len, remaining);
114 fp = fc_frame_alloc(lport, use_sg ? 0 : frame_len);
115 if (!fp)
116 return -ENOMEM;
117 to = fc_frame_payload_get(fp, 0);
118 fh_off = frame_off;
119 frame_off += frame_len;
120 /*
121 * Setup the frame's max payload which is used by base
122 * driver to indicate HW about max frame size, so that
123 * HW can do fragmentation appropriately based on
124 * "gso_max_size" of underline netdev.
125 */
126 fr_max_payload(fp) = cmd->sess->max_frame;
127 }
128 tlen = min(mem_len, frame_len);
129
130 if (use_sg) {
Andy Grover05d1c7c2011-07-20 19:13:28 +0000131 off_in_page = mem_off;
Kiran Patil3699d922011-04-18 16:24:14 -0700132 BUG_ON(!page);
133 get_page(page);
134 skb_fill_page_desc(fp_skb(fp),
135 skb_shinfo(fp_skb(fp))->nr_frags,
136 page, off_in_page, tlen);
137 fr_len(fp) += tlen;
138 fp_skb(fp)->data_len += tlen;
139 fp_skb(fp)->truesize +=
140 PAGE_SIZE << compound_order(page);
Andy Grover05d1c7c2011-07-20 19:13:28 +0000141 } else {
Kiran Patil3699d922011-04-18 16:24:14 -0700142 BUG_ON(!page);
Cong Wangca747d62011-11-25 23:14:25 +0800143 from = kmap_atomic(page + (mem_off >> PAGE_SHIFT));
Kiran Patil3699d922011-04-18 16:24:14 -0700144 page_addr = from;
Geliang Tangb75d8062015-11-25 21:49:27 +0800145 from += offset_in_page(mem_off);
Kiran Patil3699d922011-04-18 16:24:14 -0700146 tlen = min(tlen, (size_t)(PAGE_SIZE -
Geliang Tangb75d8062015-11-25 21:49:27 +0800147 offset_in_page(mem_off)));
Kiran Patil3699d922011-04-18 16:24:14 -0700148 memcpy(to, from, tlen);
Cong Wangca747d62011-11-25 23:14:25 +0800149 kunmap_atomic(page_addr);
Kiran Patil3699d922011-04-18 16:24:14 -0700150 to += tlen;
Kiran Patil3699d922011-04-18 16:24:14 -0700151 }
152
153 mem_off += tlen;
154 mem_len -= tlen;
155 frame_len -= tlen;
156 remaining -= tlen;
157
158 if (frame_len &&
159 (skb_shinfo(fp_skb(fp))->nr_frags < FC_FRAME_SG_LEN))
160 continue;
161 if (!remaining)
162 f_ctl |= FC_FC_END_SEQ;
163 fc_fill_fc_hdr(fp, FC_RCTL_DD_SOL_DATA, ep->did, ep->sid,
164 FC_TYPE_FCP, f_ctl, fh_off);
Hannes Reinecke0cac9372016-10-18 10:01:48 +0200165 error = fc_seq_send(lport, seq, fp);
Kiran Patil3699d922011-04-18 16:24:14 -0700166 if (error) {
Nicholas Bellingerb3e5fe12014-06-05 16:27:53 -0700167 pr_info_ratelimited("%s: Failed to send frame %p, "
Nicholas Bellinger95efa282011-06-23 23:28:46 +0000168 "xid <0x%x>, remaining %zu, "
Kiran Patil3699d922011-04-18 16:24:14 -0700169 "lso_max <0x%x>\n",
170 __func__, fp, ep->xid,
171 remaining, lport->lso_max);
Nicholas Bellingerb3e5fe12014-06-05 16:27:53 -0700172 /*
173 * Go ahead and set TASK_SET_FULL status ignoring the
174 * rest of the DataIN, and immediately attempt to
175 * send the response via ft_queue_status() in order
176 * to notify the initiator that it should reduce it's
177 * per LUN queue_depth.
178 */
179 se_cmd->scsi_status = SAM_STAT_TASK_SET_FULL;
180 break;
Kiran Patil3699d922011-04-18 16:24:14 -0700181 }
182 }
Nicholas Bellingerb3e5fe12014-06-05 16:27:53 -0700183queue_status:
Kiran Patil3699d922011-04-18 16:24:14 -0700184 return ft_queue_status(se_cmd);
185}
186
Christoph Hellwigb8b22532012-07-08 15:58:45 -0400187static void ft_execute_work(struct work_struct *work)
188{
189 struct ft_cmd *cmd = container_of(work, struct ft_cmd, work);
190
191 target_execute_cmd(&cmd->se_cmd);
192}
193
Kiran Patil3699d922011-04-18 16:24:14 -0700194/*
195 * Receive write data frame.
196 */
197void ft_recv_write_data(struct ft_cmd *cmd, struct fc_frame *fp)
198{
199 struct se_cmd *se_cmd = &cmd->se_cmd;
200 struct fc_seq *seq = cmd->seq;
201 struct fc_exch *ep;
202 struct fc_lport *lport;
Kiran Patil3699d922011-04-18 16:24:14 -0700203 struct fc_frame_header *fh;
Andy Groverec98f782011-07-20 19:28:46 +0000204 struct scatterlist *sg = NULL;
205 u32 mem_off = 0;
Kiran Patil3699d922011-04-18 16:24:14 -0700206 u32 rel_off;
207 size_t frame_len;
Andy Groverec98f782011-07-20 19:28:46 +0000208 size_t mem_len = 0;
Kiran Patil3699d922011-04-18 16:24:14 -0700209 size_t tlen;
Andy Groverec98f782011-07-20 19:28:46 +0000210 struct page *page = NULL;
Kiran Patil3699d922011-04-18 16:24:14 -0700211 void *page_addr;
212 void *from;
213 void *to;
214 u32 f_ctl;
215 void *buf;
216
Kiran Patil3699d922011-04-18 16:24:14 -0700217 fh = fc_frame_header_get(fp);
218 if (!(ntoh24(fh->fh_f_ctl) & FC_FC_REL_OFF))
219 goto drop;
220
Kiran Patildcd998c2011-08-03 09:20:01 +0000221 f_ctl = ntoh24(fh->fh_f_ctl);
222 ep = fc_seq_exch(seq);
223 lport = ep->lp;
224 if (cmd->was_ddp_setup) {
225 BUG_ON(!ep);
226 BUG_ON(!lport);
Kiran Patil079587b2011-08-26 09:25:25 -0700227 /*
228 * Since DDP (Large Rx offload) was setup for this request,
229 * payload is expected to be copied directly to user buffers.
230 */
231 buf = fc_frame_payload_get(fp, 1);
232 if (buf)
233 pr_err("%s: xid 0x%x, f_ctl 0x%x, cmd->sg %p, "
Kiran Patildcd998c2011-08-03 09:20:01 +0000234 "cmd->sg_cnt 0x%x. DDP was setup"
235 " hence not expected to receive frame with "
Kiran Patil079587b2011-08-26 09:25:25 -0700236 "payload, Frame will be dropped if"
237 "'Sequence Initiative' bit in f_ctl is"
Kiran Patildcd998c2011-08-03 09:20:01 +0000238 "not set\n", __func__, ep->xid, f_ctl,
Roland Dreiere182d682012-03-30 11:29:11 -0700239 se_cmd->t_data_sg, se_cmd->t_data_nents);
Kiran Patil079587b2011-08-26 09:25:25 -0700240 /*
241 * Invalidate HW DDP context if it was setup for respective
242 * command. Invalidation of HW DDP context is requited in both
243 * situation (success and error).
244 */
245 ft_invl_hw_context(cmd);
Kiran Patildcd998c2011-08-03 09:20:01 +0000246
Kiran Patil079587b2011-08-26 09:25:25 -0700247 /*
248 * If "Sequence Initiative (TSI)" bit set in f_ctl, means last
249 * write data frame is received successfully where payload is
250 * posted directly to user buffer and only the last frame's
251 * header is posted in receive queue.
252 *
253 * If "Sequence Initiative (TSI)" bit is not set, means error
254 * condition w.r.t. DDP, hence drop the packet and let explict
255 * ABORTS from other end of exchange timer trigger the recovery.
256 */
257 if (f_ctl & FC_FC_SEQ_INIT)
258 goto last_frame;
259 else
260 goto drop;
261 }
Kiran Patil3699d922011-04-18 16:24:14 -0700262
263 rel_off = ntohl(fh->fh_parm_offset);
264 frame_len = fr_len(fp);
265 if (frame_len <= sizeof(*fh))
266 goto drop;
267 frame_len -= sizeof(*fh);
268 from = fc_frame_payload_get(fp, 0);
269 if (rel_off >= se_cmd->data_length)
270 goto drop;
271 if (frame_len + rel_off > se_cmd->data_length)
272 frame_len = se_cmd->data_length - rel_off;
273
274 /*
Andy Grover05d1c7c2011-07-20 19:13:28 +0000275 * Setup to use first mem list entry, unless no data.
Kiran Patil3699d922011-04-18 16:24:14 -0700276 */
Andy Groverec98f782011-07-20 19:28:46 +0000277 BUG_ON(frame_len && !se_cmd->t_data_sg);
Andy Grover05d1c7c2011-07-20 19:13:28 +0000278 if (frame_len) {
Andy Groverec98f782011-07-20 19:28:46 +0000279 sg = se_cmd->t_data_sg;
280 mem_len = sg->length;
281 mem_off = sg->offset;
282 page = sg_page(sg);
Kiran Patil3699d922011-04-18 16:24:14 -0700283 }
284
285 while (frame_len) {
286 if (!mem_len) {
Andy Groverec98f782011-07-20 19:28:46 +0000287 sg = sg_next(sg);
288 mem_len = sg->length;
289 mem_off = sg->offset;
290 page = sg_page(sg);
Kiran Patil3699d922011-04-18 16:24:14 -0700291 }
292 if (rel_off >= mem_len) {
293 rel_off -= mem_len;
294 mem_len = 0;
295 continue;
296 }
297 mem_off += rel_off;
298 mem_len -= rel_off;
299 rel_off = 0;
300
301 tlen = min(mem_len, frame_len);
302
Cong Wangca747d62011-11-25 23:14:25 +0800303 to = kmap_atomic(page + (mem_off >> PAGE_SHIFT));
Andy Grover05d1c7c2011-07-20 19:13:28 +0000304 page_addr = to;
Geliang Tangb75d8062015-11-25 21:49:27 +0800305 to += offset_in_page(mem_off);
Andy Grover05d1c7c2011-07-20 19:13:28 +0000306 tlen = min(tlen, (size_t)(PAGE_SIZE -
Geliang Tangb75d8062015-11-25 21:49:27 +0800307 offset_in_page(mem_off)));
Andy Grover05d1c7c2011-07-20 19:13:28 +0000308 memcpy(to, from, tlen);
Cong Wangca747d62011-11-25 23:14:25 +0800309 kunmap_atomic(page_addr);
Andy Grover05d1c7c2011-07-20 19:13:28 +0000310
Kiran Patil3699d922011-04-18 16:24:14 -0700311 from += tlen;
312 frame_len -= tlen;
313 mem_off += tlen;
314 mem_len -= tlen;
315 cmd->write_data_len += tlen;
316 }
317last_frame:
Christoph Hellwigb8b22532012-07-08 15:58:45 -0400318 if (cmd->write_data_len == se_cmd->data_length) {
319 INIT_WORK(&cmd->work, ft_execute_work);
320 queue_work(cmd->sess->tport->tpg->workqueue, &cmd->work);
321 }
Kiran Patil3699d922011-04-18 16:24:14 -0700322drop:
323 fc_frame_free(fp);
324}
Kiran Patildcd998c2011-08-03 09:20:01 +0000325
326/*
327 * Handle and cleanup any HW specific resources if
328 * received ABORTS, errors, timeouts.
329 */
330void ft_invl_hw_context(struct ft_cmd *cmd)
331{
Wei Yongjun7875f172012-09-07 14:55:32 +0800332 struct fc_seq *seq;
Kiran Patildcd998c2011-08-03 09:20:01 +0000333 struct fc_exch *ep = NULL;
334 struct fc_lport *lport = NULL;
335
336 BUG_ON(!cmd);
Wei Yongjun7875f172012-09-07 14:55:32 +0800337 seq = cmd->seq;
Kiran Patildcd998c2011-08-03 09:20:01 +0000338
339 /* Cleanup the DDP context in HW if DDP was setup */
340 if (cmd->was_ddp_setup && seq) {
341 ep = fc_seq_exch(seq);
342 if (ep) {
343 lport = ep->lp;
Dan Carpenterd5565462015-02-25 16:21:03 +0300344 if (lport && (ep->xid <= lport->lro_xid)) {
Kiran Patildcd998c2011-08-03 09:20:01 +0000345 /*
346 * "ddp_done" trigger invalidation of HW
347 * specific DDP context
348 */
349 cmd->write_data_len = lport->tt.ddp_done(lport,
350 ep->xid);
351
352 /*
353 * Resetting same variable to indicate HW's
354 * DDP context has been invalidated to avoid
355 * re_invalidation of same context (context is
356 * identified using ep->xid)
357 */
358 cmd->was_ddp_setup = 0;
Dan Carpenterd5565462015-02-25 16:21:03 +0300359 }
Kiran Patildcd998c2011-08-03 09:20:01 +0000360 }
361 }
362}