2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
6 * This file contains code imported from the OFED rds source file ib_send.c
7 * Oracle elects to have and use the contents of ib_send.c under and governed
8 * by the OpenIB.org BSD license (see below for full license text). However,
9 * the following notice accompanied the original version of this file:
13 * Copyright (c) 2006 Oracle. All rights reserved.
15 * This software is available to you under a choice of one of two
16 * licenses. You may choose to be licensed under the terms of the GNU
17 * General Public License (GPL) Version 2, available from the file
18 * COPYING in the main directory of this source tree, or the
19 * OpenIB.org BSD license below:
21 * Redistribution and use in source and binary forms, with or
22 * without modification, are permitted provided that the following
25 * - Redistributions of source code must retain the above
26 * copyright notice, this list of conditions and the following
29 * - Redistributions in binary form must reproduce the above
30 * copyright notice, this list of conditions and the following
31 * disclaimer in the documentation and/or other materials
32 * provided with the distribution.
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
35 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
36 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
37 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
38 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
39 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
40 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
46 #include <sys/ib/clients/rdsv3/rdsv3.h>
47 #include <sys/ib/clients/rdsv3/rdma.h>
48 #include <sys/ib/clients/rdsv3/ib.h>
49 #include <sys/ib/clients/rdsv3/rdsv3_debug.h>
52 rdsv3_ib_send_rdma_complete(struct rdsv3_message
*rm
,
57 RDSV3_DPRINTF4("rdsv3_ib_send_rdma_complete", "rm: %p, wc_status: %d",
61 case IBT_WC_WR_FLUSHED_ERR
:
65 notify_status
= RDS_RDMA_SUCCESS
;
68 case IBT_WC_REMOTE_ACCESS_ERR
:
69 notify_status
= RDS_RDMA_REMOTE_ERROR
;
73 notify_status
= RDS_RDMA_OTHER_ERROR
;
76 rdsv3_rdma_send_complete(rm
, notify_status
);
78 RDSV3_DPRINTF4("rdsv3_ib_send_rdma_complete", "rm: %p, wc_status: %d",
82 static void rdsv3_ib_dma_unmap_sg_rdma(struct ib_device
*dev
,
83 uint_t num
, struct rdsv3_rdma_sg scat
[]);
86 rdsv3_ib_send_unmap_rdma(struct rdsv3_ib_connection
*ic
,
87 struct rdsv3_rdma_op
*op
)
89 RDSV3_DPRINTF4("rdsv3_ib_send_unmap_rdma", "ic: %p, op: %p", ic
, op
);
93 rdsv3_ib_dma_unmap_sg_rdma(ic
->i_cm_id
->device
,
94 op
->r_nents
, op
->r_rdma_sg
);
96 rdsv3_ib_dma_unmap_sg_rdma(NULL
,
97 op
->r_nents
, op
->r_rdma_sg
);
103 rdsv3_ib_send_unmap_rm(struct rdsv3_ib_connection
*ic
,
104 struct rdsv3_ib_send_work
*send
,
107 struct rdsv3_message
*rm
= send
->s_rm
;
109 RDSV3_DPRINTF4("rdsv3_ib_send_unmap_rm", "ic %p send %p rm %p\n",
112 mutex_enter(&rm
->m_rs_lock
);
114 rdsv3_ib_dma_unmap_sg(ic
->i_cm_id
->device
,
115 rm
->m_sg
, rm
->m_count
);
118 mutex_exit(&rm
->m_rs_lock
);
120 if (rm
->m_rdma_op
!= NULL
) {
121 rdsv3_ib_send_unmap_rdma(ic
, rm
->m_rdma_op
);
124 * If the user asked for a completion notification on this
125 * message, we can implement three different semantics:
126 * 1. Notify when we received the ACK on the RDS message
127 * that was queued with the RDMA. This provides reliable
128 * notification of RDMA status at the expense of a one-way
130 * 2. Notify when the IB stack gives us the completion
131 * event for the RDMA operation.
132 * 3. Notify when the IB stack gives us the completion
133 * event for the accompanying RDS messages.
134 * Here, we implement approach #3. To implement approach #2,
135 * call rdsv3_rdma_send_complete from the cq_handler.
137 * don't call rdsv3_rdma_send_complete at all, and fall back to
139 * handling in the ACK processing code.
141 * Note: There's no need to explicitly sync any RDMA buffers
143 * ib_dma_sync_sg_for_cpu - the completion for the RDMA
144 * operation itself unmapped the RDMA buffers, which takes care
147 rdsv3_ib_send_rdma_complete(rm
, wc_status
);
149 if (rm
->m_rdma_op
->r_write
)
150 rdsv3_stats_add(s_send_rdma_bytes
,
151 rm
->m_rdma_op
->r_bytes
);
153 rdsv3_stats_add(s_recv_rdma_bytes
,
154 rm
->m_rdma_op
->r_bytes
);
158 * If anyone waited for this message to get flushed out, wake
161 rdsv3_message_unmapped(rm
);
163 rdsv3_message_put(rm
);
168 rdsv3_ib_send_init_ring(struct rdsv3_ib_connection
*ic
)
170 struct rdsv3_ib_send_work
*send
;
173 RDSV3_DPRINTF4("rdsv3_ib_send_init_ring", "ic: %p", ic
);
175 for (i
= 0, send
= ic
->i_sends
; i
< ic
->i_send_ring
.w_nr
; i
++, send
++) {
182 rdsv3_ib_send_clear_ring(struct rdsv3_ib_connection
*ic
)
184 struct rdsv3_ib_send_work
*send
;
187 RDSV3_DPRINTF4("rdsv3_ib_send_clear_ring", "ic: %p", ic
);
189 for (i
= 0, send
= ic
->i_sends
; i
< ic
->i_send_ring
.w_nr
; i
++, send
++) {
190 if (send
->s_opcode
== 0xdd)
193 rdsv3_ib_send_unmap_rm(ic
, send
, IBT_WC_WR_FLUSHED_ERR
);
195 rdsv3_ib_send_unmap_rdma(ic
, send
->s_op
);
198 RDSV3_DPRINTF4("rdsv3_ib_send_clear_ring", "Return: ic: %p", ic
);
202 * The _oldest/_free ring operations here race cleanly with the alloc/unalloc
203 * operations performed in the send path. As the sender allocs and potentially
204 * unallocs the next free entry in the ring it doesn't alter which is
205 * the next to be freed, which is what this is concerned with.
208 rdsv3_ib_send_cqe_handler(struct rdsv3_ib_connection
*ic
, ibt_wc_t
*wc
)
210 struct rdsv3_connection
*conn
= ic
->conn
;
211 struct rdsv3_ib_send_work
*send
;
212 uint32_t completed
, polled
;
217 RDSV3_DPRINTF4("rdsv3_ib_send_cqe_handler",
218 "wc wc_id 0x%llx status %u byte_len %u imm_data %u\n",
219 (unsigned long long)wc
->wc_id
, wc
->wc_status
,
220 wc
->wc_bytes_xfer
, ntohl(wc
->wc_immed_data
));
222 rdsv3_ib_stats_inc(s_ib_tx_cq_event
);
224 if (wc
->wc_id
== RDSV3_IB_ACK_WR_ID
) {
225 if (ic
->i_ack_queued
+ HZ
/2 < jiffies
)
226 rdsv3_ib_stats_inc(s_ib_tx_stalled
);
227 rdsv3_ib_ack_send_complete(ic
);
231 oldest
= rdsv3_ib_ring_oldest(&ic
->i_send_ring
);
233 completed
= rdsv3_ib_ring_completed(&ic
->i_send_ring
,
234 (wc
->wc_id
& ~RDSV3_IB_SEND_OP
), oldest
);
236 for (i
= 0; i
< completed
; i
++) {
237 send
= &ic
->i_sends
[oldest
];
240 * In the error case, wc->opcode sometimes contains
243 switch (send
->s_opcode
) {
246 rdsv3_ib_send_unmap_rm(ic
, send
,
252 * Nothing to be done - the SG list will
254 * when the SEND completes.
259 RDSV3_DPRINTF2("rdsv3_ib_send_cq_comp_handler",
260 "RDS/IB: %s: unexpected opcode "
262 __func__
, send
->s_opcode
);
267 send
->s_opcode
= 0xdd;
268 if (send
->s_queued
+ HZ
/2 < jiffies
)
269 rdsv3_ib_stats_inc(s_ib_tx_stalled
);
272 * If a RDMA operation produced an error, signal
274 * away. If we don't, the subsequent SEND that goes
276 * RDMA will be canceled with ERR_WFLUSH, and the
278 * never learn that the RDMA failed.
281 IBT_WC_REMOTE_ACCESS_ERR
&& send
->s_op
) {
282 struct rdsv3_message
*rm
;
284 rm
= rdsv3_send_get_message(conn
, send
->s_op
);
286 if (rm
->m_rdma_op
!= NULL
)
287 rdsv3_ib_send_unmap_rdma(ic
,
289 rdsv3_ib_send_rdma_complete(rm
,
291 rdsv3_message_put(rm
);
295 oldest
= (oldest
+ 1) % ic
->i_send_ring
.w_nr
;
298 rdsv3_ib_ring_free(&ic
->i_send_ring
, completed
);
300 clear_bit(RDSV3_LL_SEND_FULL
, &conn
->c_flags
);
302 /* We expect errors as the qp is drained during shutdown */
303 if (wc
->wc_status
!= IBT_WC_SUCCESS
&& rdsv3_conn_up(conn
)) {
304 RDSV3_DPRINTF2("rdsv3_ib_send_cqe_handler",
305 "send completion on %u.%u.%u.%u "
306 "had status %u, disconnecting and reconnecting\n",
307 NIPQUAD(conn
->c_faddr
), wc
->wc_status
);
308 rdsv3_conn_drop(conn
);
311 RDSV3_DPRINTF4("rdsv3_ib_send_cqe_handler", "Return: conn: %p", ic
);
315 * This is the main function for allocating credits when sending
318 * Conceptually, we have two counters:
319 * - send credits: this tells us how many WRs we're allowed
320 * to submit without overruning the reciever's queue. For
321 * each SEND WR we post, we decrement this by one.
323 * - posted credits: this tells us how many WRs we recently
324 * posted to the receive queue. This value is transferred
325 * to the peer as a "credit update" in a RDS header field.
326 * Every time we transmit credits to the peer, we subtract
327 * the amount of transferred credits from this counter.
329 * It is essential that we avoid situations where both sides have
330 * exhausted their send credits, and are unable to send new credits
331 * to the peer. We achieve this by requiring that we send at least
332 * one credit update to the peer before exhausting our credits.
333 * When new credits arrive, we subtract one credit that is withheld
334 * until we've posted new buffers and are ready to transmit these
335 * credits (see rdsv3_ib_send_add_credits below).
337 * The RDS send code is essentially single-threaded; rdsv3_send_xmit
338 * grabs c_send_lock to ensure exclusive access to the send ring.
339 * However, the ACK sending code is independent and can race with
342 * In the send path, we need to update the counters for send credits
343 * and the counter of posted buffers atomically - when we use the
344 * last available credit, we cannot allow another thread to race us
345 * and grab the posted credits counter. Hence, we have to use a
346 * spinlock to protect the credit counter, or use atomics.
348 * Spinlocks shared between the send and the receive path are bad,
349 * because they create unnecessary delays. An early implementation
350 * using a spinlock showed a 5% degradation in throughput at some
353 * This implementation avoids spinlocks completely, putting both
354 * counters into a single atomic, and updating that atomic using
355 * atomic_add (in the receive path, when receiving fresh credits),
356 * and using atomic_cmpxchg when updating the two counters.
359 rdsv3_ib_send_grab_credits(struct rdsv3_ib_connection
*ic
,
360 uint32_t wanted
, uint32_t *adv_credits
, int need_posted
)
362 unsigned int avail
, posted
, got
= 0, advertise
;
365 RDSV3_DPRINTF4("rdsv3_ib_send_grab_credits", "ic: %p, %d %d %d",
366 ic
, wanted
, *adv_credits
, need_posted
);
374 oldval
= newval
= atomic_get(&ic
->i_credits
);
375 posted
= IB_GET_POST_CREDITS(oldval
);
376 avail
= IB_GET_SEND_CREDITS(oldval
);
378 RDSV3_DPRINTF5("rdsv3_ib_send_grab_credits",
379 "wanted (%u): credits=%u posted=%u\n", wanted
, avail
, posted
);
381 /* The last credit must be used to send a credit update. */
382 if (avail
&& !posted
)
385 if (avail
< wanted
) {
386 struct rdsv3_connection
*conn
= ic
->i_cm_id
->context
;
388 /* Oops, there aren't that many credits left! */
389 set_bit(RDSV3_LL_SEND_FULL
, &conn
->c_flags
);
392 /* Sometimes you get what you want, lalala. */
395 newval
-= IB_SET_SEND_CREDITS(got
);
398 * If need_posted is non-zero, then the caller wants
399 * the posted regardless of whether any send credits are
402 if (posted
&& (got
|| need_posted
)) {
403 advertise
= min(posted
, RDSV3_MAX_ADV_CREDIT
);
404 newval
-= IB_SET_POST_CREDITS(advertise
);
407 /* Finally bill everything */
408 if (atomic_cmpxchg(&ic
->i_credits
, oldval
, newval
) != oldval
)
411 *adv_credits
= advertise
;
413 RDSV3_DPRINTF4("rdsv3_ib_send_grab_credits", "ic: %p, %d %d %d",
414 ic
, got
, *adv_credits
, need_posted
);
420 rdsv3_ib_send_add_credits(struct rdsv3_connection
*conn
, unsigned int credits
)
422 struct rdsv3_ib_connection
*ic
= conn
->c_transport_data
;
427 RDSV3_DPRINTF5("rdsv3_ib_send_add_credits",
428 "credits (%u): current=%u%s\n",
430 IB_GET_SEND_CREDITS(atomic_get(&ic
->i_credits
)),
431 test_bit(RDSV3_LL_SEND_FULL
, &conn
->c_flags
) ?
432 ", ll_send_full" : "");
434 atomic_add_32(&ic
->i_credits
, IB_SET_SEND_CREDITS(credits
));
435 if (test_and_clear_bit(RDSV3_LL_SEND_FULL
, &conn
->c_flags
))
436 rdsv3_queue_delayed_work(rdsv3_wq
, &conn
->c_send_w
, 0);
438 ASSERT(!(IB_GET_SEND_CREDITS(credits
) >= 16384));
440 rdsv3_ib_stats_inc(s_ib_rx_credit_updates
);
442 RDSV3_DPRINTF4("rdsv3_ib_send_add_credits",
443 "Return: conn: %p, credits: %d",
448 rdsv3_ib_advertise_credits(struct rdsv3_connection
*conn
, unsigned int posted
)
450 struct rdsv3_ib_connection
*ic
= conn
->c_transport_data
;
452 RDSV3_DPRINTF4("rdsv3_ib_advertise_credits", "conn: %p, posted: %d",
458 atomic_add_32(&ic
->i_credits
, IB_SET_POST_CREDITS(posted
));
461 * Decide whether to send an update to the peer now.
462 * If we would send a credit update for every single buffer we
463 * post, we would end up with an ACK storm (ACK arrives,
464 * consumes buffer, we refill the ring, send ACK to remote
465 * advertising the newly posted buffer... ad inf)
467 * Performance pretty much depends on how often we send
468 * credit updates - too frequent updates mean lots of ACKs.
469 * Too infrequent updates, and the peer will run out of
470 * credits and has to throttle.
471 * For the time being, 16 seems to be a good compromise.
473 if (IB_GET_POST_CREDITS(atomic_get(&ic
->i_credits
)) >= 16)
474 set_bit(IB_ACK_REQUESTED
, &ic
->i_ack_flags
);
478 rdsv3_ib_xmit_populate_wr(struct rdsv3_ib_connection
*ic
,
479 ibt_send_wr_t
*wr
, unsigned int pos
,
480 struct rdsv3_scatterlist
*scat
, unsigned int off
, unsigned int length
,
485 RDSV3_DPRINTF4("rdsv3_ib_xmit_populate_wr",
486 "ic: %p, wr: %p scat: %p %d %d %d %d",
487 ic
, wr
, scat
, pos
, off
, length
, send_flags
);
489 wr
->wr_id
= pos
| RDSV3_IB_SEND_OP
;
490 wr
->wr_trans
= IBT_RC_SRV
;
491 wr
->wr_flags
= send_flags
;
492 wr
->wr_opcode
= IBT_WRC_SEND
;
495 int ix
, len
, assigned
;
498 ASSERT(length
<= scat
->length
- off
);
502 /* find the right sgl to begin with */
503 while (sgl
->ds_len
<= off
) {
509 ix
= 1; /* first data sgl is at 1 */
513 sge
= &wr
->wr_sgl
[ix
++];
514 sge
->ds_va
= sgl
->ds_va
+ off
;
515 assigned
= min(len
, sgl
->ds_len
- off
);
516 sge
->ds_len
= assigned
;
517 sge
->ds_key
= sgl
->ds_key
;
528 * We're sending a packet with no payload. There is only
534 sge
= &wr
->wr_sgl
[0];
535 sge
->ds_va
= ic
->i_send_hdrs_dma
+ (pos
* sizeof (struct rdsv3_header
));
536 sge
->ds_len
= sizeof (struct rdsv3_header
);
537 sge
->ds_key
= ic
->i_mr
->lkey
;
539 RDSV3_DPRINTF4("rdsv3_ib_xmit_populate_wr",
540 "Return: ic: %p, wr: %p scat: %p", ic
, wr
, scat
);
544 * This can be called multiple times for a given message. The first time
545 * we see a message we map its scatterlist into the IB device so that
546 * we can provide that mapped address to the IB scatter gather entries
547 * in the IB work requests. We translate the scatterlist into a series
548 * of work requests that fragment the message. These work requests complete
549 * in order so we pass ownership of the message to the completion handler
550 * once we send the final fragment.
552 * The RDS core uses the c_send_lock to only enter this function once
553 * per connection. This makes sure that the tx ring alloc/unalloc pairs
554 * don't get out of sync and confuse the ring.
557 rdsv3_ib_xmit(struct rdsv3_connection
*conn
, struct rdsv3_message
*rm
,
558 unsigned int hdr_off
, unsigned int sg
, unsigned int off
)
560 struct rdsv3_ib_connection
*ic
= conn
->c_transport_data
;
561 struct ib_device
*dev
= ic
->i_cm_id
->device
;
562 struct rdsv3_ib_send_work
*send
= NULL
;
563 struct rdsv3_ib_send_work
*first
;
564 struct rdsv3_ib_send_work
*prev
;
566 struct rdsv3_scatterlist
*scat
;
570 uint32_t credit_alloc
;
572 uint32_t adv_credits
= 0;
576 int flow_controlled
= 0;
578 RDSV3_DPRINTF4("rdsv3_ib_xmit", "conn: %p, rm: %p", conn
, rm
);
580 ASSERT(!(off
% RDSV3_FRAG_SIZE
));
581 ASSERT(!(hdr_off
!= 0 && hdr_off
!= sizeof (struct rdsv3_header
)));
583 /* Do not send cong updates to IB loopback */
584 if (conn
->c_loopback
&&
585 rm
->m_inc
.i_hdr
.h_flags
& RDSV3_FLAG_CONG_BITMAP
) {
586 rdsv3_cong_map_updated(conn
->c_fcong
, ~(uint64_t)0);
587 return (sizeof (struct rdsv3_header
) + RDSV3_CONG_MAP_BYTES
);
591 /* FIXME we may overallocate here */
592 if (ntohl(rm
->m_inc
.i_hdr
.h_len
) == 0)
595 i
= ceil(ntohl(rm
->m_inc
.i_hdr
.h_len
), RDSV3_FRAG_SIZE
);
598 work_alloc
= rdsv3_ib_ring_alloc(&ic
->i_send_ring
, i
, &pos
);
599 if (work_alloc
!= i
) {
600 rdsv3_ib_ring_unalloc(&ic
->i_send_ring
, work_alloc
);
601 set_bit(RDSV3_LL_SEND_FULL
, &conn
->c_flags
);
602 rdsv3_ib_stats_inc(s_ib_tx_ring_full
);
607 credit_alloc
= work_alloc
;
609 credit_alloc
= rdsv3_ib_send_grab_credits(ic
, work_alloc
,
611 adv_credits
+= posted
;
612 if (credit_alloc
< work_alloc
) {
613 rdsv3_ib_ring_unalloc(&ic
->i_send_ring
,
614 work_alloc
- credit_alloc
);
615 work_alloc
= credit_alloc
;
618 if (work_alloc
== 0) {
619 rdsv3_ib_ring_unalloc(&ic
->i_send_ring
, work_alloc
);
620 rdsv3_ib_stats_inc(s_ib_tx_throttle
);
626 /* map the message the first time we see it */
627 if (ic
->i_rm
== NULL
) {
630 * "rdsv3_ib_xmit prep msg dport=%u flags=0x%x len=%d\n",
631 * be16_to_cpu(rm->m_inc.i_hdr.h_dport),
632 * rm->m_inc.i_hdr.h_flags,
633 * be32_to_cpu(rm->m_inc.i_hdr.h_len));
636 rm
->m_count
= rdsv3_ib_dma_map_sg(dev
,
637 rm
->m_sg
, rm
->m_nents
);
638 RDSV3_DPRINTF5("rdsv3_ib_xmit",
639 "ic %p mapping rm %p: %d\n", ic
, rm
, rm
->m_count
);
640 if (rm
->m_count
== 0) {
641 rdsv3_ib_stats_inc(s_ib_tx_sg_mapping_failure
);
642 rdsv3_ib_ring_unalloc(&ic
->i_send_ring
,
644 ret
= -ENOMEM
; /* XXX ? */
645 RDSV3_DPRINTF2("rdsv3_ib_xmit",
646 "fail: ic %p mapping rm %p: %d\n",
647 ic
, rm
, rm
->m_count
);
654 ic
->i_unsignaled_wrs
= rdsv3_ib_sysctl_max_unsig_wrs
;
655 ic
->i_unsignaled_bytes
= rdsv3_ib_sysctl_max_unsig_bytes
;
656 rdsv3_message_addref(rm
);
659 /* Finalize the header */
660 if (test_bit(RDSV3_MSG_ACK_REQUIRED
, &rm
->m_flags
))
661 rm
->m_inc
.i_hdr
.h_flags
|= RDSV3_FLAG_ACK_REQUIRED
;
662 if (test_bit(RDSV3_MSG_RETRANSMITTED
, &rm
->m_flags
))
663 rm
->m_inc
.i_hdr
.h_flags
|= RDSV3_FLAG_RETRANSMITTED
;
666 * If it has a RDMA op, tell the peer we did it. This is
667 * used by the peer to release use-once RDMA MRs.
670 struct rdsv3_ext_header_rdma ext_hdr
;
672 ext_hdr
.h_rdma_rkey
= htonl(rm
->m_rdma_op
->r_key
);
673 (void) rdsv3_message_add_extension(&rm
->m_inc
.i_hdr
,
674 RDSV3_EXTHDR_RDMA
, &ext_hdr
,
677 if (rm
->m_rdma_cookie
) {
678 (void) rdsv3_message_add_rdma_dest_extension(
680 rdsv3_rdma_cookie_key(rm
->m_rdma_cookie
),
681 rdsv3_rdma_cookie_offset(rm
->m_rdma_cookie
));
685 * Note - rdsv3_ib_piggyb_ack clears the ACK_REQUIRED bit, so
686 * we should not do this unless we have a chance of at least
687 * sticking the header into the send ring. Which is why we
688 * should call rdsv3_ib_ring_alloc first.
690 rm
->m_inc
.i_hdr
.h_ack
= htonll(rdsv3_ib_piggyb_ack(ic
));
691 rdsv3_message_make_checksum(&rm
->m_inc
.i_hdr
);
694 * Update adv_credits since we reset the ACK_REQUIRED bit.
696 (void) rdsv3_ib_send_grab_credits(ic
, 0, &posted
, 1);
697 adv_credits
+= posted
;
698 ASSERT(adv_credits
<= 255);
701 send
= &ic
->i_sends
[pos
];
704 scat
= &rm
->m_sg
[sg
];
709 * Sometimes you want to put a fence between an RDMA
710 * READ and the following SEND.
711 * We could either do this all the time
712 * or when requested by the user. Right now, we let
713 * the application choose.
715 if (rm
->m_rdma_op
&& rm
->m_rdma_op
->r_fence
)
716 send_flags
= IBT_WR_SEND_FENCE
;
719 * We could be copying the header into the unused tail of the page.
720 * That would need to be changed in the future when those pages might
721 * be mapped userspace pages or page cache pages. So instead we always
722 * use a second sge and our long-lived ring of mapped headers. We send
723 * the header after the data so that the data payload can be aligned on
727 /* handle a 0-len message */
728 if (ntohl(rm
->m_inc
.i_hdr
.h_len
) == 0) {
729 wr
= &ic
->i_send_wrs
[0];
730 rdsv3_ib_xmit_populate_wr(ic
, wr
, pos
, NULL
, 0, 0, send_flags
);
731 send
->s_queued
= jiffies
;
733 send
->s_opcode
= wr
->wr_opcode
;
737 /* if there's data reference it with a chain of work reqs */
738 for (; i
< work_alloc
&& scat
!= &rm
->m_sg
[rm
->m_count
]; i
++) {
741 send
= &ic
->i_sends
[pos
];
743 wr
= &ic
->i_send_wrs
[i
];
744 len
= min(RDSV3_FRAG_SIZE
,
745 rdsv3_ib_sg_dma_len(dev
, scat
) - off
);
746 rdsv3_ib_xmit_populate_wr(ic
, wr
, pos
, scat
, off
, len
,
748 send
->s_queued
= jiffies
;
750 send
->s_opcode
= wr
->wr_opcode
;
753 * We want to delay signaling completions just enough to get
754 * the batching benefits but not so much that we create dead
758 if (ic
->i_unsignaled_wrs
-- == 0) {
759 ic
->i_unsignaled_wrs
= rdsv3_ib_sysctl_max_unsig_wrs
;
761 IBT_WR_SEND_SIGNAL
| IBT_WR_SEND_SOLICIT
;
764 ic
->i_unsignaled_bytes
-= len
;
765 if (ic
->i_unsignaled_bytes
<= 0) {
766 ic
->i_unsignaled_bytes
=
767 rdsv3_ib_sysctl_max_unsig_bytes
;
769 IBT_WR_SEND_SIGNAL
| IBT_WR_SEND_SOLICIT
;
773 * Always signal the last one if we're stopping due to flow
776 if (flow_controlled
&& i
== (work_alloc
-1)) {
778 IBT_WR_SEND_SIGNAL
| IBT_WR_SEND_SOLICIT
;
781 RDSV3_DPRINTF5("rdsv3_ib_xmit", "send %p wr %p num_sge %u \n",
782 send
, wr
, wr
->wr_nds
);
786 if (off
== rdsv3_ib_sg_dma_len(dev
, scat
)) {
793 * Tack on the header after the data. The header SGE
795 * have been set up to point to the right header buffer.
797 (void) memcpy(&ic
->i_send_hdrs
[pos
], &rm
->m_inc
.i_hdr
,
798 sizeof (struct rdsv3_header
));
801 struct rdsv3_header
*hdr
= &ic
->i_send_hdrs
[pos
];
803 RDSV3_DPRINTF2("rdsv3_ib_xmit",
804 "send WR dport=%u flags=0x%x len=%d",
810 struct rdsv3_header
*hdr
= &ic
->i_send_hdrs
[pos
];
812 /* add credit and redo the header checksum */
813 hdr
->h_credit
= adv_credits
;
814 rdsv3_message_make_checksum(hdr
);
816 rdsv3_ib_stats_inc(s_ib_tx_credit_updates
);
821 pos
= (pos
+ 1) % ic
->i_send_ring
.w_nr
;
825 * Account the RDS header in the number of bytes we sent, but just once.
826 * The caller has no concept of fragmentation.
829 sent
+= sizeof (struct rdsv3_header
);
831 /* if we finished the message then send completion owns it */
832 if (scat
== &rm
->m_sg
[rm
->m_count
]) {
833 prev
->s_rm
= ic
->i_rm
;
834 wr
->wr_flags
|= IBT_WR_SEND_SIGNAL
| IBT_WR_SEND_SOLICIT
;
838 if (i
< work_alloc
) {
839 rdsv3_ib_ring_unalloc(&ic
->i_send_ring
, work_alloc
- i
);
842 if (ic
->i_flowctl
&& i
< credit_alloc
)
843 rdsv3_ib_send_add_credits(conn
, credit_alloc
- i
);
845 /* XXX need to worry about failed_wr and partial sends. */
846 ret
= ibt_post_send(ib_get_ibt_channel_hdl(ic
->i_cm_id
),
847 ic
->i_send_wrs
, i
, &posted
);
849 RDSV3_DPRINTF2("rdsv3_ib_xmit",
850 "ic %p first %p nwr: %d ret %d:%d",
851 ic
, first
, i
, ret
, posted
);
854 RDSV3_DPRINTF2("rdsv3_ib_xmit",
855 "RDS/IB: ib_post_send to %u.%u.%u.%u "
856 "returned %d\n", NIPQUAD(conn
->c_faddr
), ret
);
857 rdsv3_ib_ring_unalloc(&ic
->i_send_ring
, work_alloc
);
859 ic
->i_rm
= prev
->s_rm
;
862 RDSV3_DPRINTF2("rdsv3_ib_xmit", "ibt_post_send failed\n");
863 rdsv3_conn_drop(ic
->conn
);
870 RDSV3_DPRINTF4("rdsv3_ib_xmit", "Return: conn: %p, rm: %p", conn
, rm
);
872 ASSERT(!adv_credits
);
877 rdsv3_ib_dma_unmap_sg_rdma(struct ib_device
*dev
, uint_t num
,
878 struct rdsv3_rdma_sg scat
[])
880 ibt_hca_hdl_t hca_hdl
;
884 RDSV3_DPRINTF4("rdsv3_ib_dma_unmap_sg", "rdma_sg: %p", scat
);
887 hca_hdl
= ib_get_ibt_hca_hdl(dev
);
889 hca_hdl
= scat
[0].hca_hdl
;
890 RDSV3_DPRINTF2("rdsv3_ib_dma_unmap_sg_rdma",
891 "NULL dev use cached hca_hdl %p", hca_hdl
);
896 scat
[0].hca_hdl
= NULL
;
898 for (i
= 0; i
< num
; i
++) {
899 if (scat
[i
].mihdl
!= NULL
) {
900 num_sgl
= (scat
[i
].iovec
.bytes
/ PAGESIZE
) + 2;
901 kmem_free(scat
[i
].swr
.wr_sgl
,
902 (num_sgl
* sizeof (ibt_wr_ds_t
)));
903 scat
[i
].swr
.wr_sgl
= NULL
;
904 (void) ibt_unmap_mem_iov(hca_hdl
, scat
[i
].mihdl
);
905 scat
[i
].mihdl
= NULL
;
913 rdsv3_ib_dma_map_sg_rdma(struct ib_device
*dev
, struct rdsv3_rdma_sg scat
[],
914 uint_t num
, struct rdsv3_scatterlist
**scatl
)
916 ibt_hca_hdl_t hca_hdl
;
917 ibt_iov_attr_t iov_attr
;
921 struct rdsv3_scatterlist
*sg
;
924 RDSV3_DPRINTF4("rdsv3_ib_dma_map_sg_rdma", "scat: %p, num: %d",
927 hca_hdl
= ib_get_ibt_hca_hdl(dev
);
928 scat
[0].hca_hdl
= hca_hdl
;
929 bzero(&iov_attr
, sizeof (ibt_iov_attr_t
));
930 iov_attr
.iov_flags
= IBT_IOV_BUF
;
931 iov_attr
.iov_lso_hdr_sz
= 0;
933 for (i
= 0, count
= 0; i
< num
; i
++) {
934 /* transpose umem_cookie to buf structure */
935 bp
= ddi_umem_iosetup(scat
[i
].umem_cookie
,
936 scat
[i
].iovec
.addr
& PAGEOFFSET
, scat
[i
].iovec
.bytes
,
937 B_WRITE
, 0, 0, NULL
, DDI_UMEM_SLEEP
);
939 /* free resources and return error */
942 /* setup ibt_map_mem_iov() attributes */
943 iov_attr
.iov_buf
= bp
;
944 iov_attr
.iov_wr_nds
= (scat
[i
].iovec
.bytes
/ PAGESIZE
) + 2;
946 kmem_zalloc(iov_attr
.iov_wr_nds
* sizeof (ibt_wr_ds_t
),
949 ret
= ibt_map_mem_iov(hca_hdl
, &iov_attr
,
950 (ibt_all_wr_t
*)&scat
[i
].swr
, &scat
[i
].mihdl
);
952 if (ret
!= IBT_SUCCESS
) {
953 RDSV3_DPRINTF2("rdsv3_ib_dma_map_sg_rdma",
954 "ibt_map_mem_iov returned: %d", ret
);
955 /* free resources and return error */
956 kmem_free(scat
[i
].swr
.wr_sgl
,
957 iov_attr
.iov_wr_nds
* sizeof (ibt_wr_ds_t
));
960 count
+= scat
[i
].swr
.wr_nds
;
963 for (j
= 0; j
< scat
[i
].swr
.wr_nds
; j
++) {
964 RDSV3_DPRINTF5("rdsv3_ib_dma_map_sg_rdma",
965 "sgl[%d] va %llx len %x", j
,
966 scat
[i
].swr
.wr_sgl
[j
].ds_va
,
967 scat
[i
].swr
.wr_sgl
[j
].ds_len
);
970 RDSV3_DPRINTF4("rdsv3_ib_dma_map_sg_rdma",
971 "iovec.bytes: 0x%x scat[%d]swr.wr_nds: %d",
972 scat
[i
].iovec
.bytes
, i
, scat
[i
].swr
.wr_nds
);
975 count
= ((count
- 1) / RDSV3_IB_MAX_SGE
) + 1;
976 RDSV3_DPRINTF4("rdsv3_ib_dma_map_sg_rdma", "Ret: num: %d", count
);
980 rdsv3_ib_dma_unmap_sg_rdma(dev
, num
, scat
);
985 rdsv3_ib_xmit_rdma(struct rdsv3_connection
*conn
, struct rdsv3_rdma_op
*op
)
987 struct rdsv3_ib_connection
*ic
= conn
->c_transport_data
;
988 struct rdsv3_ib_send_work
*send
= NULL
;
989 struct rdsv3_rdma_sg
*scat
;
990 uint64_t remote_addr
;
993 uint32_t i
, j
, k
, idx
;
994 uint32_t left
, count
;
1001 RDSV3_DPRINTF4("rdsv3_ib_xmit_rdma", "rdsv3_ib_conn: %p", ic
);
1003 /* map the message the first time we see it */
1004 if (!op
->r_mapped
) {
1005 op
->r_count
= rdsv3_ib_dma_map_sg_rdma(ic
->i_cm_id
->device
,
1006 op
->r_rdma_sg
, op
->r_nents
, &op
->r_sg
);
1007 RDSV3_DPRINTF5("rdsv3_ib_xmit_rdma", "ic %p mapping op %p: %d",
1008 ic
, op
, op
->r_count
);
1009 if (op
->r_count
== 0) {
1010 rdsv3_ib_stats_inc(s_ib_tx_sg_mapping_failure
);
1011 RDSV3_DPRINTF2("rdsv3_ib_xmit_rdma",
1012 "fail: ic %p mapping op %p: %d",
1013 ic
, op
, op
->r_count
);
1014 return (-ENOMEM
); /* XXX ? */
1020 * Instead of knowing how to return a partial rdma read/write
1021 * we insist that there
1022 * be enough work requests to send the entire message.
1024 work_alloc
= rdsv3_ib_ring_alloc(&ic
->i_send_ring
, op
->r_count
, &pos
);
1025 if (work_alloc
!= op
->r_count
) {
1026 rdsv3_ib_ring_unalloc(&ic
->i_send_ring
, work_alloc
);
1027 rdsv3_ib_stats_inc(s_ib_tx_ring_full
);
1031 RDSV3_DPRINTF4("rdsv3_ib_xmit_rdma", "pos %u cnt %u", pos
, op
->r_count
);
1033 * take the scatter list and transpose into a list of
1034 * send wr's each with a scatter list of RDSV3_IB_MAX_SGE
1036 scat
= &op
->r_rdma_sg
[0];
1038 remote_addr
= op
->r_remote_addr
;
1040 for (i
= 0, k
= 0; i
< op
->r_nents
; i
++) {
1041 left
= scat
[i
].swr
.wr_nds
;
1042 for (idx
= 0; left
> 0; k
++) {
1043 send
= &ic
->i_sends
[pos
];
1044 send
->s_queued
= jiffies
;
1045 send
->s_opcode
= op
->r_write
? IBT_WRC_RDMAW
:
1049 wr
= &ic
->i_send_wrs
[k
];
1051 wr
->wr_id
= pos
| RDSV3_IB_SEND_OP
;
1052 wr
->wr_trans
= IBT_RC_SRV
;
1053 wr
->wr_opcode
= op
->r_write
? IBT_WRC_RDMAW
:
1055 wr
->wr
.rc
.rcwr
.rdma
.rdma_raddr
= remote_addr
;
1056 wr
->wr
.rc
.rcwr
.rdma
.rdma_rkey
= op
->r_key
;
1058 if (left
> RDSV3_IB_MAX_SGE
) {
1059 count
= RDSV3_IB_MAX_SGE
;
1060 left
-= RDSV3_IB_MAX_SGE
;
1067 for (j
= 0; j
< count
; j
++) {
1068 sge
= &wr
->wr_sgl
[j
];
1069 *sge
= scat
[i
].swr
.wr_sgl
[idx
];
1070 remote_addr
+= scat
[i
].swr
.wr_sgl
[idx
].ds_len
;
1071 sent
+= scat
[i
].swr
.wr_sgl
[idx
].ds_len
;
1073 RDSV3_DPRINTF5("xmit_rdma",
1074 "send_wrs[%d]sgl[%d] va %llx len %x",
1075 k
, j
, sge
->ds_va
, sge
->ds_len
);
1077 RDSV3_DPRINTF5("rdsv3_ib_xmit_rdma",
1078 "wr[%d] %p key: %x code: %d tlen: %d",
1079 k
, wr
, wr
->wr
.rc
.rcwr
.rdma
.rdma_rkey
,
1080 wr
->wr_opcode
, sent
);
1083 * We want to delay signaling completions just enough
1084 * to get the batching benefits but not so much that
1085 * we create dead time on the wire.
1087 if (ic
->i_unsignaled_wrs
-- == 0) {
1088 ic
->i_unsignaled_wrs
=
1089 rdsv3_ib_sysctl_max_unsig_wrs
;
1090 wr
->wr_flags
= IBT_WR_SEND_SIGNAL
;
1093 pos
= (pos
+ 1) % ic
->i_send_ring
.w_nr
;
1097 status
= ibt_post_send(ib_get_ibt_channel_hdl(ic
->i_cm_id
),
1098 ic
->i_send_wrs
, k
, &posted
);
1099 if (status
!= IBT_SUCCESS
) {
1100 RDSV3_DPRINTF2("rdsv3_ib_xmit_rdma",
1101 "RDS/IB: rdma ib_post_send to %u.%u.%u.%u "
1102 "returned %d", NIPQUAD(conn
->c_faddr
), status
);
1103 rdsv3_ib_ring_unalloc(&ic
->i_send_ring
, work_alloc
);
1105 RDSV3_DPRINTF4("rdsv3_ib_xmit_rdma", "Ret: %p", ic
);
1110 rdsv3_ib_xmit_complete(struct rdsv3_connection
*conn
)
1112 struct rdsv3_ib_connection
*ic
= conn
->c_transport_data
;
1114 RDSV3_DPRINTF4("rdsv3_ib_xmit_complete", "conn: %p", conn
);
1117 * We may have a pending ACK or window update we were unable
1118 * to send previously (due to flow control). Try again.
1120 rdsv3_ib_attempt_ack(ic
);