2 * iSCSI Initiator over TCP/IP Data-Path
4 * Copyright (C) 2004 Dmitry Yusupov
5 * Copyright (C) 2004 Alex Aizman
6 * Copyright (C) 2005 - 2006 Mike Christie
7 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
8 * maintained by open-iscsi@googlegroups.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * See the file COPYING included with this distribution for more details.
29 #include <linux/types.h>
30 #include <linux/list.h>
31 #include <linux/inet.h>
32 #include <linux/blkdev.h>
33 #include <linux/crypto.h>
34 #include <linux/delay.h>
35 #include <linux/kfifo.h>
36 #include <linux/scatterlist.h>
37 #include <linux/mutex.h>
39 #include <scsi/scsi_cmnd.h>
40 #include <scsi/scsi_host.h>
41 #include <scsi/scsi.h>
42 #include <scsi/scsi_transport_iscsi.h>
44 #include "iscsi_tcp.h"
46 #define ISCSI_TCP_VERSION "1.0-574"
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 MODULE_VERSION(ISCSI_TCP_VERSION
);
53 /* #define DEBUG_TCP */
57 #define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
59 #define debug_tcp(fmt...)
69 static unsigned int iscsi_max_lun
= 512;
70 module_param_named(max_lun
, iscsi_max_lun
, uint
, S_IRUGO
);
73 iscsi_buf_init_iov(struct iscsi_buf
*ibuf
, char *vbuf
, int size
)
75 ibuf
->sg
.page
= virt_to_page(vbuf
);
76 ibuf
->sg
.offset
= offset_in_page(vbuf
);
77 ibuf
->sg
.length
= size
;
79 ibuf
->use_sendmsg
= 1;
83 iscsi_buf_init_sg(struct iscsi_buf
*ibuf
, struct scatterlist
*sg
)
85 ibuf
->sg
.page
= sg
->page
;
86 ibuf
->sg
.offset
= sg
->offset
;
87 ibuf
->sg
.length
= sg
->length
;
89 * Fastpath: sg element fits into single page
91 if (sg
->length
+ sg
->offset
<= PAGE_SIZE
&& !PageSlab(sg
->page
))
92 ibuf
->use_sendmsg
= 0;
94 ibuf
->use_sendmsg
= 1;
99 iscsi_buf_left(struct iscsi_buf
*ibuf
)
103 rc
= ibuf
->sg
.length
- ibuf
->sent
;
109 iscsi_hdr_digest(struct iscsi_conn
*conn
, struct iscsi_buf
*buf
,
112 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
114 crypto_digest_digest(tcp_conn
->tx_tfm
, &buf
->sg
, 1, crc
);
115 buf
->sg
.length
+= sizeof(uint32_t);
119 iscsi_hdr_extract(struct iscsi_tcp_conn
*tcp_conn
)
121 struct sk_buff
*skb
= tcp_conn
->in
.skb
;
123 tcp_conn
->in
.zero_copy_hdr
= 0;
125 if (tcp_conn
->in
.copy
>= tcp_conn
->hdr_size
&&
126 tcp_conn
->in_progress
== IN_PROGRESS_WAIT_HEADER
) {
128 * Zero-copy PDU Header: using connection context
129 * to store header pointer.
131 if (skb_shinfo(skb
)->frag_list
== NULL
&&
132 !skb_shinfo(skb
)->nr_frags
) {
133 tcp_conn
->in
.hdr
= (struct iscsi_hdr
*)
134 ((char*)skb
->data
+ tcp_conn
->in
.offset
);
135 tcp_conn
->in
.zero_copy_hdr
= 1;
137 /* ignoring return code since we checked
139 skb_copy_bits(skb
, tcp_conn
->in
.offset
,
140 &tcp_conn
->hdr
, tcp_conn
->hdr_size
);
141 tcp_conn
->in
.hdr
= &tcp_conn
->hdr
;
143 tcp_conn
->in
.offset
+= tcp_conn
->hdr_size
;
144 tcp_conn
->in
.copy
-= tcp_conn
->hdr_size
;
150 * PDU header scattered across SKB's,
151 * copying it... This'll happen quite rarely.
154 if (tcp_conn
->in_progress
== IN_PROGRESS_WAIT_HEADER
)
155 tcp_conn
->in
.hdr_offset
= 0;
157 hdr_remains
= tcp_conn
->hdr_size
- tcp_conn
->in
.hdr_offset
;
158 BUG_ON(hdr_remains
<= 0);
160 copylen
= min(tcp_conn
->in
.copy
, hdr_remains
);
161 skb_copy_bits(skb
, tcp_conn
->in
.offset
,
162 (char*)&tcp_conn
->hdr
+ tcp_conn
->in
.hdr_offset
,
165 debug_tcp("PDU gather offset %d bytes %d in.offset %d "
166 "in.copy %d\n", tcp_conn
->in
.hdr_offset
, copylen
,
167 tcp_conn
->in
.offset
, tcp_conn
->in
.copy
);
169 tcp_conn
->in
.offset
+= copylen
;
170 tcp_conn
->in
.copy
-= copylen
;
171 if (copylen
< hdr_remains
) {
172 tcp_conn
->in_progress
= IN_PROGRESS_HEADER_GATHER
;
173 tcp_conn
->in
.hdr_offset
+= copylen
;
176 tcp_conn
->in
.hdr
= &tcp_conn
->hdr
;
177 tcp_conn
->discontiguous_hdr_cnt
++;
178 tcp_conn
->in_progress
= IN_PROGRESS_WAIT_HEADER
;
185 * must be called with session lock
188 __iscsi_ctask_cleanup(struct iscsi_conn
*conn
, struct iscsi_cmd_task
*ctask
)
190 struct iscsi_tcp_cmd_task
*tcp_ctask
= ctask
->dd_data
;
191 struct scsi_cmnd
*sc
;
197 tcp_ctask
->xmstate
= XMSTATE_IDLE
;
198 tcp_ctask
->r2t
= NULL
;
202 * iscsi_data_rsp - SCSI Data-In Response processing
203 * @conn: iscsi connection
204 * @ctask: scsi command task
207 iscsi_data_rsp(struct iscsi_conn
*conn
, struct iscsi_cmd_task
*ctask
)
210 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
211 struct iscsi_tcp_cmd_task
*tcp_ctask
= ctask
->dd_data
;
212 struct iscsi_data_rsp
*rhdr
= (struct iscsi_data_rsp
*)tcp_conn
->in
.hdr
;
213 struct iscsi_session
*session
= conn
->session
;
214 int datasn
= be32_to_cpu(rhdr
->datasn
);
216 rc
= iscsi_check_assign_cmdsn(session
, (struct iscsi_nopin
*)rhdr
);
220 * setup Data-In byte counter (gets decremented..)
222 ctask
->data_count
= tcp_conn
->in
.datalen
;
224 if (tcp_conn
->in
.datalen
== 0)
227 if (ctask
->datasn
!= datasn
)
228 return ISCSI_ERR_DATASN
;
232 tcp_ctask
->data_offset
= be32_to_cpu(rhdr
->offset
);
233 if (tcp_ctask
->data_offset
+ tcp_conn
->in
.datalen
> ctask
->total_length
)
234 return ISCSI_ERR_DATA_OFFSET
;
236 if (rhdr
->flags
& ISCSI_FLAG_DATA_STATUS
) {
237 struct scsi_cmnd
*sc
= ctask
->sc
;
239 conn
->exp_statsn
= be32_to_cpu(rhdr
->statsn
) + 1;
240 if (rhdr
->flags
& ISCSI_FLAG_DATA_UNDERFLOW
) {
241 int res_count
= be32_to_cpu(rhdr
->residual_count
);
244 res_count
<= sc
->request_bufflen
) {
245 sc
->resid
= res_count
;
246 sc
->result
= (DID_OK
<< 16) | rhdr
->cmd_status
;
248 sc
->result
= (DID_BAD_TARGET
<< 16) |
250 } else if (rhdr
->flags
& ISCSI_FLAG_DATA_OVERFLOW
) {
251 sc
->resid
= be32_to_cpu(rhdr
->residual_count
);
252 sc
->result
= (DID_OK
<< 16) | rhdr
->cmd_status
;
254 sc
->result
= (DID_OK
<< 16) | rhdr
->cmd_status
;
257 conn
->datain_pdus_cnt
++;
262 * iscsi_solicit_data_init - initialize first Data-Out
263 * @conn: iscsi connection
264 * @ctask: scsi command task
268 * Initialize first Data-Out within this R2T sequence and finds
269 * proper data_offset within this SCSI command.
271 * This function is called with connection lock taken.
274 iscsi_solicit_data_init(struct iscsi_conn
*conn
, struct iscsi_cmd_task
*ctask
,
275 struct iscsi_r2t_info
*r2t
)
277 struct iscsi_data
*hdr
;
278 struct scsi_cmnd
*sc
= ctask
->sc
;
279 struct iscsi_tcp_cmd_task
*tcp_ctask
= ctask
->dd_data
;
281 hdr
= &r2t
->dtask
.hdr
;
282 memset(hdr
, 0, sizeof(struct iscsi_data
));
284 hdr
->datasn
= cpu_to_be32(r2t
->solicit_datasn
);
285 r2t
->solicit_datasn
++;
286 hdr
->opcode
= ISCSI_OP_SCSI_DATA_OUT
;
287 memcpy(hdr
->lun
, ctask
->hdr
->lun
, sizeof(hdr
->lun
));
288 hdr
->itt
= ctask
->hdr
->itt
;
289 hdr
->exp_statsn
= r2t
->exp_statsn
;
290 hdr
->offset
= cpu_to_be32(r2t
->data_offset
);
291 if (r2t
->data_length
> conn
->max_xmit_dlength
) {
292 hton24(hdr
->dlength
, conn
->max_xmit_dlength
);
293 r2t
->data_count
= conn
->max_xmit_dlength
;
296 hton24(hdr
->dlength
, r2t
->data_length
);
297 r2t
->data_count
= r2t
->data_length
;
298 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
300 conn
->dataout_pdus_cnt
++;
304 iscsi_buf_init_iov(&r2t
->headbuf
, (char*)hdr
,
305 sizeof(struct iscsi_hdr
));
309 struct scatterlist
*sg
= sc
->request_buffer
;
312 for (i
= 0; i
< sc
->use_sg
; i
++, sg
+= 1) {
313 /* FIXME: prefetch ? */
314 if (sg_count
+ sg
->length
> r2t
->data_offset
) {
319 /* offset within this page */
320 page_offset
= r2t
->data_offset
- sg_count
;
322 /* fill in this buffer */
323 iscsi_buf_init_sg(&r2t
->sendbuf
, sg
);
324 r2t
->sendbuf
.sg
.offset
+= page_offset
;
325 r2t
->sendbuf
.sg
.length
-= page_offset
;
327 /* xmit logic will continue with next one */
331 sg_count
+= sg
->length
;
333 BUG_ON(r2t
->sg
== NULL
);
335 iscsi_buf_init_iov(&tcp_ctask
->sendbuf
,
336 (char*)sc
->request_buffer
+ r2t
->data_offset
,
341 * iscsi_r2t_rsp - iSCSI R2T Response processing
342 * @conn: iscsi connection
343 * @ctask: scsi command task
346 iscsi_r2t_rsp(struct iscsi_conn
*conn
, struct iscsi_cmd_task
*ctask
)
348 struct iscsi_r2t_info
*r2t
;
349 struct iscsi_session
*session
= conn
->session
;
350 struct iscsi_tcp_cmd_task
*tcp_ctask
= ctask
->dd_data
;
351 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
352 struct iscsi_r2t_rsp
*rhdr
= (struct iscsi_r2t_rsp
*)tcp_conn
->in
.hdr
;
353 int r2tsn
= be32_to_cpu(rhdr
->r2tsn
);
356 if (tcp_conn
->in
.datalen
)
357 return ISCSI_ERR_DATALEN
;
359 if (tcp_ctask
->exp_r2tsn
&& tcp_ctask
->exp_r2tsn
!= r2tsn
)
360 return ISCSI_ERR_R2TSN
;
362 rc
= iscsi_check_assign_cmdsn(session
, (struct iscsi_nopin
*)rhdr
);
366 /* FIXME: use R2TSN to detect missing R2T */
368 /* fill-in new R2T associated with the task */
369 spin_lock(&session
->lock
);
370 if (!ctask
->sc
|| ctask
->mtask
||
371 session
->state
!= ISCSI_STATE_LOGGED_IN
) {
372 printk(KERN_INFO
"iscsi_tcp: dropping R2T itt %d in "
373 "recovery...\n", ctask
->itt
);
374 spin_unlock(&session
->lock
);
377 rc
= __kfifo_get(tcp_ctask
->r2tpool
.queue
, (void*)&r2t
, sizeof(void*));
380 r2t
->exp_statsn
= rhdr
->statsn
;
381 r2t
->data_length
= be32_to_cpu(rhdr
->data_length
);
382 if (r2t
->data_length
== 0 ||
383 r2t
->data_length
> session
->max_burst
) {
384 spin_unlock(&session
->lock
);
385 return ISCSI_ERR_DATALEN
;
388 r2t
->data_offset
= be32_to_cpu(rhdr
->data_offset
);
389 if (r2t
->data_offset
+ r2t
->data_length
> ctask
->total_length
) {
390 spin_unlock(&session
->lock
);
391 return ISCSI_ERR_DATALEN
;
394 r2t
->ttt
= rhdr
->ttt
; /* no flip */
395 r2t
->solicit_datasn
= 0;
397 iscsi_solicit_data_init(conn
, ctask
, r2t
);
399 tcp_ctask
->exp_r2tsn
= r2tsn
+ 1;
400 tcp_ctask
->xmstate
|= XMSTATE_SOL_HDR
;
401 __kfifo_put(tcp_ctask
->r2tqueue
, (void*)&r2t
, sizeof(void*));
402 __kfifo_put(conn
->xmitqueue
, (void*)&ctask
, sizeof(void*));
404 scsi_queue_work(session
->host
, &conn
->xmitwork
);
405 conn
->r2t_pdus_cnt
++;
406 spin_unlock(&session
->lock
);
412 iscsi_tcp_hdr_recv(struct iscsi_conn
*conn
)
414 int rc
= 0, opcode
, ahslen
;
415 struct iscsi_hdr
*hdr
;
416 struct iscsi_session
*session
= conn
->session
;
417 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
418 uint32_t cdgst
, rdgst
= 0, itt
;
420 hdr
= tcp_conn
->in
.hdr
;
422 /* verify PDU length */
423 tcp_conn
->in
.datalen
= ntoh24(hdr
->dlength
);
424 if (tcp_conn
->in
.datalen
> conn
->max_recv_dlength
) {
425 printk(KERN_ERR
"iscsi_tcp: datalen %d > %d\n",
426 tcp_conn
->in
.datalen
, conn
->max_recv_dlength
);
427 return ISCSI_ERR_DATALEN
;
429 tcp_conn
->data_copied
= 0;
432 ahslen
= hdr
->hlength
<< 2;
433 tcp_conn
->in
.offset
+= ahslen
;
434 tcp_conn
->in
.copy
-= ahslen
;
435 if (tcp_conn
->in
.copy
< 0) {
436 printk(KERN_ERR
"iscsi_tcp: can't handle AHS with length "
437 "%d bytes\n", ahslen
);
438 return ISCSI_ERR_AHSLEN
;
441 /* calculate read padding */
442 tcp_conn
->in
.padding
= tcp_conn
->in
.datalen
& (ISCSI_PAD_LEN
-1);
443 if (tcp_conn
->in
.padding
) {
444 tcp_conn
->in
.padding
= ISCSI_PAD_LEN
- tcp_conn
->in
.padding
;
445 debug_scsi("read padding %d bytes\n", tcp_conn
->in
.padding
);
448 if (conn
->hdrdgst_en
) {
449 struct scatterlist sg
;
451 sg_init_one(&sg
, (u8
*)hdr
,
452 sizeof(struct iscsi_hdr
) + ahslen
);
453 crypto_digest_digest(tcp_conn
->rx_tfm
, &sg
, 1, (u8
*)&cdgst
);
454 rdgst
= *(uint32_t*)((char*)hdr
+ sizeof(struct iscsi_hdr
) +
456 if (cdgst
!= rdgst
) {
457 printk(KERN_ERR
"iscsi_tcp: hdrdgst error "
458 "recv 0x%x calc 0x%x\n", rdgst
, cdgst
);
459 return ISCSI_ERR_HDR_DGST
;
463 opcode
= hdr
->opcode
& ISCSI_OPCODE_MASK
;
464 /* verify itt (itt encoding: age+cid+itt) */
465 rc
= iscsi_verify_itt(conn
, hdr
, &itt
);
466 if (rc
== ISCSI_ERR_NO_SCSI_CMD
) {
467 tcp_conn
->in
.datalen
= 0; /* force drop */
472 debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n",
473 opcode
, tcp_conn
->in
.offset
, tcp_conn
->in
.copy
,
474 ahslen
, tcp_conn
->in
.datalen
);
477 case ISCSI_OP_SCSI_DATA_IN
:
478 tcp_conn
->in
.ctask
= session
->cmds
[itt
];
479 rc
= iscsi_data_rsp(conn
, tcp_conn
->in
.ctask
);
481 case ISCSI_OP_SCSI_CMD_RSP
:
482 tcp_conn
->in
.ctask
= session
->cmds
[itt
];
483 if (tcp_conn
->in
.datalen
)
486 spin_lock(&session
->lock
);
487 __iscsi_ctask_cleanup(conn
, tcp_conn
->in
.ctask
);
488 rc
= __iscsi_complete_pdu(conn
, hdr
, NULL
, 0);
489 spin_unlock(&session
->lock
);
492 tcp_conn
->in
.ctask
= session
->cmds
[itt
];
494 rc
= ISCSI_ERR_AHSLEN
;
495 else if (tcp_conn
->in
.ctask
->sc
->sc_data_direction
==
497 rc
= iscsi_r2t_rsp(conn
, tcp_conn
->in
.ctask
);
499 rc
= ISCSI_ERR_PROTO
;
501 case ISCSI_OP_LOGIN_RSP
:
502 case ISCSI_OP_TEXT_RSP
:
503 case ISCSI_OP_LOGOUT_RSP
:
504 case ISCSI_OP_NOOP_IN
:
505 case ISCSI_OP_REJECT
:
506 case ISCSI_OP_ASYNC_EVENT
:
507 if (tcp_conn
->in
.datalen
)
510 case ISCSI_OP_SCSI_TMFUNC_RSP
:
511 rc
= iscsi_complete_pdu(conn
, hdr
, NULL
, 0);
514 rc
= ISCSI_ERR_BAD_OPCODE
;
522 * if we did zero copy for the header but we will need multiple
523 * skbs to complete the command then we have to copy the header
526 if (tcp_conn
->in
.zero_copy_hdr
&& tcp_conn
->in
.copy
<
527 (tcp_conn
->in
.datalen
+ tcp_conn
->in
.padding
+
528 (conn
->datadgst_en
? 4 : 0))) {
529 debug_tcp("Copying header for later use. in.copy %d in.datalen"
530 " %d\n", tcp_conn
->in
.copy
, tcp_conn
->in
.datalen
);
531 memcpy(&tcp_conn
->hdr
, tcp_conn
->in
.hdr
,
532 sizeof(struct iscsi_hdr
));
533 tcp_conn
->in
.hdr
= &tcp_conn
->hdr
;
534 tcp_conn
->in
.zero_copy_hdr
= 0;
540 * iscsi_ctask_copy - copy skb bits to the destanation cmd task
541 * @conn: iscsi tcp connection
542 * @ctask: scsi command task
543 * @buf: buffer to copy to
544 * @buf_size: size of buffer
545 * @offset: offset within the buffer
548 * The function calls skb_copy_bits() and updates per-connection and
549 * per-cmd byte counters.
551 * Read counters (in bytes):
553 * conn->in.offset offset within in progress SKB
554 * conn->in.copy left to copy from in progress SKB
556 * conn->in.copied copied already from in progress SKB
557 * conn->data_copied copied already from in progress buffer
558 * ctask->sent total bytes sent up to the MidLayer
559 * ctask->data_count left to copy from in progress Data-In
560 * buf_left left to copy from in progress buffer
563 iscsi_ctask_copy(struct iscsi_tcp_conn
*tcp_conn
, struct iscsi_cmd_task
*ctask
,
564 void *buf
, int buf_size
, int offset
)
566 struct iscsi_tcp_cmd_task
*tcp_ctask
= ctask
->dd_data
;
567 int buf_left
= buf_size
- (tcp_conn
->data_copied
+ offset
);
568 int size
= min(tcp_conn
->in
.copy
, buf_left
);
571 size
= min(size
, ctask
->data_count
);
573 debug_tcp("ctask_copy %d bytes at offset %d copied %d\n",
574 size
, tcp_conn
->in
.offset
, tcp_conn
->in
.copied
);
577 BUG_ON(tcp_ctask
->sent
+ size
> ctask
->total_length
);
579 rc
= skb_copy_bits(tcp_conn
->in
.skb
, tcp_conn
->in
.offset
,
580 (char*)buf
+ (offset
+ tcp_conn
->data_copied
), size
);
581 /* must fit into skb->len */
584 tcp_conn
->in
.offset
+= size
;
585 tcp_conn
->in
.copy
-= size
;
586 tcp_conn
->in
.copied
+= size
;
587 tcp_conn
->data_copied
+= size
;
588 tcp_ctask
->sent
+= size
;
589 ctask
->data_count
-= size
;
591 BUG_ON(tcp_conn
->in
.copy
< 0);
592 BUG_ON(ctask
->data_count
< 0);
594 if (buf_size
!= (tcp_conn
->data_copied
+ offset
)) {
595 if (!ctask
->data_count
) {
596 BUG_ON(buf_size
- tcp_conn
->data_copied
< 0);
597 /* done with this PDU */
598 return buf_size
- tcp_conn
->data_copied
;
603 /* done with this buffer or with both - PDU and buffer */
604 tcp_conn
->data_copied
= 0;
609 * iscsi_tcp_copy - copy skb bits to the destanation buffer
610 * @conn: iscsi tcp connection
613 * The function calls skb_copy_bits() and updates per-connection
617 iscsi_tcp_copy(struct iscsi_tcp_conn
*tcp_conn
)
619 void *buf
= tcp_conn
->data
;
620 int buf_size
= tcp_conn
->in
.datalen
;
621 int buf_left
= buf_size
- tcp_conn
->data_copied
;
622 int size
= min(tcp_conn
->in
.copy
, buf_left
);
625 debug_tcp("tcp_copy %d bytes at offset %d copied %d\n",
626 size
, tcp_conn
->in
.offset
, tcp_conn
->data_copied
);
629 rc
= skb_copy_bits(tcp_conn
->in
.skb
, tcp_conn
->in
.offset
,
630 (char*)buf
+ tcp_conn
->data_copied
, size
);
633 tcp_conn
->in
.offset
+= size
;
634 tcp_conn
->in
.copy
-= size
;
635 tcp_conn
->in
.copied
+= size
;
636 tcp_conn
->data_copied
+= size
;
638 if (buf_size
!= tcp_conn
->data_copied
)
645 partial_sg_digest_update(struct iscsi_tcp_conn
*tcp_conn
,
646 struct scatterlist
*sg
, int offset
, int length
)
648 struct scatterlist temp
;
650 memcpy(&temp
, sg
, sizeof(struct scatterlist
));
651 temp
.offset
= offset
;
652 temp
.length
= length
;
653 crypto_digest_update(tcp_conn
->data_rx_tfm
, &temp
, 1);
657 iscsi_recv_digest_update(struct iscsi_tcp_conn
*tcp_conn
, char* buf
, int len
)
659 struct scatterlist tmp
;
661 sg_init_one(&tmp
, buf
, len
);
662 crypto_digest_update(tcp_conn
->data_rx_tfm
, &tmp
, 1);
665 static int iscsi_scsi_data_in(struct iscsi_conn
*conn
)
667 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
668 struct iscsi_cmd_task
*ctask
= tcp_conn
->in
.ctask
;
669 struct iscsi_tcp_cmd_task
*tcp_ctask
= ctask
->dd_data
;
670 struct scsi_cmnd
*sc
= ctask
->sc
;
671 struct scatterlist
*sg
;
672 int i
, offset
, rc
= 0;
674 BUG_ON((void*)ctask
!= sc
->SCp
.ptr
);
677 * copying Data-In into the Scsi_Cmnd
680 i
= ctask
->data_count
;
681 rc
= iscsi_ctask_copy(tcp_conn
, ctask
, sc
->request_buffer
,
683 tcp_ctask
->data_offset
);
686 if (conn
->datadgst_en
)
687 iscsi_recv_digest_update(tcp_conn
, sc
->request_buffer
,
693 offset
= tcp_ctask
->data_offset
;
694 sg
= sc
->request_buffer
;
696 if (tcp_ctask
->data_offset
)
697 for (i
= 0; i
< tcp_ctask
->sg_count
; i
++)
698 offset
-= sg
[i
].length
;
699 /* we've passed through partial sg*/
703 for (i
= tcp_ctask
->sg_count
; i
< sc
->use_sg
; i
++) {
706 dest
= kmap_atomic(sg
[i
].page
, KM_SOFTIRQ0
);
707 rc
= iscsi_ctask_copy(tcp_conn
, ctask
, dest
+ sg
[i
].offset
,
708 sg
[i
].length
, offset
);
709 kunmap_atomic(dest
, KM_SOFTIRQ0
);
711 /* continue with the next SKB/PDU */
714 if (conn
->datadgst_en
) {
716 crypto_digest_update(
717 tcp_conn
->data_rx_tfm
,
720 partial_sg_digest_update(tcp_conn
,
722 sg
[i
].offset
+ offset
,
723 sg
[i
].length
- offset
);
726 tcp_ctask
->sg_count
++;
729 if (!ctask
->data_count
) {
730 if (rc
&& conn
->datadgst_en
)
732 * data-in is complete, but buffer not...
734 partial_sg_digest_update(tcp_conn
, &sg
[i
],
735 sg
[i
].offset
, sg
[i
].length
-rc
);
740 if (!tcp_conn
->in
.copy
)
743 BUG_ON(ctask
->data_count
);
746 /* check for non-exceptional status */
747 if (tcp_conn
->in
.hdr
->flags
& ISCSI_FLAG_DATA_STATUS
) {
748 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
749 (long)sc
, sc
->result
, ctask
->itt
);
750 spin_lock(&conn
->session
->lock
);
751 __iscsi_ctask_cleanup(conn
, ctask
);
752 __iscsi_complete_pdu(conn
, tcp_conn
->in
.hdr
, NULL
, 0);
753 spin_unlock(&conn
->session
->lock
);
760 iscsi_data_recv(struct iscsi_conn
*conn
)
762 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
765 opcode
= tcp_conn
->in
.hdr
->opcode
& ISCSI_OPCODE_MASK
;
767 case ISCSI_OP_SCSI_DATA_IN
:
768 rc
= iscsi_scsi_data_in(conn
);
770 case ISCSI_OP_SCSI_CMD_RSP
:
771 spin_lock(&conn
->session
->lock
);
772 __iscsi_ctask_cleanup(conn
, tcp_conn
->in
.ctask
);
773 spin_unlock(&conn
->session
->lock
);
774 case ISCSI_OP_TEXT_RSP
:
775 case ISCSI_OP_LOGIN_RSP
:
776 case ISCSI_OP_NOOP_IN
:
777 case ISCSI_OP_ASYNC_EVENT
:
778 case ISCSI_OP_REJECT
:
780 * Collect data segment to the connection's data
783 if (iscsi_tcp_copy(tcp_conn
)) {
788 rc
= iscsi_complete_pdu(conn
, tcp_conn
->in
.hdr
, tcp_conn
->data
,
789 tcp_conn
->in
.datalen
);
790 if (!rc
&& conn
->datadgst_en
&& opcode
!= ISCSI_OP_LOGIN_RSP
)
791 iscsi_recv_digest_update(tcp_conn
, tcp_conn
->data
,
792 tcp_conn
->in
.datalen
);
802 * iscsi_tcp_data_recv - TCP receive in sendfile fashion
803 * @rd_desc: read descriptor
804 * @skb: socket buffer
805 * @offset: offset in skb
806 * @len: skb->len - offset
809 iscsi_tcp_data_recv(read_descriptor_t
*rd_desc
, struct sk_buff
*skb
,
810 unsigned int offset
, size_t len
)
813 struct iscsi_conn
*conn
= rd_desc
->arg
.data
;
814 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
816 char pad
[ISCSI_PAD_LEN
];
817 struct scatterlist sg
;
820 * Save current SKB and its offset in the corresponding
821 * connection context.
823 tcp_conn
->in
.copy
= skb
->len
- offset
;
824 tcp_conn
->in
.offset
= offset
;
825 tcp_conn
->in
.skb
= skb
;
826 tcp_conn
->in
.len
= tcp_conn
->in
.copy
;
827 BUG_ON(tcp_conn
->in
.copy
<= 0);
828 debug_tcp("in %d bytes\n", tcp_conn
->in
.copy
);
831 tcp_conn
->in
.copied
= 0;
834 if (unlikely(conn
->suspend_rx
)) {
835 debug_tcp("conn %d Rx suspended!\n", conn
->id
);
839 if (tcp_conn
->in_progress
== IN_PROGRESS_WAIT_HEADER
||
840 tcp_conn
->in_progress
== IN_PROGRESS_HEADER_GATHER
) {
841 rc
= iscsi_hdr_extract(tcp_conn
);
846 iscsi_conn_failure(conn
, rc
);
852 * Verify and process incoming PDU header.
854 rc
= iscsi_tcp_hdr_recv(conn
);
855 if (!rc
&& tcp_conn
->in
.datalen
) {
856 if (conn
->datadgst_en
) {
857 BUG_ON(!tcp_conn
->data_rx_tfm
);
858 crypto_digest_init(tcp_conn
->data_rx_tfm
);
860 tcp_conn
->in_progress
= IN_PROGRESS_DATA_RECV
;
862 iscsi_conn_failure(conn
, rc
);
867 if (tcp_conn
->in_progress
== IN_PROGRESS_DDIGEST_RECV
) {
868 uint32_t recv_digest
;
870 debug_tcp("extra data_recv offset %d copy %d\n",
871 tcp_conn
->in
.offset
, tcp_conn
->in
.copy
);
872 skb_copy_bits(tcp_conn
->in
.skb
, tcp_conn
->in
.offset
,
874 tcp_conn
->in
.offset
+= 4;
875 tcp_conn
->in
.copy
-= 4;
876 if (recv_digest
!= tcp_conn
->in
.datadgst
) {
877 debug_tcp("iscsi_tcp: data digest error!"
878 "0x%x != 0x%x\n", recv_digest
,
879 tcp_conn
->in
.datadgst
);
880 iscsi_conn_failure(conn
, ISCSI_ERR_DATA_DGST
);
883 debug_tcp("iscsi_tcp: data digest match!"
884 "0x%x == 0x%x\n", recv_digest
,
885 tcp_conn
->in
.datadgst
);
886 tcp_conn
->in_progress
= IN_PROGRESS_WAIT_HEADER
;
890 if (tcp_conn
->in_progress
== IN_PROGRESS_DATA_RECV
&&
893 debug_tcp("data_recv offset %d copy %d\n",
894 tcp_conn
->in
.offset
, tcp_conn
->in
.copy
);
896 rc
= iscsi_data_recv(conn
);
900 iscsi_conn_failure(conn
, rc
);
903 tcp_conn
->in
.copy
-= tcp_conn
->in
.padding
;
904 tcp_conn
->in
.offset
+= tcp_conn
->in
.padding
;
905 if (conn
->datadgst_en
) {
906 if (tcp_conn
->in
.padding
) {
907 debug_tcp("padding -> %d\n",
908 tcp_conn
->in
.padding
);
909 memset(pad
, 0, tcp_conn
->in
.padding
);
910 sg_init_one(&sg
, pad
, tcp_conn
->in
.padding
);
911 crypto_digest_update(tcp_conn
->data_rx_tfm
,
914 crypto_digest_final(tcp_conn
->data_rx_tfm
,
915 (u8
*) & tcp_conn
->in
.datadgst
);
916 debug_tcp("rx digest 0x%x\n", tcp_conn
->in
.datadgst
);
917 tcp_conn
->in_progress
= IN_PROGRESS_DDIGEST_RECV
;
919 tcp_conn
->in_progress
= IN_PROGRESS_WAIT_HEADER
;
922 debug_tcp("f, processed %d from out of %d padding %d\n",
923 tcp_conn
->in
.offset
- offset
, (int)len
, tcp_conn
->in
.padding
);
924 BUG_ON(tcp_conn
->in
.offset
- offset
> len
);
926 if (tcp_conn
->in
.offset
- offset
!= len
) {
927 debug_tcp("continue to process %d bytes\n",
928 (int)len
- (tcp_conn
->in
.offset
- offset
));
933 processed
= tcp_conn
->in
.offset
- offset
;
934 BUG_ON(processed
== 0);
938 processed
= tcp_conn
->in
.offset
- offset
;
939 debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n",
940 processed
, (int)len
, (int)rd_desc
->count
);
941 BUG_ON(processed
== 0);
942 BUG_ON(processed
> len
);
944 conn
->rxdata_octets
+= processed
;
949 iscsi_tcp_data_ready(struct sock
*sk
, int flag
)
951 struct iscsi_conn
*conn
= sk
->sk_user_data
;
952 read_descriptor_t rd_desc
;
954 read_lock(&sk
->sk_callback_lock
);
957 * Use rd_desc to pass 'conn' to iscsi_tcp_data_recv.
958 * We set count to 1 because we want the network layer to
959 * hand us all the skbs that are available. iscsi_tcp_data_recv
960 * handled pdus that cross buffers or pdus that still need data.
962 rd_desc
.arg
.data
= conn
;
964 tcp_read_sock(sk
, &rd_desc
, iscsi_tcp_data_recv
);
966 read_unlock(&sk
->sk_callback_lock
);
970 iscsi_tcp_state_change(struct sock
*sk
)
972 struct iscsi_tcp_conn
*tcp_conn
;
973 struct iscsi_conn
*conn
;
974 struct iscsi_session
*session
;
975 void (*old_state_change
)(struct sock
*);
977 read_lock(&sk
->sk_callback_lock
);
979 conn
= (struct iscsi_conn
*)sk
->sk_user_data
;
980 session
= conn
->session
;
982 if ((sk
->sk_state
== TCP_CLOSE_WAIT
||
983 sk
->sk_state
== TCP_CLOSE
) &&
984 !atomic_read(&sk
->sk_rmem_alloc
)) {
985 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
986 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
989 tcp_conn
= conn
->dd_data
;
990 old_state_change
= tcp_conn
->old_state_change
;
992 read_unlock(&sk
->sk_callback_lock
);
994 old_state_change(sk
);
998 * iscsi_write_space - Called when more output buffer space is available
999 * @sk: socket space is available for
1002 iscsi_write_space(struct sock
*sk
)
1004 struct iscsi_conn
*conn
= (struct iscsi_conn
*)sk
->sk_user_data
;
1005 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
1007 tcp_conn
->old_write_space(sk
);
1008 debug_tcp("iscsi_write_space: cid %d\n", conn
->id
);
1009 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1010 scsi_queue_work(conn
->session
->host
, &conn
->xmitwork
);
1014 iscsi_conn_set_callbacks(struct iscsi_conn
*conn
)
1016 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
1017 struct sock
*sk
= tcp_conn
->sock
->sk
;
1019 /* assign new callbacks */
1020 write_lock_bh(&sk
->sk_callback_lock
);
1021 sk
->sk_user_data
= conn
;
1022 tcp_conn
->old_data_ready
= sk
->sk_data_ready
;
1023 tcp_conn
->old_state_change
= sk
->sk_state_change
;
1024 tcp_conn
->old_write_space
= sk
->sk_write_space
;
1025 sk
->sk_data_ready
= iscsi_tcp_data_ready
;
1026 sk
->sk_state_change
= iscsi_tcp_state_change
;
1027 sk
->sk_write_space
= iscsi_write_space
;
1028 write_unlock_bh(&sk
->sk_callback_lock
);
1032 iscsi_conn_restore_callbacks(struct iscsi_conn
*conn
)
1034 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
1035 struct sock
*sk
= tcp_conn
->sock
->sk
;
1037 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1038 write_lock_bh(&sk
->sk_callback_lock
);
1039 sk
->sk_user_data
= NULL
;
1040 sk
->sk_data_ready
= tcp_conn
->old_data_ready
;
1041 sk
->sk_state_change
= tcp_conn
->old_state_change
;
1042 sk
->sk_write_space
= tcp_conn
->old_write_space
;
1043 sk
->sk_no_check
= 0;
1044 write_unlock_bh(&sk
->sk_callback_lock
);
1048 * iscsi_send - generic send routine
1049 * @sk: kernel's socket
1050 * @buf: buffer to write from
1051 * @size: actual size to write
1052 * @flags: socket's flags
1055 iscsi_send(struct iscsi_conn
*conn
, struct iscsi_buf
*buf
, int size
, int flags
)
1057 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
1058 struct socket
*sk
= tcp_conn
->sock
;
1059 int offset
= buf
->sg
.offset
+ buf
->sent
;
1062 * if we got use_sg=0 or are sending something we kmallocd
1063 * then we did not have to do kmap (kmap returns page_address)
1065 * if we got use_sg > 0, but had to drop down, we do not
1066 * set clustering so this should only happen for that
1069 if (buf
->use_sendmsg
)
1070 return sock_no_sendpage(sk
, buf
->sg
.page
, offset
, size
, flags
);
1072 return tcp_conn
->sendpage(sk
, buf
->sg
.page
, offset
, size
,
1077 * iscsi_sendhdr - send PDU Header via tcp_sendpage()
1078 * @conn: iscsi connection
1079 * @buf: buffer to write from
1080 * @datalen: lenght of data to be sent after the header
1086 iscsi_sendhdr(struct iscsi_conn
*conn
, struct iscsi_buf
*buf
, int datalen
)
1088 struct iscsi_tcp_conn
*tcp_conn
;
1089 int flags
= 0; /* MSG_DONTWAIT; */
1092 size
= buf
->sg
.length
- buf
->sent
;
1093 BUG_ON(buf
->sent
+ size
> buf
->sg
.length
);
1094 if (buf
->sent
+ size
!= buf
->sg
.length
|| datalen
)
1097 res
= iscsi_send(conn
, buf
, size
, flags
);
1098 debug_tcp("sendhdr %d bytes, sent %d res %d\n", size
, buf
->sent
, res
);
1100 conn
->txdata_octets
+= res
;
1105 } else if (res
== -EAGAIN
) {
1106 tcp_conn
= conn
->dd_data
;
1107 tcp_conn
->sendpage_failures_cnt
++;
1108 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1109 } else if (res
== -EPIPE
)
1110 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1116 * iscsi_sendpage - send one page of iSCSI Data-Out.
1117 * @conn: iscsi connection
1118 * @buf: buffer to write from
1119 * @count: remaining data
1120 * @sent: number of bytes sent
1126 iscsi_sendpage(struct iscsi_conn
*conn
, struct iscsi_buf
*buf
,
1127 int *count
, int *sent
)
1129 struct iscsi_tcp_conn
*tcp_conn
;
1130 int flags
= 0; /* MSG_DONTWAIT; */
1133 size
= buf
->sg
.length
- buf
->sent
;
1134 BUG_ON(buf
->sent
+ size
> buf
->sg
.length
);
1137 if (buf
->sent
+ size
!= buf
->sg
.length
|| *count
!= size
)
1140 res
= iscsi_send(conn
, buf
, size
, flags
);
1141 debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n",
1142 size
, buf
->sent
, *count
, *sent
, res
);
1144 conn
->txdata_octets
+= res
;
1151 } else if (res
== -EAGAIN
) {
1152 tcp_conn
= conn
->dd_data
;
1153 tcp_conn
->sendpage_failures_cnt
++;
1154 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1155 } else if (res
== -EPIPE
)
1156 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1162 iscsi_data_digest_init(struct iscsi_tcp_conn
*tcp_conn
,
1163 struct iscsi_cmd_task
*ctask
)
1165 struct iscsi_tcp_cmd_task
*tcp_ctask
= ctask
->dd_data
;
1167 BUG_ON(!tcp_conn
->data_tx_tfm
);
1168 crypto_digest_init(tcp_conn
->data_tx_tfm
);
1169 tcp_ctask
->digest_count
= 4;
1173 iscsi_digest_final_send(struct iscsi_conn
*conn
, struct iscsi_cmd_task
*ctask
,
1174 struct iscsi_buf
*buf
, uint32_t *digest
, int final
)
1176 struct iscsi_tcp_cmd_task
*tcp_ctask
= ctask
->dd_data
;
1177 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
1182 crypto_digest_final(tcp_conn
->data_tx_tfm
, (u8
*)digest
);
1184 iscsi_buf_init_iov(buf
, (char*)digest
, 4);
1185 rc
= iscsi_sendpage(conn
, buf
, &tcp_ctask
->digest_count
, &sent
);
1187 tcp_ctask
->datadigest
= *digest
;
1188 tcp_ctask
->xmstate
|= XMSTATE_DATA_DIGEST
;
1190 tcp_ctask
->digest_count
= 4;
1195 * iscsi_solicit_data_cont - initialize next Data-Out
1196 * @conn: iscsi connection
1197 * @ctask: scsi command task
1199 * @left: bytes left to transfer
1202 * Initialize next Data-Out within this R2T sequence and continue
1203 * to process next Scatter-Gather element(if any) of this SCSI command.
1205 * Called under connection lock.
1208 iscsi_solicit_data_cont(struct iscsi_conn
*conn
, struct iscsi_cmd_task
*ctask
,
1209 struct iscsi_r2t_info
*r2t
, int left
)
1211 struct iscsi_tcp_cmd_task
*tcp_ctask
= ctask
->dd_data
;
1212 struct iscsi_data
*hdr
;
1213 struct scsi_cmnd
*sc
= ctask
->sc
;
1216 hdr
= &r2t
->dtask
.hdr
;
1217 memset(hdr
, 0, sizeof(struct iscsi_data
));
1218 hdr
->ttt
= r2t
->ttt
;
1219 hdr
->datasn
= cpu_to_be32(r2t
->solicit_datasn
);
1220 r2t
->solicit_datasn
++;
1221 hdr
->opcode
= ISCSI_OP_SCSI_DATA_OUT
;
1222 memcpy(hdr
->lun
, ctask
->hdr
->lun
, sizeof(hdr
->lun
));
1223 hdr
->itt
= ctask
->hdr
->itt
;
1224 hdr
->exp_statsn
= r2t
->exp_statsn
;
1225 new_offset
= r2t
->data_offset
+ r2t
->sent
;
1226 hdr
->offset
= cpu_to_be32(new_offset
);
1227 if (left
> conn
->max_xmit_dlength
) {
1228 hton24(hdr
->dlength
, conn
->max_xmit_dlength
);
1229 r2t
->data_count
= conn
->max_xmit_dlength
;
1231 hton24(hdr
->dlength
, left
);
1232 r2t
->data_count
= left
;
1233 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
1235 conn
->dataout_pdus_cnt
++;
1237 iscsi_buf_init_iov(&r2t
->headbuf
, (char*)hdr
,
1238 sizeof(struct iscsi_hdr
));
1240 if (sc
->use_sg
&& !iscsi_buf_left(&r2t
->sendbuf
)) {
1241 BUG_ON(tcp_ctask
->bad_sg
== r2t
->sg
);
1242 iscsi_buf_init_sg(&r2t
->sendbuf
, r2t
->sg
);
1245 iscsi_buf_init_iov(&tcp_ctask
->sendbuf
,
1246 (char*)sc
->request_buffer
+ new_offset
,
1251 iscsi_unsolicit_data_init(struct iscsi_conn
*conn
, struct iscsi_cmd_task
*ctask
)
1253 struct iscsi_tcp_cmd_task
*tcp_ctask
= ctask
->dd_data
;
1254 struct iscsi_data_task
*dtask
;
1256 dtask
= tcp_ctask
->dtask
= &tcp_ctask
->unsol_dtask
;
1257 iscsi_prep_unsolicit_data_pdu(ctask
, &dtask
->hdr
,
1258 tcp_ctask
->r2t_data_count
);
1259 iscsi_buf_init_iov(&tcp_ctask
->headbuf
, (char*)&dtask
->hdr
,
1260 sizeof(struct iscsi_hdr
));
1264 * iscsi_tcp_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
1265 * @conn: iscsi connection
1266 * @ctask: scsi command task
1270 iscsi_tcp_cmd_init(struct iscsi_cmd_task
*ctask
)
1272 struct scsi_cmnd
*sc
= ctask
->sc
;
1273 struct iscsi_tcp_cmd_task
*tcp_ctask
= ctask
->dd_data
;
1275 BUG_ON(__kfifo_len(tcp_ctask
->r2tqueue
));
1277 tcp_ctask
->sent
= 0;
1278 tcp_ctask
->sg_count
= 0;
1280 if (sc
->sc_data_direction
== DMA_TO_DEVICE
) {
1281 tcp_ctask
->xmstate
= XMSTATE_W_HDR
;
1282 tcp_ctask
->exp_r2tsn
= 0;
1283 BUG_ON(ctask
->total_length
== 0);
1286 struct scatterlist
*sg
= sc
->request_buffer
;
1288 iscsi_buf_init_sg(&tcp_ctask
->sendbuf
,
1289 &sg
[tcp_ctask
->sg_count
++]);
1291 tcp_ctask
->bad_sg
= sg
+ sc
->use_sg
;
1293 iscsi_buf_init_iov(&tcp_ctask
->sendbuf
,
1295 sc
->request_bufflen
);
1297 if (ctask
->imm_count
)
1298 tcp_ctask
->xmstate
|= XMSTATE_IMM_DATA
;
1300 tcp_ctask
->pad_count
= ctask
->total_length
& (ISCSI_PAD_LEN
-1);
1301 if (tcp_ctask
->pad_count
) {
1302 tcp_ctask
->pad_count
= ISCSI_PAD_LEN
-
1303 tcp_ctask
->pad_count
;
1304 debug_scsi("write padding %d bytes\n",
1305 tcp_ctask
->pad_count
);
1306 tcp_ctask
->xmstate
|= XMSTATE_W_PAD
;
1309 if (ctask
->unsol_count
)
1310 tcp_ctask
->xmstate
|= XMSTATE_UNS_HDR
|
1312 tcp_ctask
->r2t_data_count
= ctask
->total_length
-
1316 debug_scsi("cmd [itt %x total %d imm %d imm_data %d "
1318 ctask
->itt
, ctask
->total_length
, ctask
->imm_count
,
1319 ctask
->unsol_count
, tcp_ctask
->r2t_data_count
);
1321 tcp_ctask
->xmstate
= XMSTATE_R_HDR
;
1323 iscsi_buf_init_iov(&tcp_ctask
->headbuf
, (char*)ctask
->hdr
,
1324 sizeof(struct iscsi_hdr
));
1328 * iscsi_tcp_mtask_xmit - xmit management(immediate) task
1329 * @conn: iscsi connection
1330 * @mtask: task management task
1333 * The function can return -EAGAIN in which case caller must
1334 * call it again later, or recover. '0' return code means successful
1337 * Management xmit state machine consists of two states:
1338 * IN_PROGRESS_IMM_HEAD - PDU Header xmit in progress
1339 * IN_PROGRESS_IMM_DATA - PDU Data xmit in progress
1342 iscsi_tcp_mtask_xmit(struct iscsi_conn
*conn
, struct iscsi_mgmt_task
*mtask
)
1344 struct iscsi_tcp_mgmt_task
*tcp_mtask
= mtask
->dd_data
;
1346 debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
1347 conn
->id
, tcp_mtask
->xmstate
, mtask
->itt
);
1349 if (tcp_mtask
->xmstate
& XMSTATE_IMM_HDR
) {
1350 tcp_mtask
->xmstate
&= ~XMSTATE_IMM_HDR
;
1351 if (mtask
->data_count
)
1352 tcp_mtask
->xmstate
|= XMSTATE_IMM_DATA
;
1353 if (conn
->c_stage
!= ISCSI_CONN_INITIAL_STAGE
&&
1354 conn
->stop_stage
!= STOP_CONN_RECOVER
&&
1356 iscsi_hdr_digest(conn
, &tcp_mtask
->headbuf
,
1357 (u8
*)tcp_mtask
->hdrext
);
1358 if (iscsi_sendhdr(conn
, &tcp_mtask
->headbuf
,
1359 mtask
->data_count
)) {
1360 tcp_mtask
->xmstate
|= XMSTATE_IMM_HDR
;
1361 if (mtask
->data_count
)
1362 tcp_mtask
->xmstate
&= ~XMSTATE_IMM_DATA
;
1367 if (tcp_mtask
->xmstate
& XMSTATE_IMM_DATA
) {
1368 BUG_ON(!mtask
->data_count
);
1369 tcp_mtask
->xmstate
&= ~XMSTATE_IMM_DATA
;
1370 /* FIXME: implement.
1371 * Virtual buffer could be spreaded across multiple pages...
1374 if (iscsi_sendpage(conn
, &tcp_mtask
->sendbuf
,
1375 &mtask
->data_count
, &tcp_mtask
->sent
)) {
1376 tcp_mtask
->xmstate
|= XMSTATE_IMM_DATA
;
1379 } while (mtask
->data_count
);
1382 BUG_ON(tcp_mtask
->xmstate
!= XMSTATE_IDLE
);
1383 if (mtask
->hdr
->itt
== cpu_to_be32(ISCSI_RESERVED_TAG
)) {
1384 struct iscsi_session
*session
= conn
->session
;
1386 spin_lock_bh(&session
->lock
);
1387 list_del(&conn
->mtask
->running
);
1388 __kfifo_put(session
->mgmtpool
.queue
, (void*)&conn
->mtask
,
1390 spin_unlock_bh(&session
->lock
);
1396 handle_xmstate_r_hdr(struct iscsi_conn
*conn
,
1397 struct iscsi_tcp_cmd_task
*tcp_ctask
)
1399 tcp_ctask
->xmstate
&= ~XMSTATE_R_HDR
;
1400 if (conn
->hdrdgst_en
)
1401 iscsi_hdr_digest(conn
, &tcp_ctask
->headbuf
,
1402 (u8
*)tcp_ctask
->hdrext
);
1403 if (!iscsi_sendhdr(conn
, &tcp_ctask
->headbuf
, 0)) {
1404 BUG_ON(tcp_ctask
->xmstate
!= XMSTATE_IDLE
);
1405 return 0; /* wait for Data-In */
1407 tcp_ctask
->xmstate
|= XMSTATE_R_HDR
;
1412 handle_xmstate_w_hdr(struct iscsi_conn
*conn
,
1413 struct iscsi_cmd_task
*ctask
)
1415 struct iscsi_tcp_cmd_task
*tcp_ctask
= ctask
->dd_data
;
1417 tcp_ctask
->xmstate
&= ~XMSTATE_W_HDR
;
1418 if (conn
->hdrdgst_en
)
1419 iscsi_hdr_digest(conn
, &tcp_ctask
->headbuf
,
1420 (u8
*)tcp_ctask
->hdrext
);
1421 if (iscsi_sendhdr(conn
, &tcp_ctask
->headbuf
, ctask
->imm_count
)) {
1422 tcp_ctask
->xmstate
|= XMSTATE_W_HDR
;
1429 handle_xmstate_data_digest(struct iscsi_conn
*conn
,
1430 struct iscsi_cmd_task
*ctask
)
1432 struct iscsi_tcp_cmd_task
*tcp_ctask
= ctask
->dd_data
;
1434 tcp_ctask
->xmstate
&= ~XMSTATE_DATA_DIGEST
;
1435 debug_tcp("resent data digest 0x%x\n", tcp_ctask
->datadigest
);
1436 if (iscsi_digest_final_send(conn
, ctask
, &tcp_ctask
->immbuf
,
1437 &tcp_ctask
->datadigest
, 0)) {
1438 tcp_ctask
->xmstate
|= XMSTATE_DATA_DIGEST
;
1439 debug_tcp("resent data digest 0x%x fail!\n",
1440 tcp_ctask
->datadigest
);
1447 handle_xmstate_imm_data(struct iscsi_conn
*conn
, struct iscsi_cmd_task
*ctask
)
1449 struct iscsi_tcp_cmd_task
*tcp_ctask
= ctask
->dd_data
;
1450 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
1452 BUG_ON(!ctask
->imm_count
);
1453 tcp_ctask
->xmstate
&= ~XMSTATE_IMM_DATA
;
1455 if (conn
->datadgst_en
) {
1456 iscsi_data_digest_init(tcp_conn
, ctask
);
1457 tcp_ctask
->immdigest
= 0;
1461 if (iscsi_sendpage(conn
, &tcp_ctask
->sendbuf
, &ctask
->imm_count
,
1462 &tcp_ctask
->sent
)) {
1463 tcp_ctask
->xmstate
|= XMSTATE_IMM_DATA
;
1464 if (conn
->datadgst_en
) {
1465 crypto_digest_final(tcp_conn
->data_tx_tfm
,
1466 (u8
*)&tcp_ctask
->immdigest
);
1467 debug_tcp("tx imm sendpage fail 0x%x\n",
1468 tcp_ctask
->datadigest
);
1472 if (conn
->datadgst_en
)
1473 crypto_digest_update(tcp_conn
->data_tx_tfm
,
1474 &tcp_ctask
->sendbuf
.sg
, 1);
1476 if (!ctask
->imm_count
)
1478 iscsi_buf_init_sg(&tcp_ctask
->sendbuf
,
1479 &tcp_ctask
->sg
[tcp_ctask
->sg_count
++]);
1482 if (conn
->datadgst_en
&& !(tcp_ctask
->xmstate
& XMSTATE_W_PAD
)) {
1483 if (iscsi_digest_final_send(conn
, ctask
, &tcp_ctask
->immbuf
,
1484 &tcp_ctask
->immdigest
, 1)) {
1485 debug_tcp("sending imm digest 0x%x fail!\n",
1486 tcp_ctask
->immdigest
);
1489 debug_tcp("sending imm digest 0x%x\n", tcp_ctask
->immdigest
);
1496 handle_xmstate_uns_hdr(struct iscsi_conn
*conn
, struct iscsi_cmd_task
*ctask
)
1498 struct iscsi_tcp_cmd_task
*tcp_ctask
= ctask
->dd_data
;
1499 struct iscsi_data_task
*dtask
;
1501 tcp_ctask
->xmstate
|= XMSTATE_UNS_DATA
;
1502 if (tcp_ctask
->xmstate
& XMSTATE_UNS_INIT
) {
1503 iscsi_unsolicit_data_init(conn
, ctask
);
1504 dtask
= tcp_ctask
->dtask
;
1505 if (conn
->hdrdgst_en
)
1506 iscsi_hdr_digest(conn
, &tcp_ctask
->headbuf
,
1507 (u8
*)dtask
->hdrext
);
1508 tcp_ctask
->xmstate
&= ~XMSTATE_UNS_INIT
;
1510 if (iscsi_sendhdr(conn
, &tcp_ctask
->headbuf
, ctask
->data_count
)) {
1511 tcp_ctask
->xmstate
&= ~XMSTATE_UNS_DATA
;
1512 tcp_ctask
->xmstate
|= XMSTATE_UNS_HDR
;
1516 debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
1517 ctask
->itt
, ctask
->unsol_count
, tcp_ctask
->sent
);
1522 handle_xmstate_uns_data(struct iscsi_conn
*conn
, struct iscsi_cmd_task
*ctask
)
1524 struct iscsi_tcp_cmd_task
*tcp_ctask
= ctask
->dd_data
;
1525 struct iscsi_data_task
*dtask
= tcp_ctask
->dtask
;
1526 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
1528 BUG_ON(!ctask
->data_count
);
1529 tcp_ctask
->xmstate
&= ~XMSTATE_UNS_DATA
;
1531 if (conn
->datadgst_en
) {
1532 iscsi_data_digest_init(tcp_conn
, ctask
);
1537 int start
= tcp_ctask
->sent
;
1539 if (iscsi_sendpage(conn
, &tcp_ctask
->sendbuf
,
1540 &ctask
->data_count
, &tcp_ctask
->sent
)) {
1541 ctask
->unsol_count
-= tcp_ctask
->sent
- start
;
1542 tcp_ctask
->xmstate
|= XMSTATE_UNS_DATA
;
1543 /* will continue with this ctask later.. */
1544 if (conn
->datadgst_en
) {
1545 crypto_digest_final(tcp_conn
->data_tx_tfm
,
1546 (u8
*)&dtask
->digest
);
1547 debug_tcp("tx uns data fail 0x%x\n",
1553 BUG_ON(tcp_ctask
->sent
> ctask
->total_length
);
1554 ctask
->unsol_count
-= tcp_ctask
->sent
- start
;
1557 * XXX:we may run here with un-initial sendbuf.
1560 if (conn
->datadgst_en
&& tcp_ctask
->sent
- start
> 0)
1561 crypto_digest_update(tcp_conn
->data_tx_tfm
,
1562 &tcp_ctask
->sendbuf
.sg
, 1);
1564 if (!ctask
->data_count
)
1566 iscsi_buf_init_sg(&tcp_ctask
->sendbuf
,
1567 &tcp_ctask
->sg
[tcp_ctask
->sg_count
++]);
1569 BUG_ON(ctask
->unsol_count
< 0);
1572 * Done with the Data-Out. Next, check if we need
1573 * to send another unsolicited Data-Out.
1575 if (ctask
->unsol_count
) {
1576 if (conn
->datadgst_en
) {
1577 if (iscsi_digest_final_send(conn
, ctask
,
1579 &dtask
->digest
, 1)) {
1580 debug_tcp("send uns digest 0x%x fail\n",
1584 debug_tcp("sending uns digest 0x%x, more uns\n",
1587 tcp_ctask
->xmstate
|= XMSTATE_UNS_INIT
;
1591 if (conn
->datadgst_en
&& !(tcp_ctask
->xmstate
& XMSTATE_W_PAD
)) {
1592 if (iscsi_digest_final_send(conn
, ctask
,
1594 &dtask
->digest
, 1)) {
1595 debug_tcp("send last uns digest 0x%x fail\n",
1599 debug_tcp("sending uns digest 0x%x\n",dtask
->digest
);
1606 handle_xmstate_sol_data(struct iscsi_conn
*conn
, struct iscsi_cmd_task
*ctask
)
1608 struct iscsi_session
*session
= conn
->session
;
1609 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
1610 struct iscsi_tcp_cmd_task
*tcp_ctask
= ctask
->dd_data
;
1611 struct iscsi_r2t_info
*r2t
= tcp_ctask
->r2t
;
1612 struct iscsi_data_task
*dtask
= &r2t
->dtask
;
1615 tcp_ctask
->xmstate
&= ~XMSTATE_SOL_DATA
;
1616 tcp_ctask
->dtask
= dtask
;
1618 if (conn
->datadgst_en
) {
1619 iscsi_data_digest_init(tcp_conn
, ctask
);
1624 * send Data-Out whitnin this R2T sequence.
1626 if (!r2t
->data_count
)
1629 if (iscsi_sendpage(conn
, &r2t
->sendbuf
, &r2t
->data_count
, &r2t
->sent
)) {
1630 tcp_ctask
->xmstate
|= XMSTATE_SOL_DATA
;
1631 /* will continue with this ctask later.. */
1632 if (conn
->datadgst_en
) {
1633 crypto_digest_final(tcp_conn
->data_tx_tfm
,
1634 (u8
*)&dtask
->digest
);
1635 debug_tcp("r2t data send fail 0x%x\n", dtask
->digest
);
1640 BUG_ON(r2t
->data_count
< 0);
1641 if (conn
->datadgst_en
)
1642 crypto_digest_update(tcp_conn
->data_tx_tfm
, &r2t
->sendbuf
.sg
,
1645 if (r2t
->data_count
) {
1646 BUG_ON(ctask
->sc
->use_sg
== 0);
1647 if (!iscsi_buf_left(&r2t
->sendbuf
)) {
1648 BUG_ON(tcp_ctask
->bad_sg
== r2t
->sg
);
1649 iscsi_buf_init_sg(&r2t
->sendbuf
, r2t
->sg
);
1657 * Done with this Data-Out. Next, check if we have
1658 * to send another Data-Out for this R2T.
1660 BUG_ON(r2t
->data_length
- r2t
->sent
< 0);
1661 left
= r2t
->data_length
- r2t
->sent
;
1663 if (conn
->datadgst_en
) {
1664 if (iscsi_digest_final_send(conn
, ctask
,
1666 &dtask
->digest
, 1)) {
1667 debug_tcp("send r2t data digest 0x%x"
1668 "fail\n", dtask
->digest
);
1671 debug_tcp("r2t data send digest 0x%x\n",
1674 iscsi_solicit_data_cont(conn
, ctask
, r2t
, left
);
1675 tcp_ctask
->xmstate
|= XMSTATE_SOL_DATA
;
1676 tcp_ctask
->xmstate
&= ~XMSTATE_SOL_HDR
;
1681 * Done with this R2T. Check if there are more
1682 * outstanding R2Ts ready to be processed.
1684 BUG_ON(tcp_ctask
->r2t_data_count
- r2t
->data_length
< 0);
1685 if (conn
->datadgst_en
) {
1686 if (iscsi_digest_final_send(conn
, ctask
, &dtask
->digestbuf
,
1687 &dtask
->digest
, 1)) {
1688 debug_tcp("send last r2t data digest 0x%x"
1689 "fail\n", dtask
->digest
);
1692 debug_tcp("r2t done dout digest 0x%x\n", dtask
->digest
);
1695 tcp_ctask
->r2t_data_count
-= r2t
->data_length
;
1696 tcp_ctask
->r2t
= NULL
;
1697 spin_lock_bh(&session
->lock
);
1698 __kfifo_put(tcp_ctask
->r2tpool
.queue
, (void*)&r2t
, sizeof(void*));
1699 spin_unlock_bh(&session
->lock
);
1700 if (__kfifo_get(tcp_ctask
->r2tqueue
, (void*)&r2t
, sizeof(void*))) {
1701 tcp_ctask
->r2t
= r2t
;
1702 tcp_ctask
->xmstate
|= XMSTATE_SOL_DATA
;
1703 tcp_ctask
->xmstate
&= ~XMSTATE_SOL_HDR
;
1711 handle_xmstate_w_pad(struct iscsi_conn
*conn
, struct iscsi_cmd_task
*ctask
)
1713 struct iscsi_tcp_cmd_task
*tcp_ctask
= ctask
->dd_data
;
1714 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
1715 struct iscsi_data_task
*dtask
= tcp_ctask
->dtask
;
1718 tcp_ctask
->xmstate
&= ~XMSTATE_W_PAD
;
1719 iscsi_buf_init_iov(&tcp_ctask
->sendbuf
, (char*)&tcp_ctask
->pad
,
1720 tcp_ctask
->pad_count
);
1721 if (iscsi_sendpage(conn
, &tcp_ctask
->sendbuf
, &tcp_ctask
->pad_count
,
1723 tcp_ctask
->xmstate
|= XMSTATE_W_PAD
;
1727 if (conn
->datadgst_en
) {
1728 crypto_digest_update(tcp_conn
->data_tx_tfm
,
1729 &tcp_ctask
->sendbuf
.sg
, 1);
1732 if (iscsi_digest_final_send(conn
, ctask
,
1734 &tcp_ctask
->immdigest
, 1)) {
1735 debug_tcp("send padding digest 0x%x"
1736 "fail!\n", tcp_ctask
->immdigest
);
1739 debug_tcp("done with padding, digest 0x%x\n",
1740 tcp_ctask
->datadigest
);
1742 if (iscsi_digest_final_send(conn
, ctask
,
1744 &dtask
->digest
, 1)) {
1745 debug_tcp("send padding digest 0x%x"
1746 "fail\n", dtask
->digest
);
1749 debug_tcp("done with padding, digest 0x%x\n",
1758 iscsi_tcp_ctask_xmit(struct iscsi_conn
*conn
, struct iscsi_cmd_task
*ctask
)
1760 struct iscsi_tcp_cmd_task
*tcp_ctask
= ctask
->dd_data
;
1763 debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
1764 conn
->id
, tcp_ctask
->xmstate
, ctask
->itt
);
1767 * serialize with TMF AbortTask
1772 if (tcp_ctask
->xmstate
& XMSTATE_R_HDR
) {
1773 rc
= handle_xmstate_r_hdr(conn
, tcp_ctask
);
1777 if (tcp_ctask
->xmstate
& XMSTATE_W_HDR
) {
1778 rc
= handle_xmstate_w_hdr(conn
, ctask
);
1783 /* XXX: for data digest xmit recover */
1784 if (tcp_ctask
->xmstate
& XMSTATE_DATA_DIGEST
) {
1785 rc
= handle_xmstate_data_digest(conn
, ctask
);
1790 if (tcp_ctask
->xmstate
& XMSTATE_IMM_DATA
) {
1791 rc
= handle_xmstate_imm_data(conn
, ctask
);
1796 if (tcp_ctask
->xmstate
& XMSTATE_UNS_HDR
) {
1797 BUG_ON(!ctask
->unsol_count
);
1798 tcp_ctask
->xmstate
&= ~XMSTATE_UNS_HDR
;
1799 unsolicit_head_again
:
1800 rc
= handle_xmstate_uns_hdr(conn
, ctask
);
1805 if (tcp_ctask
->xmstate
& XMSTATE_UNS_DATA
) {
1806 rc
= handle_xmstate_uns_data(conn
, ctask
);
1808 goto unsolicit_head_again
;
1814 if (tcp_ctask
->xmstate
& XMSTATE_SOL_HDR
) {
1815 struct iscsi_r2t_info
*r2t
;
1817 tcp_ctask
->xmstate
&= ~XMSTATE_SOL_HDR
;
1818 tcp_ctask
->xmstate
|= XMSTATE_SOL_DATA
;
1819 if (!tcp_ctask
->r2t
)
1820 __kfifo_get(tcp_ctask
->r2tqueue
, (void*)&tcp_ctask
->r2t
,
1823 r2t
= tcp_ctask
->r2t
;
1824 if (conn
->hdrdgst_en
)
1825 iscsi_hdr_digest(conn
, &r2t
->headbuf
,
1826 (u8
*)r2t
->dtask
.hdrext
);
1827 if (iscsi_sendhdr(conn
, &r2t
->headbuf
, r2t
->data_count
)) {
1828 tcp_ctask
->xmstate
&= ~XMSTATE_SOL_DATA
;
1829 tcp_ctask
->xmstate
|= XMSTATE_SOL_HDR
;
1833 debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n",
1834 r2t
->solicit_datasn
- 1, ctask
->itt
, r2t
->data_count
,
1838 if (tcp_ctask
->xmstate
& XMSTATE_SOL_DATA
) {
1839 rc
= handle_xmstate_sol_data(conn
, ctask
);
1841 goto solicit_head_again
;
1848 * Last thing to check is whether we need to send write
1849 * padding. Note that we check for xmstate equality, not just the bit.
1851 if (tcp_ctask
->xmstate
== XMSTATE_W_PAD
)
1852 rc
= handle_xmstate_w_pad(conn
, ctask
);
1857 static struct iscsi_cls_conn
*
1858 iscsi_tcp_conn_create(struct iscsi_cls_session
*cls_session
, uint32_t conn_idx
)
1860 struct iscsi_conn
*conn
;
1861 struct iscsi_cls_conn
*cls_conn
;
1862 struct iscsi_tcp_conn
*tcp_conn
;
1864 cls_conn
= iscsi_conn_setup(cls_session
, conn_idx
);
1867 conn
= cls_conn
->dd_data
;
1869 * due to strange issues with iser these are not set
1870 * in iscsi_conn_setup
1872 conn
->max_recv_dlength
= DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH
;
1874 tcp_conn
= kzalloc(sizeof(*tcp_conn
), GFP_KERNEL
);
1876 goto tcp_conn_alloc_fail
;
1878 conn
->dd_data
= tcp_conn
;
1879 tcp_conn
->iscsi_conn
= conn
;
1880 tcp_conn
->in_progress
= IN_PROGRESS_WAIT_HEADER
;
1881 /* initial operational parameters */
1882 tcp_conn
->hdr_size
= sizeof(struct iscsi_hdr
);
1883 tcp_conn
->data_size
= DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH
;
1885 /* allocate initial PDU receive place holder */
1886 if (tcp_conn
->data_size
<= PAGE_SIZE
)
1887 tcp_conn
->data
= kmalloc(tcp_conn
->data_size
, GFP_KERNEL
);
1889 tcp_conn
->data
= (void*)__get_free_pages(GFP_KERNEL
,
1890 get_order(tcp_conn
->data_size
));
1891 if (!tcp_conn
->data
)
1892 goto max_recv_dlenght_alloc_fail
;
1896 max_recv_dlenght_alloc_fail
:
1898 tcp_conn_alloc_fail
:
1899 iscsi_conn_teardown(cls_conn
);
1904 iscsi_tcp_conn_destroy(struct iscsi_cls_conn
*cls_conn
)
1906 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1907 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
1910 if (conn
->hdrdgst_en
|| conn
->datadgst_en
)
1913 iscsi_conn_teardown(cls_conn
);
1915 /* now free tcp_conn */
1917 if (tcp_conn
->tx_tfm
)
1918 crypto_free_tfm(tcp_conn
->tx_tfm
);
1919 if (tcp_conn
->rx_tfm
)
1920 crypto_free_tfm(tcp_conn
->rx_tfm
);
1921 if (tcp_conn
->data_tx_tfm
)
1922 crypto_free_tfm(tcp_conn
->data_tx_tfm
);
1923 if (tcp_conn
->data_rx_tfm
)
1924 crypto_free_tfm(tcp_conn
->data_rx_tfm
);
1927 /* free conn->data, size = MaxRecvDataSegmentLength */
1928 if (tcp_conn
->data_size
<= PAGE_SIZE
)
1929 kfree(tcp_conn
->data
);
1931 free_pages((unsigned long)tcp_conn
->data
,
1932 get_order(tcp_conn
->data_size
));
1937 iscsi_tcp_conn_bind(struct iscsi_cls_session
*cls_session
,
1938 struct iscsi_cls_conn
*cls_conn
, uint64_t transport_eph
,
1941 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1942 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
1944 struct socket
*sock
;
1947 /* lookup for existing socket */
1948 sock
= sockfd_lookup((int)transport_eph
, &err
);
1950 printk(KERN_ERR
"iscsi_tcp: sockfd_lookup failed %d\n", err
);
1954 err
= iscsi_conn_bind(cls_session
, cls_conn
, is_leading
);
1958 /* bind iSCSI connection and socket */
1959 tcp_conn
->sock
= sock
;
1961 /* setup Socket parameters */
1964 sk
->sk_sndtimeo
= 15 * HZ
; /* FIXME: make it configurable */
1965 sk
->sk_allocation
= GFP_ATOMIC
;
1967 /* FIXME: disable Nagle's algorithm */
1970 * Intercept TCP callbacks for sendfile like receive
1973 conn
->recv_lock
= &sk
->sk_callback_lock
;
1974 iscsi_conn_set_callbacks(conn
);
1975 tcp_conn
->sendpage
= tcp_conn
->sock
->ops
->sendpage
;
1977 * set receive state machine into initial state
1979 tcp_conn
->in_progress
= IN_PROGRESS_WAIT_HEADER
;
1985 iscsi_tcp_cleanup_ctask(struct iscsi_conn
*conn
, struct iscsi_cmd_task
*ctask
)
1987 struct iscsi_tcp_cmd_task
*tcp_ctask
= ctask
->dd_data
;
1988 struct iscsi_r2t_info
*r2t
;
1990 /* flush ctask's r2t queues */
1991 while (__kfifo_get(tcp_ctask
->r2tqueue
, (void*)&r2t
, sizeof(void*)))
1992 __kfifo_put(tcp_ctask
->r2tpool
.queue
, (void*)&r2t
,
1995 __iscsi_ctask_cleanup(conn
, ctask
);
1999 iscsi_tcp_suspend_conn_rx(struct iscsi_conn
*conn
)
2001 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
2004 if (!tcp_conn
->sock
)
2007 sk
= tcp_conn
->sock
->sk
;
2008 write_lock_bh(&sk
->sk_callback_lock
);
2009 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
2010 write_unlock_bh(&sk
->sk_callback_lock
);
2014 iscsi_tcp_terminate_conn(struct iscsi_conn
*conn
)
2016 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
2018 if (!tcp_conn
->sock
)
2021 sock_hold(tcp_conn
->sock
->sk
);
2022 iscsi_conn_restore_callbacks(conn
);
2023 sock_put(tcp_conn
->sock
->sk
);
2025 sock_release(tcp_conn
->sock
);
2026 tcp_conn
->sock
= NULL
;
2027 conn
->recv_lock
= NULL
;
2030 /* called with host lock */
2032 iscsi_tcp_mgmt_init(struct iscsi_conn
*conn
, struct iscsi_mgmt_task
*mtask
,
2033 char *data
, uint32_t data_size
)
2035 struct iscsi_tcp_mgmt_task
*tcp_mtask
= mtask
->dd_data
;
2037 iscsi_buf_init_iov(&tcp_mtask
->headbuf
, (char*)mtask
->hdr
,
2038 sizeof(struct iscsi_hdr
));
2039 tcp_mtask
->xmstate
= XMSTATE_IMM_HDR
;
2041 if (mtask
->data_count
)
2042 iscsi_buf_init_iov(&tcp_mtask
->sendbuf
, (char*)mtask
->data
,
2047 iscsi_r2tpool_alloc(struct iscsi_session
*session
)
2053 * initialize per-task: R2T pool and xmit queue
2055 for (cmd_i
= 0; cmd_i
< session
->cmds_max
; cmd_i
++) {
2056 struct iscsi_cmd_task
*ctask
= session
->cmds
[cmd_i
];
2057 struct iscsi_tcp_cmd_task
*tcp_ctask
= ctask
->dd_data
;
2060 * pre-allocated x4 as much r2ts to handle race when
2061 * target acks DataOut faster than we data_xmit() queues
2062 * could replenish r2tqueue.
2066 if (iscsi_pool_init(&tcp_ctask
->r2tpool
, session
->max_r2t
* 4,
2067 (void***)&tcp_ctask
->r2ts
,
2068 sizeof(struct iscsi_r2t_info
))) {
2069 goto r2t_alloc_fail
;
2072 /* R2T xmit queue */
2073 tcp_ctask
->r2tqueue
= kfifo_alloc(
2074 session
->max_r2t
* 4 * sizeof(void*), GFP_KERNEL
, NULL
);
2075 if (tcp_ctask
->r2tqueue
== ERR_PTR(-ENOMEM
)) {
2076 iscsi_pool_free(&tcp_ctask
->r2tpool
,
2077 (void**)tcp_ctask
->r2ts
);
2078 goto r2t_alloc_fail
;
2085 for (i
= 0; i
< cmd_i
; i
++) {
2086 struct iscsi_cmd_task
*ctask
= session
->cmds
[i
];
2087 struct iscsi_tcp_cmd_task
*tcp_ctask
= ctask
->dd_data
;
2089 kfifo_free(tcp_ctask
->r2tqueue
);
2090 iscsi_pool_free(&tcp_ctask
->r2tpool
,
2091 (void**)tcp_ctask
->r2ts
);
2097 iscsi_r2tpool_free(struct iscsi_session
*session
)
2101 for (i
= 0; i
< session
->cmds_max
; i
++) {
2102 struct iscsi_cmd_task
*ctask
= session
->cmds
[i
];
2103 struct iscsi_tcp_cmd_task
*tcp_ctask
= ctask
->dd_data
;
2105 kfifo_free(tcp_ctask
->r2tqueue
);
2106 iscsi_pool_free(&tcp_ctask
->r2tpool
,
2107 (void**)tcp_ctask
->r2ts
);
2112 iscsi_conn_set_param(struct iscsi_cls_conn
*cls_conn
, enum iscsi_param param
,
2115 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2116 struct iscsi_session
*session
= conn
->session
;
2117 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
2120 case ISCSI_PARAM_MAX_RECV_DLENGTH
: {
2121 char *saveptr
= tcp_conn
->data
;
2122 gfp_t flags
= GFP_KERNEL
;
2124 if (tcp_conn
->data_size
>= value
) {
2125 conn
->max_recv_dlength
= value
;
2129 spin_lock_bh(&session
->lock
);
2130 if (conn
->stop_stage
== STOP_CONN_RECOVER
)
2132 spin_unlock_bh(&session
->lock
);
2134 if (value
<= PAGE_SIZE
)
2135 tcp_conn
->data
= kmalloc(value
, flags
);
2137 tcp_conn
->data
= (void*)__get_free_pages(flags
,
2139 if (tcp_conn
->data
== NULL
) {
2140 tcp_conn
->data
= saveptr
;
2143 if (tcp_conn
->data_size
<= PAGE_SIZE
)
2146 free_pages((unsigned long)saveptr
,
2147 get_order(tcp_conn
->data_size
));
2148 conn
->max_recv_dlength
= value
;
2149 tcp_conn
->data_size
= value
;
2152 case ISCSI_PARAM_MAX_XMIT_DLENGTH
:
2153 conn
->max_xmit_dlength
= value
;
2155 case ISCSI_PARAM_HDRDGST_EN
:
2156 conn
->hdrdgst_en
= value
;
2157 tcp_conn
->hdr_size
= sizeof(struct iscsi_hdr
);
2158 if (conn
->hdrdgst_en
) {
2159 tcp_conn
->hdr_size
+= sizeof(__u32
);
2160 if (!tcp_conn
->tx_tfm
)
2161 tcp_conn
->tx_tfm
= crypto_alloc_tfm("crc32c",
2163 if (!tcp_conn
->tx_tfm
)
2165 if (!tcp_conn
->rx_tfm
)
2166 tcp_conn
->rx_tfm
= crypto_alloc_tfm("crc32c",
2168 if (!tcp_conn
->rx_tfm
) {
2169 crypto_free_tfm(tcp_conn
->tx_tfm
);
2173 if (tcp_conn
->tx_tfm
)
2174 crypto_free_tfm(tcp_conn
->tx_tfm
);
2175 if (tcp_conn
->rx_tfm
)
2176 crypto_free_tfm(tcp_conn
->rx_tfm
);
2179 case ISCSI_PARAM_DATADGST_EN
:
2180 conn
->datadgst_en
= value
;
2181 if (conn
->datadgst_en
) {
2182 if (!tcp_conn
->data_tx_tfm
)
2183 tcp_conn
->data_tx_tfm
=
2184 crypto_alloc_tfm("crc32c", 0);
2185 if (!tcp_conn
->data_tx_tfm
)
2187 if (!tcp_conn
->data_rx_tfm
)
2188 tcp_conn
->data_rx_tfm
=
2189 crypto_alloc_tfm("crc32c", 0);
2190 if (!tcp_conn
->data_rx_tfm
) {
2191 crypto_free_tfm(tcp_conn
->data_tx_tfm
);
2195 if (tcp_conn
->data_tx_tfm
)
2196 crypto_free_tfm(tcp_conn
->data_tx_tfm
);
2197 if (tcp_conn
->data_rx_tfm
)
2198 crypto_free_tfm(tcp_conn
->data_rx_tfm
);
2200 tcp_conn
->sendpage
= conn
->datadgst_en
?
2201 sock_no_sendpage
: tcp_conn
->sock
->ops
->sendpage
;
2203 case ISCSI_PARAM_INITIAL_R2T_EN
:
2204 session
->initial_r2t_en
= value
;
2206 case ISCSI_PARAM_MAX_R2T
:
2207 if (session
->max_r2t
== roundup_pow_of_two(value
))
2209 iscsi_r2tpool_free(session
);
2210 session
->max_r2t
= value
;
2211 if (session
->max_r2t
& (session
->max_r2t
- 1))
2212 session
->max_r2t
= roundup_pow_of_two(session
->max_r2t
);
2213 if (iscsi_r2tpool_alloc(session
))
2216 case ISCSI_PARAM_IMM_DATA_EN
:
2217 session
->imm_data_en
= value
;
2219 case ISCSI_PARAM_FIRST_BURST
:
2220 session
->first_burst
= value
;
2222 case ISCSI_PARAM_MAX_BURST
:
2223 session
->max_burst
= value
;
2225 case ISCSI_PARAM_PDU_INORDER_EN
:
2226 session
->pdu_inorder_en
= value
;
2228 case ISCSI_PARAM_DATASEQ_INORDER_EN
:
2229 session
->dataseq_inorder_en
= value
;
2231 case ISCSI_PARAM_ERL
:
2232 session
->erl
= value
;
2234 case ISCSI_PARAM_IFMARKER_EN
:
2236 session
->ifmarker_en
= value
;
2238 case ISCSI_PARAM_OFMARKER_EN
:
2240 session
->ofmarker_en
= value
;
2242 case ISCSI_PARAM_EXP_STATSN
:
2243 conn
->exp_statsn
= value
;
2253 iscsi_session_get_param(struct iscsi_cls_session
*cls_session
,
2254 enum iscsi_param param
, uint32_t *value
)
2256 struct Scsi_Host
*shost
= iscsi_session_to_shost(cls_session
);
2257 struct iscsi_session
*session
= iscsi_hostdata(shost
->hostdata
);
2260 case ISCSI_PARAM_INITIAL_R2T_EN
:
2261 *value
= session
->initial_r2t_en
;
2263 case ISCSI_PARAM_MAX_R2T
:
2264 *value
= session
->max_r2t
;
2266 case ISCSI_PARAM_IMM_DATA_EN
:
2267 *value
= session
->imm_data_en
;
2269 case ISCSI_PARAM_FIRST_BURST
:
2270 *value
= session
->first_burst
;
2272 case ISCSI_PARAM_MAX_BURST
:
2273 *value
= session
->max_burst
;
2275 case ISCSI_PARAM_PDU_INORDER_EN
:
2276 *value
= session
->pdu_inorder_en
;
2278 case ISCSI_PARAM_DATASEQ_INORDER_EN
:
2279 *value
= session
->dataseq_inorder_en
;
2281 case ISCSI_PARAM_ERL
:
2282 *value
= session
->erl
;
2284 case ISCSI_PARAM_IFMARKER_EN
:
2285 *value
= session
->ifmarker_en
;
2287 case ISCSI_PARAM_OFMARKER_EN
:
2288 *value
= session
->ofmarker_en
;
2298 iscsi_conn_get_param(struct iscsi_cls_conn
*cls_conn
,
2299 enum iscsi_param param
, uint32_t *value
)
2301 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2302 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
2303 struct inet_sock
*inet
;
2306 case ISCSI_PARAM_MAX_RECV_DLENGTH
:
2307 *value
= conn
->max_recv_dlength
;
2309 case ISCSI_PARAM_MAX_XMIT_DLENGTH
:
2310 *value
= conn
->max_xmit_dlength
;
2312 case ISCSI_PARAM_HDRDGST_EN
:
2313 *value
= conn
->hdrdgst_en
;
2315 case ISCSI_PARAM_DATADGST_EN
:
2316 *value
= conn
->datadgst_en
;
2318 case ISCSI_PARAM_CONN_PORT
:
2319 mutex_lock(&conn
->xmitmutex
);
2320 if (!tcp_conn
->sock
) {
2321 mutex_unlock(&conn
->xmitmutex
);
2325 inet
= inet_sk(tcp_conn
->sock
->sk
);
2326 *value
= be16_to_cpu(inet
->dport
);
2327 mutex_unlock(&conn
->xmitmutex
);
2328 case ISCSI_PARAM_EXP_STATSN
:
2329 *value
= conn
->exp_statsn
;
2339 iscsi_conn_get_str_param(struct iscsi_cls_conn
*cls_conn
,
2340 enum iscsi_param param
, char *buf
)
2342 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2343 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
2345 struct inet_sock
*inet
;
2346 struct ipv6_pinfo
*np
;
2350 case ISCSI_PARAM_CONN_ADDRESS
:
2351 mutex_lock(&conn
->xmitmutex
);
2352 if (!tcp_conn
->sock
) {
2353 mutex_unlock(&conn
->xmitmutex
);
2357 sk
= tcp_conn
->sock
->sk
;
2358 if (sk
->sk_family
== PF_INET
) {
2360 len
= sprintf(buf
, "%u.%u.%u.%u\n",
2361 NIPQUAD(inet
->daddr
));
2365 "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
2368 mutex_unlock(&conn
->xmitmutex
);
2378 iscsi_conn_get_stats(struct iscsi_cls_conn
*cls_conn
, struct iscsi_stats
*stats
)
2380 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2381 struct iscsi_tcp_conn
*tcp_conn
= conn
->dd_data
;
2383 stats
->txdata_octets
= conn
->txdata_octets
;
2384 stats
->rxdata_octets
= conn
->rxdata_octets
;
2385 stats
->scsicmd_pdus
= conn
->scsicmd_pdus_cnt
;
2386 stats
->dataout_pdus
= conn
->dataout_pdus_cnt
;
2387 stats
->scsirsp_pdus
= conn
->scsirsp_pdus_cnt
;
2388 stats
->datain_pdus
= conn
->datain_pdus_cnt
;
2389 stats
->r2t_pdus
= conn
->r2t_pdus_cnt
;
2390 stats
->tmfcmd_pdus
= conn
->tmfcmd_pdus_cnt
;
2391 stats
->tmfrsp_pdus
= conn
->tmfrsp_pdus_cnt
;
2392 stats
->custom_length
= 3;
2393 strcpy(stats
->custom
[0].desc
, "tx_sendpage_failures");
2394 stats
->custom
[0].value
= tcp_conn
->sendpage_failures_cnt
;
2395 strcpy(stats
->custom
[1].desc
, "rx_discontiguous_hdr");
2396 stats
->custom
[1].value
= tcp_conn
->discontiguous_hdr_cnt
;
2397 strcpy(stats
->custom
[2].desc
, "eh_abort_cnt");
2398 stats
->custom
[2].value
= conn
->eh_abort_cnt
;
2401 static struct iscsi_cls_session
*
2402 iscsi_tcp_session_create(struct iscsi_transport
*iscsit
,
2403 struct scsi_transport_template
*scsit
,
2404 uint32_t initial_cmdsn
, uint32_t *hostno
)
2406 struct iscsi_cls_session
*cls_session
;
2407 struct iscsi_session
*session
;
2411 cls_session
= iscsi_session_setup(iscsit
, scsit
,
2412 sizeof(struct iscsi_tcp_cmd_task
),
2413 sizeof(struct iscsi_tcp_mgmt_task
),
2414 initial_cmdsn
, &hn
);
2419 session
= class_to_transport_session(cls_session
);
2420 for (cmd_i
= 0; cmd_i
< session
->cmds_max
; cmd_i
++) {
2421 struct iscsi_cmd_task
*ctask
= session
->cmds
[cmd_i
];
2422 struct iscsi_tcp_cmd_task
*tcp_ctask
= ctask
->dd_data
;
2424 ctask
->hdr
= &tcp_ctask
->hdr
;
2427 for (cmd_i
= 0; cmd_i
< session
->mgmtpool_max
; cmd_i
++) {
2428 struct iscsi_mgmt_task
*mtask
= session
->mgmt_cmds
[cmd_i
];
2429 struct iscsi_tcp_mgmt_task
*tcp_mtask
= mtask
->dd_data
;
2431 mtask
->hdr
= &tcp_mtask
->hdr
;
2434 if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session
)))
2435 goto r2tpool_alloc_fail
;
2440 iscsi_session_teardown(cls_session
);
2444 static void iscsi_tcp_session_destroy(struct iscsi_cls_session
*cls_session
)
2446 iscsi_r2tpool_free(class_to_transport_session(cls_session
));
2447 iscsi_session_teardown(cls_session
);
2450 static struct scsi_host_template iscsi_sht
= {
2451 .name
= "iSCSI Initiator over TCP/IP, v"
2453 .queuecommand
= iscsi_queuecommand
,
2454 .change_queue_depth
= iscsi_change_queue_depth
,
2455 .can_queue
= ISCSI_XMIT_CMDS_MAX
- 1,
2456 .sg_tablesize
= ISCSI_SG_TABLESIZE
,
2457 .cmd_per_lun
= ISCSI_DEF_CMD_PER_LUN
,
2458 .eh_abort_handler
= iscsi_eh_abort
,
2459 .eh_host_reset_handler
= iscsi_eh_host_reset
,
2460 .use_clustering
= DISABLE_CLUSTERING
,
2461 .proc_name
= "iscsi_tcp",
2465 static struct iscsi_transport iscsi_tcp_transport
= {
2466 .owner
= THIS_MODULE
,
2468 .caps
= CAP_RECOVERY_L0
| CAP_MULTI_R2T
| CAP_HDRDGST
2470 .param_mask
= ISCSI_MAX_RECV_DLENGTH
|
2471 ISCSI_MAX_XMIT_DLENGTH
|
2474 ISCSI_INITIAL_R2T_EN
|
2479 ISCSI_PDU_INORDER_EN
|
2480 ISCSI_DATASEQ_INORDER_EN
|
2483 ISCSI_CONN_ADDRESS
|
2485 .host_template
= &iscsi_sht
,
2486 .conndata_size
= sizeof(struct iscsi_conn
),
2488 .max_cmd_len
= ISCSI_TCP_MAX_CMD_LEN
,
2489 /* session management */
2490 .create_session
= iscsi_tcp_session_create
,
2491 .destroy_session
= iscsi_tcp_session_destroy
,
2492 /* connection management */
2493 .create_conn
= iscsi_tcp_conn_create
,
2494 .bind_conn
= iscsi_tcp_conn_bind
,
2495 .destroy_conn
= iscsi_tcp_conn_destroy
,
2496 .set_param
= iscsi_conn_set_param
,
2497 .get_conn_param
= iscsi_conn_get_param
,
2498 .get_conn_str_param
= iscsi_conn_get_str_param
,
2499 .get_session_param
= iscsi_session_get_param
,
2500 .start_conn
= iscsi_conn_start
,
2501 .stop_conn
= iscsi_conn_stop
,
2502 /* these are called as part of conn recovery */
2503 .suspend_conn_recv
= iscsi_tcp_suspend_conn_rx
,
2504 .terminate_conn
= iscsi_tcp_terminate_conn
,
2506 .send_pdu
= iscsi_conn_send_pdu
,
2507 .get_stats
= iscsi_conn_get_stats
,
2508 .init_cmd_task
= iscsi_tcp_cmd_init
,
2509 .init_mgmt_task
= iscsi_tcp_mgmt_init
,
2510 .xmit_cmd_task
= iscsi_tcp_ctask_xmit
,
2511 .xmit_mgmt_task
= iscsi_tcp_mtask_xmit
,
2512 .cleanup_cmd_task
= iscsi_tcp_cleanup_ctask
,
2514 .session_recovery_timedout
= iscsi_session_recovery_timedout
,
2518 iscsi_tcp_init(void)
2520 if (iscsi_max_lun
< 1) {
2521 printk(KERN_ERR
"iscsi_tcp: Invalid max_lun value of %u\n",
2525 iscsi_tcp_transport
.max_lun
= iscsi_max_lun
;
2527 if (!iscsi_register_transport(&iscsi_tcp_transport
))
2534 iscsi_tcp_exit(void)
2536 iscsi_unregister_transport(&iscsi_tcp_transport
);
2539 module_init(iscsi_tcp_init
);
2540 module_exit(iscsi_tcp_exit
);