RDS: cancel connection work structs as we shut down
[deliverable/linux.git] / net / rds / ib_send.c
CommitLineData
6a0979df
AG
1/*
2 * Copyright (c) 2006 Oracle. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 */
33#include <linux/kernel.h>
34#include <linux/in.h>
35#include <linux/device.h>
36#include <linux/dmapool.h>
37
38#include "rds.h"
6a0979df
AG
39#include "ib.h"
40
9c030391
AG
41/*
42 * Convert IB-specific error message to RDS error message and call core
43 * completion handler.
44 */
45static void rds_ib_send_complete(struct rds_message *rm,
46 int wc_status,
47 void (*complete)(struct rds_message *rm, int status))
6a0979df
AG
48{
49 int notify_status;
50
51 switch (wc_status) {
52 case IB_WC_WR_FLUSH_ERR:
53 return;
54
55 case IB_WC_SUCCESS:
56 notify_status = RDS_RDMA_SUCCESS;
57 break;
58
59 case IB_WC_REM_ACCESS_ERR:
60 notify_status = RDS_RDMA_REMOTE_ERROR;
61 break;
62
63 default:
64 notify_status = RDS_RDMA_OTHER_ERROR;
65 break;
66 }
9c030391 67 complete(rm, notify_status);
6a0979df
AG
68}
69
ff3d7d36
AG
70static void rds_ib_send_unmap_data(struct rds_ib_connection *ic,
71 struct rm_data_op *op,
72 int wc_status)
6a0979df 73{
ff3d7d36
AG
74 if (op->op_nents)
75 ib_dma_unmap_sg(ic->i_cm_id->device,
76 op->op_sg, op->op_nents,
77 DMA_TO_DEVICE);
78}
6a0979df 79
ff3d7d36
AG
80static void rds_ib_send_unmap_rdma(struct rds_ib_connection *ic,
81 struct rm_rdma_op *op,
82 int wc_status)
83{
84 if (op->op_mapped) {
85 ib_dma_unmap_sg(ic->i_cm_id->device,
86 op->op_sg, op->op_nents,
87 op->op_write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
88 op->op_mapped = 0;
89 }
15133f6e 90
ff3d7d36
AG
91 /* If the user asked for a completion notification on this
92 * message, we can implement three different semantics:
93 * 1. Notify when we received the ACK on the RDS message
94 * that was queued with the RDMA. This provides reliable
95 * notification of RDMA status at the expense of a one-way
96 * packet delay.
97 * 2. Notify when the IB stack gives us the completion event for
98 * the RDMA operation.
99 * 3. Notify when the IB stack gives us the completion event for
100 * the accompanying RDS messages.
101 * Here, we implement approach #3. To implement approach #2,
102 * we would need to take an event for the rdma WR. To implement #1,
103 * don't call rds_rdma_send_complete at all, and fall back to the notify
104 * handling in the ACK processing code.
105 *
106 * Note: There's no need to explicitly sync any RDMA buffers using
107 * ib_dma_sync_sg_for_cpu - the completion for the RDMA
108 * operation itself unmapped the RDMA buffers, which takes care
109 * of synching.
110 */
111 rds_ib_send_complete(container_of(op, struct rds_message, rdma),
112 wc_status, rds_rdma_send_complete);
6a0979df 113
ff3d7d36
AG
114 if (op->op_write)
115 rds_stats_add(s_send_rdma_bytes, op->op_bytes);
116 else
117 rds_stats_add(s_recv_rdma_bytes, op->op_bytes);
118}
6a0979df 119
ff3d7d36
AG
120static void rds_ib_send_unmap_atomic(struct rds_ib_connection *ic,
121 struct rm_atomic_op *op,
122 int wc_status)
123{
124 /* unmap atomic recvbuf */
125 if (op->op_mapped) {
126 ib_dma_unmap_sg(ic->i_cm_id->device, op->op_sg, 1,
127 DMA_FROM_DEVICE);
128 op->op_mapped = 0;
6a0979df
AG
129 }
130
ff3d7d36
AG
131 rds_ib_send_complete(container_of(op, struct rds_message, atomic),
132 wc_status, rds_atomic_send_complete);
15133f6e 133
ff3d7d36 134 if (op->op_type == RDS_ATOMIC_TYPE_CSWP)
51e2cba8 135 rds_ib_stats_inc(s_ib_atomic_cswp);
ff3d7d36 136 else
51e2cba8 137 rds_ib_stats_inc(s_ib_atomic_fadd);
ff3d7d36 138}
15133f6e 139
ff3d7d36
AG
140/*
141 * Unmap the resources associated with a struct send_work.
142 *
143 * Returns the rm for no good reason other than it is unobtainable
144 * other than by switching on wr.opcode, currently, and the caller,
145 * the event handler, needs it.
146 */
147static struct rds_message *rds_ib_send_unmap_op(struct rds_ib_connection *ic,
148 struct rds_ib_send_work *send,
149 int wc_status)
150{
151 struct rds_message *rm = NULL;
152
153 /* In the error case, wc.opcode sometimes contains garbage */
154 switch (send->s_wr.opcode) {
155 case IB_WR_SEND:
156 if (send->s_op) {
157 rm = container_of(send->s_op, struct rds_message, data);
158 rds_ib_send_unmap_data(ic, send->s_op, wc_status);
159 }
160 break;
161 case IB_WR_RDMA_WRITE:
162 case IB_WR_RDMA_READ:
163 if (send->s_op) {
164 rm = container_of(send->s_op, struct rds_message, rdma);
165 rds_ib_send_unmap_rdma(ic, send->s_op, wc_status);
166 }
167 break;
168 case IB_WR_ATOMIC_FETCH_AND_ADD:
169 case IB_WR_ATOMIC_CMP_AND_SWP:
170 if (send->s_op) {
171 rm = container_of(send->s_op, struct rds_message, atomic);
172 rds_ib_send_unmap_atomic(ic, send->s_op, wc_status);
173 }
174 break;
175 default:
176 if (printk_ratelimit())
177 printk(KERN_NOTICE
178 "RDS/IB: %s: unexpected opcode 0x%x in WR!\n",
179 __func__, send->s_wr.opcode);
180 break;
15133f6e
AG
181 }
182
ff3d7d36 183 send->s_wr.opcode = 0xdead;
6a0979df 184
ff3d7d36 185 return rm;
6a0979df
AG
186}
187
188void rds_ib_send_init_ring(struct rds_ib_connection *ic)
189{
190 struct rds_ib_send_work *send;
191 u32 i;
192
193 for (i = 0, send = ic->i_sends; i < ic->i_send_ring.w_nr; i++, send++) {
194 struct ib_sge *sge;
195
6a0979df
AG
196 send->s_op = NULL;
197
198 send->s_wr.wr_id = i;
199 send->s_wr.sg_list = send->s_sge;
6a0979df
AG
200 send->s_wr.ex.imm_data = 0;
201
919ced4c 202 sge = &send->s_sge[0];
6a0979df
AG
203 sge->addr = ic->i_send_hdrs_dma + (i * sizeof(struct rds_header));
204 sge->length = sizeof(struct rds_header);
205 sge->lkey = ic->i_mr->lkey;
919ced4c
AG
206
207 send->s_sge[1].lkey = ic->i_mr->lkey;
6a0979df
AG
208 }
209}
210
211void rds_ib_send_clear_ring(struct rds_ib_connection *ic)
212{
213 struct rds_ib_send_work *send;
214 u32 i;
215
216 for (i = 0, send = ic->i_sends; i < ic->i_send_ring.w_nr; i++, send++) {
ff3d7d36
AG
217 if (send->s_op && send->s_wr.opcode != 0xdead)
218 rds_ib_send_unmap_op(ic, send, IB_WC_WR_FLUSH_ERR);
6a0979df
AG
219 }
220}
221
f046011c
ZB
222/*
223 * The only fast path caller always has a non-zero nr, so we don't
224 * bother testing nr before performing the atomic sub.
225 */
226static void rds_ib_sub_signaled(struct rds_ib_connection *ic, int nr)
227{
228 if ((atomic_sub_return(nr, &ic->i_signaled_sends) == 0) &&
229 waitqueue_active(&rds_ib_ring_empty_wait))
230 wake_up(&rds_ib_ring_empty_wait);
231 BUG_ON(atomic_read(&ic->i_signaled_sends) < 0);
232}
233
6a0979df
AG
234/*
235 * The _oldest/_free ring operations here race cleanly with the alloc/unalloc
236 * operations performed in the send path. As the sender allocs and potentially
237 * unallocs the next free entry in the ring it doesn't alter which is
238 * the next to be freed, which is what this is concerned with.
239 */
240void rds_ib_send_cq_comp_handler(struct ib_cq *cq, void *context)
241{
242 struct rds_connection *conn = context;
243 struct rds_ib_connection *ic = conn->c_transport_data;
ff3d7d36 244 struct rds_message *rm = NULL;
6a0979df
AG
245 struct ib_wc wc;
246 struct rds_ib_send_work *send;
247 u32 completed;
248 u32 oldest;
249 u32 i = 0;
250 int ret;
f046011c 251 int nr_sig = 0;
6a0979df
AG
252
253 rdsdebug("cq %p conn %p\n", cq, conn);
254 rds_ib_stats_inc(s_ib_tx_cq_call);
255 ret = ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
256 if (ret)
257 rdsdebug("ib_req_notify_cq send failed: %d\n", ret);
258
259 while (ib_poll_cq(cq, 1, &wc) > 0) {
260 rdsdebug("wc wr_id 0x%llx status %u byte_len %u imm_data %u\n",
261 (unsigned long long)wc.wr_id, wc.status, wc.byte_len,
262 be32_to_cpu(wc.ex.imm_data));
263 rds_ib_stats_inc(s_ib_tx_cq_event);
264
265 if (wc.wr_id == RDS_IB_ACK_WR_ID) {
266 if (ic->i_ack_queued + HZ/2 < jiffies)
267 rds_ib_stats_inc(s_ib_tx_stalled);
268 rds_ib_ack_send_complete(ic);
269 continue;
270 }
271
272 oldest = rds_ib_ring_oldest(&ic->i_send_ring);
273
274 completed = rds_ib_ring_completed(&ic->i_send_ring, wc.wr_id, oldest);
275
276 for (i = 0; i < completed; i++) {
277 send = &ic->i_sends[oldest];
f046011c
ZB
278 if (send->s_wr.send_flags & IB_SEND_SIGNALED)
279 nr_sig++;
6a0979df 280
ff3d7d36 281 rm = rds_ib_send_unmap_op(ic, send, wc.status);
6a0979df 282
6a0979df
AG
283 if (send->s_queued + HZ/2 < jiffies)
284 rds_ib_stats_inc(s_ib_tx_stalled);
285
c9e65383
CM
286 if (send->s_op) {
287 if (send->s_op == rm->m_final_op) {
288 /* If anyone waited for this message to get flushed out, wake
289 * them up now */
290 rds_message_unmapped(rm);
291 }
ff3d7d36
AG
292 rds_message_put(rm);
293 send->s_op = NULL;
6a0979df
AG
294 }
295
296 oldest = (oldest + 1) % ic->i_send_ring.w_nr;
297 }
298
299 rds_ib_ring_free(&ic->i_send_ring, completed);
f046011c
ZB
300 rds_ib_sub_signaled(ic, nr_sig);
301 nr_sig = 0;
6a0979df 302
f64f9e71
JP
303 if (test_and_clear_bit(RDS_LL_SEND_FULL, &conn->c_flags) ||
304 test_bit(0, &conn->c_map_queued))
6a0979df
AG
305 queue_delayed_work(rds_wq, &conn->c_send_w, 0);
306
307 /* We expect errors as the qp is drained during shutdown */
308 if (wc.status != IB_WC_SUCCESS && rds_conn_up(conn)) {
309 rds_ib_conn_error(conn,
310 "send completion on %pI4 "
311 "had status %u, disconnecting and reconnecting\n",
312 &conn->c_faddr, wc.status);
313 }
314 }
315}
316
317/*
318 * This is the main function for allocating credits when sending
319 * messages.
320 *
321 * Conceptually, we have two counters:
322 * - send credits: this tells us how many WRs we're allowed
323 * to submit without overruning the reciever's queue. For
324 * each SEND WR we post, we decrement this by one.
325 *
326 * - posted credits: this tells us how many WRs we recently
327 * posted to the receive queue. This value is transferred
328 * to the peer as a "credit update" in a RDS header field.
329 * Every time we transmit credits to the peer, we subtract
330 * the amount of transferred credits from this counter.
331 *
332 * It is essential that we avoid situations where both sides have
333 * exhausted their send credits, and are unable to send new credits
334 * to the peer. We achieve this by requiring that we send at least
335 * one credit update to the peer before exhausting our credits.
336 * When new credits arrive, we subtract one credit that is withheld
337 * until we've posted new buffers and are ready to transmit these
338 * credits (see rds_ib_send_add_credits below).
339 *
340 * The RDS send code is essentially single-threaded; rds_send_xmit
0f4b1c7e 341 * sets RDS_IN_XMIT to ensure exclusive access to the send ring.
6a0979df
AG
342 * However, the ACK sending code is independent and can race with
343 * message SENDs.
344 *
345 * In the send path, we need to update the counters for send credits
346 * and the counter of posted buffers atomically - when we use the
347 * last available credit, we cannot allow another thread to race us
348 * and grab the posted credits counter. Hence, we have to use a
349 * spinlock to protect the credit counter, or use atomics.
350 *
351 * Spinlocks shared between the send and the receive path are bad,
352 * because they create unnecessary delays. An early implementation
353 * using a spinlock showed a 5% degradation in throughput at some
354 * loads.
355 *
356 * This implementation avoids spinlocks completely, putting both
357 * counters into a single atomic, and updating that atomic using
358 * atomic_add (in the receive path, when receiving fresh credits),
359 * and using atomic_cmpxchg when updating the two counters.
360 */
361int rds_ib_send_grab_credits(struct rds_ib_connection *ic,
7b70d033 362 u32 wanted, u32 *adv_credits, int need_posted, int max_posted)
6a0979df
AG
363{
364 unsigned int avail, posted, got = 0, advertise;
365 long oldval, newval;
366
367 *adv_credits = 0;
368 if (!ic->i_flowctl)
369 return wanted;
370
371try_again:
372 advertise = 0;
373 oldval = newval = atomic_read(&ic->i_credits);
374 posted = IB_GET_POST_CREDITS(oldval);
375 avail = IB_GET_SEND_CREDITS(oldval);
376
377 rdsdebug("rds_ib_send_grab_credits(%u): credits=%u posted=%u\n",
378 wanted, avail, posted);
379
380 /* The last credit must be used to send a credit update. */
381 if (avail && !posted)
382 avail--;
383
384 if (avail < wanted) {
385 struct rds_connection *conn = ic->i_cm_id->context;
386
387 /* Oops, there aren't that many credits left! */
388 set_bit(RDS_LL_SEND_FULL, &conn->c_flags);
389 got = avail;
390 } else {
391 /* Sometimes you get what you want, lalala. */
392 got = wanted;
393 }
394 newval -= IB_SET_SEND_CREDITS(got);
395
396 /*
397 * If need_posted is non-zero, then the caller wants
398 * the posted regardless of whether any send credits are
399 * available.
400 */
401 if (posted && (got || need_posted)) {
7b70d033 402 advertise = min_t(unsigned int, posted, max_posted);
6a0979df
AG
403 newval -= IB_SET_POST_CREDITS(advertise);
404 }
405
406 /* Finally bill everything */
407 if (atomic_cmpxchg(&ic->i_credits, oldval, newval) != oldval)
408 goto try_again;
409
410 *adv_credits = advertise;
411 return got;
412}
413
414void rds_ib_send_add_credits(struct rds_connection *conn, unsigned int credits)
415{
416 struct rds_ib_connection *ic = conn->c_transport_data;
417
418 if (credits == 0)
419 return;
420
421 rdsdebug("rds_ib_send_add_credits(%u): current=%u%s\n",
422 credits,
423 IB_GET_SEND_CREDITS(atomic_read(&ic->i_credits)),
424 test_bit(RDS_LL_SEND_FULL, &conn->c_flags) ? ", ll_send_full" : "");
425
426 atomic_add(IB_SET_SEND_CREDITS(credits), &ic->i_credits);
427 if (test_and_clear_bit(RDS_LL_SEND_FULL, &conn->c_flags))
428 queue_delayed_work(rds_wq, &conn->c_send_w, 0);
429
430 WARN_ON(IB_GET_SEND_CREDITS(credits) >= 16384);
431
432 rds_ib_stats_inc(s_ib_rx_credit_updates);
433}
434
435void rds_ib_advertise_credits(struct rds_connection *conn, unsigned int posted)
436{
437 struct rds_ib_connection *ic = conn->c_transport_data;
438
439 if (posted == 0)
440 return;
441
442 atomic_add(IB_SET_POST_CREDITS(posted), &ic->i_credits);
443
444 /* Decide whether to send an update to the peer now.
445 * If we would send a credit update for every single buffer we
446 * post, we would end up with an ACK storm (ACK arrives,
447 * consumes buffer, we refill the ring, send ACK to remote
448 * advertising the newly posted buffer... ad inf)
449 *
450 * Performance pretty much depends on how often we send
451 * credit updates - too frequent updates mean lots of ACKs.
452 * Too infrequent updates, and the peer will run out of
453 * credits and has to throttle.
454 * For the time being, 16 seems to be a good compromise.
455 */
456 if (IB_GET_POST_CREDITS(atomic_read(&ic->i_credits)) >= 16)
457 set_bit(IB_ACK_REQUESTED, &ic->i_ack_flags);
458}
459
f046011c
ZB
460static inline int rds_ib_set_wr_signal_state(struct rds_ib_connection *ic,
461 struct rds_ib_send_work *send,
462 bool notify)
241eef3e
AG
463{
464 /*
465 * We want to delay signaling completions just enough to get
466 * the batching benefits but not so much that we create dead time
467 * on the wire.
468 */
469 if (ic->i_unsignaled_wrs-- == 0 || notify) {
470 ic->i_unsignaled_wrs = rds_ib_sysctl_max_unsig_wrs;
471 send->s_wr.send_flags |= IB_SEND_SIGNALED;
f046011c 472 return 1;
241eef3e 473 }
f046011c 474 return 0;
241eef3e
AG
475}
476
6a0979df
AG
477/*
478 * This can be called multiple times for a given message. The first time
479 * we see a message we map its scatterlist into the IB device so that
480 * we can provide that mapped address to the IB scatter gather entries
481 * in the IB work requests. We translate the scatterlist into a series
482 * of work requests that fragment the message. These work requests complete
483 * in order so we pass ownership of the message to the completion handler
484 * once we send the final fragment.
485 *
486 * The RDS core uses the c_send_lock to only enter this function once
487 * per connection. This makes sure that the tx ring alloc/unalloc pairs
488 * don't get out of sync and confuse the ring.
489 */
490int rds_ib_xmit(struct rds_connection *conn, struct rds_message *rm,
491 unsigned int hdr_off, unsigned int sg, unsigned int off)
492{
493 struct rds_ib_connection *ic = conn->c_transport_data;
494 struct ib_device *dev = ic->i_cm_id->device;
495 struct rds_ib_send_work *send = NULL;
496 struct rds_ib_send_work *first;
497 struct rds_ib_send_work *prev;
498 struct ib_send_wr *failed_wr;
499 struct scatterlist *scat;
500 u32 pos;
501 u32 i;
502 u32 work_alloc;
da5a06ce 503 u32 credit_alloc = 0;
6a0979df
AG
504 u32 posted;
505 u32 adv_credits = 0;
506 int send_flags = 0;
da5a06ce 507 int bytes_sent = 0;
6a0979df
AG
508 int ret;
509 int flow_controlled = 0;
f046011c 510 int nr_sig = 0;
6a0979df
AG
511
512 BUG_ON(off % RDS_FRAG_SIZE);
513 BUG_ON(hdr_off != 0 && hdr_off != sizeof(struct rds_header));
514
2e7b3b99
AG
515 /* Do not send cong updates to IB loopback */
516 if (conn->c_loopback
517 && rm->m_inc.i_hdr.h_flags & RDS_FLAG_CONG_BITMAP) {
518 rds_cong_map_updated(conn->c_fcong, ~(u64) 0);
519 return sizeof(struct rds_header) + RDS_CONG_MAP_BYTES;
520 }
521
6a0979df
AG
522 /* FIXME we may overallocate here */
523 if (be32_to_cpu(rm->m_inc.i_hdr.h_len) == 0)
524 i = 1;
525 else
526 i = ceil(be32_to_cpu(rm->m_inc.i_hdr.h_len), RDS_FRAG_SIZE);
527
528 work_alloc = rds_ib_ring_alloc(&ic->i_send_ring, i, &pos);
529 if (work_alloc == 0) {
530 set_bit(RDS_LL_SEND_FULL, &conn->c_flags);
531 rds_ib_stats_inc(s_ib_tx_ring_full);
532 ret = -ENOMEM;
533 goto out;
534 }
535
6a0979df 536 if (ic->i_flowctl) {
7b70d033 537 credit_alloc = rds_ib_send_grab_credits(ic, work_alloc, &posted, 0, RDS_MAX_ADV_CREDIT);
6a0979df
AG
538 adv_credits += posted;
539 if (credit_alloc < work_alloc) {
540 rds_ib_ring_unalloc(&ic->i_send_ring, work_alloc - credit_alloc);
541 work_alloc = credit_alloc;
c8de3f10 542 flow_controlled = 1;
6a0979df
AG
543 }
544 if (work_alloc == 0) {
d39e0602 545 set_bit(RDS_LL_SEND_FULL, &conn->c_flags);
6a0979df
AG
546 rds_ib_stats_inc(s_ib_tx_throttle);
547 ret = -ENOMEM;
548 goto out;
549 }
550 }
551
552 /* map the message the first time we see it */
ff3d7d36 553 if (!ic->i_data_op) {
6c7cc6e4
AG
554 if (rm->data.op_nents) {
555 rm->data.op_count = ib_dma_map_sg(dev,
556 rm->data.op_sg,
557 rm->data.op_nents,
558 DMA_TO_DEVICE);
559 rdsdebug("ic %p mapping rm %p: %d\n", ic, rm, rm->data.op_count);
560 if (rm->data.op_count == 0) {
6a0979df
AG
561 rds_ib_stats_inc(s_ib_tx_sg_mapping_failure);
562 rds_ib_ring_unalloc(&ic->i_send_ring, work_alloc);
563 ret = -ENOMEM; /* XXX ? */
564 goto out;
565 }
566 } else {
6c7cc6e4 567 rm->data.op_count = 0;
6a0979df
AG
568 }
569
6a0979df 570 rds_message_addref(rm);
ff3d7d36 571 ic->i_data_op = &rm->data;
6a0979df
AG
572
573 /* Finalize the header */
574 if (test_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags))
575 rm->m_inc.i_hdr.h_flags |= RDS_FLAG_ACK_REQUIRED;
576 if (test_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags))
577 rm->m_inc.i_hdr.h_flags |= RDS_FLAG_RETRANSMITTED;
578
579 /* If it has a RDMA op, tell the peer we did it. This is
580 * used by the peer to release use-once RDMA MRs. */
f8b3aaf2 581 if (rm->rdma.op_active) {
6a0979df
AG
582 struct rds_ext_header_rdma ext_hdr;
583
f8b3aaf2 584 ext_hdr.h_rdma_rkey = cpu_to_be32(rm->rdma.op_rkey);
6a0979df
AG
585 rds_message_add_extension(&rm->m_inc.i_hdr,
586 RDS_EXTHDR_RDMA, &ext_hdr, sizeof(ext_hdr));
587 }
588 if (rm->m_rdma_cookie) {
589 rds_message_add_rdma_dest_extension(&rm->m_inc.i_hdr,
590 rds_rdma_cookie_key(rm->m_rdma_cookie),
591 rds_rdma_cookie_offset(rm->m_rdma_cookie));
592 }
593
594 /* Note - rds_ib_piggyb_ack clears the ACK_REQUIRED bit, so
595 * we should not do this unless we have a chance of at least
596 * sticking the header into the send ring. Which is why we
597 * should call rds_ib_ring_alloc first. */
598 rm->m_inc.i_hdr.h_ack = cpu_to_be64(rds_ib_piggyb_ack(ic));
599 rds_message_make_checksum(&rm->m_inc.i_hdr);
600
601 /*
602 * Update adv_credits since we reset the ACK_REQUIRED bit.
603 */
c8de3f10
AG
604 if (ic->i_flowctl) {
605 rds_ib_send_grab_credits(ic, 0, &posted, 1, RDS_MAX_ADV_CREDIT - adv_credits);
606 adv_credits += posted;
607 BUG_ON(adv_credits > 255);
608 }
735f61e6 609 }
6a0979df 610
6a0979df
AG
611 /* Sometimes you want to put a fence between an RDMA
612 * READ and the following SEND.
613 * We could either do this all the time
614 * or when requested by the user. Right now, we let
615 * the application choose.
616 */
f8b3aaf2 617 if (rm->rdma.op_active && rm->rdma.op_fence)
6a0979df
AG
618 send_flags = IB_SEND_FENCE;
619
da5a06ce
AG
620 /* Each frag gets a header. Msgs may be 0 bytes */
621 send = &ic->i_sends[pos];
622 first = send;
623 prev = NULL;
ff3d7d36 624 scat = &ic->i_data_op->op_sg[sg];
da5a06ce
AG
625 i = 0;
626 do {
627 unsigned int len = 0;
628
629 /* Set up the header */
630 send->s_wr.send_flags = send_flags;
631 send->s_wr.opcode = IB_WR_SEND;
632 send->s_wr.num_sge = 1;
633 send->s_wr.next = NULL;
634 send->s_queued = jiffies;
635 send->s_op = NULL;
6a0979df 636
da5a06ce
AG
637 send->s_sge[0].addr = ic->i_send_hdrs_dma
638 + (pos * sizeof(struct rds_header));
639 send->s_sge[0].length = sizeof(struct rds_header);
640
641 memcpy(&ic->i_send_hdrs[pos], &rm->m_inc.i_hdr, sizeof(struct rds_header));
6a0979df 642
da5a06ce
AG
643 /* Set up the data, if present */
644 if (i < work_alloc
6c7cc6e4 645 && scat != &rm->data.op_sg[rm->data.op_count]) {
da5a06ce
AG
646 len = min(RDS_FRAG_SIZE, ib_sg_dma_len(dev, scat) - off);
647 send->s_wr.num_sge = 2;
6a0979df 648
da5a06ce
AG
649 send->s_sge[1].addr = ib_sg_dma_address(dev, scat) + off;
650 send->s_sge[1].length = len;
6a0979df 651
da5a06ce
AG
652 bytes_sent += len;
653 off += len;
654 if (off == ib_sg_dma_len(dev, scat)) {
655 scat++;
656 off = 0;
657 }
658 }
6a0979df 659
241eef3e 660 rds_ib_set_wr_signal_state(ic, send, 0);
6a0979df 661
6a0979df
AG
662 /*
663 * Always signal the last one if we're stopping due to flow control.
664 */
c8de3f10 665 if (ic->i_flowctl && flow_controlled && i == (work_alloc-1))
6a0979df
AG
666 send->s_wr.send_flags |= IB_SEND_SIGNALED | IB_SEND_SOLICITED;
667
f046011c
ZB
668 if (send->s_wr.send_flags & IB_SEND_SIGNALED)
669 nr_sig++;
670
6a0979df
AG
671 rdsdebug("send %p wr %p num_sge %u next %p\n", send,
672 &send->s_wr, send->s_wr.num_sge, send->s_wr.next);
673
c8de3f10 674 if (ic->i_flowctl && adv_credits) {
6a0979df
AG
675 struct rds_header *hdr = &ic->i_send_hdrs[pos];
676
677 /* add credit and redo the header checksum */
678 hdr->h_credit = adv_credits;
679 rds_message_make_checksum(hdr);
680 adv_credits = 0;
681 rds_ib_stats_inc(s_ib_tx_credit_updates);
682 }
683
684 if (prev)
685 prev->s_wr.next = &send->s_wr;
686 prev = send;
687
688 pos = (pos + 1) % ic->i_send_ring.w_nr;
da5a06ce
AG
689 send = &ic->i_sends[pos];
690 i++;
691
692 } while (i < work_alloc
6c7cc6e4 693 && scat != &rm->data.op_sg[rm->data.op_count]);
6a0979df
AG
694
695 /* Account the RDS header in the number of bytes we sent, but just once.
696 * The caller has no concept of fragmentation. */
697 if (hdr_off == 0)
da5a06ce 698 bytes_sent += sizeof(struct rds_header);
6a0979df
AG
699
700 /* if we finished the message then send completion owns it */
6c7cc6e4 701 if (scat == &rm->data.op_sg[rm->data.op_count]) {
ff3d7d36 702 prev->s_op = ic->i_data_op;
241eef3e 703 prev->s_wr.send_flags |= IB_SEND_SOLICITED;
ff3d7d36 704 ic->i_data_op = NULL;
6a0979df
AG
705 }
706
da5a06ce 707 /* Put back wrs & credits we didn't use */
6a0979df
AG
708 if (i < work_alloc) {
709 rds_ib_ring_unalloc(&ic->i_send_ring, work_alloc - i);
710 work_alloc = i;
711 }
712 if (ic->i_flowctl && i < credit_alloc)
713 rds_ib_send_add_credits(conn, credit_alloc - i);
714
f046011c
ZB
715 if (nr_sig)
716 atomic_add(nr_sig, &ic->i_signaled_sends);
717
6a0979df
AG
718 /* XXX need to worry about failed_wr and partial sends. */
719 failed_wr = &first->s_wr;
720 ret = ib_post_send(ic->i_cm_id->qp, &first->s_wr, &failed_wr);
721 rdsdebug("ic %p first %p (wr %p) ret %d wr %p\n", ic,
722 first, &first->s_wr, ret, failed_wr);
723 BUG_ON(failed_wr != &first->s_wr);
724 if (ret) {
725 printk(KERN_WARNING "RDS/IB: ib_post_send to %pI4 "
726 "returned %d\n", &conn->c_faddr, ret);
727 rds_ib_ring_unalloc(&ic->i_send_ring, work_alloc);
f046011c 728 rds_ib_sub_signaled(ic, nr_sig);
ff3d7d36
AG
729 if (prev->s_op) {
730 ic->i_data_op = prev->s_op;
731 prev->s_op = NULL;
6a0979df 732 }
735f61e6
AG
733
734 rds_ib_conn_error(ic->conn, "ib_post_send failed\n");
6a0979df
AG
735 goto out;
736 }
737
da5a06ce 738 ret = bytes_sent;
6a0979df
AG
739out:
740 BUG_ON(adv_credits);
741 return ret;
742}
743
15133f6e
AG
744/*
745 * Issue atomic operation.
746 * A simplified version of the rdma case, we always map 1 SG, and
747 * only 8 bytes, for the return value from the atomic operation.
748 */
ff3d7d36 749int rds_ib_xmit_atomic(struct rds_connection *conn, struct rm_atomic_op *op)
15133f6e
AG
750{
751 struct rds_ib_connection *ic = conn->c_transport_data;
752 struct rds_ib_send_work *send = NULL;
753 struct ib_send_wr *failed_wr;
754 struct rds_ib_device *rds_ibdev;
755 u32 pos;
756 u32 work_alloc;
757 int ret;
f046011c 758 int nr_sig = 0;
15133f6e
AG
759
760 rds_ibdev = ib_get_client_data(ic->i_cm_id->device, &rds_ib_client);
761
762 work_alloc = rds_ib_ring_alloc(&ic->i_send_ring, 1, &pos);
763 if (work_alloc != 1) {
764 rds_ib_ring_unalloc(&ic->i_send_ring, work_alloc);
765 rds_ib_stats_inc(s_ib_tx_ring_full);
766 ret = -ENOMEM;
767 goto out;
768 }
769
770 /* address of send request in ring */
771 send = &ic->i_sends[pos];
772 send->s_queued = jiffies;
773
774 if (op->op_type == RDS_ATOMIC_TYPE_CSWP) {
775 send->s_wr.opcode = IB_WR_ATOMIC_CMP_AND_SWP;
776 send->s_wr.wr.atomic.compare_add = op->op_compare;
777 send->s_wr.wr.atomic.swap = op->op_swap_add;
778 } else { /* FADD */
779 send->s_wr.opcode = IB_WR_ATOMIC_FETCH_AND_ADD;
780 send->s_wr.wr.atomic.compare_add = op->op_swap_add;
781 send->s_wr.wr.atomic.swap = 0;
782 }
f046011c 783 nr_sig = rds_ib_set_wr_signal_state(ic, send, op->op_notify);
15133f6e
AG
784 send->s_wr.num_sge = 1;
785 send->s_wr.next = NULL;
786 send->s_wr.wr.atomic.remote_addr = op->op_remote_addr;
787 send->s_wr.wr.atomic.rkey = op->op_rkey;
1cc2228c
CM
788 send->s_op = op;
789 rds_message_addref(container_of(send->s_op, struct rds_message, atomic));
15133f6e
AG
790
791 /* map 8 byte retval buffer to the device */
792 ret = ib_dma_map_sg(ic->i_cm_id->device, op->op_sg, 1, DMA_FROM_DEVICE);
793 rdsdebug("ic %p mapping atomic op %p. mapped %d pg\n", ic, op, ret);
794 if (ret != 1) {
795 rds_ib_ring_unalloc(&ic->i_send_ring, work_alloc);
796 rds_ib_stats_inc(s_ib_tx_sg_mapping_failure);
797 ret = -ENOMEM; /* XXX ? */
798 goto out;
799 }
800
801 /* Convert our struct scatterlist to struct ib_sge */
802 send->s_sge[0].addr = ib_sg_dma_address(ic->i_cm_id->device, op->op_sg);
803 send->s_sge[0].length = ib_sg_dma_len(ic->i_cm_id->device, op->op_sg);
804 send->s_sge[0].lkey = ic->i_mr->lkey;
805
806 rdsdebug("rva %Lx rpa %Lx len %u\n", op->op_remote_addr,
807 send->s_sge[0].addr, send->s_sge[0].length);
808
f046011c
ZB
809 if (nr_sig)
810 atomic_add(nr_sig, &ic->i_signaled_sends);
811
15133f6e
AG
812 failed_wr = &send->s_wr;
813 ret = ib_post_send(ic->i_cm_id->qp, &send->s_wr, &failed_wr);
814 rdsdebug("ic %p send %p (wr %p) ret %d wr %p\n", ic,
815 send, &send->s_wr, ret, failed_wr);
816 BUG_ON(failed_wr != &send->s_wr);
817 if (ret) {
818 printk(KERN_WARNING "RDS/IB: atomic ib_post_send to %pI4 "
819 "returned %d\n", &conn->c_faddr, ret);
820 rds_ib_ring_unalloc(&ic->i_send_ring, work_alloc);
f046011c 821 rds_ib_sub_signaled(ic, nr_sig);
15133f6e
AG
822 goto out;
823 }
824
825 if (unlikely(failed_wr != &send->s_wr)) {
826 printk(KERN_WARNING "RDS/IB: atomic ib_post_send() rc=%d, but failed_wqe updated!\n", ret);
827 BUG_ON(failed_wr != &send->s_wr);
828 }
829
830out:
831 return ret;
832}
833
f8b3aaf2 834int rds_ib_xmit_rdma(struct rds_connection *conn, struct rm_rdma_op *op)
6a0979df
AG
835{
836 struct rds_ib_connection *ic = conn->c_transport_data;
837 struct rds_ib_send_work *send = NULL;
838 struct rds_ib_send_work *first;
839 struct rds_ib_send_work *prev;
840 struct ib_send_wr *failed_wr;
6a0979df
AG
841 struct scatterlist *scat;
842 unsigned long len;
f8b3aaf2 843 u64 remote_addr = op->op_remote_addr;
89bf9d41 844 u32 max_sge = ic->rds_ibdev->max_sge;
6a0979df
AG
845 u32 pos;
846 u32 work_alloc;
847 u32 i;
848 u32 j;
849 int sent;
850 int ret;
851 int num_sge;
f046011c 852 int nr_sig = 0;
6a0979df 853
ff3d7d36 854 /* map the op the first time we see it */
f8b3aaf2
AG
855 if (!op->op_mapped) {
856 op->op_count = ib_dma_map_sg(ic->i_cm_id->device,
857 op->op_sg, op->op_nents, (op->op_write) ?
858 DMA_TO_DEVICE : DMA_FROM_DEVICE);
859 rdsdebug("ic %p mapping op %p: %d\n", ic, op, op->op_count);
860 if (op->op_count == 0) {
6a0979df
AG
861 rds_ib_stats_inc(s_ib_tx_sg_mapping_failure);
862 ret = -ENOMEM; /* XXX ? */
863 goto out;
864 }
865
f8b3aaf2 866 op->op_mapped = 1;
6a0979df
AG
867 }
868
869 /*
870 * Instead of knowing how to return a partial rdma read/write we insist that there
871 * be enough work requests to send the entire message.
872 */
89bf9d41 873 i = ceil(op->op_count, max_sge);
6a0979df
AG
874
875 work_alloc = rds_ib_ring_alloc(&ic->i_send_ring, i, &pos);
876 if (work_alloc != i) {
877 rds_ib_ring_unalloc(&ic->i_send_ring, work_alloc);
878 rds_ib_stats_inc(s_ib_tx_ring_full);
879 ret = -ENOMEM;
880 goto out;
881 }
882
883 send = &ic->i_sends[pos];
884 first = send;
885 prev = NULL;
f8b3aaf2 886 scat = &op->op_sg[0];
6a0979df 887 sent = 0;
f8b3aaf2 888 num_sge = op->op_count;
6a0979df 889
f8b3aaf2 890 for (i = 0; i < work_alloc && scat != &op->op_sg[op->op_count]; i++) {
6a0979df
AG
891 send->s_wr.send_flags = 0;
892 send->s_queued = jiffies;
1cc2228c 893 send->s_op = NULL;
241eef3e 894
f046011c 895 nr_sig += rds_ib_set_wr_signal_state(ic, send, op->op_notify);
6a0979df 896
f8b3aaf2 897 send->s_wr.opcode = op->op_write ? IB_WR_RDMA_WRITE : IB_WR_RDMA_READ;
6a0979df 898 send->s_wr.wr.rdma.remote_addr = remote_addr;
f8b3aaf2 899 send->s_wr.wr.rdma.rkey = op->op_rkey;
6a0979df 900
89bf9d41
ZB
901 if (num_sge > max_sge) {
902 send->s_wr.num_sge = max_sge;
903 num_sge -= max_sge;
6a0979df
AG
904 } else {
905 send->s_wr.num_sge = num_sge;
906 }
907
908 send->s_wr.next = NULL;
909
910 if (prev)
911 prev->s_wr.next = &send->s_wr;
912
f8b3aaf2 913 for (j = 0; j < send->s_wr.num_sge && scat != &op->op_sg[op->op_count]; j++) {
6a0979df
AG
914 len = ib_sg_dma_len(ic->i_cm_id->device, scat);
915 send->s_sge[j].addr =
916 ib_sg_dma_address(ic->i_cm_id->device, scat);
917 send->s_sge[j].length = len;
918 send->s_sge[j].lkey = ic->i_mr->lkey;
919
920 sent += len;
921 rdsdebug("ic %p sent %d remote_addr %llu\n", ic, sent, remote_addr);
922
923 remote_addr += len;
924 scat++;
925 }
926
927 rdsdebug("send %p wr %p num_sge %u next %p\n", send,
928 &send->s_wr, send->s_wr.num_sge, send->s_wr.next);
929
930 prev = send;
931 if (++send == &ic->i_sends[ic->i_send_ring.w_nr])
932 send = ic->i_sends;
933 }
934
1cc2228c
CM
935 /* give a reference to the last op */
936 if (scat == &op->op_sg[op->op_count]) {
937 prev->s_op = op;
938 rds_message_addref(container_of(op, struct rds_message, rdma));
939 }
940
6a0979df
AG
941 if (i < work_alloc) {
942 rds_ib_ring_unalloc(&ic->i_send_ring, work_alloc - i);
943 work_alloc = i;
944 }
945
f046011c
ZB
946 if (nr_sig)
947 atomic_add(nr_sig, &ic->i_signaled_sends);
948
6a0979df
AG
949 failed_wr = &first->s_wr;
950 ret = ib_post_send(ic->i_cm_id->qp, &first->s_wr, &failed_wr);
951 rdsdebug("ic %p first %p (wr %p) ret %d wr %p\n", ic,
952 first, &first->s_wr, ret, failed_wr);
953 BUG_ON(failed_wr != &first->s_wr);
954 if (ret) {
955 printk(KERN_WARNING "RDS/IB: rdma ib_post_send to %pI4 "
956 "returned %d\n", &conn->c_faddr, ret);
957 rds_ib_ring_unalloc(&ic->i_send_ring, work_alloc);
f046011c 958 rds_ib_sub_signaled(ic, nr_sig);
6a0979df
AG
959 goto out;
960 }
961
962 if (unlikely(failed_wr != &first->s_wr)) {
963 printk(KERN_WARNING "RDS/IB: ib_post_send() rc=%d, but failed_wqe updated!\n", ret);
964 BUG_ON(failed_wr != &first->s_wr);
965 }
966
967
968out:
969 return ret;
970}
971
972void rds_ib_xmit_complete(struct rds_connection *conn)
973{
974 struct rds_ib_connection *ic = conn->c_transport_data;
975
976 /* We may have a pending ACK or window update we were unable
977 * to send previously (due to flow control). Try again. */
978 rds_ib_attempt_ack(ic);
979}
This page took 0.172547 seconds and 5 git commands to generate.