]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/scsi/libiscsi_tcp.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[karo-tx-linux.git] / drivers / scsi / libiscsi_tcp.c
1 /*
2  * iSCSI over TCP/IP Data-Path lib
3  *
4  * Copyright (C) 2004 Dmitry Yusupov
5  * Copyright (C) 2004 Alex Aizman
6  * Copyright (C) 2005 - 2006 Mike Christie
7  * Copyright (C) 2006 Red Hat, Inc.  All rights reserved.
8  * maintained by open-iscsi@googlegroups.com
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published
12  * by the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details.
19  *
20  * See the file COPYING included with this distribution for more details.
21  *
22  * Credits:
23  *      Christoph Hellwig
24  *      FUJITA Tomonori
25  *      Arne Redlich
26  *      Zhenyu Wang
27  */
28
29 #include <linux/types.h>
30 #include <linux/list.h>
31 #include <linux/inet.h>
32 #include <linux/slab.h>
33 #include <linux/file.h>
34 #include <linux/blkdev.h>
35 #include <linux/crypto.h>
36 #include <linux/delay.h>
37 #include <linux/kfifo.h>
38 #include <linux/scatterlist.h>
39 #include <net/tcp.h>
40 #include <scsi/scsi_cmnd.h>
41 #include <scsi/scsi_device.h>
42 #include <scsi/scsi_host.h>
43 #include <scsi/scsi.h>
44 #include <scsi/scsi_transport_iscsi.h>
45
46 #include "iscsi_tcp.h"
47
48 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
49               "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
50               "Alex Aizman <itn780@yahoo.com>");
51 MODULE_DESCRIPTION("iSCSI/TCP data-path");
52 MODULE_LICENSE("GPL");
53
54 static int iscsi_dbg_libtcp;
55 module_param_named(debug_libiscsi_tcp, iscsi_dbg_libtcp, int,
56                    S_IRUGO | S_IWUSR);
57 MODULE_PARM_DESC(debug_libiscsi_tcp, "Turn on debugging for libiscsi_tcp "
58                  "module. Set to 1 to turn on, and zero to turn off. Default "
59                  "is off.");
60
61 #define ISCSI_DBG_TCP(_conn, dbg_fmt, arg...)                   \
62         do {                                                    \
63                 if (iscsi_dbg_libtcp)                           \
64                         iscsi_conn_printk(KERN_INFO, _conn,     \
65                                              "%s " dbg_fmt,     \
66                                              __func__, ##arg);  \
67         } while (0);
68
69 static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
70                                    struct iscsi_segment *segment);
71
72 /*
73  * Scatterlist handling: inside the iscsi_segment, we
74  * remember an index into the scatterlist, and set data/size
75  * to the current scatterlist entry. For highmem pages, we
76  * kmap as needed.
77  *
78  * Note that the page is unmapped when we return from
79  * TCP's data_ready handler, so we may end up mapping and
80  * unmapping the same page repeatedly. The whole reason
81  * for this is that we shouldn't keep the page mapped
82  * outside the softirq.
83  */
84
85 /**
86  * iscsi_tcp_segment_init_sg - init indicated scatterlist entry
87  * @segment: the buffer object
88  * @sg: scatterlist
89  * @offset: byte offset into that sg entry
90  *
91  * This function sets up the segment so that subsequent
92  * data is copied to the indicated sg entry, at the given
93  * offset.
94  */
95 static inline void
96 iscsi_tcp_segment_init_sg(struct iscsi_segment *segment,
97                           struct scatterlist *sg, unsigned int offset)
98 {
99         segment->sg = sg;
100         segment->sg_offset = offset;
101         segment->size = min(sg->length - offset,
102                             segment->total_size - segment->total_copied);
103         segment->data = NULL;
104 }
105
106 /**
107  * iscsi_tcp_segment_map - map the current S/G page
108  * @segment: iscsi_segment
109  * @recv: 1 if called from recv path
110  *
111  * We only need to possibly kmap data if scatter lists are being used,
112  * because the iscsi passthrough and internal IO paths will never use high
113  * mem pages.
114  */
115 static void iscsi_tcp_segment_map(struct iscsi_segment *segment, int recv)
116 {
117         struct scatterlist *sg;
118
119         if (segment->data != NULL || !segment->sg)
120                 return;
121
122         sg = segment->sg;
123         BUG_ON(segment->sg_mapped);
124         BUG_ON(sg->length == 0);
125
126         /*
127          * If the page count is greater than one it is ok to send
128          * to the network layer's zero copy send path. If not we
129          * have to go the slow sendmsg path. We always map for the
130          * recv path.
131          */
132         if (page_count(sg_page(sg)) >= 1 && !recv)
133                 return;
134
135         segment->sg_mapped = kmap_atomic(sg_page(sg), KM_SOFTIRQ0);
136         segment->data = segment->sg_mapped + sg->offset + segment->sg_offset;
137 }
138
139 void iscsi_tcp_segment_unmap(struct iscsi_segment *segment)
140 {
141         if (segment->sg_mapped) {
142                 kunmap_atomic(segment->sg_mapped, KM_SOFTIRQ0);
143                 segment->sg_mapped = NULL;
144                 segment->data = NULL;
145         }
146 }
147 EXPORT_SYMBOL_GPL(iscsi_tcp_segment_unmap);
148
149 /*
150  * Splice the digest buffer into the buffer
151  */
152 static inline void
153 iscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest)
154 {
155         segment->data = digest;
156         segment->digest_len = ISCSI_DIGEST_SIZE;
157         segment->total_size += ISCSI_DIGEST_SIZE;
158         segment->size = ISCSI_DIGEST_SIZE;
159         segment->copied = 0;
160         segment->sg = NULL;
161         segment->hash = NULL;
162 }
163
164 /**
165  * iscsi_tcp_segment_done - check whether the segment is complete
166  * @tcp_conn: iscsi tcp connection
167  * @segment: iscsi segment to check
168  * @recv: set to one of this is called from the recv path
169  * @copied: number of bytes copied
170  *
171  * Check if we're done receiving this segment. If the receive
172  * buffer is full but we expect more data, move on to the
173  * next entry in the scatterlist.
174  *
175  * If the amount of data we received isn't a multiple of 4,
176  * we will transparently receive the pad bytes, too.
177  *
178  * This function must be re-entrant.
179  */
180 int iscsi_tcp_segment_done(struct iscsi_tcp_conn *tcp_conn,
181                            struct iscsi_segment *segment, int recv,
182                            unsigned copied)
183 {
184         struct scatterlist sg;
185         unsigned int pad;
186
187         ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "copied %u %u size %u %s\n",
188                       segment->copied, copied, segment->size,
189                       recv ? "recv" : "xmit");
190         if (segment->hash && copied) {
191                 /*
192                  * If a segment is kmapd we must unmap it before sending
193                  * to the crypto layer since that will try to kmap it again.
194                  */
195                 iscsi_tcp_segment_unmap(segment);
196
197                 if (!segment->data) {
198                         sg_init_table(&sg, 1);
199                         sg_set_page(&sg, sg_page(segment->sg), copied,
200                                     segment->copied + segment->sg_offset +
201                                                         segment->sg->offset);
202                 } else
203                         sg_init_one(&sg, segment->data + segment->copied,
204                                     copied);
205                 crypto_hash_update(segment->hash, &sg, copied);
206         }
207
208         segment->copied += copied;
209         if (segment->copied < segment->size) {
210                 iscsi_tcp_segment_map(segment, recv);
211                 return 0;
212         }
213
214         segment->total_copied += segment->copied;
215         segment->copied = 0;
216         segment->size = 0;
217
218         /* Unmap the current scatterlist page, if there is one. */
219         iscsi_tcp_segment_unmap(segment);
220
221         /* Do we have more scatterlist entries? */
222         ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "total copied %u total size %u\n",
223                       segment->total_copied, segment->total_size);
224         if (segment->total_copied < segment->total_size) {
225                 /* Proceed to the next entry in the scatterlist. */
226                 iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg),
227                                           0);
228                 iscsi_tcp_segment_map(segment, recv);
229                 BUG_ON(segment->size == 0);
230                 return 0;
231         }
232
233         /* Do we need to handle padding? */
234         if (!(tcp_conn->iscsi_conn->session->tt->caps & CAP_PADDING_OFFLOAD)) {
235                 pad = iscsi_padding(segment->total_copied);
236                 if (pad != 0) {
237                         ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
238                                       "consume %d pad bytes\n", pad);
239                         segment->total_size += pad;
240                         segment->size = pad;
241                         segment->data = segment->padbuf;
242                         return 0;
243                 }
244         }
245
246         /*
247          * Set us up for transferring the data digest. hdr digest
248          * is completely handled in hdr done function.
249          */
250         if (segment->hash) {
251                 crypto_hash_final(segment->hash, segment->digest);
252                 iscsi_tcp_segment_splice_digest(segment,
253                                  recv ? segment->recv_digest : segment->digest);
254                 return 0;
255         }
256
257         return 1;
258 }
259 EXPORT_SYMBOL_GPL(iscsi_tcp_segment_done);
260
261 /**
262  * iscsi_tcp_segment_recv - copy data to segment
263  * @tcp_conn: the iSCSI TCP connection
264  * @segment: the buffer to copy to
265  * @ptr: data pointer
266  * @len: amount of data available
267  *
268  * This function copies up to @len bytes to the
269  * given buffer, and returns the number of bytes
270  * consumed, which can actually be less than @len.
271  *
272  * If hash digest is enabled, the function will update the
273  * hash while copying.
274  * Combining these two operations doesn't buy us a lot (yet),
275  * but in the future we could implement combined copy+crc,
276  * just way we do for network layer checksums.
277  */
278 static int
279 iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn,
280                        struct iscsi_segment *segment, const void *ptr,
281                        unsigned int len)
282 {
283         unsigned int copy = 0, copied = 0;
284
285         while (!iscsi_tcp_segment_done(tcp_conn, segment, 1, copy)) {
286                 if (copied == len) {
287                         ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
288                                       "copied %d bytes\n", len);
289                         break;
290                 }
291
292                 copy = min(len - copied, segment->size - segment->copied);
293                 ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "copying %d\n", copy);
294                 memcpy(segment->data + segment->copied, ptr + copied, copy);
295                 copied += copy;
296         }
297         return copied;
298 }
299
300 inline void
301 iscsi_tcp_dgst_header(struct hash_desc *hash, const void *hdr, size_t hdrlen,
302                       unsigned char digest[ISCSI_DIGEST_SIZE])
303 {
304         struct scatterlist sg;
305
306         sg_init_one(&sg, hdr, hdrlen);
307         crypto_hash_digest(hash, &sg, hdrlen, digest);
308 }
309 EXPORT_SYMBOL_GPL(iscsi_tcp_dgst_header);
310
311 static inline int
312 iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn,
313                       struct iscsi_segment *segment)
314 {
315         if (!segment->digest_len)
316                 return 1;
317
318         if (memcmp(segment->recv_digest, segment->digest,
319                    segment->digest_len)) {
320                 ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "digest mismatch\n");
321                 return 0;
322         }
323
324         return 1;
325 }
326
327 /*
328  * Helper function to set up segment buffer
329  */
330 static inline void
331 __iscsi_segment_init(struct iscsi_segment *segment, size_t size,
332                      iscsi_segment_done_fn_t *done, struct hash_desc *hash)
333 {
334         memset(segment, 0, sizeof(*segment));
335         segment->total_size = size;
336         segment->done = done;
337
338         if (hash) {
339                 segment->hash = hash;
340                 crypto_hash_init(hash);
341         }
342 }
343
344 inline void
345 iscsi_segment_init_linear(struct iscsi_segment *segment, void *data,
346                           size_t size, iscsi_segment_done_fn_t *done,
347                           struct hash_desc *hash)
348 {
349         __iscsi_segment_init(segment, size, done, hash);
350         segment->data = data;
351         segment->size = size;
352 }
353 EXPORT_SYMBOL_GPL(iscsi_segment_init_linear);
354
355 inline int
356 iscsi_segment_seek_sg(struct iscsi_segment *segment,
357                       struct scatterlist *sg_list, unsigned int sg_count,
358                       unsigned int offset, size_t size,
359                       iscsi_segment_done_fn_t *done, struct hash_desc *hash)
360 {
361         struct scatterlist *sg;
362         unsigned int i;
363
364         __iscsi_segment_init(segment, size, done, hash);
365         for_each_sg(sg_list, sg, sg_count, i) {
366                 if (offset < sg->length) {
367                         iscsi_tcp_segment_init_sg(segment, sg, offset);
368                         return 0;
369                 }
370                 offset -= sg->length;
371         }
372
373         return ISCSI_ERR_DATA_OFFSET;
374 }
375 EXPORT_SYMBOL_GPL(iscsi_segment_seek_sg);
376
377 /**
378  * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
379  * @tcp_conn: iscsi connection to prep for
380  *
381  * This function always passes NULL for the hash argument, because when this
382  * function is called we do not yet know the final size of the header and want
383  * to delay the digest processing until we know that.
384  */
385 void iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn)
386 {
387         ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
388                       "(%s)\n", tcp_conn->iscsi_conn->hdrdgst_en ?
389                       "digest enabled" : "digest disabled");
390         iscsi_segment_init_linear(&tcp_conn->in.segment,
391                                 tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr),
392                                 iscsi_tcp_hdr_recv_done, NULL);
393 }
394 EXPORT_SYMBOL_GPL(iscsi_tcp_hdr_recv_prep);
395
396 /*
397  * Handle incoming reply to any other type of command
398  */
399 static int
400 iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn,
401                          struct iscsi_segment *segment)
402 {
403         struct iscsi_conn *conn = tcp_conn->iscsi_conn;
404         int rc = 0;
405
406         if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
407                 return ISCSI_ERR_DATA_DGST;
408
409         rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr,
410                         conn->data, tcp_conn->in.datalen);
411         if (rc)
412                 return rc;
413
414         iscsi_tcp_hdr_recv_prep(tcp_conn);
415         return 0;
416 }
417
418 static void
419 iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn)
420 {
421         struct iscsi_conn *conn = tcp_conn->iscsi_conn;
422         struct hash_desc *rx_hash = NULL;
423
424         if (conn->datadgst_en &
425             !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
426                 rx_hash = tcp_conn->rx_hash;
427
428         iscsi_segment_init_linear(&tcp_conn->in.segment,
429                                 conn->data, tcp_conn->in.datalen,
430                                 iscsi_tcp_data_recv_done, rx_hash);
431 }
432
433 /**
434  * iscsi_tcp_cleanup_task - free tcp_task resources
435  * @task: iscsi task
436  *
437  * must be called with session lock
438  */
439 void iscsi_tcp_cleanup_task(struct iscsi_task *task)
440 {
441         struct iscsi_tcp_task *tcp_task = task->dd_data;
442         struct iscsi_r2t_info *r2t;
443
444         /* nothing to do for mgmt */
445         if (!task->sc)
446                 return;
447
448         /* flush task's r2t queues */
449         while (kfifo_out(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*))) {
450                 kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
451                             sizeof(void*));
452                 ISCSI_DBG_TCP(task->conn, "pending r2t dropped\n");
453         }
454
455         r2t = tcp_task->r2t;
456         if (r2t != NULL) {
457                 kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
458                             sizeof(void*));
459                 tcp_task->r2t = NULL;
460         }
461 }
462 EXPORT_SYMBOL_GPL(iscsi_tcp_cleanup_task);
463
464 /**
465  * iscsi_tcp_data_in - SCSI Data-In Response processing
466  * @conn: iscsi connection
467  * @task: scsi command task
468  */
469 static int iscsi_tcp_data_in(struct iscsi_conn *conn, struct iscsi_task *task)
470 {
471         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
472         struct iscsi_tcp_task *tcp_task = task->dd_data;
473         struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
474         int datasn = be32_to_cpu(rhdr->datasn);
475         unsigned total_in_length = scsi_in(task->sc)->length;
476
477         /*
478          * lib iscsi will update this in the completion handling if there
479          * is status.
480          */
481         if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
482                 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin*)rhdr);
483
484         if (tcp_conn->in.datalen == 0)
485                 return 0;
486
487         if (tcp_task->exp_datasn != datasn) {
488                 ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->datasn(%d)"
489                               "\n", tcp_task->exp_datasn, datasn);
490                 return ISCSI_ERR_DATASN;
491         }
492
493         tcp_task->exp_datasn++;
494
495         tcp_task->data_offset = be32_to_cpu(rhdr->offset);
496         if (tcp_task->data_offset + tcp_conn->in.datalen > total_in_length) {
497                 ISCSI_DBG_TCP(conn, "data_offset(%d) + data_len(%d) > "
498                               "total_length_in(%d)\n", tcp_task->data_offset,
499                               tcp_conn->in.datalen, total_in_length);
500                 return ISCSI_ERR_DATA_OFFSET;
501         }
502
503         conn->datain_pdus_cnt++;
504         return 0;
505 }
506
507 /**
508  * iscsi_tcp_r2t_rsp - iSCSI R2T Response processing
509  * @conn: iscsi connection
510  * @task: scsi command task
511  */
512 static int iscsi_tcp_r2t_rsp(struct iscsi_conn *conn, struct iscsi_task *task)
513 {
514         struct iscsi_session *session = conn->session;
515         struct iscsi_tcp_task *tcp_task = task->dd_data;
516         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
517         struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
518         struct iscsi_r2t_info *r2t;
519         int r2tsn = be32_to_cpu(rhdr->r2tsn);
520         int rc;
521
522         if (tcp_conn->in.datalen) {
523                 iscsi_conn_printk(KERN_ERR, conn,
524                                   "invalid R2t with datalen %d\n",
525                                   tcp_conn->in.datalen);
526                 return ISCSI_ERR_DATALEN;
527         }
528
529         if (tcp_task->exp_datasn != r2tsn){
530                 ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
531                               tcp_task->exp_datasn, r2tsn);
532                 return ISCSI_ERR_R2TSN;
533         }
534
535         /* fill-in new R2T associated with the task */
536         iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
537
538         if (!task->sc || session->state != ISCSI_STATE_LOGGED_IN) {
539                 iscsi_conn_printk(KERN_INFO, conn,
540                                   "dropping R2T itt %d in recovery.\n",
541                                   task->itt);
542                 return 0;
543         }
544
545         rc = kfifo_out(&tcp_task->r2tpool.queue, (void*)&r2t, sizeof(void*));
546         if (!rc) {
547                 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate R2T. "
548                                   "Target has sent more R2Ts than it "
549                                   "negotiated for or driver has has leaked.\n");
550                 return ISCSI_ERR_PROTO;
551         }
552
553         r2t->exp_statsn = rhdr->statsn;
554         r2t->data_length = be32_to_cpu(rhdr->data_length);
555         if (r2t->data_length == 0) {
556                 iscsi_conn_printk(KERN_ERR, conn,
557                                   "invalid R2T with zero data len\n");
558                 kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
559                             sizeof(void*));
560                 return ISCSI_ERR_DATALEN;
561         }
562
563         if (r2t->data_length > session->max_burst)
564                 ISCSI_DBG_TCP(conn, "invalid R2T with data len %u and max "
565                               "burst %u. Attempting to execute request.\n",
566                               r2t->data_length, session->max_burst);
567
568         r2t->data_offset = be32_to_cpu(rhdr->data_offset);
569         if (r2t->data_offset + r2t->data_length > scsi_out(task->sc)->length) {
570                 iscsi_conn_printk(KERN_ERR, conn,
571                                   "invalid R2T with data len %u at offset %u "
572                                   "and total length %d\n", r2t->data_length,
573                                   r2t->data_offset, scsi_out(task->sc)->length);
574                 kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
575                             sizeof(void*));
576                 return ISCSI_ERR_DATALEN;
577         }
578
579         r2t->ttt = rhdr->ttt; /* no flip */
580         r2t->datasn = 0;
581         r2t->sent = 0;
582
583         tcp_task->exp_datasn = r2tsn + 1;
584         kfifo_in(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*));
585         conn->r2t_pdus_cnt++;
586
587         iscsi_requeue_task(task);
588         return 0;
589 }
590
591 /*
592  * Handle incoming reply to DataIn command
593  */
594 static int
595 iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
596                           struct iscsi_segment *segment)
597 {
598         struct iscsi_conn *conn = tcp_conn->iscsi_conn;
599         struct iscsi_hdr *hdr = tcp_conn->in.hdr;
600         int rc;
601
602         if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
603                 return ISCSI_ERR_DATA_DGST;
604
605         /* check for non-exceptional status */
606         if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
607                 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
608                 if (rc)
609                         return rc;
610         }
611
612         iscsi_tcp_hdr_recv_prep(tcp_conn);
613         return 0;
614 }
615
616 /**
617  * iscsi_tcp_hdr_dissect - process PDU header
618  * @conn: iSCSI connection
619  * @hdr: PDU header
620  *
621  * This function analyzes the header of the PDU received,
622  * and performs several sanity checks. If the PDU is accompanied
623  * by data, the receive buffer is set up to copy the incoming data
624  * to the correct location.
625  */
626 static int
627 iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
628 {
629         int rc = 0, opcode, ahslen;
630         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
631         struct iscsi_task *task;
632
633         /* verify PDU length */
634         tcp_conn->in.datalen = ntoh24(hdr->dlength);
635         if (tcp_conn->in.datalen > conn->max_recv_dlength) {
636                 iscsi_conn_printk(KERN_ERR, conn,
637                                   "iscsi_tcp: datalen %d > %d\n",
638                                   tcp_conn->in.datalen, conn->max_recv_dlength);
639                 return ISCSI_ERR_DATALEN;
640         }
641
642         /* Additional header segments. So far, we don't
643          * process additional headers.
644          */
645         ahslen = hdr->hlength << 2;
646
647         opcode = hdr->opcode & ISCSI_OPCODE_MASK;
648         /* verify itt (itt encoding: age+cid+itt) */
649         rc = iscsi_verify_itt(conn, hdr->itt);
650         if (rc)
651                 return rc;
652
653         ISCSI_DBG_TCP(conn, "opcode 0x%x ahslen %d datalen %d\n",
654                       opcode, ahslen, tcp_conn->in.datalen);
655
656         switch(opcode) {
657         case ISCSI_OP_SCSI_DATA_IN:
658                 spin_lock(&conn->session->lock);
659                 task = iscsi_itt_to_ctask(conn, hdr->itt);
660                 if (!task)
661                         rc = ISCSI_ERR_BAD_ITT;
662                 else
663                         rc = iscsi_tcp_data_in(conn, task);
664                 if (rc) {
665                         spin_unlock(&conn->session->lock);
666                         break;
667                 }
668
669                 if (tcp_conn->in.datalen) {
670                         struct iscsi_tcp_task *tcp_task = task->dd_data;
671                         struct hash_desc *rx_hash = NULL;
672                         struct scsi_data_buffer *sdb = scsi_in(task->sc);
673
674                         /*
675                          * Setup copy of Data-In into the Scsi_Cmnd
676                          * Scatterlist case:
677                          * We set up the iscsi_segment to point to the next
678                          * scatterlist entry to copy to. As we go along,
679                          * we move on to the next scatterlist entry and
680                          * update the digest per-entry.
681                          */
682                         if (conn->datadgst_en &&
683                             !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
684                                 rx_hash = tcp_conn->rx_hash;
685
686                         ISCSI_DBG_TCP(conn, "iscsi_tcp_begin_data_in( "
687                                      "offset=%d, datalen=%d)\n",
688                                       tcp_task->data_offset,
689                                       tcp_conn->in.datalen);
690                         task->last_xfer = jiffies;
691                         rc = iscsi_segment_seek_sg(&tcp_conn->in.segment,
692                                                    sdb->table.sgl,
693                                                    sdb->table.nents,
694                                                    tcp_task->data_offset,
695                                                    tcp_conn->in.datalen,
696                                                    iscsi_tcp_process_data_in,
697                                                    rx_hash);
698                         spin_unlock(&conn->session->lock);
699                         return rc;
700                 }
701                 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
702                 spin_unlock(&conn->session->lock);
703                 break;
704         case ISCSI_OP_SCSI_CMD_RSP:
705                 if (tcp_conn->in.datalen) {
706                         iscsi_tcp_data_recv_prep(tcp_conn);
707                         return 0;
708                 }
709                 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
710                 break;
711         case ISCSI_OP_R2T:
712                 spin_lock(&conn->session->lock);
713                 task = iscsi_itt_to_ctask(conn, hdr->itt);
714                 if (!task)
715                         rc = ISCSI_ERR_BAD_ITT;
716                 else if (ahslen)
717                         rc = ISCSI_ERR_AHSLEN;
718                 else if (task->sc->sc_data_direction == DMA_TO_DEVICE) {
719                         task->last_xfer = jiffies;
720                         rc = iscsi_tcp_r2t_rsp(conn, task);
721                 } else
722                         rc = ISCSI_ERR_PROTO;
723                 spin_unlock(&conn->session->lock);
724                 break;
725         case ISCSI_OP_LOGIN_RSP:
726         case ISCSI_OP_TEXT_RSP:
727         case ISCSI_OP_REJECT:
728         case ISCSI_OP_ASYNC_EVENT:
729                 /*
730                  * It is possible that we could get a PDU with a buffer larger
731                  * than 8K, but there are no targets that currently do this.
732                  * For now we fail until we find a vendor that needs it
733                  */
734                 if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
735                         iscsi_conn_printk(KERN_ERR, conn,
736                                           "iscsi_tcp: received buffer of "
737                                           "len %u but conn buffer is only %u "
738                                           "(opcode %0x)\n",
739                                           tcp_conn->in.datalen,
740                                           ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
741                         rc = ISCSI_ERR_PROTO;
742                         break;
743                 }
744
745                 /* If there's data coming in with the response,
746                  * receive it to the connection's buffer.
747                  */
748                 if (tcp_conn->in.datalen) {
749                         iscsi_tcp_data_recv_prep(tcp_conn);
750                         return 0;
751                 }
752         /* fall through */
753         case ISCSI_OP_LOGOUT_RSP:
754         case ISCSI_OP_NOOP_IN:
755         case ISCSI_OP_SCSI_TMFUNC_RSP:
756                 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
757                 break;
758         default:
759                 rc = ISCSI_ERR_BAD_OPCODE;
760                 break;
761         }
762
763         if (rc == 0) {
764                 /* Anything that comes with data should have
765                  * been handled above. */
766                 if (tcp_conn->in.datalen)
767                         return ISCSI_ERR_PROTO;
768                 iscsi_tcp_hdr_recv_prep(tcp_conn);
769         }
770
771         return rc;
772 }
773
774 /**
775  * iscsi_tcp_hdr_recv_done - process PDU header
776  *
777  * This is the callback invoked when the PDU header has
778  * been received. If the header is followed by additional
779  * header segments, we go back for more data.
780  */
781 static int
782 iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
783                         struct iscsi_segment *segment)
784 {
785         struct iscsi_conn *conn = tcp_conn->iscsi_conn;
786         struct iscsi_hdr *hdr;
787
788         /* Check if there are additional header segments
789          * *prior* to computing the digest, because we
790          * may need to go back to the caller for more.
791          */
792         hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
793         if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
794                 /* Bump the header length - the caller will
795                  * just loop around and get the AHS for us, and
796                  * call again. */
797                 unsigned int ahslen = hdr->hlength << 2;
798
799                 /* Make sure we don't overflow */
800                 if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
801                         return ISCSI_ERR_AHSLEN;
802
803                 segment->total_size += ahslen;
804                 segment->size += ahslen;
805                 return 0;
806         }
807
808         /* We're done processing the header. See if we're doing
809          * header digests; if so, set up the recv_digest buffer
810          * and go back for more. */
811         if (conn->hdrdgst_en &&
812             !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) {
813                 if (segment->digest_len == 0) {
814                         /*
815                          * Even if we offload the digest processing we
816                          * splice it in so we can increment the skb/segment
817                          * counters in preparation for the data segment.
818                          */
819                         iscsi_tcp_segment_splice_digest(segment,
820                                                         segment->recv_digest);
821                         return 0;
822                 }
823
824                 iscsi_tcp_dgst_header(tcp_conn->rx_hash, hdr,
825                                       segment->total_copied - ISCSI_DIGEST_SIZE,
826                                       segment->digest);
827
828                 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
829                         return ISCSI_ERR_HDR_DGST;
830         }
831
832         tcp_conn->in.hdr = hdr;
833         return iscsi_tcp_hdr_dissect(conn, hdr);
834 }
835
836 /**
837  * iscsi_tcp_recv_segment_is_hdr - tests if we are reading in a header
838  * @tcp_conn: iscsi tcp conn
839  *
840  * returns non zero if we are currently processing or setup to process
841  * a header.
842  */
843 inline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn *tcp_conn)
844 {
845         return tcp_conn->in.segment.done == iscsi_tcp_hdr_recv_done;
846 }
847 EXPORT_SYMBOL_GPL(iscsi_tcp_recv_segment_is_hdr);
848
849 /**
850  * iscsi_tcp_recv_skb - Process skb
851  * @conn: iscsi connection
852  * @skb: network buffer with header and/or data segment
853  * @offset: offset in skb
854  * @offload: bool indicating if transfer was offloaded
855  *
856  * Will return status of transfer in status. And will return
857  * number of bytes copied.
858  */
859 int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
860                        unsigned int offset, bool offloaded, int *status)
861 {
862         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
863         struct iscsi_segment *segment = &tcp_conn->in.segment;
864         struct skb_seq_state seq;
865         unsigned int consumed = 0;
866         int rc = 0;
867
868         ISCSI_DBG_TCP(conn, "in %d bytes\n", skb->len - offset);
869         /*
870          * Update for each skb instead of pdu, because over slow networks a
871          * data_in's data could take a while to read in. We also want to
872          * account for r2ts.
873          */
874         conn->last_recv = jiffies;
875
876         if (unlikely(conn->suspend_rx)) {
877                 ISCSI_DBG_TCP(conn, "Rx suspended!\n");
878                 *status = ISCSI_TCP_SUSPENDED;
879                 return 0;
880         }
881
882         if (offloaded) {
883                 segment->total_copied = segment->total_size;
884                 goto segment_done;
885         }
886
887         skb_prepare_seq_read(skb, offset, skb->len, &seq);
888         while (1) {
889                 unsigned int avail;
890                 const u8 *ptr;
891
892                 avail = skb_seq_read(consumed, &ptr, &seq);
893                 if (avail == 0) {
894                         ISCSI_DBG_TCP(conn, "no more data avail. Consumed %d\n",
895                                       consumed);
896                         *status = ISCSI_TCP_SKB_DONE;
897                         skb_abort_seq_read(&seq);
898                         goto skb_done;
899                 }
900                 BUG_ON(segment->copied >= segment->size);
901
902                 ISCSI_DBG_TCP(conn, "skb %p ptr=%p avail=%u\n", skb, ptr,
903                               avail);
904                 rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
905                 BUG_ON(rc == 0);
906                 consumed += rc;
907
908                 if (segment->total_copied >= segment->total_size) {
909                         skb_abort_seq_read(&seq);
910                         goto segment_done;
911                 }
912         }
913
914 segment_done:
915         *status = ISCSI_TCP_SEGMENT_DONE;
916         ISCSI_DBG_TCP(conn, "segment done\n");
917         rc = segment->done(tcp_conn, segment);
918         if (rc != 0) {
919                 *status = ISCSI_TCP_CONN_ERR;
920                 ISCSI_DBG_TCP(conn, "Error receiving PDU, errno=%d\n", rc);
921                 iscsi_conn_failure(conn, rc);
922                 return 0;
923         }
924         /* The done() functions sets up the next segment. */
925
926 skb_done:
927         conn->rxdata_octets += consumed;
928         return consumed;
929 }
930 EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb);
931
932 /**
933  * iscsi_tcp_task_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
934  * @conn: iscsi connection
935  * @task: scsi command task
936  * @sc: scsi command
937  */
938 int iscsi_tcp_task_init(struct iscsi_task *task)
939 {
940         struct iscsi_tcp_task *tcp_task = task->dd_data;
941         struct iscsi_conn *conn = task->conn;
942         struct scsi_cmnd *sc = task->sc;
943         int err;
944
945         if (!sc) {
946                 /*
947                  * mgmt tasks do not have a scatterlist since they come
948                  * in from the iscsi interface.
949                  */
950                 ISCSI_DBG_TCP(conn, "mtask deq [itt 0x%x]\n", task->itt);
951
952                 return conn->session->tt->init_pdu(task, 0, task->data_count);
953         }
954
955         BUG_ON(kfifo_len(&tcp_task->r2tqueue));
956         tcp_task->exp_datasn = 0;
957
958         /* Prepare PDU, optionally w/ immediate data */
959         ISCSI_DBG_TCP(conn, "task deq [itt 0x%x imm %d unsol %d]\n",
960                       task->itt, task->imm_count, task->unsol_r2t.data_length);
961
962         err = conn->session->tt->init_pdu(task, 0, task->imm_count);
963         if (err)
964                 return err;
965         task->imm_count = 0;
966         return 0;
967 }
968 EXPORT_SYMBOL_GPL(iscsi_tcp_task_init);
969
970 static struct iscsi_r2t_info *iscsi_tcp_get_curr_r2t(struct iscsi_task *task)
971 {
972         struct iscsi_session *session = task->conn->session;
973         struct iscsi_tcp_task *tcp_task = task->dd_data;
974         struct iscsi_r2t_info *r2t = NULL;
975
976         if (iscsi_task_has_unsol_data(task))
977                 r2t = &task->unsol_r2t;
978         else {
979                 spin_lock_bh(&session->lock);
980                 if (tcp_task->r2t) {
981                         r2t = tcp_task->r2t;
982                         /* Continue with this R2T? */
983                         if (r2t->data_length <= r2t->sent) {
984                                 ISCSI_DBG_TCP(task->conn,
985                                               "  done with r2t %p\n", r2t);
986                                 kfifo_in(&tcp_task->r2tpool.queue,
987                                             (void *)&tcp_task->r2t,
988                                             sizeof(void *));
989                                 tcp_task->r2t = r2t = NULL;
990                         }
991                 }
992
993                 if (r2t == NULL) {
994                         if (kfifo_out(&tcp_task->r2tqueue,
995                             (void *)&tcp_task->r2t, sizeof(void *)) !=
996                             sizeof(void *))
997                                 r2t = NULL;
998                         else
999                                 r2t = tcp_task->r2t;
1000                 }
1001                 spin_unlock_bh(&session->lock);
1002         }
1003
1004         return r2t;
1005 }
1006
1007 /**
1008  * iscsi_tcp_task_xmit - xmit normal PDU task
1009  * @task: iscsi command task
1010  *
1011  * We're expected to return 0 when everything was transmitted successfully,
1012  * -EAGAIN if there's still data in the queue, or != 0 for any other kind
1013  * of error.
1014  */
1015 int iscsi_tcp_task_xmit(struct iscsi_task *task)
1016 {
1017         struct iscsi_conn *conn = task->conn;
1018         struct iscsi_session *session = conn->session;
1019         struct iscsi_r2t_info *r2t;
1020         int rc = 0;
1021
1022 flush:
1023         /* Flush any pending data first. */
1024         rc = session->tt->xmit_pdu(task);
1025         if (rc < 0)
1026                 return rc;
1027
1028         /* mgmt command */
1029         if (!task->sc) {
1030                 if (task->hdr->itt == RESERVED_ITT)
1031                         iscsi_put_task(task);
1032                 return 0;
1033         }
1034
1035         /* Are we done already? */
1036         if (task->sc->sc_data_direction != DMA_TO_DEVICE)
1037                 return 0;
1038
1039         r2t = iscsi_tcp_get_curr_r2t(task);
1040         if (r2t == NULL) {
1041                 /* Waiting for more R2Ts to arrive. */
1042                 ISCSI_DBG_TCP(conn, "no R2Ts yet\n");
1043                 return 0;
1044         }
1045
1046         rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_DATA_OUT);
1047         if (rc)
1048                 return rc;
1049         iscsi_prep_data_out_pdu(task, r2t, (struct iscsi_data *) task->hdr);
1050
1051         ISCSI_DBG_TCP(conn, "sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
1052                       r2t, r2t->datasn - 1, task->hdr->itt,
1053                       r2t->data_offset + r2t->sent, r2t->data_count);
1054
1055         rc = conn->session->tt->init_pdu(task, r2t->data_offset + r2t->sent,
1056                                          r2t->data_count);
1057         if (rc) {
1058                 iscsi_conn_failure(conn, ISCSI_ERR_XMIT_FAILED);
1059                 return rc;
1060         }
1061
1062         r2t->sent += r2t->data_count;
1063         goto flush;
1064 }
1065 EXPORT_SYMBOL_GPL(iscsi_tcp_task_xmit);
1066
1067 struct iscsi_cls_conn *
1068 iscsi_tcp_conn_setup(struct iscsi_cls_session *cls_session, int dd_data_size,
1069                       uint32_t conn_idx)
1070
1071 {
1072         struct iscsi_conn *conn;
1073         struct iscsi_cls_conn *cls_conn;
1074         struct iscsi_tcp_conn *tcp_conn;
1075
1076         cls_conn = iscsi_conn_setup(cls_session, sizeof(*tcp_conn), conn_idx);
1077         if (!cls_conn)
1078                 return NULL;
1079         conn = cls_conn->dd_data;
1080         /*
1081          * due to strange issues with iser these are not set
1082          * in iscsi_conn_setup
1083          */
1084         conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
1085
1086         tcp_conn = conn->dd_data;
1087         tcp_conn->iscsi_conn = conn;
1088
1089         tcp_conn->dd_data = kzalloc(dd_data_size, GFP_KERNEL);
1090         if (!tcp_conn->dd_data) {
1091                 iscsi_conn_teardown(cls_conn);
1092                 return NULL;
1093         }
1094         return cls_conn;
1095 }
1096 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_setup);
1097
1098 void iscsi_tcp_conn_teardown(struct iscsi_cls_conn *cls_conn)
1099 {
1100         struct iscsi_conn *conn = cls_conn->dd_data;
1101         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1102
1103         kfree(tcp_conn->dd_data);
1104         iscsi_conn_teardown(cls_conn);
1105 }
1106 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_teardown);
1107
1108 int iscsi_tcp_r2tpool_alloc(struct iscsi_session *session)
1109 {
1110         int i;
1111         int cmd_i;
1112
1113         /*
1114          * initialize per-task: R2T pool and xmit queue
1115          */
1116         for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1117                 struct iscsi_task *task = session->cmds[cmd_i];
1118                 struct iscsi_tcp_task *tcp_task = task->dd_data;
1119
1120                 /*
1121                  * pre-allocated x2 as much r2ts to handle race when
1122                  * target acks DataOut faster than we data_xmit() queues
1123                  * could replenish r2tqueue.
1124                  */
1125
1126                 /* R2T pool */
1127                 if (iscsi_pool_init(&tcp_task->r2tpool,
1128                                     session->max_r2t * 2, NULL,
1129                                     sizeof(struct iscsi_r2t_info))) {
1130                         goto r2t_alloc_fail;
1131                 }
1132
1133                 /* R2T xmit queue */
1134                 if (kfifo_alloc(&tcp_task->r2tqueue,
1135                       session->max_r2t * 4 * sizeof(void*), GFP_KERNEL)) {
1136                         iscsi_pool_free(&tcp_task->r2tpool);
1137                         goto r2t_alloc_fail;
1138                 }
1139         }
1140
1141         return 0;
1142
1143 r2t_alloc_fail:
1144         for (i = 0; i < cmd_i; i++) {
1145                 struct iscsi_task *task = session->cmds[i];
1146                 struct iscsi_tcp_task *tcp_task = task->dd_data;
1147
1148                 kfifo_free(&tcp_task->r2tqueue);
1149                 iscsi_pool_free(&tcp_task->r2tpool);
1150         }
1151         return -ENOMEM;
1152 }
1153 EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_alloc);
1154
1155 void iscsi_tcp_r2tpool_free(struct iscsi_session *session)
1156 {
1157         int i;
1158
1159         for (i = 0; i < session->cmds_max; i++) {
1160                 struct iscsi_task *task = session->cmds[i];
1161                 struct iscsi_tcp_task *tcp_task = task->dd_data;
1162
1163                 kfifo_free(&tcp_task->r2tqueue);
1164                 iscsi_pool_free(&tcp_task->r2tpool);
1165         }
1166 }
1167 EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_free);
1168
1169 void iscsi_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
1170                               struct iscsi_stats *stats)
1171 {
1172         struct iscsi_conn *conn = cls_conn->dd_data;
1173
1174         stats->txdata_octets = conn->txdata_octets;
1175         stats->rxdata_octets = conn->rxdata_octets;
1176         stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
1177         stats->dataout_pdus = conn->dataout_pdus_cnt;
1178         stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
1179         stats->datain_pdus = conn->datain_pdus_cnt;
1180         stats->r2t_pdus = conn->r2t_pdus_cnt;
1181         stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
1182         stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
1183 }
1184 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_get_stats);