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