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