1 // SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause 2 3 /* Authors: Bernard Metzler <bmt@zurich.ibm.com> */ 4 /* Copyright (c) 2008-2019, IBM Corporation */ 5 6 #include <linux/errno.h> 7 #include <linux/types.h> 8 #include <linux/net.h> 9 #include <linux/scatterlist.h> 10 #include <linux/highmem.h> 11 #include <net/tcp.h> 12 13 #include <rdma/iw_cm.h> 14 #include <rdma/ib_verbs.h> 15 #include <rdma/ib_user_verbs.h> 16 17 #include "siw.h" 18 #include "siw_verbs.h" 19 #include "siw_mem.h" 20 21 #define MAX_HDR_INLINE \ 22 (((uint32_t)(sizeof(struct siw_rreq_pkt) - \ 23 sizeof(struct iwarp_send))) & 0xF8) 24 25 static struct page *siw_get_pblpage(struct siw_mem *mem, u64 addr, int *idx) 26 { 27 struct siw_pbl *pbl = mem->pbl; 28 u64 offset = addr - mem->va; 29 dma_addr_t paddr = siw_pbl_get_buffer(pbl, offset, NULL, idx); 30 31 if (paddr) 32 return virt_to_page(paddr); 33 34 return NULL; 35 } 36 37 /* 38 * Copy short payload at provided destination payload address 39 */ 40 static int siw_try_1seg(struct siw_iwarp_tx *c_tx, void *paddr) 41 { 42 struct siw_wqe *wqe = &c_tx->wqe_active; 43 struct siw_sge *sge = &wqe->sqe.sge[0]; 44 u32 bytes = sge->length; 45 46 if (bytes > MAX_HDR_INLINE || wqe->sqe.num_sge != 1) 47 return MAX_HDR_INLINE + 1; 48 49 if (!bytes) 50 return 0; 51 52 if (tx_flags(wqe) & SIW_WQE_INLINE) { 53 memcpy(paddr, &wqe->sqe.sge[1], bytes); 54 } else { 55 struct siw_mem *mem = wqe->mem[0]; 56 57 if (!mem->mem_obj) { 58 /* Kernel client using kva */ 59 memcpy(paddr, 60 (const void *)(uintptr_t)sge->laddr, bytes); 61 } else if (c_tx->in_syscall) { 62 if (copy_from_user(paddr, u64_to_user_ptr(sge->laddr), 63 bytes)) 64 return -EFAULT; 65 } else { 66 unsigned int off = sge->laddr & ~PAGE_MASK; 67 struct page *p; 68 char *buffer; 69 int pbl_idx = 0; 70 71 if (!mem->is_pbl) 72 p = siw_get_upage(mem->umem, sge->laddr); 73 else 74 p = siw_get_pblpage(mem, sge->laddr, &pbl_idx); 75 76 if (unlikely(!p)) 77 return -EFAULT; 78 79 buffer = kmap_atomic(p); 80 81 if (likely(PAGE_SIZE - off >= bytes)) { 82 memcpy(paddr, buffer + off, bytes); 83 kunmap_atomic(buffer); 84 } else { 85 unsigned long part = bytes - (PAGE_SIZE - off); 86 87 memcpy(paddr, buffer + off, part); 88 kunmap_atomic(buffer); 89 90 if (!mem->is_pbl) 91 p = siw_get_upage(mem->umem, 92 sge->laddr + part); 93 else 94 p = siw_get_pblpage(mem, 95 sge->laddr + part, 96 &pbl_idx); 97 if (unlikely(!p)) 98 return -EFAULT; 99 100 buffer = kmap_atomic(p); 101 memcpy(paddr + part, buffer, 102 bytes - part); 103 kunmap_atomic(buffer); 104 } 105 } 106 } 107 return (int)bytes; 108 } 109 110 #define PKT_FRAGMENTED 1 111 #define PKT_COMPLETE 0 112 113 /* 114 * siw_qp_prepare_tx() 115 * 116 * Prepare tx state for sending out one fpdu. Builds complete pkt 117 * if no user data or only immediate data are present. 118 * 119 * returns PKT_COMPLETE if complete pkt built, PKT_FRAGMENTED otherwise. 120 */ 121 static int siw_qp_prepare_tx(struct siw_iwarp_tx *c_tx) 122 { 123 struct siw_wqe *wqe = &c_tx->wqe_active; 124 char *crc = NULL; 125 int data = 0; 126 127 switch (tx_type(wqe)) { 128 case SIW_OP_READ: 129 case SIW_OP_READ_LOCAL_INV: 130 memcpy(&c_tx->pkt.ctrl, 131 &iwarp_pktinfo[RDMAP_RDMA_READ_REQ].ctrl, 132 sizeof(struct iwarp_ctrl)); 133 134 c_tx->pkt.rreq.rsvd = 0; 135 c_tx->pkt.rreq.ddp_qn = htonl(RDMAP_UNTAGGED_QN_RDMA_READ); 136 c_tx->pkt.rreq.ddp_msn = 137 htonl(++c_tx->ddp_msn[RDMAP_UNTAGGED_QN_RDMA_READ]); 138 c_tx->pkt.rreq.ddp_mo = 0; 139 c_tx->pkt.rreq.sink_stag = htonl(wqe->sqe.sge[0].lkey); 140 c_tx->pkt.rreq.sink_to = 141 cpu_to_be64(wqe->sqe.sge[0].laddr); 142 c_tx->pkt.rreq.source_stag = htonl(wqe->sqe.rkey); 143 c_tx->pkt.rreq.source_to = cpu_to_be64(wqe->sqe.raddr); 144 c_tx->pkt.rreq.read_size = htonl(wqe->sqe.sge[0].length); 145 146 c_tx->ctrl_len = sizeof(struct iwarp_rdma_rreq); 147 crc = (char *)&c_tx->pkt.rreq_pkt.crc; 148 break; 149 150 case SIW_OP_SEND: 151 if (tx_flags(wqe) & SIW_WQE_SOLICITED) 152 memcpy(&c_tx->pkt.ctrl, 153 &iwarp_pktinfo[RDMAP_SEND_SE].ctrl, 154 sizeof(struct iwarp_ctrl)); 155 else 156 memcpy(&c_tx->pkt.ctrl, &iwarp_pktinfo[RDMAP_SEND].ctrl, 157 sizeof(struct iwarp_ctrl)); 158 159 c_tx->pkt.send.ddp_qn = RDMAP_UNTAGGED_QN_SEND; 160 c_tx->pkt.send.ddp_msn = 161 htonl(++c_tx->ddp_msn[RDMAP_UNTAGGED_QN_SEND]); 162 c_tx->pkt.send.ddp_mo = 0; 163 164 c_tx->pkt.send_inv.inval_stag = 0; 165 166 c_tx->ctrl_len = sizeof(struct iwarp_send); 167 168 crc = (char *)&c_tx->pkt.send_pkt.crc; 169 data = siw_try_1seg(c_tx, crc); 170 break; 171 172 case SIW_OP_SEND_REMOTE_INV: 173 if (tx_flags(wqe) & SIW_WQE_SOLICITED) 174 memcpy(&c_tx->pkt.ctrl, 175 &iwarp_pktinfo[RDMAP_SEND_SE_INVAL].ctrl, 176 sizeof(struct iwarp_ctrl)); 177 else 178 memcpy(&c_tx->pkt.ctrl, 179 &iwarp_pktinfo[RDMAP_SEND_INVAL].ctrl, 180 sizeof(struct iwarp_ctrl)); 181 182 c_tx->pkt.send.ddp_qn = RDMAP_UNTAGGED_QN_SEND; 183 c_tx->pkt.send.ddp_msn = 184 htonl(++c_tx->ddp_msn[RDMAP_UNTAGGED_QN_SEND]); 185 c_tx->pkt.send.ddp_mo = 0; 186 187 c_tx->pkt.send_inv.inval_stag = cpu_to_be32(wqe->sqe.rkey); 188 189 c_tx->ctrl_len = sizeof(struct iwarp_send_inv); 190 191 crc = (char *)&c_tx->pkt.send_pkt.crc; 192 data = siw_try_1seg(c_tx, crc); 193 break; 194 195 case SIW_OP_WRITE: 196 memcpy(&c_tx->pkt.ctrl, &iwarp_pktinfo[RDMAP_RDMA_WRITE].ctrl, 197 sizeof(struct iwarp_ctrl)); 198 199 c_tx->pkt.rwrite.sink_stag = htonl(wqe->sqe.rkey); 200 c_tx->pkt.rwrite.sink_to = cpu_to_be64(wqe->sqe.raddr); 201 c_tx->ctrl_len = sizeof(struct iwarp_rdma_write); 202 203 crc = (char *)&c_tx->pkt.write_pkt.crc; 204 data = siw_try_1seg(c_tx, crc); 205 break; 206 207 case SIW_OP_READ_RESPONSE: 208 memcpy(&c_tx->pkt.ctrl, 209 &iwarp_pktinfo[RDMAP_RDMA_READ_RESP].ctrl, 210 sizeof(struct iwarp_ctrl)); 211 212 /* NBO */ 213 c_tx->pkt.rresp.sink_stag = cpu_to_be32(wqe->sqe.rkey); 214 c_tx->pkt.rresp.sink_to = cpu_to_be64(wqe->sqe.raddr); 215 216 c_tx->ctrl_len = sizeof(struct iwarp_rdma_rresp); 217 218 crc = (char *)&c_tx->pkt.write_pkt.crc; 219 data = siw_try_1seg(c_tx, crc); 220 break; 221 222 default: 223 siw_dbg_qp(tx_qp(c_tx), "stale wqe type %d\n", tx_type(wqe)); 224 return -EOPNOTSUPP; 225 } 226 if (unlikely(data < 0)) 227 return data; 228 229 c_tx->ctrl_sent = 0; 230 231 if (data <= MAX_HDR_INLINE) { 232 if (data) { 233 wqe->processed = data; 234 235 c_tx->pkt.ctrl.mpa_len = 236 htons(c_tx->ctrl_len + data - MPA_HDR_SIZE); 237 238 /* Add pad, if needed */ 239 data += -(int)data & 0x3; 240 /* advance CRC location after payload */ 241 crc += data; 242 c_tx->ctrl_len += data; 243 244 if (!(c_tx->pkt.ctrl.ddp_rdmap_ctrl & DDP_FLAG_TAGGED)) 245 c_tx->pkt.c_untagged.ddp_mo = 0; 246 else 247 c_tx->pkt.c_tagged.ddp_to = 248 cpu_to_be64(wqe->sqe.raddr); 249 } 250 251 *(u32 *)crc = 0; 252 /* 253 * Do complete CRC if enabled and short packet 254 */ 255 if (c_tx->mpa_crc_hd) { 256 crypto_shash_init(c_tx->mpa_crc_hd); 257 if (crypto_shash_update(c_tx->mpa_crc_hd, 258 (u8 *)&c_tx->pkt, 259 c_tx->ctrl_len)) 260 return -EINVAL; 261 crypto_shash_final(c_tx->mpa_crc_hd, (u8 *)crc); 262 } 263 c_tx->ctrl_len += MPA_CRC_SIZE; 264 265 return PKT_COMPLETE; 266 } 267 c_tx->ctrl_len += MPA_CRC_SIZE; 268 c_tx->sge_idx = 0; 269 c_tx->sge_off = 0; 270 c_tx->pbl_idx = 0; 271 272 /* 273 * Allow direct sending out of user buffer if WR is non signalled 274 * and payload is over threshold. 275 * Per RDMA verbs, the application should not change the send buffer 276 * until the work completed. In iWarp, work completion is only 277 * local delivery to TCP. TCP may reuse the buffer for 278 * retransmission. Changing unsent data also breaks the CRC, 279 * if applied. 280 */ 281 if (c_tx->zcopy_tx && wqe->bytes >= SENDPAGE_THRESH && 282 !(tx_flags(wqe) & SIW_WQE_SIGNALLED)) 283 c_tx->use_sendpage = 1; 284 else 285 c_tx->use_sendpage = 0; 286 287 return PKT_FRAGMENTED; 288 } 289 290 /* 291 * Send out one complete control type FPDU, or header of FPDU carrying 292 * data. Used for fixed sized packets like Read.Requests or zero length 293 * SENDs, WRITEs, READ.Responses, or header only. 294 */ 295 static int siw_tx_ctrl(struct siw_iwarp_tx *c_tx, struct socket *s, 296 int flags) 297 { 298 struct msghdr msg = { .msg_flags = flags }; 299 struct kvec iov = { .iov_base = 300 (char *)&c_tx->pkt.ctrl + c_tx->ctrl_sent, 301 .iov_len = c_tx->ctrl_len - c_tx->ctrl_sent }; 302 303 int rv = kernel_sendmsg(s, &msg, &iov, 1, 304 c_tx->ctrl_len - c_tx->ctrl_sent); 305 306 if (rv >= 0) { 307 c_tx->ctrl_sent += rv; 308 309 if (c_tx->ctrl_sent == c_tx->ctrl_len) 310 rv = 0; 311 else 312 rv = -EAGAIN; 313 } 314 return rv; 315 } 316 317 /* 318 * 0copy TCP transmit interface: Use do_tcp_sendpages. 319 * 320 * Using sendpage to push page by page appears to be less efficient 321 * than using sendmsg, even if data are copied. 322 * 323 * A general performance limitation might be the extra four bytes 324 * trailer checksum segment to be pushed after user data. 325 */ 326 static int siw_tcp_sendpages(struct socket *s, struct page **page, int offset, 327 size_t size) 328 { 329 struct sock *sk = s->sk; 330 int i = 0, rv = 0, sent = 0, 331 flags = MSG_MORE | MSG_DONTWAIT | MSG_SENDPAGE_NOTLAST; 332 333 while (size) { 334 size_t bytes = min_t(size_t, PAGE_SIZE - offset, size); 335 336 if (size + offset <= PAGE_SIZE) 337 flags = MSG_MORE | MSG_DONTWAIT; 338 339 tcp_rate_check_app_limited(sk); 340 try_page_again: 341 lock_sock(sk); 342 rv = do_tcp_sendpages(sk, page[i], offset, bytes, flags); 343 release_sock(sk); 344 345 if (rv > 0) { 346 size -= rv; 347 sent += rv; 348 if (rv != bytes) { 349 offset += rv; 350 bytes -= rv; 351 goto try_page_again; 352 } 353 offset = 0; 354 } else { 355 if (rv == -EAGAIN || rv == 0) 356 break; 357 return rv; 358 } 359 i++; 360 } 361 return sent; 362 } 363 364 /* 365 * siw_0copy_tx() 366 * 367 * Pushes list of pages to TCP socket. If pages from multiple 368 * SGE's, all referenced pages of each SGE are pushed in one 369 * shot. 370 */ 371 static int siw_0copy_tx(struct socket *s, struct page **page, 372 struct siw_sge *sge, unsigned int offset, 373 unsigned int size) 374 { 375 int i = 0, sent = 0, rv; 376 int sge_bytes = min(sge->length - offset, size); 377 378 offset = (sge->laddr + offset) & ~PAGE_MASK; 379 380 while (sent != size) { 381 rv = siw_tcp_sendpages(s, &page[i], offset, sge_bytes); 382 if (rv >= 0) { 383 sent += rv; 384 if (size == sent || sge_bytes > rv) 385 break; 386 387 i += PAGE_ALIGN(sge_bytes + offset) >> PAGE_SHIFT; 388 sge++; 389 sge_bytes = min(sge->length, size - sent); 390 offset = sge->laddr & ~PAGE_MASK; 391 } else { 392 sent = rv; 393 break; 394 } 395 } 396 return sent; 397 } 398 399 #define MAX_TRAILER (MPA_CRC_SIZE + 4) 400 401 static void siw_unmap_pages(struct page **pp, unsigned long kmap_mask) 402 { 403 while (kmap_mask) { 404 if (kmap_mask & BIT(0)) 405 kunmap(*pp); 406 pp++; 407 kmap_mask >>= 1; 408 } 409 } 410 411 /* 412 * siw_tx_hdt() tries to push a complete packet to TCP where all 413 * packet fragments are referenced by the elements of one iovec. 414 * For the data portion, each involved page must be referenced by 415 * one extra element. All sge's data can be non-aligned to page 416 * boundaries. Two more elements are referencing iWARP header 417 * and trailer: 418 * MAX_ARRAY = 64KB/PAGE_SIZE + 1 + (2 * (SIW_MAX_SGE - 1) + HDR + TRL 419 */ 420 #define MAX_ARRAY ((0xffff / PAGE_SIZE) + 1 + (2 * (SIW_MAX_SGE - 1) + 2)) 421 422 /* 423 * Write out iov referencing hdr, data and trailer of current FPDU. 424 * Update transmit state dependent on write return status 425 */ 426 static int siw_tx_hdt(struct siw_iwarp_tx *c_tx, struct socket *s) 427 { 428 struct siw_wqe *wqe = &c_tx->wqe_active; 429 struct siw_sge *sge = &wqe->sqe.sge[c_tx->sge_idx]; 430 struct kvec iov[MAX_ARRAY]; 431 struct page *page_array[MAX_ARRAY]; 432 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_EOR }; 433 434 int seg = 0, do_crc = c_tx->do_crc, is_kva = 0, rv; 435 unsigned int data_len = c_tx->bytes_unsent, hdr_len = 0, trl_len = 0, 436 sge_off = c_tx->sge_off, sge_idx = c_tx->sge_idx, 437 pbl_idx = c_tx->pbl_idx; 438 unsigned long kmap_mask = 0L; 439 440 if (c_tx->state == SIW_SEND_HDR) { 441 if (c_tx->use_sendpage) { 442 rv = siw_tx_ctrl(c_tx, s, MSG_DONTWAIT | MSG_MORE); 443 if (rv) 444 goto done; 445 446 c_tx->state = SIW_SEND_DATA; 447 } else { 448 iov[0].iov_base = 449 (char *)&c_tx->pkt.ctrl + c_tx->ctrl_sent; 450 iov[0].iov_len = hdr_len = 451 c_tx->ctrl_len - c_tx->ctrl_sent; 452 seg = 1; 453 } 454 } 455 456 wqe->processed += data_len; 457 458 while (data_len) { /* walk the list of SGE's */ 459 unsigned int sge_len = min(sge->length - sge_off, data_len); 460 unsigned int fp_off = (sge->laddr + sge_off) & ~PAGE_MASK; 461 struct siw_mem *mem; 462 463 if (!(tx_flags(wqe) & SIW_WQE_INLINE)) { 464 mem = wqe->mem[sge_idx]; 465 is_kva = mem->mem_obj == NULL ? 1 : 0; 466 } else { 467 is_kva = 1; 468 } 469 if (is_kva && !c_tx->use_sendpage) { 470 /* 471 * tx from kernel virtual address: either inline data 472 * or memory region with assigned kernel buffer 473 */ 474 iov[seg].iov_base = 475 (void *)(uintptr_t)(sge->laddr + sge_off); 476 iov[seg].iov_len = sge_len; 477 478 if (do_crc) 479 crypto_shash_update(c_tx->mpa_crc_hd, 480 iov[seg].iov_base, 481 sge_len); 482 sge_off += sge_len; 483 data_len -= sge_len; 484 seg++; 485 goto sge_done; 486 } 487 488 while (sge_len) { 489 size_t plen = min((int)PAGE_SIZE - fp_off, sge_len); 490 491 if (!is_kva) { 492 struct page *p; 493 494 if (mem->is_pbl) 495 p = siw_get_pblpage( 496 mem, sge->laddr + sge_off, 497 &pbl_idx); 498 else 499 p = siw_get_upage(mem->umem, 500 sge->laddr + sge_off); 501 if (unlikely(!p)) { 502 siw_unmap_pages(page_array, kmap_mask); 503 wqe->processed -= c_tx->bytes_unsent; 504 rv = -EFAULT; 505 goto done_crc; 506 } 507 page_array[seg] = p; 508 509 if (!c_tx->use_sendpage) { 510 iov[seg].iov_base = kmap(p) + fp_off; 511 iov[seg].iov_len = plen; 512 513 /* Remember for later kunmap() */ 514 kmap_mask |= BIT(seg); 515 516 if (do_crc) 517 crypto_shash_update( 518 c_tx->mpa_crc_hd, 519 iov[seg].iov_base, 520 plen); 521 } else if (do_crc) 522 crypto_shash_update( 523 c_tx->mpa_crc_hd, 524 page_address(p) + fp_off, 525 plen); 526 } else { 527 u64 va = sge->laddr + sge_off; 528 529 page_array[seg] = virt_to_page(va & PAGE_MASK); 530 if (do_crc) 531 crypto_shash_update( 532 c_tx->mpa_crc_hd, 533 (void *)(uintptr_t)va, 534 plen); 535 } 536 537 sge_len -= plen; 538 sge_off += plen; 539 data_len -= plen; 540 fp_off = 0; 541 542 if (++seg > (int)MAX_ARRAY) { 543 siw_dbg_qp(tx_qp(c_tx), "to many fragments\n"); 544 siw_unmap_pages(page_array, kmap_mask); 545 wqe->processed -= c_tx->bytes_unsent; 546 rv = -EMSGSIZE; 547 goto done_crc; 548 } 549 } 550 sge_done: 551 /* Update SGE variables at end of SGE */ 552 if (sge_off == sge->length && 553 (data_len != 0 || wqe->processed < wqe->bytes)) { 554 sge_idx++; 555 sge++; 556 sge_off = 0; 557 } 558 } 559 /* trailer */ 560 if (likely(c_tx->state != SIW_SEND_TRAILER)) { 561 iov[seg].iov_base = &c_tx->trailer.pad[4 - c_tx->pad]; 562 iov[seg].iov_len = trl_len = MAX_TRAILER - (4 - c_tx->pad); 563 } else { 564 iov[seg].iov_base = &c_tx->trailer.pad[c_tx->ctrl_sent]; 565 iov[seg].iov_len = trl_len = MAX_TRAILER - c_tx->ctrl_sent; 566 } 567 568 if (c_tx->pad) { 569 *(u32 *)c_tx->trailer.pad = 0; 570 if (do_crc) 571 crypto_shash_update(c_tx->mpa_crc_hd, 572 (u8 *)&c_tx->trailer.crc - c_tx->pad, 573 c_tx->pad); 574 } 575 if (!c_tx->mpa_crc_hd) 576 c_tx->trailer.crc = 0; 577 else if (do_crc) 578 crypto_shash_final(c_tx->mpa_crc_hd, (u8 *)&c_tx->trailer.crc); 579 580 data_len = c_tx->bytes_unsent; 581 582 if (c_tx->use_sendpage) { 583 rv = siw_0copy_tx(s, page_array, &wqe->sqe.sge[c_tx->sge_idx], 584 c_tx->sge_off, data_len); 585 if (rv == data_len) { 586 rv = kernel_sendmsg(s, &msg, &iov[seg], 1, trl_len); 587 if (rv > 0) 588 rv += data_len; 589 else 590 rv = data_len; 591 } 592 } else { 593 rv = kernel_sendmsg(s, &msg, iov, seg + 1, 594 hdr_len + data_len + trl_len); 595 siw_unmap_pages(page_array, kmap_mask); 596 } 597 if (rv < (int)hdr_len) { 598 /* Not even complete hdr pushed or negative rv */ 599 wqe->processed -= data_len; 600 if (rv >= 0) { 601 c_tx->ctrl_sent += rv; 602 rv = -EAGAIN; 603 } 604 goto done_crc; 605 } 606 rv -= hdr_len; 607 608 if (rv >= (int)data_len) { 609 /* all user data pushed to TCP or no data to push */ 610 if (data_len > 0 && wqe->processed < wqe->bytes) { 611 /* Save the current state for next tx */ 612 c_tx->sge_idx = sge_idx; 613 c_tx->sge_off = sge_off; 614 c_tx->pbl_idx = pbl_idx; 615 } 616 rv -= data_len; 617 618 if (rv == trl_len) /* all pushed */ 619 rv = 0; 620 else { 621 c_tx->state = SIW_SEND_TRAILER; 622 c_tx->ctrl_len = MAX_TRAILER; 623 c_tx->ctrl_sent = rv + 4 - c_tx->pad; 624 c_tx->bytes_unsent = 0; 625 rv = -EAGAIN; 626 } 627 628 } else if (data_len > 0) { 629 /* Maybe some user data pushed to TCP */ 630 c_tx->state = SIW_SEND_DATA; 631 wqe->processed -= data_len - rv; 632 633 if (rv) { 634 /* 635 * Some bytes out. Recompute tx state based 636 * on old state and bytes pushed 637 */ 638 unsigned int sge_unsent; 639 640 c_tx->bytes_unsent -= rv; 641 sge = &wqe->sqe.sge[c_tx->sge_idx]; 642 sge_unsent = sge->length - c_tx->sge_off; 643 644 while (sge_unsent <= rv) { 645 rv -= sge_unsent; 646 c_tx->sge_idx++; 647 c_tx->sge_off = 0; 648 sge++; 649 sge_unsent = sge->length; 650 } 651 c_tx->sge_off += rv; 652 } 653 rv = -EAGAIN; 654 } 655 done_crc: 656 c_tx->do_crc = 0; 657 done: 658 return rv; 659 } 660 661 static void siw_update_tcpseg(struct siw_iwarp_tx *c_tx, 662 struct socket *s) 663 { 664 struct tcp_sock *tp = tcp_sk(s->sk); 665 666 if (tp->gso_segs) { 667 if (c_tx->gso_seg_limit == 0) 668 c_tx->tcp_seglen = tp->mss_cache * tp->gso_segs; 669 else 670 c_tx->tcp_seglen = 671 tp->mss_cache * 672 min_t(u16, c_tx->gso_seg_limit, tp->gso_segs); 673 } else { 674 c_tx->tcp_seglen = tp->mss_cache; 675 } 676 /* Loopback may give odd numbers */ 677 c_tx->tcp_seglen &= 0xfffffff8; 678 } 679 680 /* 681 * siw_prepare_fpdu() 682 * 683 * Prepares transmit context to send out one FPDU if FPDU will contain 684 * user data and user data are not immediate data. 685 * Computes maximum FPDU length to fill up TCP MSS if possible. 686 * 687 * @qp: QP from which to transmit 688 * @wqe: Current WQE causing transmission 689 * 690 * TODO: Take into account real available sendspace on socket 691 * to avoid header misalignment due to send pausing within 692 * fpdu transmission 693 */ 694 static void siw_prepare_fpdu(struct siw_qp *qp, struct siw_wqe *wqe) 695 { 696 struct siw_iwarp_tx *c_tx = &qp->tx_ctx; 697 int data_len; 698 699 c_tx->ctrl_len = 700 iwarp_pktinfo[__rdmap_get_opcode(&c_tx->pkt.ctrl)].hdr_len; 701 c_tx->ctrl_sent = 0; 702 703 /* 704 * Update target buffer offset if any 705 */ 706 if (!(c_tx->pkt.ctrl.ddp_rdmap_ctrl & DDP_FLAG_TAGGED)) 707 /* Untagged message */ 708 c_tx->pkt.c_untagged.ddp_mo = cpu_to_be32(wqe->processed); 709 else /* Tagged message */ 710 c_tx->pkt.c_tagged.ddp_to = 711 cpu_to_be64(wqe->sqe.raddr + wqe->processed); 712 713 data_len = wqe->bytes - wqe->processed; 714 if (data_len + c_tx->ctrl_len + MPA_CRC_SIZE > c_tx->tcp_seglen) { 715 /* Trim DDP payload to fit into current TCP segment */ 716 data_len = c_tx->tcp_seglen - (c_tx->ctrl_len + MPA_CRC_SIZE); 717 c_tx->pkt.ctrl.ddp_rdmap_ctrl &= ~DDP_FLAG_LAST; 718 c_tx->pad = 0; 719 } else { 720 c_tx->pkt.ctrl.ddp_rdmap_ctrl |= DDP_FLAG_LAST; 721 c_tx->pad = -data_len & 0x3; 722 } 723 c_tx->bytes_unsent = data_len; 724 725 c_tx->pkt.ctrl.mpa_len = 726 htons(c_tx->ctrl_len + data_len - MPA_HDR_SIZE); 727 728 /* 729 * Init MPA CRC computation 730 */ 731 if (c_tx->mpa_crc_hd) { 732 crypto_shash_init(c_tx->mpa_crc_hd); 733 crypto_shash_update(c_tx->mpa_crc_hd, (u8 *)&c_tx->pkt, 734 c_tx->ctrl_len); 735 c_tx->do_crc = 1; 736 } 737 } 738 739 /* 740 * siw_check_sgl_tx() 741 * 742 * Check permissions for a list of SGE's (SGL). 743 * A successful check will have all memory referenced 744 * for transmission resolved and assigned to the WQE. 745 * 746 * @pd: Protection Domain SGL should belong to 747 * @wqe: WQE to be checked 748 * @perms: requested access permissions 749 * 750 */ 751 752 static int siw_check_sgl_tx(struct ib_pd *pd, struct siw_wqe *wqe, 753 enum ib_access_flags perms) 754 { 755 struct siw_sge *sge = &wqe->sqe.sge[0]; 756 int i, len, num_sge = wqe->sqe.num_sge; 757 758 if (unlikely(num_sge > SIW_MAX_SGE)) 759 return -EINVAL; 760 761 for (i = 0, len = 0; num_sge; num_sge--, i++, sge++) { 762 /* 763 * rdma verbs: do not check stag for a zero length sge 764 */ 765 if (sge->length) { 766 int rv = siw_check_sge(pd, sge, &wqe->mem[i], perms, 0, 767 sge->length); 768 769 if (unlikely(rv != E_ACCESS_OK)) 770 return rv; 771 } 772 len += sge->length; 773 } 774 return len; 775 } 776 777 /* 778 * siw_qp_sq_proc_tx() 779 * 780 * Process one WQE which needs transmission on the wire. 781 */ 782 static int siw_qp_sq_proc_tx(struct siw_qp *qp, struct siw_wqe *wqe) 783 { 784 struct siw_iwarp_tx *c_tx = &qp->tx_ctx; 785 struct socket *s = qp->attrs.sk; 786 int rv = 0, burst_len = qp->tx_ctx.burst; 787 enum rdmap_ecode ecode = RDMAP_ECODE_CATASTROPHIC_STREAM; 788 789 if (unlikely(wqe->wr_status == SIW_WR_IDLE)) 790 return 0; 791 792 if (!burst_len) 793 burst_len = SQ_USER_MAXBURST; 794 795 if (wqe->wr_status == SIW_WR_QUEUED) { 796 if (!(wqe->sqe.flags & SIW_WQE_INLINE)) { 797 if (tx_type(wqe) == SIW_OP_READ_RESPONSE) 798 wqe->sqe.num_sge = 1; 799 800 if (tx_type(wqe) != SIW_OP_READ && 801 tx_type(wqe) != SIW_OP_READ_LOCAL_INV) { 802 /* 803 * Reference memory to be tx'd w/o checking 804 * access for LOCAL_READ permission, since 805 * not defined in RDMA core. 806 */ 807 rv = siw_check_sgl_tx(qp->pd, wqe, 0); 808 if (rv < 0) { 809 if (tx_type(wqe) == 810 SIW_OP_READ_RESPONSE) 811 ecode = siw_rdmap_error(-rv); 812 rv = -EINVAL; 813 goto tx_error; 814 } 815 wqe->bytes = rv; 816 } else { 817 wqe->bytes = 0; 818 } 819 } else { 820 wqe->bytes = wqe->sqe.sge[0].length; 821 if (!qp->kernel_verbs) { 822 if (wqe->bytes > SIW_MAX_INLINE) { 823 rv = -EINVAL; 824 goto tx_error; 825 } 826 wqe->sqe.sge[0].laddr = 827 (u64)(uintptr_t)&wqe->sqe.sge[1]; 828 } 829 } 830 wqe->wr_status = SIW_WR_INPROGRESS; 831 wqe->processed = 0; 832 833 siw_update_tcpseg(c_tx, s); 834 835 rv = siw_qp_prepare_tx(c_tx); 836 if (rv == PKT_FRAGMENTED) { 837 c_tx->state = SIW_SEND_HDR; 838 siw_prepare_fpdu(qp, wqe); 839 } else if (rv == PKT_COMPLETE) { 840 c_tx->state = SIW_SEND_SHORT_FPDU; 841 } else { 842 goto tx_error; 843 } 844 } 845 846 next_segment: 847 siw_dbg_qp(qp, "wr type %d, state %d, data %u, sent %u, id %llx\n", 848 tx_type(wqe), wqe->wr_status, wqe->bytes, wqe->processed, 849 wqe->sqe.id); 850 851 if (--burst_len == 0) { 852 rv = -EINPROGRESS; 853 goto tx_done; 854 } 855 if (c_tx->state == SIW_SEND_SHORT_FPDU) { 856 enum siw_opcode tx_type = tx_type(wqe); 857 unsigned int msg_flags; 858 859 if (siw_sq_empty(qp) || !siw_tcp_nagle || burst_len == 1) 860 /* 861 * End current TCP segment, if SQ runs empty, 862 * or siw_tcp_nagle is not set, or we bail out 863 * soon due to no burst credit left. 864 */ 865 msg_flags = MSG_DONTWAIT; 866 else 867 msg_flags = MSG_DONTWAIT | MSG_MORE; 868 869 rv = siw_tx_ctrl(c_tx, s, msg_flags); 870 871 if (!rv && tx_type != SIW_OP_READ && 872 tx_type != SIW_OP_READ_LOCAL_INV) 873 wqe->processed = wqe->bytes; 874 875 goto tx_done; 876 877 } else { 878 rv = siw_tx_hdt(c_tx, s); 879 } 880 if (!rv) { 881 /* 882 * One segment sent. Processing completed if last 883 * segment, Do next segment otherwise. 884 */ 885 if (unlikely(c_tx->tx_suspend)) { 886 /* 887 * Verbs, 6.4.: Try stopping sending after a full 888 * DDP segment if the connection goes down 889 * (== peer halfclose) 890 */ 891 rv = -ECONNABORTED; 892 goto tx_done; 893 } 894 if (c_tx->pkt.ctrl.ddp_rdmap_ctrl & DDP_FLAG_LAST) { 895 siw_dbg_qp(qp, "WQE completed\n"); 896 goto tx_done; 897 } 898 c_tx->state = SIW_SEND_HDR; 899 900 siw_update_tcpseg(c_tx, s); 901 902 siw_prepare_fpdu(qp, wqe); 903 goto next_segment; 904 } 905 tx_done: 906 qp->tx_ctx.burst = burst_len; 907 return rv; 908 909 tx_error: 910 if (ecode != RDMAP_ECODE_CATASTROPHIC_STREAM) 911 siw_init_terminate(qp, TERM_ERROR_LAYER_RDMAP, 912 RDMAP_ETYPE_REMOTE_PROTECTION, ecode, 1); 913 else 914 siw_init_terminate(qp, TERM_ERROR_LAYER_RDMAP, 915 RDMAP_ETYPE_CATASTROPHIC, 916 RDMAP_ECODE_UNSPECIFIED, 1); 917 return rv; 918 } 919 920 static int siw_fastreg_mr(struct ib_pd *pd, struct siw_sqe *sqe) 921 { 922 struct ib_mr *base_mr = (struct ib_mr *)(uintptr_t)sqe->base_mr; 923 struct siw_device *sdev = to_siw_dev(pd->device); 924 struct siw_mem *mem = siw_mem_id2obj(sdev, sqe->rkey >> 8); 925 int rv = 0; 926 927 siw_dbg_pd(pd, "STag 0x%08x\n", sqe->rkey); 928 929 if (unlikely(!mem || !base_mr)) { 930 pr_warn("siw: fastreg: STag 0x%08x unknown\n", sqe->rkey); 931 return -EINVAL; 932 } 933 if (unlikely(base_mr->rkey >> 8 != sqe->rkey >> 8)) { 934 pr_warn("siw: fastreg: STag 0x%08x: bad MR\n", sqe->rkey); 935 rv = -EINVAL; 936 goto out; 937 } 938 if (unlikely(mem->pd != pd)) { 939 pr_warn("siw: fastreg: PD mismatch\n"); 940 rv = -EINVAL; 941 goto out; 942 } 943 if (unlikely(mem->stag_valid)) { 944 pr_warn("siw: fastreg: STag 0x%08x already valid\n", sqe->rkey); 945 rv = -EINVAL; 946 goto out; 947 } 948 /* Refresh STag since user may have changed key part */ 949 mem->stag = sqe->rkey; 950 mem->perms = sqe->access; 951 952 siw_dbg_mem(mem, "STag 0x%08x now valid\n", sqe->rkey); 953 mem->va = base_mr->iova; 954 mem->stag_valid = 1; 955 out: 956 siw_mem_put(mem); 957 return rv; 958 } 959 960 static int siw_qp_sq_proc_local(struct siw_qp *qp, struct siw_wqe *wqe) 961 { 962 int rv; 963 964 switch (tx_type(wqe)) { 965 case SIW_OP_REG_MR: 966 rv = siw_fastreg_mr(qp->pd, &wqe->sqe); 967 break; 968 969 case SIW_OP_INVAL_STAG: 970 rv = siw_invalidate_stag(qp->pd, wqe->sqe.rkey); 971 break; 972 973 default: 974 rv = -EINVAL; 975 } 976 return rv; 977 } 978 979 /* 980 * siw_qp_sq_process() 981 * 982 * Core TX path routine for RDMAP/DDP/MPA using a TCP kernel socket. 983 * Sends RDMAP payload for the current SQ WR @wqe of @qp in one or more 984 * MPA FPDUs, each containing a DDP segment. 985 * 986 * SQ processing may occur in user context as a result of posting 987 * new WQE's or from siw_sq_work_handler() context. Processing in 988 * user context is limited to non-kernel verbs users. 989 * 990 * SQ processing may get paused anytime, possibly in the middle of a WR 991 * or FPDU, if insufficient send space is available. SQ processing 992 * gets resumed from siw_sq_work_handler(), if send space becomes 993 * available again. 994 * 995 * Must be called with the QP state read-locked. 996 * 997 * Note: 998 * An outbound RREQ can be satisfied by the corresponding RRESP 999 * _before_ it gets assigned to the ORQ. This happens regularly 1000 * in RDMA READ via loopback case. Since both outbound RREQ and 1001 * inbound RRESP can be handled by the same CPU, locking the ORQ 1002 * is dead-lock prone and thus not an option. With that, the 1003 * RREQ gets assigned to the ORQ _before_ being sent - see 1004 * siw_activate_tx() - and pulled back in case of send failure. 1005 */ 1006 int siw_qp_sq_process(struct siw_qp *qp) 1007 { 1008 struct siw_wqe *wqe = tx_wqe(qp); 1009 enum siw_opcode tx_type; 1010 unsigned long flags; 1011 int rv = 0; 1012 1013 siw_dbg_qp(qp, "enter for type %d\n", tx_type(wqe)); 1014 1015 next_wqe: 1016 /* 1017 * Stop QP processing if SQ state changed 1018 */ 1019 if (unlikely(qp->tx_ctx.tx_suspend)) { 1020 siw_dbg_qp(qp, "tx suspended\n"); 1021 goto done; 1022 } 1023 tx_type = tx_type(wqe); 1024 1025 if (tx_type <= SIW_OP_READ_RESPONSE) 1026 rv = siw_qp_sq_proc_tx(qp, wqe); 1027 else 1028 rv = siw_qp_sq_proc_local(qp, wqe); 1029 1030 if (!rv) { 1031 /* 1032 * WQE processing done 1033 */ 1034 switch (tx_type) { 1035 case SIW_OP_SEND: 1036 case SIW_OP_SEND_REMOTE_INV: 1037 case SIW_OP_WRITE: 1038 siw_wqe_put_mem(wqe, tx_type); 1039 /* Fall through */ 1040 1041 case SIW_OP_INVAL_STAG: 1042 case SIW_OP_REG_MR: 1043 if (tx_flags(wqe) & SIW_WQE_SIGNALLED) 1044 siw_sqe_complete(qp, &wqe->sqe, wqe->bytes, 1045 SIW_WC_SUCCESS); 1046 break; 1047 1048 case SIW_OP_READ: 1049 case SIW_OP_READ_LOCAL_INV: 1050 /* 1051 * already enqueued to ORQ queue 1052 */ 1053 break; 1054 1055 case SIW_OP_READ_RESPONSE: 1056 siw_wqe_put_mem(wqe, tx_type); 1057 break; 1058 1059 default: 1060 WARN(1, "undefined WQE type %d\n", tx_type); 1061 rv = -EINVAL; 1062 goto done; 1063 } 1064 1065 spin_lock_irqsave(&qp->sq_lock, flags); 1066 wqe->wr_status = SIW_WR_IDLE; 1067 rv = siw_activate_tx(qp); 1068 spin_unlock_irqrestore(&qp->sq_lock, flags); 1069 1070 if (rv <= 0) 1071 goto done; 1072 1073 goto next_wqe; 1074 1075 } else if (rv == -EAGAIN) { 1076 siw_dbg_qp(qp, "sq paused: hd/tr %d of %d, data %d\n", 1077 qp->tx_ctx.ctrl_sent, qp->tx_ctx.ctrl_len, 1078 qp->tx_ctx.bytes_unsent); 1079 rv = 0; 1080 goto done; 1081 } else if (rv == -EINPROGRESS) { 1082 rv = siw_sq_start(qp); 1083 goto done; 1084 } else { 1085 /* 1086 * WQE processing failed. 1087 * Verbs 8.3.2: 1088 * o It turns any WQE into a signalled WQE. 1089 * o Local catastrophic error must be surfaced 1090 * o QP must be moved into Terminate state: done by code 1091 * doing socket state change processing 1092 * 1093 * o TODO: Termination message must be sent. 1094 * o TODO: Implement more precise work completion errors, 1095 * see enum ib_wc_status in ib_verbs.h 1096 */ 1097 siw_dbg_qp(qp, "wqe type %d processing failed: %d\n", 1098 tx_type(wqe), rv); 1099 1100 spin_lock_irqsave(&qp->sq_lock, flags); 1101 /* 1102 * RREQ may have already been completed by inbound RRESP! 1103 */ 1104 if (tx_type == SIW_OP_READ || 1105 tx_type == SIW_OP_READ_LOCAL_INV) { 1106 /* Cleanup pending entry in ORQ */ 1107 qp->orq_put--; 1108 qp->orq[qp->orq_put % qp->attrs.orq_size].flags = 0; 1109 } 1110 spin_unlock_irqrestore(&qp->sq_lock, flags); 1111 /* 1112 * immediately suspends further TX processing 1113 */ 1114 if (!qp->tx_ctx.tx_suspend) 1115 siw_qp_cm_drop(qp, 0); 1116 1117 switch (tx_type) { 1118 case SIW_OP_SEND: 1119 case SIW_OP_SEND_REMOTE_INV: 1120 case SIW_OP_SEND_WITH_IMM: 1121 case SIW_OP_WRITE: 1122 case SIW_OP_READ: 1123 case SIW_OP_READ_LOCAL_INV: 1124 siw_wqe_put_mem(wqe, tx_type); 1125 /* Fall through */ 1126 1127 case SIW_OP_INVAL_STAG: 1128 case SIW_OP_REG_MR: 1129 siw_sqe_complete(qp, &wqe->sqe, wqe->bytes, 1130 SIW_WC_LOC_QP_OP_ERR); 1131 1132 siw_qp_event(qp, IB_EVENT_QP_FATAL); 1133 1134 break; 1135 1136 case SIW_OP_READ_RESPONSE: 1137 siw_dbg_qp(qp, "proc. read.response failed: %d\n", rv); 1138 1139 siw_qp_event(qp, IB_EVENT_QP_REQ_ERR); 1140 1141 siw_wqe_put_mem(wqe, SIW_OP_READ_RESPONSE); 1142 1143 break; 1144 1145 default: 1146 WARN(1, "undefined WQE type %d\n", tx_type); 1147 rv = -EINVAL; 1148 } 1149 wqe->wr_status = SIW_WR_IDLE; 1150 } 1151 done: 1152 return rv; 1153 } 1154 1155 static void siw_sq_resume(struct siw_qp *qp) 1156 { 1157 if (down_read_trylock(&qp->state_lock)) { 1158 if (likely(qp->attrs.state == SIW_QP_STATE_RTS && 1159 !qp->tx_ctx.tx_suspend)) { 1160 int rv = siw_qp_sq_process(qp); 1161 1162 up_read(&qp->state_lock); 1163 1164 if (unlikely(rv < 0)) { 1165 siw_dbg_qp(qp, "SQ task failed: err %d\n", rv); 1166 1167 if (!qp->tx_ctx.tx_suspend) 1168 siw_qp_cm_drop(qp, 0); 1169 } 1170 } else { 1171 up_read(&qp->state_lock); 1172 } 1173 } else { 1174 siw_dbg_qp(qp, "Resume SQ while QP locked\n"); 1175 } 1176 siw_qp_put(qp); 1177 } 1178 1179 struct tx_task_t { 1180 struct llist_head active; 1181 wait_queue_head_t waiting; 1182 }; 1183 1184 static DEFINE_PER_CPU(struct tx_task_t, siw_tx_task_g); 1185 1186 void siw_stop_tx_thread(int nr_cpu) 1187 { 1188 kthread_stop(siw_tx_thread[nr_cpu]); 1189 wake_up(&per_cpu(siw_tx_task_g, nr_cpu).waiting); 1190 } 1191 1192 int siw_run_sq(void *data) 1193 { 1194 const int nr_cpu = (unsigned int)(long)data; 1195 struct llist_node *active; 1196 struct siw_qp *qp; 1197 struct tx_task_t *tx_task = &per_cpu(siw_tx_task_g, nr_cpu); 1198 1199 init_llist_head(&tx_task->active); 1200 init_waitqueue_head(&tx_task->waiting); 1201 1202 while (1) { 1203 struct llist_node *fifo_list = NULL; 1204 1205 wait_event_interruptible(tx_task->waiting, 1206 !llist_empty(&tx_task->active) || 1207 kthread_should_stop()); 1208 1209 if (kthread_should_stop()) 1210 break; 1211 1212 active = llist_del_all(&tx_task->active); 1213 /* 1214 * llist_del_all returns a list with newest entry first. 1215 * Re-order list for fairness among QP's. 1216 */ 1217 while (active) { 1218 struct llist_node *tmp = active; 1219 1220 active = llist_next(active); 1221 tmp->next = fifo_list; 1222 fifo_list = tmp; 1223 } 1224 while (fifo_list) { 1225 qp = container_of(fifo_list, struct siw_qp, tx_list); 1226 fifo_list = llist_next(fifo_list); 1227 qp->tx_list.next = NULL; 1228 1229 siw_sq_resume(qp); 1230 } 1231 } 1232 active = llist_del_all(&tx_task->active); 1233 if (active) { 1234 llist_for_each_entry(qp, active, tx_list) { 1235 qp->tx_list.next = NULL; 1236 siw_sq_resume(qp); 1237 } 1238 } 1239 return 0; 1240 } 1241 1242 int siw_sq_start(struct siw_qp *qp) 1243 { 1244 if (tx_wqe(qp)->wr_status == SIW_WR_IDLE) 1245 return 0; 1246 1247 if (unlikely(!cpu_online(qp->tx_cpu))) { 1248 siw_put_tx_cpu(qp->tx_cpu); 1249 qp->tx_cpu = siw_get_tx_cpu(qp->sdev); 1250 if (qp->tx_cpu < 0) { 1251 pr_warn("siw: no tx cpu available\n"); 1252 1253 return -EIO; 1254 } 1255 } 1256 siw_qp_get(qp); 1257 1258 llist_add(&qp->tx_list, &per_cpu(siw_tx_task_g, qp->tx_cpu).active); 1259 1260 wake_up(&per_cpu(siw_tx_task_g, qp->tx_cpu).waiting); 1261 1262 return 0; 1263 } 1264