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