[SCSI] iscsi: preemt fix and cleanup
[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 Mike Christie
7  * maintained by open-iscsi@googlegroups.com
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published
11  * by the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * General Public License for more details.
18  *
19  * See the file COPYING included with this distribution for more details.
20  *
21  * Credits:
22  *      Christoph Hellwig
23  *      FUJITA Tomonori
24  *      Arne Redlich
25  *      Zhenyu Wang
26  */
27
28 #include <linux/types.h>
29 #include <linux/list.h>
30 #include <linux/inet.h>
31 #include <linux/blkdev.h>
32 #include <linux/crypto.h>
33 #include <linux/delay.h>
34 #include <linux/kfifo.h>
35 #include <linux/scatterlist.h>
36 #include <net/tcp.h>
37 #include <scsi/scsi_cmnd.h>
38 #include <scsi/scsi_device.h>
39 #include <scsi/scsi_eh.h>
40 #include <scsi/scsi_request.h>
41 #include <scsi/scsi_tcq.h>
42 #include <scsi/scsi_host.h>
43 #include <scsi/scsi.h>
44 #include <scsi/scsi_transport_iscsi.h>
45
46 #include "iscsi_tcp.h"
47
48 MODULE_AUTHOR("Dmitry Yusupov <dmitry_yus@yahoo.com>, "
49               "Alex Aizman <itn780@yahoo.com>");
50 MODULE_DESCRIPTION("iSCSI/TCP data-path");
51 MODULE_LICENSE("GPL");
52
53 /* #define DEBUG_TCP */
54 /* #define DEBUG_SCSI */
55 #define DEBUG_ASSERT
56
57 #ifdef DEBUG_TCP
58 #define debug_tcp(fmt...) printk(KERN_DEBUG "tcp: " fmt)
59 #else
60 #define debug_tcp(fmt...)
61 #endif
62
63 #ifdef DEBUG_SCSI
64 #define debug_scsi(fmt...) printk(KERN_DEBUG "scsi: " fmt)
65 #else
66 #define debug_scsi(fmt...)
67 #endif
68
69 #ifndef DEBUG_ASSERT
70 #ifdef BUG_ON
71 #undef BUG_ON
72 #endif
73 #define BUG_ON(expr)
74 #endif
75
76 #define INVALID_SN_DELTA        0xffff
77
78 static unsigned int iscsi_max_lun = 512;
79 module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
80
81 /* global data */
82 static kmem_cache_t *taskcache;
83
84 static inline void
85 iscsi_buf_init_virt(struct iscsi_buf *ibuf, char *vbuf, int size)
86 {
87         sg_init_one(&ibuf->sg, (u8 *)vbuf, size);
88         ibuf->sent = 0;
89 }
90
91 static inline void
92 iscsi_buf_init_iov(struct iscsi_buf *ibuf, char *vbuf, int size)
93 {
94         ibuf->sg.page = (void*)vbuf;
95         ibuf->sg.offset = (unsigned int)-1;
96         ibuf->sg.length = size;
97         ibuf->sent = 0;
98 }
99
100 static inline void*
101 iscsi_buf_iov_base(struct iscsi_buf *ibuf)
102 {
103         return (char*)ibuf->sg.page + ibuf->sent;
104 }
105
106 static inline void
107 iscsi_buf_init_sg(struct iscsi_buf *ibuf, struct scatterlist *sg)
108 {
109         /*
110          * Fastpath: sg element fits into single page
111          */
112         if (sg->length + sg->offset <= PAGE_SIZE && page_count(sg->page) >= 2) {
113                 ibuf->sg.page = sg->page;
114                 ibuf->sg.offset = sg->offset;
115                 ibuf->sg.length = sg->length;
116         } else
117                 iscsi_buf_init_iov(ibuf, page_address(sg->page), sg->length);
118         ibuf->sent = 0;
119 }
120
121 static inline int
122 iscsi_buf_left(struct iscsi_buf *ibuf)
123 {
124         int rc;
125
126         rc = ibuf->sg.length - ibuf->sent;
127         BUG_ON(rc < 0);
128         return rc;
129 }
130
131 static inline void
132 iscsi_hdr_digest(struct iscsi_conn *conn, struct iscsi_buf *buf,
133                  u8* crc)
134 {
135         crypto_digest_digest(conn->tx_tfm, &buf->sg, 1, crc);
136         buf->sg.length += sizeof(uint32_t);
137 }
138
139 static void
140 iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
141 {
142         struct iscsi_session *session = conn->session;
143         unsigned long flags;
144
145         spin_lock_irqsave(&session->lock, flags);
146         if (session->conn_cnt == 1 || session->leadconn == conn)
147                 session->state = ISCSI_STATE_FAILED;
148         spin_unlock_irqrestore(&session->lock, flags);
149         set_bit(SUSPEND_BIT, &conn->suspend_tx);
150         set_bit(SUSPEND_BIT, &conn->suspend_rx);
151         iscsi_conn_error(iscsi_handle(conn), err);
152 }
153
154 static inline int
155 iscsi_check_assign_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
156 {
157         uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
158         uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
159
160         if (max_cmdsn < exp_cmdsn -1 &&
161             max_cmdsn > exp_cmdsn - INVALID_SN_DELTA)
162                 return ISCSI_ERR_MAX_CMDSN;
163         if (max_cmdsn > session->max_cmdsn ||
164             max_cmdsn < session->max_cmdsn - INVALID_SN_DELTA)
165                 session->max_cmdsn = max_cmdsn;
166         if (exp_cmdsn > session->exp_cmdsn ||
167             exp_cmdsn < session->exp_cmdsn - INVALID_SN_DELTA)
168                 session->exp_cmdsn = exp_cmdsn;
169
170         return 0;
171 }
172
173 static inline int
174 iscsi_hdr_extract(struct iscsi_conn *conn)
175 {
176         struct sk_buff *skb = conn->in.skb;
177
178         if (conn->in.copy >= conn->hdr_size &&
179             conn->in_progress == IN_PROGRESS_WAIT_HEADER) {
180                 /*
181                  * Zero-copy PDU Header: using connection context
182                  * to store header pointer.
183                  */
184                 if (skb_shinfo(skb)->frag_list == NULL &&
185                     !skb_shinfo(skb)->nr_frags)
186                         conn->in.hdr = (struct iscsi_hdr *)
187                                 ((char*)skb->data + conn->in.offset);
188                 else {
189                         /* ignoring return code since we checked
190                          * in.copy before */
191                         skb_copy_bits(skb, conn->in.offset,
192                                 &conn->hdr, conn->hdr_size);
193                         conn->in.hdr = &conn->hdr;
194                 }
195                 conn->in.offset += conn->hdr_size;
196                 conn->in.copy -= conn->hdr_size;
197         } else {
198                 int hdr_remains;
199                 int copylen;
200
201                 /*
202                  * PDU header scattered across SKB's,
203                  * copying it... This'll happen quite rarely.
204                  */
205
206                 if (conn->in_progress == IN_PROGRESS_WAIT_HEADER)
207                         conn->in.hdr_offset = 0;
208
209                 hdr_remains = conn->hdr_size - conn->in.hdr_offset;
210                 BUG_ON(hdr_remains <= 0);
211
212                 copylen = min(conn->in.copy, hdr_remains);
213                 skb_copy_bits(skb, conn->in.offset,
214                         (char*)&conn->hdr + conn->in.hdr_offset, copylen);
215
216                 debug_tcp("PDU gather offset %d bytes %d in.offset %d "
217                        "in.copy %d\n", conn->in.hdr_offset, copylen,
218                        conn->in.offset, conn->in.copy);
219
220                 conn->in.offset += copylen;
221                 conn->in.copy -= copylen;
222                 if (copylen < hdr_remains)  {
223                         conn->in_progress = IN_PROGRESS_HEADER_GATHER;
224                         conn->in.hdr_offset += copylen;
225                         return -EAGAIN;
226                 }
227                 conn->in.hdr = &conn->hdr;
228                 conn->discontiguous_hdr_cnt++;
229                 conn->in_progress = IN_PROGRESS_WAIT_HEADER;
230         }
231
232         return 0;
233 }
234
235 static inline void
236 iscsi_ctask_cleanup(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
237 {
238         struct scsi_cmnd *sc = ctask->sc;
239         struct iscsi_session *session = conn->session;
240
241         spin_lock(&session->lock);
242         if (unlikely(!sc)) {
243                 spin_unlock(&session->lock);
244                 return;
245         }
246         if (sc->sc_data_direction == DMA_TO_DEVICE) {
247                 struct iscsi_data_task *dtask, *n;
248                 /* WRITE: cleanup Data-Out's if any */
249                 spin_lock(&conn->lock);
250                 list_for_each_entry_safe(dtask, n, &ctask->dataqueue, item) {
251                         list_del(&dtask->item);
252                         mempool_free(dtask, ctask->datapool);
253                 }
254                 spin_unlock(&conn->lock);
255         }
256         ctask->xmstate = XMSTATE_IDLE;
257         ctask->r2t = NULL;
258         ctask->sc = NULL;
259         __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
260         spin_unlock(&session->lock);
261 }
262
263 /**
264  * iscsi_cmd_rsp - SCSI Command Response processing
265  * @conn: iscsi connection
266  * @ctask: scsi command task
267  **/
268 static int
269 iscsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
270 {
271         int rc;
272         struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)conn->in.hdr;
273         struct iscsi_session *session = conn->session;
274         struct scsi_cmnd *sc = ctask->sc;
275
276         rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
277         if (rc) {
278                 sc->result = (DID_ERROR << 16);
279                 goto out;
280         }
281
282         conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
283
284         sc->result = (DID_OK << 16) | rhdr->cmd_status;
285
286         if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
287                 sc->result = (DID_ERROR << 16);
288                 goto out;
289         }
290
291         if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION && conn->senselen) {
292                 int sensecopy = min(conn->senselen, SCSI_SENSE_BUFFERSIZE);
293
294                 memcpy(sc->sense_buffer, conn->data + 2, sensecopy);
295                 debug_scsi("copied %d bytes of sense\n", sensecopy);
296         }
297
298         if (sc->sc_data_direction == DMA_TO_DEVICE)
299                 goto out;
300
301         if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
302                 int res_count = be32_to_cpu(rhdr->residual_count);
303
304                 if (res_count > 0 && res_count <= sc->request_bufflen)
305                         sc->resid = res_count;
306                 else
307                         sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
308         } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
309                 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
310         else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
311                 sc->resid = be32_to_cpu(rhdr->residual_count);
312
313 out:
314         debug_scsi("done [sc %lx res %d itt 0x%x]\n",
315                    (long)sc, sc->result, ctask->itt);
316         conn->scsirsp_pdus_cnt++;
317         iscsi_ctask_cleanup(conn, ctask);
318         sc->scsi_done(sc);
319         return rc;
320 }
321
322 /**
323  * iscsi_data_rsp - SCSI Data-In Response processing
324  * @conn: iscsi connection
325  * @ctask: scsi command task
326  **/
327 static int
328 iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
329 {
330         int rc;
331         struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)conn->in.hdr;
332         struct iscsi_session *session = conn->session;
333         int datasn = be32_to_cpu(rhdr->datasn);
334
335         rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
336         if (rc)
337                 return rc;
338         /*
339          * setup Data-In byte counter (gets decremented..)
340          */
341         ctask->data_count = conn->in.datalen;
342
343         if (conn->in.datalen == 0)
344                 return 0;
345
346         if (ctask->datasn != datasn)
347                 return ISCSI_ERR_DATASN;
348
349         ctask->datasn++;
350
351         ctask->data_offset = be32_to_cpu(rhdr->offset);
352         if (ctask->data_offset + conn->in.datalen > ctask->total_length)
353                 return ISCSI_ERR_DATA_OFFSET;
354
355         if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
356                 struct scsi_cmnd *sc = ctask->sc;
357
358                 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
359                 if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
360                         int res_count = be32_to_cpu(rhdr->residual_count);
361
362                         if (res_count > 0 &&
363                             res_count <= sc->request_bufflen) {
364                                 sc->resid = res_count;
365                                 sc->result = (DID_OK << 16) | rhdr->cmd_status;
366                         } else
367                                 sc->result = (DID_BAD_TARGET << 16) |
368                                         rhdr->cmd_status;
369                 } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
370                         sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
371                 else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW) {
372                         sc->resid = be32_to_cpu(rhdr->residual_count);
373                         sc->result = (DID_OK << 16) | rhdr->cmd_status;
374                 } else
375                         sc->result = (DID_OK << 16) | rhdr->cmd_status;
376         }
377
378         conn->datain_pdus_cnt++;
379         return 0;
380 }
381
382 /**
383  * iscsi_solicit_data_init - initialize first Data-Out
384  * @conn: iscsi connection
385  * @ctask: scsi command task
386  * @r2t: R2T info
387  *
388  * Notes:
389  *      Initialize first Data-Out within this R2T sequence and finds
390  *      proper data_offset within this SCSI command.
391  *
392  *      This function is called with connection lock taken.
393  **/
394 static void
395 iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
396                         struct iscsi_r2t_info *r2t)
397 {
398         struct iscsi_data *hdr;
399         struct iscsi_data_task *dtask;
400         struct scsi_cmnd *sc = ctask->sc;
401
402         dtask = mempool_alloc(ctask->datapool, GFP_ATOMIC);
403         BUG_ON(!dtask);
404         hdr = &dtask->hdr;
405         memset(hdr, 0, sizeof(struct iscsi_data));
406         hdr->ttt = r2t->ttt;
407         hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
408         r2t->solicit_datasn++;
409         hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
410         memcpy(hdr->lun, ctask->hdr.lun, sizeof(hdr->lun));
411         hdr->itt = ctask->hdr.itt;
412         hdr->exp_statsn = r2t->exp_statsn;
413         hdr->offset = cpu_to_be32(r2t->data_offset);
414         if (r2t->data_length > conn->max_xmit_dlength) {
415                 hton24(hdr->dlength, conn->max_xmit_dlength);
416                 r2t->data_count = conn->max_xmit_dlength;
417                 hdr->flags = 0;
418         } else {
419                 hton24(hdr->dlength, r2t->data_length);
420                 r2t->data_count = r2t->data_length;
421                 hdr->flags = ISCSI_FLAG_CMD_FINAL;
422         }
423         conn->dataout_pdus_cnt++;
424
425         r2t->sent = 0;
426
427         iscsi_buf_init_virt(&r2t->headbuf, (char*)hdr,
428                            sizeof(struct iscsi_hdr));
429
430         r2t->dtask = dtask;
431
432         if (sc->use_sg) {
433                 int i, sg_count = 0;
434                 struct scatterlist *sg = sc->request_buffer;
435
436                 r2t->sg = NULL;
437                 for (i = 0; i < sc->use_sg; i++, sg += 1) {
438                         /* FIXME: prefetch ? */
439                         if (sg_count + sg->length > r2t->data_offset) {
440                                 int page_offset;
441
442                                 /* sg page found! */
443
444                                 /* offset within this page */
445                                 page_offset = r2t->data_offset - sg_count;
446
447                                 /* fill in this buffer */
448                                 iscsi_buf_init_sg(&r2t->sendbuf, sg);
449                                 r2t->sendbuf.sg.offset += page_offset;
450                                 r2t->sendbuf.sg.length -= page_offset;
451
452                                 /* xmit logic will continue with next one */
453                                 r2t->sg = sg + 1;
454                                 break;
455                         }
456                         sg_count += sg->length;
457                 }
458                 BUG_ON(r2t->sg == NULL);
459         } else
460                 iscsi_buf_init_iov(&ctask->sendbuf,
461                             (char*)sc->request_buffer + r2t->data_offset,
462                             r2t->data_count);
463
464         list_add(&dtask->item, &ctask->dataqueue);
465 }
466
467 /**
468  * iscsi_r2t_rsp - iSCSI R2T Response processing
469  * @conn: iscsi connection
470  * @ctask: scsi command task
471  **/
472 static int
473 iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
474 {
475         struct iscsi_r2t_info *r2t;
476         struct iscsi_session *session = conn->session;
477         struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)conn->in.hdr;
478         int r2tsn = be32_to_cpu(rhdr->r2tsn);
479         int rc;
480
481         if (conn->in.ahslen)
482                 return ISCSI_ERR_AHSLEN;
483
484         if (conn->in.datalen)
485                 return ISCSI_ERR_DATALEN;
486
487         if (ctask->exp_r2tsn && ctask->exp_r2tsn != r2tsn)
488                 return ISCSI_ERR_R2TSN;
489
490         rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
491         if (rc)
492                 return rc;
493
494         /* FIXME: use R2TSN to detect missing R2T */
495
496         /* fill-in new R2T associated with the task */
497         spin_lock(&session->lock);
498         if (!ctask->sc || ctask->mtask ||
499              session->state != ISCSI_STATE_LOGGED_IN) {
500                 printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in "
501                        "recovery...\n", ctask->itt);
502                 spin_unlock(&session->lock);
503                 return 0;
504         }
505         rc = __kfifo_get(ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
506         BUG_ON(!rc);
507
508         r2t->exp_statsn = rhdr->statsn;
509         r2t->data_length = be32_to_cpu(rhdr->data_length);
510         if (r2t->data_length == 0 ||
511             r2t->data_length > session->max_burst) {
512                 spin_unlock(&session->lock);
513                 return ISCSI_ERR_DATALEN;
514         }
515
516         r2t->data_offset = be32_to_cpu(rhdr->data_offset);
517         if (r2t->data_offset + r2t->data_length > ctask->total_length) {
518                 spin_unlock(&session->lock);
519                 return ISCSI_ERR_DATALEN;
520         }
521
522         r2t->ttt = rhdr->ttt; /* no flip */
523         r2t->solicit_datasn = 0;
524
525         iscsi_solicit_data_init(conn, ctask, r2t);
526
527         ctask->exp_r2tsn = r2tsn + 1;
528         ctask->xmstate |= XMSTATE_SOL_HDR;
529         __kfifo_put(ctask->r2tqueue, (void*)&r2t, sizeof(void*));
530         __kfifo_put(conn->writequeue, (void*)&ctask, sizeof(void*));
531
532         schedule_work(&conn->xmitwork);
533         conn->r2t_pdus_cnt++;
534         spin_unlock(&session->lock);
535
536         return 0;
537 }
538
539 static int
540 iscsi_hdr_recv(struct iscsi_conn *conn)
541 {
542         int rc = 0;
543         struct iscsi_hdr *hdr;
544         struct iscsi_cmd_task *ctask;
545         struct iscsi_session *session = conn->session;
546         uint32_t cdgst, rdgst = 0;
547
548         hdr = conn->in.hdr;
549
550         /* verify PDU length */
551         conn->in.datalen = ntoh24(hdr->dlength);
552         if (conn->in.datalen > conn->max_recv_dlength) {
553                 printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
554                        conn->in.datalen, conn->max_recv_dlength);
555                 return ISCSI_ERR_DATALEN;
556         }
557         conn->data_copied = 0;
558
559         /* read AHS */
560         conn->in.ahslen = hdr->hlength*(4*sizeof(__u16));
561         conn->in.offset += conn->in.ahslen;
562         conn->in.copy -= conn->in.ahslen;
563         if (conn->in.copy < 0) {
564                 printk(KERN_ERR "iscsi_tcp: can't handle AHS with length "
565                        "%d bytes\n", conn->in.ahslen);
566                 return ISCSI_ERR_AHSLEN;
567         }
568
569         /* calculate read padding */
570         conn->in.padding = conn->in.datalen & (ISCSI_PAD_LEN-1);
571         if (conn->in.padding) {
572                 conn->in.padding = ISCSI_PAD_LEN - conn->in.padding;
573                 debug_scsi("read padding %d bytes\n", conn->in.padding);
574         }
575
576         if (conn->hdrdgst_en) {
577                 struct scatterlist sg;
578
579                 sg_init_one(&sg, (u8 *)hdr,
580                             sizeof(struct iscsi_hdr) + conn->in.ahslen);
581                 crypto_digest_digest(conn->rx_tfm, &sg, 1, (u8 *)&cdgst);
582                 rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) +
583                                      conn->in.ahslen);
584         }
585
586         /* save opcode for later */
587         conn->in.opcode = hdr->opcode;
588
589         /* verify itt (itt encoding: age+cid+itt) */
590         if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
591                 if ((hdr->itt & AGE_MASK) !=
592                                 (session->age << AGE_SHIFT)) {
593                         printk(KERN_ERR "iscsi_tcp: received itt %x expected "
594                                 "session age (%x)\n", hdr->itt,
595                                 session->age & AGE_MASK);
596                         return ISCSI_ERR_BAD_ITT;
597                 }
598
599                 if ((hdr->itt & CID_MASK) != (conn->id << CID_SHIFT)) {
600                         printk(KERN_ERR "iscsi_tcp: received itt %x, expected "
601                                 "CID (%x)\n", hdr->itt, conn->id);
602                         return ISCSI_ERR_BAD_ITT;
603                 }
604                 conn->in.itt = hdr->itt & ITT_MASK;
605         } else
606                 conn->in.itt = hdr->itt;
607
608         debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n",
609                   hdr->opcode, conn->in.offset, conn->in.copy,
610                   conn->in.ahslen, conn->in.datalen);
611
612         if (conn->in.itt < session->cmds_max) {
613                 if (conn->hdrdgst_en && cdgst != rdgst) {
614                         printk(KERN_ERR "iscsi_tcp: itt %x: hdrdgst error "
615                                "recv 0x%x calc 0x%x\n", conn->in.itt, rdgst,
616                                cdgst);
617                         return ISCSI_ERR_HDR_DGST;
618                 }
619
620                 ctask = (struct iscsi_cmd_task *)session->cmds[conn->in.itt];
621
622                 if (!ctask->sc) {
623                         printk(KERN_INFO "iscsi_tcp: dropping ctask with "
624                                "itt 0x%x\n", ctask->itt);
625                         conn->in.datalen = 0; /* force drop */
626                         return 0;
627                 }
628
629                 if (ctask->sc->SCp.phase != session->age) {
630                         printk(KERN_ERR "iscsi_tcp: ctask's session age %d, "
631                                 "expected %d\n", ctask->sc->SCp.phase,
632                                 session->age);
633                         return ISCSI_ERR_SESSION_FAILED;
634                 }
635
636                 conn->in.ctask = ctask;
637
638                 debug_scsi("rsp [op 0x%x cid %d sc %lx itt 0x%x len %d]\n",
639                            hdr->opcode, conn->id, (long)ctask->sc,
640                            ctask->itt, conn->in.datalen);
641
642                 switch(conn->in.opcode) {
643                 case ISCSI_OP_SCSI_CMD_RSP:
644                         BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
645                         if (ctask->hdr.flags & ISCSI_FLAG_CMD_WRITE)
646                                 rc = iscsi_cmd_rsp(conn, ctask);
647                         else if (!conn->in.datalen)
648                                 rc = iscsi_cmd_rsp(conn, ctask);
649                         else
650                                 /*
651                                  * got sense or response data; copying PDU
652                                  * Header to the connection's header
653                                  * placeholder
654                                  */
655                                 memcpy(&conn->hdr, hdr,
656                                        sizeof(struct iscsi_hdr));
657                         break;
658                 case ISCSI_OP_SCSI_DATA_IN:
659                         BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
660                         /* save flags for non-exceptional status */
661                         conn->in.flags = hdr->flags;
662                         /* save cmd_status for sense data */
663                         conn->in.cmd_status =
664                                 ((struct iscsi_data_rsp*)hdr)->cmd_status;
665                         rc = iscsi_data_rsp(conn, ctask);
666                         break;
667                 case ISCSI_OP_R2T:
668                         BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
669                         if (ctask->hdr.flags & ISCSI_FLAG_CMD_WRITE &&
670                             ctask->sc->sc_data_direction == DMA_TO_DEVICE)
671                                 rc = iscsi_r2t_rsp(conn, ctask);
672                         else
673                                 rc = ISCSI_ERR_PROTO;
674                         break;
675                 case ISCSI_OP_NOOP_IN:
676                 case ISCSI_OP_TEXT_RSP:
677                 case ISCSI_OP_LOGOUT_RSP:
678                 case ISCSI_OP_ASYNC_EVENT:
679                 case ISCSI_OP_REJECT:
680                         rc = iscsi_check_assign_cmdsn(session,
681                                                  (struct iscsi_nopin*)hdr);
682                         if (rc)
683                                 break;
684
685                         /* update ExpStatSN */
686                         conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
687                         if (!conn->in.datalen) {
688                                 struct iscsi_mgmt_task *mtask;
689
690                                 rc = iscsi_recv_pdu(iscsi_handle(conn), hdr,
691                                                     NULL, 0);
692                                 mtask = (struct iscsi_mgmt_task *)
693                                         session->mgmt_cmds[conn->in.itt -
694                                                 ISCSI_MGMT_ITT_OFFSET];
695                                 if (conn->login_mtask != mtask) {
696                                         spin_lock(&session->lock);
697                                         __kfifo_put(session->mgmtpool.queue,
698                                             (void*)&mtask, sizeof(void*));
699                                         spin_unlock(&session->lock);
700                                 }
701                         }
702                         break;
703                 default:
704                         rc = ISCSI_ERR_BAD_OPCODE;
705                         break;
706                 }
707         } else if (conn->in.itt >= ISCSI_MGMT_ITT_OFFSET &&
708                    conn->in.itt < ISCSI_MGMT_ITT_OFFSET +
709                                         session->mgmtpool_max) {
710                 struct iscsi_mgmt_task *mtask = (struct iscsi_mgmt_task *)
711                                         session->mgmt_cmds[conn->in.itt -
712                                                 ISCSI_MGMT_ITT_OFFSET];
713
714                 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
715                            conn->in.opcode, conn->id, mtask->itt,
716                            conn->in.datalen);
717
718                 switch(conn->in.opcode) {
719                 case ISCSI_OP_LOGIN_RSP:
720                 case ISCSI_OP_TEXT_RSP:
721                         rc = iscsi_check_assign_cmdsn(session,
722                                                  (struct iscsi_nopin*)hdr);
723                         if (rc)
724                                 break;
725
726                         if (!conn->in.datalen) {
727                                 rc = iscsi_recv_pdu(iscsi_handle(conn), hdr,
728                                                     NULL, 0);
729                                 if (conn->login_mtask != mtask) {
730                                         spin_lock(&session->lock);
731                                         __kfifo_put(session->mgmtpool.queue,
732                                             (void*)&mtask, sizeof(void*));
733                                         spin_unlock(&session->lock);
734                                 }
735                         }
736                         break;
737                 case ISCSI_OP_SCSI_TMFUNC_RSP:
738                         rc = iscsi_check_assign_cmdsn(session,
739                                                  (struct iscsi_nopin*)hdr);
740                         if (rc)
741                                 break;
742
743                         if (conn->in.datalen || conn->in.ahslen) {
744                                 rc = ISCSI_ERR_PROTO;
745                                 break;
746                         }
747                         conn->tmfrsp_pdus_cnt++;
748                         spin_lock(&session->lock);
749                         if (conn->tmabort_state == TMABORT_INITIAL) {
750                                 __kfifo_put(session->mgmtpool.queue,
751                                                 (void*)&mtask, sizeof(void*));
752                                 conn->tmabort_state =
753                                         ((struct iscsi_tm_rsp *)hdr)->
754                                         response == SCSI_TCP_TM_RESP_COMPLETE ?
755                                                 TMABORT_SUCCESS:TMABORT_FAILED;
756                                 /* unblock eh_abort() */
757                                 wake_up(&conn->ehwait);
758                         }
759                         spin_unlock(&session->lock);
760                         break;
761                 default:
762                         rc = ISCSI_ERR_BAD_OPCODE;
763                         break;
764                 }
765         } else if (conn->in.itt == ISCSI_RESERVED_TAG) {
766                 if (conn->in.opcode == ISCSI_OP_NOOP_IN && !conn->in.datalen) {
767                         rc = iscsi_check_assign_cmdsn(session,
768                                                  (struct iscsi_nopin*)hdr);
769                         if (!rc)
770                                 rc = iscsi_recv_pdu(iscsi_handle(conn),
771                                                     hdr, NULL, 0);
772                 }
773                 else
774                         rc = ISCSI_ERR_BAD_OPCODE;
775         } else
776                 rc = ISCSI_ERR_BAD_ITT;
777
778         return rc;
779 }
780
781 /**
782  * iscsi_ctask_copy - copy skb bits to the destanation cmd task
783  * @conn: iscsi connection
784  * @ctask: scsi command task
785  * @buf: buffer to copy to
786  * @buf_size: size of buffer
787  * @offset: offset within the buffer
788  *
789  * Notes:
790  *      The function calls skb_copy_bits() and updates per-connection and
791  *      per-cmd byte counters.
792  *
793  *      Read counters (in bytes):
794  *
795  *      conn->in.offset         offset within in progress SKB
796  *      conn->in.copy           left to copy from in progress SKB
797  *                              including padding
798  *      conn->in.copied         copied already from in progress SKB
799  *      conn->data_copied       copied already from in progress buffer
800  *      ctask->sent             total bytes sent up to the MidLayer
801  *      ctask->data_count       left to copy from in progress Data-In
802  *      buf_left                left to copy from in progress buffer
803  **/
804 static inline int
805 iscsi_ctask_copy(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
806                 void *buf, int buf_size, int offset)
807 {
808         int buf_left = buf_size - (conn->data_copied + offset);
809         int size = min(conn->in.copy, buf_left);
810         int rc;
811
812         size = min(size, ctask->data_count);
813
814         debug_tcp("ctask_copy %d bytes at offset %d copied %d\n",
815                size, conn->in.offset, conn->in.copied);
816
817         BUG_ON(size <= 0);
818         BUG_ON(ctask->sent + size > ctask->total_length);
819
820         rc = skb_copy_bits(conn->in.skb, conn->in.offset,
821                            (char*)buf + (offset + conn->data_copied), size);
822         /* must fit into skb->len */
823         BUG_ON(rc);
824
825         conn->in.offset += size;
826         conn->in.copy -= size;
827         conn->in.copied += size;
828         conn->data_copied += size;
829         ctask->sent += size;
830         ctask->data_count -= size;
831
832         BUG_ON(conn->in.copy < 0);
833         BUG_ON(ctask->data_count < 0);
834
835         if (buf_size != (conn->data_copied + offset)) {
836                 if (!ctask->data_count) {
837                         BUG_ON(buf_size - conn->data_copied < 0);
838                         /* done with this PDU */
839                         return buf_size - conn->data_copied;
840                 }
841                 return -EAGAIN;
842         }
843
844         /* done with this buffer or with both - PDU and buffer */
845         conn->data_copied = 0;
846         return 0;
847 }
848
849 /**
850  * iscsi_tcp_copy - copy skb bits to the destanation buffer
851  * @conn: iscsi connection
852  * @buf: buffer to copy to
853  * @buf_size: number of bytes to copy
854  *
855  * Notes:
856  *      The function calls skb_copy_bits() and updates per-connection
857  *      byte counters.
858  **/
859 static inline int
860 iscsi_tcp_copy(struct iscsi_conn *conn, void *buf, int buf_size)
861 {
862         int buf_left = buf_size - conn->data_copied;
863         int size = min(conn->in.copy, buf_left);
864         int rc;
865
866         debug_tcp("tcp_copy %d bytes at offset %d copied %d\n",
867                size, conn->in.offset, conn->data_copied);
868         BUG_ON(size <= 0);
869
870         rc = skb_copy_bits(conn->in.skb, conn->in.offset,
871                            (char*)buf + conn->data_copied, size);
872         BUG_ON(rc);
873
874         conn->in.offset += size;
875         conn->in.copy -= size;
876         conn->in.copied += size;
877         conn->data_copied += size;
878
879         if (buf_size != conn->data_copied)
880                 return -EAGAIN;
881
882         return 0;
883 }
884
885 static inline void
886 partial_sg_digest_update(struct iscsi_conn *conn, struct scatterlist *sg,
887                          int offset, int length)
888 {
889         struct scatterlist temp;
890
891         memcpy(&temp, sg, sizeof(struct scatterlist));
892         temp.offset = offset;
893         temp.length = length;
894         crypto_digest_update(conn->data_rx_tfm, &temp, 1);
895 }
896
897 static int iscsi_scsi_data_in(struct iscsi_conn *conn)
898 {
899         struct iscsi_cmd_task *ctask = conn->in.ctask;
900         struct scsi_cmnd *sc = ctask->sc;
901         struct scatterlist tmp, *sg;
902         int i, offset, rc = 0;
903
904         BUG_ON((void*)ctask != sc->SCp.ptr);
905
906         /*
907          * copying Data-In into the Scsi_Cmnd
908          */
909         if (!sc->use_sg) {
910                 i = ctask->data_count;
911                 rc = iscsi_ctask_copy(conn, ctask, sc->request_buffer,
912                                       sc->request_bufflen, ctask->data_offset);
913                 if (rc == -EAGAIN)
914                         return rc;
915                 if (conn->datadgst_en) {
916                         sg_init_one(&tmp, sc->request_buffer, i);
917                         crypto_digest_update(conn->data_rx_tfm, &tmp, 1);
918                 }
919                 rc = 0;
920                 goto done;
921         }
922
923         offset = ctask->data_offset;
924         sg = sc->request_buffer;
925
926         if (ctask->data_offset)
927                 for (i = 0; i < ctask->sg_count; i++)
928                         offset -= sg[i].length;
929         /* we've passed through partial sg*/
930         if (offset < 0)
931                 offset = 0;
932
933         for (i = ctask->sg_count; i < sc->use_sg; i++) {
934                 char *dest;
935
936                 dest = kmap_atomic(sg[i].page, KM_SOFTIRQ0);
937                 rc = iscsi_ctask_copy(conn, ctask, dest + sg[i].offset,
938                                       sg[i].length, offset);
939                 kunmap_atomic(dest, KM_SOFTIRQ0);
940                 if (rc == -EAGAIN)
941                         /* continue with the next SKB/PDU */
942                         return rc;
943                 if (!rc) {
944                         if (conn->datadgst_en) {
945                                 if (!offset)
946                                         crypto_digest_update(conn->data_rx_tfm,
947                                                              &sg[i], 1);
948                                 else
949                                         partial_sg_digest_update(conn, &sg[i],
950                                                         sg[i].offset + offset,
951                                                         sg[i].length - offset);
952                         }
953                         offset = 0;
954                         ctask->sg_count++;
955                 }
956
957                 if (!ctask->data_count) {
958                         if (rc && conn->datadgst_en)
959                                 /*
960                                  * data-in is complete, but buffer not...
961                                  */
962                                 partial_sg_digest_update(conn, &sg[i],
963                                                 sg[i].offset, sg[i].length-rc);
964                         rc = 0;
965                         break;
966                 }
967
968                 if (!conn->in.copy)
969                         return -EAGAIN;
970         }
971         BUG_ON(ctask->data_count);
972
973 done:
974         /* check for non-exceptional status */
975         if (conn->in.flags & ISCSI_FLAG_DATA_STATUS) {
976                 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
977                            (long)sc, sc->result, ctask->itt);
978                 conn->scsirsp_pdus_cnt++;
979                 iscsi_ctask_cleanup(conn, ctask);
980                 sc->scsi_done(sc);
981         }
982
983         return rc;
984 }
985
986 static int
987 iscsi_data_recv(struct iscsi_conn *conn)
988 {
989         struct iscsi_session *session = conn->session;
990         int rc = 0;
991
992         switch(conn->in.opcode) {
993         case ISCSI_OP_SCSI_DATA_IN:
994                 rc = iscsi_scsi_data_in(conn);
995                 break;
996         case ISCSI_OP_SCSI_CMD_RSP: {
997                 /*
998                  * SCSI Sense Data:
999                  * copying the entire Data Segment.
1000                  */
1001                 if (iscsi_tcp_copy(conn, conn->data, conn->in.datalen)) {
1002                         rc = -EAGAIN;
1003                         goto exit;
1004                 }
1005
1006                 /*
1007                  * check for sense
1008                  */
1009                 conn->in.hdr = &conn->hdr;
1010                 conn->senselen = (conn->data[0] << 8) | conn->data[1];
1011                 rc = iscsi_cmd_rsp(conn, conn->in.ctask);
1012         }
1013         break;
1014         case ISCSI_OP_TEXT_RSP:
1015         case ISCSI_OP_LOGIN_RSP:
1016         case ISCSI_OP_NOOP_IN: {
1017                 struct iscsi_mgmt_task *mtask = NULL;
1018
1019                 if (conn->in.itt != ISCSI_RESERVED_TAG)
1020                         mtask = (struct iscsi_mgmt_task *)
1021                                 session->mgmt_cmds[conn->in.itt -
1022                                         ISCSI_MGMT_ITT_OFFSET];
1023
1024                 /*
1025                  * Collect data segment to the connection's data
1026                  * placeholder
1027                  */
1028                 if (iscsi_tcp_copy(conn, conn->data, conn->in.datalen)) {
1029                         rc = -EAGAIN;
1030                         goto exit;
1031                 }
1032
1033                 rc = iscsi_recv_pdu(iscsi_handle(conn), conn->in.hdr,
1034                                     conn->data, conn->in.datalen);
1035
1036                 if (mtask && conn->login_mtask != mtask) {
1037                         spin_lock(&session->lock);
1038                         __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1039                                     sizeof(void*));
1040                         spin_unlock(&session->lock);
1041                 }
1042         }
1043         break;
1044         default:
1045                 BUG_ON(1);
1046         }
1047 exit:
1048         return rc;
1049 }
1050
1051 /**
1052  * iscsi_tcp_data_recv - TCP receive in sendfile fashion
1053  * @rd_desc: read descriptor
1054  * @skb: socket buffer
1055  * @offset: offset in skb
1056  * @len: skb->len - offset
1057  **/
1058 static int
1059 iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
1060                 unsigned int offset, size_t len)
1061 {
1062         int rc;
1063         struct iscsi_conn *conn = rd_desc->arg.data;
1064         int start = skb_headlen(skb);
1065         int processed;
1066         char pad[ISCSI_PAD_LEN];
1067         struct scatterlist sg;
1068
1069         /*
1070          * Save current SKB and its offset in the corresponding
1071          * connection context.
1072          */
1073         conn->in.copy = start - offset;
1074         conn->in.offset = offset;
1075         conn->in.skb = skb;
1076         conn->in.len = conn->in.copy;
1077         BUG_ON(conn->in.copy <= 0);
1078         debug_tcp("in %d bytes\n", conn->in.copy);
1079
1080 more:
1081         conn->in.copied = 0;
1082         rc = 0;
1083
1084         if (unlikely(conn->suspend_rx)) {
1085                 debug_tcp("conn %d Rx suspended!\n", conn->id);
1086                 return 0;
1087         }
1088
1089         if (conn->in_progress == IN_PROGRESS_WAIT_HEADER ||
1090             conn->in_progress == IN_PROGRESS_HEADER_GATHER) {
1091                 rc = iscsi_hdr_extract(conn);
1092                 if (rc) {
1093                        if (rc == -EAGAIN)
1094                                 goto nomore;
1095                        else {
1096                                 iscsi_conn_failure(conn, rc);
1097                                 return 0;
1098                        }
1099                 }
1100
1101                 /*
1102                  * Verify and process incoming PDU header.
1103                  */
1104                 rc = iscsi_hdr_recv(conn);
1105                 if (!rc && conn->in.datalen) {
1106                         if (conn->datadgst_en &&
1107                             conn->in.opcode == ISCSI_OP_SCSI_DATA_IN) {
1108                                 BUG_ON(!conn->data_rx_tfm);
1109                                 crypto_digest_init(conn->data_rx_tfm);
1110                         }
1111                         conn->in_progress = IN_PROGRESS_DATA_RECV;
1112                 } else if (rc) {
1113                         iscsi_conn_failure(conn, rc);
1114                         return 0;
1115                 }
1116         }
1117
1118         if (conn->in_progress == IN_PROGRESS_DDIGEST_RECV) {
1119                 debug_tcp("extra data_recv offset %d copy %d\n",
1120                           conn->in.offset, conn->in.copy);
1121                 if (conn->in.opcode == ISCSI_OP_SCSI_DATA_IN) {
1122                         uint32_t recv_digest;
1123                         skb_copy_bits(conn->in.skb, conn->in.offset,
1124                                       &recv_digest, 4);
1125                         conn->in.offset += 4;
1126                         conn->in.copy -= 4;
1127                         if (recv_digest != conn->in.datadgst) {
1128                                 debug_tcp("iscsi_tcp: data digest error!"
1129                                           "0x%x != 0x%x\n", recv_digest,
1130                                           conn->in.datadgst);
1131                                 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
1132                                 return 0;
1133                         } else {
1134                                 debug_tcp("iscsi_tcp: data digest match!"
1135                                           "0x%x == 0x%x\n", recv_digest,
1136                                           conn->in.datadgst);
1137                                 conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1138                         }
1139                 }
1140         }
1141
1142         if (conn->in_progress == IN_PROGRESS_DATA_RECV && conn->in.copy) {
1143
1144                 debug_tcp("data_recv offset %d copy %d\n",
1145                        conn->in.offset, conn->in.copy);
1146
1147                 rc = iscsi_data_recv(conn);
1148                 if (rc) {
1149                         if (rc == -EAGAIN) {
1150                                 rd_desc->count = conn->in.datalen -
1151                                                 conn->in.ctask->data_count;
1152                                 goto again;
1153                         }
1154                         iscsi_conn_failure(conn, rc);
1155                         return 0;
1156                 }
1157                 conn->in.copy -= conn->in.padding;
1158                 conn->in.offset += conn->in.padding;
1159                 if (conn->datadgst_en &&
1160                     conn->in.opcode == ISCSI_OP_SCSI_DATA_IN) {
1161                         if (conn->in.padding) {
1162                                 debug_tcp("padding -> %d\n", conn->in.padding);
1163                                 memset(pad, 0, conn->in.padding);
1164                                 sg_init_one(&sg, pad, conn->in.padding);
1165                                 crypto_digest_update(conn->data_rx_tfm, &sg, 1);
1166                         }
1167                         crypto_digest_final(conn->data_rx_tfm,
1168                                             (u8 *) & conn->in.datadgst);
1169                         debug_tcp("rx digest 0x%x\n", conn->in.datadgst);
1170                         conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
1171                 } else
1172                         conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1173         }
1174
1175         debug_tcp("f, processed %d from out of %d padding %d\n",
1176                conn->in.offset - offset, (int)len, conn->in.padding);
1177         BUG_ON(conn->in.offset - offset > len);
1178
1179         if (conn->in.offset - offset != len) {
1180                 debug_tcp("continue to process %d bytes\n",
1181                        (int)len - (conn->in.offset - offset));
1182                 goto more;
1183         }
1184
1185 nomore:
1186         processed = conn->in.offset - offset;
1187         BUG_ON(processed == 0);
1188         return processed;
1189
1190 again:
1191         processed = conn->in.offset - offset;
1192         debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n",
1193                   processed, (int)len, (int)rd_desc->count);
1194         BUG_ON(processed == 0);
1195         BUG_ON(processed > len);
1196
1197         conn->rxdata_octets += processed;
1198         return processed;
1199 }
1200
1201 static void
1202 iscsi_tcp_data_ready(struct sock *sk, int flag)
1203 {
1204         struct iscsi_conn *conn = sk->sk_user_data;
1205         read_descriptor_t rd_desc;
1206
1207         read_lock(&sk->sk_callback_lock);
1208
1209         /* use rd_desc to pass 'conn' to iscsi_tcp_data_recv */
1210         rd_desc.arg.data = conn;
1211         rd_desc.count = 0;
1212         tcp_read_sock(sk, &rd_desc, iscsi_tcp_data_recv);
1213
1214         read_unlock(&sk->sk_callback_lock);
1215 }
1216
1217 static void
1218 iscsi_tcp_state_change(struct sock *sk)
1219 {
1220         struct iscsi_conn *conn;
1221         struct iscsi_session *session;
1222         void (*old_state_change)(struct sock *);
1223
1224         read_lock(&sk->sk_callback_lock);
1225
1226         conn = (struct iscsi_conn*)sk->sk_user_data;
1227         session = conn->session;
1228
1229         if (sk->sk_state == TCP_CLOSE_WAIT ||
1230             sk->sk_state == TCP_CLOSE) {
1231                 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1232                 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1233         }
1234
1235         old_state_change = conn->old_state_change;
1236
1237         read_unlock(&sk->sk_callback_lock);
1238
1239         old_state_change(sk);
1240 }
1241
1242 /**
1243  * iscsi_write_space - Called when more output buffer space is available
1244  * @sk: socket space is available for
1245  **/
1246 static void
1247 iscsi_write_space(struct sock *sk)
1248 {
1249         struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
1250         conn->old_write_space(sk);
1251         debug_tcp("iscsi_write_space: cid %d\n", conn->id);
1252         clear_bit(SUSPEND_BIT, &conn->suspend_tx);
1253         schedule_work(&conn->xmitwork);
1254 }
1255
1256 static void
1257 iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1258 {
1259         struct sock *sk = conn->sock->sk;
1260
1261         /* assign new callbacks */
1262         write_lock_bh(&sk->sk_callback_lock);
1263         sk->sk_user_data = conn;
1264         conn->old_data_ready = sk->sk_data_ready;
1265         conn->old_state_change = sk->sk_state_change;
1266         conn->old_write_space = sk->sk_write_space;
1267         sk->sk_data_ready = iscsi_tcp_data_ready;
1268         sk->sk_state_change = iscsi_tcp_state_change;
1269         sk->sk_write_space = iscsi_write_space;
1270         write_unlock_bh(&sk->sk_callback_lock);
1271 }
1272
1273 static void
1274 iscsi_conn_restore_callbacks(struct iscsi_conn *conn)
1275 {
1276         struct sock *sk = conn->sock->sk;
1277
1278         /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1279         write_lock_bh(&sk->sk_callback_lock);
1280         sk->sk_user_data    = NULL;
1281         sk->sk_data_ready   = conn->old_data_ready;
1282         sk->sk_state_change = conn->old_state_change;
1283         sk->sk_write_space  = conn->old_write_space;
1284         sk->sk_no_check  = 0;
1285         write_unlock_bh(&sk->sk_callback_lock);
1286 }
1287
1288 /**
1289  * iscsi_send - generic send routine
1290  * @sk: kernel's socket
1291  * @buf: buffer to write from
1292  * @size: actual size to write
1293  * @flags: socket's flags
1294  *
1295  * Notes:
1296  *      depending on buffer will use tcp_sendpage() or tcp_sendmsg().
1297  *      buf->sg.offset == -1 tells us that buffer is non S/G and forces
1298  *      to use tcp_sendmsg().
1299  */
1300 static inline int
1301 iscsi_send(struct socket *sk, struct iscsi_buf *buf, int size, int flags)
1302 {
1303         int res;
1304
1305         if ((int)buf->sg.offset >= 0) {
1306                 int offset = buf->sg.offset + buf->sent;
1307
1308                 /* tcp_sendpage */
1309                 res = sk->ops->sendpage(sk, buf->sg.page, offset, size, flags);
1310         } else {
1311                 struct msghdr msg;
1312
1313                 buf->iov.iov_base = iscsi_buf_iov_base(buf);
1314                 buf->iov.iov_len = size;
1315
1316                 memset(&msg, 0, sizeof(struct msghdr));
1317
1318                 /* tcp_sendmsg */
1319                 res = kernel_sendmsg(sk, &msg, &buf->iov, 1, size);
1320         }
1321
1322         return res;
1323 }
1324
1325 /**
1326  * iscsi_sendhdr - send PDU Header via tcp_sendpage()
1327  * @conn: iscsi connection
1328  * @buf: buffer to write from
1329  * @datalen: lenght of data to be sent after the header
1330  *
1331  * Notes:
1332  *      (Tx, Fast Path)
1333  **/
1334 static inline int
1335 iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
1336 {
1337         struct socket *sk = conn->sock;
1338         int flags = 0; /* MSG_DONTWAIT; */
1339         int res, size;
1340
1341         size = buf->sg.length - buf->sent;
1342         BUG_ON(buf->sent + size > buf->sg.length);
1343         if (buf->sent + size != buf->sg.length || datalen)
1344                 flags |= MSG_MORE;
1345
1346         res = iscsi_send(sk, buf, size, flags);
1347         debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res);
1348         if (res >= 0) {
1349                 conn->txdata_octets += res;
1350                 buf->sent += res;
1351                 if (size != res)
1352                         return -EAGAIN;
1353                 return 0;
1354         } else if (res == -EAGAIN) {
1355                 conn->sendpage_failures_cnt++;
1356                 set_bit(SUSPEND_BIT, &conn->suspend_tx);
1357         } else if (res == -EPIPE)
1358                 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1359
1360         return res;
1361 }
1362
1363 /**
1364  * iscsi_sendpage - send one page of iSCSI Data-Out.
1365  * @conn: iscsi connection
1366  * @buf: buffer to write from
1367  * @count: remaining data
1368  * @sent: number of bytes sent
1369  *
1370  * Notes:
1371  *      (Tx, Fast Path)
1372  **/
1373 static inline int
1374 iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
1375                int *count, int *sent)
1376 {
1377         struct socket *sk = conn->sock;
1378         int flags = 0; /* MSG_DONTWAIT; */
1379         int res, size;
1380
1381         size = buf->sg.length - buf->sent;
1382         BUG_ON(buf->sent + size > buf->sg.length);
1383         if (size > *count)
1384                 size = *count;
1385         if (buf->sent + size != buf->sg.length || *count != size)
1386                 flags |= MSG_MORE;
1387
1388         res = iscsi_send(sk, buf, size, flags);
1389         debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n",
1390                   size, buf->sent, *count, *sent, res);
1391         if (res >= 0) {
1392                 conn->txdata_octets += res;
1393                 buf->sent += res;
1394                 *count -= res;
1395                 *sent += res;
1396                 if (size != res)
1397                         return -EAGAIN;
1398                 return 0;
1399         } else if (res == -EAGAIN) {
1400                 conn->sendpage_failures_cnt++;
1401                 set_bit(SUSPEND_BIT, &conn->suspend_tx);
1402         } else if (res == -EPIPE)
1403                 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1404
1405         return res;
1406 }
1407
1408 static inline void
1409 iscsi_data_digest_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1410 {
1411         BUG_ON(!conn->data_tx_tfm);
1412         crypto_digest_init(conn->data_tx_tfm);
1413         ctask->digest_count = 4;
1414 }
1415
1416 static inline void
1417 iscsi_buf_data_digest_update(struct iscsi_conn *conn, struct iscsi_buf *buf)
1418 {
1419         struct scatterlist sg;
1420
1421         if (buf->sg.offset != -1)
1422                 crypto_digest_update(conn->data_tx_tfm, &buf->sg, 1);
1423         else {
1424                 sg_init_one(&sg, (char *)buf->sg.page, buf->sg.length);
1425                 crypto_digest_update(conn->data_tx_tfm, &sg, 1);
1426         }
1427 }
1428
1429 static inline int
1430 iscsi_digest_final_send(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1431                         struct iscsi_buf *buf, uint32_t *digest, int final)
1432 {
1433         int rc = 0;
1434         int sent = 0;
1435
1436         if (final)
1437                 crypto_digest_final(conn->data_tx_tfm, (u8*)digest);
1438
1439         iscsi_buf_init_virt(buf, (char*)digest, 4);
1440         rc = iscsi_sendpage(conn, buf, &ctask->digest_count, &sent);
1441         if (rc) {
1442                 ctask->datadigest = *digest;
1443                 ctask->xmstate |= XMSTATE_DATA_DIGEST;
1444         } else
1445                 ctask->digest_count = 4;
1446         return rc;
1447 }
1448
1449 /**
1450  * iscsi_solicit_data_cont - initialize next Data-Out
1451  * @conn: iscsi connection
1452  * @ctask: scsi command task
1453  * @r2t: R2T info
1454  * @left: bytes left to transfer
1455  *
1456  * Notes:
1457  *      Initialize next Data-Out within this R2T sequence and continue
1458  *      to process next Scatter-Gather element(if any) of this SCSI command.
1459  *
1460  *      Called under connection lock.
1461  **/
1462 static void
1463 iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1464                         struct iscsi_r2t_info *r2t, int left)
1465 {
1466         struct iscsi_data *hdr;
1467         struct iscsi_data_task *dtask;
1468         struct scsi_cmnd *sc = ctask->sc;
1469         int new_offset;
1470
1471         dtask = mempool_alloc(ctask->datapool, GFP_ATOMIC);
1472         BUG_ON(!dtask);
1473         hdr = &dtask->hdr;
1474         memset(hdr, 0, sizeof(struct iscsi_data));
1475         hdr->ttt = r2t->ttt;
1476         hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1477         r2t->solicit_datasn++;
1478         hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
1479         memcpy(hdr->lun, ctask->hdr.lun, sizeof(hdr->lun));
1480         hdr->itt = ctask->hdr.itt;
1481         hdr->exp_statsn = r2t->exp_statsn;
1482         new_offset = r2t->data_offset + r2t->sent;
1483         hdr->offset = cpu_to_be32(new_offset);
1484         if (left > conn->max_xmit_dlength) {
1485                 hton24(hdr->dlength, conn->max_xmit_dlength);
1486                 r2t->data_count = conn->max_xmit_dlength;
1487         } else {
1488                 hton24(hdr->dlength, left);
1489                 r2t->data_count = left;
1490                 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1491         }
1492         conn->dataout_pdus_cnt++;
1493
1494         iscsi_buf_init_virt(&r2t->headbuf, (char*)hdr,
1495                            sizeof(struct iscsi_hdr));
1496
1497         r2t->dtask = dtask;
1498
1499         if (sc->use_sg && !iscsi_buf_left(&r2t->sendbuf)) {
1500                 BUG_ON(ctask->bad_sg == r2t->sg);
1501                 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1502                 r2t->sg += 1;
1503         } else
1504                 iscsi_buf_init_iov(&ctask->sendbuf,
1505                             (char*)sc->request_buffer + new_offset,
1506                             r2t->data_count);
1507
1508         list_add(&dtask->item, &ctask->dataqueue);
1509 }
1510
1511 static void
1512 iscsi_unsolicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1513 {
1514         struct iscsi_data *hdr;
1515         struct iscsi_data_task *dtask;
1516
1517         dtask = mempool_alloc(ctask->datapool, GFP_ATOMIC);
1518         BUG_ON(!dtask);
1519         hdr = &dtask->hdr;
1520         memset(hdr, 0, sizeof(struct iscsi_data));
1521         hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
1522         hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
1523         ctask->unsol_datasn++;
1524         hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
1525         memcpy(hdr->lun, ctask->hdr.lun, sizeof(hdr->lun));
1526         hdr->itt = ctask->hdr.itt;
1527         hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
1528         hdr->offset = cpu_to_be32(ctask->total_length -
1529                                   ctask->r2t_data_count -
1530                                   ctask->unsol_count);
1531         if (ctask->unsol_count > conn->max_xmit_dlength) {
1532                 hton24(hdr->dlength, conn->max_xmit_dlength);
1533                 ctask->data_count = conn->max_xmit_dlength;
1534                 hdr->flags = 0;
1535         } else {
1536                 hton24(hdr->dlength, ctask->unsol_count);
1537                 ctask->data_count = ctask->unsol_count;
1538                 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1539         }
1540
1541         iscsi_buf_init_virt(&ctask->headbuf, (char*)hdr,
1542                            sizeof(struct iscsi_hdr));
1543
1544         list_add(&dtask->item, &ctask->dataqueue);
1545
1546         ctask->dtask = dtask;
1547 }
1548
1549 /**
1550  * iscsi_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
1551  * @conn: iscsi connection
1552  * @ctask: scsi command task
1553  * @sc: scsi command
1554  **/
1555 static void
1556 iscsi_cmd_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1557                 struct scsi_cmnd *sc)
1558 {
1559         struct iscsi_session *session = conn->session;
1560
1561         BUG_ON(__kfifo_len(ctask->r2tqueue));
1562
1563         ctask->sc = sc;
1564         ctask->conn = conn;
1565         ctask->hdr.opcode = ISCSI_OP_SCSI_CMD;
1566         ctask->hdr.flags = ISCSI_ATTR_SIMPLE;
1567         int_to_scsilun(sc->device->lun, (struct scsi_lun *)ctask->hdr.lun);
1568         ctask->hdr.itt = ctask->itt | (conn->id << CID_SHIFT) |
1569                          (session->age << AGE_SHIFT);
1570         ctask->hdr.data_length = cpu_to_be32(sc->request_bufflen);
1571         ctask->hdr.cmdsn = cpu_to_be32(session->cmdsn); session->cmdsn++;
1572         ctask->hdr.exp_statsn = cpu_to_be32(conn->exp_statsn);
1573         memcpy(ctask->hdr.cdb, sc->cmnd, sc->cmd_len);
1574         memset(&ctask->hdr.cdb[sc->cmd_len], 0, MAX_COMMAND_SIZE - sc->cmd_len);
1575
1576         ctask->mtask = NULL;
1577         ctask->sent = 0;
1578         ctask->sg_count = 0;
1579
1580         ctask->total_length = sc->request_bufflen;
1581
1582         if (sc->sc_data_direction == DMA_TO_DEVICE) {
1583                 ctask->exp_r2tsn = 0;
1584                 ctask->hdr.flags |= ISCSI_FLAG_CMD_WRITE;
1585                 BUG_ON(ctask->total_length == 0);
1586                 if (sc->use_sg) {
1587                         struct scatterlist *sg = sc->request_buffer;
1588
1589                         iscsi_buf_init_sg(&ctask->sendbuf,
1590                                           &sg[ctask->sg_count++]);
1591                         ctask->sg = sg;
1592                         ctask->bad_sg = sg + sc->use_sg;
1593                 } else {
1594                         iscsi_buf_init_iov(&ctask->sendbuf, sc->request_buffer,
1595                                         sc->request_bufflen);
1596                 }
1597
1598                 /*
1599                  * Write counters:
1600                  *
1601                  *      imm_count       bytes to be sent right after
1602                  *                      SCSI PDU Header
1603                  *
1604                  *      unsol_count     bytes(as Data-Out) to be sent
1605                  *                      without R2T ack right after
1606                  *                      immediate data
1607                  *
1608                  *      r2t_data_count  bytes to be sent via R2T ack's
1609                  *
1610                  *      pad_count       bytes to be sent as zero-padding
1611                  */
1612                 ctask->imm_count = 0;
1613                 ctask->unsol_count = 0;
1614                 ctask->unsol_datasn = 0;
1615                 ctask->xmstate = XMSTATE_W_HDR;
1616                 /* calculate write padding */
1617                 ctask->pad_count = ctask->total_length & (ISCSI_PAD_LEN-1);
1618                 if (ctask->pad_count) {
1619                         ctask->pad_count = ISCSI_PAD_LEN - ctask->pad_count;
1620                         debug_scsi("write padding %d bytes\n",
1621                                 ctask->pad_count);
1622                         ctask->xmstate |= XMSTATE_W_PAD;
1623                 }
1624                 if (session->imm_data_en) {
1625                         if (ctask->total_length >= session->first_burst)
1626                                 ctask->imm_count = min(session->first_burst,
1627                                                         conn->max_xmit_dlength);
1628                         else
1629                                 ctask->imm_count = min(ctask->total_length,
1630                                                         conn->max_xmit_dlength);
1631                         hton24(ctask->hdr.dlength, ctask->imm_count);
1632                         ctask->xmstate |= XMSTATE_IMM_DATA;
1633                 } else
1634                         zero_data(ctask->hdr.dlength);
1635
1636                 if (!session->initial_r2t_en)
1637                         ctask->unsol_count = min(session->first_burst,
1638                                 ctask->total_length) - ctask->imm_count;
1639                 if (!ctask->unsol_count)
1640                         /* No unsolicit Data-Out's */
1641                         ctask->hdr.flags |= ISCSI_FLAG_CMD_FINAL;
1642                 else
1643                         ctask->xmstate |= XMSTATE_UNS_HDR | XMSTATE_UNS_INIT;
1644
1645                 ctask->r2t_data_count = ctask->total_length -
1646                                     ctask->imm_count -
1647                                     ctask->unsol_count;
1648
1649                 debug_scsi("cmd [itt %x total %d imm %d imm_data %d "
1650                            "r2t_data %d]\n",
1651                            ctask->itt, ctask->total_length, ctask->imm_count,
1652                            ctask->unsol_count, ctask->r2t_data_count);
1653         } else {
1654                 ctask->hdr.flags |= ISCSI_FLAG_CMD_FINAL;
1655                 if (sc->sc_data_direction == DMA_FROM_DEVICE)
1656                         ctask->hdr.flags |= ISCSI_FLAG_CMD_READ;
1657                 ctask->datasn = 0;
1658                 ctask->xmstate = XMSTATE_R_HDR;
1659                 zero_data(ctask->hdr.dlength);
1660         }
1661
1662         iscsi_buf_init_virt(&ctask->headbuf, (char*)&ctask->hdr, 
1663                             sizeof(struct iscsi_hdr));
1664         conn->scsicmd_pdus_cnt++;
1665 }
1666
1667 /**
1668  * iscsi_mtask_xmit - xmit management(immediate) task
1669  * @conn: iscsi connection
1670  * @mtask: task management task
1671  *
1672  * Notes:
1673  *      The function can return -EAGAIN in which case caller must
1674  *      call it again later, or recover. '0' return code means successful
1675  *      xmit.
1676  *
1677  *      Management xmit state machine consists of two states:
1678  *              IN_PROGRESS_IMM_HEAD - PDU Header xmit in progress
1679  *              IN_PROGRESS_IMM_DATA - PDU Data xmit in progress
1680  **/
1681 static int
1682 iscsi_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
1683 {
1684
1685         debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
1686                 conn->id, mtask->xmstate, mtask->itt);
1687
1688         if (mtask->xmstate & XMSTATE_IMM_HDR) {
1689                 mtask->xmstate &= ~XMSTATE_IMM_HDR;
1690                 if (mtask->data_count)
1691                         mtask->xmstate |= XMSTATE_IMM_DATA;
1692                 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
1693                     conn->stop_stage != STOP_CONN_RECOVER &&
1694                     conn->hdrdgst_en)
1695                         iscsi_hdr_digest(conn, &mtask->headbuf,
1696                                         (u8*)mtask->hdrext);
1697                 if (iscsi_sendhdr(conn, &mtask->headbuf, mtask->data_count)) {
1698                         mtask->xmstate |= XMSTATE_IMM_HDR;
1699                         if (mtask->data_count)
1700                                 mtask->xmstate &= ~XMSTATE_IMM_DATA;
1701                         return -EAGAIN;
1702                 }
1703         }
1704
1705         if (mtask->xmstate & XMSTATE_IMM_DATA) {
1706                 BUG_ON(!mtask->data_count);
1707                 mtask->xmstate &= ~XMSTATE_IMM_DATA;
1708                 /* FIXME: implement.
1709                  * Virtual buffer could be spreaded across multiple pages...
1710                  */
1711                 do {
1712                         if (iscsi_sendpage(conn, &mtask->sendbuf,
1713                                    &mtask->data_count, &mtask->sent)) {
1714                                 mtask->xmstate |= XMSTATE_IMM_DATA;
1715                                 return -EAGAIN;
1716                         }
1717                 } while (mtask->data_count);
1718         }
1719
1720         BUG_ON(mtask->xmstate != XMSTATE_IDLE);
1721         return 0;
1722 }
1723
1724 static inline int
1725 handle_xmstate_r_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1726 {
1727         ctask->xmstate &= ~XMSTATE_R_HDR;
1728         if (conn->hdrdgst_en) 
1729                 iscsi_hdr_digest(conn, &ctask->headbuf, (u8*)ctask->hdrext);
1730         if (!iscsi_sendhdr(conn, &ctask->headbuf, 0)) {
1731                 BUG_ON(ctask->xmstate != XMSTATE_IDLE);
1732                 return 0; /* wait for Data-In */
1733         }
1734         ctask->xmstate |= XMSTATE_R_HDR;
1735         return -EAGAIN;
1736 }
1737
1738 static inline int
1739 handle_xmstate_w_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1740 {
1741         ctask->xmstate &= ~XMSTATE_W_HDR;
1742         if (conn->hdrdgst_en) 
1743                 iscsi_hdr_digest(conn, &ctask->headbuf, (u8*)ctask->hdrext);
1744         if (iscsi_sendhdr(conn, &ctask->headbuf, ctask->imm_count)) {
1745                 ctask->xmstate |= XMSTATE_W_HDR;
1746                 return -EAGAIN;
1747         }
1748         return 0;
1749 }
1750
1751 static inline int
1752 handle_xmstate_data_digest(struct iscsi_conn *conn,
1753                            struct iscsi_cmd_task *ctask)
1754 {
1755         ctask->xmstate &= ~XMSTATE_DATA_DIGEST;
1756         debug_tcp("resent data digest 0x%x\n", ctask->datadigest);
1757         if (iscsi_digest_final_send(conn, ctask, &ctask->immbuf,
1758                                     &ctask->datadigest, 0)) {
1759                 ctask->xmstate |= XMSTATE_DATA_DIGEST;
1760                 debug_tcp("resent data digest 0x%x fail!\n",
1761                           ctask->datadigest);
1762                 return -EAGAIN;
1763         }
1764         return 0;
1765 }
1766
1767 static inline int
1768 handle_xmstate_imm_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1769 {
1770         BUG_ON(!ctask->imm_count);
1771         ctask->xmstate &= ~XMSTATE_IMM_DATA;
1772
1773         if (conn->datadgst_en) {
1774                 iscsi_data_digest_init(conn, ctask);
1775                 ctask->immdigest = 0;
1776         }
1777
1778         for (;;) {
1779                 if (iscsi_sendpage(conn, &ctask->sendbuf, &ctask->imm_count,
1780                                    &ctask->sent)) {
1781                         ctask->xmstate |= XMSTATE_IMM_DATA;
1782                         if (conn->datadgst_en) {
1783                                 crypto_digest_final(conn->data_tx_tfm,
1784                                                 (u8*)&ctask->immdigest);
1785                                 debug_tcp("tx imm sendpage fail 0x%x\n",
1786                                           ctask->datadigest);
1787                         }
1788                         return -EAGAIN;
1789                 }
1790                 if (conn->datadgst_en)
1791                         iscsi_buf_data_digest_update(conn, &ctask->sendbuf);
1792
1793                 if (!ctask->imm_count)
1794                         break;
1795                 iscsi_buf_init_sg(&ctask->sendbuf,
1796                                   &ctask->sg[ctask->sg_count++]);
1797         }
1798
1799         if (conn->datadgst_en && !(ctask->xmstate & XMSTATE_W_PAD)) {
1800                 if (iscsi_digest_final_send(conn, ctask, &ctask->immbuf,
1801                                             &ctask->immdigest, 1)) {
1802                         debug_tcp("sending imm digest 0x%x fail!\n",
1803                                   ctask->immdigest);
1804                         return -EAGAIN;
1805                 }
1806                 debug_tcp("sending imm digest 0x%x\n", ctask->immdigest);
1807         }
1808
1809         return 0;
1810 }
1811
1812 static inline int
1813 handle_xmstate_uns_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1814 {
1815         struct iscsi_data_task *dtask;
1816
1817         ctask->xmstate |= XMSTATE_UNS_DATA;
1818         if (ctask->xmstate & XMSTATE_UNS_INIT) {
1819                 iscsi_unsolicit_data_init(conn, ctask);
1820                 BUG_ON(!ctask->dtask);
1821                 dtask = ctask->dtask;
1822                 if (conn->hdrdgst_en)
1823                         iscsi_hdr_digest(conn, &ctask->headbuf,
1824                                         (u8*)dtask->hdrext);
1825                 ctask->xmstate &= ~XMSTATE_UNS_INIT;
1826         }
1827         if (iscsi_sendhdr(conn, &ctask->headbuf, ctask->data_count)) {
1828                 ctask->xmstate &= ~XMSTATE_UNS_DATA;
1829                 ctask->xmstate |= XMSTATE_UNS_HDR;
1830                 return -EAGAIN;
1831         }
1832
1833         debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
1834                    ctask->itt, ctask->unsol_count, ctask->sent);
1835         return 0;
1836 }
1837
1838 static inline int
1839 handle_xmstate_uns_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1840 {
1841         struct iscsi_data_task *dtask = ctask->dtask;
1842
1843         BUG_ON(!ctask->data_count);
1844         ctask->xmstate &= ~XMSTATE_UNS_DATA;
1845
1846         if (conn->datadgst_en) {
1847                 iscsi_data_digest_init(conn, ctask);
1848                 dtask->digest = 0;
1849         }
1850
1851         for (;;) {
1852                 int start = ctask->sent;
1853
1854                 if (iscsi_sendpage(conn, &ctask->sendbuf, &ctask->data_count,
1855                                    &ctask->sent)) {
1856                         ctask->unsol_count -= ctask->sent - start;
1857                         ctask->xmstate |= XMSTATE_UNS_DATA;
1858                         /* will continue with this ctask later.. */
1859                         if (conn->datadgst_en) {
1860                                 crypto_digest_final(conn->data_tx_tfm,
1861                                                 (u8 *)&dtask->digest);
1862                                 debug_tcp("tx uns data fail 0x%x\n",
1863                                           dtask->digest);
1864                         }
1865                         return -EAGAIN;
1866                 }
1867
1868                 BUG_ON(ctask->sent > ctask->total_length);
1869                 ctask->unsol_count -= ctask->sent - start;
1870
1871                 /*
1872                  * XXX:we may run here with un-initial sendbuf.
1873                  * so pass it
1874                  */
1875                 if (conn->datadgst_en && ctask->sent - start > 0)
1876                         iscsi_buf_data_digest_update(conn, &ctask->sendbuf);
1877
1878                 if (!ctask->data_count)
1879                         break;
1880                 iscsi_buf_init_sg(&ctask->sendbuf,
1881                                   &ctask->sg[ctask->sg_count++]);
1882         }
1883         BUG_ON(ctask->unsol_count < 0);
1884
1885         /*
1886          * Done with the Data-Out. Next, check if we need
1887          * to send another unsolicited Data-Out.
1888          */
1889         if (ctask->unsol_count) {
1890                 if (conn->datadgst_en) {
1891                         if (iscsi_digest_final_send(conn, ctask,
1892                                                     &dtask->digestbuf,
1893                                                     &dtask->digest, 1)) {
1894                                 debug_tcp("send uns digest 0x%x fail\n",
1895                                           dtask->digest);
1896                                 return -EAGAIN;
1897                         }
1898                         debug_tcp("sending uns digest 0x%x, more uns\n",
1899                                   dtask->digest);
1900                 }
1901                 ctask->xmstate |= XMSTATE_UNS_INIT;
1902                 return 1;
1903         }
1904
1905         if (conn->datadgst_en && !(ctask->xmstate & XMSTATE_W_PAD)) {
1906                 if (iscsi_digest_final_send(conn, ctask,
1907                                             &dtask->digestbuf,
1908                                             &dtask->digest, 1)) {
1909                         debug_tcp("send last uns digest 0x%x fail\n",
1910                                    dtask->digest);
1911                         return -EAGAIN;
1912                 }
1913                 debug_tcp("sending uns digest 0x%x\n",dtask->digest);
1914         }
1915
1916         return 0;
1917 }
1918
1919 static inline int
1920 handle_xmstate_sol_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1921 {
1922         struct iscsi_session *session = conn->session;
1923         struct iscsi_r2t_info *r2t = ctask->r2t;
1924         struct iscsi_data_task *dtask = r2t->dtask;
1925         int left;
1926
1927         ctask->xmstate &= ~XMSTATE_SOL_DATA;
1928         ctask->dtask = dtask;
1929
1930         if (conn->datadgst_en) {
1931                 iscsi_data_digest_init(conn, ctask);
1932                 dtask->digest = 0;
1933         }
1934 solicit_again:
1935         /*
1936          * send Data-Out whitnin this R2T sequence.
1937          */
1938         if (!r2t->data_count)
1939                 goto data_out_done;
1940
1941         if (iscsi_sendpage(conn, &r2t->sendbuf, &r2t->data_count, &r2t->sent)) {
1942                 ctask->xmstate |= XMSTATE_SOL_DATA;
1943                 /* will continue with this ctask later.. */
1944                 if (conn->datadgst_en) {
1945                         crypto_digest_final(conn->data_tx_tfm,
1946                                           (u8 *)&dtask->digest);
1947                         debug_tcp("r2t data send fail 0x%x\n", dtask->digest);
1948                 }
1949                 return -EAGAIN;
1950         }
1951
1952         BUG_ON(r2t->data_count < 0);
1953         if (conn->datadgst_en)
1954                 iscsi_buf_data_digest_update(conn, &r2t->sendbuf);
1955
1956         if (r2t->data_count) {
1957                 BUG_ON(ctask->sc->use_sg == 0);
1958                 if (!iscsi_buf_left(&r2t->sendbuf)) {
1959                         BUG_ON(ctask->bad_sg == r2t->sg);
1960                         iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1961                         r2t->sg += 1;
1962                 }
1963                 goto solicit_again;
1964         }
1965
1966 data_out_done:
1967         /*
1968          * Done with this Data-Out. Next, check if we have
1969          * to send another Data-Out for this R2T.
1970          */
1971         BUG_ON(r2t->data_length - r2t->sent < 0);
1972         left = r2t->data_length - r2t->sent;
1973         if (left) {
1974                 if (conn->datadgst_en) {
1975                         if (iscsi_digest_final_send(conn, ctask,
1976                                                     &dtask->digestbuf,
1977                                                     &dtask->digest, 1)) {
1978                                 debug_tcp("send r2t data digest 0x%x"
1979                                           "fail\n", dtask->digest);
1980                                 return -EAGAIN;
1981                         }
1982                         debug_tcp("r2t data send digest 0x%x\n",
1983                                   dtask->digest);
1984                 }
1985                 iscsi_solicit_data_cont(conn, ctask, r2t, left);
1986                 ctask->xmstate |= XMSTATE_SOL_DATA;
1987                 ctask->xmstate &= ~XMSTATE_SOL_HDR;
1988                 return 1;
1989         }
1990
1991         /*
1992          * Done with this R2T. Check if there are more
1993          * outstanding R2Ts ready to be processed.
1994          */
1995         BUG_ON(ctask->r2t_data_count - r2t->data_length < 0);
1996         if (conn->datadgst_en) {
1997                 if (iscsi_digest_final_send(conn, ctask, &dtask->digestbuf,
1998                                             &dtask->digest, 1)) {
1999                         debug_tcp("send last r2t data digest 0x%x"
2000                                   "fail\n", dtask->digest);
2001                         return -EAGAIN;
2002                 }
2003                 debug_tcp("r2t done dout digest 0x%x\n", dtask->digest);
2004         }
2005
2006         ctask->r2t_data_count -= r2t->data_length;
2007         ctask->r2t = NULL;
2008         spin_lock_bh(&session->lock);
2009         __kfifo_put(ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
2010         spin_unlock_bh(&session->lock);
2011         if (__kfifo_get(ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
2012                 ctask->r2t = r2t;
2013                 ctask->xmstate |= XMSTATE_SOL_DATA;
2014                 ctask->xmstate &= ~XMSTATE_SOL_HDR;
2015                 return 1;
2016         }
2017
2018         return 0;
2019 }
2020
2021 static inline int
2022 handle_xmstate_w_pad(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
2023 {
2024         struct iscsi_data_task *dtask = ctask->dtask;
2025         int sent;
2026
2027         ctask->xmstate &= ~XMSTATE_W_PAD;
2028         iscsi_buf_init_virt(&ctask->sendbuf, (char*)&ctask->pad,
2029                             ctask->pad_count);
2030         if (iscsi_sendpage(conn, &ctask->sendbuf, &ctask->pad_count, &sent)) {
2031                 ctask->xmstate |= XMSTATE_W_PAD;
2032                 return -EAGAIN;
2033         }
2034
2035         if (conn->datadgst_en) {
2036                 iscsi_buf_data_digest_update(conn, &ctask->sendbuf);
2037                 /* imm data? */
2038                 if (!dtask) {
2039                         if (iscsi_digest_final_send(conn, ctask, &ctask->immbuf,
2040                                                     &ctask->immdigest, 1)) {
2041                                 debug_tcp("send padding digest 0x%x"
2042                                           "fail!\n", ctask->immdigest);
2043                                 return -EAGAIN;
2044                         }
2045                         debug_tcp("done with padding, digest 0x%x\n",
2046                                   ctask->datadigest);
2047                 } else {
2048                         if (iscsi_digest_final_send(conn, ctask,
2049                                                     &dtask->digestbuf,
2050                                                     &dtask->digest, 1)) {
2051                                 debug_tcp("send padding digest 0x%x"
2052                                           "fail\n", dtask->digest);
2053                                 return -EAGAIN;
2054                         }
2055                         debug_tcp("done with padding, digest 0x%x\n",
2056                                   dtask->digest);
2057                 }
2058         }
2059
2060         return 0;
2061 }
2062
2063 static int
2064 iscsi_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
2065 {
2066         int rc = 0;
2067
2068         debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
2069                 conn->id, ctask->xmstate, ctask->itt);
2070
2071         /*
2072          * serialize with TMF AbortTask
2073          */
2074         if (ctask->mtask)
2075                 return rc;
2076
2077         if (ctask->xmstate & XMSTATE_R_HDR) {
2078                 rc = handle_xmstate_r_hdr(conn, ctask);
2079                 return rc;
2080         }
2081
2082         if (ctask->xmstate & XMSTATE_W_HDR) {
2083                 rc = handle_xmstate_w_hdr(conn, ctask);
2084                 if (rc)
2085                         return rc;
2086         }
2087
2088         /* XXX: for data digest xmit recover */
2089         if (ctask->xmstate & XMSTATE_DATA_DIGEST) {
2090                 rc = handle_xmstate_data_digest(conn, ctask);
2091                 if (rc)
2092                         return rc;
2093         }
2094
2095         if (ctask->xmstate & XMSTATE_IMM_DATA) {
2096                 rc = handle_xmstate_imm_data(conn, ctask);
2097                 if (rc)
2098                         return rc;
2099         }
2100
2101         if (ctask->xmstate & XMSTATE_UNS_HDR) {
2102                 BUG_ON(!ctask->unsol_count);
2103                 ctask->xmstate &= ~XMSTATE_UNS_HDR;
2104 unsolicit_head_again:
2105                 rc = handle_xmstate_uns_hdr(conn, ctask);
2106                 if (rc)
2107                         return rc;
2108         }
2109
2110         if (ctask->xmstate & XMSTATE_UNS_DATA) {
2111                 rc = handle_xmstate_uns_data(conn, ctask);
2112                 if (rc == 1)
2113                         goto unsolicit_head_again;
2114                 else if (rc)
2115                         return rc;
2116                 goto done;
2117         }
2118
2119         if (ctask->xmstate & XMSTATE_SOL_HDR) {
2120                 struct iscsi_r2t_info *r2t;
2121
2122                 ctask->xmstate &= ~XMSTATE_SOL_HDR;
2123                 ctask->xmstate |= XMSTATE_SOL_DATA;
2124                 if (!ctask->r2t)
2125                         __kfifo_get(ctask->r2tqueue, (void*)&ctask->r2t,
2126                                     sizeof(void*));
2127 solicit_head_again:
2128                 r2t = ctask->r2t;
2129                 if (conn->hdrdgst_en)
2130                         iscsi_hdr_digest(conn, &r2t->headbuf, 
2131                                         (u8*)r2t->dtask->hdrext);
2132                 if (iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count)) {
2133                         ctask->xmstate &= ~XMSTATE_SOL_DATA;
2134                         ctask->xmstate |= XMSTATE_SOL_HDR;
2135                         return -EAGAIN;
2136                 }
2137
2138                 debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n",
2139                         r2t->solicit_datasn - 1, ctask->itt, r2t->data_count,
2140                         r2t->sent);
2141         }
2142
2143         if (ctask->xmstate & XMSTATE_SOL_DATA) {
2144                 rc = handle_xmstate_sol_data(conn, ctask);
2145                 if (rc == 1)
2146                         goto solicit_head_again;
2147                 if (rc)
2148                         return rc;
2149         }
2150
2151 done:
2152         /*
2153          * Last thing to check is whether we need to send write
2154          * padding. Note that we check for xmstate equality, not just the bit.
2155          */
2156         if (ctask->xmstate == XMSTATE_W_PAD)
2157                 rc = handle_xmstate_w_pad(conn, ctask);
2158
2159         return rc;
2160 }
2161
2162 /**
2163  * iscsi_data_xmit - xmit any command into the scheduled connection
2164  * @conn: iscsi connection
2165  *
2166  * Notes:
2167  *      The function can return -EAGAIN in which case the caller must
2168  *      re-schedule it again later or recover. '0' return code means
2169  *      successful xmit.
2170  **/
2171 static int
2172 iscsi_data_xmit(struct iscsi_conn *conn)
2173 {
2174         if (unlikely(conn->suspend_tx)) {
2175                 debug_tcp("conn %d Tx suspended!\n", conn->id);
2176                 return 0;
2177         }
2178
2179         /*
2180          * Transmit in the following order:
2181          *
2182          * 1) un-finished xmit (ctask or mtask)
2183          * 2) immediate control PDUs
2184          * 3) write data
2185          * 4) SCSI commands
2186          * 5) non-immediate control PDUs
2187          *
2188          * No need to lock around __kfifo_get as long as
2189          * there's one producer and one consumer.
2190          */
2191
2192         BUG_ON(conn->ctask && conn->mtask);
2193
2194         if (conn->ctask) {
2195                 if (iscsi_ctask_xmit(conn, conn->ctask))
2196                         goto again;
2197                 /* done with this in-progress ctask */
2198                 conn->ctask = NULL;
2199         }
2200         if (conn->mtask) {
2201                 if (iscsi_mtask_xmit(conn, conn->mtask))
2202                         goto again;
2203                 /* done with this in-progress mtask */
2204                 conn->mtask = NULL;
2205         }
2206
2207         /* process immediate first */
2208         if (unlikely(__kfifo_len(conn->immqueue))) {
2209                 struct iscsi_session *session = conn->session;
2210                 while (__kfifo_get(conn->immqueue, (void*)&conn->mtask,
2211                                    sizeof(void*))) {
2212                         if (iscsi_mtask_xmit(conn, conn->mtask))
2213                                 goto again;
2214
2215                         if (conn->mtask->hdr.itt ==
2216                                         cpu_to_be32(ISCSI_RESERVED_TAG)) {
2217                                 spin_lock_bh(&session->lock);
2218                                 __kfifo_put(session->mgmtpool.queue,
2219                                             (void*)&conn->mtask, sizeof(void*));
2220                                 spin_unlock_bh(&session->lock);
2221                         }
2222                 }
2223                 /* done with this mtask */
2224                 conn->mtask = NULL;
2225         }
2226
2227         /* process write queue */
2228         while (__kfifo_get(conn->writequeue, (void*)&conn->ctask,
2229                            sizeof(void*))) {
2230                 if (iscsi_ctask_xmit(conn, conn->ctask))
2231                         goto again;
2232         }
2233
2234         /* process command queue */
2235         while (__kfifo_get(conn->xmitqueue, (void*)&conn->ctask,
2236                            sizeof(void*))) {
2237                 if (iscsi_ctask_xmit(conn, conn->ctask))
2238                         goto again;
2239         }
2240         /* done with this ctask */
2241         conn->ctask = NULL;
2242
2243         /* process the rest control plane PDUs, if any */
2244         if (unlikely(__kfifo_len(conn->mgmtqueue))) {
2245                 struct iscsi_session *session = conn->session;
2246
2247                 while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
2248                                    sizeof(void*))) {
2249                         if (iscsi_mtask_xmit(conn, conn->mtask))
2250                                 goto again;
2251
2252                         if (conn->mtask->hdr.itt ==
2253                                         cpu_to_be32(ISCSI_RESERVED_TAG)) {
2254                                 spin_lock_bh(&session->lock);
2255                                 __kfifo_put(session->mgmtpool.queue,
2256                                             (void*)&conn->mtask,
2257                                             sizeof(void*));
2258                                 spin_unlock_bh(&session->lock);
2259                         }
2260                 }
2261                 /* done with this mtask */
2262                 conn->mtask = NULL;
2263         }
2264
2265         return 0;
2266
2267 again:
2268         if (unlikely(conn->suspend_tx))
2269                 return 0;
2270
2271         return -EAGAIN;
2272 }
2273
2274 static void
2275 iscsi_xmitworker(void *data)
2276 {
2277         struct iscsi_conn *conn = data;
2278
2279         /*
2280          * serialize Xmit worker on a per-connection basis.
2281          */
2282         down(&conn->xmitsema);
2283         if (iscsi_data_xmit(conn))
2284                 schedule_work(&conn->xmitwork);
2285         up(&conn->xmitsema);
2286 }
2287
2288 #define FAILURE_BAD_HOST                1
2289 #define FAILURE_SESSION_FAILED          2
2290 #define FAILURE_SESSION_FREED           3
2291 #define FAILURE_WINDOW_CLOSED           4
2292 #define FAILURE_SESSION_TERMINATE       5
2293
2294 static int
2295 iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
2296 {
2297         struct Scsi_Host *host;
2298         int reason = 0;
2299         struct iscsi_session *session;
2300         struct iscsi_conn *conn = NULL;
2301         struct iscsi_cmd_task *ctask = NULL;
2302
2303         sc->scsi_done = done;
2304         sc->result = 0;
2305
2306         host = sc->device->host;
2307         session = iscsi_hostdata(host->hostdata);
2308         BUG_ON(host != session->host);
2309
2310         spin_lock(&session->lock);
2311
2312         if (session->state != ISCSI_STATE_LOGGED_IN) {
2313                 if (session->state == ISCSI_STATE_FAILED) {
2314                         reason = FAILURE_SESSION_FAILED;
2315                         goto reject;
2316                 } else if (session->state == ISCSI_STATE_TERMINATE) {
2317                         reason = FAILURE_SESSION_TERMINATE;
2318                         goto fault;
2319                 }
2320                 reason = FAILURE_SESSION_FREED;
2321                 goto fault;
2322         }
2323
2324         /*
2325          * Check for iSCSI window and take care of CmdSN wrap-around
2326          */
2327         if ((int)(session->max_cmdsn - session->cmdsn) < 0) {
2328                 reason = FAILURE_WINDOW_CLOSED;
2329                 goto reject;
2330         }
2331
2332         conn = session->leadconn;
2333
2334         __kfifo_get(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
2335         BUG_ON(ctask->sc);
2336
2337         sc->SCp.phase = session->age;
2338         sc->SCp.ptr = (char*)ctask;
2339         iscsi_cmd_init(conn, ctask, sc);
2340
2341         __kfifo_put(conn->xmitqueue, (void*)&ctask, sizeof(void*));
2342         debug_scsi(
2343                "ctask enq [%s cid %d sc %lx itt 0x%x len %d cmdsn %d win %d]\n",
2344                 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
2345                 conn->id, (long)sc, ctask->itt, sc->request_bufflen,
2346                 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
2347         spin_unlock(&session->lock);
2348
2349         if (!in_interrupt() && !down_trylock(&conn->xmitsema)) {
2350                 spin_unlock_irq(host->host_lock);
2351                 if (iscsi_data_xmit(conn))
2352                         schedule_work(&conn->xmitwork);
2353                 up(&conn->xmitsema);
2354                 spin_lock_irq(host->host_lock);
2355         } else
2356                 schedule_work(&conn->xmitwork);
2357
2358         return 0;
2359
2360 reject:
2361         spin_unlock(&session->lock);
2362         debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
2363         return SCSI_MLQUEUE_HOST_BUSY;
2364
2365 fault:
2366         spin_unlock(&session->lock);
2367         printk(KERN_ERR "iscsi_tcp: cmd 0x%x is not queued (%d)\n",
2368                sc->cmnd[0], reason);
2369         sc->sense_buffer[0] = 0x70;
2370         sc->sense_buffer[2] = NOT_READY;
2371         sc->sense_buffer[7] = 0x6;
2372         sc->sense_buffer[12] = 0x08;
2373         sc->sense_buffer[13] = 0x00;
2374         sc->result = (DID_NO_CONNECT << 16);
2375         sc->resid = sc->request_bufflen;
2376         sc->scsi_done(sc);
2377         return 0;
2378 }
2379
2380 static int
2381 iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
2382 {
2383         int i;
2384
2385         *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
2386         if (*items == NULL)
2387                 return -ENOMEM;
2388
2389         q->max = max;
2390         q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
2391         if (q->pool == NULL) {
2392                 kfree(*items);
2393                 return -ENOMEM;
2394         }
2395
2396         q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
2397                               GFP_KERNEL, NULL);
2398         if (q->queue == ERR_PTR(-ENOMEM)) {
2399                 kfree(q->pool);
2400                 kfree(*items);
2401                 return -ENOMEM;
2402         }
2403
2404         for (i = 0; i < max; i++) {
2405                 q->pool[i] = kmalloc(item_size, GFP_KERNEL);
2406                 if (q->pool[i] == NULL) {
2407                         int j;
2408
2409                         for (j = 0; j < i; j++)
2410                                 kfree(q->pool[j]);
2411
2412                         kfifo_free(q->queue);
2413                         kfree(q->pool);
2414                         kfree(*items);
2415                         return -ENOMEM;
2416                 }
2417                 memset(q->pool[i], 0, item_size);
2418                 (*items)[i] = q->pool[i];
2419                 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
2420         }
2421         return 0;
2422 }
2423
2424 static void
2425 iscsi_pool_free(struct iscsi_queue *q, void **items)
2426 {
2427         int i;
2428
2429         for (i = 0; i < q->max; i++)
2430                 kfree(items[i]);
2431         kfree(q->pool);
2432         kfree(items);
2433 }
2434
2435 static iscsi_connh_t
2436 iscsi_conn_create(iscsi_sessionh_t sessionh, uint32_t conn_idx)
2437 {
2438         struct iscsi_session *session = iscsi_ptr(sessionh);
2439         struct iscsi_conn *conn = NULL;
2440
2441         conn = kmalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
2442         if (conn == NULL)
2443                 goto conn_alloc_fail;
2444         memset(conn, 0, sizeof(struct iscsi_conn));
2445
2446         conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2447         conn->in_progress = IN_PROGRESS_WAIT_HEADER;
2448         conn->id = conn_idx;
2449         conn->exp_statsn = 0;
2450         conn->tmabort_state = TMABORT_INITIAL;
2451
2452         /* initial operational parameters */
2453         conn->hdr_size = sizeof(struct iscsi_hdr);
2454         conn->data_size = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
2455         conn->max_recv_dlength = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
2456
2457         spin_lock_init(&conn->lock);
2458
2459         /* initialize general xmit PDU commands queue */
2460         conn->xmitqueue = kfifo_alloc(session->cmds_max * sizeof(void*),
2461                                         GFP_KERNEL, NULL);
2462         if (conn->xmitqueue == ERR_PTR(-ENOMEM))
2463                 goto xmitqueue_alloc_fail;
2464
2465         /* initialize write response PDU commands queue */
2466         conn->writequeue = kfifo_alloc(session->cmds_max * sizeof(void*),
2467                                         GFP_KERNEL, NULL);
2468         if (conn->writequeue == ERR_PTR(-ENOMEM))
2469                 goto writequeue_alloc_fail;
2470
2471         /* initialize general immediate & non-immediate PDU commands queue */
2472         conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
2473                                         GFP_KERNEL, NULL);
2474         if (conn->immqueue == ERR_PTR(-ENOMEM))
2475                 goto immqueue_alloc_fail;
2476
2477         conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
2478                                         GFP_KERNEL, NULL);
2479         if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
2480                 goto mgmtqueue_alloc_fail;
2481
2482         INIT_WORK(&conn->xmitwork, iscsi_xmitworker, conn);
2483
2484         /* allocate login_mtask used for the login/text sequences */
2485         spin_lock_bh(&session->lock);
2486         if (!__kfifo_get(session->mgmtpool.queue,
2487                          (void*)&conn->login_mtask,
2488                          sizeof(void*))) {
2489                 spin_unlock_bh(&session->lock);
2490                 goto login_mtask_alloc_fail;
2491         }
2492         spin_unlock_bh(&session->lock);
2493
2494         /* allocate initial PDU receive place holder */
2495         if (conn->data_size <= PAGE_SIZE)
2496                 conn->data = kmalloc(conn->data_size, GFP_KERNEL);
2497         else
2498                 conn->data = (void*)__get_free_pages(GFP_KERNEL,
2499                                         get_order(conn->data_size));
2500         if (!conn->data)
2501                 goto max_recv_dlenght_alloc_fail;
2502
2503         init_timer(&conn->tmabort_timer);
2504         init_MUTEX(&conn->xmitsema);
2505         init_waitqueue_head(&conn->ehwait);
2506
2507         return iscsi_handle(conn);
2508
2509 max_recv_dlenght_alloc_fail:
2510         spin_lock_bh(&session->lock);
2511         __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
2512                     sizeof(void*));
2513         spin_unlock_bh(&session->lock);
2514 login_mtask_alloc_fail:
2515         kfifo_free(conn->mgmtqueue);
2516 mgmtqueue_alloc_fail:
2517         kfifo_free(conn->immqueue);
2518 immqueue_alloc_fail:
2519         kfifo_free(conn->writequeue);
2520 writequeue_alloc_fail:
2521         kfifo_free(conn->xmitqueue);
2522 xmitqueue_alloc_fail:
2523         kfree(conn);
2524 conn_alloc_fail:
2525         return iscsi_handle(NULL);
2526 }
2527
2528 static void
2529 iscsi_conn_destroy(iscsi_connh_t connh)
2530 {
2531         struct iscsi_conn *conn = iscsi_ptr(connh);
2532         struct iscsi_session *session = conn->session;
2533
2534         down(&conn->xmitsema);
2535         set_bit(SUSPEND_BIT, &conn->suspend_tx);
2536         if (conn->c_stage == ISCSI_CONN_INITIAL_STAGE && conn->sock) {
2537                 struct sock *sk = conn->sock->sk;
2538
2539                 /*
2540                  * conn_start() has never been called!
2541                  * need to cleanup the socket.
2542                  */
2543                 write_lock_bh(&sk->sk_callback_lock);
2544                 set_bit(SUSPEND_BIT, &conn->suspend_rx);
2545                 write_unlock_bh(&sk->sk_callback_lock);
2546
2547                 sock_hold(conn->sock->sk);
2548                 iscsi_conn_restore_callbacks(conn);
2549                 sock_put(conn->sock->sk);
2550                 sock_release(conn->sock);
2551                 conn->sock = NULL;
2552         }
2553
2554         spin_lock_bh(&session->lock);
2555         conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2556         if (session->leadconn == conn) {
2557                 /*
2558                  * leading connection? then give up on recovery.
2559                  */
2560                 session->state = ISCSI_STATE_TERMINATE;
2561                 wake_up(&conn->ehwait);
2562         }
2563         spin_unlock_bh(&session->lock);
2564
2565         up(&conn->xmitsema);
2566
2567         /*
2568          * Block until all in-progress commands for this connection
2569          * time out or fail.
2570          */
2571         for (;;) {
2572                 spin_lock_bh(&conn->lock);
2573                 if (!session->host->host_busy) { /* OK for ERL == 0 */
2574                         spin_unlock_bh(&conn->lock);
2575                         break;
2576                 }
2577                 spin_unlock_bh(&conn->lock);
2578                 msleep_interruptible(500);
2579                 printk("conn_destroy(): host_busy %d host_failed %d\n",
2580                         session->host->host_busy, session->host->host_failed);
2581                 /*
2582                  * force eh_abort() to unblock
2583                  */
2584                 wake_up(&conn->ehwait);
2585         }
2586
2587         /* now free crypto */
2588         if (conn->hdrdgst_en || conn->datadgst_en) {
2589                 if (conn->tx_tfm)
2590                         crypto_free_tfm(conn->tx_tfm);
2591                 if (conn->rx_tfm)
2592                         crypto_free_tfm(conn->rx_tfm);
2593                 if (conn->data_tx_tfm)
2594                         crypto_free_tfm(conn->data_tx_tfm);
2595                 if (conn->data_rx_tfm)
2596                         crypto_free_tfm(conn->data_rx_tfm);
2597         }
2598
2599         /* free conn->data, size = MaxRecvDataSegmentLength */
2600         if (conn->data_size <= PAGE_SIZE)
2601                 kfree(conn->data);
2602         else
2603                 free_pages((unsigned long)conn->data,
2604                                         get_order(conn->data_size));
2605
2606         spin_lock_bh(&session->lock);
2607         __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
2608                     sizeof(void*));
2609         list_del(&conn->item);
2610         if (list_empty(&session->connections))
2611                 session->leadconn = NULL;
2612         if (session->leadconn && session->leadconn == conn)
2613                 session->leadconn = container_of(session->connections.next,
2614                         struct iscsi_conn, item);
2615
2616         if (session->leadconn == NULL)
2617                 /* none connections exits.. reset sequencing */
2618                 session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
2619         spin_unlock_bh(&session->lock);
2620
2621         kfifo_free(conn->xmitqueue);
2622         kfifo_free(conn->writequeue);
2623         kfifo_free(conn->immqueue);
2624         kfifo_free(conn->mgmtqueue);
2625         kfree(conn);
2626 }
2627
2628 static int
2629 iscsi_conn_bind(iscsi_sessionh_t sessionh, iscsi_connh_t connh,
2630                 uint32_t transport_fd, int is_leading)
2631 {
2632         struct iscsi_session *session = iscsi_ptr(sessionh);
2633         struct iscsi_conn *tmp = ERR_PTR(-EEXIST), *conn = iscsi_ptr(connh);
2634         struct sock *sk;
2635         struct socket *sock;
2636         int err;
2637
2638         /* lookup for existing socket */
2639         sock = sockfd_lookup(transport_fd, &err);
2640         if (!sock) {
2641                 printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
2642                 return -EEXIST;
2643         }
2644
2645         /* lookup for existing connection */
2646         spin_lock_bh(&session->lock);
2647         list_for_each_entry(tmp, &session->connections, item) {
2648                 if (tmp == conn) {
2649                         if (conn->c_stage != ISCSI_CONN_STOPPED ||
2650                             conn->stop_stage == STOP_CONN_TERM) {
2651                                 printk(KERN_ERR "iscsi_tcp: can't bind "
2652                                        "non-stopped connection (%d:%d)\n",
2653                                        conn->c_stage, conn->stop_stage);
2654                                 spin_unlock_bh(&session->lock);
2655                                 return -EIO;
2656                         }
2657                         break;
2658                 }
2659         }
2660         if (tmp != conn) {
2661                 /* bind new iSCSI connection to session */
2662                 conn->session = session;
2663
2664                 list_add(&conn->item, &session->connections);
2665         }
2666         spin_unlock_bh(&session->lock);
2667
2668         if (conn->stop_stage != STOP_CONN_SUSPEND) {
2669                 /* bind iSCSI connection and socket */
2670                 conn->sock = sock;
2671
2672                 /* setup Socket parameters */
2673                 sk = sock->sk;
2674                 sk->sk_reuse = 1;
2675                 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
2676                 sk->sk_allocation = GFP_ATOMIC;
2677
2678                 /* FIXME: disable Nagle's algorithm */
2679
2680                 /*
2681                  * Intercept TCP callbacks for sendfile like receive
2682                  * processing.
2683                  */
2684                 iscsi_conn_set_callbacks(conn);
2685
2686                 /*
2687                  * set receive state machine into initial state
2688                  */
2689                 conn->in_progress = IN_PROGRESS_WAIT_HEADER;
2690         }
2691
2692         if (is_leading)
2693                 session->leadconn = conn;
2694
2695         /*
2696          * Unblock xmitworker(), Login Phase will pass through.
2697          */
2698         clear_bit(SUSPEND_BIT, &conn->suspend_rx);
2699         clear_bit(SUSPEND_BIT, &conn->suspend_tx);
2700
2701         return 0;
2702 }
2703
2704 static int
2705 iscsi_conn_start(iscsi_connh_t connh)
2706 {
2707         struct iscsi_conn *conn = iscsi_ptr(connh);
2708         struct iscsi_session *session = conn->session;
2709         struct sock *sk;
2710
2711         /* FF phase warming up... */
2712
2713         if (session == NULL) {
2714                 printk(KERN_ERR "iscsi_tcp: can't start unbound connection\n");
2715                 return -EPERM;
2716         }
2717
2718         sk = conn->sock->sk;
2719
2720         write_lock_bh(&sk->sk_callback_lock);
2721         spin_lock_bh(&session->lock);
2722         conn->c_stage = ISCSI_CONN_STARTED;
2723         session->state = ISCSI_STATE_LOGGED_IN;
2724
2725         switch(conn->stop_stage) {
2726         case STOP_CONN_RECOVER:
2727                 /*
2728                  * unblock eh_abort() if it is blocked. re-try all
2729                  * commands after successful recovery
2730                  */
2731                 session->conn_cnt++;
2732                 conn->stop_stage = 0;
2733                 conn->tmabort_state = TMABORT_INITIAL;
2734                 session->age++;
2735                 wake_up(&conn->ehwait);
2736                 break;
2737         case STOP_CONN_TERM:
2738                 session->conn_cnt++;
2739                 conn->stop_stage = 0;
2740                 break;
2741         case STOP_CONN_SUSPEND:
2742                 conn->stop_stage = 0;
2743                 clear_bit(SUSPEND_BIT, &conn->suspend_rx);
2744                 clear_bit(SUSPEND_BIT, &conn->suspend_tx);
2745                 break;
2746         default:
2747                 break;
2748         }
2749         spin_unlock_bh(&session->lock);
2750         write_unlock_bh(&sk->sk_callback_lock);
2751
2752         return 0;
2753 }
2754
2755 static void
2756 iscsi_conn_stop(iscsi_connh_t connh, int flag)
2757 {
2758         struct iscsi_conn *conn = iscsi_ptr(connh);
2759         struct iscsi_session *session = conn->session;
2760         struct sock *sk;
2761         unsigned long flags;
2762
2763         BUG_ON(!conn->sock);
2764         sk = conn->sock->sk;
2765         write_lock_bh(&sk->sk_callback_lock);
2766         set_bit(SUSPEND_BIT, &conn->suspend_rx);
2767         write_unlock_bh(&sk->sk_callback_lock);
2768
2769         down(&conn->xmitsema);
2770
2771         spin_lock_irqsave(session->host->host_lock, flags);
2772         spin_lock(&session->lock);
2773         conn->stop_stage = flag;
2774         conn->c_stage = ISCSI_CONN_STOPPED;
2775         set_bit(SUSPEND_BIT, &conn->suspend_tx);
2776
2777         if (flag != STOP_CONN_SUSPEND)
2778                 session->conn_cnt--;
2779
2780         if (session->conn_cnt == 0 || session->leadconn == conn)
2781                 session->state = ISCSI_STATE_FAILED;
2782
2783         spin_unlock(&session->lock);
2784         spin_unlock_irqrestore(session->host->host_lock, flags);
2785
2786         if (flag == STOP_CONN_TERM || flag == STOP_CONN_RECOVER) {
2787                 struct iscsi_cmd_task *ctask;
2788                 struct iscsi_mgmt_task *mtask;
2789
2790                 /*
2791                  * Socket must go now.
2792                  */
2793                 sock_hold(conn->sock->sk);
2794                 iscsi_conn_restore_callbacks(conn);
2795                 sock_put(conn->sock->sk);
2796
2797                 /*
2798                  * flush xmit queues.
2799                  */
2800                 spin_lock_bh(&session->lock);
2801                 while (__kfifo_get(conn->writequeue, (void*)&ctask,
2802                             sizeof(void*)) ||
2803                         __kfifo_get(conn->xmitqueue, (void*)&ctask,
2804                             sizeof(void*))) {
2805                         struct iscsi_r2t_info *r2t;
2806
2807                         /*
2808                          * flush ctask's r2t queues
2809                          */
2810                         while (__kfifo_get(ctask->r2tqueue, (void*)&r2t,
2811                                 sizeof(void*)))
2812                                 __kfifo_put(ctask->r2tpool.queue, (void*)&r2t,
2813                                             sizeof(void*));
2814
2815                         spin_unlock_bh(&session->lock);
2816                         local_bh_disable();
2817                         iscsi_ctask_cleanup(conn, ctask);
2818                         local_bh_enable();
2819                         spin_lock_bh(&session->lock);
2820                 }
2821                 conn->ctask = NULL;
2822                 while (__kfifo_get(conn->immqueue, (void*)&mtask,
2823                            sizeof(void*)) ||
2824                         __kfifo_get(conn->mgmtqueue, (void*)&mtask,
2825                            sizeof(void*))) {
2826                         __kfifo_put(session->mgmtpool.queue,
2827                                     (void*)&mtask, sizeof(void*));
2828                 }
2829                 conn->mtask = NULL;
2830                 spin_unlock_bh(&session->lock);
2831
2832                 /*
2833                  * release socket only after we stopped data_xmit()
2834                  * activity and flushed all outstandings
2835                  */
2836                 sock_release(conn->sock);
2837                 conn->sock = NULL;
2838
2839                 /*
2840                  * for connection level recovery we should not calculate
2841                  * header digest. conn->hdr_size used for optimization
2842                  * in hdr_extract() and will be re-negotiated at
2843                  * set_param() time.
2844                  */
2845                 if (flag == STOP_CONN_RECOVER)
2846                         conn->hdr_size = sizeof(struct iscsi_hdr);
2847         }
2848         up(&conn->xmitsema);
2849 }
2850
2851 static int
2852 iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
2853                         char *data, uint32_t data_size)
2854 {
2855         struct iscsi_session *session = conn->session;
2856         struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
2857         struct iscsi_mgmt_task *mtask;
2858
2859         spin_lock_bh(&session->lock);
2860         if (session->state == ISCSI_STATE_TERMINATE) {
2861                 spin_unlock_bh(&session->lock);
2862                 return -EPERM;
2863         }
2864         if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
2865             hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
2866                 /*
2867                  * Login and Text are sent serially, in
2868                  * request-followed-by-response sequence.
2869                  * Same mtask can be used. Same ITT must be used.
2870                  * Note that login_mtask is preallocated at conn_create().
2871                  */
2872                 mtask = conn->login_mtask;
2873         else {
2874                 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
2875                 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
2876
2877                 if (!__kfifo_get(session->mgmtpool.queue,
2878                                  (void*)&mtask, sizeof(void*))) {
2879                         spin_unlock_bh(&session->lock);
2880                         return -ENOSPC;
2881                 }
2882         }
2883
2884         /*
2885          * pre-format CmdSN and ExpStatSN for outgoing PDU.
2886          */
2887         if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
2888                 hdr->itt = mtask->itt | (conn->id << CID_SHIFT) |
2889                            (session->age << AGE_SHIFT);
2890                 nop->cmdsn = cpu_to_be32(session->cmdsn);
2891                 if (conn->c_stage == ISCSI_CONN_STARTED &&
2892                     !(hdr->opcode & ISCSI_OP_IMMEDIATE))
2893                         session->cmdsn++;
2894         } else
2895                 /* do not advance CmdSN */
2896                 nop->cmdsn = cpu_to_be32(session->cmdsn);
2897
2898         nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
2899
2900         memcpy(&mtask->hdr, hdr, sizeof(struct iscsi_hdr));
2901
2902         iscsi_buf_init_virt(&mtask->headbuf, (char*)&mtask->hdr,
2903                                     sizeof(struct iscsi_hdr));
2904
2905         spin_unlock_bh(&session->lock);
2906
2907         if (data_size) {
2908                 memcpy(mtask->data, data, data_size);
2909                 mtask->data_count = data_size;
2910         } else
2911                 mtask->data_count = 0;
2912
2913         mtask->xmstate = XMSTATE_IMM_HDR;
2914
2915         if (mtask->data_count) {
2916                 iscsi_buf_init_iov(&mtask->sendbuf, (char*)mtask->data,
2917                                     mtask->data_count);
2918         }
2919
2920         debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
2921                    hdr->opcode, hdr->itt, data_size);
2922
2923         /*
2924          * since send_pdu() could be called at least from two contexts,
2925          * we need to serialize __kfifo_put, so we don't have to take
2926          * additional lock on fast data-path
2927          */
2928         if (hdr->opcode & ISCSI_OP_IMMEDIATE)
2929                 __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*));
2930         else
2931                 __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
2932
2933         schedule_work(&conn->xmitwork);
2934
2935         return 0;
2936 }
2937
2938 static int
2939 iscsi_eh_host_reset(struct scsi_cmnd *sc)
2940 {
2941         struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
2942         struct iscsi_conn *conn = ctask->conn;
2943         struct iscsi_session *session = conn->session;
2944
2945         spin_lock_bh(&session->lock);
2946         if (session->state == ISCSI_STATE_TERMINATE) {
2947                 debug_scsi("failing host reset: session terminated "
2948                            "[CID %d age %d]", conn->id, session->age);
2949                 spin_unlock_bh(&session->lock);
2950                 return FAILED;
2951         }
2952         spin_unlock_bh(&session->lock);
2953
2954         debug_scsi("failing connection CID %d due to SCSI host reset "
2955                    "[itt 0x%x age %d]", conn->id, ctask->itt,
2956                    session->age);
2957         iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2958
2959         return SUCCESS;
2960 }
2961
2962 static void
2963 iscsi_tmabort_timedout(unsigned long data)
2964 {
2965         struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
2966         struct iscsi_conn *conn = ctask->conn;
2967         struct iscsi_session *session = conn->session;
2968
2969         spin_lock(&session->lock);
2970         if (conn->tmabort_state == TMABORT_INITIAL) {
2971                 __kfifo_put(session->mgmtpool.queue,
2972                                 (void*)&ctask->mtask, sizeof(void*));
2973                 conn->tmabort_state = TMABORT_TIMEDOUT;
2974                 debug_scsi("tmabort timedout [sc %lx itt 0x%x]\n",
2975                         (long)ctask->sc, ctask->itt);
2976                 /* unblock eh_abort() */
2977                 wake_up(&conn->ehwait);
2978         }
2979         spin_unlock(&session->lock);
2980 }
2981
2982 static int
2983 iscsi_eh_abort(struct scsi_cmnd *sc)
2984 {
2985         int rc;
2986         struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
2987         struct iscsi_conn *conn = ctask->conn;
2988         struct iscsi_session *session = conn->session;
2989
2990         conn->eh_abort_cnt++;
2991         debug_scsi("aborting [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
2992
2993         /*
2994          * two cases for ERL=0 here:
2995          *
2996          * 1) connection-level failure;
2997          * 2) recovery due protocol error;
2998          */
2999         down(&conn->xmitsema);
3000         spin_lock_bh(&session->lock);
3001         if (session->state != ISCSI_STATE_LOGGED_IN) {
3002                 if (session->state == ISCSI_STATE_TERMINATE) {
3003                         spin_unlock_bh(&session->lock);
3004                         up(&conn->xmitsema);
3005                         goto failed;
3006                 }
3007                 spin_unlock_bh(&session->lock);
3008         } else {
3009                 struct iscsi_tm *hdr = &conn->tmhdr;
3010
3011                 /*
3012                  * Still LOGGED_IN...
3013                  */
3014
3015                 if (!ctask->sc || sc->SCp.phase != session->age) {
3016                         /*
3017                          * 1) ctask completed before time out. But session
3018                          *    is still ok => Happy Retry.
3019                          * 2) session was re-open during time out of ctask.
3020                          */
3021                         spin_unlock_bh(&session->lock);
3022                         up(&conn->xmitsema);
3023                         goto success;
3024                 }
3025                 conn->tmabort_state = TMABORT_INITIAL;
3026                 spin_unlock_bh(&session->lock);
3027
3028                 /*
3029                  * ctask timed out but session is OK
3030                  * ERL=0 requires task mgmt abort to be issued on each
3031                  * failed command. requests must be serialized.
3032                  */
3033                 memset(hdr, 0, sizeof(struct iscsi_tm));
3034                 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
3035                 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
3036                 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3037                 memcpy(hdr->lun, ctask->hdr.lun, sizeof(hdr->lun));
3038                 hdr->rtt = ctask->hdr.itt;
3039                 hdr->refcmdsn = ctask->hdr.cmdsn;
3040
3041                 rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr,
3042                                              NULL, 0);
3043                 if (rc) {
3044                         iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
3045                         debug_scsi("abort sent failure [itt 0x%x]", ctask->itt);
3046                 } else {
3047                         struct iscsi_r2t_info *r2t;
3048
3049                         /*
3050                          * TMF abort vs. TMF response race logic
3051                          */
3052                         spin_lock_bh(&session->lock);
3053                         ctask->mtask = (struct iscsi_mgmt_task *)
3054                                 session->mgmt_cmds[(hdr->itt & ITT_MASK) -
3055                                                         ISCSI_MGMT_ITT_OFFSET];
3056                         /*
3057                          * have to flush r2tqueue to avoid r2t leaks
3058                          */
3059                         while (__kfifo_get(ctask->r2tqueue, (void*)&r2t,
3060                                 sizeof(void*))) {
3061                                 __kfifo_put(ctask->r2tpool.queue, (void*)&r2t,
3062                                         sizeof(void*));
3063                         }
3064                         if (conn->tmabort_state == TMABORT_INITIAL) {
3065                                 conn->tmfcmd_pdus_cnt++;
3066                                 conn->tmabort_timer.expires = 3*HZ + jiffies;
3067                                 conn->tmabort_timer.function =
3068                                                 iscsi_tmabort_timedout;
3069                                 conn->tmabort_timer.data = (unsigned long)ctask;
3070                                 add_timer(&conn->tmabort_timer);
3071                                 debug_scsi("abort sent [itt 0x%x]", ctask->itt);
3072                         } else {
3073                                 if (!ctask->sc ||
3074                                     conn->tmabort_state == TMABORT_SUCCESS) {
3075                                         conn->tmabort_state = TMABORT_INITIAL;
3076                                         spin_unlock_bh(&session->lock);
3077                                         up(&conn->xmitsema);
3078                                         goto success;
3079                                 }
3080                                 conn->tmabort_state = TMABORT_INITIAL;
3081                                 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
3082                         }
3083                         spin_unlock_bh(&session->lock);
3084                 }
3085         }
3086         up(&conn->xmitsema);
3087
3088
3089         /*
3090          * block eh thread until:
3091          *
3092          * 1) abort response;
3093          * 2) abort timeout;
3094          * 3) session re-opened;
3095          * 4) session terminated;
3096          */
3097         for (;;) {
3098                 int p_state = session->state;
3099
3100                 rc = wait_event_interruptible(conn->ehwait,
3101                         (p_state == ISCSI_STATE_LOGGED_IN ?
3102                          (session->state == ISCSI_STATE_TERMINATE ||
3103                           conn->tmabort_state != TMABORT_INITIAL) :
3104                          (session->state == ISCSI_STATE_TERMINATE ||
3105                           session->state == ISCSI_STATE_LOGGED_IN)));
3106                 if (rc) {
3107                         /* shutdown.. */
3108                         session->state = ISCSI_STATE_TERMINATE;
3109                         goto failed;
3110                 }
3111
3112                 if (signal_pending(current))
3113                         flush_signals(current);
3114
3115                 if (session->state == ISCSI_STATE_TERMINATE)
3116                         goto failed;
3117
3118                 spin_lock_bh(&session->lock);
3119                 if (sc->SCp.phase == session->age &&
3120                    (conn->tmabort_state == TMABORT_TIMEDOUT ||
3121                     conn->tmabort_state == TMABORT_FAILED)) {
3122                         conn->tmabort_state = TMABORT_INITIAL;
3123                         if (!ctask->sc) {
3124                                 /*
3125                                  * ctask completed before tmf abort response or
3126                                  * time out.
3127                                  * But session is still ok => Happy Retry.
3128                                  */
3129                                 spin_unlock_bh(&session->lock);
3130                                 break;
3131                         }
3132                         spin_unlock_bh(&session->lock);
3133                         iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
3134                         continue;
3135                 }
3136                 spin_unlock_bh(&session->lock);
3137                 break;
3138         }
3139
3140 success:
3141         debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
3142         rc = SUCCESS;
3143         goto exit;
3144
3145 failed:
3146         debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
3147         rc = FAILED;
3148
3149 exit:
3150         del_timer_sync(&conn->tmabort_timer);
3151
3152         down(&conn->xmitsema);
3153         if (conn->sock) {
3154                 struct sock *sk = conn->sock->sk;
3155
3156                 write_lock_bh(&sk->sk_callback_lock);
3157                 iscsi_ctask_cleanup(conn, ctask);
3158                 write_unlock_bh(&sk->sk_callback_lock);
3159         }
3160         up(&conn->xmitsema);
3161         return rc;
3162 }
3163
3164 static int
3165 iscsi_r2tpool_alloc(struct iscsi_session *session)
3166 {
3167         int i;
3168         int cmd_i;
3169
3170         /*
3171          * initialize per-task: R2T pool and xmit queue
3172          */
3173         for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
3174                 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
3175
3176                 /*
3177                  * pre-allocated x4 as much r2ts to handle race when
3178                  * target acks DataOut faster than we data_xmit() queues
3179                  * could replenish r2tqueue.
3180                  */
3181
3182                 /* R2T pool */
3183                 if (iscsi_pool_init(&ctask->r2tpool, session->max_r2t * 4,
3184                         (void***)&ctask->r2ts, sizeof(struct iscsi_r2t_info))) {
3185                         goto r2t_alloc_fail;
3186                 }
3187
3188                 /* R2T xmit queue */
3189                 ctask->r2tqueue = kfifo_alloc(
3190                       session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
3191                 if (ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
3192                         iscsi_pool_free(&ctask->r2tpool, (void**)ctask->r2ts);
3193                         goto r2t_alloc_fail;
3194                 }
3195
3196                 /*
3197                  * number of
3198                  * Data-Out PDU's within R2T-sequence can be quite big;
3199                  * using mempool
3200                  */
3201                 ctask->datapool = mempool_create(ISCSI_DTASK_DEFAULT_MAX,
3202                          mempool_alloc_slab, mempool_free_slab, taskcache);
3203                 if (ctask->datapool == NULL) {
3204                         kfifo_free(ctask->r2tqueue);
3205                         iscsi_pool_free(&ctask->r2tpool, (void**)ctask->r2ts);
3206                         goto r2t_alloc_fail;
3207                 }
3208                 INIT_LIST_HEAD(&ctask->dataqueue);
3209         }
3210
3211         return 0;
3212
3213 r2t_alloc_fail:
3214         for (i = 0; i < cmd_i; i++) {
3215                 mempool_destroy(session->cmds[i]->datapool);
3216                 kfifo_free(session->cmds[i]->r2tqueue);
3217                 iscsi_pool_free(&session->cmds[i]->r2tpool,
3218                                 (void**)session->cmds[i]->r2ts);
3219         }
3220         return -ENOMEM;
3221 }
3222
3223 static void
3224 iscsi_r2tpool_free(struct iscsi_session *session)
3225 {
3226         int i;
3227
3228         for (i = 0; i < session->cmds_max; i++) {
3229                 mempool_destroy(session->cmds[i]->datapool);
3230                 kfifo_free(session->cmds[i]->r2tqueue);
3231                 iscsi_pool_free(&session->cmds[i]->r2tpool,
3232                                 (void**)session->cmds[i]->r2ts);
3233         }
3234 }
3235
3236 static struct scsi_host_template iscsi_sht = {
3237         .name                   = "iSCSI Initiator over TCP/IP, v."
3238                                   ISCSI_VERSION_STR,
3239         .queuecommand           = iscsi_queuecommand,
3240         .can_queue              = ISCSI_XMIT_CMDS_MAX - 1,
3241         .sg_tablesize           = ISCSI_SG_TABLESIZE,
3242         .cmd_per_lun            = ISCSI_CMD_PER_LUN,
3243         .eh_abort_handler       = iscsi_eh_abort,
3244         .eh_host_reset_handler  = iscsi_eh_host_reset,
3245         .use_clustering         = DISABLE_CLUSTERING,
3246         .proc_name              = "iscsi_tcp",
3247         .this_id                = -1,
3248 };
3249
3250 static iscsi_sessionh_t
3251 iscsi_session_create(uint32_t initial_cmdsn, struct Scsi_Host *host)
3252 {
3253         int cmd_i;
3254         struct iscsi_session *session;
3255
3256         session = iscsi_hostdata(host->hostdata);
3257         memset(session, 0, sizeof(struct iscsi_session));
3258
3259         session->host = host;
3260         session->id = host->host_no;
3261         session->state = ISCSI_STATE_LOGGED_IN;
3262         session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
3263         session->cmds_max = ISCSI_XMIT_CMDS_MAX;
3264         session->cmdsn = initial_cmdsn;
3265         session->exp_cmdsn = initial_cmdsn + 1;
3266         session->max_cmdsn = initial_cmdsn + 1;
3267         session->max_r2t = 1;
3268
3269         /* initialize SCSI PDU commands pool */
3270         if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
3271                 (void***)&session->cmds, sizeof(struct iscsi_cmd_task)))
3272                 goto cmdpool_alloc_fail;
3273
3274         /* pre-format cmds pool with ITT */
3275         for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++)
3276                 session->cmds[cmd_i]->itt = cmd_i;
3277
3278         spin_lock_init(&session->lock);
3279         INIT_LIST_HEAD(&session->connections);
3280
3281         /* initialize immediate command pool */
3282         if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
3283                 (void***)&session->mgmt_cmds, sizeof(struct iscsi_mgmt_task)))
3284                 goto mgmtpool_alloc_fail;
3285
3286
3287         /* pre-format immediate cmds pool with ITT */
3288         for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
3289                 session->mgmt_cmds[cmd_i]->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
3290                 session->mgmt_cmds[cmd_i]->data = kmalloc(
3291                         DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, GFP_KERNEL);
3292                 if (!session->mgmt_cmds[cmd_i]->data) {
3293                         int j;
3294
3295                         for (j = 0; j < cmd_i; j++)
3296                                 kfree(session->mgmt_cmds[j]->data);
3297                         goto immdata_alloc_fail;
3298                 }
3299         }
3300
3301         if (iscsi_r2tpool_alloc(session))
3302                 goto r2tpool_alloc_fail;
3303
3304         return iscsi_handle(session);
3305
3306 r2tpool_alloc_fail:
3307         for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++)
3308                 kfree(session->mgmt_cmds[cmd_i]->data);
3309         iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
3310 immdata_alloc_fail:
3311 mgmtpool_alloc_fail:
3312         iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
3313 cmdpool_alloc_fail:
3314         return iscsi_handle(NULL);
3315 }
3316
3317 static void
3318 iscsi_session_destroy(iscsi_sessionh_t sessionh)
3319 {
3320         int cmd_i;
3321         struct iscsi_data_task *dtask, *n;
3322         struct iscsi_session *session = iscsi_ptr(sessionh);
3323
3324         for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
3325                 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
3326                 list_for_each_entry_safe(dtask, n, &ctask->dataqueue, item) {
3327                         list_del(&dtask->item);
3328                         mempool_free(dtask, ctask->datapool);
3329                 }
3330         }
3331
3332         for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++)
3333                 kfree(session->mgmt_cmds[cmd_i]->data);
3334
3335         iscsi_r2tpool_free(session);
3336         iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
3337         iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
3338 }
3339
3340 static int
3341 iscsi_conn_set_param(iscsi_connh_t connh, enum iscsi_param param,
3342                      uint32_t value)
3343 {
3344         struct iscsi_conn *conn = iscsi_ptr(connh);
3345         struct iscsi_session *session = conn->session;
3346
3347         spin_lock_bh(&session->lock);
3348         if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
3349             conn->stop_stage != STOP_CONN_RECOVER) {
3350                 printk(KERN_ERR "iscsi_tcp: can not change parameter [%d]\n",
3351                        param);
3352                 spin_unlock_bh(&session->lock);
3353                 return 0;
3354         }
3355         spin_unlock_bh(&session->lock);
3356
3357         switch(param) {
3358         case ISCSI_PARAM_MAX_RECV_DLENGTH: {
3359                 char *saveptr = conn->data;
3360                 int flags = GFP_KERNEL;
3361
3362                 if (conn->data_size >= value) {
3363                         conn->max_recv_dlength = value;
3364                         break;
3365                 }
3366
3367                 spin_lock_bh(&session->lock);
3368                 if (conn->stop_stage == STOP_CONN_RECOVER)
3369                         flags = GFP_ATOMIC;
3370                 spin_unlock_bh(&session->lock);
3371
3372                 if (value <= PAGE_SIZE)
3373                         conn->data = kmalloc(value, flags);
3374                 else
3375                         conn->data = (void*)__get_free_pages(flags,
3376                                                              get_order(value));
3377                 if (conn->data == NULL) {
3378                         conn->data = saveptr;
3379                         return -ENOMEM;
3380                 }
3381                 if (conn->data_size <= PAGE_SIZE)
3382                         kfree(saveptr);
3383                 else
3384                         free_pages((unsigned long)saveptr,
3385                                    get_order(conn->data_size));
3386                 conn->max_recv_dlength = value;
3387                 conn->data_size = value;
3388                 }
3389                 break;
3390         case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3391                 conn->max_xmit_dlength =  value;
3392                 break;
3393         case ISCSI_PARAM_HDRDGST_EN:
3394                 conn->hdrdgst_en = value;
3395                 conn->hdr_size = sizeof(struct iscsi_hdr);
3396                 if (conn->hdrdgst_en) {
3397                         conn->hdr_size += sizeof(__u32);
3398                         if (!conn->tx_tfm)
3399                                 conn->tx_tfm = crypto_alloc_tfm("crc32c", 0);
3400                         if (!conn->tx_tfm)
3401                                 return -ENOMEM;
3402                         if (!conn->rx_tfm)
3403                                 conn->rx_tfm = crypto_alloc_tfm("crc32c", 0);
3404                         if (!conn->rx_tfm) {
3405                                 crypto_free_tfm(conn->tx_tfm);
3406                                 return -ENOMEM;
3407                         }
3408                 } else {
3409                         if (conn->tx_tfm)
3410                                 crypto_free_tfm(conn->tx_tfm);
3411                         if (conn->rx_tfm)
3412                                 crypto_free_tfm(conn->rx_tfm);
3413                 }
3414                 break;
3415         case ISCSI_PARAM_DATADGST_EN:
3416                 conn->datadgst_en = value;
3417                 if (conn->datadgst_en) {
3418                         if (!conn->data_tx_tfm)
3419                                 conn->data_tx_tfm =
3420                                     crypto_alloc_tfm("crc32c", 0);
3421                         if (!conn->data_tx_tfm)
3422                                 return -ENOMEM;
3423                         if (!conn->data_rx_tfm)
3424                                 conn->data_rx_tfm =
3425                                     crypto_alloc_tfm("crc32c", 0);
3426                         if (!conn->data_rx_tfm) {
3427                                 crypto_free_tfm(conn->data_tx_tfm);
3428                                 return -ENOMEM;
3429                         }
3430                 } else {
3431                         if (conn->data_tx_tfm)
3432                                 crypto_free_tfm(conn->data_tx_tfm);
3433                         if (conn->data_rx_tfm)
3434                                 crypto_free_tfm(conn->data_rx_tfm);
3435                 }
3436                 break;
3437         case ISCSI_PARAM_INITIAL_R2T_EN:
3438                 session->initial_r2t_en = value;
3439                 break;
3440         case ISCSI_PARAM_MAX_R2T:
3441                 if (session->max_r2t == roundup_pow_of_two(value))
3442                         break;
3443                 iscsi_r2tpool_free(session);
3444                 session->max_r2t = value;
3445                 if (session->max_r2t & (session->max_r2t - 1))
3446                         session->max_r2t = roundup_pow_of_two(session->max_r2t);
3447                 if (iscsi_r2tpool_alloc(session))
3448                         return -ENOMEM;
3449                 break;
3450         case ISCSI_PARAM_IMM_DATA_EN:
3451                 session->imm_data_en = value;
3452                 break;
3453         case ISCSI_PARAM_FIRST_BURST:
3454                 session->first_burst = value;
3455                 break;
3456         case ISCSI_PARAM_MAX_BURST:
3457                 session->max_burst = value;
3458                 break;
3459         case ISCSI_PARAM_PDU_INORDER_EN:
3460                 session->pdu_inorder_en = value;
3461                 break;
3462         case ISCSI_PARAM_DATASEQ_INORDER_EN:
3463                 session->dataseq_inorder_en = value;
3464                 break;
3465         case ISCSI_PARAM_ERL:
3466                 session->erl = value;
3467                 break;
3468         case ISCSI_PARAM_IFMARKER_EN:
3469                 BUG_ON(value);
3470                 session->ifmarker_en = value;
3471                 break;
3472         case ISCSI_PARAM_OFMARKER_EN:
3473                 BUG_ON(value);
3474                 session->ofmarker_en = value;
3475                 break;
3476         default:
3477                 break;
3478         }
3479
3480         return 0;
3481 }
3482
3483 static int
3484 iscsi_conn_get_param(iscsi_connh_t connh, enum iscsi_param param,
3485                      uint32_t *value)
3486 {
3487         struct iscsi_conn *conn = iscsi_ptr(connh);
3488         struct iscsi_session *session = conn->session;
3489
3490         switch(param) {
3491         case ISCSI_PARAM_MAX_RECV_DLENGTH:
3492                 *value = conn->max_recv_dlength;
3493                 break;
3494         case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3495                 *value = conn->max_xmit_dlength;
3496                 break;
3497         case ISCSI_PARAM_HDRDGST_EN:
3498                 *value = conn->hdrdgst_en;
3499                 break;
3500         case ISCSI_PARAM_DATADGST_EN:
3501                 *value = conn->datadgst_en;
3502                 break;
3503         case ISCSI_PARAM_INITIAL_R2T_EN:
3504                 *value = session->initial_r2t_en;
3505                 break;
3506         case ISCSI_PARAM_MAX_R2T:
3507                 *value = session->max_r2t;
3508                 break;
3509         case ISCSI_PARAM_IMM_DATA_EN:
3510                 *value = session->imm_data_en;
3511                 break;
3512         case ISCSI_PARAM_FIRST_BURST:
3513                 *value = session->first_burst;
3514                 break;
3515         case ISCSI_PARAM_MAX_BURST:
3516                 *value = session->max_burst;
3517                 break;
3518         case ISCSI_PARAM_PDU_INORDER_EN:
3519                 *value = session->pdu_inorder_en;
3520                 break;
3521         case ISCSI_PARAM_DATASEQ_INORDER_EN:
3522                 *value = session->dataseq_inorder_en;
3523                 break;
3524         case ISCSI_PARAM_ERL:
3525                 *value = session->erl;
3526                 break;
3527         case ISCSI_PARAM_IFMARKER_EN:
3528                 *value = session->ifmarker_en;
3529                 break;
3530         case ISCSI_PARAM_OFMARKER_EN:
3531                 *value = session->ofmarker_en;
3532                 break;
3533         default:
3534                 return ISCSI_ERR_PARAM_NOT_FOUND;
3535         }
3536
3537         return 0;
3538 }
3539
3540 static void
3541 iscsi_conn_get_stats(iscsi_connh_t connh, struct iscsi_stats *stats)
3542 {
3543         struct iscsi_conn *conn = iscsi_ptr(connh);
3544
3545         stats->txdata_octets = conn->txdata_octets;
3546         stats->rxdata_octets = conn->rxdata_octets;
3547         stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
3548         stats->dataout_pdus = conn->dataout_pdus_cnt;
3549         stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
3550         stats->datain_pdus = conn->datain_pdus_cnt;
3551         stats->r2t_pdus = conn->r2t_pdus_cnt;
3552         stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
3553         stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
3554         stats->custom_length = 3;
3555         strcpy(stats->custom[0].desc, "tx_sendpage_failures");
3556         stats->custom[0].value = conn->sendpage_failures_cnt;
3557         strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
3558         stats->custom[1].value = conn->discontiguous_hdr_cnt;
3559         strcpy(stats->custom[2].desc, "eh_abort_cnt");
3560         stats->custom[2].value = conn->eh_abort_cnt;
3561 }
3562
3563 static int
3564 iscsi_conn_send_pdu(iscsi_connh_t connh, struct iscsi_hdr *hdr, char *data,
3565                     uint32_t data_size)
3566 {
3567         struct iscsi_conn *conn = iscsi_ptr(connh);
3568         int rc;
3569
3570         down(&conn->xmitsema);
3571         rc = iscsi_conn_send_generic(conn, hdr, data, data_size);
3572         up(&conn->xmitsema);
3573
3574         return rc;
3575 }
3576
3577 static struct iscsi_transport iscsi_tcp_transport = {
3578         .owner                  = THIS_MODULE,
3579         .name                   = "tcp",
3580         .caps                   = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
3581                                   | CAP_DATADGST,
3582         .host_template          = &iscsi_sht,
3583         .hostdata_size          = sizeof(struct iscsi_session),
3584         .max_conn               = 1,
3585         .max_cmd_len            = ISCSI_TCP_MAX_CMD_LEN,
3586         .create_session         = iscsi_session_create,
3587         .destroy_session        = iscsi_session_destroy,
3588         .create_conn            = iscsi_conn_create,
3589         .bind_conn              = iscsi_conn_bind,
3590         .destroy_conn           = iscsi_conn_destroy,
3591         .set_param              = iscsi_conn_set_param,
3592         .get_param              = iscsi_conn_get_param,
3593         .start_conn             = iscsi_conn_start,
3594         .stop_conn              = iscsi_conn_stop,
3595         .send_pdu               = iscsi_conn_send_pdu,
3596         .get_stats              = iscsi_conn_get_stats,
3597 };
3598
3599 static int __init
3600 iscsi_tcp_init(void)
3601 {
3602         int error;
3603
3604         if (iscsi_max_lun < 1) {
3605                 printk(KERN_ERR "Invalid max_lun value of %u\n", iscsi_max_lun);
3606                 return -EINVAL;
3607         }
3608         iscsi_tcp_transport.max_lun = iscsi_max_lun;
3609
3610         taskcache = kmem_cache_create("iscsi_taskcache",
3611                         sizeof(struct iscsi_data_task), 0,
3612                         SLAB_HWCACHE_ALIGN | SLAB_NO_REAP, NULL, NULL);
3613         if (!taskcache)
3614                 return -ENOMEM;
3615
3616         error = iscsi_register_transport(&iscsi_tcp_transport);
3617         if (error)
3618                 kmem_cache_destroy(taskcache);
3619
3620         return error;
3621 }
3622
3623 static void __exit
3624 iscsi_tcp_exit(void)
3625 {
3626         iscsi_unregister_transport(&iscsi_tcp_transport);
3627         kmem_cache_destroy(taskcache);
3628 }
3629
3630 module_init(iscsi_tcp_init);
3631 module_exit(iscsi_tcp_exit);