Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 1 | /* |
| 2 | * iSCSI over TCP/IP Data-Path lib |
| 3 | * |
| 4 | * Copyright (C) 2004 Dmitry Yusupov |
| 5 | * Copyright (C) 2004 Alex Aizman |
| 6 | * Copyright (C) 2005 - 2006 Mike Christie |
| 7 | * Copyright (C) 2006 Red Hat, Inc. All rights reserved. |
| 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 32 | #include <linux/slab.h> |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 33 | #include <linux/file.h> |
| 34 | #include <linux/blkdev.h> |
| 35 | #include <linux/crypto.h> |
| 36 | #include <linux/delay.h> |
| 37 | #include <linux/kfifo.h> |
| 38 | #include <linux/scatterlist.h> |
Paul Gortmaker | acf3368f | 2011-05-27 09:47:43 -0400 | [diff] [blame] | 39 | #include <linux/module.h> |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 40 | #include <net/tcp.h> |
| 41 | #include <scsi/scsi_cmnd.h> |
| 42 | #include <scsi/scsi_device.h> |
| 43 | #include <scsi/scsi_host.h> |
| 44 | #include <scsi/scsi.h> |
| 45 | #include <scsi/scsi_transport_iscsi.h> |
| 46 | |
| 47 | #include "iscsi_tcp.h" |
| 48 | |
| 49 | MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, " |
| 50 | "Dmitry Yusupov <dmitry_yus@yahoo.com>, " |
| 51 | "Alex Aizman <itn780@yahoo.com>"); |
| 52 | MODULE_DESCRIPTION("iSCSI/TCP data-path"); |
| 53 | MODULE_LICENSE("GPL"); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 54 | |
Mike Christie | 0ab1c25 | 2009-03-05 14:45:59 -0600 | [diff] [blame] | 55 | static int iscsi_dbg_libtcp; |
| 56 | module_param_named(debug_libiscsi_tcp, iscsi_dbg_libtcp, int, |
| 57 | S_IRUGO | S_IWUSR); |
| 58 | MODULE_PARM_DESC(debug_libiscsi_tcp, "Turn on debugging for libiscsi_tcp " |
| 59 | "module. Set to 1 to turn on, and zero to turn off. Default " |
| 60 | "is off."); |
| 61 | |
| 62 | #define ISCSI_DBG_TCP(_conn, dbg_fmt, arg...) \ |
| 63 | do { \ |
| 64 | if (iscsi_dbg_libtcp) \ |
| 65 | iscsi_conn_printk(KERN_INFO, _conn, \ |
| 66 | "%s " dbg_fmt, \ |
| 67 | __func__, ##arg); \ |
| 68 | } while (0); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 69 | |
| 70 | static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn, |
| 71 | struct iscsi_segment *segment); |
| 72 | |
| 73 | /* |
| 74 | * Scatterlist handling: inside the iscsi_segment, we |
| 75 | * remember an index into the scatterlist, and set data/size |
| 76 | * to the current scatterlist entry. For highmem pages, we |
| 77 | * kmap as needed. |
| 78 | * |
| 79 | * Note that the page is unmapped when we return from |
| 80 | * TCP's data_ready handler, so we may end up mapping and |
| 81 | * unmapping the same page repeatedly. The whole reason |
| 82 | * for this is that we shouldn't keep the page mapped |
| 83 | * outside the softirq. |
| 84 | */ |
| 85 | |
| 86 | /** |
| 87 | * iscsi_tcp_segment_init_sg - init indicated scatterlist entry |
| 88 | * @segment: the buffer object |
| 89 | * @sg: scatterlist |
| 90 | * @offset: byte offset into that sg entry |
| 91 | * |
| 92 | * This function sets up the segment so that subsequent |
| 93 | * data is copied to the indicated sg entry, at the given |
| 94 | * offset. |
| 95 | */ |
| 96 | static inline void |
| 97 | iscsi_tcp_segment_init_sg(struct iscsi_segment *segment, |
| 98 | struct scatterlist *sg, unsigned int offset) |
| 99 | { |
| 100 | segment->sg = sg; |
| 101 | segment->sg_offset = offset; |
| 102 | segment->size = min(sg->length - offset, |
| 103 | segment->total_size - segment->total_copied); |
| 104 | segment->data = NULL; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * iscsi_tcp_segment_map - map the current S/G page |
| 109 | * @segment: iscsi_segment |
| 110 | * @recv: 1 if called from recv path |
| 111 | * |
| 112 | * We only need to possibly kmap data if scatter lists are being used, |
| 113 | * because the iscsi passthrough and internal IO paths will never use high |
| 114 | * mem pages. |
| 115 | */ |
| 116 | static void iscsi_tcp_segment_map(struct iscsi_segment *segment, int recv) |
| 117 | { |
| 118 | struct scatterlist *sg; |
| 119 | |
| 120 | if (segment->data != NULL || !segment->sg) |
| 121 | return; |
| 122 | |
| 123 | sg = segment->sg; |
| 124 | BUG_ON(segment->sg_mapped); |
| 125 | BUG_ON(sg->length == 0); |
| 126 | |
| 127 | /* |
| 128 | * If the page count is greater than one it is ok to send |
| 129 | * to the network layer's zero copy send path. If not we |
| 130 | * have to go the slow sendmsg path. We always map for the |
| 131 | * recv path. |
| 132 | */ |
| 133 | if (page_count(sg_page(sg)) >= 1 && !recv) |
| 134 | return; |
| 135 | |
Mike Christie | 70c7c88 | 2011-03-17 16:22:17 -0500 | [diff] [blame] | 136 | if (recv) { |
| 137 | segment->atomic_mapped = true; |
Cong Wang | 77dfce0 | 2011-11-25 23:14:23 +0800 | [diff] [blame] | 138 | segment->sg_mapped = kmap_atomic(sg_page(sg)); |
Mike Christie | 70c7c88 | 2011-03-17 16:22:17 -0500 | [diff] [blame] | 139 | } else { |
| 140 | segment->atomic_mapped = false; |
| 141 | /* the xmit path can sleep with the page mapped so use kmap */ |
| 142 | segment->sg_mapped = kmap(sg_page(sg)); |
| 143 | } |
| 144 | |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 145 | segment->data = segment->sg_mapped + sg->offset + segment->sg_offset; |
| 146 | } |
| 147 | |
| 148 | void iscsi_tcp_segment_unmap(struct iscsi_segment *segment) |
| 149 | { |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 150 | if (segment->sg_mapped) { |
Mike Christie | 70c7c88 | 2011-03-17 16:22:17 -0500 | [diff] [blame] | 151 | if (segment->atomic_mapped) |
Cong Wang | 77dfce0 | 2011-11-25 23:14:23 +0800 | [diff] [blame] | 152 | kunmap_atomic(segment->sg_mapped); |
Mike Christie | 70c7c88 | 2011-03-17 16:22:17 -0500 | [diff] [blame] | 153 | else |
| 154 | kunmap(sg_page(segment->sg)); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 155 | segment->sg_mapped = NULL; |
| 156 | segment->data = NULL; |
| 157 | } |
| 158 | } |
| 159 | EXPORT_SYMBOL_GPL(iscsi_tcp_segment_unmap); |
| 160 | |
| 161 | /* |
| 162 | * Splice the digest buffer into the buffer |
| 163 | */ |
| 164 | static inline void |
| 165 | iscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest) |
| 166 | { |
| 167 | segment->data = digest; |
| 168 | segment->digest_len = ISCSI_DIGEST_SIZE; |
| 169 | segment->total_size += ISCSI_DIGEST_SIZE; |
| 170 | segment->size = ISCSI_DIGEST_SIZE; |
| 171 | segment->copied = 0; |
| 172 | segment->sg = NULL; |
| 173 | segment->hash = NULL; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * iscsi_tcp_segment_done - check whether the segment is complete |
Mike Christie | 6df19a7 | 2008-12-02 00:32:16 -0600 | [diff] [blame] | 178 | * @tcp_conn: iscsi tcp connection |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 179 | * @segment: iscsi segment to check |
| 180 | * @recv: set to one of this is called from the recv path |
| 181 | * @copied: number of bytes copied |
| 182 | * |
| 183 | * Check if we're done receiving this segment. If the receive |
| 184 | * buffer is full but we expect more data, move on to the |
| 185 | * next entry in the scatterlist. |
| 186 | * |
| 187 | * If the amount of data we received isn't a multiple of 4, |
| 188 | * we will transparently receive the pad bytes, too. |
| 189 | * |
| 190 | * This function must be re-entrant. |
| 191 | */ |
Mike Christie | 6df19a7 | 2008-12-02 00:32:16 -0600 | [diff] [blame] | 192 | int iscsi_tcp_segment_done(struct iscsi_tcp_conn *tcp_conn, |
| 193 | struct iscsi_segment *segment, int recv, |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 194 | unsigned copied) |
| 195 | { |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 196 | struct scatterlist sg; |
| 197 | unsigned int pad; |
| 198 | |
Mike Christie | 0ab1c25 | 2009-03-05 14:45:59 -0600 | [diff] [blame] | 199 | ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "copied %u %u size %u %s\n", |
| 200 | segment->copied, copied, segment->size, |
| 201 | recv ? "recv" : "xmit"); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 202 | if (segment->hash && copied) { |
| 203 | /* |
| 204 | * If a segment is kmapd we must unmap it before sending |
| 205 | * to the crypto layer since that will try to kmap it again. |
| 206 | */ |
| 207 | iscsi_tcp_segment_unmap(segment); |
| 208 | |
| 209 | if (!segment->data) { |
| 210 | sg_init_table(&sg, 1); |
| 211 | sg_set_page(&sg, sg_page(segment->sg), copied, |
| 212 | segment->copied + segment->sg_offset + |
| 213 | segment->sg->offset); |
| 214 | } else |
| 215 | sg_init_one(&sg, segment->data + segment->copied, |
| 216 | copied); |
| 217 | crypto_hash_update(segment->hash, &sg, copied); |
| 218 | } |
| 219 | |
| 220 | segment->copied += copied; |
| 221 | if (segment->copied < segment->size) { |
| 222 | iscsi_tcp_segment_map(segment, recv); |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | segment->total_copied += segment->copied; |
| 227 | segment->copied = 0; |
| 228 | segment->size = 0; |
| 229 | |
| 230 | /* Unmap the current scatterlist page, if there is one. */ |
| 231 | iscsi_tcp_segment_unmap(segment); |
| 232 | |
| 233 | /* Do we have more scatterlist entries? */ |
Mike Christie | 0ab1c25 | 2009-03-05 14:45:59 -0600 | [diff] [blame] | 234 | ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "total copied %u total size %u\n", |
| 235 | segment->total_copied, segment->total_size); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 236 | if (segment->total_copied < segment->total_size) { |
| 237 | /* Proceed to the next entry in the scatterlist. */ |
| 238 | iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg), |
| 239 | 0); |
| 240 | iscsi_tcp_segment_map(segment, recv); |
| 241 | BUG_ON(segment->size == 0); |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | /* Do we need to handle padding? */ |
Mike Christie | 6df19a7 | 2008-12-02 00:32:16 -0600 | [diff] [blame] | 246 | if (!(tcp_conn->iscsi_conn->session->tt->caps & CAP_PADDING_OFFLOAD)) { |
| 247 | pad = iscsi_padding(segment->total_copied); |
| 248 | if (pad != 0) { |
Mike Christie | 0ab1c25 | 2009-03-05 14:45:59 -0600 | [diff] [blame] | 249 | ISCSI_DBG_TCP(tcp_conn->iscsi_conn, |
| 250 | "consume %d pad bytes\n", pad); |
Mike Christie | 6df19a7 | 2008-12-02 00:32:16 -0600 | [diff] [blame] | 251 | segment->total_size += pad; |
| 252 | segment->size = pad; |
Karen Xie | 2856830 | 2009-01-10 19:06:07 -0800 | [diff] [blame] | 253 | segment->data = segment->padbuf; |
Mike Christie | 6df19a7 | 2008-12-02 00:32:16 -0600 | [diff] [blame] | 254 | return 0; |
| 255 | } |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Set us up for transferring the data digest. hdr digest |
| 260 | * is completely handled in hdr done function. |
| 261 | */ |
| 262 | if (segment->hash) { |
| 263 | crypto_hash_final(segment->hash, segment->digest); |
| 264 | iscsi_tcp_segment_splice_digest(segment, |
| 265 | recv ? segment->recv_digest : segment->digest); |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | return 1; |
| 270 | } |
| 271 | EXPORT_SYMBOL_GPL(iscsi_tcp_segment_done); |
| 272 | |
| 273 | /** |
| 274 | * iscsi_tcp_segment_recv - copy data to segment |
| 275 | * @tcp_conn: the iSCSI TCP connection |
| 276 | * @segment: the buffer to copy to |
| 277 | * @ptr: data pointer |
| 278 | * @len: amount of data available |
| 279 | * |
| 280 | * This function copies up to @len bytes to the |
| 281 | * given buffer, and returns the number of bytes |
| 282 | * consumed, which can actually be less than @len. |
| 283 | * |
| 284 | * If hash digest is enabled, the function will update the |
| 285 | * hash while copying. |
| 286 | * Combining these two operations doesn't buy us a lot (yet), |
| 287 | * but in the future we could implement combined copy+crc, |
| 288 | * just way we do for network layer checksums. |
| 289 | */ |
| 290 | static int |
| 291 | iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn, |
| 292 | struct iscsi_segment *segment, const void *ptr, |
| 293 | unsigned int len) |
| 294 | { |
| 295 | unsigned int copy = 0, copied = 0; |
| 296 | |
Mike Christie | 6df19a7 | 2008-12-02 00:32:16 -0600 | [diff] [blame] | 297 | while (!iscsi_tcp_segment_done(tcp_conn, segment, 1, copy)) { |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 298 | if (copied == len) { |
Mike Christie | 0ab1c25 | 2009-03-05 14:45:59 -0600 | [diff] [blame] | 299 | ISCSI_DBG_TCP(tcp_conn->iscsi_conn, |
| 300 | "copied %d bytes\n", len); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 301 | break; |
| 302 | } |
| 303 | |
| 304 | copy = min(len - copied, segment->size - segment->copied); |
Mike Christie | 0ab1c25 | 2009-03-05 14:45:59 -0600 | [diff] [blame] | 305 | ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "copying %d\n", copy); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 306 | memcpy(segment->data + segment->copied, ptr + copied, copy); |
| 307 | copied += copy; |
| 308 | } |
| 309 | return copied; |
| 310 | } |
| 311 | |
| 312 | inline void |
| 313 | iscsi_tcp_dgst_header(struct hash_desc *hash, const void *hdr, size_t hdrlen, |
| 314 | unsigned char digest[ISCSI_DIGEST_SIZE]) |
| 315 | { |
| 316 | struct scatterlist sg; |
| 317 | |
| 318 | sg_init_one(&sg, hdr, hdrlen); |
| 319 | crypto_hash_digest(hash, &sg, hdrlen, digest); |
| 320 | } |
| 321 | EXPORT_SYMBOL_GPL(iscsi_tcp_dgst_header); |
| 322 | |
| 323 | static inline int |
| 324 | iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn, |
| 325 | struct iscsi_segment *segment) |
| 326 | { |
| 327 | if (!segment->digest_len) |
| 328 | return 1; |
| 329 | |
| 330 | if (memcmp(segment->recv_digest, segment->digest, |
| 331 | segment->digest_len)) { |
Mike Christie | 0ab1c25 | 2009-03-05 14:45:59 -0600 | [diff] [blame] | 332 | ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "digest mismatch\n"); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | return 1; |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * Helper function to set up segment buffer |
| 341 | */ |
| 342 | static inline void |
| 343 | __iscsi_segment_init(struct iscsi_segment *segment, size_t size, |
| 344 | iscsi_segment_done_fn_t *done, struct hash_desc *hash) |
| 345 | { |
| 346 | memset(segment, 0, sizeof(*segment)); |
| 347 | segment->total_size = size; |
| 348 | segment->done = done; |
| 349 | |
| 350 | if (hash) { |
| 351 | segment->hash = hash; |
| 352 | crypto_hash_init(hash); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | inline void |
| 357 | iscsi_segment_init_linear(struct iscsi_segment *segment, void *data, |
| 358 | size_t size, iscsi_segment_done_fn_t *done, |
| 359 | struct hash_desc *hash) |
| 360 | { |
| 361 | __iscsi_segment_init(segment, size, done, hash); |
| 362 | segment->data = data; |
| 363 | segment->size = size; |
| 364 | } |
| 365 | EXPORT_SYMBOL_GPL(iscsi_segment_init_linear); |
| 366 | |
| 367 | inline int |
| 368 | iscsi_segment_seek_sg(struct iscsi_segment *segment, |
| 369 | struct scatterlist *sg_list, unsigned int sg_count, |
| 370 | unsigned int offset, size_t size, |
| 371 | iscsi_segment_done_fn_t *done, struct hash_desc *hash) |
| 372 | { |
| 373 | struct scatterlist *sg; |
| 374 | unsigned int i; |
| 375 | |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 376 | __iscsi_segment_init(segment, size, done, hash); |
| 377 | for_each_sg(sg_list, sg, sg_count, i) { |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 378 | if (offset < sg->length) { |
| 379 | iscsi_tcp_segment_init_sg(segment, sg, offset); |
| 380 | return 0; |
| 381 | } |
| 382 | offset -= sg->length; |
| 383 | } |
| 384 | |
| 385 | return ISCSI_ERR_DATA_OFFSET; |
| 386 | } |
| 387 | EXPORT_SYMBOL_GPL(iscsi_segment_seek_sg); |
| 388 | |
| 389 | /** |
| 390 | * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception |
| 391 | * @tcp_conn: iscsi connection to prep for |
| 392 | * |
| 393 | * This function always passes NULL for the hash argument, because when this |
| 394 | * function is called we do not yet know the final size of the header and want |
| 395 | * to delay the digest processing until we know that. |
| 396 | */ |
| 397 | void iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn) |
| 398 | { |
Mike Christie | 0ab1c25 | 2009-03-05 14:45:59 -0600 | [diff] [blame] | 399 | ISCSI_DBG_TCP(tcp_conn->iscsi_conn, |
| 400 | "(%s)\n", tcp_conn->iscsi_conn->hdrdgst_en ? |
| 401 | "digest enabled" : "digest disabled"); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 402 | iscsi_segment_init_linear(&tcp_conn->in.segment, |
| 403 | tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr), |
| 404 | iscsi_tcp_hdr_recv_done, NULL); |
| 405 | } |
| 406 | EXPORT_SYMBOL_GPL(iscsi_tcp_hdr_recv_prep); |
| 407 | |
| 408 | /* |
| 409 | * Handle incoming reply to any other type of command |
| 410 | */ |
| 411 | static int |
| 412 | iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn, |
| 413 | struct iscsi_segment *segment) |
| 414 | { |
| 415 | struct iscsi_conn *conn = tcp_conn->iscsi_conn; |
| 416 | int rc = 0; |
| 417 | |
| 418 | if (!iscsi_tcp_dgst_verify(tcp_conn, segment)) |
| 419 | return ISCSI_ERR_DATA_DGST; |
| 420 | |
| 421 | rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, |
| 422 | conn->data, tcp_conn->in.datalen); |
| 423 | if (rc) |
| 424 | return rc; |
| 425 | |
| 426 | iscsi_tcp_hdr_recv_prep(tcp_conn); |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | static void |
| 431 | iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn) |
| 432 | { |
| 433 | struct iscsi_conn *conn = tcp_conn->iscsi_conn; |
| 434 | struct hash_desc *rx_hash = NULL; |
| 435 | |
Dan Carpenter | ef7d17a | 2010-04-09 22:07:36 -0500 | [diff] [blame] | 436 | if (conn->datadgst_en && |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 437 | !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) |
| 438 | rx_hash = tcp_conn->rx_hash; |
| 439 | |
| 440 | iscsi_segment_init_linear(&tcp_conn->in.segment, |
| 441 | conn->data, tcp_conn->in.datalen, |
| 442 | iscsi_tcp_data_recv_done, rx_hash); |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * iscsi_tcp_cleanup_task - free tcp_task resources |
| 447 | * @task: iscsi task |
| 448 | * |
| 449 | * must be called with session lock |
| 450 | */ |
| 451 | void iscsi_tcp_cleanup_task(struct iscsi_task *task) |
| 452 | { |
| 453 | struct iscsi_tcp_task *tcp_task = task->dd_data; |
| 454 | struct iscsi_r2t_info *r2t; |
| 455 | |
Mike Christie | b3cd505 | 2009-05-13 17:57:49 -0500 | [diff] [blame] | 456 | /* nothing to do for mgmt */ |
| 457 | if (!task->sc) |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 458 | return; |
| 459 | |
| 460 | /* flush task's r2t queues */ |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 461 | while (kfifo_out(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*))) { |
| 462 | kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t, |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 463 | sizeof(void*)); |
Mike Christie | 0ab1c25 | 2009-03-05 14:45:59 -0600 | [diff] [blame] | 464 | ISCSI_DBG_TCP(task->conn, "pending r2t dropped\n"); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | r2t = tcp_task->r2t; |
| 468 | if (r2t != NULL) { |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 469 | kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t, |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 470 | sizeof(void*)); |
| 471 | tcp_task->r2t = NULL; |
| 472 | } |
| 473 | } |
| 474 | EXPORT_SYMBOL_GPL(iscsi_tcp_cleanup_task); |
| 475 | |
| 476 | /** |
| 477 | * iscsi_tcp_data_in - SCSI Data-In Response processing |
| 478 | * @conn: iscsi connection |
| 479 | * @task: scsi command task |
| 480 | */ |
| 481 | static int iscsi_tcp_data_in(struct iscsi_conn *conn, struct iscsi_task *task) |
| 482 | { |
| 483 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
| 484 | struct iscsi_tcp_task *tcp_task = task->dd_data; |
| 485 | struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr; |
| 486 | int datasn = be32_to_cpu(rhdr->datasn); |
| 487 | unsigned total_in_length = scsi_in(task->sc)->length; |
| 488 | |
Mike Christie | edbc9aa05 | 2009-05-13 17:57:42 -0500 | [diff] [blame] | 489 | /* |
| 490 | * lib iscsi will update this in the completion handling if there |
| 491 | * is status. |
| 492 | */ |
| 493 | if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS)) |
| 494 | iscsi_update_cmdsn(conn->session, (struct iscsi_nopin*)rhdr); |
| 495 | |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 496 | if (tcp_conn->in.datalen == 0) |
| 497 | return 0; |
| 498 | |
| 499 | if (tcp_task->exp_datasn != datasn) { |
Mike Christie | 0ab1c25 | 2009-03-05 14:45:59 -0600 | [diff] [blame] | 500 | ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->datasn(%d)" |
| 501 | "\n", tcp_task->exp_datasn, datasn); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 502 | return ISCSI_ERR_DATASN; |
| 503 | } |
| 504 | |
| 505 | tcp_task->exp_datasn++; |
| 506 | |
| 507 | tcp_task->data_offset = be32_to_cpu(rhdr->offset); |
| 508 | if (tcp_task->data_offset + tcp_conn->in.datalen > total_in_length) { |
Mike Christie | 0ab1c25 | 2009-03-05 14:45:59 -0600 | [diff] [blame] | 509 | ISCSI_DBG_TCP(conn, "data_offset(%d) + data_len(%d) > " |
| 510 | "total_length_in(%d)\n", tcp_task->data_offset, |
| 511 | tcp_conn->in.datalen, total_in_length); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 512 | return ISCSI_ERR_DATA_OFFSET; |
| 513 | } |
| 514 | |
| 515 | conn->datain_pdus_cnt++; |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * iscsi_tcp_r2t_rsp - iSCSI R2T Response processing |
| 521 | * @conn: iscsi connection |
| 522 | * @task: scsi command task |
| 523 | */ |
| 524 | static int iscsi_tcp_r2t_rsp(struct iscsi_conn *conn, struct iscsi_task *task) |
| 525 | { |
| 526 | struct iscsi_session *session = conn->session; |
| 527 | struct iscsi_tcp_task *tcp_task = task->dd_data; |
| 528 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
| 529 | struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr; |
| 530 | struct iscsi_r2t_info *r2t; |
| 531 | int r2tsn = be32_to_cpu(rhdr->r2tsn); |
Shlomo Pongratz | 5d0fddd | 2014-02-07 00:41:37 -0600 | [diff] [blame^] | 532 | u32 data_length; |
| 533 | u32 data_offset; |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 534 | int rc; |
| 535 | |
| 536 | if (tcp_conn->in.datalen) { |
| 537 | iscsi_conn_printk(KERN_ERR, conn, |
| 538 | "invalid R2t with datalen %d\n", |
| 539 | tcp_conn->in.datalen); |
| 540 | return ISCSI_ERR_DATALEN; |
| 541 | } |
| 542 | |
| 543 | if (tcp_task->exp_datasn != r2tsn){ |
Mike Christie | 0ab1c25 | 2009-03-05 14:45:59 -0600 | [diff] [blame] | 544 | ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->r2tsn(%d)\n", |
| 545 | tcp_task->exp_datasn, r2tsn); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 546 | return ISCSI_ERR_R2TSN; |
| 547 | } |
| 548 | |
| 549 | /* fill-in new R2T associated with the task */ |
| 550 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr); |
| 551 | |
| 552 | if (!task->sc || session->state != ISCSI_STATE_LOGGED_IN) { |
| 553 | iscsi_conn_printk(KERN_INFO, conn, |
| 554 | "dropping R2T itt %d in recovery.\n", |
| 555 | task->itt); |
| 556 | return 0; |
| 557 | } |
| 558 | |
Shlomo Pongratz | 5d0fddd | 2014-02-07 00:41:37 -0600 | [diff] [blame^] | 559 | data_length = be32_to_cpu(rhdr->data_length); |
| 560 | if (data_length == 0) { |
| 561 | iscsi_conn_printk(KERN_ERR, conn, |
| 562 | "invalid R2T with zero data len\n"); |
| 563 | return ISCSI_ERR_DATALEN; |
| 564 | } |
| 565 | |
| 566 | if (data_length > session->max_burst) |
| 567 | ISCSI_DBG_TCP(conn, "invalid R2T with data len %u and max " |
| 568 | "burst %u. Attempting to execute request.\n", |
| 569 | data_length, session->max_burst); |
| 570 | |
| 571 | data_offset = be32_to_cpu(rhdr->data_offset); |
| 572 | if (data_offset + data_length > scsi_out(task->sc)->length) { |
| 573 | iscsi_conn_printk(KERN_ERR, conn, |
| 574 | "invalid R2T with data len %u at offset %u " |
| 575 | "and total length %d\n", data_length, |
| 576 | data_offset, scsi_out(task->sc)->length); |
| 577 | return ISCSI_ERR_DATALEN; |
| 578 | } |
| 579 | |
| 580 | rc = kfifo_out(&tcp_task->r2tpool.queue, (void *)&r2t, sizeof(void *)); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 581 | if (!rc) { |
| 582 | iscsi_conn_printk(KERN_ERR, conn, "Could not allocate R2T. " |
| 583 | "Target has sent more R2Ts than it " |
Masanari Iida | 278cee0 | 2013-06-01 01:30:56 +0900 | [diff] [blame] | 584 | "negotiated for or driver has leaked.\n"); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 585 | return ISCSI_ERR_PROTO; |
| 586 | } |
| 587 | |
| 588 | r2t->exp_statsn = rhdr->statsn; |
Shlomo Pongratz | 5d0fddd | 2014-02-07 00:41:37 -0600 | [diff] [blame^] | 589 | r2t->data_length = data_length; |
| 590 | r2t->data_offset = data_offset; |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 591 | |
| 592 | r2t->ttt = rhdr->ttt; /* no flip */ |
| 593 | r2t->datasn = 0; |
| 594 | r2t->sent = 0; |
| 595 | |
| 596 | tcp_task->exp_datasn = r2tsn + 1; |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 597 | kfifo_in(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*)); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 598 | conn->r2t_pdus_cnt++; |
| 599 | |
| 600 | iscsi_requeue_task(task); |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | /* |
| 605 | * Handle incoming reply to DataIn command |
| 606 | */ |
| 607 | static int |
| 608 | iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn, |
| 609 | struct iscsi_segment *segment) |
| 610 | { |
| 611 | struct iscsi_conn *conn = tcp_conn->iscsi_conn; |
| 612 | struct iscsi_hdr *hdr = tcp_conn->in.hdr; |
| 613 | int rc; |
| 614 | |
| 615 | if (!iscsi_tcp_dgst_verify(tcp_conn, segment)) |
| 616 | return ISCSI_ERR_DATA_DGST; |
| 617 | |
| 618 | /* check for non-exceptional status */ |
| 619 | if (hdr->flags & ISCSI_FLAG_DATA_STATUS) { |
| 620 | rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0); |
| 621 | if (rc) |
| 622 | return rc; |
| 623 | } |
| 624 | |
| 625 | iscsi_tcp_hdr_recv_prep(tcp_conn); |
| 626 | return 0; |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * iscsi_tcp_hdr_dissect - process PDU header |
| 631 | * @conn: iSCSI connection |
| 632 | * @hdr: PDU header |
| 633 | * |
| 634 | * This function analyzes the header of the PDU received, |
| 635 | * and performs several sanity checks. If the PDU is accompanied |
| 636 | * by data, the receive buffer is set up to copy the incoming data |
| 637 | * to the correct location. |
| 638 | */ |
| 639 | static int |
| 640 | iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr) |
| 641 | { |
| 642 | int rc = 0, opcode, ahslen; |
| 643 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
| 644 | struct iscsi_task *task; |
| 645 | |
| 646 | /* verify PDU length */ |
| 647 | tcp_conn->in.datalen = ntoh24(hdr->dlength); |
| 648 | if (tcp_conn->in.datalen > conn->max_recv_dlength) { |
| 649 | iscsi_conn_printk(KERN_ERR, conn, |
| 650 | "iscsi_tcp: datalen %d > %d\n", |
| 651 | tcp_conn->in.datalen, conn->max_recv_dlength); |
| 652 | return ISCSI_ERR_DATALEN; |
| 653 | } |
| 654 | |
| 655 | /* Additional header segments. So far, we don't |
| 656 | * process additional headers. |
| 657 | */ |
| 658 | ahslen = hdr->hlength << 2; |
| 659 | |
| 660 | opcode = hdr->opcode & ISCSI_OPCODE_MASK; |
| 661 | /* verify itt (itt encoding: age+cid+itt) */ |
| 662 | rc = iscsi_verify_itt(conn, hdr->itt); |
| 663 | if (rc) |
| 664 | return rc; |
| 665 | |
Mike Christie | 0ab1c25 | 2009-03-05 14:45:59 -0600 | [diff] [blame] | 666 | ISCSI_DBG_TCP(conn, "opcode 0x%x ahslen %d datalen %d\n", |
| 667 | opcode, ahslen, tcp_conn->in.datalen); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 668 | |
| 669 | switch(opcode) { |
| 670 | case ISCSI_OP_SCSI_DATA_IN: |
| 671 | spin_lock(&conn->session->lock); |
| 672 | task = iscsi_itt_to_ctask(conn, hdr->itt); |
| 673 | if (!task) |
| 674 | rc = ISCSI_ERR_BAD_ITT; |
| 675 | else |
| 676 | rc = iscsi_tcp_data_in(conn, task); |
| 677 | if (rc) { |
| 678 | spin_unlock(&conn->session->lock); |
| 679 | break; |
| 680 | } |
| 681 | |
| 682 | if (tcp_conn->in.datalen) { |
| 683 | struct iscsi_tcp_task *tcp_task = task->dd_data; |
| 684 | struct hash_desc *rx_hash = NULL; |
| 685 | struct scsi_data_buffer *sdb = scsi_in(task->sc); |
| 686 | |
| 687 | /* |
| 688 | * Setup copy of Data-In into the Scsi_Cmnd |
| 689 | * Scatterlist case: |
| 690 | * We set up the iscsi_segment to point to the next |
| 691 | * scatterlist entry to copy to. As we go along, |
| 692 | * we move on to the next scatterlist entry and |
| 693 | * update the digest per-entry. |
| 694 | */ |
| 695 | if (conn->datadgst_en && |
| 696 | !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) |
| 697 | rx_hash = tcp_conn->rx_hash; |
| 698 | |
Mike Christie | 0ab1c25 | 2009-03-05 14:45:59 -0600 | [diff] [blame] | 699 | ISCSI_DBG_TCP(conn, "iscsi_tcp_begin_data_in( " |
| 700 | "offset=%d, datalen=%d)\n", |
| 701 | tcp_task->data_offset, |
| 702 | tcp_conn->in.datalen); |
Mike Christie | d355e57 | 2009-06-15 22:11:08 -0500 | [diff] [blame] | 703 | task->last_xfer = jiffies; |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 704 | rc = iscsi_segment_seek_sg(&tcp_conn->in.segment, |
| 705 | sdb->table.sgl, |
| 706 | sdb->table.nents, |
| 707 | tcp_task->data_offset, |
| 708 | tcp_conn->in.datalen, |
| 709 | iscsi_tcp_process_data_in, |
| 710 | rx_hash); |
| 711 | spin_unlock(&conn->session->lock); |
| 712 | return rc; |
| 713 | } |
| 714 | rc = __iscsi_complete_pdu(conn, hdr, NULL, 0); |
| 715 | spin_unlock(&conn->session->lock); |
| 716 | break; |
| 717 | case ISCSI_OP_SCSI_CMD_RSP: |
| 718 | if (tcp_conn->in.datalen) { |
| 719 | iscsi_tcp_data_recv_prep(tcp_conn); |
| 720 | return 0; |
| 721 | } |
| 722 | rc = iscsi_complete_pdu(conn, hdr, NULL, 0); |
| 723 | break; |
| 724 | case ISCSI_OP_R2T: |
| 725 | spin_lock(&conn->session->lock); |
| 726 | task = iscsi_itt_to_ctask(conn, hdr->itt); |
| 727 | if (!task) |
| 728 | rc = ISCSI_ERR_BAD_ITT; |
| 729 | else if (ahslen) |
| 730 | rc = ISCSI_ERR_AHSLEN; |
Mike Christie | d355e57 | 2009-06-15 22:11:08 -0500 | [diff] [blame] | 731 | else if (task->sc->sc_data_direction == DMA_TO_DEVICE) { |
| 732 | task->last_xfer = jiffies; |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 733 | rc = iscsi_tcp_r2t_rsp(conn, task); |
Mike Christie | d355e57 | 2009-06-15 22:11:08 -0500 | [diff] [blame] | 734 | } else |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 735 | rc = ISCSI_ERR_PROTO; |
| 736 | spin_unlock(&conn->session->lock); |
| 737 | break; |
| 738 | case ISCSI_OP_LOGIN_RSP: |
| 739 | case ISCSI_OP_TEXT_RSP: |
| 740 | case ISCSI_OP_REJECT: |
| 741 | case ISCSI_OP_ASYNC_EVENT: |
| 742 | /* |
| 743 | * It is possible that we could get a PDU with a buffer larger |
| 744 | * than 8K, but there are no targets that currently do this. |
| 745 | * For now we fail until we find a vendor that needs it |
| 746 | */ |
| 747 | if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) { |
| 748 | iscsi_conn_printk(KERN_ERR, conn, |
| 749 | "iscsi_tcp: received buffer of " |
| 750 | "len %u but conn buffer is only %u " |
| 751 | "(opcode %0x)\n", |
| 752 | tcp_conn->in.datalen, |
| 753 | ISCSI_DEF_MAX_RECV_SEG_LEN, opcode); |
| 754 | rc = ISCSI_ERR_PROTO; |
| 755 | break; |
| 756 | } |
| 757 | |
| 758 | /* If there's data coming in with the response, |
| 759 | * receive it to the connection's buffer. |
| 760 | */ |
| 761 | if (tcp_conn->in.datalen) { |
| 762 | iscsi_tcp_data_recv_prep(tcp_conn); |
| 763 | return 0; |
| 764 | } |
| 765 | /* fall through */ |
| 766 | case ISCSI_OP_LOGOUT_RSP: |
| 767 | case ISCSI_OP_NOOP_IN: |
| 768 | case ISCSI_OP_SCSI_TMFUNC_RSP: |
| 769 | rc = iscsi_complete_pdu(conn, hdr, NULL, 0); |
| 770 | break; |
| 771 | default: |
| 772 | rc = ISCSI_ERR_BAD_OPCODE; |
| 773 | break; |
| 774 | } |
| 775 | |
| 776 | if (rc == 0) { |
| 777 | /* Anything that comes with data should have |
| 778 | * been handled above. */ |
| 779 | if (tcp_conn->in.datalen) |
| 780 | return ISCSI_ERR_PROTO; |
| 781 | iscsi_tcp_hdr_recv_prep(tcp_conn); |
| 782 | } |
| 783 | |
| 784 | return rc; |
| 785 | } |
| 786 | |
| 787 | /** |
| 788 | * iscsi_tcp_hdr_recv_done - process PDU header |
| 789 | * |
| 790 | * This is the callback invoked when the PDU header has |
| 791 | * been received. If the header is followed by additional |
| 792 | * header segments, we go back for more data. |
| 793 | */ |
| 794 | static int |
| 795 | iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn, |
| 796 | struct iscsi_segment *segment) |
| 797 | { |
| 798 | struct iscsi_conn *conn = tcp_conn->iscsi_conn; |
| 799 | struct iscsi_hdr *hdr; |
| 800 | |
| 801 | /* Check if there are additional header segments |
| 802 | * *prior* to computing the digest, because we |
| 803 | * may need to go back to the caller for more. |
| 804 | */ |
| 805 | hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf; |
| 806 | if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) { |
| 807 | /* Bump the header length - the caller will |
| 808 | * just loop around and get the AHS for us, and |
| 809 | * call again. */ |
| 810 | unsigned int ahslen = hdr->hlength << 2; |
| 811 | |
| 812 | /* Make sure we don't overflow */ |
| 813 | if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf)) |
| 814 | return ISCSI_ERR_AHSLEN; |
| 815 | |
| 816 | segment->total_size += ahslen; |
| 817 | segment->size += ahslen; |
| 818 | return 0; |
| 819 | } |
| 820 | |
| 821 | /* We're done processing the header. See if we're doing |
| 822 | * header digests; if so, set up the recv_digest buffer |
| 823 | * and go back for more. */ |
Mike Christie | 6df19a7 | 2008-12-02 00:32:16 -0600 | [diff] [blame] | 824 | if (conn->hdrdgst_en && |
| 825 | !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) { |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 826 | if (segment->digest_len == 0) { |
| 827 | /* |
| 828 | * Even if we offload the digest processing we |
| 829 | * splice it in so we can increment the skb/segment |
| 830 | * counters in preparation for the data segment. |
| 831 | */ |
| 832 | iscsi_tcp_segment_splice_digest(segment, |
| 833 | segment->recv_digest); |
| 834 | return 0; |
| 835 | } |
| 836 | |
Mike Christie | 6df19a7 | 2008-12-02 00:32:16 -0600 | [diff] [blame] | 837 | iscsi_tcp_dgst_header(tcp_conn->rx_hash, hdr, |
| 838 | segment->total_copied - ISCSI_DIGEST_SIZE, |
| 839 | segment->digest); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 840 | |
Mike Christie | 6df19a7 | 2008-12-02 00:32:16 -0600 | [diff] [blame] | 841 | if (!iscsi_tcp_dgst_verify(tcp_conn, segment)) |
| 842 | return ISCSI_ERR_HDR_DGST; |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | tcp_conn->in.hdr = hdr; |
| 846 | return iscsi_tcp_hdr_dissect(conn, hdr); |
| 847 | } |
| 848 | |
| 849 | /** |
| 850 | * iscsi_tcp_recv_segment_is_hdr - tests if we are reading in a header |
| 851 | * @tcp_conn: iscsi tcp conn |
| 852 | * |
| 853 | * returns non zero if we are currently processing or setup to process |
| 854 | * a header. |
| 855 | */ |
| 856 | inline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn *tcp_conn) |
| 857 | { |
| 858 | return tcp_conn->in.segment.done == iscsi_tcp_hdr_recv_done; |
| 859 | } |
| 860 | EXPORT_SYMBOL_GPL(iscsi_tcp_recv_segment_is_hdr); |
| 861 | |
| 862 | /** |
| 863 | * iscsi_tcp_recv_skb - Process skb |
| 864 | * @conn: iscsi connection |
| 865 | * @skb: network buffer with header and/or data segment |
| 866 | * @offset: offset in skb |
| 867 | * @offload: bool indicating if transfer was offloaded |
| 868 | * |
| 869 | * Will return status of transfer in status. And will return |
| 870 | * number of bytes copied. |
| 871 | */ |
| 872 | int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb, |
| 873 | unsigned int offset, bool offloaded, int *status) |
| 874 | { |
| 875 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
| 876 | struct iscsi_segment *segment = &tcp_conn->in.segment; |
| 877 | struct skb_seq_state seq; |
| 878 | unsigned int consumed = 0; |
| 879 | int rc = 0; |
| 880 | |
Mike Christie | 0ab1c25 | 2009-03-05 14:45:59 -0600 | [diff] [blame] | 881 | ISCSI_DBG_TCP(conn, "in %d bytes\n", skb->len - offset); |
Mike Christie | d1acfae | 2009-05-13 17:57:44 -0500 | [diff] [blame] | 882 | /* |
| 883 | * Update for each skb instead of pdu, because over slow networks a |
| 884 | * data_in's data could take a while to read in. We also want to |
| 885 | * account for r2ts. |
| 886 | */ |
| 887 | conn->last_recv = jiffies; |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 888 | |
| 889 | if (unlikely(conn->suspend_rx)) { |
Mike Christie | 0ab1c25 | 2009-03-05 14:45:59 -0600 | [diff] [blame] | 890 | ISCSI_DBG_TCP(conn, "Rx suspended!\n"); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 891 | *status = ISCSI_TCP_SUSPENDED; |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | if (offloaded) { |
| 896 | segment->total_copied = segment->total_size; |
| 897 | goto segment_done; |
| 898 | } |
| 899 | |
| 900 | skb_prepare_seq_read(skb, offset, skb->len, &seq); |
| 901 | while (1) { |
| 902 | unsigned int avail; |
| 903 | const u8 *ptr; |
| 904 | |
| 905 | avail = skb_seq_read(consumed, &ptr, &seq); |
| 906 | if (avail == 0) { |
Mike Christie | 0ab1c25 | 2009-03-05 14:45:59 -0600 | [diff] [blame] | 907 | ISCSI_DBG_TCP(conn, "no more data avail. Consumed %d\n", |
| 908 | consumed); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 909 | *status = ISCSI_TCP_SKB_DONE; |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 910 | goto skb_done; |
| 911 | } |
| 912 | BUG_ON(segment->copied >= segment->size); |
| 913 | |
Mike Christie | 0ab1c25 | 2009-03-05 14:45:59 -0600 | [diff] [blame] | 914 | ISCSI_DBG_TCP(conn, "skb %p ptr=%p avail=%u\n", skb, ptr, |
| 915 | avail); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 916 | rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail); |
| 917 | BUG_ON(rc == 0); |
| 918 | consumed += rc; |
| 919 | |
| 920 | if (segment->total_copied >= segment->total_size) { |
| 921 | skb_abort_seq_read(&seq); |
| 922 | goto segment_done; |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | segment_done: |
| 927 | *status = ISCSI_TCP_SEGMENT_DONE; |
Mike Christie | 0ab1c25 | 2009-03-05 14:45:59 -0600 | [diff] [blame] | 928 | ISCSI_DBG_TCP(conn, "segment done\n"); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 929 | rc = segment->done(tcp_conn, segment); |
| 930 | if (rc != 0) { |
| 931 | *status = ISCSI_TCP_CONN_ERR; |
Mike Christie | 0ab1c25 | 2009-03-05 14:45:59 -0600 | [diff] [blame] | 932 | ISCSI_DBG_TCP(conn, "Error receiving PDU, errno=%d\n", rc); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 933 | iscsi_conn_failure(conn, rc); |
| 934 | return 0; |
| 935 | } |
| 936 | /* The done() functions sets up the next segment. */ |
| 937 | |
| 938 | skb_done: |
| 939 | conn->rxdata_octets += consumed; |
| 940 | return consumed; |
| 941 | } |
| 942 | EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb); |
| 943 | |
| 944 | /** |
| 945 | * iscsi_tcp_task_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands |
| 946 | * @conn: iscsi connection |
| 947 | * @task: scsi command task |
| 948 | * @sc: scsi command |
| 949 | */ |
| 950 | int iscsi_tcp_task_init(struct iscsi_task *task) |
| 951 | { |
| 952 | struct iscsi_tcp_task *tcp_task = task->dd_data; |
| 953 | struct iscsi_conn *conn = task->conn; |
| 954 | struct scsi_cmnd *sc = task->sc; |
| 955 | int err; |
| 956 | |
| 957 | if (!sc) { |
| 958 | /* |
| 959 | * mgmt tasks do not have a scatterlist since they come |
| 960 | * in from the iscsi interface. |
| 961 | */ |
Mike Christie | 0ab1c25 | 2009-03-05 14:45:59 -0600 | [diff] [blame] | 962 | ISCSI_DBG_TCP(conn, "mtask deq [itt 0x%x]\n", task->itt); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 963 | |
| 964 | return conn->session->tt->init_pdu(task, 0, task->data_count); |
| 965 | } |
| 966 | |
Stefani Seibold | e64c026 | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 967 | BUG_ON(kfifo_len(&tcp_task->r2tqueue)); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 968 | tcp_task->exp_datasn = 0; |
| 969 | |
| 970 | /* Prepare PDU, optionally w/ immediate data */ |
Mike Christie | 0ab1c25 | 2009-03-05 14:45:59 -0600 | [diff] [blame] | 971 | ISCSI_DBG_TCP(conn, "task deq [itt 0x%x imm %d unsol %d]\n", |
| 972 | task->itt, task->imm_count, task->unsol_r2t.data_length); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 973 | |
| 974 | err = conn->session->tt->init_pdu(task, 0, task->imm_count); |
| 975 | if (err) |
| 976 | return err; |
| 977 | task->imm_count = 0; |
| 978 | return 0; |
| 979 | } |
| 980 | EXPORT_SYMBOL_GPL(iscsi_tcp_task_init); |
| 981 | |
| 982 | static struct iscsi_r2t_info *iscsi_tcp_get_curr_r2t(struct iscsi_task *task) |
| 983 | { |
| 984 | struct iscsi_session *session = task->conn->session; |
| 985 | struct iscsi_tcp_task *tcp_task = task->dd_data; |
| 986 | struct iscsi_r2t_info *r2t = NULL; |
| 987 | |
| 988 | if (iscsi_task_has_unsol_data(task)) |
| 989 | r2t = &task->unsol_r2t; |
| 990 | else { |
| 991 | spin_lock_bh(&session->lock); |
| 992 | if (tcp_task->r2t) { |
| 993 | r2t = tcp_task->r2t; |
| 994 | /* Continue with this R2T? */ |
| 995 | if (r2t->data_length <= r2t->sent) { |
Mike Christie | 0ab1c25 | 2009-03-05 14:45:59 -0600 | [diff] [blame] | 996 | ISCSI_DBG_TCP(task->conn, |
| 997 | " done with r2t %p\n", r2t); |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 998 | kfifo_in(&tcp_task->r2tpool.queue, |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 999 | (void *)&tcp_task->r2t, |
| 1000 | sizeof(void *)); |
| 1001 | tcp_task->r2t = r2t = NULL; |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | if (r2t == NULL) { |
Stefani Seibold | 9842c38 | 2009-12-21 14:37:29 -0800 | [diff] [blame] | 1006 | if (kfifo_out(&tcp_task->r2tqueue, |
| 1007 | (void *)&tcp_task->r2t, sizeof(void *)) != |
Mike Christie | fee099b | 2010-01-12 22:48:00 -0600 | [diff] [blame] | 1008 | sizeof(void *)) |
Stefani Seibold | 9842c38 | 2009-12-21 14:37:29 -0800 | [diff] [blame] | 1009 | r2t = NULL; |
Mike Christie | fee099b | 2010-01-12 22:48:00 -0600 | [diff] [blame] | 1010 | else |
| 1011 | r2t = tcp_task->r2t; |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 1012 | } |
| 1013 | spin_unlock_bh(&session->lock); |
| 1014 | } |
| 1015 | |
| 1016 | return r2t; |
| 1017 | } |
| 1018 | |
| 1019 | /** |
| 1020 | * iscsi_tcp_task_xmit - xmit normal PDU task |
| 1021 | * @task: iscsi command task |
| 1022 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 1023 | * We're expected to return 0 when everything was transmitted successfully, |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 1024 | * -EAGAIN if there's still data in the queue, or != 0 for any other kind |
| 1025 | * of error. |
| 1026 | */ |
| 1027 | int iscsi_tcp_task_xmit(struct iscsi_task *task) |
| 1028 | { |
| 1029 | struct iscsi_conn *conn = task->conn; |
| 1030 | struct iscsi_session *session = conn->session; |
| 1031 | struct iscsi_r2t_info *r2t; |
| 1032 | int rc = 0; |
| 1033 | |
| 1034 | flush: |
| 1035 | /* Flush any pending data first. */ |
| 1036 | rc = session->tt->xmit_pdu(task); |
| 1037 | if (rc < 0) |
| 1038 | return rc; |
| 1039 | |
| 1040 | /* mgmt command */ |
| 1041 | if (!task->sc) { |
| 1042 | if (task->hdr->itt == RESERVED_ITT) |
| 1043 | iscsi_put_task(task); |
| 1044 | return 0; |
| 1045 | } |
| 1046 | |
| 1047 | /* Are we done already? */ |
| 1048 | if (task->sc->sc_data_direction != DMA_TO_DEVICE) |
| 1049 | return 0; |
| 1050 | |
| 1051 | r2t = iscsi_tcp_get_curr_r2t(task); |
| 1052 | if (r2t == NULL) { |
| 1053 | /* Waiting for more R2Ts to arrive. */ |
Mike Christie | 0ab1c25 | 2009-03-05 14:45:59 -0600 | [diff] [blame] | 1054 | ISCSI_DBG_TCP(conn, "no R2Ts yet\n"); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 1055 | return 0; |
| 1056 | } |
| 1057 | |
Mike Christie | 2ff79d5 | 2008-12-02 00:32:14 -0600 | [diff] [blame] | 1058 | rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_DATA_OUT); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 1059 | if (rc) |
| 1060 | return rc; |
| 1061 | iscsi_prep_data_out_pdu(task, r2t, (struct iscsi_data *) task->hdr); |
| 1062 | |
Mike Christie | 0ab1c25 | 2009-03-05 14:45:59 -0600 | [diff] [blame] | 1063 | ISCSI_DBG_TCP(conn, "sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n", |
| 1064 | r2t, r2t->datasn - 1, task->hdr->itt, |
| 1065 | r2t->data_offset + r2t->sent, r2t->data_count); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 1066 | |
| 1067 | rc = conn->session->tt->init_pdu(task, r2t->data_offset + r2t->sent, |
| 1068 | r2t->data_count); |
Mike Christie | 9a6510e | 2009-04-21 15:32:31 -0500 | [diff] [blame] | 1069 | if (rc) { |
| 1070 | iscsi_conn_failure(conn, ISCSI_ERR_XMIT_FAILED); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 1071 | return rc; |
Mike Christie | 9a6510e | 2009-04-21 15:32:31 -0500 | [diff] [blame] | 1072 | } |
| 1073 | |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 1074 | r2t->sent += r2t->data_count; |
| 1075 | goto flush; |
| 1076 | } |
| 1077 | EXPORT_SYMBOL_GPL(iscsi_tcp_task_xmit); |
| 1078 | |
| 1079 | struct iscsi_cls_conn * |
| 1080 | iscsi_tcp_conn_setup(struct iscsi_cls_session *cls_session, int dd_data_size, |
| 1081 | uint32_t conn_idx) |
| 1082 | |
| 1083 | { |
| 1084 | struct iscsi_conn *conn; |
| 1085 | struct iscsi_cls_conn *cls_conn; |
| 1086 | struct iscsi_tcp_conn *tcp_conn; |
| 1087 | |
Mike Christie | 74dcd0e | 2011-06-24 15:11:55 -0500 | [diff] [blame] | 1088 | cls_conn = iscsi_conn_setup(cls_session, |
| 1089 | sizeof(*tcp_conn) + dd_data_size, conn_idx); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 1090 | if (!cls_conn) |
| 1091 | return NULL; |
| 1092 | conn = cls_conn->dd_data; |
| 1093 | /* |
| 1094 | * due to strange issues with iser these are not set |
| 1095 | * in iscsi_conn_setup |
| 1096 | */ |
| 1097 | conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN; |
| 1098 | |
| 1099 | tcp_conn = conn->dd_data; |
| 1100 | tcp_conn->iscsi_conn = conn; |
Mike Christie | 74dcd0e | 2011-06-24 15:11:55 -0500 | [diff] [blame] | 1101 | tcp_conn->dd_data = conn->dd_data + sizeof(*tcp_conn); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 1102 | return cls_conn; |
| 1103 | } |
| 1104 | EXPORT_SYMBOL_GPL(iscsi_tcp_conn_setup); |
| 1105 | |
| 1106 | void iscsi_tcp_conn_teardown(struct iscsi_cls_conn *cls_conn) |
| 1107 | { |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 1108 | iscsi_conn_teardown(cls_conn); |
| 1109 | } |
| 1110 | EXPORT_SYMBOL_GPL(iscsi_tcp_conn_teardown); |
| 1111 | |
| 1112 | int iscsi_tcp_r2tpool_alloc(struct iscsi_session *session) |
| 1113 | { |
| 1114 | int i; |
| 1115 | int cmd_i; |
| 1116 | |
| 1117 | /* |
| 1118 | * initialize per-task: R2T pool and xmit queue |
| 1119 | */ |
| 1120 | for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) { |
| 1121 | struct iscsi_task *task = session->cmds[cmd_i]; |
| 1122 | struct iscsi_tcp_task *tcp_task = task->dd_data; |
| 1123 | |
| 1124 | /* |
| 1125 | * pre-allocated x2 as much r2ts to handle race when |
| 1126 | * target acks DataOut faster than we data_xmit() queues |
| 1127 | * could replenish r2tqueue. |
| 1128 | */ |
| 1129 | |
| 1130 | /* R2T pool */ |
| 1131 | if (iscsi_pool_init(&tcp_task->r2tpool, |
| 1132 | session->max_r2t * 2, NULL, |
| 1133 | sizeof(struct iscsi_r2t_info))) { |
| 1134 | goto r2t_alloc_fail; |
| 1135 | } |
| 1136 | |
| 1137 | /* R2T xmit queue */ |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 1138 | if (kfifo_alloc(&tcp_task->r2tqueue, |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 1139 | session->max_r2t * 4 * sizeof(void*), GFP_KERNEL)) { |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 1140 | iscsi_pool_free(&tcp_task->r2tpool); |
| 1141 | goto r2t_alloc_fail; |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | return 0; |
| 1146 | |
| 1147 | r2t_alloc_fail: |
| 1148 | for (i = 0; i < cmd_i; i++) { |
| 1149 | struct iscsi_task *task = session->cmds[i]; |
| 1150 | struct iscsi_tcp_task *tcp_task = task->dd_data; |
| 1151 | |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 1152 | kfifo_free(&tcp_task->r2tqueue); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 1153 | iscsi_pool_free(&tcp_task->r2tpool); |
| 1154 | } |
| 1155 | return -ENOMEM; |
| 1156 | } |
| 1157 | EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_alloc); |
| 1158 | |
| 1159 | void iscsi_tcp_r2tpool_free(struct iscsi_session *session) |
| 1160 | { |
| 1161 | int i; |
| 1162 | |
| 1163 | for (i = 0; i < session->cmds_max; i++) { |
| 1164 | struct iscsi_task *task = session->cmds[i]; |
| 1165 | struct iscsi_tcp_task *tcp_task = task->dd_data; |
| 1166 | |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 1167 | kfifo_free(&tcp_task->r2tqueue); |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 1168 | iscsi_pool_free(&tcp_task->r2tpool); |
| 1169 | } |
| 1170 | } |
| 1171 | EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_free); |
| 1172 | |
Mike Christie | 1304be5 | 2012-01-26 21:13:10 -0600 | [diff] [blame] | 1173 | int iscsi_tcp_set_max_r2t(struct iscsi_conn *conn, char *buf) |
| 1174 | { |
| 1175 | struct iscsi_session *session = conn->session; |
| 1176 | unsigned short r2ts = 0; |
| 1177 | |
| 1178 | sscanf(buf, "%hu", &r2ts); |
| 1179 | if (session->max_r2t == r2ts) |
| 1180 | return 0; |
| 1181 | |
| 1182 | if (!r2ts || !is_power_of_2(r2ts)) |
| 1183 | return -EINVAL; |
| 1184 | |
| 1185 | session->max_r2t = r2ts; |
| 1186 | iscsi_tcp_r2tpool_free(session); |
| 1187 | return iscsi_tcp_r2tpool_alloc(session); |
| 1188 | } |
| 1189 | EXPORT_SYMBOL_GPL(iscsi_tcp_set_max_r2t); |
| 1190 | |
Mike Christie | a081c13 | 2008-12-02 00:32:11 -0600 | [diff] [blame] | 1191 | void iscsi_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn, |
| 1192 | struct iscsi_stats *stats) |
| 1193 | { |
| 1194 | struct iscsi_conn *conn = cls_conn->dd_data; |
| 1195 | |
| 1196 | stats->txdata_octets = conn->txdata_octets; |
| 1197 | stats->rxdata_octets = conn->rxdata_octets; |
| 1198 | stats->scsicmd_pdus = conn->scsicmd_pdus_cnt; |
| 1199 | stats->dataout_pdus = conn->dataout_pdus_cnt; |
| 1200 | stats->scsirsp_pdus = conn->scsirsp_pdus_cnt; |
| 1201 | stats->datain_pdus = conn->datain_pdus_cnt; |
| 1202 | stats->r2t_pdus = conn->r2t_pdus_cnt; |
| 1203 | stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt; |
| 1204 | stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt; |
| 1205 | } |
| 1206 | EXPORT_SYMBOL_GPL(iscsi_tcp_conn_get_stats); |