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