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