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>
38 #include <scsi/scsi_cmnd.h>
39 #include <scsi/scsi_host.h>
40 #include <scsi/scsi.h>
41 #include <scsi/scsi_transport_iscsi.h>
43 #include "iscsi_tcp.h"
45 MODULE_AUTHOR("Dmitry Yusupov <dmitry_yus@yahoo.com>, "
46 "Alex Aizman <itn780@yahoo.com>");
47 MODULE_DESCRIPTION("iSCSI/TCP data-path");
48 MODULE_LICENSE("GPL");
49 /* #define DEBUG_TCP */
53 #define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
55 #define debug_tcp(fmt...)
65 static unsigned int iscsi_max_lun = 512;
66 module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
69 iscsi_buf_init_iov(struct iscsi_buf *ibuf, char *vbuf, int size)
71 ibuf->sg.page = virt_to_page(vbuf);
72 ibuf->sg.offset = offset_in_page(vbuf);
73 ibuf->sg.length = size;
75 ibuf->use_sendmsg = 1;
79 iscsi_buf_init_sg(struct iscsi_buf *ibuf, struct scatterlist *sg)
81 ibuf->sg.page = sg->page;
82 ibuf->sg.offset = sg->offset;
83 ibuf->sg.length = sg->length;
85 * Fastpath: sg element fits into single page
87 if (sg->length + sg->offset <= PAGE_SIZE && !PageSlab(sg->page))
88 ibuf->use_sendmsg = 0;
90 ibuf->use_sendmsg = 1;
95 iscsi_buf_left(struct iscsi_buf *ibuf)
99 rc = ibuf->sg.length - ibuf->sent;
105 iscsi_hdr_digest(struct iscsi_conn *conn, struct iscsi_buf *buf,
108 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
110 crypto_hash_digest(&tcp_conn->tx_hash, &buf->sg, buf->sg.length, crc);
111 buf->sg.length += sizeof(u32);
115 iscsi_hdr_extract(struct iscsi_tcp_conn *tcp_conn)
117 struct sk_buff *skb = tcp_conn->in.skb;
119 tcp_conn->in.zero_copy_hdr = 0;
121 if (tcp_conn->in.copy >= tcp_conn->hdr_size &&
122 tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER) {
124 * Zero-copy PDU Header: using connection context
125 * to store header pointer.
127 if (skb_shinfo(skb)->frag_list == NULL &&
128 !skb_shinfo(skb)->nr_frags) {
129 tcp_conn->in.hdr = (struct iscsi_hdr *)
130 ((char*)skb->data + tcp_conn->in.offset);
131 tcp_conn->in.zero_copy_hdr = 1;
133 /* ignoring return code since we checked
135 skb_copy_bits(skb, tcp_conn->in.offset,
136 &tcp_conn->hdr, tcp_conn->hdr_size);
137 tcp_conn->in.hdr = &tcp_conn->hdr;
139 tcp_conn->in.offset += tcp_conn->hdr_size;
140 tcp_conn->in.copy -= tcp_conn->hdr_size;
146 * PDU header scattered across SKB's,
147 * copying it... This'll happen quite rarely.
150 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER)
151 tcp_conn->in.hdr_offset = 0;
153 hdr_remains = tcp_conn->hdr_size - tcp_conn->in.hdr_offset;
154 BUG_ON(hdr_remains <= 0);
156 copylen = min(tcp_conn->in.copy, hdr_remains);
157 skb_copy_bits(skb, tcp_conn->in.offset,
158 (char*)&tcp_conn->hdr + tcp_conn->in.hdr_offset,
161 debug_tcp("PDU gather offset %d bytes %d in.offset %d "
162 "in.copy %d\n", tcp_conn->in.hdr_offset, copylen,
163 tcp_conn->in.offset, tcp_conn->in.copy);
165 tcp_conn->in.offset += copylen;
166 tcp_conn->in.copy -= copylen;
167 if (copylen < hdr_remains) {
168 tcp_conn->in_progress = IN_PROGRESS_HEADER_GATHER;
169 tcp_conn->in.hdr_offset += copylen;
172 tcp_conn->in.hdr = &tcp_conn->hdr;
173 tcp_conn->discontiguous_hdr_cnt++;
174 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
181 * must be called with session lock
184 iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
186 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
187 struct iscsi_r2t_info *r2t;
188 struct scsi_cmnd *sc;
190 /* flush ctask's r2t queues */
191 while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
192 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
194 debug_scsi("iscsi_tcp_cleanup_ctask pending r2t dropped\n");
201 tcp_ctask->xmstate = XMSTATE_IDLE;
202 tcp_ctask->r2t = NULL;
206 * iscsi_data_rsp - SCSI Data-In Response processing
207 * @conn: iscsi connection
208 * @ctask: scsi command task
211 iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
213 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
214 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
215 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
216 struct iscsi_session *session = conn->session;
217 struct scsi_cmnd *sc = ctask->sc;
218 int datasn = be32_to_cpu(rhdr->datasn);
220 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
222 * setup Data-In byte counter (gets decremented..)
224 ctask->data_count = tcp_conn->in.datalen;
226 if (tcp_conn->in.datalen == 0)
229 if (tcp_ctask->exp_datasn != datasn) {
230 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->datasn(%d)\n",
231 __FUNCTION__, tcp_ctask->exp_datasn, datasn);
232 return ISCSI_ERR_DATASN;
235 tcp_ctask->exp_datasn++;
237 tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
238 if (tcp_ctask->data_offset + tcp_conn->in.datalen > sc->request_bufflen) {
239 debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
240 __FUNCTION__, tcp_ctask->data_offset,
241 tcp_conn->in.datalen, sc->request_bufflen);
242 return ISCSI_ERR_DATA_OFFSET;
245 if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
246 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
247 if (rhdr->flags & ISCSI_FLAG_DATA_UNDERFLOW) {
248 int res_count = be32_to_cpu(rhdr->residual_count);
251 res_count <= sc->request_bufflen) {
252 sc->resid = res_count;
253 sc->result = (DID_OK << 16) | rhdr->cmd_status;
255 sc->result = (DID_BAD_TARGET << 16) |
257 } else if (rhdr->flags & ISCSI_FLAG_DATA_OVERFLOW) {
258 sc->resid = be32_to_cpu(rhdr->residual_count);
259 sc->result = (DID_OK << 16) | rhdr->cmd_status;
261 sc->result = (DID_OK << 16) | rhdr->cmd_status;
264 conn->datain_pdus_cnt++;
269 * iscsi_solicit_data_init - initialize first Data-Out
270 * @conn: iscsi connection
271 * @ctask: scsi command task
275 * Initialize first Data-Out within this R2T sequence and finds
276 * proper data_offset within this SCSI command.
278 * This function is called with connection lock taken.
281 iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
282 struct iscsi_r2t_info *r2t)
284 struct iscsi_data *hdr;
285 struct scsi_cmnd *sc = ctask->sc;
287 hdr = &r2t->dtask.hdr;
288 memset(hdr, 0, sizeof(struct iscsi_data));
290 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
291 r2t->solicit_datasn++;
292 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
293 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
294 hdr->itt = ctask->hdr->itt;
295 hdr->exp_statsn = r2t->exp_statsn;
296 hdr->offset = cpu_to_be32(r2t->data_offset);
297 if (r2t->data_length > conn->max_xmit_dlength) {
298 hton24(hdr->dlength, conn->max_xmit_dlength);
299 r2t->data_count = conn->max_xmit_dlength;
302 hton24(hdr->dlength, r2t->data_length);
303 r2t->data_count = r2t->data_length;
304 hdr->flags = ISCSI_FLAG_CMD_FINAL;
306 conn->dataout_pdus_cnt++;
310 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
311 sizeof(struct iscsi_hdr));
315 struct scatterlist *sg = sc->request_buffer;
318 for (i = 0; i < sc->use_sg; i++, sg += 1) {
319 /* FIXME: prefetch ? */
320 if (sg_count + sg->length > r2t->data_offset) {
325 /* offset within this page */
326 page_offset = r2t->data_offset - sg_count;
328 /* fill in this buffer */
329 iscsi_buf_init_sg(&r2t->sendbuf, sg);
330 r2t->sendbuf.sg.offset += page_offset;
331 r2t->sendbuf.sg.length -= page_offset;
333 /* xmit logic will continue with next one */
337 sg_count += sg->length;
339 BUG_ON(r2t->sg == NULL);
341 iscsi_buf_init_iov(&r2t->sendbuf,
342 (char*)sc->request_buffer + r2t->data_offset,
349 * iscsi_r2t_rsp - iSCSI R2T Response processing
350 * @conn: iscsi connection
351 * @ctask: scsi command task
354 iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
356 struct iscsi_r2t_info *r2t;
357 struct iscsi_session *session = conn->session;
358 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
359 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
360 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
361 int r2tsn = be32_to_cpu(rhdr->r2tsn);
364 if (tcp_conn->in.datalen) {
365 printk(KERN_ERR "iscsi_tcp: invalid R2t with datalen %d\n",
366 tcp_conn->in.datalen);
367 return ISCSI_ERR_DATALEN;
370 if (tcp_ctask->exp_datasn != r2tsn){
371 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
372 __FUNCTION__, tcp_ctask->exp_datasn, r2tsn);
373 return ISCSI_ERR_R2TSN;
376 /* fill-in new R2T associated with the task */
377 spin_lock(&session->lock);
378 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
380 if (!ctask->sc || ctask->mtask ||
381 session->state != ISCSI_STATE_LOGGED_IN) {
382 printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in "
383 "recovery...\n", ctask->itt);
384 spin_unlock(&session->lock);
388 rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
391 r2t->exp_statsn = rhdr->statsn;
392 r2t->data_length = be32_to_cpu(rhdr->data_length);
393 if (r2t->data_length == 0) {
394 printk(KERN_ERR "iscsi_tcp: invalid R2T with zero data len\n");
395 spin_unlock(&session->lock);
396 return ISCSI_ERR_DATALEN;
399 if (r2t->data_length > session->max_burst)
400 debug_scsi("invalid R2T with data len %u and max burst %u."
401 "Attempting to execute request.\n",
402 r2t->data_length, session->max_burst);
404 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
405 if (r2t->data_offset + r2t->data_length > ctask->sc->request_bufflen) {
406 spin_unlock(&session->lock);
407 printk(KERN_ERR "iscsi_tcp: invalid R2T with data len %u at "
408 "offset %u and total length %d\n", r2t->data_length,
409 r2t->data_offset, ctask->sc->request_bufflen);
410 return ISCSI_ERR_DATALEN;
413 r2t->ttt = rhdr->ttt; /* no flip */
414 r2t->solicit_datasn = 0;
416 iscsi_solicit_data_init(conn, ctask, r2t);
418 tcp_ctask->exp_datasn = r2tsn + 1;
419 __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
420 tcp_ctask->xmstate |= XMSTATE_SOL_HDR_INIT;
421 list_move_tail(&ctask->running, &conn->xmitqueue);
423 scsi_queue_work(session->host, &conn->xmitwork);
424 conn->r2t_pdus_cnt++;
425 spin_unlock(&session->lock);
431 iscsi_tcp_hdr_recv(struct iscsi_conn *conn)
433 int rc = 0, opcode, ahslen;
434 struct iscsi_hdr *hdr;
435 struct iscsi_session *session = conn->session;
436 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
437 uint32_t cdgst, rdgst = 0, itt;
439 hdr = tcp_conn->in.hdr;
441 /* verify PDU length */
442 tcp_conn->in.datalen = ntoh24(hdr->dlength);
443 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
444 printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
445 tcp_conn->in.datalen, conn->max_recv_dlength);
446 return ISCSI_ERR_DATALEN;
448 tcp_conn->data_copied = 0;
451 ahslen = hdr->hlength << 2;
452 tcp_conn->in.offset += ahslen;
453 tcp_conn->in.copy -= ahslen;
454 if (tcp_conn->in.copy < 0) {
455 printk(KERN_ERR "iscsi_tcp: can't handle AHS with length "
456 "%d bytes\n", ahslen);
457 return ISCSI_ERR_AHSLEN;
460 /* calculate read padding */
461 tcp_conn->in.padding = tcp_conn->in.datalen & (ISCSI_PAD_LEN-1);
462 if (tcp_conn->in.padding) {
463 tcp_conn->in.padding = ISCSI_PAD_LEN - tcp_conn->in.padding;
464 debug_scsi("read padding %d bytes\n", tcp_conn->in.padding);
467 if (conn->hdrdgst_en) {
468 struct scatterlist sg;
470 sg_init_one(&sg, (u8 *)hdr,
471 sizeof(struct iscsi_hdr) + ahslen);
472 crypto_hash_digest(&tcp_conn->rx_hash, &sg, sg.length,
474 rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) +
476 if (cdgst != rdgst) {
477 printk(KERN_ERR "iscsi_tcp: hdrdgst error "
478 "recv 0x%x calc 0x%x\n", rdgst, cdgst);
479 return ISCSI_ERR_HDR_DGST;
483 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
484 /* verify itt (itt encoding: age+cid+itt) */
485 rc = iscsi_verify_itt(conn, hdr, &itt);
486 if (rc == ISCSI_ERR_NO_SCSI_CMD) {
487 tcp_conn->in.datalen = 0; /* force drop */
492 debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n",
493 opcode, tcp_conn->in.offset, tcp_conn->in.copy,
494 ahslen, tcp_conn->in.datalen);
497 case ISCSI_OP_SCSI_DATA_IN:
498 tcp_conn->in.ctask = session->cmds[itt];
499 rc = iscsi_data_rsp(conn, tcp_conn->in.ctask);
503 case ISCSI_OP_SCSI_CMD_RSP:
504 tcp_conn->in.ctask = session->cmds[itt];
505 if (tcp_conn->in.datalen)
508 spin_lock(&session->lock);
509 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
510 spin_unlock(&session->lock);
513 tcp_conn->in.ctask = session->cmds[itt];
515 rc = ISCSI_ERR_AHSLEN;
516 else if (tcp_conn->in.ctask->sc->sc_data_direction ==
518 rc = iscsi_r2t_rsp(conn, tcp_conn->in.ctask);
520 rc = ISCSI_ERR_PROTO;
522 case ISCSI_OP_LOGIN_RSP:
523 case ISCSI_OP_TEXT_RSP:
524 case ISCSI_OP_REJECT:
525 case ISCSI_OP_ASYNC_EVENT:
527 * It is possible that we could get a PDU with a buffer larger
528 * than 8K, but there are no targets that currently do this.
529 * For now we fail until we find a vendor that needs it
531 if (ISCSI_DEF_MAX_RECV_SEG_LEN <
532 tcp_conn->in.datalen) {
533 printk(KERN_ERR "iscsi_tcp: received buffer of len %u "
534 "but conn buffer is only %u (opcode %0x)\n",
535 tcp_conn->in.datalen,
536 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
537 rc = ISCSI_ERR_PROTO;
541 if (tcp_conn->in.datalen)
544 case ISCSI_OP_LOGOUT_RSP:
545 case ISCSI_OP_NOOP_IN:
546 case ISCSI_OP_SCSI_TMFUNC_RSP:
547 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
550 rc = ISCSI_ERR_BAD_OPCODE;
558 * if we did zero copy for the header but we will need multiple
559 * skbs to complete the command then we have to copy the header
562 if (tcp_conn->in.zero_copy_hdr && tcp_conn->in.copy <=
563 (tcp_conn->in.datalen + tcp_conn->in.padding +
564 (conn->datadgst_en ? 4 : 0))) {
565 debug_tcp("Copying header for later use. in.copy %d in.datalen"
566 " %d\n", tcp_conn->in.copy, tcp_conn->in.datalen);
567 memcpy(&tcp_conn->hdr, tcp_conn->in.hdr,
568 sizeof(struct iscsi_hdr));
569 tcp_conn->in.hdr = &tcp_conn->hdr;
570 tcp_conn->in.zero_copy_hdr = 0;
576 * iscsi_ctask_copy - copy skb bits to the destanation cmd task
577 * @conn: iscsi tcp connection
578 * @ctask: scsi command task
579 * @buf: buffer to copy to
580 * @buf_size: size of buffer
581 * @offset: offset within the buffer
584 * The function calls skb_copy_bits() and updates per-connection and
585 * per-cmd byte counters.
587 * Read counters (in bytes):
589 * conn->in.offset offset within in progress SKB
590 * conn->in.copy left to copy from in progress SKB
592 * conn->in.copied copied already from in progress SKB
593 * conn->data_copied copied already from in progress buffer
594 * ctask->sent total bytes sent up to the MidLayer
595 * ctask->data_count left to copy from in progress Data-In
596 * buf_left left to copy from in progress buffer
599 iscsi_ctask_copy(struct iscsi_tcp_conn *tcp_conn, struct iscsi_cmd_task *ctask,
600 void *buf, int buf_size, int offset)
602 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
603 int buf_left = buf_size - (tcp_conn->data_copied + offset);
604 unsigned size = min(tcp_conn->in.copy, buf_left);
607 size = min(size, ctask->data_count);
609 debug_tcp("ctask_copy %d bytes at offset %d copied %d\n",
610 size, tcp_conn->in.offset, tcp_conn->in.copied);
613 BUG_ON(tcp_ctask->sent + size > ctask->sc->request_bufflen);
615 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
616 (char*)buf + (offset + tcp_conn->data_copied), size);
617 /* must fit into skb->len */
620 tcp_conn->in.offset += size;
621 tcp_conn->in.copy -= size;
622 tcp_conn->in.copied += size;
623 tcp_conn->data_copied += size;
624 tcp_ctask->sent += size;
625 ctask->data_count -= size;
627 BUG_ON(tcp_conn->in.copy < 0);
628 BUG_ON(ctask->data_count < 0);
630 if (buf_size != (tcp_conn->data_copied + offset)) {
631 if (!ctask->data_count) {
632 BUG_ON(buf_size - tcp_conn->data_copied < 0);
633 /* done with this PDU */
634 return buf_size - tcp_conn->data_copied;
639 /* done with this buffer or with both - PDU and buffer */
640 tcp_conn->data_copied = 0;
645 * iscsi_tcp_copy - copy skb bits to the destanation buffer
646 * @conn: iscsi tcp connection
649 * The function calls skb_copy_bits() and updates per-connection
653 iscsi_tcp_copy(struct iscsi_conn *conn, int buf_size)
655 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
656 int buf_left = buf_size - tcp_conn->data_copied;
657 int size = min(tcp_conn->in.copy, buf_left);
660 debug_tcp("tcp_copy %d bytes at offset %d copied %d\n",
661 size, tcp_conn->in.offset, tcp_conn->data_copied);
664 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
665 (char*)conn->data + tcp_conn->data_copied, size);
668 tcp_conn->in.offset += size;
669 tcp_conn->in.copy -= size;
670 tcp_conn->in.copied += size;
671 tcp_conn->data_copied += size;
673 if (buf_size != tcp_conn->data_copied)
680 partial_sg_digest_update(struct hash_desc *desc, struct scatterlist *sg,
681 int offset, int length)
683 struct scatterlist temp;
685 memcpy(&temp, sg, sizeof(struct scatterlist));
686 temp.offset = offset;
687 temp.length = length;
688 crypto_hash_update(desc, &temp, length);
692 iscsi_recv_digest_update(struct iscsi_tcp_conn *tcp_conn, char* buf, int len)
694 struct scatterlist tmp;
696 sg_init_one(&tmp, buf, len);
697 crypto_hash_update(&tcp_conn->rx_hash, &tmp, len);
700 static int iscsi_scsi_data_in(struct iscsi_conn *conn)
702 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
703 struct iscsi_cmd_task *ctask = tcp_conn->in.ctask;
704 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
705 struct scsi_cmnd *sc = ctask->sc;
706 struct scatterlist *sg;
707 int i, offset, rc = 0;
709 BUG_ON((void*)ctask != sc->SCp.ptr);
712 * copying Data-In into the Scsi_Cmnd
715 i = ctask->data_count;
716 rc = iscsi_ctask_copy(tcp_conn, ctask, sc->request_buffer,
718 tcp_ctask->data_offset);
721 if (conn->datadgst_en)
722 iscsi_recv_digest_update(tcp_conn, sc->request_buffer,
728 offset = tcp_ctask->data_offset;
729 sg = sc->request_buffer;
731 if (tcp_ctask->data_offset)
732 for (i = 0; i < tcp_ctask->sg_count; i++)
733 offset -= sg[i].length;
734 /* we've passed through partial sg*/
738 for (i = tcp_ctask->sg_count; i < sc->use_sg; i++) {
741 dest = kmap_atomic(sg[i].page, KM_SOFTIRQ0);
742 rc = iscsi_ctask_copy(tcp_conn, ctask, dest + sg[i].offset,
743 sg[i].length, offset);
744 kunmap_atomic(dest, KM_SOFTIRQ0);
746 /* continue with the next SKB/PDU */
749 if (conn->datadgst_en) {
753 &sg[i], sg[i].length);
755 partial_sg_digest_update(
758 sg[i].offset + offset,
759 sg[i].length - offset);
762 tcp_ctask->sg_count++;
765 if (!ctask->data_count) {
766 if (rc && conn->datadgst_en)
768 * data-in is complete, but buffer not...
770 partial_sg_digest_update(&tcp_conn->rx_hash,
778 if (!tcp_conn->in.copy)
781 BUG_ON(ctask->data_count);
784 /* check for non-exceptional status */
785 if (tcp_conn->in.hdr->flags & ISCSI_FLAG_DATA_STATUS) {
786 debug_scsi("done [sc %lx res %d itt 0x%x flags 0x%x]\n",
787 (long)sc, sc->result, ctask->itt,
788 tcp_conn->in.hdr->flags);
789 spin_lock(&conn->session->lock);
790 __iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
791 spin_unlock(&conn->session->lock);
798 iscsi_data_recv(struct iscsi_conn *conn)
800 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
803 opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
805 case ISCSI_OP_SCSI_DATA_IN:
806 rc = iscsi_scsi_data_in(conn);
808 case ISCSI_OP_SCSI_CMD_RSP:
809 case ISCSI_OP_TEXT_RSP:
810 case ISCSI_OP_LOGIN_RSP:
811 case ISCSI_OP_ASYNC_EVENT:
812 case ISCSI_OP_REJECT:
814 * Collect data segment to the connection's data
817 if (iscsi_tcp_copy(conn, tcp_conn->in.datalen)) {
822 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, conn->data,
823 tcp_conn->in.datalen);
824 if (!rc && conn->datadgst_en && opcode != ISCSI_OP_LOGIN_RSP)
825 iscsi_recv_digest_update(tcp_conn, conn->data,
826 tcp_conn->in.datalen);
836 * iscsi_tcp_data_recv - TCP receive in sendfile fashion
837 * @rd_desc: read descriptor
838 * @skb: socket buffer
839 * @offset: offset in skb
840 * @len: skb->len - offset
843 iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
844 unsigned int offset, size_t len)
847 struct iscsi_conn *conn = rd_desc->arg.data;
848 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
850 char pad[ISCSI_PAD_LEN];
851 struct scatterlist sg;
854 * Save current SKB and its offset in the corresponding
855 * connection context.
857 tcp_conn->in.copy = skb->len - offset;
858 tcp_conn->in.offset = offset;
859 tcp_conn->in.skb = skb;
860 tcp_conn->in.len = tcp_conn->in.copy;
861 BUG_ON(tcp_conn->in.copy <= 0);
862 debug_tcp("in %d bytes\n", tcp_conn->in.copy);
865 tcp_conn->in.copied = 0;
868 if (unlikely(conn->suspend_rx)) {
869 debug_tcp("conn %d Rx suspended!\n", conn->id);
873 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER ||
874 tcp_conn->in_progress == IN_PROGRESS_HEADER_GATHER) {
875 rc = iscsi_hdr_extract(tcp_conn);
880 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
886 * Verify and process incoming PDU header.
888 rc = iscsi_tcp_hdr_recv(conn);
889 if (!rc && tcp_conn->in.datalen) {
890 if (conn->datadgst_en)
891 crypto_hash_init(&tcp_conn->rx_hash);
892 tcp_conn->in_progress = IN_PROGRESS_DATA_RECV;
894 iscsi_conn_failure(conn, rc);
899 if (tcp_conn->in_progress == IN_PROGRESS_DDIGEST_RECV &&
901 uint32_t recv_digest;
903 debug_tcp("extra data_recv offset %d copy %d\n",
904 tcp_conn->in.offset, tcp_conn->in.copy);
906 if (!tcp_conn->data_copied) {
907 if (tcp_conn->in.padding) {
908 debug_tcp("padding -> %d\n",
909 tcp_conn->in.padding);
910 memset(pad, 0, tcp_conn->in.padding);
911 sg_init_one(&sg, pad, tcp_conn->in.padding);
912 crypto_hash_update(&tcp_conn->rx_hash,
915 crypto_hash_final(&tcp_conn->rx_hash,
916 (u8 *) &tcp_conn->in.datadgst);
917 debug_tcp("rx digest 0x%x\n", tcp_conn->in.datadgst);
920 rc = iscsi_tcp_copy(conn, sizeof(uint32_t));
924 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
928 memcpy(&recv_digest, conn->data, sizeof(uint32_t));
929 if (recv_digest != tcp_conn->in.datadgst) {
930 debug_tcp("iscsi_tcp: data digest error!"
931 "0x%x != 0x%x\n", recv_digest,
932 tcp_conn->in.datadgst);
933 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
936 debug_tcp("iscsi_tcp: data digest match!"
937 "0x%x == 0x%x\n", recv_digest,
938 tcp_conn->in.datadgst);
939 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
943 if (tcp_conn->in_progress == IN_PROGRESS_DATA_RECV &&
945 debug_tcp("data_recv offset %d copy %d\n",
946 tcp_conn->in.offset, tcp_conn->in.copy);
948 rc = iscsi_data_recv(conn);
952 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
956 if (tcp_conn->in.padding)
957 tcp_conn->in_progress = IN_PROGRESS_PAD_RECV;
958 else if (conn->datadgst_en)
959 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
961 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
962 tcp_conn->data_copied = 0;
965 if (tcp_conn->in_progress == IN_PROGRESS_PAD_RECV &&
967 int copylen = min(tcp_conn->in.padding - tcp_conn->data_copied,
970 tcp_conn->in.copy -= copylen;
971 tcp_conn->in.offset += copylen;
972 tcp_conn->data_copied += copylen;
974 if (tcp_conn->data_copied != tcp_conn->in.padding)
975 tcp_conn->in_progress = IN_PROGRESS_PAD_RECV;
976 else if (conn->datadgst_en)
977 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
979 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
980 tcp_conn->data_copied = 0;
983 debug_tcp("f, processed %d from out of %d padding %d\n",
984 tcp_conn->in.offset - offset, (int)len, tcp_conn->in.padding);
985 BUG_ON(tcp_conn->in.offset - offset > len);
987 if (tcp_conn->in.offset - offset != len) {
988 debug_tcp("continue to process %d bytes\n",
989 (int)len - (tcp_conn->in.offset - offset));
994 processed = tcp_conn->in.offset - offset;
995 BUG_ON(processed == 0);
999 processed = tcp_conn->in.offset - offset;
1000 debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n",
1001 processed, (int)len, (int)rd_desc->count);
1002 BUG_ON(processed == 0);
1003 BUG_ON(processed > len);
1005 conn->rxdata_octets += processed;
1010 iscsi_tcp_data_ready(struct sock *sk, int flag)
1012 struct iscsi_conn *conn = sk->sk_user_data;
1013 read_descriptor_t rd_desc;
1015 read_lock(&sk->sk_callback_lock);
1018 * Use rd_desc to pass 'conn' to iscsi_tcp_data_recv.
1019 * We set count to 1 because we want the network layer to
1020 * hand us all the skbs that are available. iscsi_tcp_data_recv
1021 * handled pdus that cross buffers or pdus that still need data.
1023 rd_desc.arg.data = conn;
1025 tcp_read_sock(sk, &rd_desc, iscsi_tcp_data_recv);
1027 read_unlock(&sk->sk_callback_lock);
1031 iscsi_tcp_state_change(struct sock *sk)
1033 struct iscsi_tcp_conn *tcp_conn;
1034 struct iscsi_conn *conn;
1035 struct iscsi_session *session;
1036 void (*old_state_change)(struct sock *);
1038 read_lock(&sk->sk_callback_lock);
1040 conn = (struct iscsi_conn*)sk->sk_user_data;
1041 session = conn->session;
1043 if ((sk->sk_state == TCP_CLOSE_WAIT ||
1044 sk->sk_state == TCP_CLOSE) &&
1045 !atomic_read(&sk->sk_rmem_alloc)) {
1046 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1047 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1050 tcp_conn = conn->dd_data;
1051 old_state_change = tcp_conn->old_state_change;
1053 read_unlock(&sk->sk_callback_lock);
1055 old_state_change(sk);
1059 * iscsi_write_space - Called when more output buffer space is available
1060 * @sk: socket space is available for
1063 iscsi_write_space(struct sock *sk)
1065 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
1066 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1068 tcp_conn->old_write_space(sk);
1069 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
1070 scsi_queue_work(conn->session->host, &conn->xmitwork);
1074 iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1076 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1077 struct sock *sk = tcp_conn->sock->sk;
1079 /* assign new callbacks */
1080 write_lock_bh(&sk->sk_callback_lock);
1081 sk->sk_user_data = conn;
1082 tcp_conn->old_data_ready = sk->sk_data_ready;
1083 tcp_conn->old_state_change = sk->sk_state_change;
1084 tcp_conn->old_write_space = sk->sk_write_space;
1085 sk->sk_data_ready = iscsi_tcp_data_ready;
1086 sk->sk_state_change = iscsi_tcp_state_change;
1087 sk->sk_write_space = iscsi_write_space;
1088 write_unlock_bh(&sk->sk_callback_lock);
1092 iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
1094 struct sock *sk = tcp_conn->sock->sk;
1096 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1097 write_lock_bh(&sk->sk_callback_lock);
1098 sk->sk_user_data = NULL;
1099 sk->sk_data_ready = tcp_conn->old_data_ready;
1100 sk->sk_state_change = tcp_conn->old_state_change;
1101 sk->sk_write_space = tcp_conn->old_write_space;
1102 sk->sk_no_check = 0;
1103 write_unlock_bh(&sk->sk_callback_lock);
1107 * iscsi_send - generic send routine
1108 * @sk: kernel's socket
1109 * @buf: buffer to write from
1110 * @size: actual size to write
1111 * @flags: socket's flags
1114 iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags)
1116 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1117 struct socket *sk = tcp_conn->sock;
1118 int offset = buf->sg.offset + buf->sent, res;
1121 * if we got use_sg=0 or are sending something we kmallocd
1122 * then we did not have to do kmap (kmap returns page_address)
1124 * if we got use_sg > 0, but had to drop down, we do not
1125 * set clustering so this should only happen for that
1128 if (buf->use_sendmsg)
1129 res = sock_no_sendpage(sk, buf->sg.page, offset, size, flags);
1131 res = tcp_conn->sendpage(sk, buf->sg.page, offset, size, flags);
1134 conn->txdata_octets += res;
1139 tcp_conn->sendpage_failures_cnt++;
1143 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1148 * iscsi_sendhdr - send PDU Header via tcp_sendpage()
1149 * @conn: iscsi connection
1150 * @buf: buffer to write from
1151 * @datalen: lenght of data to be sent after the header
1157 iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
1159 int flags = 0; /* MSG_DONTWAIT; */
1162 size = buf->sg.length - buf->sent;
1163 BUG_ON(buf->sent + size > buf->sg.length);
1164 if (buf->sent + size != buf->sg.length || datalen)
1167 res = iscsi_send(conn, buf, size, flags);
1168 debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res);
1179 * iscsi_sendpage - send one page of iSCSI Data-Out.
1180 * @conn: iscsi connection
1181 * @buf: buffer to write from
1182 * @count: remaining data
1183 * @sent: number of bytes sent
1189 iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
1190 int *count, int *sent)
1192 int flags = 0; /* MSG_DONTWAIT; */
1195 size = buf->sg.length - buf->sent;
1196 BUG_ON(buf->sent + size > buf->sg.length);
1199 if (buf->sent + size != buf->sg.length || *count != size)
1202 res = iscsi_send(conn, buf, size, flags);
1203 debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n",
1204 size, buf->sent, *count, *sent, res);
1217 iscsi_data_digest_init(struct iscsi_tcp_conn *tcp_conn,
1218 struct iscsi_tcp_cmd_task *tcp_ctask)
1220 crypto_hash_init(&tcp_conn->tx_hash);
1221 tcp_ctask->digest_count = 4;
1225 * iscsi_solicit_data_cont - initialize next Data-Out
1226 * @conn: iscsi connection
1227 * @ctask: scsi command task
1229 * @left: bytes left to transfer
1232 * Initialize next Data-Out within this R2T sequence and continue
1233 * to process next Scatter-Gather element(if any) of this SCSI command.
1235 * Called under connection lock.
1238 iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1239 struct iscsi_r2t_info *r2t, int left)
1241 struct iscsi_data *hdr;
1242 struct scsi_cmnd *sc = ctask->sc;
1245 hdr = &r2t->dtask.hdr;
1246 memset(hdr, 0, sizeof(struct iscsi_data));
1247 hdr->ttt = r2t->ttt;
1248 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1249 r2t->solicit_datasn++;
1250 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
1251 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1252 hdr->itt = ctask->hdr->itt;
1253 hdr->exp_statsn = r2t->exp_statsn;
1254 new_offset = r2t->data_offset + r2t->sent;
1255 hdr->offset = cpu_to_be32(new_offset);
1256 if (left > conn->max_xmit_dlength) {
1257 hton24(hdr->dlength, conn->max_xmit_dlength);
1258 r2t->data_count = conn->max_xmit_dlength;
1260 hton24(hdr->dlength, left);
1261 r2t->data_count = left;
1262 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1264 conn->dataout_pdus_cnt++;
1266 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
1267 sizeof(struct iscsi_hdr));
1269 if (iscsi_buf_left(&r2t->sendbuf))
1273 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1276 iscsi_buf_init_iov(&r2t->sendbuf,
1277 (char*)sc->request_buffer + new_offset,
1283 static void iscsi_set_padding(struct iscsi_tcp_cmd_task *tcp_ctask,
1286 tcp_ctask->pad_count = len & (ISCSI_PAD_LEN - 1);
1287 if (!tcp_ctask->pad_count)
1290 tcp_ctask->pad_count = ISCSI_PAD_LEN - tcp_ctask->pad_count;
1291 debug_scsi("write padding %d bytes\n", tcp_ctask->pad_count);
1292 tcp_ctask->xmstate |= XMSTATE_W_PAD;
1296 * iscsi_tcp_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
1297 * @conn: iscsi connection
1298 * @ctask: scsi command task
1302 iscsi_tcp_cmd_init(struct iscsi_cmd_task *ctask)
1304 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1306 BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
1307 tcp_ctask->xmstate = XMSTATE_CMD_HDR_INIT;
1311 * iscsi_tcp_mtask_xmit - xmit management(immediate) task
1312 * @conn: iscsi connection
1313 * @mtask: task management task
1316 * The function can return -EAGAIN in which case caller must
1317 * call it again later, or recover. '0' return code means successful
1320 * Management xmit state machine consists of these states:
1321 * XMSTATE_IMM_HDR_INIT - calculate digest of PDU Header
1322 * XMSTATE_IMM_HDR - PDU Header xmit in progress
1323 * XMSTATE_IMM_DATA - PDU Data xmit in progress
1324 * XMSTATE_IDLE - management PDU is done
1327 iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
1329 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
1332 debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
1333 conn->id, tcp_mtask->xmstate, mtask->itt);
1335 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR_INIT) {
1336 iscsi_buf_init_iov(&tcp_mtask->headbuf, (char*)mtask->hdr,
1337 sizeof(struct iscsi_hdr));
1339 if (mtask->data_count) {
1340 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
1341 iscsi_buf_init_iov(&tcp_mtask->sendbuf,
1346 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
1347 conn->stop_stage != STOP_CONN_RECOVER &&
1349 iscsi_hdr_digest(conn, &tcp_mtask->headbuf,
1350 (u8*)tcp_mtask->hdrext);
1352 tcp_mtask->sent = 0;
1353 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR_INIT;
1354 tcp_mtask->xmstate |= XMSTATE_IMM_HDR;
1357 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR) {
1358 rc = iscsi_sendhdr(conn, &tcp_mtask->headbuf,
1362 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR;
1365 if (tcp_mtask->xmstate & XMSTATE_IMM_DATA) {
1366 BUG_ON(!mtask->data_count);
1367 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
1368 /* FIXME: implement.
1369 * Virtual buffer could be spreaded across multiple pages...
1374 rc = iscsi_sendpage(conn, &tcp_mtask->sendbuf,
1375 &mtask->data_count, &tcp_mtask->sent);
1377 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
1380 } while (mtask->data_count);
1383 BUG_ON(tcp_mtask->xmstate != XMSTATE_IDLE);
1384 if (mtask->hdr->itt == RESERVED_ITT) {
1385 struct iscsi_session *session = conn->session;
1387 spin_lock_bh(&session->lock);
1388 list_del(&conn->mtask->running);
1389 __kfifo_put(session->mgmtpool.queue, (void*)&conn->mtask,
1391 spin_unlock_bh(&session->lock);
1397 iscsi_send_cmd_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1399 struct scsi_cmnd *sc = ctask->sc;
1400 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1403 if (tcp_ctask->xmstate & XMSTATE_CMD_HDR_INIT) {
1404 tcp_ctask->sent = 0;
1405 tcp_ctask->sg_count = 0;
1406 tcp_ctask->exp_datasn = 0;
1408 if (sc->sc_data_direction == DMA_TO_DEVICE) {
1410 struct scatterlist *sg = sc->request_buffer;
1412 iscsi_buf_init_sg(&tcp_ctask->sendbuf, sg);
1413 tcp_ctask->sg = sg + 1;
1414 tcp_ctask->bad_sg = sg + sc->use_sg;
1416 iscsi_buf_init_iov(&tcp_ctask->sendbuf,
1418 sc->request_bufflen);
1419 tcp_ctask->sg = NULL;
1420 tcp_ctask->bad_sg = NULL;
1423 debug_scsi("cmd [itt 0x%x total %d imm_data %d "
1424 "unsol count %d, unsol offset %d]\n",
1425 ctask->itt, sc->request_bufflen,
1426 ctask->imm_count, ctask->unsol_count,
1427 ctask->unsol_offset);
1430 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)ctask->hdr,
1431 sizeof(struct iscsi_hdr));
1433 if (conn->hdrdgst_en)
1434 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1435 (u8*)tcp_ctask->hdrext);
1436 tcp_ctask->xmstate &= ~XMSTATE_CMD_HDR_INIT;
1437 tcp_ctask->xmstate |= XMSTATE_CMD_HDR_XMIT;
1440 if (tcp_ctask->xmstate & XMSTATE_CMD_HDR_XMIT) {
1441 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->imm_count);
1444 tcp_ctask->xmstate &= ~XMSTATE_CMD_HDR_XMIT;
1446 if (sc->sc_data_direction != DMA_TO_DEVICE)
1449 if (ctask->imm_count) {
1450 tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
1451 iscsi_set_padding(tcp_ctask, ctask->imm_count);
1453 if (ctask->conn->datadgst_en) {
1454 iscsi_data_digest_init(ctask->conn->dd_data,
1456 tcp_ctask->immdigest = 0;
1460 if (ctask->unsol_count)
1461 tcp_ctask->xmstate |=
1462 XMSTATE_UNS_HDR | XMSTATE_UNS_INIT;
1468 iscsi_send_padding(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1470 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1471 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1474 if (tcp_ctask->xmstate & XMSTATE_W_PAD) {
1475 iscsi_buf_init_iov(&tcp_ctask->sendbuf, (char*)&tcp_ctask->pad,
1476 tcp_ctask->pad_count);
1477 if (conn->datadgst_en)
1478 crypto_hash_update(&tcp_conn->tx_hash,
1479 &tcp_ctask->sendbuf.sg,
1480 tcp_ctask->sendbuf.sg.length);
1481 } else if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_PAD))
1484 tcp_ctask->xmstate &= ~XMSTATE_W_PAD;
1485 tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_PAD;
1486 debug_scsi("sending %d pad bytes for itt 0x%x\n",
1487 tcp_ctask->pad_count, ctask->itt);
1488 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf, &tcp_ctask->pad_count,
1491 debug_scsi("padding send failed %d\n", rc);
1492 tcp_ctask->xmstate |= XMSTATE_W_RESEND_PAD;
1498 iscsi_send_digest(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1499 struct iscsi_buf *buf, uint32_t *digest)
1501 struct iscsi_tcp_cmd_task *tcp_ctask;
1502 struct iscsi_tcp_conn *tcp_conn;
1505 if (!conn->datadgst_en)
1508 tcp_ctask = ctask->dd_data;
1509 tcp_conn = conn->dd_data;
1511 if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_DATA_DIGEST)) {
1512 crypto_hash_final(&tcp_conn->tx_hash, (u8*)digest);
1513 iscsi_buf_init_iov(buf, (char*)digest, 4);
1515 tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_DATA_DIGEST;
1517 rc = iscsi_sendpage(conn, buf, &tcp_ctask->digest_count, &sent);
1519 debug_scsi("sent digest 0x%x for itt 0x%x\n", *digest,
1522 debug_scsi("sending digest 0x%x failed for itt 0x%x!\n",
1523 *digest, ctask->itt);
1524 tcp_ctask->xmstate |= XMSTATE_W_RESEND_DATA_DIGEST;
1530 iscsi_send_data(struct iscsi_cmd_task *ctask, struct iscsi_buf *sendbuf,
1531 struct scatterlist **sg, int *sent, int *count,
1532 struct iscsi_buf *digestbuf, uint32_t *digest)
1534 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1535 struct iscsi_conn *conn = ctask->conn;
1536 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1537 int rc, buf_sent, offset;
1541 offset = sendbuf->sent;
1543 rc = iscsi_sendpage(conn, sendbuf, count, &buf_sent);
1544 *sent = *sent + buf_sent;
1545 if (buf_sent && conn->datadgst_en)
1546 partial_sg_digest_update(&tcp_conn->tx_hash,
1547 &sendbuf->sg, sendbuf->sg.offset + offset,
1549 if (!iscsi_buf_left(sendbuf) && *sg != tcp_ctask->bad_sg) {
1550 iscsi_buf_init_sg(sendbuf, *sg);
1558 rc = iscsi_send_padding(conn, ctask);
1562 return iscsi_send_digest(conn, ctask, digestbuf, digest);
1566 iscsi_send_unsol_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1568 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1569 struct iscsi_data_task *dtask;
1572 tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
1573 if (tcp_ctask->xmstate & XMSTATE_UNS_INIT) {
1574 dtask = &tcp_ctask->unsol_dtask;
1576 iscsi_prep_unsolicit_data_pdu(ctask, &dtask->hdr);
1577 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)&dtask->hdr,
1578 sizeof(struct iscsi_hdr));
1579 if (conn->hdrdgst_en)
1580 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1581 (u8*)dtask->hdrext);
1583 tcp_ctask->xmstate &= ~XMSTATE_UNS_INIT;
1584 iscsi_set_padding(tcp_ctask, ctask->data_count);
1587 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->data_count);
1589 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1590 tcp_ctask->xmstate |= XMSTATE_UNS_HDR;
1594 if (conn->datadgst_en) {
1595 dtask = &tcp_ctask->unsol_dtask;
1596 iscsi_data_digest_init(ctask->conn->dd_data, tcp_ctask);
1600 debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
1601 ctask->itt, ctask->unsol_count, tcp_ctask->sent);
1606 iscsi_send_unsol_pdu(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1608 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1611 if (tcp_ctask->xmstate & XMSTATE_UNS_HDR) {
1612 BUG_ON(!ctask->unsol_count);
1613 tcp_ctask->xmstate &= ~XMSTATE_UNS_HDR;
1615 rc = iscsi_send_unsol_hdr(conn, ctask);
1620 if (tcp_ctask->xmstate & XMSTATE_UNS_DATA) {
1621 struct iscsi_data_task *dtask = &tcp_ctask->unsol_dtask;
1622 int start = tcp_ctask->sent;
1624 rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg,
1625 &tcp_ctask->sent, &ctask->data_count,
1626 &dtask->digestbuf, &dtask->digest);
1627 ctask->unsol_count -= tcp_ctask->sent - start;
1630 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1632 * Done with the Data-Out. Next, check if we need
1633 * to send another unsolicited Data-Out.
1635 if (ctask->unsol_count) {
1636 debug_scsi("sending more uns\n");
1637 tcp_ctask->xmstate |= XMSTATE_UNS_INIT;
1644 static int iscsi_send_sol_pdu(struct iscsi_conn *conn,
1645 struct iscsi_cmd_task *ctask)
1647 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1648 struct iscsi_session *session = conn->session;
1649 struct iscsi_r2t_info *r2t;
1650 struct iscsi_data_task *dtask;
1653 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR_INIT) {
1654 if (!tcp_ctask->r2t) {
1655 spin_lock_bh(&session->lock);
1656 __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
1658 spin_unlock_bh(&session->lock);
1661 r2t = tcp_ctask->r2t;
1662 dtask = &r2t->dtask;
1664 if (conn->hdrdgst_en)
1665 iscsi_hdr_digest(conn, &r2t->headbuf,
1666 (u8*)dtask->hdrext);
1667 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR_INIT;
1668 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
1671 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR) {
1672 r2t = tcp_ctask->r2t;
1673 dtask = &r2t->dtask;
1675 rc = iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count);
1678 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
1679 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1681 if (conn->datadgst_en) {
1682 iscsi_data_digest_init(conn->dd_data, tcp_ctask);
1686 iscsi_set_padding(tcp_ctask, r2t->data_count);
1687 debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n",
1688 r2t->solicit_datasn - 1, ctask->itt, r2t->data_count,
1692 if (tcp_ctask->xmstate & XMSTATE_SOL_DATA) {
1693 r2t = tcp_ctask->r2t;
1694 dtask = &r2t->dtask;
1696 rc = iscsi_send_data(ctask, &r2t->sendbuf, &r2t->sg,
1697 &r2t->sent, &r2t->data_count,
1698 &dtask->digestbuf, &dtask->digest);
1701 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
1704 * Done with this Data-Out. Next, check if we have
1705 * to send another Data-Out for this R2T.
1707 BUG_ON(r2t->data_length - r2t->sent < 0);
1708 left = r2t->data_length - r2t->sent;
1710 iscsi_solicit_data_cont(conn, ctask, r2t, left);
1715 * Done with this R2T. Check if there are more
1716 * outstanding R2Ts ready to be processed.
1718 spin_lock_bh(&session->lock);
1719 tcp_ctask->r2t = NULL;
1720 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
1722 if (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t,
1724 tcp_ctask->r2t = r2t;
1725 spin_unlock_bh(&session->lock);
1728 spin_unlock_bh(&session->lock);
1734 * iscsi_tcp_ctask_xmit - xmit normal PDU task
1735 * @conn: iscsi connection
1736 * @ctask: iscsi command task
1739 * The function can return -EAGAIN in which case caller must
1740 * call it again later, or recover. '0' return code means successful
1742 * The function is devided to logical helpers (above) for the different
1745 *iscsi_send_cmd_hdr()
1746 * XMSTATE_CMD_HDR_INIT - prepare Header and Data buffers Calculate
1748 * XMSTATE_CMD_HDR_XMIT - Transmit header in progress
1751 * XMSTATE_W_PAD - Prepare and send pading
1752 * XMSTATE_W_RESEND_PAD - retry send pading
1755 * XMSTATE_W_RESEND_DATA_DIGEST - Finalize and send Data Digest
1756 * XMSTATE_W_RESEND_DATA_DIGEST - retry sending digest
1758 *iscsi_send_unsol_hdr
1759 * XMSTATE_UNS_INIT - prepare un-solicit data header and digest
1760 * XMSTATE_UNS_HDR - send un-solicit header
1762 *iscsi_send_unsol_pdu
1763 * XMSTATE_UNS_DATA - send un-solicit data in progress
1766 * XMSTATE_SOL_HDR_INIT - solicit data header and digest initialize
1767 * XMSTATE_SOL_HDR - send solicit header
1768 * XMSTATE_SOL_DATA - send solicit data
1770 *iscsi_tcp_ctask_xmit
1771 * XMSTATE_IMM_DATA - xmit managment data (??)
1774 iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1776 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1779 debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
1780 conn->id, tcp_ctask->xmstate, ctask->itt);
1782 rc = iscsi_send_cmd_hdr(conn, ctask);
1785 if (ctask->sc->sc_data_direction != DMA_TO_DEVICE)
1788 if (tcp_ctask->xmstate & XMSTATE_IMM_DATA) {
1789 rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg,
1790 &tcp_ctask->sent, &ctask->imm_count,
1791 &tcp_ctask->immbuf, &tcp_ctask->immdigest);
1794 tcp_ctask->xmstate &= ~XMSTATE_IMM_DATA;
1797 rc = iscsi_send_unsol_pdu(conn, ctask);
1801 rc = iscsi_send_sol_pdu(conn, ctask);
1808 static struct iscsi_cls_conn *
1809 iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1811 struct iscsi_conn *conn;
1812 struct iscsi_cls_conn *cls_conn;
1813 struct iscsi_tcp_conn *tcp_conn;
1815 cls_conn = iscsi_conn_setup(cls_session, conn_idx);
1818 conn = cls_conn->dd_data;
1820 * due to strange issues with iser these are not set
1821 * in iscsi_conn_setup
1823 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
1825 tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
1827 goto tcp_conn_alloc_fail;
1829 conn->dd_data = tcp_conn;
1830 tcp_conn->iscsi_conn = conn;
1831 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1832 /* initial operational parameters */
1833 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
1835 tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1837 tcp_conn->tx_hash.flags = 0;
1838 if (IS_ERR(tcp_conn->tx_hash.tfm)) {
1839 printk(KERN_ERR "Could not create connection due to crc32c "
1840 "loading error %ld. Make sure the crc32c module is "
1841 "built as a module or into the kernel\n",
1842 PTR_ERR(tcp_conn->tx_hash.tfm));
1846 tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1848 tcp_conn->rx_hash.flags = 0;
1849 if (IS_ERR(tcp_conn->rx_hash.tfm)) {
1850 printk(KERN_ERR "Could not create connection due to crc32c "
1851 "loading error %ld. Make sure the crc32c module is "
1852 "built as a module or into the kernel\n",
1853 PTR_ERR(tcp_conn->rx_hash.tfm));
1860 crypto_free_hash(tcp_conn->tx_hash.tfm);
1863 tcp_conn_alloc_fail:
1864 iscsi_conn_teardown(cls_conn);
1869 iscsi_tcp_release_conn(struct iscsi_conn *conn)
1871 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1873 if (!tcp_conn->sock)
1876 sock_hold(tcp_conn->sock->sk);
1877 iscsi_conn_restore_callbacks(tcp_conn);
1878 sock_put(tcp_conn->sock->sk);
1880 sock_release(tcp_conn->sock);
1881 tcp_conn->sock = NULL;
1882 conn->recv_lock = NULL;
1886 iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
1888 struct iscsi_conn *conn = cls_conn->dd_data;
1889 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1891 iscsi_tcp_release_conn(conn);
1892 iscsi_conn_teardown(cls_conn);
1894 if (tcp_conn->tx_hash.tfm)
1895 crypto_free_hash(tcp_conn->tx_hash.tfm);
1896 if (tcp_conn->rx_hash.tfm)
1897 crypto_free_hash(tcp_conn->rx_hash.tfm);
1903 iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1905 struct iscsi_conn *conn = cls_conn->dd_data;
1906 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1908 iscsi_conn_stop(cls_conn, flag);
1909 iscsi_tcp_release_conn(conn);
1910 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
1914 iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
1915 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
1918 struct iscsi_conn *conn = cls_conn->dd_data;
1919 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1921 struct socket *sock;
1924 /* lookup for existing socket */
1925 sock = sockfd_lookup((int)transport_eph, &err);
1927 printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
1931 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
1935 /* bind iSCSI connection and socket */
1936 tcp_conn->sock = sock;
1938 /* setup Socket parameters */
1941 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
1942 sk->sk_allocation = GFP_ATOMIC;
1944 /* FIXME: disable Nagle's algorithm */
1947 * Intercept TCP callbacks for sendfile like receive
1950 conn->recv_lock = &sk->sk_callback_lock;
1951 iscsi_conn_set_callbacks(conn);
1952 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
1954 * set receive state machine into initial state
1956 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1961 /* called with host lock */
1963 iscsi_tcp_mgmt_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
1965 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
1966 tcp_mtask->xmstate = XMSTATE_IMM_HDR_INIT;
1970 iscsi_r2tpool_alloc(struct iscsi_session *session)
1976 * initialize per-task: R2T pool and xmit queue
1978 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1979 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1980 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1983 * pre-allocated x4 as much r2ts to handle race when
1984 * target acks DataOut faster than we data_xmit() queues
1985 * could replenish r2tqueue.
1989 if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4,
1990 (void***)&tcp_ctask->r2ts,
1991 sizeof(struct iscsi_r2t_info))) {
1992 goto r2t_alloc_fail;
1995 /* R2T xmit queue */
1996 tcp_ctask->r2tqueue = kfifo_alloc(
1997 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
1998 if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
1999 iscsi_pool_free(&tcp_ctask->r2tpool,
2000 (void**)tcp_ctask->r2ts);
2001 goto r2t_alloc_fail;
2008 for (i = 0; i < cmd_i; i++) {
2009 struct iscsi_cmd_task *ctask = session->cmds[i];
2010 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2012 kfifo_free(tcp_ctask->r2tqueue);
2013 iscsi_pool_free(&tcp_ctask->r2tpool,
2014 (void**)tcp_ctask->r2ts);
2020 iscsi_r2tpool_free(struct iscsi_session *session)
2024 for (i = 0; i < session->cmds_max; i++) {
2025 struct iscsi_cmd_task *ctask = session->cmds[i];
2026 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2028 kfifo_free(tcp_ctask->r2tqueue);
2029 iscsi_pool_free(&tcp_ctask->r2tpool,
2030 (void**)tcp_ctask->r2ts);
2035 iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
2036 char *buf, int buflen)
2038 struct iscsi_conn *conn = cls_conn->dd_data;
2039 struct iscsi_session *session = conn->session;
2040 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2044 case ISCSI_PARAM_HDRDGST_EN:
2045 iscsi_set_param(cls_conn, param, buf, buflen);
2046 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
2047 if (conn->hdrdgst_en)
2048 tcp_conn->hdr_size += sizeof(__u32);
2050 case ISCSI_PARAM_DATADGST_EN:
2051 iscsi_set_param(cls_conn, param, buf, buflen);
2052 tcp_conn->sendpage = conn->datadgst_en ?
2053 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
2055 case ISCSI_PARAM_MAX_R2T:
2056 sscanf(buf, "%d", &value);
2057 if (session->max_r2t == roundup_pow_of_two(value))
2059 iscsi_r2tpool_free(session);
2060 iscsi_set_param(cls_conn, param, buf, buflen);
2061 if (session->max_r2t & (session->max_r2t - 1))
2062 session->max_r2t = roundup_pow_of_two(session->max_r2t);
2063 if (iscsi_r2tpool_alloc(session))
2067 return iscsi_set_param(cls_conn, param, buf, buflen);
2074 iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
2075 enum iscsi_param param, char *buf)
2077 struct iscsi_conn *conn = cls_conn->dd_data;
2078 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2079 struct inet_sock *inet;
2080 struct ipv6_pinfo *np;
2085 case ISCSI_PARAM_CONN_PORT:
2086 if (!tcp_conn->sock)
2089 inet = inet_sk(tcp_conn->sock->sk);
2090 len = sprintf(buf, "%hu\n", be16_to_cpu(inet->dport));
2092 case ISCSI_PARAM_CONN_ADDRESS:
2093 if (!tcp_conn->sock)
2096 sk = tcp_conn->sock->sk;
2097 if (sk->sk_family == PF_INET) {
2099 len = sprintf(buf, NIPQUAD_FMT "\n",
2100 NIPQUAD(inet->daddr));
2103 len = sprintf(buf, NIP6_FMT "\n", NIP6(np->daddr));
2107 return iscsi_conn_get_param(cls_conn, param, buf);
2114 iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
2116 struct iscsi_conn *conn = cls_conn->dd_data;
2117 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2119 stats->txdata_octets = conn->txdata_octets;
2120 stats->rxdata_octets = conn->rxdata_octets;
2121 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
2122 stats->dataout_pdus = conn->dataout_pdus_cnt;
2123 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
2124 stats->datain_pdus = conn->datain_pdus_cnt;
2125 stats->r2t_pdus = conn->r2t_pdus_cnt;
2126 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
2127 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
2128 stats->custom_length = 3;
2129 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
2130 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
2131 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
2132 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
2133 strcpy(stats->custom[2].desc, "eh_abort_cnt");
2134 stats->custom[2].value = conn->eh_abort_cnt;
2137 static struct iscsi_cls_session *
2138 iscsi_tcp_session_create(struct iscsi_transport *iscsit,
2139 struct scsi_transport_template *scsit,
2140 uint16_t cmds_max, uint16_t qdepth,
2141 uint32_t initial_cmdsn, uint32_t *hostno)
2143 struct iscsi_cls_session *cls_session;
2144 struct iscsi_session *session;
2148 cls_session = iscsi_session_setup(iscsit, scsit, cmds_max, qdepth,
2149 sizeof(struct iscsi_tcp_cmd_task),
2150 sizeof(struct iscsi_tcp_mgmt_task),
2151 initial_cmdsn, &hn);
2156 session = class_to_transport_session(cls_session);
2157 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2158 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
2159 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2161 ctask->hdr = &tcp_ctask->hdr;
2164 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
2165 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
2166 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
2168 mtask->hdr = &tcp_mtask->hdr;
2171 if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
2172 goto r2tpool_alloc_fail;
2177 iscsi_session_teardown(cls_session);
2181 static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
2183 iscsi_r2tpool_free(class_to_transport_session(cls_session));
2184 iscsi_session_teardown(cls_session);
2187 static struct scsi_host_template iscsi_sht = {
2188 .name = "iSCSI Initiator over TCP/IP",
2189 .queuecommand = iscsi_queuecommand,
2190 .change_queue_depth = iscsi_change_queue_depth,
2191 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
2192 .sg_tablesize = ISCSI_SG_TABLESIZE,
2193 .max_sectors = 0xFFFF,
2194 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
2195 .eh_abort_handler = iscsi_eh_abort,
2196 .eh_host_reset_handler = iscsi_eh_host_reset,
2197 .use_clustering = DISABLE_CLUSTERING,
2198 .proc_name = "iscsi_tcp",
2202 static struct iscsi_transport iscsi_tcp_transport = {
2203 .owner = THIS_MODULE,
2205 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
2207 .param_mask = ISCSI_MAX_RECV_DLENGTH |
2208 ISCSI_MAX_XMIT_DLENGTH |
2211 ISCSI_INITIAL_R2T_EN |
2216 ISCSI_PDU_INORDER_EN |
2217 ISCSI_DATASEQ_INORDER_EN |
2220 ISCSI_CONN_ADDRESS |
2222 ISCSI_PERSISTENT_PORT |
2223 ISCSI_PERSISTENT_ADDRESS |
2224 ISCSI_TARGET_NAME | ISCSI_TPGT |
2225 ISCSI_USERNAME | ISCSI_PASSWORD |
2226 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN,
2227 .host_param_mask = ISCSI_HOST_HWADDRESS |
2228 ISCSI_HOST_INITIATOR_NAME,
2229 .host_template = &iscsi_sht,
2230 .conndata_size = sizeof(struct iscsi_conn),
2232 .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN,
2233 /* session management */
2234 .create_session = iscsi_tcp_session_create,
2235 .destroy_session = iscsi_tcp_session_destroy,
2236 /* connection management */
2237 .create_conn = iscsi_tcp_conn_create,
2238 .bind_conn = iscsi_tcp_conn_bind,
2239 .destroy_conn = iscsi_tcp_conn_destroy,
2240 .set_param = iscsi_conn_set_param,
2241 .get_conn_param = iscsi_tcp_conn_get_param,
2242 .get_session_param = iscsi_session_get_param,
2243 .start_conn = iscsi_conn_start,
2244 .stop_conn = iscsi_tcp_conn_stop,
2245 /* iscsi host params */
2246 .get_host_param = iscsi_host_get_param,
2247 .set_host_param = iscsi_host_set_param,
2249 .send_pdu = iscsi_conn_send_pdu,
2250 .get_stats = iscsi_conn_get_stats,
2251 .init_cmd_task = iscsi_tcp_cmd_init,
2252 .init_mgmt_task = iscsi_tcp_mgmt_init,
2253 .xmit_cmd_task = iscsi_tcp_ctask_xmit,
2254 .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
2255 .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
2257 .session_recovery_timedout = iscsi_session_recovery_timedout,
2261 iscsi_tcp_init(void)
2263 if (iscsi_max_lun < 1) {
2264 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
2268 iscsi_tcp_transport.max_lun = iscsi_max_lun;
2270 if (!iscsi_register_transport(&iscsi_tcp_transport))
2277 iscsi_tcp_exit(void)
2279 iscsi_unregister_transport(&iscsi_tcp_transport);
2282 module_init(iscsi_tcp_init);
2283 module_exit(iscsi_tcp_exit);