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