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