[SCSI] attempt to complete r2t with data len greater than max burst
[pandora-kernel.git] / 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/blkdev.h>
33 #include <linux/crypto.h>
34 #include <linux/delay.h>
35 #include <linux/kfifo.h>
36 #include <linux/scatterlist.h>
37 #include <linux/mutex.h>
38 #include <net/tcp.h>
39 #include <scsi/scsi_cmnd.h>
40 #include <scsi/scsi_host.h>
41 #include <scsi/scsi.h>
42 #include <scsi/scsi_transport_iscsi.h>
43
44 #include "iscsi_tcp.h"
45
46 MODULE_AUTHOR("Dmitry Yusupov <dmitry_yus@yahoo.com>, "
47               "Alex Aizman <itn780@yahoo.com>");
48 MODULE_DESCRIPTION("iSCSI/TCP data-path");
49 MODULE_LICENSE("GPL");
50 /* #define DEBUG_TCP */
51 #define DEBUG_ASSERT
52
53 #ifdef DEBUG_TCP
54 #define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
55 #else
56 #define debug_tcp(fmt...)
57 #endif
58
59 #ifndef DEBUG_ASSERT
60 #ifdef BUG_ON
61 #undef BUG_ON
62 #endif
63 #define BUG_ON(expr)
64 #endif
65
66 static unsigned int iscsi_max_lun = 512;
67 module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
68
69 static inline void
70 iscsi_buf_init_iov(struct iscsi_buf *ibuf, char *vbuf, int size)
71 {
72         ibuf->sg.page = virt_to_page(vbuf);
73         ibuf->sg.offset = offset_in_page(vbuf);
74         ibuf->sg.length = size;
75         ibuf->sent = 0;
76         ibuf->use_sendmsg = 1;
77 }
78
79 static inline void
80 iscsi_buf_init_sg(struct iscsi_buf *ibuf, struct scatterlist *sg)
81 {
82         ibuf->sg.page = sg->page;
83         ibuf->sg.offset = sg->offset;
84         ibuf->sg.length = sg->length;
85         /*
86          * Fastpath: sg element fits into single page
87          */
88         if (sg->length + sg->offset <= PAGE_SIZE && !PageSlab(sg->page))
89                 ibuf->use_sendmsg = 0;
90         else
91                 ibuf->use_sendmsg = 1;
92         ibuf->sent = 0;
93 }
94
95 static inline int
96 iscsi_buf_left(struct iscsi_buf *ibuf)
97 {
98         int rc;
99
100         rc = ibuf->sg.length - ibuf->sent;
101         BUG_ON(rc < 0);
102         return rc;
103 }
104
105 static inline void
106 iscsi_hdr_digest(struct iscsi_conn *conn, struct iscsi_buf *buf,
107                  u8* crc)
108 {
109         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
110
111         crypto_digest_digest(tcp_conn->tx_tfm, &buf->sg, 1, crc);
112         buf->sg.length += sizeof(uint32_t);
113 }
114
115 static inline int
116 iscsi_hdr_extract(struct iscsi_tcp_conn *tcp_conn)
117 {
118         struct sk_buff *skb = tcp_conn->in.skb;
119
120         tcp_conn->in.zero_copy_hdr = 0;
121
122         if (tcp_conn->in.copy >= tcp_conn->hdr_size &&
123             tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER) {
124                 /*
125                  * Zero-copy PDU Header: using connection context
126                  * to store header pointer.
127                  */
128                 if (skb_shinfo(skb)->frag_list == NULL &&
129                     !skb_shinfo(skb)->nr_frags) {
130                         tcp_conn->in.hdr = (struct iscsi_hdr *)
131                                 ((char*)skb->data + tcp_conn->in.offset);
132                         tcp_conn->in.zero_copy_hdr = 1;
133                 } else {
134                         /* ignoring return code since we checked
135                          * in.copy before */
136                         skb_copy_bits(skb, tcp_conn->in.offset,
137                                 &tcp_conn->hdr, tcp_conn->hdr_size);
138                         tcp_conn->in.hdr = &tcp_conn->hdr;
139                 }
140                 tcp_conn->in.offset += tcp_conn->hdr_size;
141                 tcp_conn->in.copy -= tcp_conn->hdr_size;
142         } else {
143                 int hdr_remains;
144                 int copylen;
145
146                 /*
147                  * PDU header scattered across SKB's,
148                  * copying it... This'll happen quite rarely.
149                  */
150
151                 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER)
152                         tcp_conn->in.hdr_offset = 0;
153
154                 hdr_remains = tcp_conn->hdr_size - tcp_conn->in.hdr_offset;
155                 BUG_ON(hdr_remains <= 0);
156
157                 copylen = min(tcp_conn->in.copy, hdr_remains);
158                 skb_copy_bits(skb, tcp_conn->in.offset,
159                         (char*)&tcp_conn->hdr + tcp_conn->in.hdr_offset,
160                         copylen);
161
162                 debug_tcp("PDU gather offset %d bytes %d in.offset %d "
163                        "in.copy %d\n", tcp_conn->in.hdr_offset, copylen,
164                        tcp_conn->in.offset, tcp_conn->in.copy);
165
166                 tcp_conn->in.offset += copylen;
167                 tcp_conn->in.copy -= copylen;
168                 if (copylen < hdr_remains)  {
169                         tcp_conn->in_progress = IN_PROGRESS_HEADER_GATHER;
170                         tcp_conn->in.hdr_offset += copylen;
171                         return -EAGAIN;
172                 }
173                 tcp_conn->in.hdr = &tcp_conn->hdr;
174                 tcp_conn->discontiguous_hdr_cnt++;
175                 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
176         }
177
178         return 0;
179 }
180
181 /*
182  * must be called with session lock
183  */
184 static void
185 iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
186 {
187         struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
188         struct iscsi_r2t_info *r2t;
189         struct scsi_cmnd *sc;
190
191         /* flush ctask's r2t queues */
192         while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
193                 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
194                             sizeof(void*));
195                 debug_scsi("iscsi_tcp_cleanup_ctask pending r2t dropped\n");
196         }
197
198         sc = ctask->sc;
199         if (unlikely(!sc))
200                 return;
201
202         tcp_ctask->xmstate = XMSTATE_IDLE;
203         tcp_ctask->r2t = NULL;
204 }
205
206 /**
207  * iscsi_data_rsp - SCSI Data-In Response processing
208  * @conn: iscsi connection
209  * @ctask: scsi command task
210  **/
211 static int
212 iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
213 {
214         int rc;
215         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
216         struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
217         struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
218         struct iscsi_session *session = conn->session;
219         int datasn = be32_to_cpu(rhdr->datasn);
220
221         rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
222         if (rc)
223                 return rc;
224         /*
225          * setup Data-In byte counter (gets decremented..)
226          */
227         ctask->data_count = tcp_conn->in.datalen;
228
229         if (tcp_conn->in.datalen == 0)
230                 return 0;
231
232         if (ctask->datasn != datasn)
233                 return ISCSI_ERR_DATASN;
234
235         ctask->datasn++;
236
237         tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
238         if (tcp_ctask->data_offset + tcp_conn->in.datalen > ctask->total_length)
239                 return ISCSI_ERR_DATA_OFFSET;
240
241         if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
242                 struct scsi_cmnd *sc = ctask->sc;
243
244                 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
245                 if (rhdr->flags & ISCSI_FLAG_DATA_UNDERFLOW) {
246                         int res_count = be32_to_cpu(rhdr->residual_count);
247
248                         if (res_count > 0 &&
249                             res_count <= sc->request_bufflen) {
250                                 sc->resid = res_count;
251                                 sc->result = (DID_OK << 16) | rhdr->cmd_status;
252                         } else
253                                 sc->result = (DID_BAD_TARGET << 16) |
254                                         rhdr->cmd_status;
255                 } else if (rhdr->flags & ISCSI_FLAG_DATA_OVERFLOW) {
256                         sc->resid = be32_to_cpu(rhdr->residual_count);
257                         sc->result = (DID_OK << 16) | rhdr->cmd_status;
258                 } else
259                         sc->result = (DID_OK << 16) | rhdr->cmd_status;
260         }
261
262         conn->datain_pdus_cnt++;
263         return 0;
264 }
265
266 /**
267  * iscsi_solicit_data_init - initialize first Data-Out
268  * @conn: iscsi connection
269  * @ctask: scsi command task
270  * @r2t: R2T info
271  *
272  * Notes:
273  *      Initialize first Data-Out within this R2T sequence and finds
274  *      proper data_offset within this SCSI command.
275  *
276  *      This function is called with connection lock taken.
277  **/
278 static void
279 iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
280                         struct iscsi_r2t_info *r2t)
281 {
282         struct iscsi_data *hdr;
283         struct scsi_cmnd *sc = ctask->sc;
284         struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
285
286         hdr = &r2t->dtask.hdr;
287         memset(hdr, 0, sizeof(struct iscsi_data));
288         hdr->ttt = r2t->ttt;
289         hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
290         r2t->solicit_datasn++;
291         hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
292         memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
293         hdr->itt = ctask->hdr->itt;
294         hdr->exp_statsn = r2t->exp_statsn;
295         hdr->offset = cpu_to_be32(r2t->data_offset);
296         if (r2t->data_length > conn->max_xmit_dlength) {
297                 hton24(hdr->dlength, conn->max_xmit_dlength);
298                 r2t->data_count = conn->max_xmit_dlength;
299                 hdr->flags = 0;
300         } else {
301                 hton24(hdr->dlength, r2t->data_length);
302                 r2t->data_count = r2t->data_length;
303                 hdr->flags = ISCSI_FLAG_CMD_FINAL;
304         }
305         conn->dataout_pdus_cnt++;
306
307         r2t->sent = 0;
308
309         iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
310                            sizeof(struct iscsi_hdr));
311
312         if (sc->use_sg) {
313                 int i, sg_count = 0;
314                 struct scatterlist *sg = sc->request_buffer;
315
316                 r2t->sg = NULL;
317                 for (i = 0; i < sc->use_sg; i++, sg += 1) {
318                         /* FIXME: prefetch ? */
319                         if (sg_count + sg->length > r2t->data_offset) {
320                                 int page_offset;
321
322                                 /* sg page found! */
323
324                                 /* offset within this page */
325                                 page_offset = r2t->data_offset - sg_count;
326
327                                 /* fill in this buffer */
328                                 iscsi_buf_init_sg(&r2t->sendbuf, sg);
329                                 r2t->sendbuf.sg.offset += page_offset;
330                                 r2t->sendbuf.sg.length -= page_offset;
331
332                                 /* xmit logic will continue with next one */
333                                 r2t->sg = sg + 1;
334                                 break;
335                         }
336                         sg_count += sg->length;
337                 }
338                 BUG_ON(r2t->sg == NULL);
339         } else
340                 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
341                             (char*)sc->request_buffer + r2t->data_offset,
342                             r2t->data_count);
343 }
344
345 /**
346  * iscsi_r2t_rsp - iSCSI R2T Response processing
347  * @conn: iscsi connection
348  * @ctask: scsi command task
349  **/
350 static int
351 iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
352 {
353         struct iscsi_r2t_info *r2t;
354         struct iscsi_session *session = conn->session;
355         struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
356         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
357         struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
358         int r2tsn = be32_to_cpu(rhdr->r2tsn);
359         int rc;
360
361         if (tcp_conn->in.datalen) {
362                 printk(KERN_ERR "iscsi_tcp: invalid R2t with datalen %d\n",
363                        tcp_conn->in.datalen);
364                 return ISCSI_ERR_DATALEN;
365         }
366
367         if (tcp_ctask->exp_r2tsn && tcp_ctask->exp_r2tsn != r2tsn)
368                 return ISCSI_ERR_R2TSN;
369
370         rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
371         if (rc)
372                 return rc;
373
374         /* FIXME: use R2TSN to detect missing R2T */
375
376         /* fill-in new R2T associated with the task */
377         spin_lock(&session->lock);
378         if (!ctask->sc || ctask->mtask ||
379              session->state != ISCSI_STATE_LOGGED_IN) {
380                 printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in "
381                        "recovery...\n", ctask->itt);
382                 spin_unlock(&session->lock);
383                 return 0;
384         }
385
386         rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
387         BUG_ON(!rc);
388
389         r2t->exp_statsn = rhdr->statsn;
390         r2t->data_length = be32_to_cpu(rhdr->data_length);
391         if (r2t->data_length == 0) {
392                 printk(KERN_ERR "iscsi_tcp: invalid R2T with zero data len\n");
393                 spin_unlock(&session->lock);
394                 return ISCSI_ERR_DATALEN;
395         }
396
397         if (r2t->data_length > session->max_burst)
398                 debug_scsi("invalid R2T with data len %u and max burst %u."
399                            "Attempting to execute request.\n",
400                             r2t->data_length, session->max_burst);
401
402         r2t->data_offset = be32_to_cpu(rhdr->data_offset);
403         if (r2t->data_offset + r2t->data_length > ctask->total_length) {
404                 spin_unlock(&session->lock);
405                 printk(KERN_ERR "iscsi_tcp: invalid R2T with data len %u at "
406                        "offset %u and total length %d\n", r2t->data_length,
407                        r2t->data_offset, ctask->total_length);
408                 return ISCSI_ERR_DATALEN;
409         }
410
411         r2t->ttt = rhdr->ttt; /* no flip */
412         r2t->solicit_datasn = 0;
413
414         iscsi_solicit_data_init(conn, ctask, r2t);
415
416         tcp_ctask->exp_r2tsn = r2tsn + 1;
417         tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
418         __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
419         list_move_tail(&ctask->running, &conn->xmitqueue);
420
421         scsi_queue_work(session->host, &conn->xmitwork);
422         conn->r2t_pdus_cnt++;
423         spin_unlock(&session->lock);
424
425         return 0;
426 }
427
428 static int
429 iscsi_tcp_hdr_recv(struct iscsi_conn *conn)
430 {
431         int rc = 0, opcode, ahslen;
432         struct iscsi_hdr *hdr;
433         struct iscsi_session *session = conn->session;
434         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
435         uint32_t cdgst, rdgst = 0, itt;
436
437         hdr = tcp_conn->in.hdr;
438
439         /* verify PDU length */
440         tcp_conn->in.datalen = ntoh24(hdr->dlength);
441         if (tcp_conn->in.datalen > conn->max_recv_dlength) {
442                 printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
443                        tcp_conn->in.datalen, conn->max_recv_dlength);
444                 return ISCSI_ERR_DATALEN;
445         }
446         tcp_conn->data_copied = 0;
447
448         /* read AHS */
449         ahslen = hdr->hlength << 2;
450         tcp_conn->in.offset += ahslen;
451         tcp_conn->in.copy -= ahslen;
452         if (tcp_conn->in.copy < 0) {
453                 printk(KERN_ERR "iscsi_tcp: can't handle AHS with length "
454                        "%d bytes\n", ahslen);
455                 return ISCSI_ERR_AHSLEN;
456         }
457
458         /* calculate read padding */
459         tcp_conn->in.padding = tcp_conn->in.datalen & (ISCSI_PAD_LEN-1);
460         if (tcp_conn->in.padding) {
461                 tcp_conn->in.padding = ISCSI_PAD_LEN - tcp_conn->in.padding;
462                 debug_scsi("read padding %d bytes\n", tcp_conn->in.padding);
463         }
464
465         if (conn->hdrdgst_en) {
466                 struct scatterlist sg;
467
468                 sg_init_one(&sg, (u8 *)hdr,
469                             sizeof(struct iscsi_hdr) + ahslen);
470                 crypto_digest_digest(tcp_conn->rx_tfm, &sg, 1, (u8 *)&cdgst);
471                 rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) +
472                                      ahslen);
473                 if (cdgst != rdgst) {
474                         printk(KERN_ERR "iscsi_tcp: hdrdgst error "
475                                "recv 0x%x calc 0x%x\n", rdgst, cdgst);
476                         return ISCSI_ERR_HDR_DGST;
477                 }
478         }
479
480         opcode = hdr->opcode & ISCSI_OPCODE_MASK;
481         /* verify itt (itt encoding: age+cid+itt) */
482         rc = iscsi_verify_itt(conn, hdr, &itt);
483         if (rc == ISCSI_ERR_NO_SCSI_CMD) {
484                 tcp_conn->in.datalen = 0; /* force drop */
485                 return 0;
486         } else if (rc)
487                 return rc;
488
489         debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n",
490                   opcode, tcp_conn->in.offset, tcp_conn->in.copy,
491                   ahslen, tcp_conn->in.datalen);
492
493         switch(opcode) {
494         case ISCSI_OP_SCSI_DATA_IN:
495                 tcp_conn->in.ctask = session->cmds[itt];
496                 rc = iscsi_data_rsp(conn, tcp_conn->in.ctask);
497                 if (rc)
498                         return rc;
499                 /* fall through */
500         case ISCSI_OP_SCSI_CMD_RSP:
501                 tcp_conn->in.ctask = session->cmds[itt];
502                 if (tcp_conn->in.datalen)
503                         goto copy_hdr;
504
505                 spin_lock(&session->lock);
506                 iscsi_tcp_cleanup_ctask(conn, tcp_conn->in.ctask);
507                 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
508                 spin_unlock(&session->lock);
509                 break;
510         case ISCSI_OP_R2T:
511                 tcp_conn->in.ctask = session->cmds[itt];
512                 if (ahslen)
513                         rc = ISCSI_ERR_AHSLEN;
514                 else if (tcp_conn->in.ctask->sc->sc_data_direction ==
515                                                                 DMA_TO_DEVICE)
516                         rc = iscsi_r2t_rsp(conn, tcp_conn->in.ctask);
517                 else
518                         rc = ISCSI_ERR_PROTO;
519                 break;
520         case ISCSI_OP_LOGIN_RSP:
521         case ISCSI_OP_TEXT_RSP:
522         case ISCSI_OP_REJECT:
523         case ISCSI_OP_ASYNC_EVENT:
524                 /*
525                  * It is possible that we could get a PDU with a buffer larger
526                  * than 8K, but there are no targets that currently do this.
527                  * For now we fail until we find a vendor that needs it
528                  */
529                 if (DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH <
530                     tcp_conn->in.datalen) {
531                         printk(KERN_ERR "iscsi_tcp: received buffer of len %u "
532                               "but conn buffer is only %u (opcode %0x)\n",
533                               tcp_conn->in.datalen,
534                               DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, opcode);
535                         rc = ISCSI_ERR_PROTO;
536                         break;
537                 }
538
539                 if (tcp_conn->in.datalen)
540                         goto copy_hdr;
541         /* fall through */
542         case ISCSI_OP_LOGOUT_RSP:
543         case ISCSI_OP_NOOP_IN:
544         case ISCSI_OP_SCSI_TMFUNC_RSP:
545                 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
546                 break;
547         default:
548                 rc = ISCSI_ERR_BAD_OPCODE;
549                 break;
550         }
551
552         return rc;
553
554 copy_hdr:
555         /*
556          * if we did zero copy for the header but we will need multiple
557          * skbs to complete the command then we have to copy the header
558          * for later use
559          */
560         if (tcp_conn->in.zero_copy_hdr && tcp_conn->in.copy <=
561            (tcp_conn->in.datalen + tcp_conn->in.padding +
562             (conn->datadgst_en ? 4 : 0))) {
563                 debug_tcp("Copying header for later use. in.copy %d in.datalen"
564                           " %d\n", tcp_conn->in.copy, tcp_conn->in.datalen);
565                 memcpy(&tcp_conn->hdr, tcp_conn->in.hdr,
566                        sizeof(struct iscsi_hdr));
567                 tcp_conn->in.hdr = &tcp_conn->hdr;
568                 tcp_conn->in.zero_copy_hdr = 0;
569         }
570         return 0;
571 }
572
573 /**
574  * iscsi_ctask_copy - copy skb bits to the destanation cmd task
575  * @conn: iscsi tcp connection
576  * @ctask: scsi command task
577  * @buf: buffer to copy to
578  * @buf_size: size of buffer
579  * @offset: offset within the buffer
580  *
581  * Notes:
582  *      The function calls skb_copy_bits() and updates per-connection and
583  *      per-cmd byte counters.
584  *
585  *      Read counters (in bytes):
586  *
587  *      conn->in.offset         offset within in progress SKB
588  *      conn->in.copy           left to copy from in progress SKB
589  *                              including padding
590  *      conn->in.copied         copied already from in progress SKB
591  *      conn->data_copied       copied already from in progress buffer
592  *      ctask->sent             total bytes sent up to the MidLayer
593  *      ctask->data_count       left to copy from in progress Data-In
594  *      buf_left                left to copy from in progress buffer
595  **/
596 static inline int
597 iscsi_ctask_copy(struct iscsi_tcp_conn *tcp_conn, struct iscsi_cmd_task *ctask,
598                 void *buf, int buf_size, int offset)
599 {
600         struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
601         int buf_left = buf_size - (tcp_conn->data_copied + offset);
602         int size = min(tcp_conn->in.copy, buf_left);
603         int rc;
604
605         size = min(size, ctask->data_count);
606
607         debug_tcp("ctask_copy %d bytes at offset %d copied %d\n",
608                size, tcp_conn->in.offset, tcp_conn->in.copied);
609
610         BUG_ON(size <= 0);
611         BUG_ON(tcp_ctask->sent + size > ctask->total_length);
612
613         rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
614                            (char*)buf + (offset + tcp_conn->data_copied), size);
615         /* must fit into skb->len */
616         BUG_ON(rc);
617
618         tcp_conn->in.offset += size;
619         tcp_conn->in.copy -= size;
620         tcp_conn->in.copied += size;
621         tcp_conn->data_copied += size;
622         tcp_ctask->sent += size;
623         ctask->data_count -= size;
624
625         BUG_ON(tcp_conn->in.copy < 0);
626         BUG_ON(ctask->data_count < 0);
627
628         if (buf_size != (tcp_conn->data_copied + offset)) {
629                 if (!ctask->data_count) {
630                         BUG_ON(buf_size - tcp_conn->data_copied < 0);
631                         /* done with this PDU */
632                         return buf_size - tcp_conn->data_copied;
633                 }
634                 return -EAGAIN;
635         }
636
637         /* done with this buffer or with both - PDU and buffer */
638         tcp_conn->data_copied = 0;
639         return 0;
640 }
641
642 /**
643  * iscsi_tcp_copy - copy skb bits to the destanation buffer
644  * @conn: iscsi tcp connection
645  *
646  * Notes:
647  *      The function calls skb_copy_bits() and updates per-connection
648  *      byte counters.
649  **/
650 static inline int
651 iscsi_tcp_copy(struct iscsi_conn *conn)
652 {
653         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
654         int buf_size = tcp_conn->in.datalen;
655         int buf_left = buf_size - tcp_conn->data_copied;
656         int size = min(tcp_conn->in.copy, buf_left);
657         int rc;
658
659         debug_tcp("tcp_copy %d bytes at offset %d copied %d\n",
660                size, tcp_conn->in.offset, tcp_conn->data_copied);
661         BUG_ON(size <= 0);
662
663         rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
664                            (char*)conn->data + tcp_conn->data_copied, size);
665         BUG_ON(rc);
666
667         tcp_conn->in.offset += size;
668         tcp_conn->in.copy -= size;
669         tcp_conn->in.copied += size;
670         tcp_conn->data_copied += size;
671
672         if (buf_size != tcp_conn->data_copied)
673                 return -EAGAIN;
674
675         return 0;
676 }
677
678 static inline void
679 partial_sg_digest_update(struct iscsi_tcp_conn *tcp_conn,
680                          struct scatterlist *sg, int offset, int length)
681 {
682         struct scatterlist temp;
683
684         memcpy(&temp, sg, sizeof(struct scatterlist));
685         temp.offset = offset;
686         temp.length = length;
687         crypto_digest_update(tcp_conn->data_rx_tfm, &temp, 1);
688 }
689
690 static void
691 iscsi_recv_digest_update(struct iscsi_tcp_conn *tcp_conn, char* buf, int len)
692 {
693         struct scatterlist tmp;
694
695         sg_init_one(&tmp, buf, len);
696         crypto_digest_update(tcp_conn->data_rx_tfm, &tmp, 1);
697 }
698
699 static int iscsi_scsi_data_in(struct iscsi_conn *conn)
700 {
701         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
702         struct iscsi_cmd_task *ctask = tcp_conn->in.ctask;
703         struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
704         struct scsi_cmnd *sc = ctask->sc;
705         struct scatterlist *sg;
706         int i, offset, rc = 0;
707
708         BUG_ON((void*)ctask != sc->SCp.ptr);
709
710         /*
711          * copying Data-In into the Scsi_Cmnd
712          */
713         if (!sc->use_sg) {
714                 i = ctask->data_count;
715                 rc = iscsi_ctask_copy(tcp_conn, ctask, sc->request_buffer,
716                                       sc->request_bufflen,
717                                       tcp_ctask->data_offset);
718                 if (rc == -EAGAIN)
719                         return rc;
720                 if (conn->datadgst_en)
721                         iscsi_recv_digest_update(tcp_conn, sc->request_buffer,
722                                                  i);
723                 rc = 0;
724                 goto done;
725         }
726
727         offset = tcp_ctask->data_offset;
728         sg = sc->request_buffer;
729
730         if (tcp_ctask->data_offset)
731                 for (i = 0; i < tcp_ctask->sg_count; i++)
732                         offset -= sg[i].length;
733         /* we've passed through partial sg*/
734         if (offset < 0)
735                 offset = 0;
736
737         for (i = tcp_ctask->sg_count; i < sc->use_sg; i++) {
738                 char *dest;
739
740                 dest = kmap_atomic(sg[i].page, KM_SOFTIRQ0);
741                 rc = iscsi_ctask_copy(tcp_conn, ctask, dest + sg[i].offset,
742                                       sg[i].length, offset);
743                 kunmap_atomic(dest, KM_SOFTIRQ0);
744                 if (rc == -EAGAIN)
745                         /* continue with the next SKB/PDU */
746                         return rc;
747                 if (!rc) {
748                         if (conn->datadgst_en) {
749                                 if (!offset)
750                                         crypto_digest_update(
751                                                         tcp_conn->data_rx_tfm,
752                                                         &sg[i], 1);
753                                 else
754                                         partial_sg_digest_update(tcp_conn,
755                                                         &sg[i],
756                                                         sg[i].offset + offset,
757                                                         sg[i].length - offset);
758                         }
759                         offset = 0;
760                         tcp_ctask->sg_count++;
761                 }
762
763                 if (!ctask->data_count) {
764                         if (rc && conn->datadgst_en)
765                                 /*
766                                  * data-in is complete, but buffer not...
767                                  */
768                                 partial_sg_digest_update(tcp_conn, &sg[i],
769                                                 sg[i].offset, sg[i].length-rc);
770                         rc = 0;
771                         break;
772                 }
773
774                 if (!tcp_conn->in.copy)
775                         return -EAGAIN;
776         }
777         BUG_ON(ctask->data_count);
778
779 done:
780         /* check for non-exceptional status */
781         if (tcp_conn->in.hdr->flags & ISCSI_FLAG_DATA_STATUS) {
782                 debug_scsi("done [sc %lx res %d itt 0x%x flags 0x%x]\n",
783                            (long)sc, sc->result, ctask->itt,
784                            tcp_conn->in.hdr->flags);
785                 spin_lock(&conn->session->lock);
786                 iscsi_tcp_cleanup_ctask(conn, ctask);
787                 __iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
788                 spin_unlock(&conn->session->lock);
789         }
790
791         return rc;
792 }
793
794 static int
795 iscsi_data_recv(struct iscsi_conn *conn)
796 {
797         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
798         int rc = 0, opcode;
799
800         opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
801         switch (opcode) {
802         case ISCSI_OP_SCSI_DATA_IN:
803                 rc = iscsi_scsi_data_in(conn);
804                 break;
805         case ISCSI_OP_SCSI_CMD_RSP:
806                 spin_lock(&conn->session->lock);
807                 iscsi_tcp_cleanup_ctask(conn, tcp_conn->in.ctask);
808                 spin_unlock(&conn->session->lock);
809         case ISCSI_OP_TEXT_RSP:
810         case ISCSI_OP_LOGIN_RSP:
811         case ISCSI_OP_ASYNC_EVENT:
812         case ISCSI_OP_REJECT:
813                 /*
814                  * Collect data segment to the connection's data
815                  * placeholder
816                  */
817                 if (iscsi_tcp_copy(conn)) {
818                         rc = -EAGAIN;
819                         goto exit;
820                 }
821
822                 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, conn->data,
823                                         tcp_conn->in.datalen);
824                 if (!rc && conn->datadgst_en && opcode != ISCSI_OP_LOGIN_RSP)
825                         iscsi_recv_digest_update(tcp_conn, conn->data,
826                                                 tcp_conn->in.datalen);
827                 break;
828         default:
829                 BUG_ON(1);
830         }
831 exit:
832         return rc;
833 }
834
835 /**
836  * iscsi_tcp_data_recv - TCP receive in sendfile fashion
837  * @rd_desc: read descriptor
838  * @skb: socket buffer
839  * @offset: offset in skb
840  * @len: skb->len - offset
841  **/
842 static int
843 iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
844                 unsigned int offset, size_t len)
845 {
846         int rc;
847         struct iscsi_conn *conn = rd_desc->arg.data;
848         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
849         int processed;
850         char pad[ISCSI_PAD_LEN];
851         struct scatterlist sg;
852
853         /*
854          * Save current SKB and its offset in the corresponding
855          * connection context.
856          */
857         tcp_conn->in.copy = skb->len - offset;
858         tcp_conn->in.offset = offset;
859         tcp_conn->in.skb = skb;
860         tcp_conn->in.len = tcp_conn->in.copy;
861         BUG_ON(tcp_conn->in.copy <= 0);
862         debug_tcp("in %d bytes\n", tcp_conn->in.copy);
863
864 more:
865         tcp_conn->in.copied = 0;
866         rc = 0;
867
868         if (unlikely(conn->suspend_rx)) {
869                 debug_tcp("conn %d Rx suspended!\n", conn->id);
870                 return 0;
871         }
872
873         if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER ||
874             tcp_conn->in_progress == IN_PROGRESS_HEADER_GATHER) {
875                 rc = iscsi_hdr_extract(tcp_conn);
876                 if (rc) {
877                        if (rc == -EAGAIN)
878                                 goto nomore;
879                        else {
880                                 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
881                                 return 0;
882                        }
883                 }
884
885                 /*
886                  * Verify and process incoming PDU header.
887                  */
888                 rc = iscsi_tcp_hdr_recv(conn);
889                 if (!rc && tcp_conn->in.datalen) {
890                         if (conn->datadgst_en) {
891                                 BUG_ON(!tcp_conn->data_rx_tfm);
892                                 crypto_digest_init(tcp_conn->data_rx_tfm);
893                         }
894                         tcp_conn->in_progress = IN_PROGRESS_DATA_RECV;
895                 } else if (rc) {
896                         iscsi_conn_failure(conn, rc);
897                         return 0;
898                 }
899         }
900
901         if (tcp_conn->in_progress == IN_PROGRESS_DDIGEST_RECV) {
902                 uint32_t recv_digest;
903
904                 debug_tcp("extra data_recv offset %d copy %d\n",
905                           tcp_conn->in.offset, tcp_conn->in.copy);
906                 skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
907                                 &recv_digest, 4);
908                 tcp_conn->in.offset += 4;
909                 tcp_conn->in.copy -= 4;
910                 if (recv_digest != tcp_conn->in.datadgst) {
911                         debug_tcp("iscsi_tcp: data digest error!"
912                                   "0x%x != 0x%x\n", recv_digest,
913                                   tcp_conn->in.datadgst);
914                         iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
915                         return 0;
916                 } else {
917                         debug_tcp("iscsi_tcp: data digest match!"
918                                   "0x%x == 0x%x\n", recv_digest,
919                                   tcp_conn->in.datadgst);
920                         tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
921                 }
922         }
923
924         if (tcp_conn->in_progress == IN_PROGRESS_DATA_RECV &&
925            tcp_conn->in.copy) {
926
927                 debug_tcp("data_recv offset %d copy %d\n",
928                        tcp_conn->in.offset, tcp_conn->in.copy);
929
930                 rc = iscsi_data_recv(conn);
931                 if (rc) {
932                         if (rc == -EAGAIN)
933                                 goto again;
934                         iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
935                         return 0;
936                 }
937                 tcp_conn->in.copy -= tcp_conn->in.padding;
938                 tcp_conn->in.offset += tcp_conn->in.padding;
939                 if (conn->datadgst_en) {
940                         if (tcp_conn->in.padding) {
941                                 debug_tcp("padding -> %d\n",
942                                           tcp_conn->in.padding);
943                                 memset(pad, 0, tcp_conn->in.padding);
944                                 sg_init_one(&sg, pad, tcp_conn->in.padding);
945                                 crypto_digest_update(tcp_conn->data_rx_tfm,
946                                                      &sg, 1);
947                         }
948                         crypto_digest_final(tcp_conn->data_rx_tfm,
949                                             (u8 *) & tcp_conn->in.datadgst);
950                         debug_tcp("rx digest 0x%x\n", tcp_conn->in.datadgst);
951                         tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
952                 } else
953                         tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
954         }
955
956         debug_tcp("f, processed %d from out of %d padding %d\n",
957                tcp_conn->in.offset - offset, (int)len, tcp_conn->in.padding);
958         BUG_ON(tcp_conn->in.offset - offset > len);
959
960         if (tcp_conn->in.offset - offset != len) {
961                 debug_tcp("continue to process %d bytes\n",
962                        (int)len - (tcp_conn->in.offset - offset));
963                 goto more;
964         }
965
966 nomore:
967         processed = tcp_conn->in.offset - offset;
968         BUG_ON(processed == 0);
969         return processed;
970
971 again:
972         processed = tcp_conn->in.offset - offset;
973         debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n",
974                   processed, (int)len, (int)rd_desc->count);
975         BUG_ON(processed == 0);
976         BUG_ON(processed > len);
977
978         conn->rxdata_octets += processed;
979         return processed;
980 }
981
982 static void
983 iscsi_tcp_data_ready(struct sock *sk, int flag)
984 {
985         struct iscsi_conn *conn = sk->sk_user_data;
986         read_descriptor_t rd_desc;
987
988         read_lock(&sk->sk_callback_lock);
989
990         /*
991          * Use rd_desc to pass 'conn' to iscsi_tcp_data_recv.
992          * We set count to 1 because we want the network layer to
993          * hand us all the skbs that are available. iscsi_tcp_data_recv
994          * handled pdus that cross buffers or pdus that still need data.
995          */
996         rd_desc.arg.data = conn;
997         rd_desc.count = 1;
998         tcp_read_sock(sk, &rd_desc, iscsi_tcp_data_recv);
999
1000         read_unlock(&sk->sk_callback_lock);
1001 }
1002
1003 static void
1004 iscsi_tcp_state_change(struct sock *sk)
1005 {
1006         struct iscsi_tcp_conn *tcp_conn;
1007         struct iscsi_conn *conn;
1008         struct iscsi_session *session;
1009         void (*old_state_change)(struct sock *);
1010
1011         read_lock(&sk->sk_callback_lock);
1012
1013         conn = (struct iscsi_conn*)sk->sk_user_data;
1014         session = conn->session;
1015
1016         if ((sk->sk_state == TCP_CLOSE_WAIT ||
1017              sk->sk_state == TCP_CLOSE) &&
1018             !atomic_read(&sk->sk_rmem_alloc)) {
1019                 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1020                 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1021         }
1022
1023         tcp_conn = conn->dd_data;
1024         old_state_change = tcp_conn->old_state_change;
1025
1026         read_unlock(&sk->sk_callback_lock);
1027
1028         old_state_change(sk);
1029 }
1030
1031 /**
1032  * iscsi_write_space - Called when more output buffer space is available
1033  * @sk: socket space is available for
1034  **/
1035 static void
1036 iscsi_write_space(struct sock *sk)
1037 {
1038         struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
1039         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1040
1041         tcp_conn->old_write_space(sk);
1042         debug_tcp("iscsi_write_space: cid %d\n", conn->id);
1043         scsi_queue_work(conn->session->host, &conn->xmitwork);
1044 }
1045
1046 static void
1047 iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1048 {
1049         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1050         struct sock *sk = tcp_conn->sock->sk;
1051
1052         /* assign new callbacks */
1053         write_lock_bh(&sk->sk_callback_lock);
1054         sk->sk_user_data = conn;
1055         tcp_conn->old_data_ready = sk->sk_data_ready;
1056         tcp_conn->old_state_change = sk->sk_state_change;
1057         tcp_conn->old_write_space = sk->sk_write_space;
1058         sk->sk_data_ready = iscsi_tcp_data_ready;
1059         sk->sk_state_change = iscsi_tcp_state_change;
1060         sk->sk_write_space = iscsi_write_space;
1061         write_unlock_bh(&sk->sk_callback_lock);
1062 }
1063
1064 static void
1065 iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
1066 {
1067         struct sock *sk = tcp_conn->sock->sk;
1068
1069         /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1070         write_lock_bh(&sk->sk_callback_lock);
1071         sk->sk_user_data    = NULL;
1072         sk->sk_data_ready   = tcp_conn->old_data_ready;
1073         sk->sk_state_change = tcp_conn->old_state_change;
1074         sk->sk_write_space  = tcp_conn->old_write_space;
1075         sk->sk_no_check  = 0;
1076         write_unlock_bh(&sk->sk_callback_lock);
1077 }
1078
1079 /**
1080  * iscsi_send - generic send routine
1081  * @sk: kernel's socket
1082  * @buf: buffer to write from
1083  * @size: actual size to write
1084  * @flags: socket's flags
1085  */
1086 static inline int
1087 iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags)
1088 {
1089         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1090         struct socket *sk = tcp_conn->sock;
1091         int offset = buf->sg.offset + buf->sent, res;
1092
1093         /*
1094          * if we got use_sg=0 or are sending something we kmallocd
1095          * then we did not have to do kmap (kmap returns page_address)
1096          *
1097          * if we got use_sg > 0, but had to drop down, we do not
1098          * set clustering so this should only happen for that
1099          * slab case.
1100          */
1101         if (buf->use_sendmsg)
1102                 res = sock_no_sendpage(sk, buf->sg.page, offset, size, flags);
1103         else
1104                 res = tcp_conn->sendpage(sk, buf->sg.page, offset, size, flags);
1105
1106         if (res >= 0) {
1107                 conn->txdata_octets += res;
1108                 buf->sent += res;
1109                 return res;
1110         }
1111
1112         tcp_conn->sendpage_failures_cnt++;
1113         if (res == -EAGAIN)
1114                 res = -ENOBUFS;
1115         else
1116                 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1117         return res;
1118 }
1119
1120 /**
1121  * iscsi_sendhdr - send PDU Header via tcp_sendpage()
1122  * @conn: iscsi connection
1123  * @buf: buffer to write from
1124  * @datalen: lenght of data to be sent after the header
1125  *
1126  * Notes:
1127  *      (Tx, Fast Path)
1128  **/
1129 static inline int
1130 iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
1131 {
1132         int flags = 0; /* MSG_DONTWAIT; */
1133         int res, size;
1134
1135         size = buf->sg.length - buf->sent;
1136         BUG_ON(buf->sent + size > buf->sg.length);
1137         if (buf->sent + size != buf->sg.length || datalen)
1138                 flags |= MSG_MORE;
1139
1140         res = iscsi_send(conn, buf, size, flags);
1141         debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res);
1142         if (res >= 0) {
1143                 if (size != res)
1144                         return -EAGAIN;
1145                 return 0;
1146         }
1147
1148         return res;
1149 }
1150
1151 /**
1152  * iscsi_sendpage - send one page of iSCSI Data-Out.
1153  * @conn: iscsi connection
1154  * @buf: buffer to write from
1155  * @count: remaining data
1156  * @sent: number of bytes sent
1157  *
1158  * Notes:
1159  *      (Tx, Fast Path)
1160  **/
1161 static inline int
1162 iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
1163                int *count, int *sent)
1164 {
1165         int flags = 0; /* MSG_DONTWAIT; */
1166         int res, size;
1167
1168         size = buf->sg.length - buf->sent;
1169         BUG_ON(buf->sent + size > buf->sg.length);
1170         if (size > *count)
1171                 size = *count;
1172         if (buf->sent + size != buf->sg.length || *count != size)
1173                 flags |= MSG_MORE;
1174
1175         res = iscsi_send(conn, buf, size, flags);
1176         debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n",
1177                   size, buf->sent, *count, *sent, res);
1178         if (res >= 0) {
1179                 *count -= res;
1180                 *sent += res;
1181                 if (size != res)
1182                         return -EAGAIN;
1183                 return 0;
1184         }
1185
1186         return res;
1187 }
1188
1189 static inline void
1190 iscsi_data_digest_init(struct iscsi_tcp_conn *tcp_conn,
1191                       struct iscsi_cmd_task *ctask)
1192 {
1193         struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1194
1195         BUG_ON(!tcp_conn->data_tx_tfm);
1196         crypto_digest_init(tcp_conn->data_tx_tfm);
1197         tcp_ctask->digest_count = 4;
1198 }
1199
1200 static int
1201 iscsi_digest_final_send(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1202                         struct iscsi_buf *buf, uint32_t *digest, int final)
1203 {
1204         struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1205         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1206         int rc = 0;
1207         int sent = 0;
1208
1209         if (final)
1210                 crypto_digest_final(tcp_conn->data_tx_tfm, (u8*)digest);
1211
1212         iscsi_buf_init_iov(buf, (char*)digest, 4);
1213         rc = iscsi_sendpage(conn, buf, &tcp_ctask->digest_count, &sent);
1214         if (rc) {
1215                 tcp_ctask->datadigest = *digest;
1216                 tcp_ctask->xmstate |= XMSTATE_DATA_DIGEST;
1217         } else
1218                 tcp_ctask->digest_count = 4;
1219         return rc;
1220 }
1221
1222 /**
1223  * iscsi_solicit_data_cont - initialize next Data-Out
1224  * @conn: iscsi connection
1225  * @ctask: scsi command task
1226  * @r2t: R2T info
1227  * @left: bytes left to transfer
1228  *
1229  * Notes:
1230  *      Initialize next Data-Out within this R2T sequence and continue
1231  *      to process next Scatter-Gather element(if any) of this SCSI command.
1232  *
1233  *      Called under connection lock.
1234  **/
1235 static void
1236 iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1237                         struct iscsi_r2t_info *r2t, int left)
1238 {
1239         struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1240         struct iscsi_data *hdr;
1241         struct scsi_cmnd *sc = ctask->sc;
1242         int new_offset;
1243
1244         hdr = &r2t->dtask.hdr;
1245         memset(hdr, 0, sizeof(struct iscsi_data));
1246         hdr->ttt = r2t->ttt;
1247         hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1248         r2t->solicit_datasn++;
1249         hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
1250         memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1251         hdr->itt = ctask->hdr->itt;
1252         hdr->exp_statsn = r2t->exp_statsn;
1253         new_offset = r2t->data_offset + r2t->sent;
1254         hdr->offset = cpu_to_be32(new_offset);
1255         if (left > conn->max_xmit_dlength) {
1256                 hton24(hdr->dlength, conn->max_xmit_dlength);
1257                 r2t->data_count = conn->max_xmit_dlength;
1258         } else {
1259                 hton24(hdr->dlength, left);
1260                 r2t->data_count = left;
1261                 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1262         }
1263         conn->dataout_pdus_cnt++;
1264
1265         iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
1266                            sizeof(struct iscsi_hdr));
1267
1268         if (sc->use_sg && !iscsi_buf_left(&r2t->sendbuf)) {
1269                 BUG_ON(tcp_ctask->bad_sg == r2t->sg);
1270                 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1271                 r2t->sg += 1;
1272         } else
1273                 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
1274                             (char*)sc->request_buffer + new_offset,
1275                             r2t->data_count);
1276 }
1277
1278 /**
1279  * iscsi_tcp_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
1280  * @conn: iscsi connection
1281  * @ctask: scsi command task
1282  * @sc: scsi command
1283  **/
1284 static void
1285 iscsi_tcp_cmd_init(struct iscsi_cmd_task *ctask)
1286 {
1287         struct scsi_cmnd *sc = ctask->sc;
1288         struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1289
1290         BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
1291
1292         tcp_ctask->sent = 0;
1293         tcp_ctask->sg_count = 0;
1294
1295         if (sc->sc_data_direction == DMA_TO_DEVICE) {
1296                 tcp_ctask->xmstate = XMSTATE_W_HDR;
1297                 tcp_ctask->exp_r2tsn = 0;
1298                 BUG_ON(ctask->total_length == 0);
1299
1300                 if (sc->use_sg) {
1301                         struct scatterlist *sg = sc->request_buffer;
1302
1303                         iscsi_buf_init_sg(&tcp_ctask->sendbuf,
1304                                           &sg[tcp_ctask->sg_count++]);
1305                         tcp_ctask->sg = sg;
1306                         tcp_ctask->bad_sg = sg + sc->use_sg;
1307                 } else
1308                         iscsi_buf_init_iov(&tcp_ctask->sendbuf,
1309                                            sc->request_buffer,
1310                                            sc->request_bufflen);
1311
1312                 if (ctask->imm_count)
1313                         tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
1314
1315                 tcp_ctask->pad_count = ctask->total_length & (ISCSI_PAD_LEN-1);
1316                 if (tcp_ctask->pad_count) {
1317                         tcp_ctask->pad_count = ISCSI_PAD_LEN -
1318                                                         tcp_ctask->pad_count;
1319                         debug_scsi("write padding %d bytes\n",
1320                                    tcp_ctask->pad_count);
1321                         tcp_ctask->xmstate |= XMSTATE_W_PAD;
1322                 }
1323
1324                 if (ctask->unsol_count)
1325                         tcp_ctask->xmstate |= XMSTATE_UNS_HDR |
1326                                                 XMSTATE_UNS_INIT;
1327
1328                 debug_scsi("cmd [itt 0x%x total %d imm_data %d "
1329                            "unsol count %d, unsol offset %d]\n",
1330                            ctask->itt, ctask->total_length, ctask->imm_count,
1331                            ctask->unsol_count, ctask->unsol_offset);
1332         } else
1333                 tcp_ctask->xmstate = XMSTATE_R_HDR;
1334
1335         iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)ctask->hdr,
1336                             sizeof(struct iscsi_hdr));
1337 }
1338
1339 /**
1340  * iscsi_tcp_mtask_xmit - xmit management(immediate) task
1341  * @conn: iscsi connection
1342  * @mtask: task management task
1343  *
1344  * Notes:
1345  *      The function can return -EAGAIN in which case caller must
1346  *      call it again later, or recover. '0' return code means successful
1347  *      xmit.
1348  *
1349  *      Management xmit state machine consists of two states:
1350  *              IN_PROGRESS_IMM_HEAD - PDU Header xmit in progress
1351  *              IN_PROGRESS_IMM_DATA - PDU Data xmit in progress
1352  **/
1353 static int
1354 iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
1355 {
1356         struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
1357         int rc;
1358
1359         debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
1360                 conn->id, tcp_mtask->xmstate, mtask->itt);
1361
1362         if (tcp_mtask->xmstate & XMSTATE_IMM_HDR) {
1363                 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR;
1364                 if (mtask->data_count)
1365                         tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
1366                 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
1367                     conn->stop_stage != STOP_CONN_RECOVER &&
1368                     conn->hdrdgst_en)
1369                         iscsi_hdr_digest(conn, &tcp_mtask->headbuf,
1370                                         (u8*)tcp_mtask->hdrext);
1371                 rc = iscsi_sendhdr(conn, &tcp_mtask->headbuf,
1372                                    mtask->data_count);
1373                 if (rc) {
1374                         tcp_mtask->xmstate |= XMSTATE_IMM_HDR;
1375                         if (mtask->data_count)
1376                                 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
1377                         return rc;
1378                 }
1379         }
1380
1381         if (tcp_mtask->xmstate & XMSTATE_IMM_DATA) {
1382                 BUG_ON(!mtask->data_count);
1383                 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
1384                 /* FIXME: implement.
1385                  * Virtual buffer could be spreaded across multiple pages...
1386                  */
1387                 do {
1388                         int rc;
1389
1390                         rc = iscsi_sendpage(conn, &tcp_mtask->sendbuf,
1391                                         &mtask->data_count, &tcp_mtask->sent);
1392                         if (rc) {
1393                                 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
1394                                 return rc;
1395                         }
1396                 } while (mtask->data_count);
1397         }
1398
1399         BUG_ON(tcp_mtask->xmstate != XMSTATE_IDLE);
1400         if (mtask->hdr->itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
1401                 struct iscsi_session *session = conn->session;
1402
1403                 spin_lock_bh(&session->lock);
1404                 list_del(&conn->mtask->running);
1405                 __kfifo_put(session->mgmtpool.queue, (void*)&conn->mtask,
1406                             sizeof(void*));
1407                 spin_unlock_bh(&session->lock);
1408         }
1409         return 0;
1410 }
1411
1412 static inline int
1413 handle_xmstate_r_hdr(struct iscsi_conn *conn,
1414                      struct iscsi_tcp_cmd_task *tcp_ctask)
1415 {
1416         int rc;
1417
1418         tcp_ctask->xmstate &= ~XMSTATE_R_HDR;
1419         if (conn->hdrdgst_en)
1420                 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1421                                  (u8*)tcp_ctask->hdrext);
1422         rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, 0);
1423         if (!rc) {
1424                 BUG_ON(tcp_ctask->xmstate != XMSTATE_IDLE);
1425                 return 0; /* wait for Data-In */
1426         }
1427         tcp_ctask->xmstate |= XMSTATE_R_HDR;
1428         return rc;
1429 }
1430
1431 static inline int
1432 handle_xmstate_w_hdr(struct iscsi_conn *conn,
1433                      struct iscsi_cmd_task *ctask)
1434 {
1435         struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1436         int rc;
1437
1438         tcp_ctask->xmstate &= ~XMSTATE_W_HDR;
1439         if (conn->hdrdgst_en)
1440                 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1441                                  (u8*)tcp_ctask->hdrext);
1442         rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->imm_count);
1443         if (rc)
1444                 tcp_ctask->xmstate |= XMSTATE_W_HDR;
1445         return rc;
1446 }
1447
1448 static inline int
1449 handle_xmstate_data_digest(struct iscsi_conn *conn,
1450                            struct iscsi_cmd_task *ctask)
1451 {
1452         struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1453         int rc;
1454
1455         tcp_ctask->xmstate &= ~XMSTATE_DATA_DIGEST;
1456         debug_tcp("resent data digest 0x%x\n", tcp_ctask->datadigest);
1457         rc = iscsi_digest_final_send(conn, ctask, &tcp_ctask->immbuf,
1458                                     &tcp_ctask->datadigest, 0);
1459         if (rc) {
1460                 tcp_ctask->xmstate |= XMSTATE_DATA_DIGEST;
1461                 debug_tcp("resent data digest 0x%x fail!\n",
1462                           tcp_ctask->datadigest);
1463         }
1464
1465         return rc;
1466 }
1467
1468 static inline int
1469 handle_xmstate_imm_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1470 {
1471         struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1472         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1473         int rc;
1474
1475         BUG_ON(!ctask->imm_count);
1476         tcp_ctask->xmstate &= ~XMSTATE_IMM_DATA;
1477
1478         if (conn->datadgst_en) {
1479                 iscsi_data_digest_init(tcp_conn, ctask);
1480                 tcp_ctask->immdigest = 0;
1481         }
1482
1483         for (;;) {
1484                 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf,
1485                                    &ctask->imm_count, &tcp_ctask->sent);
1486                 if (rc) {
1487                         tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
1488                         if (conn->datadgst_en) {
1489                                 crypto_digest_final(tcp_conn->data_tx_tfm,
1490                                                 (u8*)&tcp_ctask->immdigest);
1491                                 debug_tcp("tx imm sendpage fail 0x%x\n",
1492                                           tcp_ctask->datadigest);
1493                         }
1494                         return rc;
1495                 }
1496                 if (conn->datadgst_en)
1497                         crypto_digest_update(tcp_conn->data_tx_tfm,
1498                                              &tcp_ctask->sendbuf.sg, 1);
1499
1500                 if (!ctask->imm_count)
1501                         break;
1502                 iscsi_buf_init_sg(&tcp_ctask->sendbuf,
1503                                   &tcp_ctask->sg[tcp_ctask->sg_count++]);
1504         }
1505
1506         if (conn->datadgst_en && !(tcp_ctask->xmstate & XMSTATE_W_PAD)) {
1507                 rc = iscsi_digest_final_send(conn, ctask, &tcp_ctask->immbuf,
1508                                             &tcp_ctask->immdigest, 1);
1509                 if (rc) {
1510                         debug_tcp("sending imm digest 0x%x fail!\n",
1511                                   tcp_ctask->immdigest);
1512                         return rc;
1513                 }
1514                 debug_tcp("sending imm digest 0x%x\n", tcp_ctask->immdigest);
1515         }
1516
1517         return 0;
1518 }
1519
1520 static inline int
1521 handle_xmstate_uns_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1522 {
1523         struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1524         struct iscsi_data_task *dtask;
1525         int rc;
1526
1527         tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
1528         if (tcp_ctask->xmstate & XMSTATE_UNS_INIT) {
1529                 dtask = tcp_ctask->dtask = &tcp_ctask->unsol_dtask;
1530                 iscsi_prep_unsolicit_data_pdu(ctask, &dtask->hdr);
1531                 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)&dtask->hdr,
1532                                    sizeof(struct iscsi_hdr));
1533                 if (conn->hdrdgst_en)
1534                         iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1535                                         (u8*)dtask->hdrext);
1536                 tcp_ctask->xmstate &= ~XMSTATE_UNS_INIT;
1537         }
1538
1539         rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->data_count);
1540         if (rc) {
1541                 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1542                 tcp_ctask->xmstate |= XMSTATE_UNS_HDR;
1543                 return rc;
1544         }
1545
1546         debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
1547                    ctask->itt, ctask->unsol_count, tcp_ctask->sent);
1548         return 0;
1549 }
1550
1551 static inline int
1552 handle_xmstate_uns_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1553 {
1554         struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1555         struct iscsi_data_task *dtask = tcp_ctask->dtask;
1556         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1557         int rc;
1558
1559         BUG_ON(!ctask->data_count);
1560         tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1561
1562         if (conn->datadgst_en) {
1563                 iscsi_data_digest_init(tcp_conn, ctask);
1564                 dtask->digest = 0;
1565         }
1566
1567         for (;;) {
1568                 int start = tcp_ctask->sent;
1569
1570                 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf,
1571                                    &ctask->data_count, &tcp_ctask->sent);
1572                 if (rc) {
1573                         ctask->unsol_count -= tcp_ctask->sent - start;
1574                         tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
1575                         /* will continue with this ctask later.. */
1576                         if (conn->datadgst_en) {
1577                                 crypto_digest_final(tcp_conn->data_tx_tfm,
1578                                                 (u8 *)&dtask->digest);
1579                                 debug_tcp("tx uns data fail 0x%x\n",
1580                                           dtask->digest);
1581                         }
1582                         return rc;
1583                 }
1584
1585                 BUG_ON(tcp_ctask->sent > ctask->total_length);
1586                 ctask->unsol_count -= tcp_ctask->sent - start;
1587
1588                 /*
1589                  * XXX:we may run here with un-initial sendbuf.
1590                  * so pass it
1591                  */
1592                 if (conn->datadgst_en && tcp_ctask->sent - start > 0)
1593                         crypto_digest_update(tcp_conn->data_tx_tfm,
1594                                              &tcp_ctask->sendbuf.sg, 1);
1595
1596                 if (!ctask->data_count)
1597                         break;
1598                 iscsi_buf_init_sg(&tcp_ctask->sendbuf,
1599                                   &tcp_ctask->sg[tcp_ctask->sg_count++]);
1600         }
1601         BUG_ON(ctask->unsol_count < 0);
1602
1603         /*
1604          * Done with the Data-Out. Next, check if we need
1605          * to send another unsolicited Data-Out.
1606          */
1607         if (ctask->unsol_count) {
1608                 if (conn->datadgst_en) {
1609                         rc = iscsi_digest_final_send(conn, ctask,
1610                                                     &dtask->digestbuf,
1611                                                     &dtask->digest, 1);
1612                         if (rc) {
1613                                 debug_tcp("send uns digest 0x%x fail\n",
1614                                           dtask->digest);
1615                                 return rc;
1616                         }
1617                         debug_tcp("sending uns digest 0x%x, more uns\n",
1618                                   dtask->digest);
1619                 }
1620                 tcp_ctask->xmstate |= XMSTATE_UNS_INIT;
1621                 return 1;
1622         }
1623
1624         if (conn->datadgst_en && !(tcp_ctask->xmstate & XMSTATE_W_PAD)) {
1625                 rc = iscsi_digest_final_send(conn, ctask,
1626                                             &dtask->digestbuf,
1627                                             &dtask->digest, 1);
1628                 if (rc) {
1629                         debug_tcp("send last uns digest 0x%x fail\n",
1630                                    dtask->digest);
1631                         return rc;
1632                 }
1633                 debug_tcp("sending uns digest 0x%x\n",dtask->digest);
1634         }
1635
1636         return 0;
1637 }
1638
1639 static inline int
1640 handle_xmstate_sol_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1641 {
1642         struct iscsi_session *session = conn->session;
1643         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1644         struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1645         struct iscsi_r2t_info *r2t = tcp_ctask->r2t;
1646         struct iscsi_data_task *dtask = &r2t->dtask;
1647         int left, rc;
1648
1649         tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
1650         tcp_ctask->dtask = dtask;
1651
1652         if (conn->datadgst_en) {
1653                 iscsi_data_digest_init(tcp_conn, ctask);
1654                 dtask->digest = 0;
1655         }
1656 solicit_again:
1657         /*
1658          * send Data-Out within this R2T sequence.
1659          */
1660         if (!r2t->data_count)
1661                 goto data_out_done;
1662
1663         rc = iscsi_sendpage(conn, &r2t->sendbuf, &r2t->data_count, &r2t->sent);
1664         if (rc) {
1665                 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1666                 /* will continue with this ctask later.. */
1667                 if (conn->datadgst_en) {
1668                         crypto_digest_final(tcp_conn->data_tx_tfm,
1669                                           (u8 *)&dtask->digest);
1670                         debug_tcp("r2t data send fail 0x%x\n", dtask->digest);
1671                 }
1672                 return rc;
1673         }
1674
1675         BUG_ON(r2t->data_count < 0);
1676         if (conn->datadgst_en)
1677                 crypto_digest_update(tcp_conn->data_tx_tfm, &r2t->sendbuf.sg,
1678                                      1);
1679
1680         if (r2t->data_count) {
1681                 BUG_ON(ctask->sc->use_sg == 0);
1682                 if (!iscsi_buf_left(&r2t->sendbuf)) {
1683                         BUG_ON(tcp_ctask->bad_sg == r2t->sg);
1684                         iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1685                         r2t->sg += 1;
1686                 }
1687                 goto solicit_again;
1688         }
1689
1690 data_out_done:
1691         /*
1692          * Done with this Data-Out. Next, check if we have
1693          * to send another Data-Out for this R2T.
1694          */
1695         BUG_ON(r2t->data_length - r2t->sent < 0);
1696         left = r2t->data_length - r2t->sent;
1697         if (left) {
1698                 if (conn->datadgst_en) {
1699                         rc = iscsi_digest_final_send(conn, ctask,
1700                                                     &dtask->digestbuf,
1701                                                     &dtask->digest, 1);
1702                         if (rc) {
1703                                 debug_tcp("send r2t data digest 0x%x"
1704                                           "fail\n", dtask->digest);
1705                                 return rc;
1706                         }
1707                         debug_tcp("r2t data send digest 0x%x\n",
1708                                   dtask->digest);
1709                 }
1710                 iscsi_solicit_data_cont(conn, ctask, r2t, left);
1711                 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1712                 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
1713                 return 1;
1714         }
1715
1716         /*
1717          * Done with this R2T. Check if there are more
1718          * outstanding R2Ts ready to be processed.
1719          */
1720         if (conn->datadgst_en) {
1721                 rc = iscsi_digest_final_send(conn, ctask, &dtask->digestbuf,
1722                                             &dtask->digest, 1);
1723                 if (rc) {
1724                         debug_tcp("send last r2t data digest 0x%x"
1725                                   "fail\n", dtask->digest);
1726                         return rc;
1727                 }
1728                 debug_tcp("r2t done dout digest 0x%x\n", dtask->digest);
1729         }
1730
1731         tcp_ctask->r2t = NULL;
1732         spin_lock_bh(&session->lock);
1733         __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
1734         spin_unlock_bh(&session->lock);
1735         if (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
1736                 tcp_ctask->r2t = r2t;
1737                 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1738                 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
1739                 return 1;
1740         }
1741
1742         return 0;
1743 }
1744
1745 static inline int
1746 handle_xmstate_w_pad(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1747 {
1748         struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1749         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1750         struct iscsi_data_task *dtask = tcp_ctask->dtask;
1751         int sent = 0, rc;
1752
1753         tcp_ctask->xmstate &= ~XMSTATE_W_PAD;
1754         iscsi_buf_init_iov(&tcp_ctask->sendbuf, (char*)&tcp_ctask->pad,
1755                             tcp_ctask->pad_count);
1756         rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf, &tcp_ctask->pad_count,
1757                            &sent);
1758         if (rc) {
1759                 tcp_ctask->xmstate |= XMSTATE_W_PAD;
1760                 return rc;
1761         }
1762
1763         if (conn->datadgst_en) {
1764                 crypto_digest_update(tcp_conn->data_tx_tfm,
1765                                      &tcp_ctask->sendbuf.sg, 1);
1766                 /* imm data? */
1767                 if (!dtask) {
1768                         rc = iscsi_digest_final_send(conn, ctask,
1769                                                     &tcp_ctask->immbuf,
1770                                                     &tcp_ctask->immdigest, 1);
1771                         if (rc) {
1772                                 debug_tcp("send padding digest 0x%x"
1773                                           "fail!\n", tcp_ctask->immdigest);
1774                                 return rc;
1775                         }
1776                         debug_tcp("done with padding, digest 0x%x\n",
1777                                   tcp_ctask->datadigest);
1778                 } else {
1779                         rc = iscsi_digest_final_send(conn, ctask,
1780                                                     &dtask->digestbuf,
1781                                                     &dtask->digest, 1);
1782                         if (rc) {
1783                                 debug_tcp("send padding digest 0x%x"
1784                                           "fail\n", dtask->digest);
1785                                 return rc;
1786                         }
1787                         debug_tcp("done with padding, digest 0x%x\n",
1788                                   dtask->digest);
1789                 }
1790         }
1791
1792         return 0;
1793 }
1794
1795 static int
1796 iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1797 {
1798         struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1799         int rc = 0;
1800
1801         debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
1802                 conn->id, tcp_ctask->xmstate, ctask->itt);
1803
1804         /*
1805          * serialize with TMF AbortTask
1806          */
1807         if (ctask->mtask)
1808                 return rc;
1809
1810         if (tcp_ctask->xmstate & XMSTATE_R_HDR)
1811                 return handle_xmstate_r_hdr(conn, tcp_ctask);
1812
1813         if (tcp_ctask->xmstate & XMSTATE_W_HDR) {
1814                 rc = handle_xmstate_w_hdr(conn, ctask);
1815                 if (rc)
1816                         return rc;
1817         }
1818
1819         /* XXX: for data digest xmit recover */
1820         if (tcp_ctask->xmstate & XMSTATE_DATA_DIGEST) {
1821                 rc = handle_xmstate_data_digest(conn, ctask);
1822                 if (rc)
1823                         return rc;
1824         }
1825
1826         if (tcp_ctask->xmstate & XMSTATE_IMM_DATA) {
1827                 rc = handle_xmstate_imm_data(conn, ctask);
1828                 if (rc)
1829                         return rc;
1830         }
1831
1832         if (tcp_ctask->xmstate & XMSTATE_UNS_HDR) {
1833                 BUG_ON(!ctask->unsol_count);
1834                 tcp_ctask->xmstate &= ~XMSTATE_UNS_HDR;
1835 unsolicit_head_again:
1836                 rc = handle_xmstate_uns_hdr(conn, ctask);
1837                 if (rc)
1838                         return rc;
1839         }
1840
1841         if (tcp_ctask->xmstate & XMSTATE_UNS_DATA) {
1842                 rc = handle_xmstate_uns_data(conn, ctask);
1843                 if (rc == 1)
1844                         goto unsolicit_head_again;
1845                 else if (rc)
1846                         return rc;
1847                 goto done;
1848         }
1849
1850         if (tcp_ctask->xmstate & XMSTATE_SOL_HDR) {
1851                 struct iscsi_r2t_info *r2t;
1852
1853                 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
1854                 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1855                 if (!tcp_ctask->r2t)
1856                         __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
1857                                     sizeof(void*));
1858 solicit_head_again:
1859                 r2t = tcp_ctask->r2t;
1860                 if (conn->hdrdgst_en)
1861                         iscsi_hdr_digest(conn, &r2t->headbuf,
1862                                         (u8*)r2t->dtask.hdrext);
1863                 rc = iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count);
1864                 if (rc) {
1865                         tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
1866                         tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
1867                         return rc;
1868                 }
1869
1870                 debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n",
1871                         r2t->solicit_datasn - 1, ctask->itt, r2t->data_count,
1872                         r2t->sent);
1873         }
1874
1875         if (tcp_ctask->xmstate & XMSTATE_SOL_DATA) {
1876                 rc = handle_xmstate_sol_data(conn, ctask);
1877                 if (rc == 1)
1878                         goto solicit_head_again;
1879                 if (rc)
1880                         return rc;
1881         }
1882
1883 done:
1884         /*
1885          * Last thing to check is whether we need to send write
1886          * padding. Note that we check for xmstate equality, not just the bit.
1887          */
1888         if (tcp_ctask->xmstate == XMSTATE_W_PAD)
1889                 rc = handle_xmstate_w_pad(conn, ctask);
1890
1891         return rc;
1892 }
1893
1894 static struct iscsi_cls_conn *
1895 iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1896 {
1897         struct iscsi_conn *conn;
1898         struct iscsi_cls_conn *cls_conn;
1899         struct iscsi_tcp_conn *tcp_conn;
1900
1901         cls_conn = iscsi_conn_setup(cls_session, conn_idx);
1902         if (!cls_conn)
1903                 return NULL;
1904         conn = cls_conn->dd_data;
1905         /*
1906          * due to strange issues with iser these are not set
1907          * in iscsi_conn_setup
1908          */
1909         conn->max_recv_dlength = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
1910
1911         tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
1912         if (!tcp_conn)
1913                 goto tcp_conn_alloc_fail;
1914
1915         conn->dd_data = tcp_conn;
1916         tcp_conn->iscsi_conn = conn;
1917         tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1918         /* initial operational parameters */
1919         tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
1920
1921         return cls_conn;
1922
1923 tcp_conn_alloc_fail:
1924         iscsi_conn_teardown(cls_conn);
1925         return NULL;
1926 }
1927
1928 static void
1929 iscsi_tcp_release_conn(struct iscsi_conn *conn)
1930 {
1931         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1932
1933         if (!tcp_conn->sock)
1934                 return;
1935
1936         sock_hold(tcp_conn->sock->sk);
1937         iscsi_conn_restore_callbacks(tcp_conn);
1938         sock_put(tcp_conn->sock->sk);
1939
1940         sock_release(tcp_conn->sock);
1941         tcp_conn->sock = NULL;
1942         conn->recv_lock = NULL;
1943 }
1944
1945 static void
1946 iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
1947 {
1948         struct iscsi_conn *conn = cls_conn->dd_data;
1949         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1950         int digest = 0;
1951
1952         if (conn->hdrdgst_en || conn->datadgst_en)
1953                 digest = 1;
1954
1955         iscsi_tcp_release_conn(conn);
1956         iscsi_conn_teardown(cls_conn);
1957
1958         /* now free tcp_conn */
1959         if (digest) {
1960                 if (tcp_conn->tx_tfm)
1961                         crypto_free_tfm(tcp_conn->tx_tfm);
1962                 if (tcp_conn->rx_tfm)
1963                         crypto_free_tfm(tcp_conn->rx_tfm);
1964                 if (tcp_conn->data_tx_tfm)
1965                         crypto_free_tfm(tcp_conn->data_tx_tfm);
1966                 if (tcp_conn->data_rx_tfm)
1967                         crypto_free_tfm(tcp_conn->data_rx_tfm);
1968         }
1969
1970         kfree(tcp_conn);
1971 }
1972
1973 static void
1974 iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1975 {
1976         struct iscsi_conn *conn = cls_conn->dd_data;
1977
1978         iscsi_conn_stop(cls_conn, flag);
1979         iscsi_tcp_release_conn(conn);
1980 }
1981
1982 static int
1983 iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
1984                     struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
1985                     int is_leading)
1986 {
1987         struct iscsi_conn *conn = cls_conn->dd_data;
1988         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1989         struct sock *sk;
1990         struct socket *sock;
1991         int err;
1992
1993         /* lookup for existing socket */
1994         sock = sockfd_lookup((int)transport_eph, &err);
1995         if (!sock) {
1996                 printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
1997                 return -EEXIST;
1998         }
1999
2000         err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
2001         if (err)
2002                 return err;
2003
2004         /* bind iSCSI connection and socket */
2005         tcp_conn->sock = sock;
2006
2007         /* setup Socket parameters */
2008         sk = sock->sk;
2009         sk->sk_reuse = 1;
2010         sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
2011         sk->sk_allocation = GFP_ATOMIC;
2012
2013         /* FIXME: disable Nagle's algorithm */
2014
2015         /*
2016          * Intercept TCP callbacks for sendfile like receive
2017          * processing.
2018          */
2019         conn->recv_lock = &sk->sk_callback_lock;
2020         iscsi_conn_set_callbacks(conn);
2021         tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
2022         /*
2023          * set receive state machine into initial state
2024          */
2025         tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
2026
2027         return 0;
2028 }
2029
2030 /* called with host lock */
2031 static void
2032 iscsi_tcp_mgmt_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask,
2033                     char *data, uint32_t data_size)
2034 {
2035         struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
2036
2037         iscsi_buf_init_iov(&tcp_mtask->headbuf, (char*)mtask->hdr,
2038                            sizeof(struct iscsi_hdr));
2039         tcp_mtask->xmstate = XMSTATE_IMM_HDR;
2040         tcp_mtask->sent = 0;
2041
2042         if (mtask->data_count)
2043                 iscsi_buf_init_iov(&tcp_mtask->sendbuf, (char*)mtask->data,
2044                                     mtask->data_count);
2045 }
2046
2047 static int
2048 iscsi_r2tpool_alloc(struct iscsi_session *session)
2049 {
2050         int i;
2051         int cmd_i;
2052
2053         /*
2054          * initialize per-task: R2T pool and xmit queue
2055          */
2056         for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2057                 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
2058                 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2059
2060                 /*
2061                  * pre-allocated x4 as much r2ts to handle race when
2062                  * target acks DataOut faster than we data_xmit() queues
2063                  * could replenish r2tqueue.
2064                  */
2065
2066                 /* R2T pool */
2067                 if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4,
2068                                     (void***)&tcp_ctask->r2ts,
2069                                     sizeof(struct iscsi_r2t_info))) {
2070                         goto r2t_alloc_fail;
2071                 }
2072
2073                 /* R2T xmit queue */
2074                 tcp_ctask->r2tqueue = kfifo_alloc(
2075                       session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
2076                 if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
2077                         iscsi_pool_free(&tcp_ctask->r2tpool,
2078                                         (void**)tcp_ctask->r2ts);
2079                         goto r2t_alloc_fail;
2080                 }
2081         }
2082
2083         return 0;
2084
2085 r2t_alloc_fail:
2086         for (i = 0; i < cmd_i; i++) {
2087                 struct iscsi_cmd_task *ctask = session->cmds[i];
2088                 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2089
2090                 kfifo_free(tcp_ctask->r2tqueue);
2091                 iscsi_pool_free(&tcp_ctask->r2tpool,
2092                                 (void**)tcp_ctask->r2ts);
2093         }
2094         return -ENOMEM;
2095 }
2096
2097 static void
2098 iscsi_r2tpool_free(struct iscsi_session *session)
2099 {
2100         int i;
2101
2102         for (i = 0; i < session->cmds_max; i++) {
2103                 struct iscsi_cmd_task *ctask = session->cmds[i];
2104                 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2105
2106                 kfifo_free(tcp_ctask->r2tqueue);
2107                 iscsi_pool_free(&tcp_ctask->r2tpool,
2108                                 (void**)tcp_ctask->r2ts);
2109         }
2110 }
2111
2112 static int
2113 iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
2114                      char *buf, int buflen)
2115 {
2116         struct iscsi_conn *conn = cls_conn->dd_data;
2117         struct iscsi_session *session = conn->session;
2118         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2119         int value;
2120
2121         switch(param) {
2122         case ISCSI_PARAM_HDRDGST_EN:
2123                 iscsi_set_param(cls_conn, param, buf, buflen);
2124                 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
2125                 if (conn->hdrdgst_en) {
2126                         tcp_conn->hdr_size += sizeof(__u32);
2127                         if (!tcp_conn->tx_tfm)
2128                                 tcp_conn->tx_tfm = crypto_alloc_tfm("crc32c",
2129                                                                     0);
2130                         if (!tcp_conn->tx_tfm)
2131                                 return -ENOMEM;
2132                         if (!tcp_conn->rx_tfm)
2133                                 tcp_conn->rx_tfm = crypto_alloc_tfm("crc32c",
2134                                                                     0);
2135                         if (!tcp_conn->rx_tfm) {
2136                                 crypto_free_tfm(tcp_conn->tx_tfm);
2137                                 return -ENOMEM;
2138                         }
2139                 } else {
2140                         if (tcp_conn->tx_tfm)
2141                                 crypto_free_tfm(tcp_conn->tx_tfm);
2142                         if (tcp_conn->rx_tfm)
2143                                 crypto_free_tfm(tcp_conn->rx_tfm);
2144                 }
2145                 break;
2146         case ISCSI_PARAM_DATADGST_EN:
2147                 iscsi_set_param(cls_conn, param, buf, buflen);
2148                 if (conn->datadgst_en) {
2149                         if (!tcp_conn->data_tx_tfm)
2150                                 tcp_conn->data_tx_tfm =
2151                                     crypto_alloc_tfm("crc32c", 0);
2152                         if (!tcp_conn->data_tx_tfm)
2153                                 return -ENOMEM;
2154                         if (!tcp_conn->data_rx_tfm)
2155                                 tcp_conn->data_rx_tfm =
2156                                     crypto_alloc_tfm("crc32c", 0);
2157                         if (!tcp_conn->data_rx_tfm) {
2158                                 crypto_free_tfm(tcp_conn->data_tx_tfm);
2159                                 return -ENOMEM;
2160                         }
2161                 } else {
2162                         if (tcp_conn->data_tx_tfm)
2163                                 crypto_free_tfm(tcp_conn->data_tx_tfm);
2164                         if (tcp_conn->data_rx_tfm)
2165                                 crypto_free_tfm(tcp_conn->data_rx_tfm);
2166                 }
2167                 tcp_conn->sendpage = conn->datadgst_en ?
2168                         sock_no_sendpage : tcp_conn->sock->ops->sendpage;
2169                 break;
2170         case ISCSI_PARAM_MAX_R2T:
2171                 sscanf(buf, "%d", &value);
2172                 if (session->max_r2t == roundup_pow_of_two(value))
2173                         break;
2174                 iscsi_r2tpool_free(session);
2175                 iscsi_set_param(cls_conn, param, buf, buflen);
2176                 if (session->max_r2t & (session->max_r2t - 1))
2177                         session->max_r2t = roundup_pow_of_two(session->max_r2t);
2178                 if (iscsi_r2tpool_alloc(session))
2179                         return -ENOMEM;
2180                 break;
2181         default:
2182                 return iscsi_set_param(cls_conn, param, buf, buflen);
2183         }
2184
2185         return 0;
2186 }
2187
2188 static int
2189 iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
2190                          enum iscsi_param param, char *buf)
2191 {
2192         struct iscsi_conn *conn = cls_conn->dd_data;
2193         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2194         struct inet_sock *inet;
2195         struct ipv6_pinfo *np;
2196         struct sock *sk;
2197         int len;
2198
2199         switch(param) {
2200         case ISCSI_PARAM_CONN_PORT:
2201                 mutex_lock(&conn->xmitmutex);
2202                 if (!tcp_conn->sock) {
2203                         mutex_unlock(&conn->xmitmutex);
2204                         return -EINVAL;
2205                 }
2206
2207                 inet = inet_sk(tcp_conn->sock->sk);
2208                 len = sprintf(buf, "%hu\n", be16_to_cpu(inet->dport));
2209                 mutex_unlock(&conn->xmitmutex);
2210                 break;
2211         case ISCSI_PARAM_CONN_ADDRESS:
2212                 mutex_lock(&conn->xmitmutex);
2213                 if (!tcp_conn->sock) {
2214                         mutex_unlock(&conn->xmitmutex);
2215                         return -EINVAL;
2216                 }
2217
2218                 sk = tcp_conn->sock->sk;
2219                 if (sk->sk_family == PF_INET) {
2220                         inet = inet_sk(sk);
2221                         len = sprintf(buf, "%u.%u.%u.%u\n",
2222                                       NIPQUAD(inet->daddr));
2223                 } else {
2224                         np = inet6_sk(sk);
2225                         len = sprintf(buf,
2226                                 "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
2227                                 NIP6(np->daddr));
2228                 }
2229                 mutex_unlock(&conn->xmitmutex);
2230                 break;
2231         default:
2232                 return iscsi_conn_get_param(cls_conn, param, buf);
2233         }
2234
2235         return len;
2236 }
2237
2238 static void
2239 iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
2240 {
2241         struct iscsi_conn *conn = cls_conn->dd_data;
2242         struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2243
2244         stats->txdata_octets = conn->txdata_octets;
2245         stats->rxdata_octets = conn->rxdata_octets;
2246         stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
2247         stats->dataout_pdus = conn->dataout_pdus_cnt;
2248         stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
2249         stats->datain_pdus = conn->datain_pdus_cnt;
2250         stats->r2t_pdus = conn->r2t_pdus_cnt;
2251         stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
2252         stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
2253         stats->custom_length = 3;
2254         strcpy(stats->custom[0].desc, "tx_sendpage_failures");
2255         stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
2256         strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
2257         stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
2258         strcpy(stats->custom[2].desc, "eh_abort_cnt");
2259         stats->custom[2].value = conn->eh_abort_cnt;
2260 }
2261
2262 static struct iscsi_cls_session *
2263 iscsi_tcp_session_create(struct iscsi_transport *iscsit,
2264                          struct scsi_transport_template *scsit,
2265                          uint32_t initial_cmdsn, uint32_t *hostno)
2266 {
2267         struct iscsi_cls_session *cls_session;
2268         struct iscsi_session *session;
2269         uint32_t hn;
2270         int cmd_i;
2271
2272         cls_session = iscsi_session_setup(iscsit, scsit,
2273                                          sizeof(struct iscsi_tcp_cmd_task),
2274                                          sizeof(struct iscsi_tcp_mgmt_task),
2275                                          initial_cmdsn, &hn);
2276         if (!cls_session)
2277                 return NULL;
2278         *hostno = hn;
2279
2280         session = class_to_transport_session(cls_session);
2281         for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2282                 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
2283                 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2284
2285                 ctask->hdr = &tcp_ctask->hdr;
2286         }
2287
2288         for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
2289                 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
2290                 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
2291
2292                 mtask->hdr = &tcp_mtask->hdr;
2293         }
2294
2295         if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
2296                 goto r2tpool_alloc_fail;
2297
2298         return cls_session;
2299
2300 r2tpool_alloc_fail:
2301         iscsi_session_teardown(cls_session);
2302         return NULL;
2303 }
2304
2305 static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
2306 {
2307         iscsi_r2tpool_free(class_to_transport_session(cls_session));
2308         iscsi_session_teardown(cls_session);
2309 }
2310
2311 static struct scsi_host_template iscsi_sht = {
2312         .name                   = "iSCSI Initiator over TCP/IP",
2313         .queuecommand           = iscsi_queuecommand,
2314         .change_queue_depth     = iscsi_change_queue_depth,
2315         .can_queue              = ISCSI_XMIT_CMDS_MAX - 1,
2316         .sg_tablesize           = ISCSI_SG_TABLESIZE,
2317         .cmd_per_lun            = ISCSI_DEF_CMD_PER_LUN,
2318         .eh_abort_handler       = iscsi_eh_abort,
2319         .eh_host_reset_handler  = iscsi_eh_host_reset,
2320         .use_clustering         = DISABLE_CLUSTERING,
2321         .proc_name              = "iscsi_tcp",
2322         .this_id                = -1,
2323 };
2324
2325 static struct iscsi_transport iscsi_tcp_transport = {
2326         .owner                  = THIS_MODULE,
2327         .name                   = "tcp",
2328         .caps                   = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
2329                                   | CAP_DATADGST,
2330         .param_mask             = ISCSI_MAX_RECV_DLENGTH |
2331                                   ISCSI_MAX_XMIT_DLENGTH |
2332                                   ISCSI_HDRDGST_EN |
2333                                   ISCSI_DATADGST_EN |
2334                                   ISCSI_INITIAL_R2T_EN |
2335                                   ISCSI_MAX_R2T |
2336                                   ISCSI_IMM_DATA_EN |
2337                                   ISCSI_FIRST_BURST |
2338                                   ISCSI_MAX_BURST |
2339                                   ISCSI_PDU_INORDER_EN |
2340                                   ISCSI_DATASEQ_INORDER_EN |
2341                                   ISCSI_ERL |
2342                                   ISCSI_CONN_PORT |
2343                                   ISCSI_CONN_ADDRESS |
2344                                   ISCSI_EXP_STATSN |
2345                                   ISCSI_PERSISTENT_PORT |
2346                                   ISCSI_PERSISTENT_ADDRESS |
2347                                   ISCSI_TARGET_NAME |
2348                                   ISCSI_TPGT,
2349         .host_template          = &iscsi_sht,
2350         .conndata_size          = sizeof(struct iscsi_conn),
2351         .max_conn               = 1,
2352         .max_cmd_len            = ISCSI_TCP_MAX_CMD_LEN,
2353         /* session management */
2354         .create_session         = iscsi_tcp_session_create,
2355         .destroy_session        = iscsi_tcp_session_destroy,
2356         /* connection management */
2357         .create_conn            = iscsi_tcp_conn_create,
2358         .bind_conn              = iscsi_tcp_conn_bind,
2359         .destroy_conn           = iscsi_tcp_conn_destroy,
2360         .set_param              = iscsi_conn_set_param,
2361         .get_conn_param         = iscsi_tcp_conn_get_param,
2362         .get_session_param      = iscsi_session_get_param,
2363         .start_conn             = iscsi_conn_start,
2364         .stop_conn              = iscsi_tcp_conn_stop,
2365         /* IO */
2366         .send_pdu               = iscsi_conn_send_pdu,
2367         .get_stats              = iscsi_conn_get_stats,
2368         .init_cmd_task          = iscsi_tcp_cmd_init,
2369         .init_mgmt_task         = iscsi_tcp_mgmt_init,
2370         .xmit_cmd_task          = iscsi_tcp_ctask_xmit,
2371         .xmit_mgmt_task         = iscsi_tcp_mtask_xmit,
2372         .cleanup_cmd_task       = iscsi_tcp_cleanup_ctask,
2373         /* recovery */
2374         .session_recovery_timedout = iscsi_session_recovery_timedout,
2375 };
2376
2377 static int __init
2378 iscsi_tcp_init(void)
2379 {
2380         if (iscsi_max_lun < 1) {
2381                 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
2382                        iscsi_max_lun);
2383                 return -EINVAL;
2384         }
2385         iscsi_tcp_transport.max_lun = iscsi_max_lun;
2386
2387         if (!iscsi_register_transport(&iscsi_tcp_transport))
2388                 return -ENODEV;
2389
2390         return 0;
2391 }
2392
2393 static void __exit
2394 iscsi_tcp_exit(void)
2395 {
2396         iscsi_unregister_transport(&iscsi_tcp_transport);
2397 }
2398
2399 module_init(iscsi_tcp_init);
2400 module_exit(iscsi_tcp_exit);