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