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