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