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 /* 484 * lib iscsi will update this in the completion handling if there 485 * is status. 486 */ 487 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS)) 488 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin*)rhdr); 489 490 if (tcp_conn->in.datalen == 0) 491 return 0; 492 493 if (tcp_task->exp_datasn != datasn) { 494 ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->datasn(%d)" 495 "\n", tcp_task->exp_datasn, datasn); 496 return ISCSI_ERR_DATASN; 497 } 498 499 tcp_task->exp_datasn++; 500 501 tcp_task->data_offset = be32_to_cpu(rhdr->offset); 502 if (tcp_task->data_offset + tcp_conn->in.datalen > total_in_length) { 503 ISCSI_DBG_TCP(conn, "data_offset(%d) + data_len(%d) > " 504 "total_length_in(%d)\n", tcp_task->data_offset, 505 tcp_conn->in.datalen, total_in_length); 506 return ISCSI_ERR_DATA_OFFSET; 507 } 508 509 conn->datain_pdus_cnt++; 510 return 0; 511 } 512 513 /** 514 * iscsi_tcp_r2t_rsp - iSCSI R2T Response processing 515 * @conn: iscsi connection 516 * @hdr: PDU header 517 */ 518 static int iscsi_tcp_r2t_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr) 519 { 520 struct iscsi_session *session = conn->session; 521 struct iscsi_tcp_task *tcp_task; 522 struct iscsi_tcp_conn *tcp_conn; 523 struct iscsi_r2t_rsp *rhdr; 524 struct iscsi_r2t_info *r2t; 525 struct iscsi_task *task; 526 u32 data_length; 527 u32 data_offset; 528 int r2tsn; 529 int rc; 530 531 spin_lock(&session->back_lock); 532 task = iscsi_itt_to_ctask(conn, hdr->itt); 533 if (!task) { 534 spin_unlock(&session->back_lock); 535 return ISCSI_ERR_BAD_ITT; 536 } else if (task->sc->sc_data_direction != DMA_TO_DEVICE) { 537 spin_unlock(&session->back_lock); 538 return ISCSI_ERR_PROTO; 539 } 540 /* 541 * A bad target might complete the cmd before we have handled R2Ts 542 * so get a ref to the task that will be dropped in the xmit path. 543 */ 544 if (task->state != ISCSI_TASK_RUNNING) { 545 spin_unlock(&session->back_lock); 546 /* Let the path that got the early rsp complete it */ 547 return 0; 548 } 549 task->last_xfer = jiffies; 550 if (!iscsi_get_task(task)) { 551 spin_unlock(&session->back_lock); 552 /* Let the path that got the early rsp complete it */ 553 return 0; 554 } 555 556 tcp_conn = conn->dd_data; 557 rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr; 558 /* fill-in new R2T associated with the task */ 559 iscsi_update_cmdsn(session, (struct iscsi_nopin *)rhdr); 560 spin_unlock(&session->back_lock); 561 562 if (tcp_conn->in.datalen) { 563 iscsi_conn_printk(KERN_ERR, conn, 564 "invalid R2t with datalen %d\n", 565 tcp_conn->in.datalen); 566 rc = ISCSI_ERR_DATALEN; 567 goto put_task; 568 } 569 570 tcp_task = task->dd_data; 571 r2tsn = be32_to_cpu(rhdr->r2tsn); 572 if (tcp_task->exp_datasn != r2tsn){ 573 ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->r2tsn(%d)\n", 574 tcp_task->exp_datasn, r2tsn); 575 rc = ISCSI_ERR_R2TSN; 576 goto put_task; 577 } 578 579 if (session->state != ISCSI_STATE_LOGGED_IN) { 580 iscsi_conn_printk(KERN_INFO, conn, 581 "dropping R2T itt %d in recovery.\n", 582 task->itt); 583 rc = 0; 584 goto put_task; 585 } 586 587 data_length = be32_to_cpu(rhdr->data_length); 588 if (data_length == 0) { 589 iscsi_conn_printk(KERN_ERR, conn, 590 "invalid R2T with zero data len\n"); 591 rc = ISCSI_ERR_DATALEN; 592 goto put_task; 593 } 594 595 if (data_length > session->max_burst) 596 ISCSI_DBG_TCP(conn, "invalid R2T with data len %u and max " 597 "burst %u. Attempting to execute request.\n", 598 data_length, session->max_burst); 599 600 data_offset = be32_to_cpu(rhdr->data_offset); 601 if (data_offset + data_length > task->sc->sdb.length) { 602 iscsi_conn_printk(KERN_ERR, conn, 603 "invalid R2T with data len %u at offset %u " 604 "and total length %d\n", data_length, 605 data_offset, task->sc->sdb.length); 606 rc = ISCSI_ERR_DATALEN; 607 goto put_task; 608 } 609 610 spin_lock(&tcp_task->pool2queue); 611 rc = kfifo_out(&tcp_task->r2tpool.queue, (void *)&r2t, sizeof(void *)); 612 if (!rc) { 613 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate R2T. " 614 "Target has sent more R2Ts than it " 615 "negotiated for or driver has leaked.\n"); 616 spin_unlock(&tcp_task->pool2queue); 617 rc = ISCSI_ERR_PROTO; 618 goto put_task; 619 } 620 621 r2t->exp_statsn = rhdr->statsn; 622 r2t->data_length = data_length; 623 r2t->data_offset = data_offset; 624 625 r2t->ttt = rhdr->ttt; /* no flip */ 626 r2t->datasn = 0; 627 r2t->sent = 0; 628 629 tcp_task->exp_datasn = r2tsn + 1; 630 kfifo_in(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*)); 631 conn->r2t_pdus_cnt++; 632 spin_unlock(&tcp_task->pool2queue); 633 634 iscsi_requeue_task(task); 635 return 0; 636 637 put_task: 638 iscsi_put_task(task); 639 return rc; 640 } 641 642 /* 643 * Handle incoming reply to DataIn command 644 */ 645 static int 646 iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn, 647 struct iscsi_segment *segment) 648 { 649 struct iscsi_conn *conn = tcp_conn->iscsi_conn; 650 struct iscsi_hdr *hdr = tcp_conn->in.hdr; 651 int rc; 652 653 if (!iscsi_tcp_dgst_verify(tcp_conn, segment)) 654 return ISCSI_ERR_DATA_DGST; 655 656 /* check for non-exceptional status */ 657 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) { 658 rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0); 659 if (rc) 660 return rc; 661 } 662 663 iscsi_tcp_hdr_recv_prep(tcp_conn); 664 return 0; 665 } 666 667 /** 668 * iscsi_tcp_hdr_dissect - process PDU header 669 * @conn: iSCSI connection 670 * @hdr: PDU header 671 * 672 * This function analyzes the header of the PDU received, 673 * and performs several sanity checks. If the PDU is accompanied 674 * by data, the receive buffer is set up to copy the incoming data 675 * to the correct location. 676 */ 677 static int 678 iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr) 679 { 680 int rc = 0, opcode, ahslen; 681 struct iscsi_tcp_conn *tcp_conn = conn->dd_data; 682 struct iscsi_task *task; 683 684 /* verify PDU length */ 685 tcp_conn->in.datalen = ntoh24(hdr->dlength); 686 if (tcp_conn->in.datalen > conn->max_recv_dlength) { 687 iscsi_conn_printk(KERN_ERR, conn, 688 "iscsi_tcp: datalen %d > %d\n", 689 tcp_conn->in.datalen, conn->max_recv_dlength); 690 return ISCSI_ERR_DATALEN; 691 } 692 693 /* Additional header segments. So far, we don't 694 * process additional headers. 695 */ 696 ahslen = hdr->hlength << 2; 697 698 opcode = hdr->opcode & ISCSI_OPCODE_MASK; 699 /* verify itt (itt encoding: age+cid+itt) */ 700 rc = iscsi_verify_itt(conn, hdr->itt); 701 if (rc) 702 return rc; 703 704 ISCSI_DBG_TCP(conn, "opcode 0x%x ahslen %d datalen %d\n", 705 opcode, ahslen, tcp_conn->in.datalen); 706 707 switch(opcode) { 708 case ISCSI_OP_SCSI_DATA_IN: 709 spin_lock(&conn->session->back_lock); 710 task = iscsi_itt_to_ctask(conn, hdr->itt); 711 if (!task) 712 rc = ISCSI_ERR_BAD_ITT; 713 else 714 rc = iscsi_tcp_data_in(conn, task); 715 if (rc) { 716 spin_unlock(&conn->session->back_lock); 717 break; 718 } 719 720 if (tcp_conn->in.datalen) { 721 struct iscsi_tcp_task *tcp_task = task->dd_data; 722 u32 *rx_crcp = NULL; 723 struct scsi_data_buffer *sdb = &task->sc->sdb; 724 725 /* 726 * Setup copy of Data-In into the struct scsi_cmnd 727 * Scatterlist case: 728 * We set up the iscsi_segment to point to the next 729 * scatterlist entry to copy to. As we go along, 730 * we move on to the next scatterlist entry and 731 * update the digest per-entry. 732 */ 733 if (conn->datadgst_en && 734 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) 735 rx_crcp = tcp_conn->rx_crcp; 736 737 ISCSI_DBG_TCP(conn, "iscsi_tcp_begin_data_in( " 738 "offset=%d, datalen=%d)\n", 739 tcp_task->data_offset, 740 tcp_conn->in.datalen); 741 task->last_xfer = jiffies; 742 rc = iscsi_segment_seek_sg(&tcp_conn->in.segment, 743 sdb->table.sgl, 744 sdb->table.nents, 745 tcp_task->data_offset, 746 tcp_conn->in.datalen, 747 iscsi_tcp_process_data_in, 748 rx_crcp); 749 spin_unlock(&conn->session->back_lock); 750 return rc; 751 } 752 rc = __iscsi_complete_pdu(conn, hdr, NULL, 0); 753 spin_unlock(&conn->session->back_lock); 754 break; 755 case ISCSI_OP_R2T: 756 if (ahslen) { 757 rc = ISCSI_ERR_AHSLEN; 758 break; 759 } 760 rc = iscsi_tcp_r2t_rsp(conn, hdr); 761 break; 762 case ISCSI_OP_SCSI_CMD_RSP: 763 case ISCSI_OP_LOGIN_RSP: 764 case ISCSI_OP_TEXT_RSP: 765 case ISCSI_OP_REJECT: 766 case ISCSI_OP_ASYNC_EVENT: 767 /* 768 * It is possible that we could get a PDU with a buffer larger 769 * than 8K, but there are no targets that currently do this. 770 * For now we fail until we find a vendor that needs it 771 */ 772 if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) { 773 iscsi_conn_printk(KERN_ERR, conn, 774 "iscsi_tcp: received buffer of " 775 "len %u but conn buffer is only %u " 776 "(opcode %0x)\n", 777 tcp_conn->in.datalen, 778 ISCSI_DEF_MAX_RECV_SEG_LEN, opcode); 779 rc = ISCSI_ERR_PROTO; 780 break; 781 } 782 783 /* If there's data coming in with the response, 784 * receive it to the connection's buffer. 785 */ 786 if (tcp_conn->in.datalen) { 787 iscsi_tcp_data_recv_prep(tcp_conn); 788 return 0; 789 } 790 fallthrough; 791 case ISCSI_OP_LOGOUT_RSP: 792 case ISCSI_OP_NOOP_IN: 793 case ISCSI_OP_SCSI_TMFUNC_RSP: 794 rc = iscsi_complete_pdu(conn, hdr, NULL, 0); 795 break; 796 default: 797 rc = ISCSI_ERR_BAD_OPCODE; 798 break; 799 } 800 801 if (rc == 0) { 802 /* Anything that comes with data should have 803 * been handled above. */ 804 if (tcp_conn->in.datalen) 805 return ISCSI_ERR_PROTO; 806 iscsi_tcp_hdr_recv_prep(tcp_conn); 807 } 808 809 return rc; 810 } 811 812 /** 813 * iscsi_tcp_hdr_recv_done - process PDU header 814 * @tcp_conn: iSCSI TCP connection 815 * @segment: the buffer segment being processed 816 * 817 * This is the callback invoked when the PDU header has 818 * been received. If the header is followed by additional 819 * header segments, we go back for more data. 820 */ 821 static int 822 iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn, 823 struct iscsi_segment *segment) 824 { 825 struct iscsi_conn *conn = tcp_conn->iscsi_conn; 826 struct iscsi_hdr *hdr; 827 828 /* Check if there are additional header segments 829 * *prior* to computing the digest, because we 830 * may need to go back to the caller for more. 831 */ 832 hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf; 833 if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) { 834 /* Bump the header length - the caller will 835 * just loop around and get the AHS for us, and 836 * call again. */ 837 unsigned int ahslen = hdr->hlength << 2; 838 839 /* Make sure we don't overflow */ 840 if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf)) 841 return ISCSI_ERR_AHSLEN; 842 843 segment->total_size += ahslen; 844 segment->size += ahslen; 845 return 0; 846 } 847 848 /* We're done processing the header. See if we're doing 849 * header digests; if so, set up the recv_digest buffer 850 * and go back for more. */ 851 if (conn->hdrdgst_en && 852 !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) { 853 if (segment->digest_len == 0) { 854 /* 855 * Even if we offload the digest processing we 856 * splice it in so we can increment the skb/segment 857 * counters in preparation for the data segment. 858 */ 859 iscsi_tcp_segment_splice_digest(segment, 860 segment->recv_digest); 861 return 0; 862 } 863 864 iscsi_tcp_dgst_header(hdr, 865 segment->total_copied - ISCSI_DIGEST_SIZE, 866 segment->digest); 867 868 if (!iscsi_tcp_dgst_verify(tcp_conn, segment)) 869 return ISCSI_ERR_HDR_DGST; 870 } 871 872 tcp_conn->in.hdr = hdr; 873 return iscsi_tcp_hdr_dissect(conn, hdr); 874 } 875 876 /** 877 * iscsi_tcp_recv_segment_is_hdr - tests if we are reading in a header 878 * @tcp_conn: iscsi tcp conn 879 * 880 * returns non zero if we are currently processing or setup to process 881 * a header. 882 */ 883 inline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn *tcp_conn) 884 { 885 return tcp_conn->in.segment.done == iscsi_tcp_hdr_recv_done; 886 } 887 EXPORT_SYMBOL_GPL(iscsi_tcp_recv_segment_is_hdr); 888 889 /** 890 * iscsi_tcp_recv_skb - Process skb 891 * @conn: iscsi connection 892 * @skb: network buffer with header and/or data segment 893 * @offset: offset in skb 894 * @offloaded: bool indicating if transfer was offloaded 895 * @status: iscsi TCP status result 896 * 897 * Will return status of transfer in @status. And will return 898 * number of bytes copied. 899 */ 900 int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb, 901 unsigned int offset, bool offloaded, int *status) 902 { 903 struct iscsi_tcp_conn *tcp_conn = conn->dd_data; 904 struct iscsi_segment *segment = &tcp_conn->in.segment; 905 struct skb_seq_state seq; 906 unsigned int consumed = 0; 907 int rc = 0; 908 909 ISCSI_DBG_TCP(conn, "in %d bytes\n", skb->len - offset); 910 /* 911 * Update for each skb instead of pdu, because over slow networks a 912 * data_in's data could take a while to read in. We also want to 913 * account for r2ts. 914 */ 915 conn->last_recv = jiffies; 916 917 if (unlikely(test_bit(ISCSI_CONN_FLAG_SUSPEND_RX, &conn->flags))) { 918 ISCSI_DBG_TCP(conn, "Rx suspended!\n"); 919 *status = ISCSI_TCP_SUSPENDED; 920 return 0; 921 } 922 923 if (offloaded) { 924 segment->total_copied = segment->total_size; 925 goto segment_done; 926 } 927 928 skb_prepare_seq_read(skb, offset, skb->len, &seq); 929 while (1) { 930 unsigned int avail; 931 const u8 *ptr; 932 933 avail = skb_seq_read(consumed, &ptr, &seq); 934 if (avail == 0) { 935 ISCSI_DBG_TCP(conn, "no more data avail. Consumed %d\n", 936 consumed); 937 *status = ISCSI_TCP_SKB_DONE; 938 goto skb_done; 939 } 940 BUG_ON(segment->copied >= segment->size); 941 942 ISCSI_DBG_TCP(conn, "skb %p ptr=%p avail=%u\n", skb, ptr, 943 avail); 944 rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail); 945 BUG_ON(rc == 0); 946 consumed += rc; 947 948 if (segment->total_copied >= segment->total_size) { 949 skb_abort_seq_read(&seq); 950 goto segment_done; 951 } 952 } 953 954 segment_done: 955 *status = ISCSI_TCP_SEGMENT_DONE; 956 ISCSI_DBG_TCP(conn, "segment done\n"); 957 rc = segment->done(tcp_conn, segment); 958 if (rc != 0) { 959 *status = ISCSI_TCP_CONN_ERR; 960 ISCSI_DBG_TCP(conn, "Error receiving PDU, errno=%d\n", rc); 961 iscsi_conn_failure(conn, rc); 962 return 0; 963 } 964 /* The done() functions sets up the next segment. */ 965 966 skb_done: 967 conn->rxdata_octets += consumed; 968 return consumed; 969 } 970 EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb); 971 972 /** 973 * iscsi_tcp_task_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands 974 * @task: scsi command task 975 */ 976 int iscsi_tcp_task_init(struct iscsi_task *task) 977 { 978 struct iscsi_tcp_task *tcp_task = task->dd_data; 979 struct iscsi_conn *conn = task->conn; 980 struct scsi_cmnd *sc = task->sc; 981 int err; 982 983 if (!sc) { 984 /* 985 * mgmt tasks do not have a scatterlist since they come 986 * in from the iscsi interface. 987 */ 988 ISCSI_DBG_TCP(conn, "mtask deq [itt 0x%x]\n", task->itt); 989 990 return conn->session->tt->init_pdu(task, 0, task->data_count); 991 } 992 993 BUG_ON(kfifo_len(&tcp_task->r2tqueue)); 994 tcp_task->exp_datasn = 0; 995 996 /* Prepare PDU, optionally w/ immediate data */ 997 ISCSI_DBG_TCP(conn, "task deq [itt 0x%x imm %d unsol %d]\n", 998 task->itt, task->imm_count, task->unsol_r2t.data_length); 999 1000 err = conn->session->tt->init_pdu(task, 0, task->imm_count); 1001 if (err) 1002 return err; 1003 task->imm_count = 0; 1004 return 0; 1005 } 1006 EXPORT_SYMBOL_GPL(iscsi_tcp_task_init); 1007 1008 static struct iscsi_r2t_info *iscsi_tcp_get_curr_r2t(struct iscsi_task *task) 1009 { 1010 struct iscsi_tcp_task *tcp_task = task->dd_data; 1011 struct iscsi_r2t_info *r2t = NULL; 1012 1013 if (iscsi_task_has_unsol_data(task)) 1014 r2t = &task->unsol_r2t; 1015 else { 1016 spin_lock_bh(&tcp_task->queue2pool); 1017 if (tcp_task->r2t) { 1018 r2t = tcp_task->r2t; 1019 /* Continue with this R2T? */ 1020 if (r2t->data_length <= r2t->sent) { 1021 ISCSI_DBG_TCP(task->conn, 1022 " done with r2t %p\n", r2t); 1023 kfifo_in(&tcp_task->r2tpool.queue, 1024 (void *)&tcp_task->r2t, 1025 sizeof(void *)); 1026 tcp_task->r2t = r2t = NULL; 1027 } 1028 } 1029 1030 if (r2t == NULL) { 1031 if (kfifo_out(&tcp_task->r2tqueue, 1032 (void *)&tcp_task->r2t, sizeof(void *)) != 1033 sizeof(void *)) 1034 r2t = NULL; 1035 else 1036 r2t = tcp_task->r2t; 1037 } 1038 spin_unlock_bh(&tcp_task->queue2pool); 1039 } 1040 1041 return r2t; 1042 } 1043 1044 /** 1045 * iscsi_tcp_task_xmit - xmit normal PDU task 1046 * @task: iscsi command task 1047 * 1048 * We're expected to return 0 when everything was transmitted successfully, 1049 * -EAGAIN if there's still data in the queue, or != 0 for any other kind 1050 * of error. 1051 */ 1052 int iscsi_tcp_task_xmit(struct iscsi_task *task) 1053 { 1054 struct iscsi_conn *conn = task->conn; 1055 struct iscsi_session *session = conn->session; 1056 struct iscsi_r2t_info *r2t; 1057 int rc = 0; 1058 1059 flush: 1060 /* Flush any pending data first. */ 1061 rc = session->tt->xmit_pdu(task); 1062 if (rc < 0) 1063 return rc; 1064 1065 /* mgmt command */ 1066 if (!task->sc) { 1067 if (task->hdr->itt == RESERVED_ITT) 1068 iscsi_put_task(task); 1069 return 0; 1070 } 1071 1072 /* Are we done already? */ 1073 if (task->sc->sc_data_direction != DMA_TO_DEVICE) 1074 return 0; 1075 1076 r2t = iscsi_tcp_get_curr_r2t(task); 1077 if (r2t == NULL) { 1078 /* Waiting for more R2Ts to arrive. */ 1079 ISCSI_DBG_TCP(conn, "no R2Ts yet\n"); 1080 return 0; 1081 } 1082 1083 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_DATA_OUT); 1084 if (rc) 1085 return rc; 1086 iscsi_prep_data_out_pdu(task, r2t, (struct iscsi_data *) task->hdr); 1087 1088 ISCSI_DBG_TCP(conn, "sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n", 1089 r2t, r2t->datasn - 1, task->hdr->itt, 1090 r2t->data_offset + r2t->sent, r2t->data_count); 1091 1092 rc = conn->session->tt->init_pdu(task, r2t->data_offset + r2t->sent, 1093 r2t->data_count); 1094 if (rc) { 1095 iscsi_conn_failure(conn, ISCSI_ERR_XMIT_FAILED); 1096 return rc; 1097 } 1098 1099 r2t->sent += r2t->data_count; 1100 goto flush; 1101 } 1102 EXPORT_SYMBOL_GPL(iscsi_tcp_task_xmit); 1103 1104 struct iscsi_cls_conn * 1105 iscsi_tcp_conn_setup(struct iscsi_cls_session *cls_session, int dd_data_size, 1106 uint32_t conn_idx) 1107 1108 { 1109 struct iscsi_conn *conn; 1110 struct iscsi_cls_conn *cls_conn; 1111 struct iscsi_tcp_conn *tcp_conn; 1112 1113 cls_conn = iscsi_conn_setup(cls_session, 1114 sizeof(*tcp_conn) + dd_data_size, conn_idx); 1115 if (!cls_conn) 1116 return NULL; 1117 conn = cls_conn->dd_data; 1118 /* 1119 * due to strange issues with iser these are not set 1120 * in iscsi_conn_setup 1121 */ 1122 conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN; 1123 1124 tcp_conn = conn->dd_data; 1125 tcp_conn->iscsi_conn = conn; 1126 tcp_conn->dd_data = conn->dd_data + sizeof(*tcp_conn); 1127 return cls_conn; 1128 } 1129 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_setup); 1130 1131 void iscsi_tcp_conn_teardown(struct iscsi_cls_conn *cls_conn) 1132 { 1133 iscsi_conn_teardown(cls_conn); 1134 } 1135 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_teardown); 1136 1137 int iscsi_tcp_r2tpool_alloc(struct iscsi_session *session) 1138 { 1139 int i; 1140 int cmd_i; 1141 1142 /* 1143 * initialize per-task: R2T pool and xmit queue 1144 */ 1145 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) { 1146 struct iscsi_task *task = session->cmds[cmd_i]; 1147 struct iscsi_tcp_task *tcp_task = task->dd_data; 1148 1149 /* 1150 * pre-allocated x2 as much r2ts to handle race when 1151 * target acks DataOut faster than we data_xmit() queues 1152 * could replenish r2tqueue. 1153 */ 1154 1155 /* R2T pool */ 1156 if (iscsi_pool_init(&tcp_task->r2tpool, 1157 session->max_r2t * 2, NULL, 1158 sizeof(struct iscsi_r2t_info))) { 1159 goto r2t_alloc_fail; 1160 } 1161 1162 /* R2T xmit queue */ 1163 if (kfifo_alloc(&tcp_task->r2tqueue, 1164 session->max_r2t * 4 * sizeof(void*), GFP_KERNEL)) { 1165 iscsi_pool_free(&tcp_task->r2tpool); 1166 goto r2t_alloc_fail; 1167 } 1168 spin_lock_init(&tcp_task->pool2queue); 1169 spin_lock_init(&tcp_task->queue2pool); 1170 } 1171 1172 return 0; 1173 1174 r2t_alloc_fail: 1175 for (i = 0; i < cmd_i; i++) { 1176 struct iscsi_task *task = session->cmds[i]; 1177 struct iscsi_tcp_task *tcp_task = task->dd_data; 1178 1179 kfifo_free(&tcp_task->r2tqueue); 1180 iscsi_pool_free(&tcp_task->r2tpool); 1181 } 1182 return -ENOMEM; 1183 } 1184 EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_alloc); 1185 1186 void iscsi_tcp_r2tpool_free(struct iscsi_session *session) 1187 { 1188 int i; 1189 1190 for (i = 0; i < session->cmds_max; i++) { 1191 struct iscsi_task *task = session->cmds[i]; 1192 struct iscsi_tcp_task *tcp_task = task->dd_data; 1193 1194 kfifo_free(&tcp_task->r2tqueue); 1195 iscsi_pool_free(&tcp_task->r2tpool); 1196 } 1197 } 1198 EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_free); 1199 1200 int iscsi_tcp_set_max_r2t(struct iscsi_conn *conn, char *buf) 1201 { 1202 struct iscsi_session *session = conn->session; 1203 unsigned short r2ts = 0; 1204 1205 sscanf(buf, "%hu", &r2ts); 1206 if (session->max_r2t == r2ts) 1207 return 0; 1208 1209 if (!r2ts || !is_power_of_2(r2ts)) 1210 return -EINVAL; 1211 1212 session->max_r2t = r2ts; 1213 iscsi_tcp_r2tpool_free(session); 1214 return iscsi_tcp_r2tpool_alloc(session); 1215 } 1216 EXPORT_SYMBOL_GPL(iscsi_tcp_set_max_r2t); 1217 1218 void iscsi_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn, 1219 struct iscsi_stats *stats) 1220 { 1221 struct iscsi_conn *conn = cls_conn->dd_data; 1222 1223 stats->txdata_octets = conn->txdata_octets; 1224 stats->rxdata_octets = conn->rxdata_octets; 1225 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt; 1226 stats->dataout_pdus = conn->dataout_pdus_cnt; 1227 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt; 1228 stats->datain_pdus = conn->datain_pdus_cnt; 1229 stats->r2t_pdus = conn->r2t_pdus_cnt; 1230 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt; 1231 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt; 1232 } 1233 EXPORT_SYMBOL_GPL(iscsi_tcp_conn_get_stats); 1234