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/file.h>
33 #include <linux/blkdev.h>
34 #include <linux/crypto.h>
35 #include <linux/delay.h>
36 #include <linux/kfifo.h>
37 #include <linux/scatterlist.h>
39 #include <scsi/scsi_cmnd.h>
40 #include <scsi/scsi_device.h>
41 #include <scsi/scsi_host.h>
42 #include <scsi/scsi.h>
43 #include <scsi/scsi_transport_iscsi.h>
45 #include "iscsi_tcp.h"
47 MODULE_AUTHOR("Dmitry Yusupov <dmitry_yus@yahoo.com>, "
48 "Alex Aizman <itn780@yahoo.com>");
49 MODULE_DESCRIPTION("iSCSI/TCP data-path");
50 MODULE_LICENSE("GPL");
51 /* #define DEBUG_TCP */
55 #define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
57 #define debug_tcp(fmt...)
67 static unsigned int iscsi_max_lun = 512;
68 module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
71 iscsi_buf_init_iov(struct iscsi_buf *ibuf, char *vbuf, int size)
73 sg_init_one(&ibuf->sg, vbuf, size);
75 ibuf->use_sendmsg = 1;
79 iscsi_buf_init_sg(struct iscsi_buf *ibuf, struct scatterlist *sg)
81 sg_init_table(&ibuf->sg, 1);
82 sg_set_page(&ibuf->sg, sg_page(sg));
83 ibuf->sg.offset = sg->offset;
84 ibuf->sg.length = sg->length;
86 * Fastpath: sg element fits into single page
88 if (sg->length + sg->offset <= PAGE_SIZE && !PageSlab(sg_page(sg)))
89 ibuf->use_sendmsg = 0;
91 ibuf->use_sendmsg = 1;
96 iscsi_buf_left(struct iscsi_buf *ibuf)
100 rc = ibuf->sg.length - ibuf->sent;
106 iscsi_hdr_digest(struct iscsi_conn *conn, struct iscsi_buf *buf,
109 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
111 crypto_hash_digest(&tcp_conn->tx_hash, &buf->sg, buf->sg.length, crc);
112 buf->sg.length += sizeof(u32);
116 iscsi_hdr_extract(struct iscsi_tcp_conn *tcp_conn)
118 struct sk_buff *skb = tcp_conn->in.skb;
120 tcp_conn->in.zero_copy_hdr = 0;
122 if (tcp_conn->in.copy >= tcp_conn->hdr_size &&
123 tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER) {
125 * Zero-copy PDU Header: using connection context
126 * to store header pointer.
128 if (skb_shinfo(skb)->frag_list == NULL &&
129 !skb_shinfo(skb)->nr_frags) {
130 tcp_conn->in.hdr = (struct iscsi_hdr *)
131 ((char*)skb->data + tcp_conn->in.offset);
132 tcp_conn->in.zero_copy_hdr = 1;
134 /* ignoring return code since we checked
136 skb_copy_bits(skb, tcp_conn->in.offset,
137 &tcp_conn->hdr, tcp_conn->hdr_size);
138 tcp_conn->in.hdr = &tcp_conn->hdr;
140 tcp_conn->in.offset += tcp_conn->hdr_size;
141 tcp_conn->in.copy -= tcp_conn->hdr_size;
147 * PDU header scattered across SKB's,
148 * copying it... This'll happen quite rarely.
151 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER)
152 tcp_conn->in.hdr_offset = 0;
154 hdr_remains = tcp_conn->hdr_size - tcp_conn->in.hdr_offset;
155 BUG_ON(hdr_remains <= 0);
157 copylen = min(tcp_conn->in.copy, hdr_remains);
158 skb_copy_bits(skb, tcp_conn->in.offset,
159 (char*)&tcp_conn->hdr + tcp_conn->in.hdr_offset,
162 debug_tcp("PDU gather offset %d bytes %d in.offset %d "
163 "in.copy %d\n", tcp_conn->in.hdr_offset, copylen,
164 tcp_conn->in.offset, tcp_conn->in.copy);
166 tcp_conn->in.offset += copylen;
167 tcp_conn->in.copy -= copylen;
168 if (copylen < hdr_remains) {
169 tcp_conn->in_progress = IN_PROGRESS_HEADER_GATHER;
170 tcp_conn->in.hdr_offset += copylen;
173 tcp_conn->in.hdr = &tcp_conn->hdr;
174 tcp_conn->discontiguous_hdr_cnt++;
175 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
182 * must be called with session lock
185 iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
187 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
188 struct iscsi_r2t_info *r2t;
189 struct scsi_cmnd *sc;
191 /* flush ctask's r2t queues */
192 while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
193 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
195 debug_scsi("iscsi_tcp_cleanup_ctask pending r2t dropped\n");
202 tcp_ctask->xmstate = XMSTATE_IDLE;
203 tcp_ctask->r2t = NULL;
207 * iscsi_data_rsp - SCSI Data-In Response processing
208 * @conn: iscsi connection
209 * @ctask: scsi command task
212 iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
214 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
215 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
216 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
217 struct iscsi_session *session = conn->session;
218 struct scsi_cmnd *sc = ctask->sc;
219 int datasn = be32_to_cpu(rhdr->datasn);
221 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
223 * setup Data-In byte counter (gets decremented..)
225 ctask->data_count = tcp_conn->in.datalen;
227 if (tcp_conn->in.datalen == 0)
230 if (tcp_ctask->exp_datasn != datasn) {
231 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->datasn(%d)\n",
232 __FUNCTION__, tcp_ctask->exp_datasn, datasn);
233 return ISCSI_ERR_DATASN;
236 tcp_ctask->exp_datasn++;
238 tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
239 if (tcp_ctask->data_offset + tcp_conn->in.datalen > scsi_bufflen(sc)) {
240 debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
241 __FUNCTION__, tcp_ctask->data_offset,
242 tcp_conn->in.datalen, scsi_bufflen(sc));
243 return ISCSI_ERR_DATA_OFFSET;
246 if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
247 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
248 if (rhdr->flags & ISCSI_FLAG_DATA_UNDERFLOW) {
249 int res_count = be32_to_cpu(rhdr->residual_count);
252 res_count <= scsi_bufflen(sc)) {
253 scsi_set_resid(sc, res_count);
254 sc->result = (DID_OK << 16) | rhdr->cmd_status;
256 sc->result = (DID_BAD_TARGET << 16) |
258 } else if (rhdr->flags & ISCSI_FLAG_DATA_OVERFLOW) {
259 scsi_set_resid(sc, be32_to_cpu(rhdr->residual_count));
260 sc->result = (DID_OK << 16) | rhdr->cmd_status;
262 sc->result = (DID_OK << 16) | rhdr->cmd_status;
265 conn->datain_pdus_cnt++;
270 * iscsi_solicit_data_init - initialize first Data-Out
271 * @conn: iscsi connection
272 * @ctask: scsi command task
276 * Initialize first Data-Out within this R2T sequence and finds
277 * proper data_offset within this SCSI command.
279 * This function is called with connection lock taken.
282 iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
283 struct iscsi_r2t_info *r2t)
285 struct iscsi_data *hdr;
286 struct scsi_cmnd *sc = ctask->sc;
288 struct scatterlist *sg;
290 hdr = &r2t->dtask.hdr;
291 memset(hdr, 0, sizeof(struct iscsi_data));
293 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
294 r2t->solicit_datasn++;
295 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
296 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
297 hdr->itt = ctask->hdr->itt;
298 hdr->exp_statsn = r2t->exp_statsn;
299 hdr->offset = cpu_to_be32(r2t->data_offset);
300 if (r2t->data_length > conn->max_xmit_dlength) {
301 hton24(hdr->dlength, conn->max_xmit_dlength);
302 r2t->data_count = conn->max_xmit_dlength;
305 hton24(hdr->dlength, r2t->data_length);
306 r2t->data_count = r2t->data_length;
307 hdr->flags = ISCSI_FLAG_CMD_FINAL;
309 conn->dataout_pdus_cnt++;
313 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
314 sizeof(struct iscsi_hdr));
316 sg = scsi_sglist(sc);
318 for (i = 0; i < scsi_sg_count(sc); 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);
343 * iscsi_r2t_rsp - iSCSI R2T Response processing
344 * @conn: iscsi connection
345 * @ctask: scsi command task
348 iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
350 struct iscsi_r2t_info *r2t;
351 struct iscsi_session *session = conn->session;
352 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
353 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
354 struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
355 int r2tsn = be32_to_cpu(rhdr->r2tsn);
358 if (tcp_conn->in.datalen) {
359 printk(KERN_ERR "iscsi_tcp: invalid R2t with datalen %d\n",
360 tcp_conn->in.datalen);
361 return ISCSI_ERR_DATALEN;
364 if (tcp_ctask->exp_datasn != r2tsn){
365 debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
366 __FUNCTION__, tcp_ctask->exp_datasn, r2tsn);
367 return ISCSI_ERR_R2TSN;
370 /* fill-in new R2T associated with the task */
371 spin_lock(&session->lock);
372 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
374 if (!ctask->sc || ctask->mtask ||
375 session->state != ISCSI_STATE_LOGGED_IN) {
376 printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in "
377 "recovery...\n", ctask->itt);
378 spin_unlock(&session->lock);
382 rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
385 r2t->exp_statsn = rhdr->statsn;
386 r2t->data_length = be32_to_cpu(rhdr->data_length);
387 if (r2t->data_length == 0) {
388 printk(KERN_ERR "iscsi_tcp: invalid R2T with zero data len\n");
389 spin_unlock(&session->lock);
390 return ISCSI_ERR_DATALEN;
393 if (r2t->data_length > session->max_burst)
394 debug_scsi("invalid R2T with data len %u and max burst %u."
395 "Attempting to execute request.\n",
396 r2t->data_length, session->max_burst);
398 r2t->data_offset = be32_to_cpu(rhdr->data_offset);
399 if (r2t->data_offset + r2t->data_length > scsi_bufflen(ctask->sc)) {
400 spin_unlock(&session->lock);
401 printk(KERN_ERR "iscsi_tcp: invalid R2T with data len %u at "
402 "offset %u and total length %d\n", r2t->data_length,
403 r2t->data_offset, scsi_bufflen(ctask->sc));
404 return ISCSI_ERR_DATALEN;
407 r2t->ttt = rhdr->ttt; /* no flip */
408 r2t->solicit_datasn = 0;
410 iscsi_solicit_data_init(conn, ctask, r2t);
412 tcp_ctask->exp_datasn = r2tsn + 1;
413 __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
414 tcp_ctask->xmstate |= XMSTATE_SOL_HDR_INIT;
415 list_move_tail(&ctask->running, &conn->xmitqueue);
417 scsi_queue_work(session->host, &conn->xmitwork);
418 conn->r2t_pdus_cnt++;
419 spin_unlock(&session->lock);
425 iscsi_tcp_hdr_recv(struct iscsi_conn *conn)
427 int rc = 0, opcode, ahslen;
428 struct iscsi_hdr *hdr;
429 struct iscsi_session *session = conn->session;
430 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
431 uint32_t cdgst, rdgst = 0, itt;
433 hdr = tcp_conn->in.hdr;
435 /* verify PDU length */
436 tcp_conn->in.datalen = ntoh24(hdr->dlength);
437 if (tcp_conn->in.datalen > conn->max_recv_dlength) {
438 printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
439 tcp_conn->in.datalen, conn->max_recv_dlength);
440 return ISCSI_ERR_DATALEN;
442 tcp_conn->data_copied = 0;
445 ahslen = hdr->hlength << 2;
446 tcp_conn->in.offset += ahslen;
447 tcp_conn->in.copy -= ahslen;
448 if (tcp_conn->in.copy < 0) {
449 printk(KERN_ERR "iscsi_tcp: can't handle AHS with length "
450 "%d bytes\n", ahslen);
451 return ISCSI_ERR_AHSLEN;
454 /* calculate read padding */
455 tcp_conn->in.padding = tcp_conn->in.datalen & (ISCSI_PAD_LEN-1);
456 if (tcp_conn->in.padding) {
457 tcp_conn->in.padding = ISCSI_PAD_LEN - tcp_conn->in.padding;
458 debug_scsi("read padding %d bytes\n", tcp_conn->in.padding);
461 if (conn->hdrdgst_en) {
462 struct scatterlist sg;
464 sg_init_one(&sg, (u8 *)hdr,
465 sizeof(struct iscsi_hdr) + ahslen);
466 crypto_hash_digest(&tcp_conn->rx_hash, &sg, sg.length,
468 rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) +
470 if (cdgst != rdgst) {
471 printk(KERN_ERR "iscsi_tcp: hdrdgst error "
472 "recv 0x%x calc 0x%x\n", rdgst, cdgst);
473 return ISCSI_ERR_HDR_DGST;
477 opcode = hdr->opcode & ISCSI_OPCODE_MASK;
478 /* verify itt (itt encoding: age+cid+itt) */
479 rc = iscsi_verify_itt(conn, hdr, &itt);
480 if (rc == ISCSI_ERR_NO_SCSI_CMD) {
481 tcp_conn->in.datalen = 0; /* force drop */
486 debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n",
487 opcode, tcp_conn->in.offset, tcp_conn->in.copy,
488 ahslen, tcp_conn->in.datalen);
491 case ISCSI_OP_SCSI_DATA_IN:
492 tcp_conn->in.ctask = session->cmds[itt];
493 rc = iscsi_data_rsp(conn, tcp_conn->in.ctask);
497 case ISCSI_OP_SCSI_CMD_RSP:
498 tcp_conn->in.ctask = session->cmds[itt];
499 if (tcp_conn->in.datalen)
502 spin_lock(&session->lock);
503 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
504 spin_unlock(&session->lock);
507 tcp_conn->in.ctask = session->cmds[itt];
509 rc = ISCSI_ERR_AHSLEN;
510 else if (tcp_conn->in.ctask->sc->sc_data_direction ==
512 rc = iscsi_r2t_rsp(conn, tcp_conn->in.ctask);
514 rc = ISCSI_ERR_PROTO;
516 case ISCSI_OP_LOGIN_RSP:
517 case ISCSI_OP_TEXT_RSP:
518 case ISCSI_OP_REJECT:
519 case ISCSI_OP_ASYNC_EVENT:
521 * It is possible that we could get a PDU with a buffer larger
522 * than 8K, but there are no targets that currently do this.
523 * For now we fail until we find a vendor that needs it
525 if (ISCSI_DEF_MAX_RECV_SEG_LEN <
526 tcp_conn->in.datalen) {
527 printk(KERN_ERR "iscsi_tcp: received buffer of len %u "
528 "but conn buffer is only %u (opcode %0x)\n",
529 tcp_conn->in.datalen,
530 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
531 rc = ISCSI_ERR_PROTO;
535 if (tcp_conn->in.datalen)
538 case ISCSI_OP_LOGOUT_RSP:
539 case ISCSI_OP_NOOP_IN:
540 case ISCSI_OP_SCSI_TMFUNC_RSP:
541 rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
544 rc = ISCSI_ERR_BAD_OPCODE;
552 * if we did zero copy for the header but we will need multiple
553 * skbs to complete the command then we have to copy the header
556 if (tcp_conn->in.zero_copy_hdr && tcp_conn->in.copy <=
557 (tcp_conn->in.datalen + tcp_conn->in.padding +
558 (conn->datadgst_en ? 4 : 0))) {
559 debug_tcp("Copying header for later use. in.copy %d in.datalen"
560 " %d\n", tcp_conn->in.copy, tcp_conn->in.datalen);
561 memcpy(&tcp_conn->hdr, tcp_conn->in.hdr,
562 sizeof(struct iscsi_hdr));
563 tcp_conn->in.hdr = &tcp_conn->hdr;
564 tcp_conn->in.zero_copy_hdr = 0;
570 * iscsi_ctask_copy - copy skb bits to the destanation cmd task
571 * @conn: iscsi tcp connection
572 * @ctask: scsi command task
573 * @buf: buffer to copy to
574 * @buf_size: size of buffer
575 * @offset: offset within the buffer
578 * The function calls skb_copy_bits() and updates per-connection and
579 * per-cmd byte counters.
581 * Read counters (in bytes):
583 * conn->in.offset offset within in progress SKB
584 * conn->in.copy left to copy from in progress SKB
586 * conn->in.copied copied already from in progress SKB
587 * conn->data_copied copied already from in progress buffer
588 * ctask->sent total bytes sent up to the MidLayer
589 * ctask->data_count left to copy from in progress Data-In
590 * buf_left left to copy from in progress buffer
593 iscsi_ctask_copy(struct iscsi_tcp_conn *tcp_conn, struct iscsi_cmd_task *ctask,
594 void *buf, int buf_size, int offset)
596 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
597 int buf_left = buf_size - (tcp_conn->data_copied + offset);
598 unsigned size = min(tcp_conn->in.copy, buf_left);
601 size = min(size, ctask->data_count);
603 debug_tcp("ctask_copy %d bytes at offset %d copied %d\n",
604 size, tcp_conn->in.offset, tcp_conn->in.copied);
607 BUG_ON(tcp_ctask->sent + size > scsi_bufflen(ctask->sc));
609 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
610 (char*)buf + (offset + tcp_conn->data_copied), size);
611 /* must fit into skb->len */
614 tcp_conn->in.offset += size;
615 tcp_conn->in.copy -= size;
616 tcp_conn->in.copied += size;
617 tcp_conn->data_copied += size;
618 tcp_ctask->sent += size;
619 ctask->data_count -= size;
621 BUG_ON(tcp_conn->in.copy < 0);
622 BUG_ON(ctask->data_count < 0);
624 if (buf_size != (tcp_conn->data_copied + offset)) {
625 if (!ctask->data_count) {
626 BUG_ON(buf_size - tcp_conn->data_copied < 0);
627 /* done with this PDU */
628 return buf_size - tcp_conn->data_copied;
633 /* done with this buffer or with both - PDU and buffer */
634 tcp_conn->data_copied = 0;
639 * iscsi_tcp_copy - copy skb bits to the destanation buffer
640 * @conn: iscsi tcp connection
643 * The function calls skb_copy_bits() and updates per-connection
647 iscsi_tcp_copy(struct iscsi_conn *conn, int buf_size)
649 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
650 int buf_left = buf_size - tcp_conn->data_copied;
651 int size = min(tcp_conn->in.copy, buf_left);
654 debug_tcp("tcp_copy %d bytes at offset %d copied %d\n",
655 size, tcp_conn->in.offset, tcp_conn->data_copied);
658 rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
659 (char*)conn->data + tcp_conn->data_copied, size);
662 tcp_conn->in.offset += size;
663 tcp_conn->in.copy -= size;
664 tcp_conn->in.copied += size;
665 tcp_conn->data_copied += size;
667 if (buf_size != tcp_conn->data_copied)
674 partial_sg_digest_update(struct hash_desc *desc, struct scatterlist *sg,
675 int offset, int length)
677 struct scatterlist temp;
679 memcpy(&temp, sg, sizeof(struct scatterlist));
680 temp.offset = offset;
681 temp.length = length;
682 crypto_hash_update(desc, &temp, length);
686 iscsi_recv_digest_update(struct iscsi_tcp_conn *tcp_conn, char* buf, int len)
688 struct scatterlist tmp;
690 sg_init_one(&tmp, buf, len);
691 crypto_hash_update(&tcp_conn->rx_hash, &tmp, len);
694 static int iscsi_scsi_data_in(struct iscsi_conn *conn)
696 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
697 struct iscsi_cmd_task *ctask = tcp_conn->in.ctask;
698 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
699 struct scsi_cmnd *sc = ctask->sc;
700 struct scatterlist *sg;
701 int i, offset, rc = 0;
703 BUG_ON((void*)ctask != sc->SCp.ptr);
705 offset = tcp_ctask->data_offset;
706 sg = scsi_sglist(sc);
708 if (tcp_ctask->data_offset)
709 for (i = 0; i < tcp_ctask->sg_count; i++)
710 offset -= sg[i].length;
711 /* we've passed through partial sg*/
715 for (i = tcp_ctask->sg_count; i < scsi_sg_count(sc); i++) {
718 dest = kmap_atomic(sg_page(&sg[i]), KM_SOFTIRQ0);
719 rc = iscsi_ctask_copy(tcp_conn, ctask, dest + sg[i].offset,
720 sg[i].length, offset);
721 kunmap_atomic(dest, KM_SOFTIRQ0);
723 /* continue with the next SKB/PDU */
726 if (conn->datadgst_en) {
730 &sg[i], sg[i].length);
732 partial_sg_digest_update(
735 sg[i].offset + offset,
736 sg[i].length - offset);
739 tcp_ctask->sg_count++;
742 if (!ctask->data_count) {
743 if (rc && conn->datadgst_en)
745 * data-in is complete, but buffer not...
747 partial_sg_digest_update(&tcp_conn->rx_hash,
755 if (!tcp_conn->in.copy)
758 BUG_ON(ctask->data_count);
760 /* check for non-exceptional status */
761 if (tcp_conn->in.hdr->flags & ISCSI_FLAG_DATA_STATUS) {
762 debug_scsi("done [sc %lx res %d itt 0x%x flags 0x%x]\n",
763 (long)sc, sc->result, ctask->itt,
764 tcp_conn->in.hdr->flags);
765 spin_lock(&conn->session->lock);
766 __iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
767 spin_unlock(&conn->session->lock);
774 iscsi_data_recv(struct iscsi_conn *conn)
776 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
779 opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
781 case ISCSI_OP_SCSI_DATA_IN:
782 rc = iscsi_scsi_data_in(conn);
784 case ISCSI_OP_SCSI_CMD_RSP:
785 case ISCSI_OP_TEXT_RSP:
786 case ISCSI_OP_LOGIN_RSP:
787 case ISCSI_OP_ASYNC_EVENT:
788 case ISCSI_OP_REJECT:
790 * Collect data segment to the connection's data
793 if (iscsi_tcp_copy(conn, tcp_conn->in.datalen)) {
798 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, conn->data,
799 tcp_conn->in.datalen);
800 if (!rc && conn->datadgst_en && opcode != ISCSI_OP_LOGIN_RSP)
801 iscsi_recv_digest_update(tcp_conn, conn->data,
802 tcp_conn->in.datalen);
812 * iscsi_tcp_data_recv - TCP receive in sendfile fashion
813 * @rd_desc: read descriptor
814 * @skb: socket buffer
815 * @offset: offset in skb
816 * @len: skb->len - offset
819 iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
820 unsigned int offset, size_t len)
823 struct iscsi_conn *conn = rd_desc->arg.data;
824 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
826 char pad[ISCSI_PAD_LEN];
827 struct scatterlist sg;
830 * Save current SKB and its offset in the corresponding
831 * connection context.
833 tcp_conn->in.copy = skb->len - offset;
834 tcp_conn->in.offset = offset;
835 tcp_conn->in.skb = skb;
836 tcp_conn->in.len = tcp_conn->in.copy;
837 BUG_ON(tcp_conn->in.copy <= 0);
838 debug_tcp("in %d bytes\n", tcp_conn->in.copy);
841 tcp_conn->in.copied = 0;
844 if (unlikely(conn->suspend_rx)) {
845 debug_tcp("conn %d Rx suspended!\n", conn->id);
849 if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER ||
850 tcp_conn->in_progress == IN_PROGRESS_HEADER_GATHER) {
851 rc = iscsi_hdr_extract(tcp_conn);
856 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
862 * Verify and process incoming PDU header.
864 rc = iscsi_tcp_hdr_recv(conn);
865 if (!rc && tcp_conn->in.datalen) {
866 if (conn->datadgst_en)
867 crypto_hash_init(&tcp_conn->rx_hash);
868 tcp_conn->in_progress = IN_PROGRESS_DATA_RECV;
870 iscsi_conn_failure(conn, rc);
875 if (tcp_conn->in_progress == IN_PROGRESS_DDIGEST_RECV &&
877 uint32_t recv_digest;
879 debug_tcp("extra data_recv offset %d copy %d\n",
880 tcp_conn->in.offset, tcp_conn->in.copy);
882 if (!tcp_conn->data_copied) {
883 if (tcp_conn->in.padding) {
884 debug_tcp("padding -> %d\n",
885 tcp_conn->in.padding);
886 memset(pad, 0, tcp_conn->in.padding);
887 sg_init_one(&sg, pad, tcp_conn->in.padding);
888 crypto_hash_update(&tcp_conn->rx_hash,
891 crypto_hash_final(&tcp_conn->rx_hash,
892 (u8 *) &tcp_conn->in.datadgst);
893 debug_tcp("rx digest 0x%x\n", tcp_conn->in.datadgst);
896 rc = iscsi_tcp_copy(conn, sizeof(uint32_t));
900 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
904 memcpy(&recv_digest, conn->data, sizeof(uint32_t));
905 if (recv_digest != tcp_conn->in.datadgst) {
906 debug_tcp("iscsi_tcp: data digest error!"
907 "0x%x != 0x%x\n", recv_digest,
908 tcp_conn->in.datadgst);
909 iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
912 debug_tcp("iscsi_tcp: data digest match!"
913 "0x%x == 0x%x\n", recv_digest,
914 tcp_conn->in.datadgst);
915 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
919 if (tcp_conn->in_progress == IN_PROGRESS_DATA_RECV &&
921 debug_tcp("data_recv offset %d copy %d\n",
922 tcp_conn->in.offset, tcp_conn->in.copy);
924 rc = iscsi_data_recv(conn);
928 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
932 if (tcp_conn->in.padding)
933 tcp_conn->in_progress = IN_PROGRESS_PAD_RECV;
934 else if (conn->datadgst_en)
935 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
937 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
938 tcp_conn->data_copied = 0;
941 if (tcp_conn->in_progress == IN_PROGRESS_PAD_RECV &&
943 int copylen = min(tcp_conn->in.padding - tcp_conn->data_copied,
946 tcp_conn->in.copy -= copylen;
947 tcp_conn->in.offset += copylen;
948 tcp_conn->data_copied += copylen;
950 if (tcp_conn->data_copied != tcp_conn->in.padding)
951 tcp_conn->in_progress = IN_PROGRESS_PAD_RECV;
952 else if (conn->datadgst_en)
953 tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
955 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
956 tcp_conn->data_copied = 0;
959 debug_tcp("f, processed %d from out of %d padding %d\n",
960 tcp_conn->in.offset - offset, (int)len, tcp_conn->in.padding);
961 BUG_ON(tcp_conn->in.offset - offset > len);
963 if (tcp_conn->in.offset - offset != len) {
964 debug_tcp("continue to process %d bytes\n",
965 (int)len - (tcp_conn->in.offset - offset));
970 processed = tcp_conn->in.offset - offset;
971 BUG_ON(processed == 0);
975 processed = tcp_conn->in.offset - offset;
976 debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n",
977 processed, (int)len, (int)rd_desc->count);
978 BUG_ON(processed == 0);
979 BUG_ON(processed > len);
981 conn->rxdata_octets += processed;
986 iscsi_tcp_data_ready(struct sock *sk, int flag)
988 struct iscsi_conn *conn = sk->sk_user_data;
989 read_descriptor_t rd_desc;
991 read_lock(&sk->sk_callback_lock);
994 * Use rd_desc to pass 'conn' to iscsi_tcp_data_recv.
995 * We set count to 1 because we want the network layer to
996 * hand us all the skbs that are available. iscsi_tcp_data_recv
997 * handled pdus that cross buffers or pdus that still need data.
999 rd_desc.arg.data = conn;
1001 tcp_read_sock(sk, &rd_desc, iscsi_tcp_data_recv);
1003 read_unlock(&sk->sk_callback_lock);
1007 iscsi_tcp_state_change(struct sock *sk)
1009 struct iscsi_tcp_conn *tcp_conn;
1010 struct iscsi_conn *conn;
1011 struct iscsi_session *session;
1012 void (*old_state_change)(struct sock *);
1014 read_lock(&sk->sk_callback_lock);
1016 conn = (struct iscsi_conn*)sk->sk_user_data;
1017 session = conn->session;
1019 if ((sk->sk_state == TCP_CLOSE_WAIT ||
1020 sk->sk_state == TCP_CLOSE) &&
1021 !atomic_read(&sk->sk_rmem_alloc)) {
1022 debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
1023 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1026 tcp_conn = conn->dd_data;
1027 old_state_change = tcp_conn->old_state_change;
1029 read_unlock(&sk->sk_callback_lock);
1031 old_state_change(sk);
1035 * iscsi_write_space - Called when more output buffer space is available
1036 * @sk: socket space is available for
1039 iscsi_write_space(struct sock *sk)
1041 struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
1042 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1044 tcp_conn->old_write_space(sk);
1045 debug_tcp("iscsi_write_space: cid %d\n", conn->id);
1046 scsi_queue_work(conn->session->host, &conn->xmitwork);
1050 iscsi_conn_set_callbacks(struct iscsi_conn *conn)
1052 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1053 struct sock *sk = tcp_conn->sock->sk;
1055 /* assign new callbacks */
1056 write_lock_bh(&sk->sk_callback_lock);
1057 sk->sk_user_data = conn;
1058 tcp_conn->old_data_ready = sk->sk_data_ready;
1059 tcp_conn->old_state_change = sk->sk_state_change;
1060 tcp_conn->old_write_space = sk->sk_write_space;
1061 sk->sk_data_ready = iscsi_tcp_data_ready;
1062 sk->sk_state_change = iscsi_tcp_state_change;
1063 sk->sk_write_space = iscsi_write_space;
1064 write_unlock_bh(&sk->sk_callback_lock);
1068 iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
1070 struct sock *sk = tcp_conn->sock->sk;
1072 /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
1073 write_lock_bh(&sk->sk_callback_lock);
1074 sk->sk_user_data = NULL;
1075 sk->sk_data_ready = tcp_conn->old_data_ready;
1076 sk->sk_state_change = tcp_conn->old_state_change;
1077 sk->sk_write_space = tcp_conn->old_write_space;
1078 sk->sk_no_check = 0;
1079 write_unlock_bh(&sk->sk_callback_lock);
1083 * iscsi_send - generic send routine
1084 * @sk: kernel's socket
1085 * @buf: buffer to write from
1086 * @size: actual size to write
1087 * @flags: socket's flags
1090 iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags)
1092 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1093 struct socket *sk = tcp_conn->sock;
1094 int offset = buf->sg.offset + buf->sent, res;
1097 * if we got use_sg=0 or are sending something we kmallocd
1098 * then we did not have to do kmap (kmap returns page_address)
1100 * if we got use_sg > 0, but had to drop down, we do not
1101 * set clustering so this should only happen for that
1104 if (buf->use_sendmsg)
1105 res = sock_no_sendpage(sk, sg_page(&buf->sg), offset, size, flags);
1107 res = tcp_conn->sendpage(sk, sg_page(&buf->sg), offset, size, flags);
1110 conn->txdata_octets += res;
1115 tcp_conn->sendpage_failures_cnt++;
1119 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1124 * iscsi_sendhdr - send PDU Header via tcp_sendpage()
1125 * @conn: iscsi connection
1126 * @buf: buffer to write from
1127 * @datalen: lenght of data to be sent after the header
1133 iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
1135 int flags = 0; /* MSG_DONTWAIT; */
1138 size = buf->sg.length - buf->sent;
1139 BUG_ON(buf->sent + size > buf->sg.length);
1140 if (buf->sent + size != buf->sg.length || datalen)
1143 res = iscsi_send(conn, buf, size, flags);
1144 debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res);
1155 * iscsi_sendpage - send one page of iSCSI Data-Out.
1156 * @conn: iscsi connection
1157 * @buf: buffer to write from
1158 * @count: remaining data
1159 * @sent: number of bytes sent
1165 iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
1166 int *count, int *sent)
1168 int flags = 0; /* MSG_DONTWAIT; */
1171 size = buf->sg.length - buf->sent;
1172 BUG_ON(buf->sent + size > buf->sg.length);
1175 if (buf->sent + size != buf->sg.length || *count != size)
1178 res = iscsi_send(conn, buf, size, flags);
1179 debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n",
1180 size, buf->sent, *count, *sent, res);
1193 iscsi_data_digest_init(struct iscsi_tcp_conn *tcp_conn,
1194 struct iscsi_tcp_cmd_task *tcp_ctask)
1196 crypto_hash_init(&tcp_conn->tx_hash);
1197 tcp_ctask->digest_count = 4;
1201 * iscsi_solicit_data_cont - initialize next Data-Out
1202 * @conn: iscsi connection
1203 * @ctask: scsi command task
1205 * @left: bytes left to transfer
1208 * Initialize next Data-Out within this R2T sequence and continue
1209 * to process next Scatter-Gather element(if any) of this SCSI command.
1211 * Called under connection lock.
1214 iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1215 struct iscsi_r2t_info *r2t, int left)
1217 struct iscsi_data *hdr;
1220 hdr = &r2t->dtask.hdr;
1221 memset(hdr, 0, sizeof(struct iscsi_data));
1222 hdr->ttt = r2t->ttt;
1223 hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
1224 r2t->solicit_datasn++;
1225 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
1226 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1227 hdr->itt = ctask->hdr->itt;
1228 hdr->exp_statsn = r2t->exp_statsn;
1229 new_offset = r2t->data_offset + r2t->sent;
1230 hdr->offset = cpu_to_be32(new_offset);
1231 if (left > conn->max_xmit_dlength) {
1232 hton24(hdr->dlength, conn->max_xmit_dlength);
1233 r2t->data_count = conn->max_xmit_dlength;
1235 hton24(hdr->dlength, left);
1236 r2t->data_count = left;
1237 hdr->flags = ISCSI_FLAG_CMD_FINAL;
1239 conn->dataout_pdus_cnt++;
1241 iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
1242 sizeof(struct iscsi_hdr));
1244 if (iscsi_buf_left(&r2t->sendbuf))
1247 iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
1251 static void iscsi_set_padding(struct iscsi_tcp_cmd_task *tcp_ctask,
1254 tcp_ctask->pad_count = len & (ISCSI_PAD_LEN - 1);
1255 if (!tcp_ctask->pad_count)
1258 tcp_ctask->pad_count = ISCSI_PAD_LEN - tcp_ctask->pad_count;
1259 debug_scsi("write padding %d bytes\n", tcp_ctask->pad_count);
1260 tcp_ctask->xmstate |= XMSTATE_W_PAD;
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 iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1274 BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
1275 tcp_ctask->xmstate = XMSTATE_CMD_HDR_INIT;
1279 * iscsi_tcp_mtask_xmit - xmit management(immediate) task
1280 * @conn: iscsi connection
1281 * @mtask: task management task
1284 * The function can return -EAGAIN in which case caller must
1285 * call it again later, or recover. '0' return code means successful
1288 * Management xmit state machine consists of these states:
1289 * XMSTATE_IMM_HDR_INIT - calculate digest of PDU Header
1290 * XMSTATE_IMM_HDR - PDU Header xmit in progress
1291 * XMSTATE_IMM_DATA - PDU Data xmit in progress
1292 * XMSTATE_IDLE - management PDU is done
1295 iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
1297 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
1300 debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
1301 conn->id, tcp_mtask->xmstate, mtask->itt);
1303 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR_INIT) {
1304 iscsi_buf_init_iov(&tcp_mtask->headbuf, (char*)mtask->hdr,
1305 sizeof(struct iscsi_hdr));
1307 if (mtask->data_count) {
1308 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
1309 iscsi_buf_init_iov(&tcp_mtask->sendbuf,
1314 if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
1315 conn->stop_stage != STOP_CONN_RECOVER &&
1317 iscsi_hdr_digest(conn, &tcp_mtask->headbuf,
1318 (u8*)tcp_mtask->hdrext);
1320 tcp_mtask->sent = 0;
1321 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR_INIT;
1322 tcp_mtask->xmstate |= XMSTATE_IMM_HDR;
1325 if (tcp_mtask->xmstate & XMSTATE_IMM_HDR) {
1326 rc = iscsi_sendhdr(conn, &tcp_mtask->headbuf,
1330 tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR;
1333 if (tcp_mtask->xmstate & XMSTATE_IMM_DATA) {
1334 BUG_ON(!mtask->data_count);
1335 tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
1336 /* FIXME: implement.
1337 * Virtual buffer could be spreaded across multiple pages...
1342 rc = iscsi_sendpage(conn, &tcp_mtask->sendbuf,
1343 &mtask->data_count, &tcp_mtask->sent);
1345 tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
1348 } while (mtask->data_count);
1351 BUG_ON(tcp_mtask->xmstate != XMSTATE_IDLE);
1352 if (mtask->hdr->itt == RESERVED_ITT) {
1353 struct iscsi_session *session = conn->session;
1355 spin_lock_bh(&session->lock);
1356 list_del(&conn->mtask->running);
1357 __kfifo_put(session->mgmtpool.queue, (void*)&conn->mtask,
1359 spin_unlock_bh(&session->lock);
1365 iscsi_send_cmd_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1367 struct scsi_cmnd *sc = ctask->sc;
1368 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1371 if (tcp_ctask->xmstate & XMSTATE_CMD_HDR_INIT) {
1372 tcp_ctask->sent = 0;
1373 tcp_ctask->sg_count = 0;
1374 tcp_ctask->exp_datasn = 0;
1376 if (sc->sc_data_direction == DMA_TO_DEVICE) {
1377 struct scatterlist *sg = scsi_sglist(sc);
1379 iscsi_buf_init_sg(&tcp_ctask->sendbuf, sg);
1380 tcp_ctask->sg = sg + 1;
1381 tcp_ctask->bad_sg = sg + scsi_sg_count(sc);
1383 debug_scsi("cmd [itt 0x%x total %d imm_data %d "
1384 "unsol count %d, unsol offset %d]\n",
1385 ctask->itt, scsi_bufflen(sc),
1386 ctask->imm_count, ctask->unsol_count,
1387 ctask->unsol_offset);
1390 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)ctask->hdr,
1391 sizeof(struct iscsi_hdr));
1393 if (conn->hdrdgst_en)
1394 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1395 (u8*)tcp_ctask->hdrext);
1396 tcp_ctask->xmstate &= ~XMSTATE_CMD_HDR_INIT;
1397 tcp_ctask->xmstate |= XMSTATE_CMD_HDR_XMIT;
1400 if (tcp_ctask->xmstate & XMSTATE_CMD_HDR_XMIT) {
1401 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->imm_count);
1404 tcp_ctask->xmstate &= ~XMSTATE_CMD_HDR_XMIT;
1406 if (sc->sc_data_direction != DMA_TO_DEVICE)
1409 if (ctask->imm_count) {
1410 tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
1411 iscsi_set_padding(tcp_ctask, ctask->imm_count);
1413 if (ctask->conn->datadgst_en) {
1414 iscsi_data_digest_init(ctask->conn->dd_data,
1416 tcp_ctask->immdigest = 0;
1420 if (ctask->unsol_count)
1421 tcp_ctask->xmstate |=
1422 XMSTATE_UNS_HDR | XMSTATE_UNS_INIT;
1428 iscsi_send_padding(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1430 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1431 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1434 if (tcp_ctask->xmstate & XMSTATE_W_PAD) {
1435 iscsi_buf_init_iov(&tcp_ctask->sendbuf, (char*)&tcp_ctask->pad,
1436 tcp_ctask->pad_count);
1437 if (conn->datadgst_en)
1438 crypto_hash_update(&tcp_conn->tx_hash,
1439 &tcp_ctask->sendbuf.sg,
1440 tcp_ctask->sendbuf.sg.length);
1441 } else if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_PAD))
1444 tcp_ctask->xmstate &= ~XMSTATE_W_PAD;
1445 tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_PAD;
1446 debug_scsi("sending %d pad bytes for itt 0x%x\n",
1447 tcp_ctask->pad_count, ctask->itt);
1448 rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf, &tcp_ctask->pad_count,
1451 debug_scsi("padding send failed %d\n", rc);
1452 tcp_ctask->xmstate |= XMSTATE_W_RESEND_PAD;
1458 iscsi_send_digest(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1459 struct iscsi_buf *buf, uint32_t *digest)
1461 struct iscsi_tcp_cmd_task *tcp_ctask;
1462 struct iscsi_tcp_conn *tcp_conn;
1465 if (!conn->datadgst_en)
1468 tcp_ctask = ctask->dd_data;
1469 tcp_conn = conn->dd_data;
1471 if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_DATA_DIGEST)) {
1472 crypto_hash_final(&tcp_conn->tx_hash, (u8*)digest);
1473 iscsi_buf_init_iov(buf, (char*)digest, 4);
1475 tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_DATA_DIGEST;
1477 rc = iscsi_sendpage(conn, buf, &tcp_ctask->digest_count, &sent);
1479 debug_scsi("sent digest 0x%x for itt 0x%x\n", *digest,
1482 debug_scsi("sending digest 0x%x failed for itt 0x%x!\n",
1483 *digest, ctask->itt);
1484 tcp_ctask->xmstate |= XMSTATE_W_RESEND_DATA_DIGEST;
1490 iscsi_send_data(struct iscsi_cmd_task *ctask, struct iscsi_buf *sendbuf,
1491 struct scatterlist **sg, int *sent, int *count,
1492 struct iscsi_buf *digestbuf, uint32_t *digest)
1494 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1495 struct iscsi_conn *conn = ctask->conn;
1496 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1497 int rc, buf_sent, offset;
1501 offset = sendbuf->sent;
1503 rc = iscsi_sendpage(conn, sendbuf, count, &buf_sent);
1504 *sent = *sent + buf_sent;
1505 if (buf_sent && conn->datadgst_en)
1506 partial_sg_digest_update(&tcp_conn->tx_hash,
1507 &sendbuf->sg, sendbuf->sg.offset + offset,
1509 if (!iscsi_buf_left(sendbuf) && *sg != tcp_ctask->bad_sg) {
1510 iscsi_buf_init_sg(sendbuf, *sg);
1518 rc = iscsi_send_padding(conn, ctask);
1522 return iscsi_send_digest(conn, ctask, digestbuf, digest);
1526 iscsi_send_unsol_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1528 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1529 struct iscsi_data_task *dtask;
1532 tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
1533 if (tcp_ctask->xmstate & XMSTATE_UNS_INIT) {
1534 dtask = &tcp_ctask->unsol_dtask;
1536 iscsi_prep_unsolicit_data_pdu(ctask, &dtask->hdr);
1537 iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)&dtask->hdr,
1538 sizeof(struct iscsi_hdr));
1539 if (conn->hdrdgst_en)
1540 iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
1541 (u8*)dtask->hdrext);
1543 tcp_ctask->xmstate &= ~XMSTATE_UNS_INIT;
1544 iscsi_set_padding(tcp_ctask, ctask->data_count);
1547 rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->data_count);
1549 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1550 tcp_ctask->xmstate |= XMSTATE_UNS_HDR;
1554 if (conn->datadgst_en) {
1555 dtask = &tcp_ctask->unsol_dtask;
1556 iscsi_data_digest_init(ctask->conn->dd_data, tcp_ctask);
1560 debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
1561 ctask->itt, ctask->unsol_count, tcp_ctask->sent);
1566 iscsi_send_unsol_pdu(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1568 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1571 if (tcp_ctask->xmstate & XMSTATE_UNS_HDR) {
1572 BUG_ON(!ctask->unsol_count);
1573 tcp_ctask->xmstate &= ~XMSTATE_UNS_HDR;
1575 rc = iscsi_send_unsol_hdr(conn, ctask);
1580 if (tcp_ctask->xmstate & XMSTATE_UNS_DATA) {
1581 struct iscsi_data_task *dtask = &tcp_ctask->unsol_dtask;
1582 int start = tcp_ctask->sent;
1584 rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg,
1585 &tcp_ctask->sent, &ctask->data_count,
1586 &dtask->digestbuf, &dtask->digest);
1587 ctask->unsol_count -= tcp_ctask->sent - start;
1590 tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
1592 * Done with the Data-Out. Next, check if we need
1593 * to send another unsolicited Data-Out.
1595 if (ctask->unsol_count) {
1596 debug_scsi("sending more uns\n");
1597 tcp_ctask->xmstate |= XMSTATE_UNS_INIT;
1604 static int iscsi_send_sol_pdu(struct iscsi_conn *conn,
1605 struct iscsi_cmd_task *ctask)
1607 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1608 struct iscsi_session *session = conn->session;
1609 struct iscsi_r2t_info *r2t;
1610 struct iscsi_data_task *dtask;
1613 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR_INIT) {
1614 if (!tcp_ctask->r2t) {
1615 spin_lock_bh(&session->lock);
1616 __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
1618 spin_unlock_bh(&session->lock);
1621 r2t = tcp_ctask->r2t;
1622 dtask = &r2t->dtask;
1624 if (conn->hdrdgst_en)
1625 iscsi_hdr_digest(conn, &r2t->headbuf,
1626 (u8*)dtask->hdrext);
1627 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR_INIT;
1628 tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
1631 if (tcp_ctask->xmstate & XMSTATE_SOL_HDR) {
1632 r2t = tcp_ctask->r2t;
1633 dtask = &r2t->dtask;
1635 rc = iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count);
1638 tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
1639 tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
1641 if (conn->datadgst_en) {
1642 iscsi_data_digest_init(conn->dd_data, tcp_ctask);
1646 iscsi_set_padding(tcp_ctask, r2t->data_count);
1647 debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n",
1648 r2t->solicit_datasn - 1, ctask->itt, r2t->data_count,
1652 if (tcp_ctask->xmstate & XMSTATE_SOL_DATA) {
1653 r2t = tcp_ctask->r2t;
1654 dtask = &r2t->dtask;
1656 rc = iscsi_send_data(ctask, &r2t->sendbuf, &r2t->sg,
1657 &r2t->sent, &r2t->data_count,
1658 &dtask->digestbuf, &dtask->digest);
1661 tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
1664 * Done with this Data-Out. Next, check if we have
1665 * to send another Data-Out for this R2T.
1667 BUG_ON(r2t->data_length - r2t->sent < 0);
1668 left = r2t->data_length - r2t->sent;
1670 iscsi_solicit_data_cont(conn, ctask, r2t, left);
1675 * Done with this R2T. Check if there are more
1676 * outstanding R2Ts ready to be processed.
1678 spin_lock_bh(&session->lock);
1679 tcp_ctask->r2t = NULL;
1680 __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
1682 if (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t,
1684 tcp_ctask->r2t = r2t;
1685 spin_unlock_bh(&session->lock);
1688 spin_unlock_bh(&session->lock);
1694 * iscsi_tcp_ctask_xmit - xmit normal PDU task
1695 * @conn: iscsi connection
1696 * @ctask: iscsi command task
1699 * The function can return -EAGAIN in which case caller must
1700 * call it again later, or recover. '0' return code means successful
1702 * The function is devided to logical helpers (above) for the different
1705 *iscsi_send_cmd_hdr()
1706 * XMSTATE_CMD_HDR_INIT - prepare Header and Data buffers Calculate
1708 * XMSTATE_CMD_HDR_XMIT - Transmit header in progress
1711 * XMSTATE_W_PAD - Prepare and send pading
1712 * XMSTATE_W_RESEND_PAD - retry send pading
1715 * XMSTATE_W_RESEND_DATA_DIGEST - Finalize and send Data Digest
1716 * XMSTATE_W_RESEND_DATA_DIGEST - retry sending digest
1718 *iscsi_send_unsol_hdr
1719 * XMSTATE_UNS_INIT - prepare un-solicit data header and digest
1720 * XMSTATE_UNS_HDR - send un-solicit header
1722 *iscsi_send_unsol_pdu
1723 * XMSTATE_UNS_DATA - send un-solicit data in progress
1726 * XMSTATE_SOL_HDR_INIT - solicit data header and digest initialize
1727 * XMSTATE_SOL_HDR - send solicit header
1728 * XMSTATE_SOL_DATA - send solicit data
1730 *iscsi_tcp_ctask_xmit
1731 * XMSTATE_IMM_DATA - xmit managment data (??)
1734 iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
1736 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
1739 debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
1740 conn->id, tcp_ctask->xmstate, ctask->itt);
1742 rc = iscsi_send_cmd_hdr(conn, ctask);
1745 if (ctask->sc->sc_data_direction != DMA_TO_DEVICE)
1748 if (tcp_ctask->xmstate & XMSTATE_IMM_DATA) {
1749 rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg,
1750 &tcp_ctask->sent, &ctask->imm_count,
1751 &tcp_ctask->immbuf, &tcp_ctask->immdigest);
1754 tcp_ctask->xmstate &= ~XMSTATE_IMM_DATA;
1757 rc = iscsi_send_unsol_pdu(conn, ctask);
1761 rc = iscsi_send_sol_pdu(conn, ctask);
1768 static struct iscsi_cls_conn *
1769 iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1771 struct iscsi_conn *conn;
1772 struct iscsi_cls_conn *cls_conn;
1773 struct iscsi_tcp_conn *tcp_conn;
1775 cls_conn = iscsi_conn_setup(cls_session, conn_idx);
1778 conn = cls_conn->dd_data;
1780 * due to strange issues with iser these are not set
1781 * in iscsi_conn_setup
1783 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
1785 tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
1787 goto tcp_conn_alloc_fail;
1789 conn->dd_data = tcp_conn;
1790 tcp_conn->iscsi_conn = conn;
1791 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1792 /* initial operational parameters */
1793 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
1795 tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1797 tcp_conn->tx_hash.flags = 0;
1798 if (IS_ERR(tcp_conn->tx_hash.tfm)) {
1799 printk(KERN_ERR "Could not create connection due to crc32c "
1800 "loading error %ld. Make sure the crc32c module is "
1801 "built as a module or into the kernel\n",
1802 PTR_ERR(tcp_conn->tx_hash.tfm));
1806 tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
1808 tcp_conn->rx_hash.flags = 0;
1809 if (IS_ERR(tcp_conn->rx_hash.tfm)) {
1810 printk(KERN_ERR "Could not create connection due to crc32c "
1811 "loading error %ld. Make sure the crc32c module is "
1812 "built as a module or into the kernel\n",
1813 PTR_ERR(tcp_conn->rx_hash.tfm));
1820 crypto_free_hash(tcp_conn->tx_hash.tfm);
1823 tcp_conn_alloc_fail:
1824 iscsi_conn_teardown(cls_conn);
1829 iscsi_tcp_release_conn(struct iscsi_conn *conn)
1831 struct iscsi_session *session = conn->session;
1832 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1833 struct socket *sock = tcp_conn->sock;
1838 sock_hold(sock->sk);
1839 iscsi_conn_restore_callbacks(tcp_conn);
1842 spin_lock_bh(&session->lock);
1843 tcp_conn->sock = NULL;
1844 conn->recv_lock = NULL;
1845 spin_unlock_bh(&session->lock);
1850 iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
1852 struct iscsi_conn *conn = cls_conn->dd_data;
1853 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1855 iscsi_tcp_release_conn(conn);
1856 iscsi_conn_teardown(cls_conn);
1858 if (tcp_conn->tx_hash.tfm)
1859 crypto_free_hash(tcp_conn->tx_hash.tfm);
1860 if (tcp_conn->rx_hash.tfm)
1861 crypto_free_hash(tcp_conn->rx_hash.tfm);
1867 iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1869 struct iscsi_conn *conn = cls_conn->dd_data;
1870 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1872 iscsi_conn_stop(cls_conn, flag);
1873 iscsi_tcp_release_conn(conn);
1874 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
1877 static int iscsi_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
1878 char *buf, int *port,
1879 int (*getname)(struct socket *, struct sockaddr *,
1882 struct sockaddr_storage *addr;
1883 struct sockaddr_in6 *sin6;
1884 struct sockaddr_in *sin;
1887 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
1891 if (getname(sock, (struct sockaddr *) addr, &len)) {
1896 switch (addr->ss_family) {
1898 sin = (struct sockaddr_in *)addr;
1899 spin_lock_bh(&conn->session->lock);
1900 sprintf(buf, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr));
1901 *port = be16_to_cpu(sin->sin_port);
1902 spin_unlock_bh(&conn->session->lock);
1905 sin6 = (struct sockaddr_in6 *)addr;
1906 spin_lock_bh(&conn->session->lock);
1907 sprintf(buf, NIP6_FMT, NIP6(sin6->sin6_addr));
1908 *port = be16_to_cpu(sin6->sin6_port);
1909 spin_unlock_bh(&conn->session->lock);
1918 iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
1919 struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
1922 struct iscsi_conn *conn = cls_conn->dd_data;
1923 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
1925 struct socket *sock;
1928 /* lookup for existing socket */
1929 sock = sockfd_lookup((int)transport_eph, &err);
1931 printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
1935 * copy these values now because if we drop the session
1936 * userspace may still want to query the values since we will
1937 * be using them for the reconnect
1939 err = iscsi_tcp_get_addr(conn, sock, conn->portal_address,
1940 &conn->portal_port, kernel_getpeername);
1944 err = iscsi_tcp_get_addr(conn, sock, conn->local_address,
1945 &conn->local_port, kernel_getsockname);
1949 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
1953 /* bind iSCSI connection and socket */
1954 tcp_conn->sock = sock;
1956 /* setup Socket parameters */
1959 sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
1960 sk->sk_allocation = GFP_ATOMIC;
1962 /* FIXME: disable Nagle's algorithm */
1965 * Intercept TCP callbacks for sendfile like receive
1968 conn->recv_lock = &sk->sk_callback_lock;
1969 iscsi_conn_set_callbacks(conn);
1970 tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
1972 * set receive state machine into initial state
1974 tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
1982 /* called with host lock */
1984 iscsi_tcp_mgmt_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
1986 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
1987 tcp_mtask->xmstate = XMSTATE_IMM_HDR_INIT;
1991 iscsi_r2tpool_alloc(struct iscsi_session *session)
1997 * initialize per-task: R2T pool and xmit queue
1999 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2000 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
2001 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2004 * pre-allocated x4 as much r2ts to handle race when
2005 * target acks DataOut faster than we data_xmit() queues
2006 * could replenish r2tqueue.
2010 if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4,
2011 (void***)&tcp_ctask->r2ts,
2012 sizeof(struct iscsi_r2t_info))) {
2013 goto r2t_alloc_fail;
2016 /* R2T xmit queue */
2017 tcp_ctask->r2tqueue = kfifo_alloc(
2018 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
2019 if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
2020 iscsi_pool_free(&tcp_ctask->r2tpool,
2021 (void**)tcp_ctask->r2ts);
2022 goto r2t_alloc_fail;
2029 for (i = 0; i < cmd_i; i++) {
2030 struct iscsi_cmd_task *ctask = session->cmds[i];
2031 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2033 kfifo_free(tcp_ctask->r2tqueue);
2034 iscsi_pool_free(&tcp_ctask->r2tpool,
2035 (void**)tcp_ctask->r2ts);
2041 iscsi_r2tpool_free(struct iscsi_session *session)
2045 for (i = 0; i < session->cmds_max; i++) {
2046 struct iscsi_cmd_task *ctask = session->cmds[i];
2047 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2049 kfifo_free(tcp_ctask->r2tqueue);
2050 iscsi_pool_free(&tcp_ctask->r2tpool,
2051 (void**)tcp_ctask->r2ts);
2056 iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
2057 char *buf, int buflen)
2059 struct iscsi_conn *conn = cls_conn->dd_data;
2060 struct iscsi_session *session = conn->session;
2061 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2065 case ISCSI_PARAM_HDRDGST_EN:
2066 iscsi_set_param(cls_conn, param, buf, buflen);
2067 tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
2068 if (conn->hdrdgst_en)
2069 tcp_conn->hdr_size += sizeof(__u32);
2071 case ISCSI_PARAM_DATADGST_EN:
2072 iscsi_set_param(cls_conn, param, buf, buflen);
2073 tcp_conn->sendpage = conn->datadgst_en ?
2074 sock_no_sendpage : tcp_conn->sock->ops->sendpage;
2076 case ISCSI_PARAM_MAX_R2T:
2077 sscanf(buf, "%d", &value);
2078 if (session->max_r2t == roundup_pow_of_two(value))
2080 iscsi_r2tpool_free(session);
2081 iscsi_set_param(cls_conn, param, buf, buflen);
2082 if (session->max_r2t & (session->max_r2t - 1))
2083 session->max_r2t = roundup_pow_of_two(session->max_r2t);
2084 if (iscsi_r2tpool_alloc(session))
2088 return iscsi_set_param(cls_conn, param, buf, buflen);
2095 iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
2096 enum iscsi_param param, char *buf)
2098 struct iscsi_conn *conn = cls_conn->dd_data;
2102 case ISCSI_PARAM_CONN_PORT:
2103 spin_lock_bh(&conn->session->lock);
2104 len = sprintf(buf, "%hu\n", conn->portal_port);
2105 spin_unlock_bh(&conn->session->lock);
2107 case ISCSI_PARAM_CONN_ADDRESS:
2108 spin_lock_bh(&conn->session->lock);
2109 len = sprintf(buf, "%s\n", conn->portal_address);
2110 spin_unlock_bh(&conn->session->lock);
2113 return iscsi_conn_get_param(cls_conn, param, buf);
2120 iscsi_tcp_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2123 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2127 case ISCSI_HOST_PARAM_IPADDRESS:
2128 spin_lock_bh(&session->lock);
2129 if (!session->leadconn)
2132 len = sprintf(buf, "%s\n",
2133 session->leadconn->local_address);
2134 spin_unlock_bh(&session->lock);
2137 return iscsi_host_get_param(shost, param, buf);
2143 iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
2145 struct iscsi_conn *conn = cls_conn->dd_data;
2146 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
2148 stats->txdata_octets = conn->txdata_octets;
2149 stats->rxdata_octets = conn->rxdata_octets;
2150 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
2151 stats->dataout_pdus = conn->dataout_pdus_cnt;
2152 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
2153 stats->datain_pdus = conn->datain_pdus_cnt;
2154 stats->r2t_pdus = conn->r2t_pdus_cnt;
2155 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
2156 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
2157 stats->custom_length = 3;
2158 strcpy(stats->custom[0].desc, "tx_sendpage_failures");
2159 stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
2160 strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
2161 stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
2162 strcpy(stats->custom[2].desc, "eh_abort_cnt");
2163 stats->custom[2].value = conn->eh_abort_cnt;
2166 static struct iscsi_cls_session *
2167 iscsi_tcp_session_create(struct iscsi_transport *iscsit,
2168 struct scsi_transport_template *scsit,
2169 uint16_t cmds_max, uint16_t qdepth,
2170 uint32_t initial_cmdsn, uint32_t *hostno)
2172 struct iscsi_cls_session *cls_session;
2173 struct iscsi_session *session;
2177 cls_session = iscsi_session_setup(iscsit, scsit, cmds_max, qdepth,
2178 sizeof(struct iscsi_tcp_cmd_task),
2179 sizeof(struct iscsi_tcp_mgmt_task),
2180 initial_cmdsn, &hn);
2185 session = class_to_transport_session(cls_session);
2186 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2187 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
2188 struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
2190 ctask->hdr = &tcp_ctask->hdr;
2193 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
2194 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
2195 struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
2197 mtask->hdr = &tcp_mtask->hdr;
2200 if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
2201 goto r2tpool_alloc_fail;
2206 iscsi_session_teardown(cls_session);
2210 static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
2212 iscsi_r2tpool_free(class_to_transport_session(cls_session));
2213 iscsi_session_teardown(cls_session);
2216 static int iscsi_tcp_slave_configure(struct scsi_device *sdev)
2218 blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
2219 blk_queue_dma_alignment(sdev->request_queue, 0);
2223 static struct scsi_host_template iscsi_sht = {
2224 .module = THIS_MODULE,
2225 .name = "iSCSI Initiator over TCP/IP",
2226 .queuecommand = iscsi_queuecommand,
2227 .change_queue_depth = iscsi_change_queue_depth,
2228 .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
2229 .sg_tablesize = ISCSI_SG_TABLESIZE,
2230 .max_sectors = 0xFFFF,
2231 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
2232 .eh_abort_handler = iscsi_eh_abort,
2233 .eh_host_reset_handler = iscsi_eh_host_reset,
2234 .use_clustering = DISABLE_CLUSTERING,
2235 .slave_configure = iscsi_tcp_slave_configure,
2236 .proc_name = "iscsi_tcp",
2240 static struct iscsi_transport iscsi_tcp_transport = {
2241 .owner = THIS_MODULE,
2243 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
2245 .param_mask = ISCSI_MAX_RECV_DLENGTH |
2246 ISCSI_MAX_XMIT_DLENGTH |
2249 ISCSI_INITIAL_R2T_EN |
2254 ISCSI_PDU_INORDER_EN |
2255 ISCSI_DATASEQ_INORDER_EN |
2258 ISCSI_CONN_ADDRESS |
2260 ISCSI_PERSISTENT_PORT |
2261 ISCSI_PERSISTENT_ADDRESS |
2262 ISCSI_TARGET_NAME | ISCSI_TPGT |
2263 ISCSI_USERNAME | ISCSI_PASSWORD |
2264 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN,
2265 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
2266 ISCSI_HOST_INITIATOR_NAME |
2267 ISCSI_HOST_NETDEV_NAME,
2268 .host_template = &iscsi_sht,
2269 .conndata_size = sizeof(struct iscsi_conn),
2271 .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN,
2272 /* session management */
2273 .create_session = iscsi_tcp_session_create,
2274 .destroy_session = iscsi_tcp_session_destroy,
2275 /* connection management */
2276 .create_conn = iscsi_tcp_conn_create,
2277 .bind_conn = iscsi_tcp_conn_bind,
2278 .destroy_conn = iscsi_tcp_conn_destroy,
2279 .set_param = iscsi_conn_set_param,
2280 .get_conn_param = iscsi_tcp_conn_get_param,
2281 .get_session_param = iscsi_session_get_param,
2282 .start_conn = iscsi_conn_start,
2283 .stop_conn = iscsi_tcp_conn_stop,
2284 /* iscsi host params */
2285 .get_host_param = iscsi_tcp_host_get_param,
2286 .set_host_param = iscsi_host_set_param,
2288 .send_pdu = iscsi_conn_send_pdu,
2289 .get_stats = iscsi_conn_get_stats,
2290 .init_cmd_task = iscsi_tcp_cmd_init,
2291 .init_mgmt_task = iscsi_tcp_mgmt_init,
2292 .xmit_cmd_task = iscsi_tcp_ctask_xmit,
2293 .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
2294 .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
2296 .session_recovery_timedout = iscsi_session_recovery_timedout,
2300 iscsi_tcp_init(void)
2302 if (iscsi_max_lun < 1) {
2303 printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
2307 iscsi_tcp_transport.max_lun = iscsi_max_lun;
2309 if (!iscsi_register_transport(&iscsi_tcp_transport))
2316 iscsi_tcp_exit(void)
2318 iscsi_unregister_transport(&iscsi_tcp_transport);
2321 module_init(iscsi_tcp_init);
2322 module_exit(iscsi_tcp_exit);