1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2017, Microsoft Corporation. 4 * Copyright (c) 2025, Stefan Metzmacher 5 */ 6 7 #include "internal.h" 8 #include <linux/folio_queue.h> 9 10 struct smbdirect_map_sges { 11 struct ib_sge *sge; 12 size_t num_sge; 13 size_t max_sge; 14 struct ib_device *device; 15 u32 local_dma_lkey; 16 enum dma_data_direction direction; 17 }; 18 19 static ssize_t smbdirect_map_sges_from_iter(struct iov_iter *iter, size_t len, 20 struct smbdirect_map_sges *state); 21 22 static void smbdirect_connection_recv_io_refill_work(struct work_struct *work); 23 static void smbdirect_connection_send_immediate_work(struct work_struct *work); 24 25 static void smbdirect_connection_qp_event_handler(struct ib_event *event, void *context) 26 { 27 struct smbdirect_socket *sc = context; 28 29 smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR, 30 "%s on device %.*s socket %p (cm_id=%p) status %s first_error %1pe\n", 31 ib_event_msg(event->event), 32 IB_DEVICE_NAME_MAX, 33 event->device->name, 34 sc, sc->rdma.cm_id, 35 smbdirect_socket_status_string(sc->status), 36 SMBDIRECT_DEBUG_ERR_PTR(sc->first_error)); 37 38 switch (event->event) { 39 case IB_EVENT_CQ_ERR: 40 case IB_EVENT_QP_FATAL: 41 smbdirect_socket_schedule_cleanup(sc, -ECONNABORTED); 42 break; 43 44 default: 45 break; 46 } 47 } 48 49 static int smbdirect_connection_rdma_event_handler(struct rdma_cm_id *id, 50 struct rdma_cm_event *event) 51 { 52 struct smbdirect_socket *sc = id->context; 53 int ret = -ECONNRESET; 54 55 if (event->event == RDMA_CM_EVENT_DEVICE_REMOVAL) 56 ret = -ENETDOWN; 57 if (IS_ERR(SMBDIRECT_DEBUG_ERR_PTR(event->status))) 58 ret = event->status; 59 60 /* 61 * cma_cm_event_handler() has 62 * lockdep_assert_held(&id_priv->handler_mutex); 63 * 64 * Mutexes are not allowed in interrupts, 65 * and we rely on not being in an interrupt here. 66 */ 67 WARN_ON_ONCE(in_interrupt()); 68 69 if (event->event != sc->rdma.expected_event) { 70 smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR, 71 "%s (first_error=%1pe, expected=%s) => event=%s status=%d => ret=%1pe\n", 72 smbdirect_socket_status_string(sc->status), 73 SMBDIRECT_DEBUG_ERR_PTR(sc->first_error), 74 rdma_event_msg(sc->rdma.expected_event), 75 rdma_event_msg(event->event), 76 event->status, 77 SMBDIRECT_DEBUG_ERR_PTR(ret)); 78 79 /* 80 * If we get RDMA_CM_EVENT_DEVICE_REMOVAL, 81 * we should change to SMBDIRECT_SOCKET_DISCONNECTED, 82 * so that rdma_disconnect() is avoided later via 83 * smbdirect_socket_schedule_cleanup[_status]() => 84 * smbdirect_socket_cleanup_work(). 85 * 86 * As otherwise we'd set SMBDIRECT_SOCKET_DISCONNECTING, 87 * but never ever get RDMA_CM_EVENT_DISCONNECTED and 88 * never reach SMBDIRECT_SOCKET_DISCONNECTED. 89 */ 90 if (event->event == RDMA_CM_EVENT_DEVICE_REMOVAL) 91 smbdirect_socket_schedule_cleanup_status(sc, 92 SMBDIRECT_LOG_ERR, 93 ret, 94 SMBDIRECT_SOCKET_DISCONNECTED); 95 else 96 smbdirect_socket_schedule_cleanup(sc, ret); 97 if (sc->ib.qp) 98 ib_drain_qp(sc->ib.qp); 99 return 0; 100 } 101 102 smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_INFO, 103 "%s (first_error=%1pe) event=%s\n", 104 smbdirect_socket_status_string(sc->status), 105 SMBDIRECT_DEBUG_ERR_PTR(sc->first_error), 106 rdma_event_msg(event->event)); 107 108 switch (event->event) { 109 case RDMA_CM_EVENT_DISCONNECTED: 110 /* 111 * We need to change to SMBDIRECT_SOCKET_DISCONNECTED, 112 * so that rdma_disconnect() is avoided later via 113 * smbdirect_socket_schedule_cleanup_status() => 114 * smbdirect_socket_cleanup_work(). 115 * 116 * As otherwise we'd set SMBDIRECT_SOCKET_DISCONNECTING, 117 * but never ever get RDMA_CM_EVENT_DISCONNECTED and 118 * never reach SMBDIRECT_SOCKET_DISCONNECTED. 119 * 120 * This is also a normal disconnect so 121 * SMBDIRECT_LOG_INFO should be good enough 122 * and avoids spamming the default logs. 123 */ 124 smbdirect_socket_schedule_cleanup_status(sc, 125 SMBDIRECT_LOG_INFO, 126 ret, 127 SMBDIRECT_SOCKET_DISCONNECTED); 128 if (sc->ib.qp) 129 ib_drain_qp(sc->ib.qp); 130 return 0; 131 132 default: 133 break; 134 } 135 136 /* 137 * This is an internal error, should be handled above via 138 * event->event != sc->rdma.expected_event already. 139 */ 140 WARN_ON_ONCE(sc->rdma.expected_event != RDMA_CM_EVENT_DISCONNECTED); 141 smbdirect_socket_schedule_cleanup(sc, -ECONNABORTED); 142 return 0; 143 } 144 145 void smbdirect_connection_rdma_established(struct smbdirect_socket *sc) 146 { 147 smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_INFO, 148 "rdma established: device: %.*s local: %pISpsfc remote: %pISpsfc\n", 149 IB_DEVICE_NAME_MAX, 150 sc->ib.dev->name, 151 &sc->rdma.cm_id->route.addr.src_addr, 152 &sc->rdma.cm_id->route.addr.dst_addr); 153 154 sc->rdma.cm_id->event_handler = smbdirect_connection_rdma_event_handler; 155 sc->rdma.expected_event = RDMA_CM_EVENT_DISCONNECTED; 156 } 157 158 void smbdirect_connection_negotiation_done(struct smbdirect_socket *sc) 159 { 160 if (unlikely(sc->first_error)) 161 return; 162 163 if (sc->status == SMBDIRECT_SOCKET_CONNECTED) 164 /* 165 * This is the accept case where 166 * smbdirect_socket_accept() already sets 167 * SMBDIRECT_SOCKET_CONNECTED 168 */ 169 goto done; 170 171 if (sc->status != SMBDIRECT_SOCKET_NEGOTIATE_RUNNING) { 172 /* 173 * Something went wrong... 174 */ 175 smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR, 176 "status=%s first_error=%1pe local: %pISpsfc remote: %pISpsfc\n", 177 smbdirect_socket_status_string(sc->status), 178 SMBDIRECT_DEBUG_ERR_PTR(sc->first_error), 179 &sc->rdma.cm_id->route.addr.src_addr, 180 &sc->rdma.cm_id->route.addr.dst_addr); 181 return; 182 } 183 184 /* 185 * We are done, so we can wake up the waiter. 186 */ 187 WARN_ONCE(sc->status == SMBDIRECT_SOCKET_CONNECTED, 188 "status=%s first_error=%1pe", 189 smbdirect_socket_status_string(sc->status), 190 SMBDIRECT_DEBUG_ERR_PTR(sc->first_error)); 191 sc->status = SMBDIRECT_SOCKET_CONNECTED; 192 193 /* 194 * We need to setup the refill and send immediate work 195 * in order to get a working connection. 196 */ 197 done: 198 INIT_WORK(&sc->recv_io.posted.refill_work, smbdirect_connection_recv_io_refill_work); 199 INIT_WORK(&sc->idle.immediate_work, smbdirect_connection_send_immediate_work); 200 201 smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_INFO, 202 "negotiated: local: %pISpsfc remote: %pISpsfc\n", 203 &sc->rdma.cm_id->route.addr.src_addr, 204 &sc->rdma.cm_id->route.addr.dst_addr); 205 206 wake_up(&sc->status_wait); 207 } 208 209 static u32 smbdirect_rdma_rw_send_wrs(struct ib_device *dev, 210 const struct ib_qp_init_attr *attr) 211 { 212 /* 213 * This could be split out of rdma_rw_init_qp() 214 * and be a helper function next to rdma_rw_mr_factor() 215 * 216 * We can't check unlikely(rdma_rw_force_mr) here, 217 * but that is most likely 0 anyway. 218 */ 219 u32 factor; 220 221 WARN_ON_ONCE(attr->port_num == 0); 222 223 /* 224 * Each context needs at least one RDMA READ or WRITE WR. 225 * 226 * For some hardware we might need more, eventually we should ask the 227 * HCA driver for a multiplier here. 228 */ 229 factor = 1; 230 231 /* 232 * If the device needs MRs to perform RDMA READ or WRITE operations, 233 * we'll need two additional MRs for the registrations and the 234 * invalidation. 235 */ 236 if (rdma_protocol_iwarp(dev, attr->port_num) || dev->attrs.max_sgl_rd) 237 factor += 2; /* inv + reg */ 238 239 return factor * attr->cap.max_rdma_ctxs; 240 } 241 242 int smbdirect_connection_create_qp(struct smbdirect_socket *sc) 243 { 244 const struct smbdirect_socket_parameters *sp = &sc->parameters; 245 struct ib_qp_init_attr qp_attr; 246 struct ib_qp_cap qp_cap; 247 u32 rdma_send_wr; 248 u32 max_send_wr; 249 int ret; 250 251 /* 252 * Note that {rdma,ib}_create_qp() will call 253 * rdma_rw_init_qp() if max_rdma_ctxs is not 0. 254 * It will adjust max_send_wr to the required 255 * number of additional WRs for the RDMA RW operations. 256 * It will cap max_send_wr to the device limit. 257 * 258 * We use allocate sp->responder_resources * 2 MRs 259 * and each MR needs WRs for REG and INV, so 260 * we use '* 4'. 261 * 262 * +1 for ib_drain_qp() 263 */ 264 memset(&qp_cap, 0, sizeof(qp_cap)); 265 qp_cap.max_send_wr = sp->send_credit_target + sp->responder_resources * 4 + 1; 266 qp_cap.max_recv_wr = sp->recv_credit_max + 1; 267 qp_cap.max_send_sge = SMBDIRECT_SEND_IO_MAX_SGE; 268 qp_cap.max_recv_sge = SMBDIRECT_RECV_IO_MAX_SGE; 269 qp_cap.max_inline_data = 0; 270 qp_cap.max_rdma_ctxs = sc->rw_io.credits.max; 271 272 /* 273 * Find out the number of max_send_wr 274 * after rdma_rw_init_qp() adjusted it. 275 * 276 * We only do it on a temporary variable, 277 * as rdma_create_qp() will trigger 278 * rdma_rw_init_qp() again. 279 */ 280 memset(&qp_attr, 0, sizeof(qp_attr)); 281 qp_attr.cap = qp_cap; 282 qp_attr.port_num = sc->rdma.cm_id->port_num; 283 rdma_send_wr = smbdirect_rdma_rw_send_wrs(sc->ib.dev, &qp_attr); 284 max_send_wr = qp_cap.max_send_wr + rdma_send_wr; 285 286 if (qp_cap.max_send_wr > sc->ib.dev->attrs.max_cqe || 287 qp_cap.max_send_wr > sc->ib.dev->attrs.max_qp_wr) { 288 pr_err("Possible CQE overrun: max_send_wr %d\n", 289 qp_cap.max_send_wr); 290 pr_err("device %.*s reporting max_cqe %u max_qp_wr %u\n", 291 IB_DEVICE_NAME_MAX, 292 sc->ib.dev->name, 293 sc->ib.dev->attrs.max_cqe, 294 sc->ib.dev->attrs.max_qp_wr); 295 pr_err("consider lowering send_credit_target = %d\n", 296 sp->send_credit_target); 297 return -EINVAL; 298 } 299 300 if (qp_cap.max_rdma_ctxs && 301 (max_send_wr >= sc->ib.dev->attrs.max_cqe || 302 max_send_wr >= sc->ib.dev->attrs.max_qp_wr)) { 303 pr_err("Possible CQE overrun: rdma_send_wr %d + max_send_wr %d = %d\n", 304 rdma_send_wr, qp_cap.max_send_wr, max_send_wr); 305 pr_err("device %.*s reporting max_cqe %u max_qp_wr %u\n", 306 IB_DEVICE_NAME_MAX, 307 sc->ib.dev->name, 308 sc->ib.dev->attrs.max_cqe, 309 sc->ib.dev->attrs.max_qp_wr); 310 pr_err("consider lowering send_credit_target = %d, max_rdma_ctxs = %d\n", 311 sp->send_credit_target, qp_cap.max_rdma_ctxs); 312 return -EINVAL; 313 } 314 315 if (qp_cap.max_recv_wr > sc->ib.dev->attrs.max_cqe || 316 qp_cap.max_recv_wr > sc->ib.dev->attrs.max_qp_wr) { 317 pr_err("Possible CQE overrun: max_recv_wr %d\n", 318 qp_cap.max_recv_wr); 319 pr_err("device %.*s reporting max_cqe %u max_qp_wr %u\n", 320 IB_DEVICE_NAME_MAX, 321 sc->ib.dev->name, 322 sc->ib.dev->attrs.max_cqe, 323 sc->ib.dev->attrs.max_qp_wr); 324 pr_err("consider lowering receive_credit_max = %d\n", 325 sp->recv_credit_max); 326 return -EINVAL; 327 } 328 329 if (qp_cap.max_send_sge > sc->ib.dev->attrs.max_send_sge || 330 qp_cap.max_recv_sge > sc->ib.dev->attrs.max_recv_sge) { 331 pr_err("device %.*s max_send_sge/max_recv_sge = %u/%u too small\n", 332 IB_DEVICE_NAME_MAX, 333 sc->ib.dev->name, 334 sc->ib.dev->attrs.max_send_sge, 335 sc->ib.dev->attrs.max_recv_sge); 336 return -EINVAL; 337 } 338 339 sc->ib.pd = ib_alloc_pd(sc->ib.dev, 0); 340 if (IS_ERR(sc->ib.pd)) { 341 pr_err("Can't create RDMA PD: %1pe\n", sc->ib.pd); 342 ret = PTR_ERR(sc->ib.pd); 343 sc->ib.pd = NULL; 344 return ret; 345 } 346 347 sc->ib.send_cq = ib_alloc_cq_any(sc->ib.dev, sc, 348 max_send_wr, 349 sc->ib.poll_ctx); 350 if (IS_ERR(sc->ib.send_cq)) { 351 pr_err("Can't create RDMA send CQ: %1pe\n", sc->ib.send_cq); 352 ret = PTR_ERR(sc->ib.send_cq); 353 sc->ib.send_cq = NULL; 354 goto err; 355 } 356 357 sc->ib.recv_cq = ib_alloc_cq_any(sc->ib.dev, sc, 358 qp_cap.max_recv_wr, 359 sc->ib.poll_ctx); 360 if (IS_ERR(sc->ib.recv_cq)) { 361 pr_err("Can't create RDMA recv CQ: %1pe\n", sc->ib.recv_cq); 362 ret = PTR_ERR(sc->ib.recv_cq); 363 sc->ib.recv_cq = NULL; 364 goto err; 365 } 366 367 /* 368 * We reset completely here! 369 * As the above use was just temporary 370 * to calc max_send_wr and rdma_send_wr. 371 * 372 * rdma_create_qp() will trigger rdma_rw_init_qp() 373 * again if max_rdma_ctxs is not 0. 374 */ 375 memset(&qp_attr, 0, sizeof(qp_attr)); 376 qp_attr.event_handler = smbdirect_connection_qp_event_handler; 377 qp_attr.qp_context = sc; 378 qp_attr.cap = qp_cap; 379 qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR; 380 qp_attr.qp_type = IB_QPT_RC; 381 qp_attr.send_cq = sc->ib.send_cq; 382 qp_attr.recv_cq = sc->ib.recv_cq; 383 qp_attr.port_num = ~0; 384 385 ret = rdma_create_qp(sc->rdma.cm_id, sc->ib.pd, &qp_attr); 386 if (ret) { 387 pr_err("Can't create RDMA QP: %1pe\n", 388 SMBDIRECT_DEBUG_ERR_PTR(ret)); 389 goto err; 390 } 391 sc->ib.qp = sc->rdma.cm_id->qp; 392 393 return 0; 394 err: 395 smbdirect_connection_destroy_qp(sc); 396 return ret; 397 } 398 399 void smbdirect_connection_destroy_qp(struct smbdirect_socket *sc) 400 { 401 if (sc->ib.qp) { 402 ib_drain_qp(sc->ib.qp); 403 sc->ib.qp = NULL; 404 rdma_destroy_qp(sc->rdma.cm_id); 405 } 406 /* 407 * These CQs were created with ib_alloc_cq_any(), which arms an internal 408 * completion handler (ib_cq_poll_work for IB_POLL_WORKQUEUE). They MUST be 409 * torn down with ib_free_cq(), which cancel_work_sync()es that poll work 410 * before freeing the CQ. ib_destroy_cq() skips that step, so a completion 411 * posted late by the (software) provider — e.g. rxe posting an RNR error 412 * from rxe_receiver after rdma_destroy_qp() — re-queues ib_cq_poll_work on 413 * an already-freed CQ (KASAN slab-use-after-free in ib_cq_poll_work). 414 */ 415 if (sc->ib.recv_cq) { 416 ib_free_cq(sc->ib.recv_cq); 417 sc->ib.recv_cq = NULL; 418 } 419 if (sc->ib.send_cq) { 420 ib_free_cq(sc->ib.send_cq); 421 sc->ib.send_cq = NULL; 422 } 423 if (sc->ib.pd) { 424 ib_dealloc_pd(sc->ib.pd); 425 sc->ib.pd = NULL; 426 } 427 } 428 429 int smbdirect_connection_create_mem_pools(struct smbdirect_socket *sc) 430 { 431 const struct smbdirect_socket_parameters *sp = &sc->parameters; 432 char name[80]; 433 size_t i; 434 435 /* 436 * We use sizeof(struct smbdirect_negotiate_resp) for the 437 * payload size as it is larger as 438 * sizeof(struct smbdirect_data_transfer). 439 * 440 * This will fit client and server usage for now. 441 */ 442 snprintf(name, sizeof(name), "smbdirect_send_io_cache_%p", sc); 443 struct kmem_cache_args send_io_args = { 444 .align = __alignof__(struct smbdirect_send_io), 445 }; 446 sc->send_io.mem.cache = kmem_cache_create(name, 447 sizeof(struct smbdirect_send_io) + 448 sizeof(struct smbdirect_negotiate_resp), 449 &send_io_args, 450 SLAB_HWCACHE_ALIGN); 451 if (!sc->send_io.mem.cache) 452 goto err; 453 454 sc->send_io.mem.pool = mempool_create_slab_pool(sp->send_credit_target, 455 sc->send_io.mem.cache); 456 if (!sc->send_io.mem.pool) 457 goto err; 458 459 /* 460 * A payload size of sp->max_recv_size should fit 461 * any message. 462 * 463 * For smbdirect_data_transfer messages the whole 464 * buffer might be exposed to userspace 465 * (currently on the client side...) 466 * The documentation says data_offset = 0 would be 467 * strange but valid. 468 */ 469 snprintf(name, sizeof(name), "smbdirect_recv_io_cache_%p", sc); 470 struct kmem_cache_args recv_io_args = { 471 .align = __alignof__(struct smbdirect_recv_io), 472 .useroffset = sizeof(struct smbdirect_recv_io), 473 .usersize = sp->max_recv_size, 474 }; 475 sc->recv_io.mem.cache = kmem_cache_create(name, 476 sizeof(struct smbdirect_recv_io) + 477 sp->max_recv_size, 478 &recv_io_args, 479 SLAB_HWCACHE_ALIGN); 480 if (!sc->recv_io.mem.cache) 481 goto err; 482 483 sc->recv_io.mem.pool = mempool_create_slab_pool(sp->recv_credit_max, 484 sc->recv_io.mem.cache); 485 if (!sc->recv_io.mem.pool) 486 goto err; 487 488 for (i = 0; i < sp->recv_credit_max; i++) { 489 struct smbdirect_recv_io *recv_io; 490 491 recv_io = mempool_alloc(sc->recv_io.mem.pool, 492 sc->recv_io.mem.gfp_mask); 493 if (!recv_io) 494 goto err; 495 recv_io->socket = sc; 496 recv_io->sge.length = 0; 497 list_add_tail(&recv_io->list, &sc->recv_io.free.list); 498 } 499 500 return 0; 501 err: 502 smbdirect_connection_destroy_mem_pools(sc); 503 return -ENOMEM; 504 } 505 506 void smbdirect_connection_destroy_mem_pools(struct smbdirect_socket *sc) 507 { 508 struct smbdirect_recv_io *recv_io, *next_io; 509 510 list_for_each_entry_safe(recv_io, next_io, &sc->recv_io.free.list, list) { 511 list_del(&recv_io->list); 512 mempool_free(recv_io, sc->recv_io.mem.pool); 513 } 514 515 /* 516 * Note mempool_destroy() and kmem_cache_destroy() 517 * work fine with a NULL pointer 518 */ 519 520 mempool_destroy(sc->recv_io.mem.pool); 521 sc->recv_io.mem.pool = NULL; 522 523 kmem_cache_destroy(sc->recv_io.mem.cache); 524 sc->recv_io.mem.cache = NULL; 525 526 mempool_destroy(sc->send_io.mem.pool); 527 sc->send_io.mem.pool = NULL; 528 529 kmem_cache_destroy(sc->send_io.mem.cache); 530 sc->send_io.mem.cache = NULL; 531 } 532 533 struct smbdirect_send_io *smbdirect_connection_alloc_send_io(struct smbdirect_socket *sc) 534 { 535 struct smbdirect_send_io *msg; 536 537 msg = mempool_alloc(sc->send_io.mem.pool, sc->send_io.mem.gfp_mask); 538 if (!msg) 539 return ERR_PTR(-ENOMEM); 540 msg->socket = sc; 541 INIT_LIST_HEAD(&msg->sibling_list); 542 msg->num_sge = 0; 543 544 return msg; 545 } 546 547 void smbdirect_connection_free_send_io(struct smbdirect_send_io *msg) 548 { 549 struct smbdirect_socket *sc = msg->socket; 550 size_t i; 551 552 /* 553 * The list needs to be empty! 554 * The caller should take care of it. 555 */ 556 WARN_ON_ONCE(!list_empty(&msg->sibling_list)); 557 558 /* 559 * Note we call ib_dma_unmap_page(), even if some sges are mapped using 560 * ib_dma_map_single(). 561 * 562 * The difference between _single() and _page() only matters for the 563 * ib_dma_map_*() case. 564 * 565 * For the ib_dma_unmap_*() case it does not matter as both take the 566 * dma_addr_t and dma_unmap_single_attrs() is just an alias to 567 * dma_unmap_page_attrs(). 568 */ 569 for (i = 0; i < msg->num_sge; i++) 570 ib_dma_unmap_page(sc->ib.dev, 571 msg->sge[i].addr, 572 msg->sge[i].length, 573 DMA_TO_DEVICE); 574 575 mempool_free(msg, sc->send_io.mem.pool); 576 } 577 578 struct smbdirect_recv_io *smbdirect_connection_get_recv_io(struct smbdirect_socket *sc) 579 { 580 struct smbdirect_recv_io *msg = NULL; 581 unsigned long flags; 582 583 spin_lock_irqsave(&sc->recv_io.free.lock, flags); 584 if (likely(!sc->first_error)) 585 msg = list_first_entry_or_null(&sc->recv_io.free.list, 586 struct smbdirect_recv_io, 587 list); 588 if (likely(msg)) { 589 list_del(&msg->list); 590 sc->statistics.get_receive_buffer++; 591 } 592 spin_unlock_irqrestore(&sc->recv_io.free.lock, flags); 593 594 return msg; 595 } 596 597 void smbdirect_connection_put_recv_io(struct smbdirect_recv_io *msg) 598 { 599 struct smbdirect_socket *sc = msg->socket; 600 unsigned long flags; 601 602 if (likely(msg->sge.length != 0)) { 603 ib_dma_unmap_single(sc->ib.dev, 604 msg->sge.addr, 605 msg->sge.length, 606 DMA_FROM_DEVICE); 607 msg->sge.length = 0; 608 } 609 610 spin_lock_irqsave(&sc->recv_io.free.lock, flags); 611 list_add_tail(&msg->list, &sc->recv_io.free.list); 612 sc->statistics.put_receive_buffer++; 613 spin_unlock_irqrestore(&sc->recv_io.free.lock, flags); 614 615 queue_work(sc->workqueues.refill, &sc->recv_io.posted.refill_work); 616 } 617 618 void smbdirect_connection_reassembly_append_recv_io(struct smbdirect_socket *sc, 619 struct smbdirect_recv_io *msg, 620 u32 data_length) 621 { 622 unsigned long flags; 623 624 spin_lock_irqsave(&sc->recv_io.reassembly.lock, flags); 625 list_add_tail(&msg->list, &sc->recv_io.reassembly.list); 626 sc->recv_io.reassembly.queue_length++; 627 /* 628 * Make sure reassembly_data_length is updated after list and 629 * reassembly_queue_length are updated. On the dequeue side 630 * reassembly_data_length is checked without a lock to determine 631 * if reassembly_queue_length and list is up to date 632 */ 633 virt_wmb(); 634 sc->recv_io.reassembly.data_length += data_length; 635 spin_unlock_irqrestore(&sc->recv_io.reassembly.lock, flags); 636 sc->statistics.enqueue_reassembly_queue++; 637 } 638 639 struct smbdirect_recv_io * 640 smbdirect_connection_reassembly_first_recv_io(struct smbdirect_socket *sc) 641 { 642 struct smbdirect_recv_io *msg; 643 644 msg = list_first_entry_or_null(&sc->recv_io.reassembly.list, 645 struct smbdirect_recv_io, 646 list); 647 648 return msg; 649 } 650 651 void smbdirect_connection_negotiate_rdma_resources(struct smbdirect_socket *sc, 652 u8 peer_initiator_depth, 653 u8 peer_responder_resources, 654 const struct rdma_conn_param *param) 655 { 656 struct smbdirect_socket_parameters *sp = &sc->parameters; 657 658 if (rdma_protocol_iwarp(sc->ib.dev, sc->rdma.cm_id->port_num) && 659 param->private_data_len == 8) { 660 /* 661 * Legacy clients with only iWarp MPA v1 support 662 * need a private blob in order to negotiate 663 * the IRD/ORD values. 664 */ 665 const __be32 *ird_ord_hdr = param->private_data; 666 u32 ird32 = be32_to_cpu(ird_ord_hdr[0]); 667 u32 ord32 = be32_to_cpu(ird_ord_hdr[1]); 668 669 /* 670 * cifs.ko sends the legacy IRD/ORD negotiation 671 * event if iWarp MPA v2 was used. 672 * 673 * Here we check that the values match and only 674 * mark the client as legacy if they don't match. 675 */ 676 if ((u32)param->initiator_depth != ird32 || 677 (u32)param->responder_resources != ord32) { 678 /* 679 * There are broken clients (old cifs.ko) 680 * using little endian and also 681 * struct rdma_conn_param only uses u8 682 * for initiator_depth and responder_resources, 683 * so we truncate the value to U8_MAX. 684 * 685 * smb_direct_accept_client() will then 686 * do the real negotiation in order to 687 * select the minimum between client and 688 * server. 689 */ 690 ird32 = min_t(u32, ird32, U8_MAX); 691 ord32 = min_t(u32, ord32, U8_MAX); 692 693 sc->rdma.legacy_iwarp = true; 694 peer_initiator_depth = (u8)ird32; 695 peer_responder_resources = (u8)ord32; 696 } 697 } 698 699 /* 700 * negotiate the value by using the minimum 701 * between client and server if the client provided 702 * non 0 values. 703 */ 704 if (peer_initiator_depth != 0) 705 sp->initiator_depth = min_t(u8, sp->initiator_depth, 706 peer_initiator_depth); 707 if (peer_responder_resources != 0) 708 sp->responder_resources = min_t(u8, sp->responder_resources, 709 peer_responder_resources); 710 } 711 712 bool smbdirect_connection_is_connected(struct smbdirect_socket *sc) 713 { 714 if (unlikely(!sc || sc->first_error || sc->status != SMBDIRECT_SOCKET_CONNECTED)) 715 return false; 716 return true; 717 } 718 EXPORT_SYMBOL_GPL(smbdirect_connection_is_connected); 719 720 int smbdirect_connection_wait_for_connected(struct smbdirect_socket *sc) 721 { 722 const struct smbdirect_socket_parameters *sp = &sc->parameters; 723 union { 724 struct sockaddr sa; 725 struct sockaddr_storage ss; 726 } src_addr, dst_addr; 727 const struct sockaddr *src = NULL; 728 const struct sockaddr *dst = NULL; 729 char _devname[IB_DEVICE_NAME_MAX] = { 0, }; 730 const char *devname = NULL; 731 int ret; 732 733 if (sc->rdma.cm_id) { 734 src_addr.ss = sc->rdma.cm_id->route.addr.src_addr; 735 if (src_addr.sa.sa_family != AF_UNSPEC) 736 src = &src_addr.sa; 737 dst_addr.ss = sc->rdma.cm_id->route.addr.dst_addr; 738 if (dst_addr.sa.sa_family != AF_UNSPEC) 739 dst = &dst_addr.sa; 740 741 if (sc->ib.dev) { 742 memcpy(_devname, sc->ib.dev->name, IB_DEVICE_NAME_MAX); 743 devname = _devname; 744 } 745 } 746 747 smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_INFO, 748 "waiting for connection: device: %.*s local: %pISpsfc remote: %pISpsfc\n", 749 IB_DEVICE_NAME_MAX, devname, src, dst); 750 751 ret = wait_event_interruptible_timeout(sc->status_wait, 752 sc->status == SMBDIRECT_SOCKET_CONNECTED || 753 sc->first_error, 754 msecs_to_jiffies(sp->negotiate_timeout_msec)); 755 if (sc->rdma.cm_id) { 756 /* 757 * Maybe src and dev are updated in the meantime. 758 */ 759 src_addr.ss = sc->rdma.cm_id->route.addr.src_addr; 760 if (src_addr.sa.sa_family != AF_UNSPEC) 761 src = &src_addr.sa; 762 dst_addr.ss = sc->rdma.cm_id->route.addr.dst_addr; 763 if (dst_addr.sa.sa_family != AF_UNSPEC) 764 dst = &dst_addr.sa; 765 766 if (sc->ib.dev) { 767 memcpy(_devname, sc->ib.dev->name, IB_DEVICE_NAME_MAX); 768 devname = _devname; 769 } 770 } 771 if (ret == 0) 772 ret = -ETIMEDOUT; 773 if (ret < 0) 774 smbdirect_socket_schedule_cleanup(sc, ret); 775 if (sc->first_error) { 776 int lvl = SMBDIRECT_LOG_ERR; 777 778 ret = sc->first_error; 779 if (ret == -ENODEV) 780 lvl = SMBDIRECT_LOG_INFO; 781 782 smbdirect_log_rdma_event(sc, lvl, 783 "connection failed %1pe device: %.*s local: %pISpsfc remote: %pISpsfc\n", 784 SMBDIRECT_DEBUG_ERR_PTR(ret), 785 IB_DEVICE_NAME_MAX, devname, src, dst); 786 return ret; 787 } 788 789 return 0; 790 } 791 EXPORT_SYMBOL_GPL(smbdirect_connection_wait_for_connected); 792 793 void smbdirect_connection_idle_timer_work(struct work_struct *work) 794 { 795 struct smbdirect_socket *sc = 796 container_of(work, struct smbdirect_socket, idle.timer_work.work); 797 const struct smbdirect_socket_parameters *sp = &sc->parameters; 798 799 if (sc->idle.keepalive != SMBDIRECT_KEEPALIVE_NONE) { 800 smbdirect_log_keep_alive(sc, SMBDIRECT_LOG_ERR, 801 "%s => timeout sc->idle.keepalive=%s\n", 802 smbdirect_socket_status_string(sc->status), 803 sc->idle.keepalive == SMBDIRECT_KEEPALIVE_SENT ? 804 "SENT" : "PENDING"); 805 smbdirect_socket_schedule_cleanup(sc, -ETIMEDOUT); 806 return; 807 } 808 809 if (sc->status != SMBDIRECT_SOCKET_CONNECTED) 810 return; 811 812 /* 813 * Now use the keepalive timeout (instead of keepalive interval) 814 * in order to wait for a response 815 */ 816 sc->idle.keepalive = SMBDIRECT_KEEPALIVE_PENDING; 817 mod_delayed_work(sc->workqueues.idle, &sc->idle.timer_work, 818 msecs_to_jiffies(sp->keepalive_timeout_msec)); 819 smbdirect_log_keep_alive(sc, SMBDIRECT_LOG_INFO, 820 "schedule send of empty idle message\n"); 821 queue_work(sc->workqueues.immediate, &sc->idle.immediate_work); 822 } 823 824 u16 smbdirect_connection_grant_recv_credits(struct smbdirect_socket *sc) 825 { 826 int missing; 827 int available; 828 int new_credits; 829 830 if (atomic_read(&sc->recv_io.credits.count) >= sc->recv_io.credits.target) 831 return 0; 832 833 missing = (int)sc->recv_io.credits.target - atomic_read(&sc->recv_io.credits.count); 834 available = atomic_xchg(&sc->recv_io.credits.available, 0); 835 new_credits = min3((int)U16_MAX, missing, available); 836 if (new_credits <= 0) { 837 /* 838 * If credits are available, but not granted 839 * we need to re-add them again. 840 */ 841 if (available) 842 atomic_add(available, &sc->recv_io.credits.available); 843 return 0; 844 } 845 846 if (new_credits < available) { 847 /* 848 * Readd the remaining available again. 849 */ 850 available -= new_credits; 851 atomic_add(available, &sc->recv_io.credits.available); 852 } 853 854 /* 855 * Remember we granted the credits 856 */ 857 atomic_add(new_credits, &sc->recv_io.credits.count); 858 return new_credits; 859 } 860 861 static bool smbdirect_connection_request_keep_alive(struct smbdirect_socket *sc) 862 { 863 const struct smbdirect_socket_parameters *sp = &sc->parameters; 864 865 if (sc->idle.keepalive == SMBDIRECT_KEEPALIVE_PENDING) { 866 sc->idle.keepalive = SMBDIRECT_KEEPALIVE_SENT; 867 /* 868 * Now use the keepalive timeout (instead of keepalive interval) 869 * in order to wait for a response 870 */ 871 mod_delayed_work(sc->workqueues.idle, &sc->idle.timer_work, 872 msecs_to_jiffies(sp->keepalive_timeout_msec)); 873 return true; 874 } 875 876 return false; 877 } 878 879 int smbdirect_connection_post_send_wr(struct smbdirect_socket *sc, 880 struct ib_send_wr *wr) 881 { 882 int ret; 883 884 if (unlikely(sc->first_error)) 885 return sc->first_error; 886 887 atomic_inc(&sc->send_io.pending.count); 888 ret = ib_post_send(sc->ib.qp, wr, NULL); 889 if (ret) { 890 atomic_dec(&sc->send_io.pending.count); 891 smbdirect_log_rdma_send(sc, SMBDIRECT_LOG_ERR, 892 "ib_post_send() failed %1pe\n", 893 SMBDIRECT_DEBUG_ERR_PTR(ret)); 894 smbdirect_socket_schedule_cleanup(sc, ret); 895 } 896 897 return ret; 898 } 899 900 static void smbdirect_connection_send_batch_init(struct smbdirect_send_batch *batch, 901 bool need_invalidate_rkey, 902 unsigned int remote_key) 903 { 904 INIT_LIST_HEAD(&batch->msg_list); 905 batch->wr_cnt = 0; 906 batch->need_invalidate_rkey = need_invalidate_rkey; 907 batch->remote_key = remote_key; 908 batch->credit = 0; 909 } 910 911 int smbdirect_connection_send_batch_flush(struct smbdirect_socket *sc, 912 struct smbdirect_send_batch *batch, 913 bool is_last) 914 { 915 struct smbdirect_send_io *first, *last; 916 int ret = 0; 917 918 if (list_empty(&batch->msg_list)) 919 goto release_credit; 920 921 first = list_first_entry(&batch->msg_list, 922 struct smbdirect_send_io, 923 sibling_list); 924 last = list_last_entry(&batch->msg_list, 925 struct smbdirect_send_io, 926 sibling_list); 927 928 if (batch->need_invalidate_rkey) { 929 first->wr.opcode = IB_WR_SEND_WITH_INV; 930 first->wr.ex.invalidate_rkey = batch->remote_key; 931 batch->need_invalidate_rkey = false; 932 batch->remote_key = 0; 933 } 934 935 last->wr.send_flags = IB_SEND_SIGNALED; 936 last->wr.wr_cqe = &last->cqe; 937 938 /* 939 * Remove last from send_ctx->msg_list 940 * and splice the rest of send_ctx->msg_list 941 * to last->sibling_list. 942 * 943 * send_ctx->msg_list is a valid empty list 944 * at the end. 945 */ 946 list_del_init(&last->sibling_list); 947 list_splice_tail_init(&batch->msg_list, &last->sibling_list); 948 batch->wr_cnt = 0; 949 950 ret = smbdirect_connection_post_send_wr(sc, &first->wr); 951 if (ret) { 952 struct smbdirect_send_io *sibling, *next; 953 954 list_for_each_entry_safe(sibling, next, &last->sibling_list, sibling_list) { 955 list_del_init(&sibling->sibling_list); 956 smbdirect_connection_free_send_io(sibling); 957 } 958 smbdirect_connection_free_send_io(last); 959 } 960 961 release_credit: 962 if (is_last && !ret && batch->credit) { 963 atomic_add(batch->credit, &sc->send_io.bcredits.count); 964 batch->credit = 0; 965 wake_up(&sc->send_io.bcredits.wait_queue); 966 } 967 968 return ret; 969 } 970 EXPORT_SYMBOL_GPL(smbdirect_connection_send_batch_flush); 971 972 struct smbdirect_send_batch * 973 smbdirect_init_send_batch_storage(struct smbdirect_send_batch_storage *storage, 974 bool need_invalidate_rkey, 975 unsigned int remote_key) 976 { 977 struct smbdirect_send_batch *batch = (struct smbdirect_send_batch *)storage; 978 979 memset(storage, 0, sizeof(*storage)); 980 BUILD_BUG_ON(sizeof(*batch) > sizeof(*storage)); 981 982 smbdirect_connection_send_batch_init(batch, 983 need_invalidate_rkey, 984 remote_key); 985 986 return batch; 987 } 988 EXPORT_SYMBOL_GPL(smbdirect_init_send_batch_storage); 989 990 static int smbdirect_connection_wait_for_send_bcredit(struct smbdirect_socket *sc, 991 struct smbdirect_send_batch *batch) 992 { 993 int ret; 994 995 if (batch->credit) 996 return 0; 997 998 ret = smbdirect_socket_wait_for_credits(sc, 999 SMBDIRECT_SOCKET_CONNECTED, 1000 -ENOTCONN, 1001 &sc->send_io.bcredits.wait_queue, 1002 &sc->send_io.bcredits.count, 1003 1); 1004 if (ret) 1005 return ret; 1006 1007 batch->credit = 1; 1008 return 0; 1009 } 1010 1011 static int smbdirect_connection_wait_for_send_lcredit(struct smbdirect_socket *sc, 1012 struct smbdirect_send_batch *batch) 1013 { 1014 if (batch && atomic_read(&sc->send_io.lcredits.count) <= 1) { 1015 int ret; 1016 1017 ret = smbdirect_connection_send_batch_flush(sc, batch, false); 1018 if (ret) 1019 return ret; 1020 } 1021 1022 return smbdirect_socket_wait_for_credits(sc, 1023 SMBDIRECT_SOCKET_CONNECTED, 1024 -ENOTCONN, 1025 &sc->send_io.lcredits.wait_queue, 1026 &sc->send_io.lcredits.count, 1027 1); 1028 } 1029 1030 static int smbdirect_connection_wait_for_send_credits(struct smbdirect_socket *sc, 1031 struct smbdirect_send_batch *batch) 1032 { 1033 if (batch && (batch->wr_cnt >= 16 || atomic_read(&sc->send_io.credits.count) <= 1)) { 1034 int ret; 1035 1036 ret = smbdirect_connection_send_batch_flush(sc, batch, false); 1037 if (ret) 1038 return ret; 1039 } 1040 1041 return smbdirect_socket_wait_for_credits(sc, 1042 SMBDIRECT_SOCKET_CONNECTED, 1043 -ENOTCONN, 1044 &sc->send_io.credits.wait_queue, 1045 &sc->send_io.credits.count, 1046 1); 1047 } 1048 1049 static void smbdirect_connection_send_io_done(struct ib_cq *cq, struct ib_wc *wc); 1050 1051 static int smbdirect_connection_post_send_io(struct smbdirect_socket *sc, 1052 struct smbdirect_send_batch *batch, 1053 struct smbdirect_send_io *msg) 1054 { 1055 int i; 1056 1057 for (i = 0; i < msg->num_sge; i++) 1058 ib_dma_sync_single_for_device(sc->ib.dev, 1059 msg->sge[i].addr, msg->sge[i].length, 1060 DMA_TO_DEVICE); 1061 1062 msg->cqe.done = smbdirect_connection_send_io_done; 1063 msg->wr.wr_cqe = &msg->cqe; 1064 msg->wr.opcode = IB_WR_SEND; 1065 msg->wr.sg_list = &msg->sge[0]; 1066 msg->wr.num_sge = msg->num_sge; 1067 msg->wr.next = NULL; 1068 1069 if (batch) { 1070 msg->wr.send_flags = 0; 1071 if (!list_empty(&batch->msg_list)) { 1072 struct smbdirect_send_io *last; 1073 1074 last = list_last_entry(&batch->msg_list, 1075 struct smbdirect_send_io, 1076 sibling_list); 1077 last->wr.next = &msg->wr; 1078 } 1079 list_add_tail(&msg->sibling_list, &batch->msg_list); 1080 batch->wr_cnt++; 1081 return 0; 1082 } 1083 1084 msg->wr.send_flags = IB_SEND_SIGNALED; 1085 return smbdirect_connection_post_send_wr(sc, &msg->wr); 1086 } 1087 1088 int smbdirect_connection_send_single_iter(struct smbdirect_socket *sc, 1089 struct smbdirect_send_batch *batch, 1090 struct iov_iter *iter, 1091 unsigned int flags, 1092 u32 remaining_data_length) 1093 { 1094 const struct smbdirect_socket_parameters *sp = &sc->parameters; 1095 struct smbdirect_send_batch _batch; 1096 struct smbdirect_send_io *msg; 1097 struct smbdirect_data_transfer *packet; 1098 size_t header_length; 1099 u16 new_credits = 0; 1100 u32 data_length = 0; 1101 int ret; 1102 1103 if (WARN_ON_ONCE(flags)) 1104 return -EINVAL; /* no flags support for now */ 1105 1106 if (iter) { 1107 if (WARN_ON_ONCE(iov_iter_rw(iter) != ITER_SOURCE)) 1108 return -EINVAL; /* It's a bug in upper layer to get there */ 1109 1110 header_length = sizeof(struct smbdirect_data_transfer); 1111 if (WARN_ON_ONCE(remaining_data_length == 0 || 1112 iov_iter_count(iter) > remaining_data_length)) 1113 return -EINVAL; 1114 } else { 1115 /* If this is a packet without payload, don't send padding */ 1116 header_length = offsetof(struct smbdirect_data_transfer, padding); 1117 if (WARN_ON_ONCE(remaining_data_length)) 1118 return -EINVAL; 1119 } 1120 1121 if (sc->status != SMBDIRECT_SOCKET_CONNECTED) { 1122 smbdirect_log_write(sc, SMBDIRECT_LOG_ERR, 1123 "status=%s first_error=%1pe => %1pe\n", 1124 smbdirect_socket_status_string(sc->status), 1125 SMBDIRECT_DEBUG_ERR_PTR(sc->first_error), 1126 SMBDIRECT_DEBUG_ERR_PTR(-ENOTCONN)); 1127 return -ENOTCONN; 1128 } 1129 1130 if (!batch) { 1131 smbdirect_connection_send_batch_init(&_batch, false, 0); 1132 batch = &_batch; 1133 } 1134 1135 ret = smbdirect_connection_wait_for_send_bcredit(sc, batch); 1136 if (ret) 1137 goto bcredit_failed; 1138 1139 ret = smbdirect_connection_wait_for_send_lcredit(sc, batch); 1140 if (ret) 1141 goto lcredit_failed; 1142 1143 ret = smbdirect_connection_wait_for_send_credits(sc, batch); 1144 if (ret) 1145 goto credit_failed; 1146 1147 new_credits = smbdirect_connection_grant_recv_credits(sc); 1148 if (new_credits == 0 && 1149 atomic_read(&sc->send_io.credits.count) == 0 && 1150 atomic_read(&sc->recv_io.credits.count) == 0) { 1151 /* 1152 * queue the refill work in order to 1153 * get some new recv credits we can grant to 1154 * the peer. 1155 */ 1156 queue_work(sc->workqueues.refill, &sc->recv_io.posted.refill_work); 1157 1158 /* 1159 * wait until either the refill work or the peer 1160 * granted new credits 1161 */ 1162 ret = wait_event_interruptible(sc->send_io.credits.wait_queue, 1163 atomic_read(&sc->send_io.credits.count) >= 1 || 1164 atomic_read(&sc->recv_io.credits.available) >= 1 || 1165 sc->status != SMBDIRECT_SOCKET_CONNECTED); 1166 if (sc->status != SMBDIRECT_SOCKET_CONNECTED) 1167 ret = -ENOTCONN; 1168 if (ret < 0) 1169 goto credit_failed; 1170 1171 new_credits = smbdirect_connection_grant_recv_credits(sc); 1172 } 1173 1174 msg = smbdirect_connection_alloc_send_io(sc); 1175 if (IS_ERR(msg)) { 1176 ret = PTR_ERR(msg); 1177 goto alloc_failed; 1178 } 1179 1180 /* Map the packet to DMA */ 1181 msg->sge[0].addr = ib_dma_map_single(sc->ib.dev, 1182 msg->packet, 1183 header_length, 1184 DMA_TO_DEVICE); 1185 ret = ib_dma_mapping_error(sc->ib.dev, msg->sge[0].addr); 1186 if (ret) 1187 goto err; 1188 1189 msg->sge[0].length = header_length; 1190 msg->sge[0].lkey = sc->ib.pd->local_dma_lkey; 1191 msg->num_sge = 1; 1192 1193 if (iter) { 1194 struct smbdirect_map_sges extract = { 1195 .num_sge = msg->num_sge, 1196 .max_sge = ARRAY_SIZE(msg->sge), 1197 .sge = msg->sge, 1198 .device = sc->ib.dev, 1199 .local_dma_lkey = sc->ib.pd->local_dma_lkey, 1200 .direction = DMA_TO_DEVICE, 1201 }; 1202 size_t payload_len = umin(iov_iter_count(iter), 1203 sp->max_send_size - sizeof(*packet)); 1204 1205 ret = smbdirect_map_sges_from_iter(iter, payload_len, &extract); 1206 if (ret < 0) 1207 goto err; 1208 data_length = ret; 1209 remaining_data_length -= data_length; 1210 msg->num_sge = extract.num_sge; 1211 } 1212 1213 /* Fill in the packet header */ 1214 packet = (struct smbdirect_data_transfer *)msg->packet; 1215 packet->credits_requested = cpu_to_le16(sp->send_credit_target); 1216 packet->credits_granted = cpu_to_le16(new_credits); 1217 1218 packet->flags = 0; 1219 if (smbdirect_connection_request_keep_alive(sc)) 1220 packet->flags |= cpu_to_le16(SMBDIRECT_FLAG_RESPONSE_REQUESTED); 1221 1222 packet->reserved = 0; 1223 if (!data_length) 1224 packet->data_offset = 0; 1225 else 1226 packet->data_offset = cpu_to_le32(24); 1227 packet->data_length = cpu_to_le32(data_length); 1228 packet->remaining_data_length = cpu_to_le32(remaining_data_length); 1229 packet->padding = 0; 1230 1231 smbdirect_log_outgoing(sc, SMBDIRECT_LOG_INFO, 1232 "DataOut: %s=%u, %s=%u, %s=0x%x, %s=%u, %s=%u, %s=%u\n", 1233 "CreditsRequested", 1234 le16_to_cpu(packet->credits_requested), 1235 "CreditsGranted", 1236 le16_to_cpu(packet->credits_granted), 1237 "Flags", 1238 le16_to_cpu(packet->flags), 1239 "RemainingDataLength", 1240 le32_to_cpu(packet->remaining_data_length), 1241 "DataOffset", 1242 le32_to_cpu(packet->data_offset), 1243 "DataLength", 1244 le32_to_cpu(packet->data_length)); 1245 1246 ret = smbdirect_connection_post_send_io(sc, batch, msg); 1247 if (ret) 1248 goto err; 1249 1250 /* 1251 * From here msg is moved to send_ctx 1252 * and we should not free it explicitly. 1253 */ 1254 1255 if (batch == &_batch) { 1256 ret = smbdirect_connection_send_batch_flush(sc, batch, true); 1257 if (ret) 1258 goto flush_failed; 1259 } 1260 1261 return data_length; 1262 err: 1263 smbdirect_connection_free_send_io(msg); 1264 flush_failed: 1265 alloc_failed: 1266 atomic_inc(&sc->send_io.credits.count); 1267 credit_failed: 1268 atomic_inc(&sc->send_io.lcredits.count); 1269 lcredit_failed: 1270 atomic_add(batch->credit, &sc->send_io.bcredits.count); 1271 batch->credit = 0; 1272 bcredit_failed: 1273 return ret; 1274 } 1275 EXPORT_SYMBOL_GPL(smbdirect_connection_send_single_iter); 1276 1277 int smbdirect_connection_send_wait_zero_pending(struct smbdirect_socket *sc) 1278 { 1279 /* 1280 * As an optimization, we don't wait for individual I/O to finish 1281 * before sending the next one. 1282 * Send them all and wait for pending send count to get to 0 1283 * that means all the I/Os have been out and we are good to return 1284 */ 1285 1286 wait_event(sc->send_io.pending.zero_wait_queue, 1287 atomic_read(&sc->send_io.pending.count) == 0 || 1288 sc->status != SMBDIRECT_SOCKET_CONNECTED); 1289 if (sc->status != SMBDIRECT_SOCKET_CONNECTED) { 1290 smbdirect_log_write(sc, SMBDIRECT_LOG_ERR, 1291 "status=%s first_error=%1pe => %1pe\n", 1292 smbdirect_socket_status_string(sc->status), 1293 SMBDIRECT_DEBUG_ERR_PTR(sc->first_error), 1294 SMBDIRECT_DEBUG_ERR_PTR(-ENOTCONN)); 1295 return -ENOTCONN; 1296 } 1297 1298 return 0; 1299 } 1300 EXPORT_SYMBOL_GPL(smbdirect_connection_send_wait_zero_pending); 1301 1302 int smbdirect_connection_send_iter(struct smbdirect_socket *sc, 1303 struct iov_iter *iter, 1304 unsigned int flags, 1305 bool need_invalidate, 1306 unsigned int remote_key) 1307 { 1308 const struct smbdirect_socket_parameters *sp = &sc->parameters; 1309 struct smbdirect_send_batch batch; 1310 int total_count = iov_iter_count(iter); 1311 int ret; 1312 int error = 0; 1313 __be32 hdr; 1314 1315 if (WARN_ONCE(flags, "unexpected flags=0x%x\n", flags)) 1316 return -EINVAL; /* no flags support for now */ 1317 1318 if (WARN_ON_ONCE(iov_iter_rw(iter) != ITER_SOURCE)) 1319 return -EINVAL; /* It's a bug in upper layer to get there */ 1320 1321 if (sc->status != SMBDIRECT_SOCKET_CONNECTED) { 1322 smbdirect_log_write(sc, SMBDIRECT_LOG_INFO, 1323 "status=%s first_error=%1pe => %1pe\n", 1324 smbdirect_socket_status_string(sc->status), 1325 SMBDIRECT_DEBUG_ERR_PTR(sc->first_error), 1326 SMBDIRECT_DEBUG_ERR_PTR(-ENOTCONN)); 1327 return -ENOTCONN; 1328 } 1329 1330 /* 1331 * For now we expect the iter to have the full 1332 * message, including a 4 byte length header. 1333 */ 1334 if (iov_iter_count(iter) <= 4) 1335 return -EINVAL; 1336 if (!copy_from_iter_full(&hdr, sizeof(hdr), iter)) 1337 return -EFAULT; 1338 if (iov_iter_count(iter) != be32_to_cpu(hdr)) 1339 return -EINVAL; 1340 1341 /* 1342 * The size must fit into the negotiated 1343 * fragmented send size. 1344 */ 1345 if (iov_iter_count(iter) > sp->max_fragmented_send_size) 1346 return -EMSGSIZE; 1347 1348 smbdirect_log_write(sc, SMBDIRECT_LOG_INFO, 1349 "Sending (RDMA): length=%zu\n", 1350 iov_iter_count(iter)); 1351 1352 smbdirect_connection_send_batch_init(&batch, need_invalidate, remote_key); 1353 while (iov_iter_count(iter)) { 1354 ret = smbdirect_connection_send_single_iter(sc, 1355 &batch, 1356 iter, 1357 flags, 1358 iov_iter_count(iter)); 1359 if (unlikely(ret < 0)) { 1360 error = ret; 1361 break; 1362 } 1363 } 1364 1365 ret = smbdirect_connection_send_batch_flush(sc, &batch, true); 1366 if (unlikely(ret && !error)) 1367 error = ret; 1368 1369 /* 1370 * As an optimization, we don't wait for individual I/O to finish 1371 * before sending the next one. 1372 * Send them all and wait for pending send count to get to 0 1373 * that means all the I/Os have been out and we are good to return 1374 */ 1375 1376 ret = smbdirect_connection_send_wait_zero_pending(sc); 1377 if (unlikely(ret && !error)) 1378 error = ret; 1379 1380 if (unlikely(error)) 1381 return error; 1382 1383 return total_count; 1384 } 1385 EXPORT_SYMBOL_GPL(smbdirect_connection_send_iter); 1386 1387 static void smbdirect_connection_send_io_done(struct ib_cq *cq, struct ib_wc *wc) 1388 { 1389 struct smbdirect_send_io *msg = 1390 container_of(wc->wr_cqe, struct smbdirect_send_io, cqe); 1391 struct smbdirect_socket *sc = msg->socket; 1392 struct smbdirect_send_io *sibling, *next; 1393 int lcredits = 0; 1394 1395 smbdirect_log_rdma_send(sc, SMBDIRECT_LOG_INFO, 1396 "smbdirect_send_io completed. status='%s (%d)', opcode=%d\n", 1397 ib_wc_status_msg(wc->status), wc->status, wc->opcode); 1398 1399 if (unlikely(!(msg->wr.send_flags & IB_SEND_SIGNALED))) { 1400 /* 1401 * This happens when smbdirect_send_io is a sibling 1402 * before the final message, it is signaled on 1403 * error anyway, so we need to skip 1404 * smbdirect_connection_free_send_io here, 1405 * otherwise is will destroy the memory 1406 * of the siblings too, which will cause 1407 * use after free problems for the others 1408 * triggered from ib_drain_qp(). 1409 */ 1410 if (wc->status != IB_WC_SUCCESS) 1411 goto skip_free; 1412 1413 /* 1414 * This should not happen! 1415 * But we better just close the 1416 * connection... 1417 */ 1418 smbdirect_log_rdma_send(sc, SMBDIRECT_LOG_ERR, 1419 "unexpected send completion wc->status=%s (%d) wc->opcode=%d\n", 1420 ib_wc_status_msg(wc->status), wc->status, wc->opcode); 1421 smbdirect_socket_schedule_cleanup(sc, -ECONNABORTED); 1422 return; 1423 } 1424 1425 /* 1426 * Free possible siblings and then the main send_io 1427 */ 1428 list_for_each_entry_safe(sibling, next, &msg->sibling_list, sibling_list) { 1429 list_del_init(&sibling->sibling_list); 1430 smbdirect_connection_free_send_io(sibling); 1431 lcredits += 1; 1432 } 1433 /* Note this frees wc->wr_cqe, but not wc */ 1434 smbdirect_connection_free_send_io(msg); 1435 lcredits += 1; 1436 1437 if (unlikely(wc->status != IB_WC_SUCCESS || WARN_ON_ONCE(wc->opcode != IB_WC_SEND))) { 1438 skip_free: 1439 if (wc->status != IB_WC_WR_FLUSH_ERR) 1440 smbdirect_log_rdma_send(sc, SMBDIRECT_LOG_ERR, 1441 "wc->status=%s (%d) wc->opcode=%d\n", 1442 ib_wc_status_msg(wc->status), wc->status, wc->opcode); 1443 smbdirect_socket_schedule_cleanup(sc, -ECONNABORTED); 1444 return; 1445 } 1446 1447 atomic_add(lcredits, &sc->send_io.lcredits.count); 1448 wake_up(&sc->send_io.lcredits.wait_queue); 1449 1450 if (atomic_dec_and_test(&sc->send_io.pending.count)) 1451 wake_up(&sc->send_io.pending.zero_wait_queue); 1452 } 1453 1454 static void smbdirect_connection_send_immediate_work(struct work_struct *work) 1455 { 1456 struct smbdirect_socket *sc = 1457 container_of(work, struct smbdirect_socket, idle.immediate_work); 1458 int ret; 1459 1460 if (sc->status != SMBDIRECT_SOCKET_CONNECTED) 1461 return; 1462 1463 smbdirect_log_keep_alive(sc, SMBDIRECT_LOG_INFO, 1464 "send an empty message\n"); 1465 sc->statistics.send_empty++; 1466 ret = smbdirect_connection_send_single_iter(sc, NULL, NULL, 0, 0); 1467 if (ret < 0) { 1468 smbdirect_log_write(sc, SMBDIRECT_LOG_ERR, 1469 "smbdirect_connection_send_single_iter ret=%1pe\n", 1470 SMBDIRECT_DEBUG_ERR_PTR(ret)); 1471 smbdirect_socket_schedule_cleanup(sc, ret); 1472 } 1473 } 1474 1475 int smbdirect_connection_post_recv_io(struct smbdirect_recv_io *msg) 1476 { 1477 struct smbdirect_socket *sc = msg->socket; 1478 const struct smbdirect_socket_parameters *sp = &sc->parameters; 1479 struct ib_recv_wr recv_wr = { 1480 .wr_cqe = &msg->cqe, 1481 .sg_list = &msg->sge, 1482 .num_sge = 1, 1483 }; 1484 int ret; 1485 1486 if (unlikely(sc->first_error)) 1487 return sc->first_error; 1488 1489 msg->sge.addr = ib_dma_map_single(sc->ib.dev, 1490 msg->packet, 1491 sp->max_recv_size, 1492 DMA_FROM_DEVICE); 1493 ret = ib_dma_mapping_error(sc->ib.dev, msg->sge.addr); 1494 if (ret) 1495 return ret; 1496 1497 msg->sge.length = sp->max_recv_size; 1498 msg->sge.lkey = sc->ib.pd->local_dma_lkey; 1499 1500 ret = ib_post_recv(sc->ib.qp, &recv_wr, NULL); 1501 if (ret) { 1502 smbdirect_log_rdma_recv(sc, SMBDIRECT_LOG_ERR, 1503 "ib_post_recv failed ret=%d (%1pe)\n", 1504 ret, SMBDIRECT_DEBUG_ERR_PTR(ret)); 1505 ib_dma_unmap_single(sc->ib.dev, 1506 msg->sge.addr, 1507 msg->sge.length, 1508 DMA_FROM_DEVICE); 1509 msg->sge.length = 0; 1510 smbdirect_socket_schedule_cleanup(sc, ret); 1511 } 1512 1513 return ret; 1514 } 1515 1516 void smbdirect_connection_recv_io_done(struct ib_cq *cq, struct ib_wc *wc) 1517 { 1518 struct smbdirect_recv_io *recv_io = 1519 container_of(wc->wr_cqe, struct smbdirect_recv_io, cqe); 1520 struct smbdirect_socket *sc = recv_io->socket; 1521 const struct smbdirect_socket_parameters *sp = &sc->parameters; 1522 struct smbdirect_data_transfer *data_transfer; 1523 int current_recv_credits; 1524 u16 old_recv_credit_target; 1525 u16 credits_requested; 1526 u16 credits_granted; 1527 u16 flags; 1528 u32 data_offset; 1529 u32 data_length; 1530 u32 remaining_data_length; 1531 1532 if (unlikely(wc->status != IB_WC_SUCCESS || WARN_ON_ONCE(wc->opcode != IB_WC_RECV))) { 1533 if (wc->status != IB_WC_WR_FLUSH_ERR) 1534 smbdirect_log_rdma_recv(sc, SMBDIRECT_LOG_ERR, 1535 "wc->status=%s (%d) wc->opcode=%d\n", 1536 ib_wc_status_msg(wc->status), wc->status, wc->opcode); 1537 goto error; 1538 } 1539 1540 smbdirect_log_rdma_recv(sc, SMBDIRECT_LOG_INFO, 1541 "recv_io=0x%p type=%d wc status=%s wc opcode %d byte_len=%d pkey_index=%u\n", 1542 recv_io, sc->recv_io.expected, 1543 ib_wc_status_msg(wc->status), wc->opcode, 1544 wc->byte_len, wc->pkey_index); 1545 1546 /* 1547 * Reset timer to the keepalive interval in 1548 * order to trigger our next keepalive message. 1549 */ 1550 sc->idle.keepalive = SMBDIRECT_KEEPALIVE_NONE; 1551 mod_delayed_work(sc->workqueues.idle, &sc->idle.timer_work, 1552 msecs_to_jiffies(sp->keepalive_interval_msec)); 1553 1554 ib_dma_sync_single_for_cpu(sc->ib.dev, 1555 recv_io->sge.addr, 1556 recv_io->sge.length, 1557 DMA_FROM_DEVICE); 1558 1559 if (unlikely(wc->byte_len < 1560 offsetof(struct smbdirect_data_transfer, padding))) { 1561 smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR, 1562 "wc->byte_len=%u < %zu\n", 1563 wc->byte_len, 1564 offsetof(struct smbdirect_data_transfer, padding)); 1565 goto error; 1566 } 1567 1568 data_transfer = (struct smbdirect_data_transfer *)recv_io->packet; 1569 credits_requested = le16_to_cpu(data_transfer->credits_requested); 1570 credits_granted = le16_to_cpu(data_transfer->credits_granted); 1571 flags = le16_to_cpu(data_transfer->flags); 1572 remaining_data_length = le32_to_cpu(data_transfer->remaining_data_length); 1573 data_offset = le32_to_cpu(data_transfer->data_offset); 1574 data_length = le32_to_cpu(data_transfer->data_length); 1575 1576 smbdirect_log_incoming(sc, SMBDIRECT_LOG_INFO, 1577 "DataIn: %s=%u, %s=%u, %s=0x%x, %s=%u, %s=%u, %s=%u\n", 1578 "CreditsRequested", 1579 credits_requested, 1580 "CreditsGranted", 1581 credits_granted, 1582 "Flags", 1583 flags, 1584 "RemainingDataLength", 1585 remaining_data_length, 1586 "DataOffset", 1587 data_offset, 1588 "DataLength", 1589 data_length); 1590 1591 if (unlikely(credits_requested == 0)) { 1592 smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR, 1593 "invalid: credits_requested == 0\n"); 1594 goto error; 1595 } 1596 1597 if (unlikely(data_offset % 8 != 0)) { 1598 smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR, 1599 "invalid: data_offset=%u (0x%x) not aligned to 8\n", 1600 data_offset, data_offset); 1601 goto error; 1602 } 1603 1604 if (unlikely(wc->byte_len < data_offset || 1605 (u64)wc->byte_len < (u64)data_offset + data_length)) { 1606 smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR, 1607 "wc->byte_len=%u < date_offset=%u + data_length=%u\n", 1608 wc->byte_len, data_offset, data_length); 1609 goto error; 1610 } 1611 1612 if (unlikely(remaining_data_length > sp->max_fragmented_recv_size || 1613 data_length > sp->max_fragmented_recv_size || 1614 (u64)remaining_data_length + (u64)data_length > (u64)sp->max_fragmented_recv_size)) { 1615 smbdirect_log_rdma_event(sc, SMBDIRECT_LOG_ERR, 1616 "remaining_data_length=%u + data_length=%u > max_fragmented=%u\n", 1617 remaining_data_length, data_length, sp->max_fragmented_recv_size); 1618 goto error; 1619 } 1620 1621 if (data_length) { 1622 if (sc->recv_io.reassembly.full_packet_received) 1623 recv_io->first_segment = true; 1624 1625 if (remaining_data_length) 1626 sc->recv_io.reassembly.full_packet_received = false; 1627 else 1628 sc->recv_io.reassembly.full_packet_received = true; 1629 } 1630 1631 atomic_dec(&sc->recv_io.posted.count); 1632 current_recv_credits = atomic_dec_return(&sc->recv_io.credits.count); 1633 1634 /* 1635 * We take the value from the peer, which is checked to be higher than 0, 1636 * but we limit it to the max value we support in order to have 1637 * the main logic simpler. 1638 */ 1639 old_recv_credit_target = sc->recv_io.credits.target; 1640 sc->recv_io.credits.target = credits_requested; 1641 sc->recv_io.credits.target = min_t(u16, sc->recv_io.credits.target, 1642 sp->recv_credit_max); 1643 if (credits_granted) { 1644 atomic_add(credits_granted, &sc->send_io.credits.count); 1645 /* 1646 * We have new send credits granted from remote peer 1647 * If any sender is waiting for credits, unblock it 1648 */ 1649 wake_up(&sc->send_io.credits.wait_queue); 1650 } 1651 1652 /* Send an immediate response right away if requested */ 1653 if (flags & SMBDIRECT_FLAG_RESPONSE_REQUESTED) { 1654 smbdirect_log_keep_alive(sc, SMBDIRECT_LOG_INFO, 1655 "schedule send of immediate response\n"); 1656 queue_work(sc->workqueues.immediate, &sc->idle.immediate_work); 1657 } 1658 1659 /* 1660 * If this is a packet with data playload place the data in 1661 * reassembly queue and wake up the reading thread 1662 */ 1663 if (data_length) { 1664 if (current_recv_credits <= (sc->recv_io.credits.target / 4) || 1665 sc->recv_io.credits.target > old_recv_credit_target) 1666 queue_work(sc->workqueues.refill, &sc->recv_io.posted.refill_work); 1667 1668 smbdirect_connection_reassembly_append_recv_io(sc, recv_io, data_length); 1669 wake_up(&sc->recv_io.reassembly.wait_queue); 1670 } else 1671 smbdirect_connection_put_recv_io(recv_io); 1672 1673 return; 1674 1675 error: 1676 /* 1677 * Make sure smbdirect_connection_put_recv_io() does not 1678 * start recv_io.posted.refill_work. 1679 */ 1680 disable_work(&sc->recv_io.posted.refill_work); 1681 smbdirect_connection_put_recv_io(recv_io); 1682 smbdirect_socket_schedule_cleanup(sc, -ECONNABORTED); 1683 } 1684 1685 int smbdirect_connection_recv_io_refill(struct smbdirect_socket *sc) 1686 { 1687 int missing; 1688 int posted = 0; 1689 1690 if (unlikely(sc->first_error)) 1691 return sc->first_error; 1692 1693 /* 1694 * Find out how much smbdirect_recv_io buffers we should post. 1695 * 1696 * Note that sc->recv_io.credits.target is the value 1697 * from the peer and it can in theory change over time, 1698 * but it is forced to be at least 1 and at max 1699 * sp->recv_credit_max. 1700 * 1701 * So it can happen that missing will be lower than 0, 1702 * which means the peer has recently lowered its desired 1703 * target, while be already granted a higher number of credits. 1704 * 1705 * Note 'posted' is the number of smbdirect_recv_io buffers 1706 * posted within this function, while sc->recv_io.posted.count 1707 * is the overall value of posted smbdirect_recv_io buffers. 1708 * 1709 * We try to post as much buffers as missing, but 1710 * this is limited if a lot of smbdirect_recv_io buffers 1711 * are still in the sc->recv_io.reassembly.list instead of 1712 * the sc->recv_io.free.list. 1713 * 1714 */ 1715 missing = (int)sc->recv_io.credits.target - atomic_read(&sc->recv_io.posted.count); 1716 while (posted < missing) { 1717 struct smbdirect_recv_io *recv_io; 1718 int ret; 1719 1720 /* 1721 * It's ok if smbdirect_connection_get_recv_io() 1722 * returns NULL, it means smbdirect_recv_io structures 1723 * are still be in the reassembly.list. 1724 */ 1725 recv_io = smbdirect_connection_get_recv_io(sc); 1726 if (!recv_io) 1727 break; 1728 1729 recv_io->first_segment = false; 1730 1731 ret = smbdirect_connection_post_recv_io(recv_io); 1732 if (ret) { 1733 smbdirect_log_rdma_recv(sc, SMBDIRECT_LOG_ERR, 1734 "smbdirect_connection_post_recv_io failed rc=%d (%1pe)\n", 1735 ret, SMBDIRECT_DEBUG_ERR_PTR(ret)); 1736 smbdirect_connection_put_recv_io(recv_io); 1737 return ret; 1738 } 1739 1740 atomic_inc(&sc->recv_io.posted.count); 1741 posted += 1; 1742 } 1743 1744 /* If nothing was posted we're done */ 1745 if (posted == 0) 1746 return 0; 1747 1748 atomic_add(posted, &sc->recv_io.credits.available); 1749 1750 /* 1751 * If the last send credit is waiting for credits 1752 * it can grant we need to wake it up 1753 */ 1754 if (atomic_read(&sc->send_io.bcredits.count) == 0 && 1755 atomic_read(&sc->send_io.credits.count) == 0) 1756 wake_up(&sc->send_io.credits.wait_queue); 1757 1758 /* 1759 * If we posted at least one smbdirect_recv_io buffer, 1760 * we need to inform the peer about it and grant 1761 * additional credits. 1762 * 1763 * However there is one case where we don't want to 1764 * do that. 1765 * 1766 * If only a single credit was missing before 1767 * reaching the requested target, we should not 1768 * post an immediate send, as that would cause 1769 * endless ping pong once a keep alive exchange 1770 * is started. 1771 * 1772 * However if sc->recv_io.credits.target is only 1, 1773 * the peer has no credit left and we need to 1774 * grant the credit anyway. 1775 */ 1776 if (missing == 1 && sc->recv_io.credits.target != 1) 1777 return 0; 1778 1779 return posted; 1780 } 1781 1782 static void smbdirect_connection_recv_io_refill_work(struct work_struct *work) 1783 { 1784 struct smbdirect_socket *sc = 1785 container_of(work, struct smbdirect_socket, recv_io.posted.refill_work); 1786 int posted; 1787 1788 posted = smbdirect_connection_recv_io_refill(sc); 1789 if (unlikely(posted < 0)) { 1790 smbdirect_socket_schedule_cleanup(sc, posted); 1791 return; 1792 } 1793 if (posted > 0) { 1794 smbdirect_log_keep_alive(sc, SMBDIRECT_LOG_INFO, 1795 "schedule send of an empty message\n"); 1796 queue_work(sc->workqueues.immediate, &sc->idle.immediate_work); 1797 } 1798 } 1799 1800 int smbdirect_connection_recvmsg(struct smbdirect_socket *sc, 1801 struct msghdr *msg, 1802 unsigned int flags) 1803 { 1804 struct smbdirect_recv_io *response; 1805 struct smbdirect_data_transfer *data_transfer; 1806 size_t size = iov_iter_count(&msg->msg_iter); 1807 int to_copy, to_read, data_read, offset; 1808 u32 data_length, remaining_data_length, data_offset; 1809 int ret; 1810 1811 if (WARN_ONCE(flags, "unexpected flags=0x%x\n", flags)) 1812 return -EINVAL; /* no flags support for now */ 1813 1814 if (WARN_ON_ONCE(iov_iter_rw(&msg->msg_iter) != ITER_DEST)) 1815 return -EINVAL; /* It's a bug in upper layer to get there */ 1816 1817 again: 1818 if (sc->status != SMBDIRECT_SOCKET_CONNECTED) { 1819 smbdirect_log_read(sc, SMBDIRECT_LOG_INFO, 1820 "status=%s first_error=%1pe => %1pe\n", 1821 smbdirect_socket_status_string(sc->status), 1822 SMBDIRECT_DEBUG_ERR_PTR(sc->first_error), 1823 SMBDIRECT_DEBUG_ERR_PTR(-ENOTCONN)); 1824 return -ENOTCONN; 1825 } 1826 1827 /* 1828 * No need to hold the reassembly queue lock all the time as we are 1829 * the only one reading from the front of the queue. The transport 1830 * may add more entries to the back of the queue at the same time 1831 */ 1832 smbdirect_log_read(sc, SMBDIRECT_LOG_INFO, 1833 "size=%zd sc->recv_io.reassembly.data_length=%d\n", 1834 size, sc->recv_io.reassembly.data_length); 1835 if (sc->recv_io.reassembly.data_length >= size) { 1836 int queue_length; 1837 int queue_removed = 0; 1838 unsigned long flags; 1839 1840 /* 1841 * Need to make sure reassembly_data_length is read before 1842 * reading reassembly_queue_length and calling 1843 * smbdirect_connection_reassembly_first_recv_io. This call is lock free 1844 * as we never read at the end of the queue which are being 1845 * updated in SOFTIRQ as more data is received 1846 */ 1847 virt_rmb(); 1848 queue_length = sc->recv_io.reassembly.queue_length; 1849 data_read = 0; 1850 to_read = size; 1851 offset = sc->recv_io.reassembly.first_entry_offset; 1852 while (data_read < size) { 1853 response = smbdirect_connection_reassembly_first_recv_io(sc); 1854 data_transfer = (void *)response->packet; 1855 data_length = le32_to_cpu(data_transfer->data_length); 1856 remaining_data_length = 1857 le32_to_cpu( 1858 data_transfer->remaining_data_length); 1859 data_offset = le32_to_cpu(data_transfer->data_offset); 1860 1861 /* 1862 * The upper layer expects RFC1002 length at the 1863 * beginning of the payload. Return it to indicate 1864 * the total length of the packet. This minimize the 1865 * change to upper layer packet processing logic. This 1866 * will be eventually remove when an intermediate 1867 * transport layer is added 1868 */ 1869 if (response->first_segment && size == 4) { 1870 unsigned int rfc1002_len = 1871 data_length + remaining_data_length; 1872 __be32 rfc1002_hdr = cpu_to_be32(rfc1002_len); 1873 1874 if (copy_to_iter(&rfc1002_hdr, sizeof(rfc1002_hdr), 1875 &msg->msg_iter) != sizeof(rfc1002_hdr)) 1876 return -EFAULT; 1877 data_read = 4; 1878 response->first_segment = false; 1879 smbdirect_log_read(sc, SMBDIRECT_LOG_INFO, 1880 "returning rfc1002 length %d\n", 1881 rfc1002_len); 1882 goto read_rfc1002_done; 1883 } 1884 1885 to_copy = min_t(int, data_length - offset, to_read); 1886 if (copy_to_iter((u8 *)data_transfer + data_offset + offset, 1887 to_copy, &msg->msg_iter) != to_copy) 1888 return -EFAULT; 1889 1890 /* move on to the next buffer? */ 1891 if (to_copy == data_length - offset) { 1892 queue_length--; 1893 /* 1894 * No need to lock if we are not at the 1895 * end of the queue 1896 */ 1897 if (queue_length) 1898 list_del(&response->list); 1899 else { 1900 spin_lock_irqsave( 1901 &sc->recv_io.reassembly.lock, flags); 1902 list_del(&response->list); 1903 spin_unlock_irqrestore( 1904 &sc->recv_io.reassembly.lock, flags); 1905 } 1906 queue_removed++; 1907 sc->statistics.dequeue_reassembly_queue++; 1908 smbdirect_connection_put_recv_io(response); 1909 offset = 0; 1910 smbdirect_log_read(sc, SMBDIRECT_LOG_INFO, 1911 "smbdirect_connection_put_recv_io offset=0\n"); 1912 } else 1913 offset += to_copy; 1914 1915 to_read -= to_copy; 1916 data_read += to_copy; 1917 1918 smbdirect_log_read(sc, SMBDIRECT_LOG_INFO, 1919 "memcpy %d bytes len-ofs=%u => todo=%u done=%u ofs=%u\n", 1920 to_copy, data_length - offset, 1921 to_read, data_read, offset); 1922 } 1923 1924 spin_lock_irqsave(&sc->recv_io.reassembly.lock, flags); 1925 sc->recv_io.reassembly.data_length -= data_read; 1926 sc->recv_io.reassembly.queue_length -= queue_removed; 1927 spin_unlock_irqrestore(&sc->recv_io.reassembly.lock, flags); 1928 1929 sc->recv_io.reassembly.first_entry_offset = offset; 1930 smbdirect_log_read(sc, SMBDIRECT_LOG_INFO, 1931 "returning data_read=%d reassembly_length=%d first_ofs=%u\n", 1932 data_read, sc->recv_io.reassembly.data_length, 1933 sc->recv_io.reassembly.first_entry_offset); 1934 read_rfc1002_done: 1935 return data_read; 1936 } 1937 1938 smbdirect_log_read(sc, SMBDIRECT_LOG_INFO, 1939 "wait_event on more data\n"); 1940 ret = wait_event_interruptible(sc->recv_io.reassembly.wait_queue, 1941 sc->recv_io.reassembly.data_length >= size || 1942 sc->status != SMBDIRECT_SOCKET_CONNECTED); 1943 /* Don't return any data if interrupted */ 1944 if (ret) 1945 return ret; 1946 1947 goto again; 1948 } 1949 EXPORT_SYMBOL_GPL(smbdirect_connection_recvmsg); 1950 1951 static bool smbdirect_map_sges_single_page(struct smbdirect_map_sges *state, 1952 struct page *page, size_t off, size_t len) 1953 { 1954 struct ib_sge *sge; 1955 u64 addr; 1956 1957 if (state->num_sge >= state->max_sge) 1958 return false; 1959 1960 addr = ib_dma_map_page(state->device, page, 1961 off, len, state->direction); 1962 if (ib_dma_mapping_error(state->device, addr)) 1963 return false; 1964 1965 sge = &state->sge[state->num_sge++]; 1966 sge->addr = addr; 1967 sge->length = len; 1968 sge->lkey = state->local_dma_lkey; 1969 1970 return true; 1971 } 1972 1973 /* 1974 * Extract page fragments from a BVEC-class iterator and add them to an ib_sge 1975 * list. The pages are not pinned. 1976 */ 1977 static ssize_t smbdirect_map_sges_from_bvec(struct iov_iter *iter, 1978 struct smbdirect_map_sges *state, 1979 ssize_t maxsize) 1980 { 1981 const struct bio_vec *bv = iter->bvec; 1982 unsigned long start = iter->iov_offset; 1983 unsigned int i; 1984 ssize_t ret = 0; 1985 1986 for (i = 0; i < iter->nr_segs; i++) { 1987 size_t off, len; 1988 bool ok; 1989 1990 len = bv[i].bv_len; 1991 if (start >= len) { 1992 start -= len; 1993 continue; 1994 } 1995 1996 len = min_t(size_t, maxsize, len - start); 1997 off = bv[i].bv_offset + start; 1998 1999 ok = smbdirect_map_sges_single_page(state, 2000 bv[i].bv_page, 2001 off, 2002 len); 2003 if (!ok) 2004 return -EIO; 2005 2006 ret += len; 2007 maxsize -= len; 2008 if (state->num_sge >= state->max_sge || maxsize <= 0) 2009 break; 2010 start = 0; 2011 } 2012 2013 if (ret > 0) 2014 iov_iter_advance(iter, ret); 2015 return ret; 2016 } 2017 2018 /* 2019 * Extract fragments from a KVEC-class iterator and add them to an ib_sge list. 2020 * This can deal with vmalloc'd buffers as well as kmalloc'd or static buffers. 2021 * The pages are not pinned. 2022 */ 2023 static ssize_t smbdirect_map_sges_from_kvec(struct iov_iter *iter, 2024 struct smbdirect_map_sges *state, 2025 ssize_t maxsize) 2026 { 2027 const struct kvec *kv = iter->kvec; 2028 unsigned long start = iter->iov_offset; 2029 unsigned int i; 2030 ssize_t ret = 0; 2031 2032 for (i = 0; i < iter->nr_segs; i++) { 2033 struct page *page; 2034 unsigned long kaddr; 2035 size_t off, len, seg; 2036 2037 len = kv[i].iov_len; 2038 if (start >= len) { 2039 start -= len; 2040 continue; 2041 } 2042 2043 kaddr = (unsigned long)kv[i].iov_base + start; 2044 off = kaddr & ~PAGE_MASK; 2045 len = min_t(size_t, maxsize, len - start); 2046 kaddr &= PAGE_MASK; 2047 2048 maxsize -= len; 2049 do { 2050 bool ok; 2051 2052 seg = min_t(size_t, len, PAGE_SIZE - off); 2053 2054 if (is_vmalloc_or_module_addr((void *)kaddr)) 2055 page = vmalloc_to_page((void *)kaddr); 2056 else 2057 page = virt_to_page((void *)kaddr); 2058 2059 ok = smbdirect_map_sges_single_page(state, page, off, seg); 2060 if (!ok) 2061 return -EIO; 2062 2063 ret += seg; 2064 len -= seg; 2065 kaddr += PAGE_SIZE; 2066 off = 0; 2067 } while (len > 0 && state->num_sge < state->max_sge); 2068 2069 if (state->num_sge >= state->max_sge || maxsize <= 0) 2070 break; 2071 start = 0; 2072 } 2073 2074 if (ret > 0) 2075 iov_iter_advance(iter, ret); 2076 return ret; 2077 } 2078 2079 /* 2080 * Extract folio fragments from a FOLIOQ-class iterator and add them to an 2081 * ib_sge list. The folios are not pinned. 2082 */ 2083 static ssize_t smbdirect_map_sges_from_folioq(struct iov_iter *iter, 2084 struct smbdirect_map_sges *state, 2085 ssize_t maxsize) 2086 { 2087 const struct folio_queue *folioq = iter->folioq; 2088 unsigned int slot = iter->folioq_slot; 2089 ssize_t ret = 0; 2090 size_t offset = iter->iov_offset; 2091 2092 if (WARN_ON_ONCE(!folioq)) 2093 return -EIO; 2094 2095 if (slot >= folioq_nr_slots(folioq)) { 2096 folioq = folioq->next; 2097 if (WARN_ON_ONCE(!folioq)) 2098 return -EIO; 2099 slot = 0; 2100 } 2101 2102 do { 2103 struct folio *folio = folioq_folio(folioq, slot); 2104 size_t fsize = folioq_folio_size(folioq, slot); 2105 2106 if (offset < fsize) { 2107 size_t part = umin(maxsize, fsize - offset); 2108 bool ok; 2109 2110 ok = smbdirect_map_sges_single_page(state, 2111 folio_page(folio, 0), 2112 offset, 2113 part); 2114 if (!ok) 2115 return -EIO; 2116 2117 offset += part; 2118 ret += part; 2119 maxsize -= part; 2120 } 2121 2122 if (offset >= fsize) { 2123 offset = 0; 2124 slot++; 2125 if (slot >= folioq_nr_slots(folioq)) { 2126 if (!folioq->next) { 2127 WARN_ON_ONCE(ret < iter->count); 2128 break; 2129 } 2130 folioq = folioq->next; 2131 slot = 0; 2132 } 2133 } 2134 } while (state->num_sge < state->max_sge && maxsize > 0); 2135 2136 iter->folioq = folioq; 2137 iter->folioq_slot = slot; 2138 iter->iov_offset = offset; 2139 iter->count -= ret; 2140 return ret; 2141 } 2142 2143 /* 2144 * Extract page fragments from up to the given amount of the source iterator 2145 * and build up an ib_sge list that refers to all of those bits. The ib_sge list 2146 * is appended to, up to the maximum number of elements set in the parameter 2147 * block. 2148 * 2149 * The extracted page fragments are not pinned or ref'd in any way; if an 2150 * IOVEC/UBUF-type iterator is to be used, it should be converted to a 2151 * BVEC-type iterator and the pages pinned, ref'd or otherwise held in some 2152 * way. 2153 */ 2154 static ssize_t smbdirect_map_sges_from_iter(struct iov_iter *iter, size_t len, 2155 struct smbdirect_map_sges *state) 2156 { 2157 ssize_t ret; 2158 size_t before = state->num_sge; 2159 2160 if (WARN_ON_ONCE(iov_iter_rw(iter) != ITER_SOURCE)) 2161 return -EIO; 2162 2163 switch (iov_iter_type(iter)) { 2164 case ITER_BVEC: 2165 ret = smbdirect_map_sges_from_bvec(iter, state, len); 2166 break; 2167 case ITER_KVEC: 2168 ret = smbdirect_map_sges_from_kvec(iter, state, len); 2169 break; 2170 case ITER_FOLIOQ: 2171 ret = smbdirect_map_sges_from_folioq(iter, state, len); 2172 break; 2173 default: 2174 WARN_ONCE(1, "iov_iter_type[%u]\n", iov_iter_type(iter)); 2175 return -EIO; 2176 } 2177 2178 if (ret < 0) { 2179 while (state->num_sge > before) { 2180 struct ib_sge *sge = &state->sge[--state->num_sge]; 2181 2182 ib_dma_unmap_page(state->device, 2183 sge->addr, 2184 sge->length, 2185 state->direction); 2186 } 2187 } 2188 2189 return ret; 2190 } 2191