Merge branch 'master' into for-next
[cascardo/linux.git] / drivers / target / tcm_fc / tfc_io.c
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
27 /* XXX TBD some includes may be extraneous */
28
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
31 #include <generated/utsrelease.h>
32 #include <linux/utsname.h>
33 #include <linux/init.h>
34 #include <linux/slab.h>
35 #include <linux/kthread.h>
36 #include <linux/types.h>
37 #include <linux/string.h>
38 #include <linux/configfs.h>
39 #include <linux/ctype.h>
40 #include <linux/hash.h>
41 #include <linux/ratelimit.h>
42 #include <asm/unaligned.h>
43 #include <scsi/scsi.h>
44 #include <scsi/scsi_host.h>
45 #include <scsi/scsi_device.h>
46 #include <scsi/scsi_cmnd.h>
47 #include <scsi/libfc.h>
48 #include <scsi/fc_encode.h>
49
50 #include <target/target_core_base.h>
51 #include <target/target_core_transport.h>
52 #include <target/target_core_fabric_ops.h>
53 #include <target/target_core_device.h>
54 #include <target/target_core_tpg.h>
55 #include <target/target_core_configfs.h>
56 #include <target/configfs_macros.h>
57
58 #include "tcm_fc.h"
59
60 /*
61  * Deliver read data back to initiator.
62  * XXX TBD handle resource problems later.
63  */
64 int ft_queue_data_in(struct se_cmd *se_cmd)
65 {
66         struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd);
67         struct fc_frame *fp = NULL;
68         struct fc_exch *ep;
69         struct fc_lport *lport;
70         struct scatterlist *sg = NULL;
71         size_t remaining;
72         u32 f_ctl = FC_FC_EX_CTX | FC_FC_REL_OFF;
73         u32 mem_off = 0;
74         u32 fh_off = 0;
75         u32 frame_off = 0;
76         size_t frame_len = 0;
77         size_t mem_len = 0;
78         size_t tlen;
79         size_t off_in_page;
80         struct page *page = NULL;
81         int use_sg;
82         int error;
83         void *page_addr;
84         void *from;
85         void *to = NULL;
86
87         ep = fc_seq_exch(cmd->seq);
88         lport = ep->lp;
89         cmd->seq = lport->tt.seq_start_next(cmd->seq);
90
91         remaining = se_cmd->data_length;
92
93         /*
94          * Setup to use first mem list entry, unless no data.
95          */
96         BUG_ON(remaining && !se_cmd->t_data_sg);
97         if (remaining) {
98                 sg = se_cmd->t_data_sg;
99                 mem_len = sg->length;
100                 mem_off = sg->offset;
101                 page = sg_page(sg);
102         }
103
104         /* no scatter/gather in skb for odd word length due to fc_seq_send() */
105         use_sg = !(remaining % 4);
106
107         while (remaining) {
108                 if (!mem_len) {
109                         sg = sg_next(sg);
110                         mem_len = min((size_t)sg->length, remaining);
111                         mem_off = sg->offset;
112                         page = sg_page(sg);
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
139                 if (use_sg) {
140                         off_in_page = mem_off;
141                         BUG_ON(!page);
142                         get_page(page);
143                         skb_fill_page_desc(fp_skb(fp),
144                                            skb_shinfo(fp_skb(fp))->nr_frags,
145                                            page, off_in_page, tlen);
146                         fr_len(fp) += tlen;
147                         fp_skb(fp)->data_len += tlen;
148                         fp_skb(fp)->truesize +=
149                                         PAGE_SIZE << compound_order(page);
150                 } else {
151                         BUG_ON(!page);
152                         from = kmap_atomic(page + (mem_off >> PAGE_SHIFT),
153                                            KM_SOFTIRQ0);
154                         page_addr = from;
155                         from += mem_off & ~PAGE_MASK;
156                         tlen = min(tlen, (size_t)(PAGE_SIZE -
157                                                 (mem_off & ~PAGE_MASK)));
158                         memcpy(to, from, tlen);
159                         kunmap_atomic(page_addr, KM_SOFTIRQ0);
160                         to += tlen;
161                 }
162
163                 mem_off += tlen;
164                 mem_len -= tlen;
165                 frame_len -= tlen;
166                 remaining -= tlen;
167
168                 if (frame_len &&
169                     (skb_shinfo(fp_skb(fp))->nr_frags < FC_FRAME_SG_LEN))
170                         continue;
171                 if (!remaining)
172                         f_ctl |= FC_FC_END_SEQ;
173                 fc_fill_fc_hdr(fp, FC_RCTL_DD_SOL_DATA, ep->did, ep->sid,
174                                FC_TYPE_FCP, f_ctl, fh_off);
175                 error = lport->tt.seq_send(lport, cmd->seq, fp);
176                 if (error) {
177                         /* XXX For now, initiator will retry */
178                         pr_err_ratelimited("%s: Failed to send frame %p, "
179                                                 "xid <0x%x>, remaining %zu, "
180                                                 "lso_max <0x%x>\n",
181                                                 __func__, fp, ep->xid,
182                                                 remaining, lport->lso_max);
183                 }
184         }
185         return ft_queue_status(se_cmd);
186 }
187
188 /*
189  * Receive write data frame.
190  */
191 void ft_recv_write_data(struct ft_cmd *cmd, struct fc_frame *fp)
192 {
193         struct se_cmd *se_cmd = &cmd->se_cmd;
194         struct fc_seq *seq = cmd->seq;
195         struct fc_exch *ep;
196         struct fc_lport *lport;
197         struct fc_frame_header *fh;
198         struct scatterlist *sg = NULL;
199         u32 mem_off = 0;
200         u32 rel_off;
201         size_t frame_len;
202         size_t mem_len = 0;
203         size_t tlen;
204         struct page *page = NULL;
205         void *page_addr;
206         void *from;
207         void *to;
208         u32 f_ctl;
209         void *buf;
210
211         fh = fc_frame_header_get(fp);
212         if (!(ntoh24(fh->fh_f_ctl) & FC_FC_REL_OFF))
213                 goto drop;
214
215         f_ctl = ntoh24(fh->fh_f_ctl);
216         ep = fc_seq_exch(seq);
217         lport = ep->lp;
218         if (cmd->was_ddp_setup) {
219                 BUG_ON(!ep);
220                 BUG_ON(!lport);
221         }
222
223         /*
224          * Doesn't expect payload if DDP is setup. Payload
225          * is expected to be copied directly to user buffers
226          * due to DDP (Large Rx offload),
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                                 cmd->sg, cmd->sg_cnt);
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         /*
245          * If "Sequence Initiative (TSI)" bit set in f_ctl, means last
246          * write data frame is received successfully where payload is
247          * posted directly to user buffer and only the last frame's
248          * header is posted in receive queue.
249          *
250          * If "Sequence Initiative (TSI)" bit is not set, means error
251          * condition w.r.t. DDP, hence drop the packet and let explict
252          * ABORTS from other end of exchange timer trigger the recovery.
253          */
254         if (f_ctl & FC_FC_SEQ_INIT)
255                 goto last_frame;
256         else
257                 goto drop;
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         /*
271          * Setup to use first mem list entry, unless no data.
272          */
273         BUG_ON(frame_len && !se_cmd->t_data_sg);
274         if (frame_len) {
275                 sg = se_cmd->t_data_sg;
276                 mem_len = sg->length;
277                 mem_off = sg->offset;
278                 page = sg_page(sg);
279         }
280
281         while (frame_len) {
282                 if (!mem_len) {
283                         sg = sg_next(sg);
284                         mem_len = sg->length;
285                         mem_off = sg->offset;
286                         page = sg_page(sg);
287                 }
288                 if (rel_off >= mem_len) {
289                         rel_off -= mem_len;
290                         mem_len = 0;
291                         continue;
292                 }
293                 mem_off += rel_off;
294                 mem_len -= rel_off;
295                 rel_off = 0;
296
297                 tlen = min(mem_len, frame_len);
298
299                 to = kmap_atomic(page + (mem_off >> PAGE_SHIFT),
300                                  KM_SOFTIRQ0);
301                 page_addr = to;
302                 to += mem_off & ~PAGE_MASK;
303                 tlen = min(tlen, (size_t)(PAGE_SIZE -
304                                           (mem_off & ~PAGE_MASK)));
305                 memcpy(to, from, tlen);
306                 kunmap_atomic(page_addr, KM_SOFTIRQ0);
307
308                 from += tlen;
309                 frame_len -= tlen;
310                 mem_off += tlen;
311                 mem_len -= tlen;
312                 cmd->write_data_len += tlen;
313         }
314 last_frame:
315         if (cmd->write_data_len == se_cmd->data_length)
316                 transport_generic_handle_data(se_cmd);
317 drop:
318         fc_frame_free(fp);
319 }
320
321 /*
322  * Handle and cleanup any HW specific resources if
323  * received ABORTS, errors, timeouts.
324  */
325 void ft_invl_hw_context(struct ft_cmd *cmd)
326 {
327         struct fc_seq *seq = cmd->seq;
328         struct fc_exch *ep = NULL;
329         struct fc_lport *lport = NULL;
330
331         BUG_ON(!cmd);
332
333         /* Cleanup the DDP context in HW if DDP was setup */
334         if (cmd->was_ddp_setup && seq) {
335                 ep = fc_seq_exch(seq);
336                 if (ep) {
337                         lport = ep->lp;
338                         if (lport && (ep->xid <= lport->lro_xid))
339                                 /*
340                                  * "ddp_done" trigger invalidation of HW
341                                  * specific DDP context
342                                  */
343                                 cmd->write_data_len = lport->tt.ddp_done(lport,
344                                                                       ep->xid);
345
346                                 /*
347                                  * Resetting same variable to indicate HW's
348                                  * DDP context has been invalidated to avoid
349                                  * re_invalidation of same context (context is
350                                  * identified using ep->xid)
351                                  */
352                                 cmd->was_ddp_setup = 0;
353                 }
354         }
355 }