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