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