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"); |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 51 | #undef 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 | |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 70 | static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn, |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 71 | struct iscsi_segment *segment); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 72 | |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 73 | /* |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 74 | * Scatterlist handling: inside the iscsi_segment, we |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 75 | * remember an index into the scatterlist, and set data/size |
| 76 | * to the current scatterlist entry. For highmem pages, we |
| 77 | * kmap as needed. |
| 78 | * |
| 79 | * Note that the page is unmapped when we return from |
| 80 | * TCP's data_ready handler, so we may end up mapping and |
| 81 | * unmapping the same page repeatedly. The whole reason |
| 82 | * for this is that we shouldn't keep the page mapped |
| 83 | * outside the softirq. |
| 84 | */ |
| 85 | |
| 86 | /** |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 87 | * iscsi_tcp_segment_init_sg - init indicated scatterlist entry |
| 88 | * @segment: the buffer object |
| 89 | * @sg: scatterlist |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 90 | * @offset: byte offset into that sg entry |
| 91 | * |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 92 | * This function sets up the segment so that subsequent |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 93 | * data is copied to the indicated sg entry, at the given |
| 94 | * offset. |
| 95 | */ |
| 96 | static inline void |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 97 | iscsi_tcp_segment_init_sg(struct iscsi_segment *segment, |
| 98 | struct scatterlist *sg, unsigned int offset) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 99 | { |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 100 | segment->sg = sg; |
| 101 | segment->sg_offset = offset; |
| 102 | segment->size = min(sg->length - offset, |
| 103 | segment->total_size - segment->total_copied); |
| 104 | segment->data = NULL; |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 105 | } |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 106 | |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 107 | /** |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 108 | * iscsi_tcp_segment_map - map the current S/G page |
| 109 | * @segment: iscsi_segment |
| 110 | * @recv: 1 if called from recv path |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 111 | * |
| 112 | * We only need to possibly kmap data if scatter lists are being used, |
| 113 | * because the iscsi passthrough and internal IO paths will never use high |
| 114 | * mem pages. |
| 115 | */ |
| 116 | static inline void |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 117 | iscsi_tcp_segment_map(struct iscsi_segment *segment, int recv) |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 118 | { |
| 119 | struct scatterlist *sg; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 120 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 121 | if (segment->data != NULL || !segment->sg) |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 122 | return; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 123 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 124 | sg = segment->sg; |
| 125 | BUG_ON(segment->sg_mapped); |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 126 | BUG_ON(sg->length == 0); |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 127 | |
| 128 | /* |
| 129 | * If the page count is greater than one it is ok to send |
| 130 | * to the network layer's zero copy send path. If not we |
| 131 | * have to go the slow sendmsg path. We always map for the |
| 132 | * recv path. |
| 133 | */ |
| 134 | if (page_count(sg_page(sg)) >= 1 && !recv) |
| 135 | return; |
| 136 | |
| 137 | debug_tcp("iscsi_tcp_segment_map %s %p\n", recv ? "recv" : "xmit", |
| 138 | segment); |
| 139 | segment->sg_mapped = kmap_atomic(sg_page(sg), KM_SOFTIRQ0); |
| 140 | segment->data = segment->sg_mapped + sg->offset + segment->sg_offset; |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 141 | } |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 142 | |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 143 | static inline void |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 144 | iscsi_tcp_segment_unmap(struct iscsi_segment *segment) |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 145 | { |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 146 | debug_tcp("iscsi_tcp_segment_unmap %p\n", segment); |
| 147 | |
| 148 | if (segment->sg_mapped) { |
| 149 | debug_tcp("iscsi_tcp_segment_unmap valid\n"); |
| 150 | kunmap_atomic(segment->sg_mapped, KM_SOFTIRQ0); |
| 151 | segment->sg_mapped = NULL; |
| 152 | segment->data = NULL; |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 153 | } |
| 154 | } |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 155 | |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 156 | /* |
| 157 | * Splice the digest buffer into the buffer |
| 158 | */ |
| 159 | static inline void |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 160 | iscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest) |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 161 | { |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 162 | segment->data = digest; |
| 163 | segment->digest_len = ISCSI_DIGEST_SIZE; |
| 164 | segment->total_size += ISCSI_DIGEST_SIZE; |
| 165 | segment->size = ISCSI_DIGEST_SIZE; |
| 166 | segment->copied = 0; |
| 167 | segment->sg = NULL; |
| 168 | segment->hash = NULL; |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 169 | } |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 170 | |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 171 | /** |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 172 | * iscsi_tcp_segment_done - check whether the segment is complete |
| 173 | * @segment: iscsi segment to check |
| 174 | * @recv: set to one of this is called from the recv path |
| 175 | * @copied: number of bytes copied |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 176 | * |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 177 | * Check if we're done receiving this segment. If the receive |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 178 | * buffer is full but we expect more data, move on to the |
| 179 | * next entry in the scatterlist. |
| 180 | * |
| 181 | * If the amount of data we received isn't a multiple of 4, |
| 182 | * we will transparently receive the pad bytes, too. |
| 183 | * |
| 184 | * This function must be re-entrant. |
| 185 | */ |
| 186 | static inline int |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 187 | iscsi_tcp_segment_done(struct iscsi_segment *segment, int recv, unsigned copied) |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 188 | { |
| 189 | static unsigned char padbuf[ISCSI_PAD_LEN]; |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 190 | struct scatterlist sg; |
Boaz Harrosh | 004d653 | 2007-12-13 12:43:23 -0600 | [diff] [blame] | 191 | unsigned int pad; |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 192 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 193 | debug_tcp("copied %u %u size %u %s\n", segment->copied, copied, |
| 194 | segment->size, recv ? "recv" : "xmit"); |
| 195 | if (segment->hash && copied) { |
| 196 | /* |
| 197 | * If a segment is kmapd we must unmap it before sending |
| 198 | * to the crypto layer since that will try to kmap it again. |
| 199 | */ |
| 200 | iscsi_tcp_segment_unmap(segment); |
| 201 | |
| 202 | if (!segment->data) { |
| 203 | sg_init_table(&sg, 1); |
| 204 | sg_set_page(&sg, sg_page(segment->sg), copied, |
| 205 | segment->copied + segment->sg_offset + |
| 206 | segment->sg->offset); |
| 207 | } else |
| 208 | sg_init_one(&sg, segment->data + segment->copied, |
| 209 | copied); |
| 210 | crypto_hash_update(segment->hash, &sg, copied); |
| 211 | } |
| 212 | |
| 213 | segment->copied += copied; |
| 214 | if (segment->copied < segment->size) { |
| 215 | iscsi_tcp_segment_map(segment, recv); |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 216 | return 0; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 219 | segment->total_copied += segment->copied; |
| 220 | segment->copied = 0; |
| 221 | segment->size = 0; |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 222 | |
| 223 | /* Unmap the current scatterlist page, if there is one. */ |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 224 | iscsi_tcp_segment_unmap(segment); |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 225 | |
| 226 | /* Do we have more scatterlist entries? */ |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 227 | debug_tcp("total copied %u total size %u\n", segment->total_copied, |
| 228 | segment->total_size); |
| 229 | if (segment->total_copied < segment->total_size) { |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 230 | /* Proceed to the next entry in the scatterlist. */ |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 231 | iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg), |
| 232 | 0); |
| 233 | iscsi_tcp_segment_map(segment, recv); |
| 234 | BUG_ON(segment->size == 0); |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | /* Do we need to handle padding? */ |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 239 | pad = iscsi_padding(segment->total_copied); |
Boaz Harrosh | 004d653 | 2007-12-13 12:43:23 -0600 | [diff] [blame] | 240 | if (pad != 0) { |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 241 | debug_tcp("consume %d pad bytes\n", pad); |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 242 | segment->total_size += pad; |
| 243 | segment->size = pad; |
| 244 | segment->data = padbuf; |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | /* |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 249 | * Set us up for transferring the data digest. hdr digest |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 250 | * is completely handled in hdr done function. |
| 251 | */ |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 252 | if (segment->hash) { |
| 253 | crypto_hash_final(segment->hash, segment->digest); |
| 254 | iscsi_tcp_segment_splice_digest(segment, |
| 255 | recv ? segment->recv_digest : segment->digest); |
| 256 | return 0; |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | return 1; |
| 260 | } |
| 261 | |
| 262 | /** |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 263 | * iscsi_tcp_xmit_segment - transmit segment |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 264 | * @tcp_conn: the iSCSI TCP connection |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 265 | * @segment: the buffer to transmnit |
| 266 | * |
| 267 | * This function transmits as much of the buffer as |
| 268 | * the network layer will accept, and returns the number of |
| 269 | * bytes transmitted. |
| 270 | * |
| 271 | * If CRC hashing is enabled, the function will compute the |
| 272 | * hash as it goes. When the entire segment has been transmitted, |
| 273 | * it will retrieve the hash value and send it as well. |
| 274 | */ |
| 275 | static int |
| 276 | iscsi_tcp_xmit_segment(struct iscsi_tcp_conn *tcp_conn, |
| 277 | struct iscsi_segment *segment) |
| 278 | { |
| 279 | struct socket *sk = tcp_conn->sock; |
| 280 | unsigned int copied = 0; |
| 281 | int r = 0; |
| 282 | |
| 283 | while (!iscsi_tcp_segment_done(segment, 0, r)) { |
| 284 | struct scatterlist *sg; |
| 285 | unsigned int offset, copy; |
| 286 | int flags = 0; |
| 287 | |
| 288 | r = 0; |
| 289 | offset = segment->copied; |
| 290 | copy = segment->size - offset; |
| 291 | |
| 292 | if (segment->total_copied + segment->size < segment->total_size) |
| 293 | flags |= MSG_MORE; |
| 294 | |
| 295 | /* Use sendpage if we can; else fall back to sendmsg */ |
| 296 | if (!segment->data) { |
| 297 | sg = segment->sg; |
| 298 | offset += segment->sg_offset + sg->offset; |
| 299 | r = tcp_conn->sendpage(sk, sg_page(sg), offset, copy, |
| 300 | flags); |
| 301 | } else { |
| 302 | struct msghdr msg = { .msg_flags = flags }; |
| 303 | struct kvec iov = { |
| 304 | .iov_base = segment->data + offset, |
| 305 | .iov_len = copy |
| 306 | }; |
| 307 | |
| 308 | r = kernel_sendmsg(sk, &msg, &iov, 1, copy); |
| 309 | } |
| 310 | |
| 311 | if (r < 0) { |
| 312 | iscsi_tcp_segment_unmap(segment); |
| 313 | if (copied || r == -EAGAIN) |
| 314 | break; |
| 315 | return r; |
| 316 | } |
| 317 | copied += r; |
| 318 | } |
| 319 | return copied; |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * iscsi_tcp_segment_recv - copy data to segment |
| 324 | * @tcp_conn: the iSCSI TCP connection |
| 325 | * @segment: the buffer to copy to |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 326 | * @ptr: data pointer |
| 327 | * @len: amount of data available |
| 328 | * |
| 329 | * This function copies up to @len bytes to the |
| 330 | * given buffer, and returns the number of bytes |
| 331 | * consumed, which can actually be less than @len. |
| 332 | * |
| 333 | * If hash digest is enabled, the function will update the |
| 334 | * hash while copying. |
| 335 | * Combining these two operations doesn't buy us a lot (yet), |
| 336 | * but in the future we could implement combined copy+crc, |
| 337 | * just way we do for network layer checksums. |
| 338 | */ |
| 339 | static int |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 340 | iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn, |
| 341 | struct iscsi_segment *segment, const void *ptr, |
| 342 | unsigned int len) |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 343 | { |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 344 | unsigned int copy = 0, copied = 0; |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 345 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 346 | while (!iscsi_tcp_segment_done(segment, 1, copy)) { |
| 347 | if (copied == len) { |
| 348 | debug_tcp("iscsi_tcp_segment_recv copied %d bytes\n", |
| 349 | len); |
| 350 | break; |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 351 | } |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 352 | |
| 353 | copy = min(len - copied, segment->size - segment->copied); |
| 354 | debug_tcp("iscsi_tcp_segment_recv copying %d\n", copy); |
| 355 | memcpy(segment->data + segment->copied, ptr + copied, copy); |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 356 | copied += copy; |
| 357 | } |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 358 | return copied; |
| 359 | } |
| 360 | |
| 361 | static inline void |
| 362 | iscsi_tcp_dgst_header(struct hash_desc *hash, const void *hdr, size_t hdrlen, |
| 363 | unsigned char digest[ISCSI_DIGEST_SIZE]) |
| 364 | { |
| 365 | struct scatterlist sg; |
| 366 | |
| 367 | sg_init_one(&sg, hdr, hdrlen); |
| 368 | crypto_hash_digest(hash, &sg, hdrlen, digest); |
| 369 | } |
| 370 | |
| 371 | static inline int |
| 372 | iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn, |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 373 | struct iscsi_segment *segment) |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 374 | { |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 375 | if (!segment->digest_len) |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 376 | return 1; |
| 377 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 378 | if (memcmp(segment->recv_digest, segment->digest, |
| 379 | segment->digest_len)) { |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 380 | debug_scsi("digest mismatch\n"); |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | return 1; |
| 385 | } |
| 386 | |
| 387 | /* |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 388 | * Helper function to set up segment buffer |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 389 | */ |
| 390 | static inline void |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 391 | __iscsi_segment_init(struct iscsi_segment *segment, size_t size, |
| 392 | iscsi_segment_done_fn_t *done, struct hash_desc *hash) |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 393 | { |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 394 | memset(segment, 0, sizeof(*segment)); |
| 395 | segment->total_size = size; |
| 396 | segment->done = done; |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 397 | |
| 398 | if (hash) { |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 399 | segment->hash = hash; |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 400 | crypto_hash_init(hash); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | static inline void |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 405 | iscsi_segment_init_linear(struct iscsi_segment *segment, void *data, |
| 406 | size_t size, iscsi_segment_done_fn_t *done, |
| 407 | struct hash_desc *hash) |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 408 | { |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 409 | __iscsi_segment_init(segment, size, done, hash); |
| 410 | segment->data = data; |
| 411 | segment->size = size; |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | static inline int |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 415 | iscsi_segment_seek_sg(struct iscsi_segment *segment, |
| 416 | struct scatterlist *sg_list, unsigned int sg_count, |
| 417 | unsigned int offset, size_t size, |
| 418 | iscsi_segment_done_fn_t *done, struct hash_desc *hash) |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 419 | { |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 420 | struct scatterlist *sg; |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 421 | unsigned int i; |
| 422 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 423 | debug_scsi("iscsi_segment_seek_sg offset %u size %llu\n", |
| 424 | offset, size); |
| 425 | __iscsi_segment_init(segment, size, done, hash); |
| 426 | for_each_sg(sg_list, sg, sg_count, i) { |
| 427 | debug_scsi("sg %d, len %u offset %u\n", i, sg->length, |
| 428 | sg->offset); |
| 429 | if (offset < sg->length) { |
| 430 | iscsi_tcp_segment_init_sg(segment, sg, offset); |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 431 | return 0; |
| 432 | } |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 433 | offset -= sg->length; |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | return ISCSI_ERR_DATA_OFFSET; |
| 437 | } |
| 438 | |
| 439 | /** |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 440 | * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 441 | * @tcp_conn: iscsi connection to prep for |
| 442 | * |
| 443 | * This function always passes NULL for the hash argument, because when this |
| 444 | * function is called we do not yet know the final size of the header and want |
| 445 | * to delay the digest processing until we know that. |
| 446 | */ |
| 447 | static void |
| 448 | iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn) |
| 449 | { |
| 450 | debug_tcp("iscsi_tcp_hdr_recv_prep(%p%s)\n", tcp_conn, |
| 451 | tcp_conn->iscsi_conn->hdrdgst_en ? ", digest enabled" : ""); |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 452 | iscsi_segment_init_linear(&tcp_conn->in.segment, |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 453 | tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr), |
| 454 | iscsi_tcp_hdr_recv_done, NULL); |
| 455 | } |
| 456 | |
| 457 | /* |
| 458 | * Handle incoming reply to any other type of command |
| 459 | */ |
| 460 | static int |
| 461 | iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn, |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 462 | struct iscsi_segment *segment) |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 463 | { |
| 464 | struct iscsi_conn *conn = tcp_conn->iscsi_conn; |
| 465 | int rc = 0; |
| 466 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 467 | if (!iscsi_tcp_dgst_verify(tcp_conn, segment)) |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 468 | return ISCSI_ERR_DATA_DGST; |
| 469 | |
| 470 | rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, |
| 471 | conn->data, tcp_conn->in.datalen); |
| 472 | if (rc) |
| 473 | return rc; |
| 474 | |
| 475 | iscsi_tcp_hdr_recv_prep(tcp_conn); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 476 | return 0; |
| 477 | } |
| 478 | |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 479 | static void |
| 480 | iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn) |
| 481 | { |
| 482 | struct iscsi_conn *conn = tcp_conn->iscsi_conn; |
| 483 | struct hash_desc *rx_hash = NULL; |
| 484 | |
| 485 | if (conn->datadgst_en) |
| 486 | rx_hash = &tcp_conn->rx_hash; |
| 487 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 488 | iscsi_segment_init_linear(&tcp_conn->in.segment, |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 489 | conn->data, tcp_conn->in.datalen, |
| 490 | iscsi_tcp_data_recv_done, rx_hash); |
| 491 | } |
| 492 | |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 493 | /* |
| 494 | * must be called with session lock |
| 495 | */ |
| 496 | static void |
Mike Christie | b6c395e | 2006-07-24 15:47:15 -0500 | [diff] [blame] | 497 | 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] | 498 | { |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 499 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; |
Mike Christie | b6c395e | 2006-07-24 15:47:15 -0500 | [diff] [blame] | 500 | struct iscsi_r2t_info *r2t; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 501 | |
Mike Christie | b6c395e | 2006-07-24 15:47:15 -0500 | [diff] [blame] | 502 | /* flush ctask's r2t queues */ |
| 503 | while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) { |
| 504 | __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t, |
| 505 | sizeof(void*)); |
| 506 | debug_scsi("iscsi_tcp_cleanup_ctask pending r2t dropped\n"); |
| 507 | } |
| 508 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 509 | r2t = tcp_ctask->r2t; |
| 510 | if (r2t != NULL) { |
| 511 | __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t, |
| 512 | sizeof(void*)); |
| 513 | tcp_ctask->r2t = NULL; |
| 514 | } |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | /** |
| 518 | * iscsi_data_rsp - SCSI Data-In Response processing |
| 519 | * @conn: iscsi connection |
| 520 | * @ctask: scsi command task |
| 521 | **/ |
| 522 | static int |
| 523 | iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) |
| 524 | { |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 525 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
| 526 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; |
| 527 | 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] | 528 | struct iscsi_session *session = conn->session; |
Mike Christie | 857ae0b | 2007-05-30 12:57:15 -0500 | [diff] [blame] | 529 | struct scsi_cmnd *sc = ctask->sc; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 530 | int datasn = be32_to_cpu(rhdr->datasn); |
Boaz Harrosh | 94795b6 | 2008-04-18 10:11:53 -0500 | [diff] [blame] | 531 | unsigned total_in_length = scsi_in(sc)->length; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 532 | |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 533 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr); |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 534 | if (tcp_conn->in.datalen == 0) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 535 | return 0; |
| 536 | |
Mike Christie | d473cc7 | 2007-05-30 12:57:14 -0500 | [diff] [blame] | 537 | if (tcp_ctask->exp_datasn != datasn) { |
| 538 | debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->datasn(%d)\n", |
| 539 | __FUNCTION__, tcp_ctask->exp_datasn, datasn); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 540 | return ISCSI_ERR_DATASN; |
Mike Christie | d473cc7 | 2007-05-30 12:57:14 -0500 | [diff] [blame] | 541 | } |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 542 | |
Mike Christie | d473cc7 | 2007-05-30 12:57:14 -0500 | [diff] [blame] | 543 | tcp_ctask->exp_datasn++; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 544 | |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 545 | tcp_ctask->data_offset = be32_to_cpu(rhdr->offset); |
Boaz Harrosh | 94795b6 | 2008-04-18 10:11:53 -0500 | [diff] [blame] | 546 | if (tcp_ctask->data_offset + tcp_conn->in.datalen > total_in_length) { |
Mike Christie | 857ae0b | 2007-05-30 12:57:15 -0500 | [diff] [blame] | 547 | debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n", |
| 548 | __FUNCTION__, tcp_ctask->data_offset, |
Boaz Harrosh | 94795b6 | 2008-04-18 10:11:53 -0500 | [diff] [blame] | 549 | tcp_conn->in.datalen, total_in_length); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 550 | return ISCSI_ERR_DATA_OFFSET; |
Mike Christie | 857ae0b | 2007-05-30 12:57:15 -0500 | [diff] [blame] | 551 | } |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 552 | |
| 553 | if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) { |
Boaz Harrosh | 7207fea | 2007-12-13 12:43:22 -0600 | [diff] [blame] | 554 | sc->result = (DID_OK << 16) | rhdr->cmd_status; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 555 | conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1; |
Boaz Harrosh | 7207fea | 2007-12-13 12:43:22 -0600 | [diff] [blame] | 556 | if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW | |
| 557 | ISCSI_FLAG_DATA_OVERFLOW)) { |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 558 | int res_count = be32_to_cpu(rhdr->residual_count); |
| 559 | |
| 560 | if (res_count > 0 && |
Boaz Harrosh | 7207fea | 2007-12-13 12:43:22 -0600 | [diff] [blame] | 561 | (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW || |
Boaz Harrosh | 94795b6 | 2008-04-18 10:11:53 -0500 | [diff] [blame] | 562 | res_count <= total_in_length)) |
| 563 | scsi_in(sc)->resid = res_count; |
Boaz Harrosh | 7207fea | 2007-12-13 12:43:22 -0600 | [diff] [blame] | 564 | else |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 565 | sc->result = (DID_BAD_TARGET << 16) | |
| 566 | rhdr->cmd_status; |
Boaz Harrosh | 7207fea | 2007-12-13 12:43:22 -0600 | [diff] [blame] | 567 | } |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | conn->datain_pdus_cnt++; |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * iscsi_solicit_data_init - initialize first Data-Out |
| 576 | * @conn: iscsi connection |
| 577 | * @ctask: scsi command task |
| 578 | * @r2t: R2T info |
| 579 | * |
| 580 | * Notes: |
| 581 | * Initialize first Data-Out within this R2T sequence and finds |
| 582 | * proper data_offset within this SCSI command. |
| 583 | * |
| 584 | * This function is called with connection lock taken. |
| 585 | **/ |
| 586 | static void |
| 587 | iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask, |
| 588 | struct iscsi_r2t_info *r2t) |
| 589 | { |
| 590 | struct iscsi_data *hdr; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 591 | |
Mike Christie | ffbfe92 | 2006-05-18 20:31:36 -0500 | [diff] [blame] | 592 | hdr = &r2t->dtask.hdr; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 593 | memset(hdr, 0, sizeof(struct iscsi_data)); |
| 594 | hdr->ttt = r2t->ttt; |
| 595 | hdr->datasn = cpu_to_be32(r2t->solicit_datasn); |
| 596 | r2t->solicit_datasn++; |
| 597 | hdr->opcode = ISCSI_OP_SCSI_DATA_OUT; |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 598 | memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun)); |
| 599 | hdr->itt = ctask->hdr->itt; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 600 | hdr->exp_statsn = r2t->exp_statsn; |
| 601 | hdr->offset = cpu_to_be32(r2t->data_offset); |
| 602 | if (r2t->data_length > conn->max_xmit_dlength) { |
| 603 | hton24(hdr->dlength, conn->max_xmit_dlength); |
| 604 | r2t->data_count = conn->max_xmit_dlength; |
| 605 | hdr->flags = 0; |
| 606 | } else { |
| 607 | hton24(hdr->dlength, r2t->data_length); |
| 608 | r2t->data_count = r2t->data_length; |
| 609 | hdr->flags = ISCSI_FLAG_CMD_FINAL; |
| 610 | } |
| 611 | conn->dataout_pdus_cnt++; |
| 612 | |
| 613 | r2t->sent = 0; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | /** |
| 617 | * iscsi_r2t_rsp - iSCSI R2T Response processing |
| 618 | * @conn: iscsi connection |
| 619 | * @ctask: scsi command task |
| 620 | **/ |
| 621 | static int |
| 622 | iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask) |
| 623 | { |
| 624 | struct iscsi_r2t_info *r2t; |
| 625 | struct iscsi_session *session = conn->session; |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 626 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; |
| 627 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
| 628 | 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] | 629 | int r2tsn = be32_to_cpu(rhdr->r2tsn); |
| 630 | int rc; |
| 631 | |
Mike Christie | 98a9416 | 2006-08-31 18:09:26 -0400 | [diff] [blame] | 632 | if (tcp_conn->in.datalen) { |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 633 | iscsi_conn_printk(KERN_ERR, conn, |
| 634 | "invalid R2t with datalen %d\n", |
| 635 | tcp_conn->in.datalen); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 636 | return ISCSI_ERR_DATALEN; |
Mike Christie | 98a9416 | 2006-08-31 18:09:26 -0400 | [diff] [blame] | 637 | } |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 638 | |
Mike Christie | d473cc7 | 2007-05-30 12:57:14 -0500 | [diff] [blame] | 639 | if (tcp_ctask->exp_datasn != r2tsn){ |
| 640 | debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->r2tsn(%d)\n", |
| 641 | __FUNCTION__, tcp_ctask->exp_datasn, r2tsn); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 642 | return ISCSI_ERR_R2TSN; |
Mike Christie | d473cc7 | 2007-05-30 12:57:14 -0500 | [diff] [blame] | 643 | } |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 644 | |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 645 | /* fill-in new R2T associated with the task */ |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 646 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr); |
| 647 | |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 648 | if (!ctask->sc || session->state != ISCSI_STATE_LOGGED_IN) { |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 649 | iscsi_conn_printk(KERN_INFO, conn, |
| 650 | "dropping R2T itt %d in recovery.\n", |
| 651 | ctask->itt); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 652 | return 0; |
| 653 | } |
Mike Christie | b6c395e | 2006-07-24 15:47:15 -0500 | [diff] [blame] | 654 | |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 655 | rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*)); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 656 | BUG_ON(!rc); |
| 657 | |
| 658 | r2t->exp_statsn = rhdr->statsn; |
| 659 | r2t->data_length = be32_to_cpu(rhdr->data_length); |
Mike Christie | 98a9416 | 2006-08-31 18:09:26 -0400 | [diff] [blame] | 660 | if (r2t->data_length == 0) { |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 661 | iscsi_conn_printk(KERN_ERR, conn, |
| 662 | "invalid R2T with zero data len\n"); |
Olaf Kirch | 03766a1 | 2007-12-13 12:43:36 -0600 | [diff] [blame] | 663 | __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t, |
| 664 | sizeof(void*)); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 665 | return ISCSI_ERR_DATALEN; |
| 666 | } |
| 667 | |
Mike Christie | 98a9416 | 2006-08-31 18:09:26 -0400 | [diff] [blame] | 668 | if (r2t->data_length > session->max_burst) |
| 669 | debug_scsi("invalid R2T with data len %u and max burst %u." |
| 670 | "Attempting to execute request.\n", |
| 671 | r2t->data_length, session->max_burst); |
| 672 | |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 673 | r2t->data_offset = be32_to_cpu(rhdr->data_offset); |
Boaz Harrosh | 94795b6 | 2008-04-18 10:11:53 -0500 | [diff] [blame] | 674 | if (r2t->data_offset + r2t->data_length > scsi_out(ctask->sc)->length) { |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 675 | iscsi_conn_printk(KERN_ERR, conn, |
| 676 | "invalid R2T with data len %u at offset %u " |
| 677 | "and total length %d\n", r2t->data_length, |
Boaz Harrosh | 94795b6 | 2008-04-18 10:11:53 -0500 | [diff] [blame] | 678 | r2t->data_offset, scsi_out(ctask->sc)->length); |
Olaf Kirch | 03766a1 | 2007-12-13 12:43:36 -0600 | [diff] [blame] | 679 | __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t, |
| 680 | sizeof(void*)); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 681 | return ISCSI_ERR_DATALEN; |
| 682 | } |
| 683 | |
| 684 | r2t->ttt = rhdr->ttt; /* no flip */ |
| 685 | r2t->solicit_datasn = 0; |
| 686 | |
| 687 | iscsi_solicit_data_init(conn, ctask, r2t); |
| 688 | |
Mike Christie | d473cc7 | 2007-05-30 12:57:14 -0500 | [diff] [blame] | 689 | tcp_ctask->exp_datasn = r2tsn + 1; |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 690 | __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*)); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 691 | conn->r2t_pdus_cnt++; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 692 | |
| 693 | iscsi_requeue_ctask(ctask); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 694 | return 0; |
| 695 | } |
| 696 | |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 697 | /* |
| 698 | * Handle incoming reply to DataIn command |
| 699 | */ |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 700 | static int |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 701 | iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn, |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 702 | struct iscsi_segment *segment) |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 703 | { |
| 704 | struct iscsi_conn *conn = tcp_conn->iscsi_conn; |
| 705 | struct iscsi_hdr *hdr = tcp_conn->in.hdr; |
| 706 | int rc; |
| 707 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 708 | if (!iscsi_tcp_dgst_verify(tcp_conn, segment)) |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 709 | return ISCSI_ERR_DATA_DGST; |
| 710 | |
| 711 | /* check for non-exceptional status */ |
| 712 | if (hdr->flags & ISCSI_FLAG_DATA_STATUS) { |
| 713 | rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0); |
| 714 | if (rc) |
| 715 | return rc; |
| 716 | } |
| 717 | |
| 718 | iscsi_tcp_hdr_recv_prep(tcp_conn); |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | /** |
| 723 | * iscsi_tcp_hdr_dissect - process PDU header |
| 724 | * @conn: iSCSI connection |
| 725 | * @hdr: PDU header |
| 726 | * |
| 727 | * This function analyzes the header of the PDU received, |
| 728 | * and performs several sanity checks. If the PDU is accompanied |
| 729 | * by data, the receive buffer is set up to copy the incoming data |
| 730 | * to the correct location. |
| 731 | */ |
| 732 | static int |
| 733 | iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 734 | { |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 735 | int rc = 0, opcode, ahslen; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 736 | struct iscsi_session *session = conn->session; |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 737 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 738 | struct iscsi_cmd_task *ctask; |
| 739 | uint32_t itt; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 740 | |
| 741 | /* verify PDU length */ |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 742 | tcp_conn->in.datalen = ntoh24(hdr->dlength); |
| 743 | if (tcp_conn->in.datalen > conn->max_recv_dlength) { |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 744 | iscsi_conn_printk(KERN_ERR, conn, |
| 745 | "iscsi_tcp: datalen %d > %d\n", |
| 746 | tcp_conn->in.datalen, conn->max_recv_dlength); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 747 | return ISCSI_ERR_DATALEN; |
| 748 | } |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 749 | |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 750 | /* Additional header segments. So far, we don't |
| 751 | * process additional headers. |
| 752 | */ |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 753 | ahslen = hdr->hlength << 2; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 754 | |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 755 | opcode = hdr->opcode & ISCSI_OPCODE_MASK; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 756 | /* verify itt (itt encoding: age+cid+itt) */ |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 757 | rc = iscsi_verify_itt(conn, hdr, &itt); |
Mike Christie | 7a53dc5 | 2007-12-13 12:43:37 -0600 | [diff] [blame] | 758 | if (rc) |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 759 | return rc; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 760 | |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 761 | debug_tcp("opcode 0x%x ahslen %d datalen %d\n", |
| 762 | opcode, ahslen, tcp_conn->in.datalen); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 763 | |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 764 | switch(opcode) { |
| 765 | case ISCSI_OP_SCSI_DATA_IN: |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 766 | ctask = session->cmds[itt]; |
Mike Christie | 4545a88 | 2007-12-13 12:43:40 -0600 | [diff] [blame] | 767 | spin_lock(&conn->session->lock); |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 768 | rc = iscsi_data_rsp(conn, ctask); |
Mike Christie | 4545a88 | 2007-12-13 12:43:40 -0600 | [diff] [blame] | 769 | spin_unlock(&conn->session->lock); |
Mike Christie | 275fd7d | 2006-07-24 15:47:17 -0500 | [diff] [blame] | 770 | if (rc) |
| 771 | return rc; |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 772 | if (tcp_conn->in.datalen) { |
| 773 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; |
| 774 | struct hash_desc *rx_hash = NULL; |
Boaz Harrosh | 94795b6 | 2008-04-18 10:11:53 -0500 | [diff] [blame] | 775 | struct scsi_data_buffer *sdb = scsi_in(ctask->sc); |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 776 | |
| 777 | /* |
| 778 | * Setup copy of Data-In into the Scsi_Cmnd |
| 779 | * Scatterlist case: |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 780 | * We set up the iscsi_segment to point to the next |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 781 | * scatterlist entry to copy to. As we go along, |
| 782 | * we move on to the next scatterlist entry and |
| 783 | * update the digest per-entry. |
| 784 | */ |
| 785 | if (conn->datadgst_en) |
| 786 | rx_hash = &tcp_conn->rx_hash; |
| 787 | |
| 788 | debug_tcp("iscsi_tcp_begin_data_in(%p, offset=%d, " |
| 789 | "datalen=%d)\n", tcp_conn, |
| 790 | tcp_ctask->data_offset, |
| 791 | tcp_conn->in.datalen); |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 792 | return iscsi_segment_seek_sg(&tcp_conn->in.segment, |
Boaz Harrosh | 94795b6 | 2008-04-18 10:11:53 -0500 | [diff] [blame] | 793 | sdb->table.sgl, |
| 794 | sdb->table.nents, |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 795 | tcp_ctask->data_offset, |
| 796 | tcp_conn->in.datalen, |
| 797 | iscsi_tcp_process_data_in, |
| 798 | rx_hash); |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 799 | } |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 800 | /* fall through */ |
| 801 | case ISCSI_OP_SCSI_CMD_RSP: |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 802 | if (tcp_conn->in.datalen) { |
| 803 | iscsi_tcp_data_recv_prep(tcp_conn); |
| 804 | return 0; |
| 805 | } |
| 806 | rc = iscsi_complete_pdu(conn, hdr, NULL, 0); |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 807 | break; |
| 808 | case ISCSI_OP_R2T: |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 809 | ctask = session->cmds[itt]; |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 810 | if (ahslen) |
| 811 | rc = ISCSI_ERR_AHSLEN; |
Mike Christie | 4545a88 | 2007-12-13 12:43:40 -0600 | [diff] [blame] | 812 | else if (ctask->sc->sc_data_direction == DMA_TO_DEVICE) { |
| 813 | spin_lock(&session->lock); |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 814 | rc = iscsi_r2t_rsp(conn, ctask); |
Mike Christie | 4545a88 | 2007-12-13 12:43:40 -0600 | [diff] [blame] | 815 | spin_unlock(&session->lock); |
| 816 | } else |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 817 | rc = ISCSI_ERR_PROTO; |
| 818 | break; |
| 819 | case ISCSI_OP_LOGIN_RSP: |
| 820 | case ISCSI_OP_TEXT_RSP: |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 821 | case ISCSI_OP_REJECT: |
| 822 | case ISCSI_OP_ASYNC_EVENT: |
Mike Christie | c8dc1e5 | 2006-07-24 15:47:39 -0500 | [diff] [blame] | 823 | /* |
| 824 | * It is possible that we could get a PDU with a buffer larger |
| 825 | * than 8K, but there are no targets that currently do this. |
| 826 | * For now we fail until we find a vendor that needs it |
| 827 | */ |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 828 | if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) { |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 829 | iscsi_conn_printk(KERN_ERR, conn, |
| 830 | "iscsi_tcp: received buffer of " |
| 831 | "len %u but conn buffer is only %u " |
| 832 | "(opcode %0x)\n", |
| 833 | tcp_conn->in.datalen, |
| 834 | ISCSI_DEF_MAX_RECV_SEG_LEN, opcode); |
Mike Christie | c8dc1e5 | 2006-07-24 15:47:39 -0500 | [diff] [blame] | 835 | rc = ISCSI_ERR_PROTO; |
| 836 | break; |
| 837 | } |
| 838 | |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 839 | /* If there's data coming in with the response, |
| 840 | * receive it to the connection's buffer. |
| 841 | */ |
| 842 | if (tcp_conn->in.datalen) { |
| 843 | iscsi_tcp_data_recv_prep(tcp_conn); |
| 844 | return 0; |
| 845 | } |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 846 | /* fall through */ |
Mike Christie | c8dc1e5 | 2006-07-24 15:47:39 -0500 | [diff] [blame] | 847 | case ISCSI_OP_LOGOUT_RSP: |
| 848 | case ISCSI_OP_NOOP_IN: |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 849 | case ISCSI_OP_SCSI_TMFUNC_RSP: |
| 850 | rc = iscsi_complete_pdu(conn, hdr, NULL, 0); |
| 851 | break; |
| 852 | default: |
| 853 | rc = ISCSI_ERR_BAD_OPCODE; |
| 854 | break; |
| 855 | } |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 856 | |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 857 | if (rc == 0) { |
| 858 | /* Anything that comes with data should have |
| 859 | * been handled above. */ |
| 860 | if (tcp_conn->in.datalen) |
| 861 | return ISCSI_ERR_PROTO; |
| 862 | iscsi_tcp_hdr_recv_prep(tcp_conn); |
| 863 | } |
| 864 | |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 865 | return rc; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 866 | } |
| 867 | |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 868 | /** |
| 869 | * iscsi_tcp_hdr_recv_done - process PDU header |
| 870 | * |
| 871 | * This is the callback invoked when the PDU header has |
| 872 | * been received. If the header is followed by additional |
| 873 | * header segments, we go back for more data. |
| 874 | */ |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 875 | static int |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 876 | iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn, |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 877 | struct iscsi_segment *segment) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 878 | { |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 879 | struct iscsi_conn *conn = tcp_conn->iscsi_conn; |
| 880 | struct iscsi_hdr *hdr; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 881 | |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 882 | /* Check if there are additional header segments |
| 883 | * *prior* to computing the digest, because we |
| 884 | * may need to go back to the caller for more. |
| 885 | */ |
| 886 | hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf; |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 887 | if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) { |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 888 | /* Bump the header length - the caller will |
| 889 | * just loop around and get the AHS for us, and |
| 890 | * call again. */ |
| 891 | unsigned int ahslen = hdr->hlength << 2; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 892 | |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 893 | /* Make sure we don't overflow */ |
| 894 | if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf)) |
| 895 | return ISCSI_ERR_AHSLEN; |
| 896 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 897 | segment->total_size += ahslen; |
| 898 | segment->size += ahslen; |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 899 | return 0; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 900 | } |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 901 | |
| 902 | /* We're done processing the header. See if we're doing |
| 903 | * header digests; if so, set up the recv_digest buffer |
| 904 | * and go back for more. */ |
| 905 | if (conn->hdrdgst_en) { |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 906 | if (segment->digest_len == 0) { |
| 907 | iscsi_tcp_segment_splice_digest(segment, |
| 908 | segment->recv_digest); |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 909 | return 0; |
| 910 | } |
| 911 | iscsi_tcp_dgst_header(&tcp_conn->rx_hash, hdr, |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 912 | segment->total_copied - ISCSI_DIGEST_SIZE, |
| 913 | segment->digest); |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 914 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 915 | if (!iscsi_tcp_dgst_verify(tcp_conn, segment)) |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 916 | return ISCSI_ERR_HDR_DGST; |
| 917 | } |
| 918 | |
| 919 | tcp_conn->in.hdr = hdr; |
| 920 | return iscsi_tcp_hdr_dissect(conn, hdr); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | /** |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 924 | * iscsi_tcp_recv - TCP receive in sendfile fashion |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 925 | * @rd_desc: read descriptor |
| 926 | * @skb: socket buffer |
| 927 | * @offset: offset in skb |
| 928 | * @len: skb->len - offset |
| 929 | **/ |
| 930 | static int |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 931 | iscsi_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, |
| 932 | unsigned int offset, size_t len) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 933 | { |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 934 | struct iscsi_conn *conn = rd_desc->arg.data; |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 935 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 936 | struct iscsi_segment *segment = &tcp_conn->in.segment; |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 937 | struct skb_seq_state seq; |
| 938 | unsigned int consumed = 0; |
| 939 | int rc = 0; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 940 | |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 941 | debug_tcp("in %d bytes\n", skb->len - offset); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 942 | |
| 943 | if (unlikely(conn->suspend_rx)) { |
| 944 | debug_tcp("conn %d Rx suspended!\n", conn->id); |
| 945 | return 0; |
| 946 | } |
| 947 | |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 948 | skb_prepare_seq_read(skb, offset, skb->len, &seq); |
| 949 | while (1) { |
| 950 | unsigned int avail; |
| 951 | const u8 *ptr; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 952 | |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 953 | avail = skb_seq_read(consumed, &ptr, &seq); |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 954 | if (avail == 0) { |
| 955 | debug_tcp("no more data avail. Consumed %d\n", |
| 956 | consumed); |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 957 | break; |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 958 | } |
| 959 | BUG_ON(segment->copied >= segment->size); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 960 | |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 961 | debug_tcp("skb %p ptr=%p avail=%u\n", skb, ptr, avail); |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 962 | rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail); |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 963 | BUG_ON(rc == 0); |
| 964 | consumed += rc; |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 965 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 966 | if (segment->total_copied >= segment->total_size) { |
| 967 | debug_tcp("segment done\n"); |
| 968 | rc = segment->done(tcp_conn, segment); |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 969 | if (rc != 0) { |
| 970 | skb_abort_seq_read(&seq); |
| 971 | goto error; |
Mike Christie | dbdb016 | 2007-05-30 12:57:20 -0500 | [diff] [blame] | 972 | } |
Mike Christie | dbdb016 | 2007-05-30 12:57:20 -0500 | [diff] [blame] | 973 | |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 974 | /* The done() functions sets up the |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 975 | * next segment. */ |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 976 | } |
| 977 | } |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 978 | skb_abort_seq_read(&seq); |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 979 | conn->rxdata_octets += consumed; |
| 980 | return consumed; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 981 | |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 982 | error: |
| 983 | debug_tcp("Error receiving PDU, errno=%d\n", rc); |
| 984 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
| 985 | return 0; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | static void |
| 989 | iscsi_tcp_data_ready(struct sock *sk, int flag) |
| 990 | { |
| 991 | struct iscsi_conn *conn = sk->sk_user_data; |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 992 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 993 | read_descriptor_t rd_desc; |
| 994 | |
| 995 | read_lock(&sk->sk_callback_lock); |
| 996 | |
Mike Christie | 665b44a | 2006-05-02 19:46:49 -0500 | [diff] [blame] | 997 | /* |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 998 | * Use rd_desc to pass 'conn' to iscsi_tcp_recv. |
Mike Christie | 665b44a | 2006-05-02 19:46:49 -0500 | [diff] [blame] | 999 | * We set count to 1 because we want the network layer to |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 1000 | * hand us all the skbs that are available. iscsi_tcp_recv |
Mike Christie | 665b44a | 2006-05-02 19:46:49 -0500 | [diff] [blame] | 1001 | * handled pdus that cross buffers or pdus that still need data. |
| 1002 | */ |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1003 | rd_desc.arg.data = conn; |
Mike Christie | 665b44a | 2006-05-02 19:46:49 -0500 | [diff] [blame] | 1004 | rd_desc.count = 1; |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 1005 | tcp_read_sock(sk, &rd_desc, iscsi_tcp_recv); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1006 | |
| 1007 | read_unlock(&sk->sk_callback_lock); |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 1008 | |
| 1009 | /* If we had to (atomically) map a highmem page, |
| 1010 | * unmap it now. */ |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1011 | iscsi_tcp_segment_unmap(&tcp_conn->in.segment); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | static void |
| 1015 | iscsi_tcp_state_change(struct sock *sk) |
| 1016 | { |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1017 | struct iscsi_tcp_conn *tcp_conn; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1018 | struct iscsi_conn *conn; |
| 1019 | struct iscsi_session *session; |
| 1020 | void (*old_state_change)(struct sock *); |
| 1021 | |
| 1022 | read_lock(&sk->sk_callback_lock); |
| 1023 | |
| 1024 | conn = (struct iscsi_conn*)sk->sk_user_data; |
| 1025 | session = conn->session; |
| 1026 | |
Mike Christie | e627399 | 2005-11-29 23:12:49 -0600 | [diff] [blame] | 1027 | if ((sk->sk_state == TCP_CLOSE_WAIT || |
| 1028 | sk->sk_state == TCP_CLOSE) && |
| 1029 | !atomic_read(&sk->sk_rmem_alloc)) { |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1030 | debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n"); |
| 1031 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
| 1032 | } |
| 1033 | |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1034 | tcp_conn = conn->dd_data; |
| 1035 | old_state_change = tcp_conn->old_state_change; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1036 | |
| 1037 | read_unlock(&sk->sk_callback_lock); |
| 1038 | |
| 1039 | old_state_change(sk); |
| 1040 | } |
| 1041 | |
| 1042 | /** |
| 1043 | * iscsi_write_space - Called when more output buffer space is available |
| 1044 | * @sk: socket space is available for |
| 1045 | **/ |
| 1046 | static void |
| 1047 | iscsi_write_space(struct sock *sk) |
| 1048 | { |
| 1049 | struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data; |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1050 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
| 1051 | |
| 1052 | tcp_conn->old_write_space(sk); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1053 | debug_tcp("iscsi_write_space: cid %d\n", conn->id); |
Mike Christie | 55e3299 | 2006-01-13 18:05:53 -0600 | [diff] [blame] | 1054 | scsi_queue_work(conn->session->host, &conn->xmitwork); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | static void |
| 1058 | iscsi_conn_set_callbacks(struct iscsi_conn *conn) |
| 1059 | { |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1060 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
| 1061 | struct sock *sk = tcp_conn->sock->sk; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1062 | |
| 1063 | /* assign new callbacks */ |
| 1064 | write_lock_bh(&sk->sk_callback_lock); |
| 1065 | sk->sk_user_data = conn; |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1066 | tcp_conn->old_data_ready = sk->sk_data_ready; |
| 1067 | tcp_conn->old_state_change = sk->sk_state_change; |
| 1068 | tcp_conn->old_write_space = sk->sk_write_space; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1069 | sk->sk_data_ready = iscsi_tcp_data_ready; |
| 1070 | sk->sk_state_change = iscsi_tcp_state_change; |
| 1071 | sk->sk_write_space = iscsi_write_space; |
| 1072 | write_unlock_bh(&sk->sk_callback_lock); |
| 1073 | } |
| 1074 | |
| 1075 | static void |
Mike Christie | 1c83469 | 2006-07-24 15:47:26 -0500 | [diff] [blame] | 1076 | iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1077 | { |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1078 | struct sock *sk = tcp_conn->sock->sk; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1079 | |
| 1080 | /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */ |
| 1081 | write_lock_bh(&sk->sk_callback_lock); |
| 1082 | sk->sk_user_data = NULL; |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1083 | sk->sk_data_ready = tcp_conn->old_data_ready; |
| 1084 | sk->sk_state_change = tcp_conn->old_state_change; |
| 1085 | sk->sk_write_space = tcp_conn->old_write_space; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1086 | sk->sk_no_check = 0; |
| 1087 | write_unlock_bh(&sk->sk_callback_lock); |
| 1088 | } |
| 1089 | |
| 1090 | /** |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1091 | * iscsi_xmit - TCP transmit |
| 1092 | **/ |
| 1093 | static int |
| 1094 | iscsi_xmit(struct iscsi_conn *conn) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1095 | { |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1096 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1097 | struct iscsi_segment *segment = &tcp_conn->out.segment; |
| 1098 | unsigned int consumed = 0; |
| 1099 | int rc = 0; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1100 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1101 | while (1) { |
| 1102 | rc = iscsi_tcp_xmit_segment(tcp_conn, segment); |
| 1103 | if (rc < 0) |
| 1104 | goto error; |
| 1105 | if (rc == 0) |
| 1106 | break; |
| 1107 | |
| 1108 | consumed += rc; |
| 1109 | |
| 1110 | if (segment->total_copied >= segment->total_size) { |
| 1111 | if (segment->done != NULL) { |
| 1112 | rc = segment->done(tcp_conn, segment); |
| 1113 | if (rc < 0) |
| 1114 | goto error; |
| 1115 | } |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | debug_tcp("xmit %d bytes\n", consumed); |
| 1120 | |
| 1121 | conn->txdata_octets += consumed; |
| 1122 | return consumed; |
| 1123 | |
| 1124 | error: |
| 1125 | /* Transmit error. We could initiate error recovery |
| 1126 | * here. */ |
| 1127 | debug_tcp("Error sending PDU, errno=%d\n", rc); |
| 1128 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
| 1129 | return rc; |
| 1130 | } |
| 1131 | |
| 1132 | /** |
| 1133 | * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit |
| 1134 | */ |
| 1135 | static inline int |
| 1136 | iscsi_tcp_xmit_qlen(struct iscsi_conn *conn) |
| 1137 | { |
| 1138 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
| 1139 | struct iscsi_segment *segment = &tcp_conn->out.segment; |
| 1140 | |
| 1141 | return segment->total_copied - segment->total_size; |
| 1142 | } |
| 1143 | |
| 1144 | static inline int |
| 1145 | iscsi_tcp_flush(struct iscsi_conn *conn) |
| 1146 | { |
| 1147 | int rc; |
| 1148 | |
| 1149 | while (iscsi_tcp_xmit_qlen(conn)) { |
| 1150 | rc = iscsi_xmit(conn); |
| 1151 | if (rc == 0) |
| 1152 | return -EAGAIN; |
| 1153 | if (rc < 0) |
| 1154 | return rc; |
| 1155 | } |
| 1156 | |
| 1157 | return 0; |
| 1158 | } |
| 1159 | |
| 1160 | /* |
| 1161 | * This is called when we're done sending the header. |
| 1162 | * Simply copy the data_segment to the send segment, and return. |
| 1163 | */ |
| 1164 | static int |
| 1165 | iscsi_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn, |
| 1166 | struct iscsi_segment *segment) |
| 1167 | { |
| 1168 | tcp_conn->out.segment = tcp_conn->out.data_segment; |
| 1169 | debug_tcp("Header done. Next segment size %u total_size %u\n", |
| 1170 | tcp_conn->out.segment.size, tcp_conn->out.segment.total_size); |
| 1171 | return 0; |
| 1172 | } |
| 1173 | |
| 1174 | static void |
| 1175 | iscsi_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr, size_t hdrlen) |
| 1176 | { |
| 1177 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
| 1178 | |
| 1179 | debug_tcp("%s(%p%s)\n", __FUNCTION__, tcp_conn, |
| 1180 | conn->hdrdgst_en? ", digest enabled" : ""); |
| 1181 | |
| 1182 | /* Clear the data segment - needs to be filled in by the |
| 1183 | * caller using iscsi_tcp_send_data_prep() */ |
| 1184 | memset(&tcp_conn->out.data_segment, 0, sizeof(struct iscsi_segment)); |
| 1185 | |
| 1186 | /* If header digest is enabled, compute the CRC and |
| 1187 | * place the digest into the same buffer. We make |
| 1188 | * sure that both iscsi_tcp_ctask and mtask have |
| 1189 | * sufficient room. |
Mike Christie | 7cae515 | 2006-01-13 18:05:47 -0600 | [diff] [blame] | 1190 | */ |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1191 | if (conn->hdrdgst_en) { |
| 1192 | iscsi_tcp_dgst_header(&tcp_conn->tx_hash, hdr, hdrlen, |
| 1193 | hdr + hdrlen); |
| 1194 | hdrlen += ISCSI_DIGEST_SIZE; |
Mike Christie | 3219e52 | 2006-05-30 00:37:28 -0500 | [diff] [blame] | 1195 | } |
| 1196 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1197 | /* Remember header pointer for later, when we need |
| 1198 | * to decide whether there's a payload to go along |
| 1199 | * with the header. */ |
| 1200 | tcp_conn->out.hdr = hdr; |
| 1201 | |
| 1202 | iscsi_segment_init_linear(&tcp_conn->out.segment, hdr, hdrlen, |
| 1203 | iscsi_tcp_send_hdr_done, NULL); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1206 | /* |
| 1207 | * Prepare the send buffer for the payload data. |
| 1208 | * Padding and checksumming will all be taken care |
| 1209 | * of by the iscsi_segment routines. |
| 1210 | */ |
| 1211 | static int |
| 1212 | iscsi_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg, |
| 1213 | unsigned int count, unsigned int offset, |
| 1214 | unsigned int len) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1215 | { |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1216 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
| 1217 | struct hash_desc *tx_hash = NULL; |
| 1218 | unsigned int hdr_spec_len; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1219 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1220 | debug_tcp("%s(%p, offset=%d, datalen=%d%s)\n", __FUNCTION__, |
| 1221 | tcp_conn, offset, len, |
| 1222 | conn->datadgst_en? ", digest enabled" : ""); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1223 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1224 | /* Make sure the datalen matches what the caller |
| 1225 | said he would send. */ |
| 1226 | hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength); |
| 1227 | WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len)); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1228 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1229 | if (conn->datadgst_en) |
| 1230 | tx_hash = &tcp_conn->tx_hash; |
| 1231 | |
| 1232 | return iscsi_segment_seek_sg(&tcp_conn->out.data_segment, |
| 1233 | sg, count, offset, len, |
| 1234 | NULL, tx_hash); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1237 | static void |
| 1238 | iscsi_tcp_send_linear_data_prepare(struct iscsi_conn *conn, void *data, |
| 1239 | size_t len) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1240 | { |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1241 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
| 1242 | struct hash_desc *tx_hash = NULL; |
| 1243 | unsigned int hdr_spec_len; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1244 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1245 | debug_tcp("%s(%p, datalen=%d%s)\n", __FUNCTION__, tcp_conn, len, |
| 1246 | conn->datadgst_en? ", digest enabled" : ""); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1247 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1248 | /* Make sure the datalen matches what the caller |
| 1249 | said he would send. */ |
| 1250 | hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength); |
| 1251 | WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len)); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1252 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1253 | if (conn->datadgst_en) |
| 1254 | tx_hash = &tcp_conn->tx_hash; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1255 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1256 | iscsi_segment_init_linear(&tcp_conn->out.data_segment, |
| 1257 | data, len, NULL, tx_hash); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1258 | } |
| 1259 | |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1260 | /** |
| 1261 | * iscsi_solicit_data_cont - initialize next Data-Out |
| 1262 | * @conn: iscsi connection |
| 1263 | * @ctask: scsi command task |
| 1264 | * @r2t: R2T info |
| 1265 | * @left: bytes left to transfer |
| 1266 | * |
| 1267 | * Notes: |
| 1268 | * Initialize next Data-Out within this R2T sequence and continue |
| 1269 | * to process next Scatter-Gather element(if any) of this SCSI command. |
| 1270 | * |
| 1271 | * Called under connection lock. |
| 1272 | **/ |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1273 | static int |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1274 | iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask, |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1275 | struct iscsi_r2t_info *r2t) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1276 | { |
| 1277 | struct iscsi_data *hdr; |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1278 | int new_offset, left; |
| 1279 | |
| 1280 | BUG_ON(r2t->data_length - r2t->sent < 0); |
| 1281 | left = r2t->data_length - r2t->sent; |
| 1282 | if (left == 0) |
| 1283 | return 0; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1284 | |
Mike Christie | ffbfe92 | 2006-05-18 20:31:36 -0500 | [diff] [blame] | 1285 | hdr = &r2t->dtask.hdr; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1286 | memset(hdr, 0, sizeof(struct iscsi_data)); |
| 1287 | hdr->ttt = r2t->ttt; |
| 1288 | hdr->datasn = cpu_to_be32(r2t->solicit_datasn); |
| 1289 | r2t->solicit_datasn++; |
| 1290 | hdr->opcode = ISCSI_OP_SCSI_DATA_OUT; |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1291 | memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun)); |
| 1292 | hdr->itt = ctask->hdr->itt; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1293 | hdr->exp_statsn = r2t->exp_statsn; |
| 1294 | new_offset = r2t->data_offset + r2t->sent; |
| 1295 | hdr->offset = cpu_to_be32(new_offset); |
| 1296 | if (left > conn->max_xmit_dlength) { |
| 1297 | hton24(hdr->dlength, conn->max_xmit_dlength); |
| 1298 | r2t->data_count = conn->max_xmit_dlength; |
| 1299 | } else { |
| 1300 | hton24(hdr->dlength, left); |
| 1301 | r2t->data_count = left; |
| 1302 | hdr->flags = ISCSI_FLAG_CMD_FINAL; |
| 1303 | } |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1304 | |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1305 | conn->dataout_pdus_cnt++; |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1306 | return 1; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | /** |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1310 | * iscsi_tcp_ctask - Initialize iSCSI SCSI_READ or SCSI_WRITE commands |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1311 | * @conn: iscsi connection |
| 1312 | * @ctask: scsi command task |
| 1313 | * @sc: scsi command |
| 1314 | **/ |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1315 | static int |
| 1316 | iscsi_tcp_ctask_init(struct iscsi_cmd_task *ctask) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1317 | { |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1318 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1319 | struct iscsi_conn *conn = ctask->conn; |
| 1320 | struct scsi_cmnd *sc = ctask->sc; |
| 1321 | int err; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1322 | |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1323 | BUG_ON(__kfifo_len(tcp_ctask->r2tqueue)); |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1324 | tcp_ctask->sent = 0; |
| 1325 | tcp_ctask->exp_datasn = 0; |
| 1326 | |
| 1327 | /* Prepare PDU, optionally w/ immediate data */ |
| 1328 | debug_scsi("ctask deq [cid %d itt 0x%x imm %d unsol %d]\n", |
| 1329 | conn->id, ctask->itt, ctask->imm_count, |
| 1330 | ctask->unsol_count); |
| 1331 | iscsi_tcp_send_hdr_prep(conn, ctask->hdr, ctask->hdr_len); |
| 1332 | |
| 1333 | if (!ctask->imm_count) |
| 1334 | return 0; |
| 1335 | |
| 1336 | /* If we have immediate data, attach a payload */ |
Boaz Harrosh | 94795b6 | 2008-04-18 10:11:53 -0500 | [diff] [blame] | 1337 | err = iscsi_tcp_send_data_prep(conn, scsi_out(sc)->table.sgl, |
| 1338 | scsi_out(sc)->table.nents, |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1339 | 0, ctask->imm_count); |
| 1340 | if (err) |
| 1341 | return err; |
| 1342 | tcp_ctask->sent += ctask->imm_count; |
| 1343 | ctask->imm_count = 0; |
| 1344 | return 0; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | /** |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1348 | * iscsi_tcp_mtask_xmit - xmit management(immediate) task |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1349 | * @conn: iscsi connection |
| 1350 | * @mtask: task management task |
| 1351 | * |
| 1352 | * Notes: |
| 1353 | * The function can return -EAGAIN in which case caller must |
| 1354 | * call it again later, or recover. '0' return code means successful |
| 1355 | * xmit. |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1356 | **/ |
| 1357 | static int |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1358 | 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] | 1359 | { |
Mike Christie | 3219e52 | 2006-05-30 00:37:28 -0500 | [diff] [blame] | 1360 | int rc; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1361 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1362 | /* Flush any pending data first. */ |
| 1363 | rc = iscsi_tcp_flush(conn); |
| 1364 | if (rc < 0) |
| 1365 | return rc; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1366 | |
Al Viro | b437735 | 2007-02-09 16:39:40 +0000 | [diff] [blame] | 1367 | if (mtask->hdr->itt == RESERVED_ITT) { |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1368 | struct iscsi_session *session = conn->session; |
| 1369 | |
| 1370 | spin_lock_bh(&session->lock); |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 1371 | iscsi_free_mgmt_task(conn, mtask); |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1372 | spin_unlock_bh(&session->lock); |
| 1373 | } |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1374 | |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1375 | return 0; |
| 1376 | } |
| 1377 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1378 | /* |
Mike Christie | 218432c | 2007-05-30 12:57:17 -0500 | [diff] [blame] | 1379 | * iscsi_tcp_ctask_xmit - xmit normal PDU task |
| 1380 | * @conn: iscsi connection |
| 1381 | * @ctask: iscsi command task |
| 1382 | * |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1383 | * We're expected to return 0 when everything was transmitted succesfully, |
| 1384 | * -EAGAIN if there's still data in the queue, or != 0 for any other kind |
| 1385 | * of error. |
| 1386 | */ |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1387 | static int |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1388 | 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] | 1389 | { |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1390 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1391 | struct scsi_cmnd *sc = ctask->sc; |
Boaz Harrosh | 94795b6 | 2008-04-18 10:11:53 -0500 | [diff] [blame] | 1392 | struct scsi_data_buffer *sdb = scsi_out(sc); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1393 | int rc = 0; |
| 1394 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1395 | flush: |
| 1396 | /* Flush any pending data first. */ |
| 1397 | rc = iscsi_tcp_flush(conn); |
| 1398 | if (rc < 0) |
Mike Christie | 218432c | 2007-05-30 12:57:17 -0500 | [diff] [blame] | 1399 | return rc; |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1400 | |
| 1401 | /* Are we done already? */ |
| 1402 | if (sc->sc_data_direction != DMA_TO_DEVICE) |
Mike Christie | 218432c | 2007-05-30 12:57:17 -0500 | [diff] [blame] | 1403 | return 0; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1404 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1405 | if (ctask->unsol_count != 0) { |
| 1406 | struct iscsi_data *hdr = &tcp_ctask->unsol_dtask.hdr; |
| 1407 | |
| 1408 | /* Prepare a header for the unsolicited PDU. |
| 1409 | * The amount of data we want to send will be |
| 1410 | * in ctask->data_count. |
| 1411 | * FIXME: return the data count instead. |
| 1412 | */ |
| 1413 | iscsi_prep_unsolicit_data_pdu(ctask, hdr); |
| 1414 | |
| 1415 | debug_tcp("unsol dout [itt 0x%x doff %d dlen %d]\n", |
| 1416 | ctask->itt, tcp_ctask->sent, ctask->data_count); |
| 1417 | |
| 1418 | iscsi_tcp_send_hdr_prep(conn, hdr, sizeof(*hdr)); |
Boaz Harrosh | 94795b6 | 2008-04-18 10:11:53 -0500 | [diff] [blame] | 1419 | rc = iscsi_tcp_send_data_prep(conn, sdb->table.sgl, |
| 1420 | sdb->table.nents, tcp_ctask->sent, |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1421 | ctask->data_count); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1422 | if (rc) |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1423 | goto fail; |
| 1424 | tcp_ctask->sent += ctask->data_count; |
| 1425 | ctask->unsol_count -= ctask->data_count; |
| 1426 | goto flush; |
| 1427 | } else { |
| 1428 | struct iscsi_session *session = conn->session; |
| 1429 | struct iscsi_r2t_info *r2t; |
| 1430 | |
| 1431 | /* All unsolicited PDUs sent. Check for solicited PDUs. |
| 1432 | */ |
| 1433 | spin_lock_bh(&session->lock); |
| 1434 | r2t = tcp_ctask->r2t; |
| 1435 | if (r2t != NULL) { |
| 1436 | /* Continue with this R2T? */ |
| 1437 | if (!iscsi_solicit_data_cont(conn, ctask, r2t)) { |
| 1438 | debug_scsi(" done with r2t %p\n", r2t); |
| 1439 | |
| 1440 | __kfifo_put(tcp_ctask->r2tpool.queue, |
| 1441 | (void*)&r2t, sizeof(void*)); |
| 1442 | tcp_ctask->r2t = r2t = NULL; |
| 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | if (r2t == NULL) { |
| 1447 | __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t, |
| 1448 | sizeof(void*)); |
| 1449 | r2t = tcp_ctask->r2t; |
| 1450 | } |
| 1451 | spin_unlock_bh(&session->lock); |
| 1452 | |
| 1453 | /* Waiting for more R2Ts to arrive. */ |
| 1454 | if (r2t == NULL) { |
| 1455 | debug_tcp("no R2Ts yet\n"); |
| 1456 | return 0; |
| 1457 | } |
| 1458 | |
| 1459 | debug_scsi("sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n", |
| 1460 | r2t, r2t->solicit_datasn - 1, ctask->itt, |
| 1461 | r2t->data_offset + r2t->sent, r2t->data_count); |
| 1462 | |
| 1463 | iscsi_tcp_send_hdr_prep(conn, &r2t->dtask.hdr, |
| 1464 | sizeof(struct iscsi_hdr)); |
| 1465 | |
Boaz Harrosh | 94795b6 | 2008-04-18 10:11:53 -0500 | [diff] [blame] | 1466 | rc = iscsi_tcp_send_data_prep(conn, sdb->table.sgl, |
| 1467 | sdb->table.nents, |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1468 | r2t->data_offset + r2t->sent, |
| 1469 | r2t->data_count); |
| 1470 | if (rc) |
| 1471 | goto fail; |
| 1472 | tcp_ctask->sent += r2t->data_count; |
| 1473 | r2t->sent += r2t->data_count; |
| 1474 | goto flush; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1475 | } |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1476 | return 0; |
| 1477 | fail: |
| 1478 | iscsi_conn_failure(conn, rc); |
| 1479 | return -EIO; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1480 | } |
| 1481 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1482 | static struct iscsi_cls_conn * |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1483 | 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] | 1484 | { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1485 | struct iscsi_conn *conn; |
| 1486 | struct iscsi_cls_conn *cls_conn; |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1487 | struct iscsi_tcp_conn *tcp_conn; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1488 | |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1489 | cls_conn = iscsi_conn_setup(cls_session, conn_idx); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1490 | if (!cls_conn) |
| 1491 | return NULL; |
| 1492 | conn = cls_conn->dd_data; |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1493 | /* |
| 1494 | * due to strange issues with iser these are not set |
| 1495 | * in iscsi_conn_setup |
| 1496 | */ |
Mike Christie | bf32ed3 | 2007-02-28 17:32:17 -0600 | [diff] [blame] | 1497 | conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1498 | |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1499 | tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL); |
| 1500 | if (!tcp_conn) |
| 1501 | goto tcp_conn_alloc_fail; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1502 | |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1503 | conn->dd_data = tcp_conn; |
| 1504 | tcp_conn->iscsi_conn = conn; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1505 | |
James Bottomley | c9802cd | 2006-09-23 15:33:43 -0500 | [diff] [blame] | 1506 | tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0, |
| 1507 | CRYPTO_ALG_ASYNC); |
| 1508 | tcp_conn->tx_hash.flags = 0; |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 1509 | if (IS_ERR(tcp_conn->tx_hash.tfm)) |
Mike Christie | dd8c0d9 | 2006-08-31 18:09:28 -0400 | [diff] [blame] | 1510 | goto free_tcp_conn; |
| 1511 | |
James Bottomley | c9802cd | 2006-09-23 15:33:43 -0500 | [diff] [blame] | 1512 | tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0, |
| 1513 | CRYPTO_ALG_ASYNC); |
| 1514 | tcp_conn->rx_hash.flags = 0; |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 1515 | if (IS_ERR(tcp_conn->rx_hash.tfm)) |
Mike Christie | dd8c0d9 | 2006-08-31 18:09:28 -0400 | [diff] [blame] | 1516 | goto free_tx_tfm; |
| 1517 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1518 | return cls_conn; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1519 | |
Mike Christie | dd8c0d9 | 2006-08-31 18:09:28 -0400 | [diff] [blame] | 1520 | free_tx_tfm: |
James Bottomley | c9802cd | 2006-09-23 15:33:43 -0500 | [diff] [blame] | 1521 | crypto_free_hash(tcp_conn->tx_hash.tfm); |
Mike Christie | dd8c0d9 | 2006-08-31 18:09:28 -0400 | [diff] [blame] | 1522 | free_tcp_conn: |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 1523 | iscsi_conn_printk(KERN_ERR, conn, |
| 1524 | "Could not create connection due to crc32c " |
| 1525 | "loading error. Make sure the crc32c " |
| 1526 | "module is built as a module or into the " |
| 1527 | "kernel\n"); |
Mike Christie | dd8c0d9 | 2006-08-31 18:09:28 -0400 | [diff] [blame] | 1528 | kfree(tcp_conn); |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1529 | tcp_conn_alloc_fail: |
| 1530 | iscsi_conn_teardown(cls_conn); |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1531 | return NULL; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | static void |
Mike Christie | 1c83469 | 2006-07-24 15:47:26 -0500 | [diff] [blame] | 1535 | iscsi_tcp_release_conn(struct iscsi_conn *conn) |
| 1536 | { |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 1537 | struct iscsi_session *session = conn->session; |
Mike Christie | 1c83469 | 2006-07-24 15:47:26 -0500 | [diff] [blame] | 1538 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 1539 | struct socket *sock = tcp_conn->sock; |
Mike Christie | 1c83469 | 2006-07-24 15:47:26 -0500 | [diff] [blame] | 1540 | |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 1541 | if (!sock) |
Mike Christie | 1c83469 | 2006-07-24 15:47:26 -0500 | [diff] [blame] | 1542 | return; |
| 1543 | |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 1544 | sock_hold(sock->sk); |
Mike Christie | 1c83469 | 2006-07-24 15:47:26 -0500 | [diff] [blame] | 1545 | iscsi_conn_restore_callbacks(tcp_conn); |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 1546 | sock_put(sock->sk); |
Mike Christie | 1c83469 | 2006-07-24 15:47:26 -0500 | [diff] [blame] | 1547 | |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 1548 | spin_lock_bh(&session->lock); |
Mike Christie | 1c83469 | 2006-07-24 15:47:26 -0500 | [diff] [blame] | 1549 | tcp_conn->sock = NULL; |
| 1550 | conn->recv_lock = NULL; |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 1551 | spin_unlock_bh(&session->lock); |
| 1552 | sockfd_put(sock); |
Mike Christie | 1c83469 | 2006-07-24 15:47:26 -0500 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | static void |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1556 | iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1557 | { |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1558 | struct iscsi_conn *conn = cls_conn->dd_data; |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1559 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1560 | |
Mike Christie | 1c83469 | 2006-07-24 15:47:26 -0500 | [diff] [blame] | 1561 | iscsi_tcp_release_conn(conn); |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1562 | iscsi_conn_teardown(cls_conn); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1563 | |
Pete Wyckoff | 534284a | 2006-11-08 15:58:31 -0600 | [diff] [blame] | 1564 | if (tcp_conn->tx_hash.tfm) |
| 1565 | crypto_free_hash(tcp_conn->tx_hash.tfm); |
| 1566 | if (tcp_conn->rx_hash.tfm) |
| 1567 | crypto_free_hash(tcp_conn->rx_hash.tfm); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1568 | |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1569 | kfree(tcp_conn); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1570 | } |
| 1571 | |
Mike Christie | 1c83469 | 2006-07-24 15:47:26 -0500 | [diff] [blame] | 1572 | static void |
| 1573 | iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag) |
| 1574 | { |
| 1575 | struct iscsi_conn *conn = cls_conn->dd_data; |
| 1576 | |
| 1577 | iscsi_conn_stop(cls_conn, flag); |
| 1578 | iscsi_tcp_release_conn(conn); |
| 1579 | } |
| 1580 | |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 1581 | static int iscsi_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock, |
| 1582 | char *buf, int *port, |
| 1583 | int (*getname)(struct socket *, struct sockaddr *, |
| 1584 | int *addrlen)) |
| 1585 | { |
| 1586 | struct sockaddr_storage *addr; |
| 1587 | struct sockaddr_in6 *sin6; |
| 1588 | struct sockaddr_in *sin; |
| 1589 | int rc = 0, len; |
| 1590 | |
Al Viro | a920487 | 2007-07-20 16:03:40 +0100 | [diff] [blame] | 1591 | addr = kmalloc(sizeof(*addr), GFP_KERNEL); |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 1592 | if (!addr) |
| 1593 | return -ENOMEM; |
| 1594 | |
| 1595 | if (getname(sock, (struct sockaddr *) addr, &len)) { |
| 1596 | rc = -ENODEV; |
| 1597 | goto free_addr; |
| 1598 | } |
| 1599 | |
| 1600 | switch (addr->ss_family) { |
| 1601 | case AF_INET: |
| 1602 | sin = (struct sockaddr_in *)addr; |
| 1603 | spin_lock_bh(&conn->session->lock); |
| 1604 | sprintf(buf, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr)); |
| 1605 | *port = be16_to_cpu(sin->sin_port); |
| 1606 | spin_unlock_bh(&conn->session->lock); |
| 1607 | break; |
| 1608 | case AF_INET6: |
| 1609 | sin6 = (struct sockaddr_in6 *)addr; |
| 1610 | spin_lock_bh(&conn->session->lock); |
| 1611 | sprintf(buf, NIP6_FMT, NIP6(sin6->sin6_addr)); |
| 1612 | *port = be16_to_cpu(sin6->sin6_port); |
| 1613 | spin_unlock_bh(&conn->session->lock); |
| 1614 | break; |
| 1615 | } |
| 1616 | free_addr: |
| 1617 | kfree(addr); |
| 1618 | return rc; |
| 1619 | } |
| 1620 | |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1621 | static int |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1622 | iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session, |
Or Gerlitz | 264faaa | 2006-05-02 19:46:36 -0500 | [diff] [blame] | 1623 | struct iscsi_cls_conn *cls_conn, uint64_t transport_eph, |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1624 | int is_leading) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1625 | { |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1626 | struct iscsi_conn *conn = cls_conn->dd_data; |
| 1627 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1628 | struct sock *sk; |
| 1629 | struct socket *sock; |
| 1630 | int err; |
| 1631 | |
| 1632 | /* lookup for existing socket */ |
Or Gerlitz | 264faaa | 2006-05-02 19:46:36 -0500 | [diff] [blame] | 1633 | sock = sockfd_lookup((int)transport_eph, &err); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1634 | if (!sock) { |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 1635 | iscsi_conn_printk(KERN_ERR, conn, |
| 1636 | "sockfd_lookup failed %d\n", err); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1637 | return -EEXIST; |
| 1638 | } |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 1639 | /* |
| 1640 | * copy these values now because if we drop the session |
| 1641 | * userspace may still want to query the values since we will |
| 1642 | * be using them for the reconnect |
| 1643 | */ |
| 1644 | err = iscsi_tcp_get_addr(conn, sock, conn->portal_address, |
| 1645 | &conn->portal_port, kernel_getpeername); |
| 1646 | if (err) |
| 1647 | goto free_socket; |
| 1648 | |
| 1649 | err = iscsi_tcp_get_addr(conn, sock, conn->local_address, |
| 1650 | &conn->local_port, kernel_getsockname); |
| 1651 | if (err) |
| 1652 | goto free_socket; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1653 | |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1654 | err = iscsi_conn_bind(cls_session, cls_conn, is_leading); |
| 1655 | if (err) |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 1656 | goto free_socket; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1657 | |
Mike Christie | 67a6111 | 2006-05-30 00:37:20 -0500 | [diff] [blame] | 1658 | /* bind iSCSI connection and socket */ |
| 1659 | tcp_conn->sock = sock; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1660 | |
Mike Christie | 67a6111 | 2006-05-30 00:37:20 -0500 | [diff] [blame] | 1661 | /* setup Socket parameters */ |
| 1662 | sk = sock->sk; |
| 1663 | sk->sk_reuse = 1; |
| 1664 | sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */ |
| 1665 | sk->sk_allocation = GFP_ATOMIC; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1666 | |
Mike Christie | 67a6111 | 2006-05-30 00:37:20 -0500 | [diff] [blame] | 1667 | /* FIXME: disable Nagle's algorithm */ |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1668 | |
Mike Christie | 67a6111 | 2006-05-30 00:37:20 -0500 | [diff] [blame] | 1669 | /* |
| 1670 | * Intercept TCP callbacks for sendfile like receive |
| 1671 | * processing. |
| 1672 | */ |
| 1673 | conn->recv_lock = &sk->sk_callback_lock; |
| 1674 | iscsi_conn_set_callbacks(conn); |
| 1675 | tcp_conn->sendpage = tcp_conn->sock->ops->sendpage; |
| 1676 | /* |
| 1677 | * set receive state machine into initial state |
| 1678 | */ |
Olaf Kirch | da32dd6 | 2007-12-13 12:43:21 -0600 | [diff] [blame] | 1679 | iscsi_tcp_hdr_recv_prep(tcp_conn); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1680 | return 0; |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 1681 | |
| 1682 | free_socket: |
| 1683 | sockfd_put(sock); |
| 1684 | return err; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1685 | } |
| 1686 | |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1687 | /* called with host lock */ |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1688 | static void |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1689 | iscsi_tcp_mtask_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask) |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 1690 | { |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1691 | debug_scsi("mtask deq [cid %d itt 0x%x]\n", conn->id, mtask->itt); |
| 1692 | |
| 1693 | /* Prepare PDU, optionally w/ immediate data */ |
| 1694 | iscsi_tcp_send_hdr_prep(conn, mtask->hdr, sizeof(*mtask->hdr)); |
| 1695 | |
| 1696 | /* If we have immediate data, attach a payload */ |
| 1697 | if (mtask->data_count) |
| 1698 | iscsi_tcp_send_linear_data_prepare(conn, mtask->data, |
| 1699 | mtask->data_count); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | static int |
| 1703 | iscsi_r2tpool_alloc(struct iscsi_session *session) |
| 1704 | { |
| 1705 | int i; |
| 1706 | int cmd_i; |
| 1707 | |
| 1708 | /* |
| 1709 | * initialize per-task: R2T pool and xmit queue |
| 1710 | */ |
| 1711 | for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) { |
| 1712 | struct iscsi_cmd_task *ctask = session->cmds[cmd_i]; |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1713 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1714 | |
| 1715 | /* |
| 1716 | * pre-allocated x4 as much r2ts to handle race when |
| 1717 | * target acks DataOut faster than we data_xmit() queues |
| 1718 | * could replenish r2tqueue. |
| 1719 | */ |
| 1720 | |
| 1721 | /* R2T pool */ |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 1722 | if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4, NULL, |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1723 | sizeof(struct iscsi_r2t_info))) { |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1724 | goto r2t_alloc_fail; |
| 1725 | } |
| 1726 | |
| 1727 | /* R2T xmit queue */ |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1728 | tcp_ctask->r2tqueue = kfifo_alloc( |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1729 | session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL); |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1730 | if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) { |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 1731 | iscsi_pool_free(&tcp_ctask->r2tpool); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1732 | goto r2t_alloc_fail; |
| 1733 | } |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | return 0; |
| 1737 | |
| 1738 | r2t_alloc_fail: |
| 1739 | for (i = 0; i < cmd_i; i++) { |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1740 | struct iscsi_cmd_task *ctask = session->cmds[i]; |
| 1741 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; |
| 1742 | |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1743 | kfifo_free(tcp_ctask->r2tqueue); |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 1744 | iscsi_pool_free(&tcp_ctask->r2tpool); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1745 | } |
| 1746 | return -ENOMEM; |
| 1747 | } |
| 1748 | |
| 1749 | static void |
| 1750 | iscsi_r2tpool_free(struct iscsi_session *session) |
| 1751 | { |
| 1752 | int i; |
| 1753 | |
| 1754 | for (i = 0; i < session->cmds_max; i++) { |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1755 | struct iscsi_cmd_task *ctask = session->cmds[i]; |
| 1756 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; |
| 1757 | |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1758 | kfifo_free(tcp_ctask->r2tqueue); |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 1759 | iscsi_pool_free(&tcp_ctask->r2tpool); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1760 | } |
| 1761 | } |
| 1762 | |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1763 | static int |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 1764 | 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] | 1765 | char *buf, int buflen) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1766 | { |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 1767 | struct iscsi_conn *conn = cls_conn->dd_data; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1768 | struct iscsi_session *session = conn->session; |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1769 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
Mike Christie | 5c75b7f | 2006-06-28 12:00:26 -0500 | [diff] [blame] | 1770 | int value; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1771 | |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1772 | switch(param) { |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1773 | case ISCSI_PARAM_HDRDGST_EN: |
Mike Christie | 5c75b7f | 2006-06-28 12:00:26 -0500 | [diff] [blame] | 1774 | iscsi_set_param(cls_conn, param, buf, buflen); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1775 | break; |
| 1776 | case ISCSI_PARAM_DATADGST_EN: |
Mike Christie | 5c75b7f | 2006-06-28 12:00:26 -0500 | [diff] [blame] | 1777 | iscsi_set_param(cls_conn, param, buf, buflen); |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1778 | tcp_conn->sendpage = conn->datadgst_en ? |
| 1779 | sock_no_sendpage : tcp_conn->sock->ops->sendpage; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1780 | break; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1781 | case ISCSI_PARAM_MAX_R2T: |
Mike Christie | 5c75b7f | 2006-06-28 12:00:26 -0500 | [diff] [blame] | 1782 | sscanf(buf, "%d", &value); |
Mike Christie | df93ffc | 2007-12-13 12:43:42 -0600 | [diff] [blame] | 1783 | if (value <= 0 || !is_power_of_2(value)) |
| 1784 | return -EINVAL; |
| 1785 | if (session->max_r2t == value) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1786 | break; |
| 1787 | iscsi_r2tpool_free(session); |
Mike Christie | 5c75b7f | 2006-06-28 12:00:26 -0500 | [diff] [blame] | 1788 | iscsi_set_param(cls_conn, param, buf, buflen); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1789 | if (iscsi_r2tpool_alloc(session)) |
| 1790 | return -ENOMEM; |
| 1791 | break; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1792 | default: |
Mike Christie | 5c75b7f | 2006-06-28 12:00:26 -0500 | [diff] [blame] | 1793 | return iscsi_set_param(cls_conn, param, buf, buflen); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | return 0; |
| 1797 | } |
| 1798 | |
| 1799 | static int |
Mike Christie | 5c75b7f | 2006-06-28 12:00:26 -0500 | [diff] [blame] | 1800 | iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn, |
| 1801 | enum iscsi_param param, char *buf) |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1802 | { |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 1803 | struct iscsi_conn *conn = cls_conn->dd_data; |
Mike Christie | 5c75b7f | 2006-06-28 12:00:26 -0500 | [diff] [blame] | 1804 | int len; |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1805 | |
| 1806 | switch(param) { |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1807 | case ISCSI_PARAM_CONN_PORT: |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 1808 | spin_lock_bh(&conn->session->lock); |
| 1809 | len = sprintf(buf, "%hu\n", conn->portal_port); |
| 1810 | spin_unlock_bh(&conn->session->lock); |
Mike Christie | 8d2860b | 2006-05-02 19:46:47 -0500 | [diff] [blame] | 1811 | break; |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1812 | case ISCSI_PARAM_CONN_ADDRESS: |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 1813 | spin_lock_bh(&conn->session->lock); |
| 1814 | len = sprintf(buf, "%s\n", conn->portal_address); |
| 1815 | spin_unlock_bh(&conn->session->lock); |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1816 | break; |
| 1817 | default: |
Mike Christie | 5c75b7f | 2006-06-28 12:00:26 -0500 | [diff] [blame] | 1818 | return iscsi_conn_get_param(cls_conn, param, buf); |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | return len; |
| 1822 | } |
| 1823 | |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 1824 | static int |
| 1825 | iscsi_tcp_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param, |
| 1826 | char *buf) |
| 1827 | { |
| 1828 | struct iscsi_session *session = iscsi_hostdata(shost->hostdata); |
| 1829 | int len; |
| 1830 | |
| 1831 | switch (param) { |
| 1832 | case ISCSI_HOST_PARAM_IPADDRESS: |
| 1833 | spin_lock_bh(&session->lock); |
| 1834 | if (!session->leadconn) |
| 1835 | len = -ENODEV; |
| 1836 | else |
| 1837 | len = sprintf(buf, "%s\n", |
| 1838 | session->leadconn->local_address); |
| 1839 | spin_unlock_bh(&session->lock); |
| 1840 | break; |
| 1841 | default: |
| 1842 | return iscsi_host_get_param(shost, param, buf); |
| 1843 | } |
| 1844 | return len; |
| 1845 | } |
| 1846 | |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1847 | static void |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 1848 | 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] | 1849 | { |
Mike Christie | 7b7232f | 2006-02-01 21:06:49 -0600 | [diff] [blame] | 1850 | struct iscsi_conn *conn = cls_conn->dd_data; |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1851 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1852 | |
| 1853 | stats->txdata_octets = conn->txdata_octets; |
| 1854 | stats->rxdata_octets = conn->rxdata_octets; |
| 1855 | stats->scsicmd_pdus = conn->scsicmd_pdus_cnt; |
| 1856 | stats->dataout_pdus = conn->dataout_pdus_cnt; |
| 1857 | stats->scsirsp_pdus = conn->scsirsp_pdus_cnt; |
| 1858 | stats->datain_pdus = conn->datain_pdus_cnt; |
| 1859 | stats->r2t_pdus = conn->r2t_pdus_cnt; |
| 1860 | stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt; |
| 1861 | stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt; |
| 1862 | stats->custom_length = 3; |
| 1863 | strcpy(stats->custom[0].desc, "tx_sendpage_failures"); |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1864 | stats->custom[0].value = tcp_conn->sendpage_failures_cnt; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1865 | strcpy(stats->custom[1].desc, "rx_discontiguous_hdr"); |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1866 | stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1867 | strcpy(stats->custom[2].desc, "eh_abort_cnt"); |
| 1868 | stats->custom[2].value = conn->eh_abort_cnt; |
| 1869 | } |
| 1870 | |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1871 | static struct iscsi_cls_session * |
| 1872 | iscsi_tcp_session_create(struct iscsi_transport *iscsit, |
| 1873 | struct scsi_transport_template *scsit, |
Mike Christie | 1548271 | 2007-05-30 12:57:19 -0500 | [diff] [blame] | 1874 | uint16_t cmds_max, uint16_t qdepth, |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1875 | uint32_t initial_cmdsn, uint32_t *hostno) |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1876 | { |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1877 | struct iscsi_cls_session *cls_session; |
| 1878 | struct iscsi_session *session; |
| 1879 | uint32_t hn; |
| 1880 | int cmd_i; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1881 | |
Mike Christie | 1548271 | 2007-05-30 12:57:19 -0500 | [diff] [blame] | 1882 | cls_session = iscsi_session_setup(iscsit, scsit, cmds_max, qdepth, |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1883 | sizeof(struct iscsi_tcp_cmd_task), |
| 1884 | sizeof(struct iscsi_tcp_mgmt_task), |
| 1885 | initial_cmdsn, &hn); |
| 1886 | if (!cls_session) |
| 1887 | return NULL; |
| 1888 | *hostno = hn; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1889 | |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1890 | session = class_to_transport_session(cls_session); |
| 1891 | for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) { |
| 1892 | struct iscsi_cmd_task *ctask = session->cmds[cmd_i]; |
| 1893 | struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data; |
| 1894 | |
Boaz Harrosh | 004d653 | 2007-12-13 12:43:23 -0600 | [diff] [blame] | 1895 | ctask->hdr = &tcp_ctask->hdr.cmd_hdr; |
| 1896 | ctask->hdr_max = sizeof(tcp_ctask->hdr) - ISCSI_DIGEST_SIZE; |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1897 | } |
| 1898 | |
| 1899 | for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) { |
| 1900 | struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i]; |
| 1901 | struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data; |
| 1902 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 1903 | mtask->hdr = (struct iscsi_hdr *) &tcp_mtask->hdr; |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1904 | } |
| 1905 | |
| 1906 | if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session))) |
| 1907 | goto r2tpool_alloc_fail; |
| 1908 | |
| 1909 | return cls_session; |
| 1910 | |
| 1911 | r2tpool_alloc_fail: |
| 1912 | iscsi_session_teardown(cls_session); |
| 1913 | return NULL; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1914 | } |
| 1915 | |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1916 | static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session) |
| 1917 | { |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1918 | iscsi_r2tpool_free(class_to_transport_session(cls_session)); |
| 1919 | iscsi_session_teardown(cls_session); |
| 1920 | } |
| 1921 | |
Mike Christie | d1d81c0 | 2007-05-30 12:57:21 -0500 | [diff] [blame] | 1922 | static int iscsi_tcp_slave_configure(struct scsi_device *sdev) |
| 1923 | { |
Mike Christie | b6d44fe | 2007-07-26 12:46:47 -0500 | [diff] [blame] | 1924 | blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY); |
Mike Christie | d1d81c0 | 2007-05-30 12:57:21 -0500 | [diff] [blame] | 1925 | blk_queue_dma_alignment(sdev->request_queue, 0); |
| 1926 | return 0; |
| 1927 | } |
| 1928 | |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1929 | static struct scsi_host_template iscsi_sht = { |
Mike Christie | 7974392 | 2007-07-26 12:46:46 -0500 | [diff] [blame] | 1930 | .module = THIS_MODULE, |
Mike Christie | f4246b3 | 2006-07-24 15:47:54 -0500 | [diff] [blame] | 1931 | .name = "iSCSI Initiator over TCP/IP", |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1932 | .queuecommand = iscsi_queuecommand, |
| 1933 | .change_queue_depth = iscsi_change_queue_depth, |
Mike Christie | 1548271 | 2007-05-30 12:57:19 -0500 | [diff] [blame] | 1934 | .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1, |
Mike Christie | 66bbe0c | 2007-12-13 12:43:39 -0600 | [diff] [blame] | 1935 | .sg_tablesize = 4096, |
Mike Christie | 8231f0e | 2007-02-28 17:32:20 -0600 | [diff] [blame] | 1936 | .max_sectors = 0xFFFF, |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1937 | .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN, |
| 1938 | .eh_abort_handler = iscsi_eh_abort, |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1939 | .eh_device_reset_handler= iscsi_eh_device_reset, |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1940 | .eh_host_reset_handler = iscsi_eh_host_reset, |
| 1941 | .use_clustering = DISABLE_CLUSTERING, |
Mike Christie | d1d81c0 | 2007-05-30 12:57:21 -0500 | [diff] [blame] | 1942 | .slave_configure = iscsi_tcp_slave_configure, |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1943 | .proc_name = "iscsi_tcp", |
| 1944 | .this_id = -1, |
| 1945 | }; |
| 1946 | |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1947 | static struct iscsi_transport iscsi_tcp_transport = { |
| 1948 | .owner = THIS_MODULE, |
| 1949 | .name = "tcp", |
| 1950 | .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST |
| 1951 | | CAP_DATADGST, |
Mike Christie | fd7255f | 2006-04-06 21:13:36 -0500 | [diff] [blame] | 1952 | .param_mask = ISCSI_MAX_RECV_DLENGTH | |
| 1953 | ISCSI_MAX_XMIT_DLENGTH | |
| 1954 | ISCSI_HDRDGST_EN | |
| 1955 | ISCSI_DATADGST_EN | |
| 1956 | ISCSI_INITIAL_R2T_EN | |
| 1957 | ISCSI_MAX_R2T | |
| 1958 | ISCSI_IMM_DATA_EN | |
| 1959 | ISCSI_FIRST_BURST | |
| 1960 | ISCSI_MAX_BURST | |
| 1961 | ISCSI_PDU_INORDER_EN | |
| 1962 | ISCSI_DATASEQ_INORDER_EN | |
| 1963 | ISCSI_ERL | |
| 1964 | ISCSI_CONN_PORT | |
Mike Christie | 8d2860b | 2006-05-02 19:46:47 -0500 | [diff] [blame] | 1965 | ISCSI_CONN_ADDRESS | |
Mike Christie | 5c75b7f | 2006-06-28 12:00:26 -0500 | [diff] [blame] | 1966 | ISCSI_EXP_STATSN | |
| 1967 | ISCSI_PERSISTENT_PORT | |
| 1968 | ISCSI_PERSISTENT_ADDRESS | |
Mike Christie | b2c6416 | 2007-05-30 12:57:16 -0500 | [diff] [blame] | 1969 | ISCSI_TARGET_NAME | ISCSI_TPGT | |
| 1970 | ISCSI_USERNAME | ISCSI_PASSWORD | |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1971 | ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN | |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1972 | ISCSI_FAST_ABORT | ISCSI_ABORT_TMO | |
| 1973 | ISCSI_LU_RESET_TMO | |
| 1974 | ISCSI_PING_TMO | ISCSI_RECV_TMO, |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 1975 | .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS | |
Mike Christie | d8196ed | 2007-05-30 12:57:25 -0500 | [diff] [blame] | 1976 | ISCSI_HOST_INITIATOR_NAME | |
| 1977 | ISCSI_HOST_NETDEV_NAME, |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1978 | .host_template = &iscsi_sht, |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1979 | .conndata_size = sizeof(struct iscsi_conn), |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1980 | .max_conn = 1, |
Mike Christie | 66bbe0c | 2007-12-13 12:43:39 -0600 | [diff] [blame] | 1981 | .max_cmd_len = 16, |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1982 | /* session management */ |
| 1983 | .create_session = iscsi_tcp_session_create, |
| 1984 | .destroy_session = iscsi_tcp_session_destroy, |
| 1985 | /* connection management */ |
| 1986 | .create_conn = iscsi_tcp_conn_create, |
| 1987 | .bind_conn = iscsi_tcp_conn_bind, |
| 1988 | .destroy_conn = iscsi_tcp_conn_destroy, |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1989 | .set_param = iscsi_conn_set_param, |
Mike Christie | 5c75b7f | 2006-06-28 12:00:26 -0500 | [diff] [blame] | 1990 | .get_conn_param = iscsi_tcp_conn_get_param, |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 1991 | .get_session_param = iscsi_session_get_param, |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1992 | .start_conn = iscsi_conn_start, |
Mike Christie | 1c83469 | 2006-07-24 15:47:26 -0500 | [diff] [blame] | 1993 | .stop_conn = iscsi_tcp_conn_stop, |
Mike Christie | 0801c24 | 2007-05-30 12:57:12 -0500 | [diff] [blame] | 1994 | /* iscsi host params */ |
Mike Christie | 2223696 | 2007-05-30 12:57:24 -0500 | [diff] [blame] | 1995 | .get_host_param = iscsi_tcp_host_get_param, |
Mike Christie | 0801c24 | 2007-05-30 12:57:12 -0500 | [diff] [blame] | 1996 | .set_host_param = iscsi_host_set_param, |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 1997 | /* IO */ |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 1998 | .send_pdu = iscsi_conn_send_pdu, |
| 1999 | .get_stats = iscsi_conn_get_stats, |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 2000 | .init_cmd_task = iscsi_tcp_ctask_init, |
| 2001 | .init_mgmt_task = iscsi_tcp_mtask_init, |
Mike Christie | 5bb0b55 | 2006-04-06 21:26:46 -0500 | [diff] [blame] | 2002 | .xmit_cmd_task = iscsi_tcp_ctask_xmit, |
| 2003 | .xmit_mgmt_task = iscsi_tcp_mtask_xmit, |
| 2004 | .cleanup_cmd_task = iscsi_tcp_cleanup_ctask, |
| 2005 | /* recovery */ |
Mike Christie | 30a6c65 | 2006-04-06 21:13:39 -0500 | [diff] [blame] | 2006 | .session_recovery_timedout = iscsi_session_recovery_timedout, |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 2007 | }; |
| 2008 | |
| 2009 | static int __init |
| 2010 | iscsi_tcp_init(void) |
| 2011 | { |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 2012 | if (iscsi_max_lun < 1) { |
Or Gerlitz | be2df72 | 2006-05-02 19:46:43 -0500 | [diff] [blame] | 2013 | printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n", |
| 2014 | iscsi_max_lun); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 2015 | return -EINVAL; |
| 2016 | } |
| 2017 | iscsi_tcp_transport.max_lun = iscsi_max_lun; |
| 2018 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 2019 | if (!iscsi_register_transport(&iscsi_tcp_transport)) |
Mike Christie | ffbfe92 | 2006-05-18 20:31:36 -0500 | [diff] [blame] | 2020 | return -ENODEV; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 2021 | |
Mike Christie | 7b8631b | 2006-01-13 18:05:50 -0600 | [diff] [blame] | 2022 | return 0; |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | static void __exit |
| 2026 | iscsi_tcp_exit(void) |
| 2027 | { |
| 2028 | iscsi_unregister_transport(&iscsi_tcp_transport); |
Alex Aizman | 7ba2471 | 2005-08-04 19:30:08 -0700 | [diff] [blame] | 2029 | } |
| 2030 | |
| 2031 | module_init(iscsi_tcp_init); |
| 2032 | module_exit(iscsi_tcp_exit); |