[SCSI] qla2xxx: Clear EFT buffer before firmware reinitialization.
[linux-2.6] / drivers / scsi / iscsi_tcp.c
1 /*
2  * iSCSI Initiator over TCP/IP Data-Path
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("Dmitry Yusupov <dmitry_yus@yahoo.com>, "
48               "Alex Aizman <itn780@yahoo.com>");
49 MODULE_DESCRIPTION("iSCSI/TCP data-path");
50 MODULE_LICENSE("GPL");
51 #undef DEBUG_TCP
52 #define DEBUG_ASSERT
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 #ifndef DEBUG_ASSERT
61 #ifdef BUG_ON
62 #undef BUG_ON
63 #endif
64 #define BUG_ON(expr)
65 #endif
66
67 static unsigned int iscsi_max_lun = 512;
68 module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
69
70 static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
71                                    struct iscsi_segment *segment);
72
73 /*
74  * Scatterlist handling: inside the iscsi_segment, we
75  * remember an index into the scatterlist, and set data/size
76  * to the current scatterlist entry. For highmem pages, we
77  * kmap as needed.
78  *
79  * Note that the page is unmapped when we return from
80  * TCP's data_ready handler, so we may end up mapping and
81  * unmapping the same page repeatedly. The whole reason
82  * for this is that we shouldn't keep the page mapped
83  * outside the softirq.
84  */
85
86 /**
87  * iscsi_tcp_segment_init_sg - init indicated scatterlist entry
88  * @segment: the buffer object
89  * @sg: scatterlist
90  * @offset: byte offset into that sg entry
91  *
92  * This function sets up the segment so that subsequent
93  * data is copied to the indicated sg entry, at the given
94  * offset.
95  */
96 static inline void
97 iscsi_tcp_segment_init_sg(struct iscsi_segment *segment,
98                           struct scatterlist *sg, unsigned int offset)
99 {
100         segment->sg = sg;
101         segment->sg_offset = offset;
102         segment->size = min(sg->length - offset,
103                             segment->total_size - segment->total_copied);
104         segment->data = NULL;
105 }
106
107 /**
108  * iscsi_tcp_segment_map - map the current S/G page
109  * @segment: iscsi_segment
110  * @recv: 1 if called from recv path
111  *
112  * We only need to possibly kmap data if scatter lists are being used,
113  * because the iscsi passthrough and internal IO paths will never use high
114  * mem pages.
115  */
116 static inline void
117 iscsi_tcp_segment_map(struct iscsi_segment *segment, int recv)
118 {
119         struct scatterlist *sg;
120
121         if (segment->data != NULL || !segment->sg)
122                 return;
123
124         sg = segment->sg;
125         BUG_ON(segment->sg_mapped);
126         BUG_ON(sg->length == 0);
127
128         /*
129          * If the page count is greater than one it is ok to send
130          * to the network layer's zero copy send path. If not we
131          * have to go the slow sendmsg path. We always map for the
132          * recv path.
133          */
134         if (page_count(sg_page(sg)) >= 1 && !recv)
135                 return;
136
137         debug_tcp("iscsi_tcp_segment_map %s %p\n", recv ? "recv" : "xmit",
138                   segment);
139         segment->sg_mapped = kmap_atomic(sg_page(sg), KM_SOFTIRQ0);
140         segment->data = segment->sg_mapped + sg->offset + segment->sg_offset;
141 }
142
143 static inline void
144 iscsi_tcp_segment_unmap(struct iscsi_segment *segment)
145 {
146         debug_tcp("iscsi_tcp_segment_unmap %p\n", segment);
147
148         if (segment->sg_mapped) {
149                 debug_tcp("iscsi_tcp_segment_unmap valid\n");
150                 kunmap_atomic(segment->sg_mapped, KM_SOFTIRQ0);
151                 segment->sg_mapped = NULL;
152                 segment->data = NULL;
153         }
154 }
155
156 /*
157  * Splice the digest buffer into the buffer
158  */
159 static inline void
160 iscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest)
161 {
162         segment->data = digest;
163         segment->digest_len = ISCSI_DIGEST_SIZE;
164         segment->total_size += ISCSI_DIGEST_SIZE;
165         segment->size = ISCSI_DIGEST_SIZE;
166         segment->copied = 0;
167         segment->sg = NULL;
168         segment->hash = NULL;
169 }
170
171 /**
172  * iscsi_tcp_segment_done - check whether the segment is complete
173  * @segment: iscsi segment to check
174  * @recv: set to one of this is called from the recv path
175  * @copied: number of bytes copied
176  *
177  * Check if we're done receiving this segment. If the receive
178  * buffer is full but we expect more data, move on to the
179  * next entry in the scatterlist.
180  *
181  * If the amount of data we received isn't a multiple of 4,
182  * we will transparently receive the pad bytes, too.
183  *
184  * This function must be re-entrant.
185  */
186 static inline int
187 iscsi_tcp_segment_done(struct iscsi_segment *segment, int recv, unsigned copied)
188 {
189         static unsigned char padbuf[ISCSI_PAD_LEN];
190         struct scatterlist sg;
191         unsigned int pad;
192
193         debug_tcp("copied %u %u size %u %s\n", segment->copied, copied,
194                   segment->size, recv ? "recv" : "xmit");
195         if (segment->hash && copied) {
196                 /*
197                  * If a segment is kmapd we must unmap it before sending
198                  * to the crypto layer since that will try to kmap it again.
199                  */
200                 iscsi_tcp_segment_unmap(segment);
201
202                 if (!segment->data) {
203                         sg_init_table(&sg, 1);
204                         sg_set_page(&sg, sg_page(segment->sg), copied,
205                                     segment->copied + segment->sg_offset +
206                                                         segment->sg->offset);
207                 } else
208                         sg_init_one(&sg, segment->data + segment->copied,
209                                     copied);
210                 crypto_hash_update(segment->hash, &sg, copied);
211         }
212
213         segment->copied += copied;
214         if (segment->copied < segment->size) {
215                 iscsi_tcp_segment_map(segment, recv);
216                 return 0;
217         }
218
219         segment->total_copied += segment->copied;
220         segment->copied = 0;
221         segment->size = 0;
222
223         /* Unmap the current scatterlist page, if there is one. */
224         iscsi_tcp_segment_unmap(segment);
225
226         /* Do we have more scatterlist entries? */
227         debug_tcp("total copied %u total size %u\n", segment->total_copied,
228                    segment->total_size);
229         if (segment->total_copied < segment->total_size) {
230                 /* Proceed to the next entry in the scatterlist. */
231                 iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg),
232                                           0);
233                 iscsi_tcp_segment_map(segment, recv);
234                 BUG_ON(segment->size == 0);
235                 return 0;
236         }
237
238         /* Do we need to handle padding? */
239         pad = iscsi_padding(segment->total_copied);
240         if (pad != 0) {
241                 debug_tcp("consume %d pad bytes\n", pad);
242                 segment->total_size += pad;
243                 segment->size = pad;
244                 segment->data = padbuf;
245                 return 0;
246         }
247
248         /*
249          * Set us up for transferring the data digest. hdr digest
250          * is completely handled in hdr done function.
251          */
252         if (segment->hash) {
253                 crypto_hash_final(segment->hash, segment->digest);
254                 iscsi_tcp_segment_splice_digest(segment,
255                                  recv ? segment->recv_digest : segment->digest);
256                 return 0;
257         }
258
259         return 1;
260 }
261
262 /**
263  * iscsi_tcp_xmit_segment - transmit segment
264  * @tcp_conn: the iSCSI TCP connection
265  * @segment: the buffer to transmnit
266  *
267  * This function transmits as much of the buffer as
268  * the network layer will accept, and returns the number of
269  * bytes transmitted.
270  *
271  * If CRC hashing is enabled, the function will compute the
272  * hash as it goes. When the entire segment has been transmitted,
273  * it will retrieve the hash value and send it as well.
274  */
275 static int
276 iscsi_tcp_xmit_segment(struct iscsi_tcp_conn *tcp_conn,
277                        struct iscsi_segment *segment)
278 {
279         struct socket *sk = tcp_conn->sock;
280         unsigned int copied = 0;
281         int r = 0;
282
283         while (!iscsi_tcp_segment_done(segment, 0, r)) {
284                 struct scatterlist *sg;
285                 unsigned int offset, copy;
286                 int flags = 0;
287
288                 r = 0;
289                 offset = segment->copied;
290                 copy = segment->size - offset;
291
292                 if (segment->total_copied + segment->size < segment->total_size)
293                         flags |= MSG_MORE;
294
295                 /* Use sendpage if we can; else fall back to sendmsg */
296                 if (!segment->data) {
297                         sg = segment->sg;
298                         offset += segment->sg_offset + sg->offset;
299                         r = tcp_conn->sendpage(sk, sg_page(sg), offset, copy,
300                                                flags);
301                 } else {
302                         struct msghdr msg = { .msg_flags = flags };
303                         struct kvec iov = {
304                                 .iov_base = segment->data + offset,
305                                 .iov_len = copy
306                         };
307
308                         r = kernel_sendmsg(sk, &msg, &iov, 1, copy);
309                 }
310
311                 if (r < 0) {
312                         iscsi_tcp_segment_unmap(segment);
313                         if (copied || r == -EAGAIN)
314                                 break;
315                         return r;
316                 }
317                 copied += r;
318         }
319         return copied;
320 }
321
322 /**
323  * iscsi_tcp_segment_recv - copy data to segment
324  * @tcp_conn: the iSCSI TCP connection
325  * @segment: the buffer to copy to
326  * @ptr: data pointer
327  * @len: amount of data available
328  *
329  * This function copies up to @len bytes to the
330  * given buffer, and returns the number of bytes
331  * consumed, which can actually be less than @len.
332  *
333  * If hash digest is enabled, the function will update the
334  * hash while copying.
335  * Combining these two operations doesn't buy us a lot (yet),
336  * but in the future we could implement combined copy+crc,
337  * just way we do for network layer checksums.
338  */
339 static int
340 iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn,
341                        struct iscsi_segment *segment, const void *ptr,
342                        unsigned int len)
343 {
344         unsigned int copy = 0, copied = 0;
345
346         while (!iscsi_tcp_segment_done(segment, 1, copy)) {
347                 if (copied == len) {
348                         debug_tcp("iscsi_tcp_segment_recv copied %d bytes\n",
349                                   len);
350                         break;
351                 }
352
353                 copy = min(len - copied, segment->size - segment->copied);
354                 debug_tcp("iscsi_tcp_segment_recv copying %d\n", copy);
355                 memcpy(segment->data + segment->copied, ptr + copied, copy);
356                 copied += copy;
357         }
358         return copied;
359 }
360
361 static inline void
362 iscsi_tcp_dgst_header(struct hash_desc *hash, const void *hdr, size_t hdrlen,
363                       unsigned char digest[ISCSI_DIGEST_SIZE])
364 {
365         struct scatterlist sg;
366
367         sg_init_one(&sg, hdr, hdrlen);
368         crypto_hash_digest(hash, &sg, hdrlen, digest);
369 }
370
371 static inline int
372 iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn,
373                       struct iscsi_segment *segment)
374 {
375         if (!segment->digest_len)
376                 return 1;
377
378         if (memcmp(segment->recv_digest, segment->digest,
379                    segment->digest_len)) {
380                 debug_scsi("digest mismatch\n");
381                 return 0;
382         }
383
384         return 1;
385 }
386
387 /*
388  * Helper function to set up segment buffer
389  */
390 static inline void
391 __iscsi_segment_init(struct iscsi_segment *segment, size_t size,
392                      iscsi_segment_done_fn_t *done, struct hash_desc *hash)
393 {
394         memset(segment, 0, sizeof(*segment));
395         segment->total_size = size;
396         segment->done = done;
397
398         if (hash) {
399                 segment->hash = hash;
400                 crypto_hash_init(hash);
401         }
402 }
403
404 static inline void
405 iscsi_segment_init_linear(struct iscsi_segment *segment, void *data,
406                           size_t size, iscsi_segment_done_fn_t *done,
407                           struct hash_desc *hash)
408 {
409         __iscsi_segment_init(segment, size, done, hash);
410         segment->data = data;
411         segment->size = size;
412 }
413
414 static inline int
415 iscsi_segment_seek_sg(struct iscsi_segment *segment,
416                       struct scatterlist *sg_list, unsigned int sg_count,
417                       unsigned int offset, size_t size,
418                       iscsi_segment_done_fn_t *done, struct hash_desc *hash)
419 {
420         struct scatterlist *sg;
421         unsigned int i;
422
423         debug_scsi("iscsi_segment_seek_sg offset %u size %llu\n",
424                   offset, size);
425         __iscsi_segment_init(segment, size, done, hash);
426         for_each_sg(sg_list, sg, sg_count, i) {
427                 debug_scsi("sg %d, len %u offset %u\n", i, sg->length,
428                            sg->offset);
429                 if (offset < sg->length) {
430                         iscsi_tcp_segment_init_sg(segment, sg, offset);
431                         return 0;
432                 }
433                 offset -= sg->length;
434         }
435
436         return ISCSI_ERR_DATA_OFFSET;
437 }
438
439 /**
440  * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
441  * @tcp_conn: iscsi connection to prep for
442  *
443  * This function always passes NULL for the hash argument, because when this
444  * function is called we do not yet know the final size of the header and want
445  * to delay the digest processing until we know that.
446  */
447 static void
448 iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn)
449 {
450         debug_tcp("iscsi_tcp_hdr_recv_prep(%p%s)\n", tcp_conn,
451                   tcp_conn->iscsi_conn->hdrdgst_en ? ", digest enabled" : "");
452         iscsi_segment_init_linear(&tcp_conn->in.segment,
453                                 tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr),
454                                 iscsi_tcp_hdr_recv_done, NULL);
455 }
456
457 /*
458  * Handle incoming reply to any other type of command
459  */
460 static int
461 iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn,
462                          struct iscsi_segment *segment)
463 {
464         struct iscsi_conn *conn = tcp_conn->iscsi_conn;
465         int rc = 0;
466
467         if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
468                 return ISCSI_ERR_DATA_DGST;
469
470         rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr,
471                         conn->data, tcp_conn->in.datalen);
472         if (rc)
473                 return rc;
474
475         iscsi_tcp_hdr_recv_prep(tcp_conn);
476         return 0;
477 }
478
479 static void
480 iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn)
481 {
482         struct iscsi_conn *conn = tcp_conn->iscsi_conn;
483         struct hash_desc *rx_hash = NULL;
484
485         if (conn->datadgst_en)
486                 rx_hash = &tcp_conn->rx_hash;
487
488         iscsi_segment_init_linear(&tcp_conn->in.segment,
489                                 conn->data, tcp_conn->in.datalen,
490                                 iscsi_tcp_data_recv_done, rx_hash);
491 }
492
493 /*
494  * must be called with session lock
495  */
496 static void
497 iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
498 {
499         struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
500         struct iscsi_r2t_info *r2t;
501
502         /* flush ctask's r2t queues */
503         while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
504                 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
505                             sizeof(void*));
506                 debug_scsi("iscsi_tcp_cleanup_ctask pending r2t dropped\n");
507         }
508
509         r2t = tcp_ctask->r2t;
510         if (r2t != NULL) {
511                 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
512                             sizeof(void*));
513                 tcp_ctask->r2t = NULL;
514         }
515 }
516
517 /**
518  * iscsi_data_rsp - SCSI Data-In Response processing
519  * @conn: iscsi connection
520  * @ctask: scsi command task
521  **/
522 static int
523 iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
524 {
525         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
526         struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
527         struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
528         struct iscsi_session *session = conn->session;
529         struct scsi_cmnd *sc = ctask->sc;
530         int datasn = be32_to_cpu(rhdr->datasn);
531
532         iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
533         if (tcp_conn->in.datalen == 0)
534                 return 0;
535
536         if (tcp_ctask->exp_datasn != datasn) {
537                 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->datasn(%d)\n",
538                           __FUNCTION__, tcp_ctask->exp_datasn, datasn);
539                 return ISCSI_ERR_DATASN;
540         }
541
542         tcp_ctask->exp_datasn++;
543
544         tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
545         if (tcp_ctask->data_offset + tcp_conn->in.datalen > scsi_bufflen(sc)) {
546                 debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
547                           __FUNCTION__, tcp_ctask->data_offset,
548                           tcp_conn->in.datalen, scsi_bufflen(sc));
549                 return ISCSI_ERR_DATA_OFFSET;
550         }
551
552         if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
553                 sc->result = (DID_OK << 16) | rhdr->cmd_status;
554                 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
555                 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
556                                    ISCSI_FLAG_DATA_OVERFLOW)) {
557                         int res_count = be32_to_cpu(rhdr->residual_count);
558
559                         if (res_count > 0 &&
560                             (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
561                              res_count <= scsi_bufflen(sc)))
562                                 scsi_set_resid(sc, res_count);
563                         else
564                                 sc->result = (DID_BAD_TARGET << 16) |
565                                         rhdr->cmd_status;
566                 }
567         }
568
569         conn->datain_pdus_cnt++;
570         return 0;
571 }
572
573 /**
574  * iscsi_solicit_data_init - initialize first Data-Out
575  * @conn: iscsi connection
576  * @ctask: scsi command task
577  * @r2t: R2T info
578  *
579  * Notes:
580  *      Initialize first Data-Out within this R2T sequence and finds
581  *      proper data_offset within this SCSI command.
582  *
583  *      This function is called with connection lock taken.
584  **/
585 static void
586 iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
587                         struct iscsi_r2t_info *r2t)
588 {
589         struct iscsi_data *hdr;
590
591         hdr = &r2t->dtask.hdr;
592         memset(hdr, 0, sizeof(struct iscsi_data));
593         hdr->ttt = r2t->ttt;
594         hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
595         r2t->solicit_datasn++;
596         hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
597         memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
598         hdr->itt = ctask->hdr->itt;
599         hdr->exp_statsn = r2t->exp_statsn;
600         hdr->offset = cpu_to_be32(r2t->data_offset);
601         if (r2t->data_length > conn->max_xmit_dlength) {
602                 hton24(hdr->dlength, conn->max_xmit_dlength);
603                 r2t->data_count = conn->max_xmit_dlength;
604                 hdr->flags = 0;
605         } else {
606                 hton24(hdr->dlength, r2t->data_length);
607                 r2t->data_count = r2t->data_length;
608                 hdr->flags = ISCSI_FLAG_CMD_FINAL;
609         }
610         conn->dataout_pdus_cnt++;
611
612         r2t->sent = 0;
613 }
614
615 /**
616  * iscsi_r2t_rsp - iSCSI R2T Response processing
617  * @conn: iscsi connection
618  * @ctask: scsi command task
619  **/
620 static int
621 iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
622 {
623         struct iscsi_r2t_info *r2t;
624         struct iscsi_session *session = conn->session;
625         struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
626         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
627         struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
628         int r2tsn = be32_to_cpu(rhdr->r2tsn);
629         int rc;
630
631         if (tcp_conn->in.datalen) {
632                 iscsi_conn_printk(KERN_ERR, conn,
633                                   "invalid R2t with datalen %d\n",
634                                   tcp_conn->in.datalen);
635                 return ISCSI_ERR_DATALEN;
636         }
637
638         if (tcp_ctask->exp_datasn != r2tsn){
639                 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
640                           __FUNCTION__, tcp_ctask->exp_datasn, r2tsn);
641                 return ISCSI_ERR_R2TSN;
642         }
643
644         /* fill-in new R2T associated with the task */
645         iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
646
647         if (!ctask->sc || session->state != ISCSI_STATE_LOGGED_IN) {
648                 iscsi_conn_printk(KERN_INFO, conn,
649                                   "dropping R2T itt %d in recovery.\n",
650                                   ctask->itt);
651                 return 0;
652         }
653
654         rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
655         BUG_ON(!rc);
656
657         r2t->exp_statsn = rhdr->statsn;
658         r2t->data_length = be32_to_cpu(rhdr->data_length);
659         if (r2t->data_length == 0) {
660                 iscsi_conn_printk(KERN_ERR, conn,
661                                   "invalid R2T with zero data len\n");
662                 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
663                             sizeof(void*));
664                 return ISCSI_ERR_DATALEN;
665         }
666
667         if (r2t->data_length > session->max_burst)
668                 debug_scsi("invalid R2T with data len %u and max burst %u."
669                            "Attempting to execute request.\n",
670                             r2t->data_length, session->max_burst);
671
672         r2t->data_offset = be32_to_cpu(rhdr->data_offset);
673         if (r2t->data_offset + r2t->data_length > scsi_bufflen(ctask->sc)) {
674                 iscsi_conn_printk(KERN_ERR, conn,
675                                   "invalid R2T with data len %u at offset %u "
676                                   "and total length %d\n", r2t->data_length,
677                                   r2t->data_offset, scsi_bufflen(ctask->sc));
678                 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
679                             sizeof(void*));
680                 return ISCSI_ERR_DATALEN;
681         }
682
683         r2t->ttt = rhdr->ttt; /* no flip */
684         r2t->solicit_datasn = 0;
685
686         iscsi_solicit_data_init(conn, ctask, r2t);
687
688         tcp_ctask->exp_datasn = r2tsn + 1;
689         __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
690         conn->r2t_pdus_cnt++;
691
692         iscsi_requeue_ctask(ctask);
693         return 0;
694 }
695
696 /*
697  * Handle incoming reply to DataIn command
698  */
699 static int
700 iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
701                           struct iscsi_segment *segment)
702 {
703         struct iscsi_conn *conn = tcp_conn->iscsi_conn;
704         struct iscsi_hdr *hdr = tcp_conn->in.hdr;
705         int rc;
706
707         if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
708                 return ISCSI_ERR_DATA_DGST;
709
710         /* check for non-exceptional status */
711         if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
712                 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
713                 if (rc)
714                         return rc;
715         }
716
717         iscsi_tcp_hdr_recv_prep(tcp_conn);
718         return 0;
719 }
720
721 /**
722  * iscsi_tcp_hdr_dissect - process PDU header
723  * @conn: iSCSI connection
724  * @hdr: PDU header
725  *
726  * This function analyzes the header of the PDU received,
727  * and performs several sanity checks. If the PDU is accompanied
728  * by data, the receive buffer is set up to copy the incoming data
729  * to the correct location.
730  */
731 static int
732 iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
733 {
734         int rc = 0, opcode, ahslen;
735         struct iscsi_session *session = conn->session;
736         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
737         struct iscsi_cmd_task *ctask;
738         uint32_t itt;
739
740         /* verify PDU length */
741         tcp_conn->in.datalen = ntoh24(hdr->dlength);
742         if (tcp_conn->in.datalen > conn->max_recv_dlength) {
743                 iscsi_conn_printk(KERN_ERR, conn,
744                                   "iscsi_tcp: datalen %d > %d\n",
745                                   tcp_conn->in.datalen, conn->max_recv_dlength);
746                 return ISCSI_ERR_DATALEN;
747         }
748
749         /* Additional header segments. So far, we don't
750          * process additional headers.
751          */
752         ahslen = hdr->hlength << 2;
753
754         opcode = hdr->opcode & ISCSI_OPCODE_MASK;
755         /* verify itt (itt encoding: age+cid+itt) */
756         rc = iscsi_verify_itt(conn, hdr, &itt);
757         if (rc)
758                 return rc;
759
760         debug_tcp("opcode 0x%x ahslen %d datalen %d\n",
761                   opcode, ahslen, tcp_conn->in.datalen);
762
763         switch(opcode) {
764         case ISCSI_OP_SCSI_DATA_IN:
765                 ctask = session->cmds[itt];
766                 spin_lock(&conn->session->lock);
767                 rc = iscsi_data_rsp(conn, ctask);
768                 spin_unlock(&conn->session->lock);
769                 if (rc)
770                         return rc;
771                 if (tcp_conn->in.datalen) {
772                         struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
773                         struct hash_desc *rx_hash = NULL;
774
775                         /*
776                          * Setup copy of Data-In into the Scsi_Cmnd
777                          * Scatterlist case:
778                          * We set up the iscsi_segment to point to the next
779                          * scatterlist entry to copy to. As we go along,
780                          * we move on to the next scatterlist entry and
781                          * update the digest per-entry.
782                          */
783                         if (conn->datadgst_en)
784                                 rx_hash = &tcp_conn->rx_hash;
785
786                         debug_tcp("iscsi_tcp_begin_data_in(%p, offset=%d, "
787                                   "datalen=%d)\n", tcp_conn,
788                                   tcp_ctask->data_offset,
789                                   tcp_conn->in.datalen);
790                         return iscsi_segment_seek_sg(&tcp_conn->in.segment,
791                                                      scsi_sglist(ctask->sc),
792                                                      scsi_sg_count(ctask->sc),
793                                                      tcp_ctask->data_offset,
794                                                      tcp_conn->in.datalen,
795                                                      iscsi_tcp_process_data_in,
796                                                      rx_hash);
797                 }
798                 /* fall through */
799         case ISCSI_OP_SCSI_CMD_RSP:
800                 if (tcp_conn->in.datalen) {
801                         iscsi_tcp_data_recv_prep(tcp_conn);
802                         return 0;
803                 }
804                 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
805                 break;
806         case ISCSI_OP_R2T:
807                 ctask = session->cmds[itt];
808                 if (ahslen)
809                         rc = ISCSI_ERR_AHSLEN;
810                 else if (ctask->sc->sc_data_direction == DMA_TO_DEVICE) {
811                         spin_lock(&session->lock);
812                         rc = iscsi_r2t_rsp(conn, ctask);
813                         spin_unlock(&session->lock);
814                 } else
815                         rc = ISCSI_ERR_PROTO;
816                 break;
817         case ISCSI_OP_LOGIN_RSP:
818         case ISCSI_OP_TEXT_RSP:
819         case ISCSI_OP_REJECT:
820         case ISCSI_OP_ASYNC_EVENT:
821                 /*
822                  * It is possible that we could get a PDU with a buffer larger
823                  * than 8K, but there are no targets that currently do this.
824                  * For now we fail until we find a vendor that needs it
825                  */
826                 if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
827                         iscsi_conn_printk(KERN_ERR, conn,
828                                           "iscsi_tcp: received buffer of "
829                                           "len %u but conn buffer is only %u "
830                                           "(opcode %0x)\n",
831                                           tcp_conn->in.datalen,
832                                           ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
833                         rc = ISCSI_ERR_PROTO;
834                         break;
835                 }
836
837                 /* If there's data coming in with the response,
838                  * receive it to the connection's buffer.
839                  */
840                 if (tcp_conn->in.datalen) {
841                         iscsi_tcp_data_recv_prep(tcp_conn);
842                         return 0;
843                 }
844         /* fall through */
845         case ISCSI_OP_LOGOUT_RSP:
846         case ISCSI_OP_NOOP_IN:
847         case ISCSI_OP_SCSI_TMFUNC_RSP:
848                 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
849                 break;
850         default:
851                 rc = ISCSI_ERR_BAD_OPCODE;
852                 break;
853         }
854
855         if (rc == 0) {
856                 /* Anything that comes with data should have
857                  * been handled above. */
858                 if (tcp_conn->in.datalen)
859                         return ISCSI_ERR_PROTO;
860                 iscsi_tcp_hdr_recv_prep(tcp_conn);
861         }
862
863         return rc;
864 }
865
866 /**
867  * iscsi_tcp_hdr_recv_done - process PDU header
868  *
869  * This is the callback invoked when the PDU header has
870  * been received. If the header is followed by additional
871  * header segments, we go back for more data.
872  */
873 static int
874 iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
875                         struct iscsi_segment *segment)
876 {
877         struct iscsi_conn *conn = tcp_conn->iscsi_conn;
878         struct iscsi_hdr *hdr;
879
880         /* Check if there are additional header segments
881          * *prior* to computing the digest, because we
882          * may need to go back to the caller for more.
883          */
884         hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
885         if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
886                 /* Bump the header length - the caller will
887                  * just loop around and get the AHS for us, and
888                  * call again. */
889                 unsigned int ahslen = hdr->hlength << 2;
890
891                 /* Make sure we don't overflow */
892                 if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
893                         return ISCSI_ERR_AHSLEN;
894
895                 segment->total_size += ahslen;
896                 segment->size += ahslen;
897                 return 0;
898         }
899
900         /* We're done processing the header. See if we're doing
901          * header digests; if so, set up the recv_digest buffer
902          * and go back for more. */
903         if (conn->hdrdgst_en) {
904                 if (segment->digest_len == 0) {
905                         iscsi_tcp_segment_splice_digest(segment,
906                                                         segment->recv_digest);
907                         return 0;
908                 }
909                 iscsi_tcp_dgst_header(&tcp_conn->rx_hash, hdr,
910                                       segment->total_copied - ISCSI_DIGEST_SIZE,
911                                       segment->digest);
912
913                 if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
914                         return ISCSI_ERR_HDR_DGST;
915         }
916
917         tcp_conn->in.hdr = hdr;
918         return iscsi_tcp_hdr_dissect(conn, hdr);
919 }
920
921 /**
922  * iscsi_tcp_recv - TCP receive in sendfile fashion
923  * @rd_desc: read descriptor
924  * @skb: socket buffer
925  * @offset: offset in skb
926  * @len: skb->len - offset
927  **/
928 static int
929 iscsi_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
930                unsigned int offset, size_t len)
931 {
932         struct iscsi_conn *conn = rd_desc->arg.data;
933         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
934         struct iscsi_segment *segment = &tcp_conn->in.segment;
935         struct skb_seq_state seq;
936         unsigned int consumed = 0;
937         int rc = 0;
938
939         debug_tcp("in %d bytes\n", skb->len - offset);
940
941         if (unlikely(conn->suspend_rx)) {
942                 debug_tcp("conn %d Rx suspended!\n", conn->id);
943                 return 0;
944         }
945
946         skb_prepare_seq_read(skb, offset, skb->len, &seq);
947         while (1) {
948                 unsigned int avail;
949                 const u8 *ptr;
950
951                 avail = skb_seq_read(consumed, &ptr, &seq);
952                 if (avail == 0) {
953                         debug_tcp("no more data avail. Consumed %d\n",
954                                   consumed);
955                         break;
956                 }
957                 BUG_ON(segment->copied >= segment->size);
958
959                 debug_tcp("skb %p ptr=%p avail=%u\n", skb, ptr, avail);
960                 rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
961                 BUG_ON(rc == 0);
962                 consumed += rc;
963
964                 if (segment->total_copied >= segment->total_size) {
965                         debug_tcp("segment done\n");
966                         rc = segment->done(tcp_conn, segment);
967                         if (rc != 0) {
968                                 skb_abort_seq_read(&seq);
969                                 goto error;
970                         }
971
972                         /* The done() functions sets up the
973                          * next segment. */
974                 }
975         }
976         skb_abort_seq_read(&seq);
977         conn->rxdata_octets += consumed;
978         return consumed;
979
980 error:
981         debug_tcp("Error receiving PDU, errno=%d\n", rc);
982         iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
983         return 0;
984 }
985
986 static void
987 iscsi_tcp_data_ready(struct sock *sk, int flag)
988 {
989         struct iscsi_conn *conn = sk->sk_user_data;
990         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
991         read_descriptor_t rd_desc;
992
993         read_lock(&sk->sk_callback_lock);
994
995         /*
996          * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
997          * We set count to 1 because we want the network layer to
998          * hand us all the skbs that are available. iscsi_tcp_recv
999          * handled pdus that cross buffers or pdus that still need data.
1000          */
1001         rd_desc.arg.data = conn;
1002         rd_desc.count = 1;
1003         tcp_read_sock(sk, &rd_desc, iscsi_tcp_recv);
1004
1005         read_unlock(&sk->sk_callback_lock);
1006
1007         /* If we had to (atomically) map a highmem page,
1008          * unmap it now. */
1009         iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
1010 }
1011
1012 static void
1013 iscsi_tcp_state_change(struct sock *sk)
1014 {
1015         struct iscsi_tcp_conn *tcp_conn;
1016         struct iscsi_conn *conn;
1017         struct iscsi_session *session;
1018         void (*old_state_change)(struct sock *);
1019
1020         read_lock(&sk->sk_callback_lock);
1021
1022         conn = (struct iscsi_conn*)sk->sk_user_data;
1023         session = conn->session;
1024
1025         if ((sk->sk_state == TCP_CLOSE_WAIT ||
1026              sk->sk_state == TCP_CLOSE) &&
1027             !atomic_read(&sk->sk_rmem_alloc)) {
1028                 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1029                 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1030         }
1031
1032         tcp_conn = conn->dd_data;
1033         old_state_change = tcp_conn->old_state_change;
1034
1035         read_unlock(&sk->sk_callback_lock);
1036
1037         old_state_change(sk);
1038 }
1039
1040 /**
1041  * iscsi_write_space - Called when more output buffer space is available
1042  * @sk: socket space is available for
1043  **/
1044 static void
1045 iscsi_write_space(struct sock *sk)
1046 {
1047         struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
1048         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1049
1050         tcp_conn->old_write_space(sk);
1051         debug_tcp("iscsi_write_space: cid %d\n", conn->id);
1052         scsi_queue_work(conn->session->host, &conn->xmitwork);
1053 }
1054
1055 static void
1056 iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1057 {
1058         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1059         struct sock *sk = tcp_conn->sock->sk;
1060
1061         /* assign new callbacks */
1062         write_lock_bh(&sk->sk_callback_lock);
1063         sk->sk_user_data = conn;
1064         tcp_conn->old_data_ready = sk->sk_data_ready;
1065         tcp_conn->old_state_change = sk->sk_state_change;
1066         tcp_conn->old_write_space = sk->sk_write_space;
1067         sk->sk_data_ready = iscsi_tcp_data_ready;
1068         sk->sk_state_change = iscsi_tcp_state_change;
1069         sk->sk_write_space = iscsi_write_space;
1070         write_unlock_bh(&sk->sk_callback_lock);
1071 }
1072
1073 static void
1074 iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
1075 {
1076         struct sock *sk = tcp_conn->sock->sk;
1077
1078         /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1079         write_lock_bh(&sk->sk_callback_lock);
1080         sk->sk_user_data    = NULL;
1081         sk->sk_data_ready   = tcp_conn->old_data_ready;
1082         sk->sk_state_change = tcp_conn->old_state_change;
1083         sk->sk_write_space  = tcp_conn->old_write_space;
1084         sk->sk_no_check  = 0;
1085         write_unlock_bh(&sk->sk_callback_lock);
1086 }
1087
1088 /**
1089  * iscsi_xmit - TCP transmit
1090  **/
1091 static int
1092 iscsi_xmit(struct iscsi_conn *conn)
1093 {
1094         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1095         struct iscsi_segment *segment = &tcp_conn->out.segment;
1096         unsigned int consumed = 0;
1097         int rc = 0;
1098
1099         while (1) {
1100                 rc = iscsi_tcp_xmit_segment(tcp_conn, segment);
1101                 if (rc < 0)
1102                         goto error;
1103                 if (rc == 0)
1104                         break;
1105
1106                 consumed += rc;
1107
1108                 if (segment->total_copied >= segment->total_size) {
1109                         if (segment->done != NULL) {
1110                                 rc = segment->done(tcp_conn, segment);
1111                                 if (rc < 0)
1112                                         goto error;
1113                         }
1114                 }
1115         }
1116
1117         debug_tcp("xmit %d bytes\n", consumed);
1118
1119         conn->txdata_octets += consumed;
1120         return consumed;
1121
1122 error:
1123         /* Transmit error. We could initiate error recovery
1124          * here. */
1125         debug_tcp("Error sending PDU, errno=%d\n", rc);
1126         iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1127         return rc;
1128 }
1129
1130 /**
1131  * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
1132  */
1133 static inline int
1134 iscsi_tcp_xmit_qlen(struct iscsi_conn *conn)
1135 {
1136         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1137         struct iscsi_segment *segment = &tcp_conn->out.segment;
1138
1139         return segment->total_copied - segment->total_size;
1140 }
1141
1142 static inline int
1143 iscsi_tcp_flush(struct iscsi_conn *conn)
1144 {
1145         int rc;
1146
1147         while (iscsi_tcp_xmit_qlen(conn)) {
1148                 rc = iscsi_xmit(conn);
1149                 if (rc == 0)
1150                         return -EAGAIN;
1151                 if (rc < 0)
1152                         return rc;
1153         }
1154
1155         return 0;
1156 }
1157
1158 /*
1159  * This is called when we're done sending the header.
1160  * Simply copy the data_segment to the send segment, and return.
1161  */
1162 static int
1163 iscsi_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
1164                         struct iscsi_segment *segment)
1165 {
1166         tcp_conn->out.segment = tcp_conn->out.data_segment;
1167         debug_tcp("Header done. Next segment size %u total_size %u\n",
1168                   tcp_conn->out.segment.size, tcp_conn->out.segment.total_size);
1169         return 0;
1170 }
1171
1172 static void
1173 iscsi_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr, size_t hdrlen)
1174 {
1175         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1176
1177         debug_tcp("%s(%p%s)\n", __FUNCTION__, tcp_conn,
1178                         conn->hdrdgst_en? ", digest enabled" : "");
1179
1180         /* Clear the data segment - needs to be filled in by the
1181          * caller using iscsi_tcp_send_data_prep() */
1182         memset(&tcp_conn->out.data_segment, 0, sizeof(struct iscsi_segment));
1183
1184         /* If header digest is enabled, compute the CRC and
1185          * place the digest into the same buffer. We make
1186          * sure that both iscsi_tcp_ctask and mtask have
1187          * sufficient room.
1188          */
1189         if (conn->hdrdgst_en) {
1190                 iscsi_tcp_dgst_header(&tcp_conn->tx_hash, hdr, hdrlen,
1191                                       hdr + hdrlen);
1192                 hdrlen += ISCSI_DIGEST_SIZE;
1193         }
1194
1195         /* Remember header pointer for later, when we need
1196          * to decide whether there's a payload to go along
1197          * with the header. */
1198         tcp_conn->out.hdr = hdr;
1199
1200         iscsi_segment_init_linear(&tcp_conn->out.segment, hdr, hdrlen,
1201                                 iscsi_tcp_send_hdr_done, NULL);
1202 }
1203
1204 /*
1205  * Prepare the send buffer for the payload data.
1206  * Padding and checksumming will all be taken care
1207  * of by the iscsi_segment routines.
1208  */
1209 static int
1210 iscsi_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
1211                          unsigned int count, unsigned int offset,
1212                          unsigned int len)
1213 {
1214         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1215         struct hash_desc *tx_hash = NULL;
1216         unsigned int hdr_spec_len;
1217
1218         debug_tcp("%s(%p, offset=%d, datalen=%d%s)\n", __FUNCTION__,
1219                         tcp_conn, offset, len,
1220                         conn->datadgst_en? ", digest enabled" : "");
1221
1222         /* Make sure the datalen matches what the caller
1223            said he would send. */
1224         hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
1225         WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
1226
1227         if (conn->datadgst_en)
1228                 tx_hash = &tcp_conn->tx_hash;
1229
1230         return iscsi_segment_seek_sg(&tcp_conn->out.data_segment,
1231                                    sg, count, offset, len,
1232                                    NULL, tx_hash);
1233 }
1234
1235 static void
1236 iscsi_tcp_send_linear_data_prepare(struct iscsi_conn *conn, void *data,
1237                                    size_t len)
1238 {
1239         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1240         struct hash_desc *tx_hash = NULL;
1241         unsigned int hdr_spec_len;
1242
1243         debug_tcp("%s(%p, datalen=%d%s)\n", __FUNCTION__, tcp_conn, len,
1244                   conn->datadgst_en? ", digest enabled" : "");
1245
1246         /* Make sure the datalen matches what the caller
1247            said he would send. */
1248         hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
1249         WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
1250
1251         if (conn->datadgst_en)
1252                 tx_hash = &tcp_conn->tx_hash;
1253
1254         iscsi_segment_init_linear(&tcp_conn->out.data_segment,
1255                                 data, len, NULL, tx_hash);
1256 }
1257
1258 /**
1259  * iscsi_solicit_data_cont - initialize next Data-Out
1260  * @conn: iscsi connection
1261  * @ctask: scsi command task
1262  * @r2t: R2T info
1263  * @left: bytes left to transfer
1264  *
1265  * Notes:
1266  *      Initialize next Data-Out within this R2T sequence and continue
1267  *      to process next Scatter-Gather element(if any) of this SCSI command.
1268  *
1269  *      Called under connection lock.
1270  **/
1271 static int
1272 iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1273                         struct iscsi_r2t_info *r2t)
1274 {
1275         struct iscsi_data *hdr;
1276         int new_offset, left;
1277
1278         BUG_ON(r2t->data_length - r2t->sent < 0);
1279         left = r2t->data_length - r2t->sent;
1280         if (left == 0)
1281                 return 0;
1282
1283         hdr = &r2t->dtask.hdr;
1284         memset(hdr, 0, sizeof(struct iscsi_data));
1285         hdr->ttt = r2t->ttt;
1286         hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1287         r2t->solicit_datasn++;
1288         hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
1289         memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1290         hdr->itt = ctask->hdr->itt;
1291         hdr->exp_statsn = r2t->exp_statsn;
1292         new_offset = r2t->data_offset + r2t->sent;
1293         hdr->offset = cpu_to_be32(new_offset);
1294         if (left > conn->max_xmit_dlength) {
1295                 hton24(hdr->dlength, conn->max_xmit_dlength);
1296                 r2t->data_count = conn->max_xmit_dlength;
1297         } else {
1298                 hton24(hdr->dlength, left);
1299                 r2t->data_count = left;
1300                 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1301         }
1302
1303         conn->dataout_pdus_cnt++;
1304         return 1;
1305 }
1306
1307 /**
1308  * iscsi_tcp_ctask - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
1309  * @conn: iscsi connection
1310  * @ctask: scsi command task
1311  * @sc: scsi command
1312  **/
1313 static int
1314 iscsi_tcp_ctask_init(struct iscsi_cmd_task *ctask)
1315 {
1316         struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1317         struct iscsi_conn *conn = ctask->conn;
1318         struct scsi_cmnd *sc = ctask->sc;
1319         int err;
1320
1321         BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
1322         tcp_ctask->sent = 0;
1323         tcp_ctask->exp_datasn = 0;
1324
1325         /* Prepare PDU, optionally w/ immediate data */
1326         debug_scsi("ctask deq [cid %d itt 0x%x imm %d unsol %d]\n",
1327                     conn->id, ctask->itt, ctask->imm_count,
1328                     ctask->unsol_count);
1329         iscsi_tcp_send_hdr_prep(conn, ctask->hdr, ctask->hdr_len);
1330
1331         if (!ctask->imm_count)
1332                 return 0;
1333
1334         /* If we have immediate data, attach a payload */
1335         err = iscsi_tcp_send_data_prep(conn, scsi_sglist(sc), scsi_sg_count(sc),
1336                                        0, ctask->imm_count);
1337         if (err)
1338                 return err;
1339         tcp_ctask->sent += ctask->imm_count;
1340         ctask->imm_count = 0;
1341         return 0;
1342 }
1343
1344 /**
1345  * iscsi_tcp_mtask_xmit - xmit management(immediate) task
1346  * @conn: iscsi connection
1347  * @mtask: task management task
1348  *
1349  * Notes:
1350  *      The function can return -EAGAIN in which case caller must
1351  *      call it again later, or recover. '0' return code means successful
1352  *      xmit.
1353  **/
1354 static int
1355 iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
1356 {
1357         int rc;
1358
1359         /* Flush any pending data first. */
1360         rc = iscsi_tcp_flush(conn);
1361         if (rc < 0)
1362                 return rc;
1363
1364         if (mtask->hdr->itt == RESERVED_ITT) {
1365                 struct iscsi_session *session = conn->session;
1366
1367                 spin_lock_bh(&session->lock);
1368                 iscsi_free_mgmt_task(conn, mtask);
1369                 spin_unlock_bh(&session->lock);
1370         }
1371
1372         return 0;
1373 }
1374
1375 /*
1376  * iscsi_tcp_ctask_xmit - xmit normal PDU task
1377  * @conn: iscsi connection
1378  * @ctask: iscsi command task
1379  *
1380  * We're expected to return 0 when everything was transmitted succesfully,
1381  * -EAGAIN if there's still data in the queue, or != 0 for any other kind
1382  * of error.
1383  */
1384 static int
1385 iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1386 {
1387         struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1388         struct scsi_cmnd *sc = ctask->sc;
1389         int rc = 0;
1390
1391 flush:
1392         /* Flush any pending data first. */
1393         rc = iscsi_tcp_flush(conn);
1394         if (rc < 0)
1395                 return rc;
1396
1397         /* Are we done already? */
1398         if (sc->sc_data_direction != DMA_TO_DEVICE)
1399                 return 0;
1400
1401         if (ctask->unsol_count != 0) {
1402                 struct iscsi_data *hdr = &tcp_ctask->unsol_dtask.hdr;
1403
1404                 /* Prepare a header for the unsolicited PDU.
1405                  * The amount of data we want to send will be
1406                  * in ctask->data_count.
1407                  * FIXME: return the data count instead.
1408                  */
1409                 iscsi_prep_unsolicit_data_pdu(ctask, hdr);
1410
1411                 debug_tcp("unsol dout [itt 0x%x doff %d dlen %d]\n",
1412                                 ctask->itt, tcp_ctask->sent, ctask->data_count);
1413
1414                 iscsi_tcp_send_hdr_prep(conn, hdr, sizeof(*hdr));
1415                 rc = iscsi_tcp_send_data_prep(conn, scsi_sglist(sc),
1416                                               scsi_sg_count(sc),
1417                                               tcp_ctask->sent,
1418                                               ctask->data_count);
1419                 if (rc)
1420                         goto fail;
1421                 tcp_ctask->sent += ctask->data_count;
1422                 ctask->unsol_count -= ctask->data_count;
1423                 goto flush;
1424         } else {
1425                 struct iscsi_session *session = conn->session;
1426                 struct iscsi_r2t_info *r2t;
1427
1428                 /* All unsolicited PDUs sent. Check for solicited PDUs.
1429                  */
1430                 spin_lock_bh(&session->lock);
1431                 r2t = tcp_ctask->r2t;
1432                 if (r2t != NULL) {
1433                         /* Continue with this R2T? */
1434                         if (!iscsi_solicit_data_cont(conn, ctask, r2t)) {
1435                                 debug_scsi("  done with r2t %p\n", r2t);
1436
1437                                 __kfifo_put(tcp_ctask->r2tpool.queue,
1438                                             (void*)&r2t, sizeof(void*));
1439                                 tcp_ctask->r2t = r2t = NULL;
1440                         }
1441                 }
1442
1443                 if (r2t == NULL) {
1444                         __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
1445                                     sizeof(void*));
1446                         r2t = tcp_ctask->r2t;
1447                 }
1448                 spin_unlock_bh(&session->lock);
1449
1450                 /* Waiting for more R2Ts to arrive. */
1451                 if (r2t == NULL) {
1452                         debug_tcp("no R2Ts yet\n");
1453                         return 0;
1454                 }
1455
1456                 debug_scsi("sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
1457                         r2t, r2t->solicit_datasn - 1, ctask->itt,
1458                         r2t->data_offset + r2t->sent, r2t->data_count);
1459
1460                 iscsi_tcp_send_hdr_prep(conn, &r2t->dtask.hdr,
1461                                         sizeof(struct iscsi_hdr));
1462
1463                 rc = iscsi_tcp_send_data_prep(conn, scsi_sglist(sc),
1464                                               scsi_sg_count(sc),
1465                                               r2t->data_offset + r2t->sent,
1466                                               r2t->data_count);
1467                 if (rc)
1468                         goto fail;
1469                 tcp_ctask->sent += r2t->data_count;
1470                 r2t->sent += r2t->data_count;
1471                 goto flush;
1472         }
1473         return 0;
1474 fail:
1475         iscsi_conn_failure(conn, rc);
1476         return -EIO;
1477 }
1478
1479 static struct iscsi_cls_conn *
1480 iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1481 {
1482         struct iscsi_conn *conn;
1483         struct iscsi_cls_conn *cls_conn;
1484         struct iscsi_tcp_conn *tcp_conn;
1485
1486         cls_conn = iscsi_conn_setup(cls_session, conn_idx);
1487         if (!cls_conn)
1488                 return NULL;
1489         conn = cls_conn->dd_data;
1490         /*
1491          * due to strange issues with iser these are not set
1492          * in iscsi_conn_setup
1493          */
1494         conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
1495
1496         tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
1497         if (!tcp_conn)
1498                 goto tcp_conn_alloc_fail;
1499
1500         conn->dd_data = tcp_conn;
1501         tcp_conn->iscsi_conn = conn;
1502
1503         tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1504                                                   CRYPTO_ALG_ASYNC);
1505         tcp_conn->tx_hash.flags = 0;
1506         if (IS_ERR(tcp_conn->tx_hash.tfm))
1507                 goto free_tcp_conn;
1508
1509         tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1510                                                   CRYPTO_ALG_ASYNC);
1511         tcp_conn->rx_hash.flags = 0;
1512         if (IS_ERR(tcp_conn->rx_hash.tfm))
1513                 goto free_tx_tfm;
1514
1515         return cls_conn;
1516
1517 free_tx_tfm:
1518         crypto_free_hash(tcp_conn->tx_hash.tfm);
1519 free_tcp_conn:
1520         iscsi_conn_printk(KERN_ERR, conn,
1521                           "Could not create connection due to crc32c "
1522                           "loading error. Make sure the crc32c "
1523                           "module is built as a module or into the "
1524                           "kernel\n");
1525         kfree(tcp_conn);
1526 tcp_conn_alloc_fail:
1527         iscsi_conn_teardown(cls_conn);
1528         return NULL;
1529 }
1530
1531 static void
1532 iscsi_tcp_release_conn(struct iscsi_conn *conn)
1533 {
1534         struct iscsi_session *session = conn->session;
1535         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1536         struct socket *sock = tcp_conn->sock;
1537
1538         if (!sock)
1539                 return;
1540
1541         sock_hold(sock->sk);
1542         iscsi_conn_restore_callbacks(tcp_conn);
1543         sock_put(sock->sk);
1544
1545         spin_lock_bh(&session->lock);
1546         tcp_conn->sock = NULL;
1547         conn->recv_lock = NULL;
1548         spin_unlock_bh(&session->lock);
1549         sockfd_put(sock);
1550 }
1551
1552 static void
1553 iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
1554 {
1555         struct iscsi_conn *conn = cls_conn->dd_data;
1556         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1557
1558         iscsi_tcp_release_conn(conn);
1559         iscsi_conn_teardown(cls_conn);
1560
1561         if (tcp_conn->tx_hash.tfm)
1562                 crypto_free_hash(tcp_conn->tx_hash.tfm);
1563         if (tcp_conn->rx_hash.tfm)
1564                 crypto_free_hash(tcp_conn->rx_hash.tfm);
1565
1566         kfree(tcp_conn);
1567 }
1568
1569 static void
1570 iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1571 {
1572         struct iscsi_conn *conn = cls_conn->dd_data;
1573
1574         iscsi_conn_stop(cls_conn, flag);
1575         iscsi_tcp_release_conn(conn);
1576 }
1577
1578 static int iscsi_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
1579                               char *buf, int *port,
1580                               int (*getname)(struct socket *, struct sockaddr *,
1581                                         int *addrlen))
1582 {
1583         struct sockaddr_storage *addr;
1584         struct sockaddr_in6 *sin6;
1585         struct sockaddr_in *sin;
1586         int rc = 0, len;
1587
1588         addr = kmalloc(sizeof(*addr), GFP_KERNEL);
1589         if (!addr)
1590                 return -ENOMEM;
1591
1592         if (getname(sock, (struct sockaddr *) addr, &len)) {
1593                 rc = -ENODEV;
1594                 goto free_addr;
1595         }
1596
1597         switch (addr->ss_family) {
1598         case AF_INET:
1599                 sin = (struct sockaddr_in *)addr;
1600                 spin_lock_bh(&conn->session->lock);
1601                 sprintf(buf, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr));
1602                 *port = be16_to_cpu(sin->sin_port);
1603                 spin_unlock_bh(&conn->session->lock);
1604                 break;
1605         case AF_INET6:
1606                 sin6 = (struct sockaddr_in6 *)addr;
1607                 spin_lock_bh(&conn->session->lock);
1608                 sprintf(buf, NIP6_FMT, NIP6(sin6->sin6_addr));
1609                 *port = be16_to_cpu(sin6->sin6_port);
1610                 spin_unlock_bh(&conn->session->lock);
1611                 break;
1612         }
1613 free_addr:
1614         kfree(addr);
1615         return rc;
1616 }
1617
1618 static int
1619 iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
1620                     struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
1621                     int is_leading)
1622 {
1623         struct iscsi_conn *conn = cls_conn->dd_data;
1624         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1625         struct sock *sk;
1626         struct socket *sock;
1627         int err;
1628
1629         /* lookup for existing socket */
1630         sock = sockfd_lookup((int)transport_eph, &err);
1631         if (!sock) {
1632                 iscsi_conn_printk(KERN_ERR, conn,
1633                                   "sockfd_lookup failed %d\n", err);
1634                 return -EEXIST;
1635         }
1636         /*
1637          * copy these values now because if we drop the session
1638          * userspace may still want to query the values since we will
1639          * be using them for the reconnect
1640          */
1641         err = iscsi_tcp_get_addr(conn, sock, conn->portal_address,
1642                                  &conn->portal_port, kernel_getpeername);
1643         if (err)
1644                 goto free_socket;
1645
1646         err = iscsi_tcp_get_addr(conn, sock, conn->local_address,
1647                                 &conn->local_port, kernel_getsockname);
1648         if (err)
1649                 goto free_socket;
1650
1651         err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
1652         if (err)
1653                 goto free_socket;
1654
1655         /* bind iSCSI connection and socket */
1656         tcp_conn->sock = sock;
1657
1658         /* setup Socket parameters */
1659         sk = sock->sk;
1660         sk->sk_reuse = 1;
1661         sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
1662         sk->sk_allocation = GFP_ATOMIC;
1663
1664         /* FIXME: disable Nagle's algorithm */
1665
1666         /*
1667          * Intercept TCP callbacks for sendfile like receive
1668          * processing.
1669          */
1670         conn->recv_lock = &sk->sk_callback_lock;
1671         iscsi_conn_set_callbacks(conn);
1672         tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
1673         /*
1674          * set receive state machine into initial state
1675          */
1676         iscsi_tcp_hdr_recv_prep(tcp_conn);
1677         return 0;
1678
1679 free_socket:
1680         sockfd_put(sock);
1681         return err;
1682 }
1683
1684 /* called with host lock */
1685 static void
1686 iscsi_tcp_mtask_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
1687 {
1688         debug_scsi("mtask deq [cid %d itt 0x%x]\n", conn->id, mtask->itt);
1689
1690         /* Prepare PDU, optionally w/ immediate data */
1691         iscsi_tcp_send_hdr_prep(conn, mtask->hdr, sizeof(*mtask->hdr));
1692
1693         /* If we have immediate data, attach a payload */
1694         if (mtask->data_count)
1695                 iscsi_tcp_send_linear_data_prepare(conn, mtask->data,
1696                                                    mtask->data_count);
1697 }
1698
1699 static int
1700 iscsi_r2tpool_alloc(struct iscsi_session *session)
1701 {
1702         int i;
1703         int cmd_i;
1704
1705         /*
1706          * initialize per-task: R2T pool and xmit queue
1707          */
1708         for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1709                 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1710                 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1711
1712                 /*
1713                  * pre-allocated x4 as much r2ts to handle race when
1714                  * target acks DataOut faster than we data_xmit() queues
1715                  * could replenish r2tqueue.
1716                  */
1717
1718                 /* R2T pool */
1719                 if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4, NULL,
1720                                     sizeof(struct iscsi_r2t_info))) {
1721                         goto r2t_alloc_fail;
1722                 }
1723
1724                 /* R2T xmit queue */
1725                 tcp_ctask->r2tqueue = kfifo_alloc(
1726                       session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
1727                 if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
1728                         iscsi_pool_free(&tcp_ctask->r2tpool);
1729                         goto r2t_alloc_fail;
1730                 }
1731         }
1732
1733         return 0;
1734
1735 r2t_alloc_fail:
1736         for (i = 0; i < cmd_i; i++) {
1737                 struct iscsi_cmd_task *ctask = session->cmds[i];
1738                 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1739
1740                 kfifo_free(tcp_ctask->r2tqueue);
1741                 iscsi_pool_free(&tcp_ctask->r2tpool);
1742         }
1743         return -ENOMEM;
1744 }
1745
1746 static void
1747 iscsi_r2tpool_free(struct iscsi_session *session)
1748 {
1749         int i;
1750
1751         for (i = 0; i < session->cmds_max; i++) {
1752                 struct iscsi_cmd_task *ctask = session->cmds[i];
1753                 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1754
1755                 kfifo_free(tcp_ctask->r2tqueue);
1756                 iscsi_pool_free(&tcp_ctask->r2tpool);
1757         }
1758 }
1759
1760 static int
1761 iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
1762                      char *buf, int buflen)
1763 {
1764         struct iscsi_conn *conn = cls_conn->dd_data;
1765         struct iscsi_session *session = conn->session;
1766         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1767         int value;
1768
1769         switch(param) {
1770         case ISCSI_PARAM_HDRDGST_EN:
1771                 iscsi_set_param(cls_conn, param, buf, buflen);
1772                 break;
1773         case ISCSI_PARAM_DATADGST_EN:
1774                 iscsi_set_param(cls_conn, param, buf, buflen);
1775                 tcp_conn->sendpage = conn->datadgst_en ?
1776                         sock_no_sendpage : tcp_conn->sock->ops->sendpage;
1777                 break;
1778         case ISCSI_PARAM_MAX_R2T:
1779                 sscanf(buf, "%d", &value);
1780                 if (value <= 0 || !is_power_of_2(value))
1781                         return -EINVAL;
1782                 if (session->max_r2t == value)
1783                         break;
1784                 iscsi_r2tpool_free(session);
1785                 iscsi_set_param(cls_conn, param, buf, buflen);
1786                 if (iscsi_r2tpool_alloc(session))
1787                         return -ENOMEM;
1788                 break;
1789         default:
1790                 return iscsi_set_param(cls_conn, param, buf, buflen);
1791         }
1792
1793         return 0;
1794 }
1795
1796 static int
1797 iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
1798                          enum iscsi_param param, char *buf)
1799 {
1800         struct iscsi_conn *conn = cls_conn->dd_data;
1801         int len;
1802
1803         switch(param) {
1804         case ISCSI_PARAM_CONN_PORT:
1805                 spin_lock_bh(&conn->session->lock);
1806                 len = sprintf(buf, "%hu\n", conn->portal_port);
1807                 spin_unlock_bh(&conn->session->lock);
1808                 break;
1809         case ISCSI_PARAM_CONN_ADDRESS:
1810                 spin_lock_bh(&conn->session->lock);
1811                 len = sprintf(buf, "%s\n", conn->portal_address);
1812                 spin_unlock_bh(&conn->session->lock);
1813                 break;
1814         default:
1815                 return iscsi_conn_get_param(cls_conn, param, buf);
1816         }
1817
1818         return len;
1819 }
1820
1821 static int
1822 iscsi_tcp_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
1823                          char *buf)
1824 {
1825         struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1826         int len;
1827
1828         switch (param) {
1829         case ISCSI_HOST_PARAM_IPADDRESS:
1830                 spin_lock_bh(&session->lock);
1831                 if (!session->leadconn)
1832                         len = -ENODEV;
1833                 else
1834                         len = sprintf(buf, "%s\n",
1835                                      session->leadconn->local_address);
1836                 spin_unlock_bh(&session->lock);
1837                 break;
1838         default:
1839                 return iscsi_host_get_param(shost, param, buf);
1840         }
1841         return len;
1842 }
1843
1844 static void
1845 iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
1846 {
1847         struct iscsi_conn *conn = cls_conn->dd_data;
1848         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1849
1850         stats->txdata_octets = conn->txdata_octets;
1851         stats->rxdata_octets = conn->rxdata_octets;
1852         stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
1853         stats->dataout_pdus = conn->dataout_pdus_cnt;
1854         stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
1855         stats->datain_pdus = conn->datain_pdus_cnt;
1856         stats->r2t_pdus = conn->r2t_pdus_cnt;
1857         stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
1858         stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
1859         stats->custom_length = 3;
1860         strcpy(stats->custom[0].desc, "tx_sendpage_failures");
1861         stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
1862         strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
1863         stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
1864         strcpy(stats->custom[2].desc, "eh_abort_cnt");
1865         stats->custom[2].value = conn->eh_abort_cnt;
1866 }
1867
1868 static struct iscsi_cls_session *
1869 iscsi_tcp_session_create(struct iscsi_transport *iscsit,
1870                          struct scsi_transport_template *scsit,
1871                          uint16_t cmds_max, uint16_t qdepth,
1872                          uint32_t initial_cmdsn, uint32_t *hostno)
1873 {
1874         struct iscsi_cls_session *cls_session;
1875         struct iscsi_session *session;
1876         uint32_t hn;
1877         int cmd_i;
1878
1879         cls_session = iscsi_session_setup(iscsit, scsit, cmds_max, qdepth,
1880                                          sizeof(struct iscsi_tcp_cmd_task),
1881                                          sizeof(struct iscsi_tcp_mgmt_task),
1882                                          initial_cmdsn, &hn);
1883         if (!cls_session)
1884                 return NULL;
1885         *hostno = hn;
1886
1887         session = class_to_transport_session(cls_session);
1888         for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1889                 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1890                 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1891
1892                 ctask->hdr = &tcp_ctask->hdr.cmd_hdr;
1893                 ctask->hdr_max = sizeof(tcp_ctask->hdr) - ISCSI_DIGEST_SIZE;
1894         }
1895
1896         for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1897                 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1898                 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
1899
1900                 mtask->hdr = (struct iscsi_hdr *) &tcp_mtask->hdr;
1901         }
1902
1903         if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
1904                 goto r2tpool_alloc_fail;
1905
1906         return cls_session;
1907
1908 r2tpool_alloc_fail:
1909         iscsi_session_teardown(cls_session);
1910         return NULL;
1911 }
1912
1913 static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
1914 {
1915         iscsi_r2tpool_free(class_to_transport_session(cls_session));
1916         iscsi_session_teardown(cls_session);
1917 }
1918
1919 static int iscsi_tcp_slave_configure(struct scsi_device *sdev)
1920 {
1921         blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
1922         blk_queue_dma_alignment(sdev->request_queue, 0);
1923         return 0;
1924 }
1925
1926 static struct scsi_host_template iscsi_sht = {
1927         .module                 = THIS_MODULE,
1928         .name                   = "iSCSI Initiator over TCP/IP",
1929         .queuecommand           = iscsi_queuecommand,
1930         .change_queue_depth     = iscsi_change_queue_depth,
1931         .can_queue              = ISCSI_DEF_XMIT_CMDS_MAX - 1,
1932         .sg_tablesize           = 4096,
1933         .max_sectors            = 0xFFFF,
1934         .cmd_per_lun            = ISCSI_DEF_CMD_PER_LUN,
1935         .eh_abort_handler       = iscsi_eh_abort,
1936         .eh_device_reset_handler= iscsi_eh_device_reset,
1937         .eh_host_reset_handler  = iscsi_eh_host_reset,
1938         .use_clustering         = DISABLE_CLUSTERING,
1939         .slave_configure        = iscsi_tcp_slave_configure,
1940         .proc_name              = "iscsi_tcp",
1941         .this_id                = -1,
1942 };
1943
1944 static struct iscsi_transport iscsi_tcp_transport = {
1945         .owner                  = THIS_MODULE,
1946         .name                   = "tcp",
1947         .caps                   = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
1948                                   | CAP_DATADGST,
1949         .param_mask             = ISCSI_MAX_RECV_DLENGTH |
1950                                   ISCSI_MAX_XMIT_DLENGTH |
1951                                   ISCSI_HDRDGST_EN |
1952                                   ISCSI_DATADGST_EN |
1953                                   ISCSI_INITIAL_R2T_EN |
1954                                   ISCSI_MAX_R2T |
1955                                   ISCSI_IMM_DATA_EN |
1956                                   ISCSI_FIRST_BURST |
1957                                   ISCSI_MAX_BURST |
1958                                   ISCSI_PDU_INORDER_EN |
1959                                   ISCSI_DATASEQ_INORDER_EN |
1960                                   ISCSI_ERL |
1961                                   ISCSI_CONN_PORT |
1962                                   ISCSI_CONN_ADDRESS |
1963                                   ISCSI_EXP_STATSN |
1964                                   ISCSI_PERSISTENT_PORT |
1965                                   ISCSI_PERSISTENT_ADDRESS |
1966                                   ISCSI_TARGET_NAME | ISCSI_TPGT |
1967                                   ISCSI_USERNAME | ISCSI_PASSWORD |
1968                                   ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
1969                                   ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
1970                                   ISCSI_LU_RESET_TMO |
1971                                   ISCSI_PING_TMO | ISCSI_RECV_TMO,
1972         .host_param_mask        = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
1973                                   ISCSI_HOST_INITIATOR_NAME |
1974                                   ISCSI_HOST_NETDEV_NAME,
1975         .host_template          = &iscsi_sht,
1976         .conndata_size          = sizeof(struct iscsi_conn),
1977         .max_conn               = 1,
1978         .max_cmd_len            = 16,
1979         /* session management */
1980         .create_session         = iscsi_tcp_session_create,
1981         .destroy_session        = iscsi_tcp_session_destroy,
1982         /* connection management */
1983         .create_conn            = iscsi_tcp_conn_create,
1984         .bind_conn              = iscsi_tcp_conn_bind,
1985         .destroy_conn           = iscsi_tcp_conn_destroy,
1986         .set_param              = iscsi_conn_set_param,
1987         .get_conn_param         = iscsi_tcp_conn_get_param,
1988         .get_session_param      = iscsi_session_get_param,
1989         .start_conn             = iscsi_conn_start,
1990         .stop_conn              = iscsi_tcp_conn_stop,
1991         /* iscsi host params */
1992         .get_host_param         = iscsi_tcp_host_get_param,
1993         .set_host_param         = iscsi_host_set_param,
1994         /* IO */
1995         .send_pdu               = iscsi_conn_send_pdu,
1996         .get_stats              = iscsi_conn_get_stats,
1997         .init_cmd_task          = iscsi_tcp_ctask_init,
1998         .init_mgmt_task         = iscsi_tcp_mtask_init,
1999         .xmit_cmd_task          = iscsi_tcp_ctask_xmit,
2000         .xmit_mgmt_task         = iscsi_tcp_mtask_xmit,
2001         .cleanup_cmd_task       = iscsi_tcp_cleanup_ctask,
2002         /* recovery */
2003         .session_recovery_timedout = iscsi_session_recovery_timedout,
2004 };
2005
2006 static int __init
2007 iscsi_tcp_init(void)
2008 {
2009         if (iscsi_max_lun < 1) {
2010                 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
2011                        iscsi_max_lun);
2012                 return -EINVAL;
2013         }
2014         iscsi_tcp_transport.max_lun = iscsi_max_lun;
2015
2016         if (!iscsi_register_transport(&iscsi_tcp_transport))
2017                 return -ENODEV;
2018
2019         return 0;
2020 }
2021
2022 static void __exit
2023 iscsi_tcp_exit(void)
2024 {
2025         iscsi_unregister_transport(&iscsi_tcp_transport);
2026 }
2027
2028 module_init(iscsi_tcp_init);
2029 module_exit(iscsi_tcp_exit);