blob: 6f6b8a56212a904c3adda46f3a1a53470bb7dd32 [file] [log] [blame]
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -04001/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04002 * Copyright (c) 2003-2007 Network Appliance, Inc. 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 BSD-type
8 * license below:
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
17 * Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials provided
20 * with the distribution.
21 *
22 * Neither the name of the Network Appliance, Inc. nor the names of
23 * its contributors may be used to endorse or promote products
24 * derived from this software without specific prior written
25 * permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -040038 */
39
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040040/*
41 * verbs.c
42 *
43 * Encapsulates the major functions managing:
44 * o adapters
45 * o endpoints
46 * o connections
47 * o buffer memory
48 */
49
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000050#include <linux/interrupt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090051#include <linux/slab.h>
Chuck Levereba8ff62015-01-21 11:03:02 -050052#include <linux/prefetch.h>
Chuck Lever0dd39ca2015-03-30 14:33:43 -040053#include <linux/sunrpc/addr.h>
Chuck Lever65866f82014-05-28 10:33:59 -040054#include <asm/bitops.h>
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040055
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -040056#include "xprt_rdma.h"
57
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040058/*
59 * Globals/Macros
60 */
61
Jeff Laytonf895b252014-11-17 16:58:04 -050062#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040063# define RPCDBG_FACILITY RPCDBG_TRANS
64#endif
65
66/*
67 * internal functions
68 */
69
70/*
71 * handle replies in tasklet context, using a single, global list
72 * rdma tasklet function -- just turn around and call the func
73 * for all replies on the list
74 */
75
76static DEFINE_SPINLOCK(rpcrdma_tk_lock_g);
77static LIST_HEAD(rpcrdma_tasklets_g);
78
79static void
80rpcrdma_run_tasklet(unsigned long data)
81{
82 struct rpcrdma_rep *rep;
83 void (*func)(struct rpcrdma_rep *);
84 unsigned long flags;
85
86 data = data;
87 spin_lock_irqsave(&rpcrdma_tk_lock_g, flags);
88 while (!list_empty(&rpcrdma_tasklets_g)) {
89 rep = list_entry(rpcrdma_tasklets_g.next,
90 struct rpcrdma_rep, rr_list);
91 list_del(&rep->rr_list);
92 func = rep->rr_func;
93 rep->rr_func = NULL;
94 spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags);
95
96 if (func)
97 func(rep);
98 else
99 rpcrdma_recv_buffer_put(rep);
100
101 spin_lock_irqsave(&rpcrdma_tk_lock_g, flags);
102 }
103 spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags);
104}
105
106static DECLARE_TASKLET(rpcrdma_tasklet_g, rpcrdma_run_tasklet, 0UL);
107
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400108static void
Chuck Leverf1a03b72014-11-08 20:14:37 -0500109rpcrdma_schedule_tasklet(struct list_head *sched_list)
110{
111 unsigned long flags;
112
113 spin_lock_irqsave(&rpcrdma_tk_lock_g, flags);
114 list_splice_tail(sched_list, &rpcrdma_tasklets_g);
115 spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags);
116 tasklet_schedule(&rpcrdma_tasklet_g);
117}
118
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400119static void
120rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context)
121{
122 struct rpcrdma_ep *ep = context;
123
Chuck Lever7ff11de2014-11-08 20:15:01 -0500124 pr_err("RPC: %s: %s on device %s ep %p\n",
Sagi Grimberg76357c72015-05-18 13:40:32 +0300125 __func__, ib_event_msg(event->event),
Chuck Lever7ff11de2014-11-08 20:15:01 -0500126 event->device->name, context);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400127 if (ep->rep_connected == 1) {
128 ep->rep_connected = -EIO;
Chuck Leverafadc462015-01-21 11:03:11 -0500129 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400130 wake_up_all(&ep->rep_connect_wait);
131 }
132}
133
134static void
135rpcrdma_cq_async_error_upcall(struct ib_event *event, void *context)
136{
137 struct rpcrdma_ep *ep = context;
138
Chuck Lever7ff11de2014-11-08 20:15:01 -0500139 pr_err("RPC: %s: %s on device %s ep %p\n",
Sagi Grimberg76357c72015-05-18 13:40:32 +0300140 __func__, ib_event_msg(event->event),
Chuck Lever7ff11de2014-11-08 20:15:01 -0500141 event->device->name, context);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400142 if (ep->rep_connected == 1) {
143 ep->rep_connected = -EIO;
Chuck Leverafadc462015-01-21 11:03:11 -0500144 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400145 wake_up_all(&ep->rep_connect_wait);
146 }
147}
148
Chuck Leverfc664482014-05-28 10:33:25 -0400149static void
150rpcrdma_sendcq_process_wc(struct ib_wc *wc)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400151{
Chuck Lever85024272015-01-21 11:02:04 -0500152 /* WARNING: Only wr_id and status are reliable at this point */
Chuck Levere46ac342015-03-30 14:35:35 -0400153 if (wc->wr_id == RPCRDMA_IGNORE_COMPLETION) {
154 if (wc->status != IB_WC_SUCCESS &&
155 wc->status != IB_WC_WR_FLUSH_ERR)
Chuck Lever85024272015-01-21 11:02:04 -0500156 pr_err("RPC: %s: SEND: %s\n",
Sagi Grimberg76357c72015-05-18 13:40:32 +0300157 __func__, ib_wc_status_msg(wc->status));
Chuck Lever85024272015-01-21 11:02:04 -0500158 } else {
159 struct rpcrdma_mw *r;
160
161 r = (struct rpcrdma_mw *)(unsigned long)wc->wr_id;
Chuck Levere46ac342015-03-30 14:35:35 -0400162 r->mw_sendcompletion(wc);
Chuck Lever85024272015-01-21 11:02:04 -0500163 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400164}
165
Chuck Leverfc664482014-05-28 10:33:25 -0400166static int
Chuck Lever1c00dd02014-05-28 10:33:42 -0400167rpcrdma_sendcq_poll(struct ib_cq *cq, struct rpcrdma_ep *ep)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400168{
Chuck Lever1c00dd02014-05-28 10:33:42 -0400169 struct ib_wc *wcs;
Chuck Lever8301a2c2014-05-28 10:33:51 -0400170 int budget, count, rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400171
Chuck Lever8301a2c2014-05-28 10:33:51 -0400172 budget = RPCRDMA_WC_BUDGET / RPCRDMA_POLLSIZE;
Chuck Lever1c00dd02014-05-28 10:33:42 -0400173 do {
174 wcs = ep->rep_send_wcs;
175
176 rc = ib_poll_cq(cq, RPCRDMA_POLLSIZE, wcs);
177 if (rc <= 0)
178 return rc;
179
180 count = rc;
181 while (count-- > 0)
182 rpcrdma_sendcq_process_wc(wcs++);
Chuck Lever8301a2c2014-05-28 10:33:51 -0400183 } while (rc == RPCRDMA_POLLSIZE && --budget);
Chuck Lever1c00dd02014-05-28 10:33:42 -0400184 return 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400185}
186
187/*
Chuck Leverfc664482014-05-28 10:33:25 -0400188 * Handle send, fast_reg_mr, and local_inv completions.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400189 *
Chuck Leverfc664482014-05-28 10:33:25 -0400190 * Send events are typically suppressed and thus do not result
191 * in an upcall. Occasionally one is signaled, however. This
192 * prevents the provider's completion queue from wrapping and
193 * losing a completion.
194 */
195static void
196rpcrdma_sendcq_upcall(struct ib_cq *cq, void *cq_context)
197{
Chuck Lever1c00dd02014-05-28 10:33:42 -0400198 struct rpcrdma_ep *ep = (struct rpcrdma_ep *)cq_context;
Chuck Leverfc664482014-05-28 10:33:25 -0400199 int rc;
200
Chuck Lever1c00dd02014-05-28 10:33:42 -0400201 rc = rpcrdma_sendcq_poll(cq, ep);
Chuck Leverfc664482014-05-28 10:33:25 -0400202 if (rc) {
203 dprintk("RPC: %s: ib_poll_cq failed: %i\n",
204 __func__, rc);
205 return;
206 }
207
Chuck Lever7f23f6f2014-05-28 10:33:34 -0400208 rc = ib_req_notify_cq(cq,
209 IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS);
210 if (rc == 0)
211 return;
212 if (rc < 0) {
Chuck Leverfc664482014-05-28 10:33:25 -0400213 dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
214 __func__, rc);
215 return;
216 }
217
Chuck Lever1c00dd02014-05-28 10:33:42 -0400218 rpcrdma_sendcq_poll(cq, ep);
Chuck Leverfc664482014-05-28 10:33:25 -0400219}
220
221static void
Chuck Leverbb961932014-07-29 17:25:46 -0400222rpcrdma_recvcq_process_wc(struct ib_wc *wc, struct list_head *sched_list)
Chuck Leverfc664482014-05-28 10:33:25 -0400223{
224 struct rpcrdma_rep *rep =
225 (struct rpcrdma_rep *)(unsigned long)wc->wr_id;
226
Chuck Lever85024272015-01-21 11:02:04 -0500227 /* WARNING: Only wr_id and status are reliable at this point */
228 if (wc->status != IB_WC_SUCCESS)
229 goto out_fail;
Chuck Leverfc664482014-05-28 10:33:25 -0400230
Chuck Lever85024272015-01-21 11:02:04 -0500231 /* status == SUCCESS means all fields in wc are trustworthy */
Chuck Leverfc664482014-05-28 10:33:25 -0400232 if (wc->opcode != IB_WC_RECV)
233 return;
234
Chuck Lever85024272015-01-21 11:02:04 -0500235 dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n",
236 __func__, rep, wc->byte_len);
237
Chuck Leverfc664482014-05-28 10:33:25 -0400238 rep->rr_len = wc->byte_len;
239 ib_dma_sync_single_for_cpu(rdmab_to_ia(rep->rr_buffer)->ri_id->device,
Chuck Lever6b1184c2015-01-21 11:04:25 -0500240 rdmab_addr(rep->rr_rdmabuf),
241 rep->rr_len, DMA_FROM_DEVICE);
242 prefetch(rdmab_to_msg(rep->rr_rdmabuf));
Chuck Leverfc664482014-05-28 10:33:25 -0400243
244out_schedule:
Chuck Leverbb961932014-07-29 17:25:46 -0400245 list_add_tail(&rep->rr_list, sched_list);
Chuck Lever85024272015-01-21 11:02:04 -0500246 return;
247out_fail:
248 if (wc->status != IB_WC_WR_FLUSH_ERR)
249 pr_err("RPC: %s: rep %p: %s\n",
Sagi Grimberg76357c72015-05-18 13:40:32 +0300250 __func__, rep, ib_wc_status_msg(wc->status));
Chuck Lever85024272015-01-21 11:02:04 -0500251 rep->rr_len = ~0U;
252 goto out_schedule;
Chuck Leverfc664482014-05-28 10:33:25 -0400253}
254
255static int
Chuck Lever1c00dd02014-05-28 10:33:42 -0400256rpcrdma_recvcq_poll(struct ib_cq *cq, struct rpcrdma_ep *ep)
Chuck Leverfc664482014-05-28 10:33:25 -0400257{
Chuck Leverbb961932014-07-29 17:25:46 -0400258 struct list_head sched_list;
Chuck Lever1c00dd02014-05-28 10:33:42 -0400259 struct ib_wc *wcs;
Chuck Lever8301a2c2014-05-28 10:33:51 -0400260 int budget, count, rc;
Chuck Leverfc664482014-05-28 10:33:25 -0400261
Chuck Leverbb961932014-07-29 17:25:46 -0400262 INIT_LIST_HEAD(&sched_list);
Chuck Lever8301a2c2014-05-28 10:33:51 -0400263 budget = RPCRDMA_WC_BUDGET / RPCRDMA_POLLSIZE;
Chuck Lever1c00dd02014-05-28 10:33:42 -0400264 do {
265 wcs = ep->rep_recv_wcs;
266
267 rc = ib_poll_cq(cq, RPCRDMA_POLLSIZE, wcs);
268 if (rc <= 0)
Chuck Leverbb961932014-07-29 17:25:46 -0400269 goto out_schedule;
Chuck Lever1c00dd02014-05-28 10:33:42 -0400270
271 count = rc;
272 while (count-- > 0)
Chuck Leverbb961932014-07-29 17:25:46 -0400273 rpcrdma_recvcq_process_wc(wcs++, &sched_list);
Chuck Lever8301a2c2014-05-28 10:33:51 -0400274 } while (rc == RPCRDMA_POLLSIZE && --budget);
Chuck Leverbb961932014-07-29 17:25:46 -0400275 rc = 0;
276
277out_schedule:
Chuck Leverf1a03b72014-11-08 20:14:37 -0500278 rpcrdma_schedule_tasklet(&sched_list);
Chuck Leverbb961932014-07-29 17:25:46 -0400279 return rc;
Chuck Leverfc664482014-05-28 10:33:25 -0400280}
281
282/*
283 * Handle receive completions.
284 *
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400285 * It is reentrant but processes single events in order to maintain
286 * ordering of receives to keep server credits.
287 *
288 * It is the responsibility of the scheduled tasklet to return
289 * recv buffers to the pool. NOTE: this affects synchronization of
290 * connection shutdown. That is, the structures required for
291 * the completion of the reply handler must remain intact until
292 * all memory has been reclaimed.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400293 */
294static void
Chuck Leverfc664482014-05-28 10:33:25 -0400295rpcrdma_recvcq_upcall(struct ib_cq *cq, void *cq_context)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400296{
Chuck Lever1c00dd02014-05-28 10:33:42 -0400297 struct rpcrdma_ep *ep = (struct rpcrdma_ep *)cq_context;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400298 int rc;
299
Chuck Lever1c00dd02014-05-28 10:33:42 -0400300 rc = rpcrdma_recvcq_poll(cq, ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400301 if (rc) {
Chuck Leverfc664482014-05-28 10:33:25 -0400302 dprintk("RPC: %s: ib_poll_cq failed: %i\n",
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400303 __func__, rc);
304 return;
305 }
306
Chuck Lever7f23f6f2014-05-28 10:33:34 -0400307 rc = ib_req_notify_cq(cq,
308 IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS);
309 if (rc == 0)
310 return;
311 if (rc < 0) {
Chuck Leverfc664482014-05-28 10:33:25 -0400312 dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
313 __func__, rc);
314 return;
315 }
316
Chuck Lever1c00dd02014-05-28 10:33:42 -0400317 rpcrdma_recvcq_poll(cq, ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400318}
319
Chuck Levera7bc2112014-07-29 17:23:52 -0400320static void
321rpcrdma_flush_cqs(struct rpcrdma_ep *ep)
322{
Chuck Lever5c166bef2014-11-08 20:14:45 -0500323 struct ib_wc wc;
324 LIST_HEAD(sched_list);
325
326 while (ib_poll_cq(ep->rep_attr.recv_cq, 1, &wc) > 0)
327 rpcrdma_recvcq_process_wc(&wc, &sched_list);
328 if (!list_empty(&sched_list))
329 rpcrdma_schedule_tasklet(&sched_list);
330 while (ib_poll_cq(ep->rep_attr.send_cq, 1, &wc) > 0)
331 rpcrdma_sendcq_process_wc(&wc);
Chuck Levera7bc2112014-07-29 17:23:52 -0400332}
333
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400334static int
335rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
336{
337 struct rpcrdma_xprt *xprt = id->context;
338 struct rpcrdma_ia *ia = &xprt->rx_ia;
339 struct rpcrdma_ep *ep = &xprt->rx_ep;
Jeff Laytonf895b252014-11-17 16:58:04 -0500340#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400341 struct sockaddr *sap = (struct sockaddr *)&ep->rep_remote_addr;
Ingo Molnarff0db042008-11-25 16:58:42 -0800342#endif
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500343 struct ib_qp_attr *attr = &ia->ri_qp_attr;
344 struct ib_qp_init_attr *iattr = &ia->ri_qp_init_attr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400345 int connstate = 0;
346
347 switch (event->event) {
348 case RDMA_CM_EVENT_ADDR_RESOLVED:
349 case RDMA_CM_EVENT_ROUTE_RESOLVED:
Tom Talpey5675add2008-10-09 15:01:41 -0400350 ia->ri_async_rc = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400351 complete(&ia->ri_done);
352 break;
353 case RDMA_CM_EVENT_ADDR_ERROR:
354 ia->ri_async_rc = -EHOSTUNREACH;
355 dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
356 __func__, ep);
357 complete(&ia->ri_done);
358 break;
359 case RDMA_CM_EVENT_ROUTE_ERROR:
360 ia->ri_async_rc = -ENETUNREACH;
361 dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
362 __func__, ep);
363 complete(&ia->ri_done);
364 break;
365 case RDMA_CM_EVENT_ESTABLISHED:
366 connstate = 1;
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500367 ib_query_qp(ia->ri_id->qp, attr,
368 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_MAX_DEST_RD_ATOMIC,
369 iattr);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400370 dprintk("RPC: %s: %d responder resources"
371 " (%d initiator)\n",
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500372 __func__, attr->max_dest_rd_atomic,
373 attr->max_rd_atomic);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400374 goto connected;
375 case RDMA_CM_EVENT_CONNECT_ERROR:
376 connstate = -ENOTCONN;
377 goto connected;
378 case RDMA_CM_EVENT_UNREACHABLE:
379 connstate = -ENETDOWN;
380 goto connected;
381 case RDMA_CM_EVENT_REJECTED:
382 connstate = -ECONNREFUSED;
383 goto connected;
384 case RDMA_CM_EVENT_DISCONNECTED:
385 connstate = -ECONNABORTED;
386 goto connected;
387 case RDMA_CM_EVENT_DEVICE_REMOVAL:
388 connstate = -ENODEV;
389connected:
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400390 dprintk("RPC: %s: %sconnected\n",
391 __func__, connstate > 0 ? "" : "dis");
392 ep->rep_connected = connstate;
Chuck Leverafadc462015-01-21 11:03:11 -0500393 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400394 wake_up_all(&ep->rep_connect_wait);
Chuck Lever8079fb72014-07-29 17:26:12 -0400395 /*FALLTHROUGH*/
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400396 default:
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400397 dprintk("RPC: %s: %pIS:%u (ep 0x%p): %s\n",
398 __func__, sap, rpc_get_port(sap), ep,
Sagi Grimberg76357c72015-05-18 13:40:32 +0300399 rdma_event_msg(event->event));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400400 break;
401 }
402
Jeff Laytonf895b252014-11-17 16:58:04 -0500403#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400404 if (connstate == 1) {
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500405 int ird = attr->max_dest_rd_atomic;
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400406 int tird = ep->rep_remote_cma.responder_resources;
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400407
Chuck Levera0ce85f2015-03-30 14:34:21 -0400408 pr_info("rpcrdma: connection to %pIS:%u on %s, memreg '%s', %d credits, %d responders%s\n",
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400409 sap, rpc_get_port(sap),
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400410 ia->ri_id->device->name,
Chuck Levera0ce85f2015-03-30 14:34:21 -0400411 ia->ri_ops->ro_displayname,
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400412 xprt->rx_buf.rb_max_requests,
413 ird, ird < 4 && ird < tird / 2 ? " (low!)" : "");
414 } else if (connstate < 0) {
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400415 pr_info("rpcrdma: connection to %pIS:%u closed (%d)\n",
416 sap, rpc_get_port(sap), connstate);
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400417 }
418#endif
419
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400420 return 0;
421}
422
423static struct rdma_cm_id *
424rpcrdma_create_id(struct rpcrdma_xprt *xprt,
425 struct rpcrdma_ia *ia, struct sockaddr *addr)
426{
427 struct rdma_cm_id *id;
428 int rc;
429
Tom Talpey1a954052008-10-09 15:01:31 -0400430 init_completion(&ia->ri_done);
431
Sean Heftyb26f9b92010-04-01 17:08:41 +0000432 id = rdma_create_id(rpcrdma_conn_upcall, xprt, RDMA_PS_TCP, IB_QPT_RC);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400433 if (IS_ERR(id)) {
434 rc = PTR_ERR(id);
435 dprintk("RPC: %s: rdma_create_id() failed %i\n",
436 __func__, rc);
437 return id;
438 }
439
Tom Talpey5675add2008-10-09 15:01:41 -0400440 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400441 rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
442 if (rc) {
443 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
444 __func__, rc);
445 goto out;
446 }
Tom Talpey5675add2008-10-09 15:01:41 -0400447 wait_for_completion_interruptible_timeout(&ia->ri_done,
448 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400449 rc = ia->ri_async_rc;
450 if (rc)
451 goto out;
452
Tom Talpey5675add2008-10-09 15:01:41 -0400453 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400454 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
455 if (rc) {
456 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
457 __func__, rc);
458 goto out;
459 }
Tom Talpey5675add2008-10-09 15:01:41 -0400460 wait_for_completion_interruptible_timeout(&ia->ri_done,
461 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400462 rc = ia->ri_async_rc;
463 if (rc)
464 goto out;
465
466 return id;
467
468out:
469 rdma_destroy_id(id);
470 return ERR_PTR(rc);
471}
472
473/*
474 * Drain any cq, prior to teardown.
475 */
476static void
477rpcrdma_clean_cq(struct ib_cq *cq)
478{
479 struct ib_wc wc;
480 int count = 0;
481
482 while (1 == ib_poll_cq(cq, 1, &wc))
483 ++count;
484
485 if (count)
486 dprintk("RPC: %s: flushed %d events (last 0x%x)\n",
487 __func__, count, wc.opcode);
488}
489
490/*
491 * Exported functions.
492 */
493
494/*
495 * Open and initialize an Interface Adapter.
496 * o initializes fields of struct rpcrdma_ia, including
497 * interface and provider attributes and protection zone.
498 */
499int
500rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr, int memreg)
501{
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400502 int rc, mem_priv;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400503 struct rpcrdma_ia *ia = &xprt->rx_ia;
Chuck Lever7bc79722015-01-21 11:03:27 -0500504 struct ib_device_attr *devattr = &ia->ri_devattr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400505
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400506 ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
507 if (IS_ERR(ia->ri_id)) {
508 rc = PTR_ERR(ia->ri_id);
509 goto out1;
510 }
511
512 ia->ri_pd = ib_alloc_pd(ia->ri_id->device);
513 if (IS_ERR(ia->ri_pd)) {
514 rc = PTR_ERR(ia->ri_pd);
515 dprintk("RPC: %s: ib_alloc_pd() failed %i\n",
516 __func__, rc);
517 goto out2;
518 }
519
Chuck Lever7bc79722015-01-21 11:03:27 -0500520 rc = ib_query_device(ia->ri_id->device, devattr);
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400521 if (rc) {
522 dprintk("RPC: %s: ib_query_device failed %d\n",
523 __func__, rc);
Chuck Lever5ae711a2015-01-21 11:03:19 -0500524 goto out3;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400525 }
526
Chuck Lever7bc79722015-01-21 11:03:27 -0500527 if (devattr->device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY) {
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400528 ia->ri_have_dma_lkey = 1;
529 ia->ri_dma_lkey = ia->ri_id->device->local_dma_lkey;
530 }
531
Chuck Leverf10eafd2014-05-28 10:32:51 -0400532 if (memreg == RPCRDMA_FRMR) {
Tom Talpey3197d3092008-10-09 15:00:20 -0400533 /* Requires both frmr reg and local dma lkey */
Chuck Lever41f97022015-03-30 14:34:12 -0400534 if (((devattr->device_cap_flags &
Tom Talpey3197d3092008-10-09 15:00:20 -0400535 (IB_DEVICE_MEM_MGT_EXTENSIONS|IB_DEVICE_LOCAL_DMA_LKEY)) !=
Chuck Lever41f97022015-03-30 14:34:12 -0400536 (IB_DEVICE_MEM_MGT_EXTENSIONS|IB_DEVICE_LOCAL_DMA_LKEY)) ||
537 (devattr->max_fast_reg_page_list_len == 0)) {
Tom Talpey3197d3092008-10-09 15:00:20 -0400538 dprintk("RPC: %s: FRMR registration "
Chuck Leverf10eafd2014-05-28 10:32:51 -0400539 "not supported by HCA\n", __func__);
540 memreg = RPCRDMA_MTHCAFMR;
Tom Talpey3197d3092008-10-09 15:00:20 -0400541 }
Chuck Leverf10eafd2014-05-28 10:32:51 -0400542 }
543 if (memreg == RPCRDMA_MTHCAFMR) {
544 if (!ia->ri_id->device->alloc_fmr) {
545 dprintk("RPC: %s: MTHCAFMR registration "
546 "not supported by HCA\n", __func__);
Chuck Leverf10eafd2014-05-28 10:32:51 -0400547 memreg = RPCRDMA_ALLPHYSICAL;
Chuck Leverf10eafd2014-05-28 10:32:51 -0400548 }
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400549 }
550
551 /*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400552 * Optionally obtain an underlying physical identity mapping in
553 * order to do a memory window-based bind. This base registration
554 * is protected from remote access - that is enabled only by binding
555 * for the specific bytes targeted during each RPC operation, and
556 * revoked after the corresponding completion similar to a storage
557 * adapter.
558 */
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400559 switch (memreg) {
Tom Talpey3197d3092008-10-09 15:00:20 -0400560 case RPCRDMA_FRMR:
Chuck Levera0ce85f2015-03-30 14:34:21 -0400561 ia->ri_ops = &rpcrdma_frwr_memreg_ops;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400562 break;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400563 case RPCRDMA_ALLPHYSICAL:
Chuck Levera0ce85f2015-03-30 14:34:21 -0400564 ia->ri_ops = &rpcrdma_physical_memreg_ops;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400565 mem_priv = IB_ACCESS_LOCAL_WRITE |
566 IB_ACCESS_REMOTE_WRITE |
567 IB_ACCESS_REMOTE_READ;
568 goto register_setup;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400569 case RPCRDMA_MTHCAFMR:
Chuck Levera0ce85f2015-03-30 14:34:21 -0400570 ia->ri_ops = &rpcrdma_fmr_memreg_ops;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400571 if (ia->ri_have_dma_lkey)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400572 break;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400573 mem_priv = IB_ACCESS_LOCAL_WRITE;
574 register_setup:
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400575 ia->ri_bind_mem = ib_get_dma_mr(ia->ri_pd, mem_priv);
576 if (IS_ERR(ia->ri_bind_mem)) {
577 printk(KERN_ALERT "%s: ib_get_dma_mr for "
Chuck Lever0ac531c2014-05-28 10:32:43 -0400578 "phys register failed with %lX\n",
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400579 __func__, PTR_ERR(ia->ri_bind_mem));
Chuck Lever0ac531c2014-05-28 10:32:43 -0400580 rc = -ENOMEM;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500581 goto out3;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400582 }
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400583 break;
584 default:
Chuck Levercdd9ade2014-05-28 10:33:00 -0400585 printk(KERN_ERR "RPC: Unsupported memory "
586 "registration mode: %d\n", memreg);
587 rc = -ENOMEM;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500588 goto out3;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400589 }
Chuck Levera0ce85f2015-03-30 14:34:21 -0400590 dprintk("RPC: %s: memory registration strategy is '%s'\n",
591 __func__, ia->ri_ops->ro_displayname);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400592
593 /* Else will do memory reg/dereg for each chunk */
594 ia->ri_memreg_strategy = memreg;
595
Chuck Lever73806c82014-07-29 17:23:25 -0400596 rwlock_init(&ia->ri_qplock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400597 return 0;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500598
599out3:
600 ib_dealloc_pd(ia->ri_pd);
601 ia->ri_pd = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400602out2:
603 rdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400604 ia->ri_id = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400605out1:
606 return rc;
607}
608
609/*
610 * Clean up/close an IA.
611 * o if event handles and PD have been initialized, free them.
612 * o close the IA
613 */
614void
615rpcrdma_ia_close(struct rpcrdma_ia *ia)
616{
617 int rc;
618
619 dprintk("RPC: %s: entering\n", __func__);
620 if (ia->ri_bind_mem != NULL) {
621 rc = ib_dereg_mr(ia->ri_bind_mem);
622 dprintk("RPC: %s: ib_dereg_mr returned %i\n",
623 __func__, rc);
624 }
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400625 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
626 if (ia->ri_id->qp)
627 rdma_destroy_qp(ia->ri_id);
628 rdma_destroy_id(ia->ri_id);
629 ia->ri_id = NULL;
630 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400631 if (ia->ri_pd != NULL && !IS_ERR(ia->ri_pd)) {
632 rc = ib_dealloc_pd(ia->ri_pd);
633 dprintk("RPC: %s: ib_dealloc_pd returned %i\n",
634 __func__, rc);
635 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400636}
637
638/*
639 * Create unconnected endpoint.
640 */
641int
642rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
643 struct rpcrdma_create_data_internal *cdata)
644{
Chuck Lever7bc79722015-01-21 11:03:27 -0500645 struct ib_device_attr *devattr = &ia->ri_devattr;
Chuck Leverfc664482014-05-28 10:33:25 -0400646 struct ib_cq *sendcq, *recvcq;
Chuck Lever5d40a8a2007-10-26 13:30:54 -0400647 int rc, err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400648
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400649 /* check provider's send/recv wr limits */
Chuck Lever7bc79722015-01-21 11:03:27 -0500650 if (cdata->max_requests > devattr->max_qp_wr)
651 cdata->max_requests = devattr->max_qp_wr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400652
653 ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
654 ep->rep_attr.qp_context = ep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400655 ep->rep_attr.srq = NULL;
656 ep->rep_attr.cap.max_send_wr = cdata->max_requests;
Chuck Lever3968cb52015-03-30 14:35:26 -0400657 rc = ia->ri_ops->ro_open(ia, ep, cdata);
658 if (rc)
659 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400660 ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
661 ep->rep_attr.cap.max_send_sge = (cdata->padding ? 4 : 2);
662 ep->rep_attr.cap.max_recv_sge = 1;
663 ep->rep_attr.cap.max_inline_data = 0;
664 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
665 ep->rep_attr.qp_type = IB_QPT_RC;
666 ep->rep_attr.port_num = ~0;
667
Chuck Leverc05fbb52015-01-21 11:04:33 -0500668 if (cdata->padding) {
669 ep->rep_padbuf = rpcrdma_alloc_regbuf(ia, cdata->padding,
670 GFP_KERNEL);
671 if (IS_ERR(ep->rep_padbuf))
672 return PTR_ERR(ep->rep_padbuf);
673 } else
674 ep->rep_padbuf = NULL;
675
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400676 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
677 "iovs: send %d recv %d\n",
678 __func__,
679 ep->rep_attr.cap.max_send_wr,
680 ep->rep_attr.cap.max_recv_wr,
681 ep->rep_attr.cap.max_send_sge,
682 ep->rep_attr.cap.max_recv_sge);
683
684 /* set trigger for requesting send completion */
Chuck Leverfc664482014-05-28 10:33:25 -0400685 ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
Chuck Levere7104a22014-11-08 20:14:20 -0500686 if (ep->rep_cqinit > RPCRDMA_MAX_UNSIGNALED_SENDS)
687 ep->rep_cqinit = RPCRDMA_MAX_UNSIGNALED_SENDS;
688 else if (ep->rep_cqinit <= 2)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400689 ep->rep_cqinit = 0;
690 INIT_CQCOUNT(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400691 init_waitqueue_head(&ep->rep_connect_wait);
Chuck Lever254f91e2014-05-28 10:32:17 -0400692 INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400693
Chuck Leverfc664482014-05-28 10:33:25 -0400694 sendcq = ib_create_cq(ia->ri_id->device, rpcrdma_sendcq_upcall,
Chuck Lever1c00dd02014-05-28 10:33:42 -0400695 rpcrdma_cq_async_error_upcall, ep,
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400696 ep->rep_attr.cap.max_send_wr + 1, 0);
Chuck Leverfc664482014-05-28 10:33:25 -0400697 if (IS_ERR(sendcq)) {
698 rc = PTR_ERR(sendcq);
699 dprintk("RPC: %s: failed to create send CQ: %i\n",
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400700 __func__, rc);
701 goto out1;
702 }
703
Chuck Leverfc664482014-05-28 10:33:25 -0400704 rc = ib_req_notify_cq(sendcq, IB_CQ_NEXT_COMP);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400705 if (rc) {
706 dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
707 __func__, rc);
708 goto out2;
709 }
710
Chuck Leverfc664482014-05-28 10:33:25 -0400711 recvcq = ib_create_cq(ia->ri_id->device, rpcrdma_recvcq_upcall,
Chuck Lever1c00dd02014-05-28 10:33:42 -0400712 rpcrdma_cq_async_error_upcall, ep,
Chuck Leverfc664482014-05-28 10:33:25 -0400713 ep->rep_attr.cap.max_recv_wr + 1, 0);
714 if (IS_ERR(recvcq)) {
715 rc = PTR_ERR(recvcq);
716 dprintk("RPC: %s: failed to create recv CQ: %i\n",
717 __func__, rc);
718 goto out2;
719 }
720
721 rc = ib_req_notify_cq(recvcq, IB_CQ_NEXT_COMP);
722 if (rc) {
723 dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
724 __func__, rc);
725 ib_destroy_cq(recvcq);
726 goto out2;
727 }
728
729 ep->rep_attr.send_cq = sendcq;
730 ep->rep_attr.recv_cq = recvcq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400731
732 /* Initialize cma parameters */
733
734 /* RPC/RDMA does not use private data */
735 ep->rep_remote_cma.private_data = NULL;
736 ep->rep_remote_cma.private_data_len = 0;
737
738 /* Client offers RDMA Read but does not initiate */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400739 ep->rep_remote_cma.initiator_depth = 0;
Chuck Lever7bc79722015-01-21 11:03:27 -0500740 if (devattr->max_qp_rd_atom > 32) /* arbitrary but <= 255 */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400741 ep->rep_remote_cma.responder_resources = 32;
742 else
Chuck Lever7bc79722015-01-21 11:03:27 -0500743 ep->rep_remote_cma.responder_resources =
744 devattr->max_qp_rd_atom;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400745
746 ep->rep_remote_cma.retry_count = 7;
747 ep->rep_remote_cma.flow_control = 0;
748 ep->rep_remote_cma.rnr_retry_count = 0;
749
750 return 0;
751
752out2:
Chuck Leverfc664482014-05-28 10:33:25 -0400753 err = ib_destroy_cq(sendcq);
Chuck Lever5d40a8a2007-10-26 13:30:54 -0400754 if (err)
755 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
756 __func__, err);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400757out1:
Chuck Leverc05fbb52015-01-21 11:04:33 -0500758 rpcrdma_free_regbuf(ia, ep->rep_padbuf);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400759 return rc;
760}
761
762/*
763 * rpcrdma_ep_destroy
764 *
765 * Disconnect and destroy endpoint. After this, the only
766 * valid operations on the ep are to free it (if dynamically
767 * allocated) or re-create it.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400768 */
Chuck Lever7f1d5412014-05-28 10:33:16 -0400769void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400770rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
771{
772 int rc;
773
774 dprintk("RPC: %s: entering, connected is %d\n",
775 __func__, ep->rep_connected);
776
Chuck Lever254f91e2014-05-28 10:32:17 -0400777 cancel_delayed_work_sync(&ep->rep_connect_worker);
778
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400779 if (ia->ri_id->qp) {
Chuck Lever282191c2014-07-29 17:25:55 -0400780 rpcrdma_ep_disconnect(ep, ia);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400781 rdma_destroy_qp(ia->ri_id);
782 ia->ri_id->qp = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400783 }
784
Chuck Leverc05fbb52015-01-21 11:04:33 -0500785 rpcrdma_free_regbuf(ia, ep->rep_padbuf);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400786
Chuck Leverfc664482014-05-28 10:33:25 -0400787 rpcrdma_clean_cq(ep->rep_attr.recv_cq);
788 rc = ib_destroy_cq(ep->rep_attr.recv_cq);
789 if (rc)
790 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
791 __func__, rc);
792
793 rpcrdma_clean_cq(ep->rep_attr.send_cq);
794 rc = ib_destroy_cq(ep->rep_attr.send_cq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400795 if (rc)
796 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
797 __func__, rc);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400798}
799
800/*
801 * Connect unconnected endpoint.
802 */
803int
804rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
805{
Chuck Lever73806c82014-07-29 17:23:25 -0400806 struct rdma_cm_id *id, *old;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400807 int rc = 0;
808 int retry_count = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400809
Tom Talpeyc0555512008-10-10 11:32:45 -0400810 if (ep->rep_connected != 0) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400811 struct rpcrdma_xprt *xprt;
812retry:
Chuck Leverec62f402014-05-28 10:34:07 -0400813 dprintk("RPC: %s: reconnecting...\n", __func__);
Chuck Lever282191c2014-07-29 17:25:55 -0400814
815 rpcrdma_ep_disconnect(ep, ia);
Chuck Levera7bc2112014-07-29 17:23:52 -0400816 rpcrdma_flush_cqs(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400817
818 xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
Chuck Lever31a701a2015-03-30 14:35:07 -0400819 ia->ri_ops->ro_reset(xprt);
820
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400821 id = rpcrdma_create_id(xprt, ia,
822 (struct sockaddr *)&xprt->rx_data.addr);
823 if (IS_ERR(id)) {
Chuck Leverec62f402014-05-28 10:34:07 -0400824 rc = -EHOSTUNREACH;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400825 goto out;
826 }
827 /* TEMP TEMP TEMP - fail if new device:
828 * Deregister/remarshal *all* requests!
829 * Close and recreate adapter, pd, etc!
830 * Re-determine all attributes still sane!
831 * More stuff I haven't thought of!
832 * Rrrgh!
833 */
834 if (ia->ri_id->device != id->device) {
835 printk("RPC: %s: can't reconnect on "
836 "different device!\n", __func__);
837 rdma_destroy_id(id);
Chuck Leverec62f402014-05-28 10:34:07 -0400838 rc = -ENETUNREACH;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400839 goto out;
840 }
841 /* END TEMP */
Chuck Leverec62f402014-05-28 10:34:07 -0400842 rc = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
843 if (rc) {
844 dprintk("RPC: %s: rdma_create_qp failed %i\n",
845 __func__, rc);
846 rdma_destroy_id(id);
847 rc = -ENETUNREACH;
848 goto out;
849 }
Chuck Lever73806c82014-07-29 17:23:25 -0400850
851 write_lock(&ia->ri_qplock);
852 old = ia->ri_id;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400853 ia->ri_id = id;
Chuck Lever73806c82014-07-29 17:23:25 -0400854 write_unlock(&ia->ri_qplock);
855
856 rdma_destroy_qp(old);
857 rdma_destroy_id(old);
Chuck Leverec62f402014-05-28 10:34:07 -0400858 } else {
859 dprintk("RPC: %s: connecting...\n", __func__);
860 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
861 if (rc) {
862 dprintk("RPC: %s: rdma_create_qp failed %i\n",
863 __func__, rc);
864 /* do not update ep->rep_connected */
865 return -ENETUNREACH;
866 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400867 }
868
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400869 ep->rep_connected = 0;
870
871 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
872 if (rc) {
873 dprintk("RPC: %s: rdma_connect() failed with %i\n",
874 __func__, rc);
875 goto out;
876 }
877
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400878 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
879
880 /*
881 * Check state. A non-peer reject indicates no listener
882 * (ECONNREFUSED), which may be a transient state. All
883 * others indicate a transport condition which has already
884 * undergone a best-effort.
885 */
Joe Perchesf64f9e72009-11-29 16:55:45 -0800886 if (ep->rep_connected == -ECONNREFUSED &&
887 ++retry_count <= RDMA_CONNECT_RETRY_MAX) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400888 dprintk("RPC: %s: non-peer_reject, retry\n", __func__);
889 goto retry;
890 }
891 if (ep->rep_connected <= 0) {
892 /* Sometimes, the only way to reliably connect to remote
893 * CMs is to use same nonzero values for ORD and IRD. */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400894 if (retry_count++ <= RDMA_CONNECT_RETRY_MAX + 1 &&
895 (ep->rep_remote_cma.responder_resources == 0 ||
896 ep->rep_remote_cma.initiator_depth !=
897 ep->rep_remote_cma.responder_resources)) {
898 if (ep->rep_remote_cma.responder_resources == 0)
899 ep->rep_remote_cma.responder_resources = 1;
900 ep->rep_remote_cma.initiator_depth =
901 ep->rep_remote_cma.responder_resources;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400902 goto retry;
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400903 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400904 rc = ep->rep_connected;
905 } else {
906 dprintk("RPC: %s: connected\n", __func__);
907 }
908
909out:
910 if (rc)
911 ep->rep_connected = rc;
912 return rc;
913}
914
915/*
916 * rpcrdma_ep_disconnect
917 *
918 * This is separate from destroy to facilitate the ability
919 * to reconnect without recreating the endpoint.
920 *
921 * This call is not reentrant, and must not be made in parallel
922 * on the same endpoint.
923 */
Chuck Lever282191c2014-07-29 17:25:55 -0400924void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400925rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
926{
927 int rc;
928
Chuck Levera7bc2112014-07-29 17:23:52 -0400929 rpcrdma_flush_cqs(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400930 rc = rdma_disconnect(ia->ri_id);
931 if (!rc) {
932 /* returns without wait if not connected */
933 wait_event_interruptible(ep->rep_connect_wait,
934 ep->rep_connected != 1);
935 dprintk("RPC: %s: after wait, %sconnected\n", __func__,
936 (ep->rep_connected == 1) ? "still " : "dis");
937 } else {
938 dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
939 ep->rep_connected = rc;
940 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400941}
942
Chuck Lever13924022015-01-21 11:03:52 -0500943static struct rpcrdma_req *
944rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
945{
Chuck Lever13924022015-01-21 11:03:52 -0500946 struct rpcrdma_req *req;
Chuck Lever13924022015-01-21 11:03:52 -0500947
Chuck Lever85275c82015-01-21 11:04:16 -0500948 req = kzalloc(sizeof(*req), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500949 if (req == NULL)
Chuck Lever85275c82015-01-21 11:04:16 -0500950 return ERR_PTR(-ENOMEM);
Chuck Lever13924022015-01-21 11:03:52 -0500951
Chuck Lever13924022015-01-21 11:03:52 -0500952 req->rl_buffer = &r_xprt->rx_buf;
953 return req;
Chuck Lever13924022015-01-21 11:03:52 -0500954}
955
956static struct rpcrdma_rep *
957rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt)
958{
959 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Lever13924022015-01-21 11:03:52 -0500960 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
961 struct rpcrdma_rep *rep;
962 int rc;
963
964 rc = -ENOMEM;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500965 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500966 if (rep == NULL)
967 goto out;
Chuck Lever13924022015-01-21 11:03:52 -0500968
Chuck Lever6b1184c2015-01-21 11:04:25 -0500969 rep->rr_rdmabuf = rpcrdma_alloc_regbuf(ia, cdata->inline_rsize,
970 GFP_KERNEL);
971 if (IS_ERR(rep->rr_rdmabuf)) {
972 rc = PTR_ERR(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -0500973 goto out_free;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500974 }
Chuck Lever13924022015-01-21 11:03:52 -0500975
976 rep->rr_buffer = &r_xprt->rx_buf;
977 return rep;
978
979out_free:
980 kfree(rep);
981out:
982 return ERR_PTR(rc);
983}
984
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400985int
Chuck Leverac920d02015-01-21 11:03:44 -0500986rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400987{
Chuck Leverac920d02015-01-21 11:03:44 -0500988 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
989 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
990 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400991 char *p;
Chuck Lever13924022015-01-21 11:03:52 -0500992 size_t len;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400993 int i, rc;
994
995 buf->rb_max_requests = cdata->max_requests;
996 spin_lock_init(&buf->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400997
998 /* Need to allocate:
999 * 1. arrays for send and recv pointers
1000 * 2. arrays of struct rpcrdma_req to fill in pointers
1001 * 3. array of struct rpcrdma_rep for replies
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001002 * Send/recv buffers in req/rep need to be registered
1003 */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001004 len = buf->rb_max_requests *
1005 (sizeof(struct rpcrdma_req *) + sizeof(struct rpcrdma_rep *));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001006
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001007 p = kzalloc(len, GFP_KERNEL);
1008 if (p == NULL) {
1009 dprintk("RPC: %s: req_t/rep_t/pad kzalloc(%zd) failed\n",
1010 __func__, len);
1011 rc = -ENOMEM;
1012 goto out;
1013 }
1014 buf->rb_pool = p; /* for freeing it later */
1015
1016 buf->rb_send_bufs = (struct rpcrdma_req **) p;
1017 p = (char *) &buf->rb_send_bufs[buf->rb_max_requests];
1018 buf->rb_recv_bufs = (struct rpcrdma_rep **) p;
1019 p = (char *) &buf->rb_recv_bufs[buf->rb_max_requests];
1020
Chuck Lever91e70e72015-03-30 14:34:58 -04001021 rc = ia->ri_ops->ro_init(r_xprt);
1022 if (rc)
1023 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001024
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001025 for (i = 0; i < buf->rb_max_requests; i++) {
1026 struct rpcrdma_req *req;
1027 struct rpcrdma_rep *rep;
1028
Chuck Lever13924022015-01-21 11:03:52 -05001029 req = rpcrdma_create_req(r_xprt);
1030 if (IS_ERR(req)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001031 dprintk("RPC: %s: request buffer %d alloc"
1032 " failed\n", __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -05001033 rc = PTR_ERR(req);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001034 goto out;
1035 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001036 buf->rb_send_bufs[i] = req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001037
Chuck Lever13924022015-01-21 11:03:52 -05001038 rep = rpcrdma_create_rep(r_xprt);
1039 if (IS_ERR(rep)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001040 dprintk("RPC: %s: reply buffer %d alloc failed\n",
1041 __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -05001042 rc = PTR_ERR(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001043 goto out;
1044 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001045 buf->rb_recv_bufs[i] = rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001046 }
Chuck Lever13924022015-01-21 11:03:52 -05001047
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001048 return 0;
1049out:
1050 rpcrdma_buffer_destroy(buf);
1051 return rc;
1052}
1053
Chuck Lever2e845222014-07-29 17:25:38 -04001054static void
Chuck Lever13924022015-01-21 11:03:52 -05001055rpcrdma_destroy_rep(struct rpcrdma_ia *ia, struct rpcrdma_rep *rep)
1056{
1057 if (!rep)
1058 return;
1059
Chuck Lever6b1184c2015-01-21 11:04:25 -05001060 rpcrdma_free_regbuf(ia, rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001061 kfree(rep);
1062}
1063
1064static void
1065rpcrdma_destroy_req(struct rpcrdma_ia *ia, struct rpcrdma_req *req)
1066{
1067 if (!req)
1068 return;
1069
Chuck Lever0ca77dc2015-01-21 11:04:08 -05001070 rpcrdma_free_regbuf(ia, req->rl_sendbuf);
Chuck Lever85275c82015-01-21 11:04:16 -05001071 rpcrdma_free_regbuf(ia, req->rl_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001072 kfree(req);
1073}
1074
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001075void
1076rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1077{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001078 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
Chuck Lever2e845222014-07-29 17:25:38 -04001079 int i;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001080
1081 /* clean up in reverse order from create
1082 * 1. recv mr memory (mr free, then kfree)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001083 * 2. send mr memory (mr free, then kfree)
Chuck Lever2e845222014-07-29 17:25:38 -04001084 * 3. MWs
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001085 */
1086 dprintk("RPC: %s: entering\n", __func__);
1087
1088 for (i = 0; i < buf->rb_max_requests; i++) {
Chuck Lever13924022015-01-21 11:03:52 -05001089 if (buf->rb_recv_bufs)
1090 rpcrdma_destroy_rep(ia, buf->rb_recv_bufs[i]);
1091 if (buf->rb_send_bufs)
1092 rpcrdma_destroy_req(ia, buf->rb_send_bufs[i]);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001093 }
1094
Chuck Lever4561f342015-03-30 14:35:17 -04001095 ia->ri_ops->ro_destroy(buf);
Allen Andrews4034ba02014-05-28 10:32:09 -04001096
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001097 kfree(buf->rb_pool);
1098}
1099
Chuck Leverc2922c02014-07-29 17:24:36 -04001100/* "*mw" can be NULL when rpcrdma_buffer_get_mrs() fails, leaving
1101 * some req segments uninitialized.
1102 */
1103static void
1104rpcrdma_buffer_put_mr(struct rpcrdma_mw **mw, struct rpcrdma_buffer *buf)
1105{
1106 if (*mw) {
1107 list_add_tail(&(*mw)->mw_list, &buf->rb_mws);
1108 *mw = NULL;
1109 }
1110}
1111
1112/* Cycle mw's back in reverse order, and "spin" them.
1113 * This delays and scrambles reuse as much as possible.
1114 */
1115static void
1116rpcrdma_buffer_put_mrs(struct rpcrdma_req *req, struct rpcrdma_buffer *buf)
1117{
1118 struct rpcrdma_mr_seg *seg = req->rl_segments;
1119 struct rpcrdma_mr_seg *seg1 = seg;
1120 int i;
1121
1122 for (i = 1, seg++; i < RPCRDMA_MAX_SEGS; seg++, i++)
Chuck Lever3eb35812015-01-21 11:02:54 -05001123 rpcrdma_buffer_put_mr(&seg->rl_mw, buf);
1124 rpcrdma_buffer_put_mr(&seg1->rl_mw, buf);
Chuck Leverc2922c02014-07-29 17:24:36 -04001125}
1126
1127static void
1128rpcrdma_buffer_put_sendbuf(struct rpcrdma_req *req, struct rpcrdma_buffer *buf)
1129{
1130 buf->rb_send_bufs[--buf->rb_send_index] = req;
1131 req->rl_niovs = 0;
1132 if (req->rl_reply) {
1133 buf->rb_recv_bufs[--buf->rb_recv_index] = req->rl_reply;
1134 req->rl_reply->rr_func = NULL;
1135 req->rl_reply = NULL;
1136 }
1137}
1138
Chuck Lever6814bae2015-03-30 14:34:48 -04001139/* rpcrdma_unmap_one() was already done during deregistration.
Chuck Leverddb6beb2014-07-29 17:24:54 -04001140 * Redo only the ib_post_send().
1141 */
1142static void
1143rpcrdma_retry_local_inv(struct rpcrdma_mw *r, struct rpcrdma_ia *ia)
1144{
1145 struct rpcrdma_xprt *r_xprt =
1146 container_of(ia, struct rpcrdma_xprt, rx_ia);
1147 struct ib_send_wr invalidate_wr, *bad_wr;
1148 int rc;
1149
1150 dprintk("RPC: %s: FRMR %p is stale\n", __func__, r);
1151
1152 /* When this FRMR is re-inserted into rb_mws, it is no longer stale */
Chuck Leverdab7e3b2014-07-29 17:25:20 -04001153 r->r.frmr.fr_state = FRMR_IS_INVALID;
Chuck Leverddb6beb2014-07-29 17:24:54 -04001154
1155 memset(&invalidate_wr, 0, sizeof(invalidate_wr));
1156 invalidate_wr.wr_id = (unsigned long)(void *)r;
1157 invalidate_wr.opcode = IB_WR_LOCAL_INV;
Chuck Leverddb6beb2014-07-29 17:24:54 -04001158 invalidate_wr.ex.invalidate_rkey = r->r.frmr.fr_mr->rkey;
1159 DECR_CQCOUNT(&r_xprt->rx_ep);
1160
1161 dprintk("RPC: %s: frmr %p invalidating rkey %08x\n",
1162 __func__, r, r->r.frmr.fr_mr->rkey);
1163
1164 read_lock(&ia->ri_qplock);
1165 rc = ib_post_send(ia->ri_id->qp, &invalidate_wr, &bad_wr);
1166 read_unlock(&ia->ri_qplock);
1167 if (rc) {
1168 /* Force rpcrdma_buffer_get() to retry */
1169 r->r.frmr.fr_state = FRMR_IS_STALE;
1170 dprintk("RPC: %s: ib_post_send failed, %i\n",
1171 __func__, rc);
1172 }
1173}
1174
1175static void
1176rpcrdma_retry_flushed_linv(struct list_head *stale,
1177 struct rpcrdma_buffer *buf)
1178{
1179 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
1180 struct list_head *pos;
1181 struct rpcrdma_mw *r;
1182 unsigned long flags;
1183
1184 list_for_each(pos, stale) {
1185 r = list_entry(pos, struct rpcrdma_mw, mw_list);
1186 rpcrdma_retry_local_inv(r, ia);
1187 }
1188
1189 spin_lock_irqsave(&buf->rb_lock, flags);
1190 list_splice_tail(stale, &buf->rb_mws);
1191 spin_unlock_irqrestore(&buf->rb_lock, flags);
1192}
1193
Chuck Leverc2922c02014-07-29 17:24:36 -04001194static struct rpcrdma_req *
Chuck Leverddb6beb2014-07-29 17:24:54 -04001195rpcrdma_buffer_get_frmrs(struct rpcrdma_req *req, struct rpcrdma_buffer *buf,
1196 struct list_head *stale)
1197{
1198 struct rpcrdma_mw *r;
1199 int i;
1200
1201 i = RPCRDMA_MAX_SEGS - 1;
1202 while (!list_empty(&buf->rb_mws)) {
1203 r = list_entry(buf->rb_mws.next,
1204 struct rpcrdma_mw, mw_list);
1205 list_del(&r->mw_list);
1206 if (r->r.frmr.fr_state == FRMR_IS_STALE) {
1207 list_add(&r->mw_list, stale);
1208 continue;
1209 }
Chuck Lever3eb35812015-01-21 11:02:54 -05001210 req->rl_segments[i].rl_mw = r;
Chuck Leverddb6beb2014-07-29 17:24:54 -04001211 if (unlikely(i-- == 0))
1212 return req; /* Success */
1213 }
1214
1215 /* Not enough entries on rb_mws for this req */
1216 rpcrdma_buffer_put_sendbuf(req, buf);
1217 rpcrdma_buffer_put_mrs(req, buf);
1218 return NULL;
1219}
1220
1221static struct rpcrdma_req *
1222rpcrdma_buffer_get_fmrs(struct rpcrdma_req *req, struct rpcrdma_buffer *buf)
Chuck Leverc2922c02014-07-29 17:24:36 -04001223{
1224 struct rpcrdma_mw *r;
1225 int i;
1226
1227 i = RPCRDMA_MAX_SEGS - 1;
1228 while (!list_empty(&buf->rb_mws)) {
1229 r = list_entry(buf->rb_mws.next,
1230 struct rpcrdma_mw, mw_list);
1231 list_del(&r->mw_list);
Chuck Lever3eb35812015-01-21 11:02:54 -05001232 req->rl_segments[i].rl_mw = r;
Chuck Leverc2922c02014-07-29 17:24:36 -04001233 if (unlikely(i-- == 0))
1234 return req; /* Success */
1235 }
1236
1237 /* Not enough entries on rb_mws for this req */
1238 rpcrdma_buffer_put_sendbuf(req, buf);
1239 rpcrdma_buffer_put_mrs(req, buf);
1240 return NULL;
1241}
1242
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001243/*
1244 * Get a set of request/reply buffers.
1245 *
1246 * Reply buffer (if needed) is attached to send buffer upon return.
1247 * Rule:
1248 * rb_send_index and rb_recv_index MUST always be pointing to the
1249 * *next* available buffer (non-NULL). They are incremented after
1250 * removing buffers, and decremented *before* returning them.
1251 */
1252struct rpcrdma_req *
1253rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1254{
Chuck Leverc2922c02014-07-29 17:24:36 -04001255 struct rpcrdma_ia *ia = rdmab_to_ia(buffers);
Chuck Leverddb6beb2014-07-29 17:24:54 -04001256 struct list_head stale;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001257 struct rpcrdma_req *req;
1258 unsigned long flags;
1259
1260 spin_lock_irqsave(&buffers->rb_lock, flags);
1261 if (buffers->rb_send_index == buffers->rb_max_requests) {
1262 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1263 dprintk("RPC: %s: out of request buffers\n", __func__);
1264 return ((struct rpcrdma_req *)NULL);
1265 }
1266
1267 req = buffers->rb_send_bufs[buffers->rb_send_index];
1268 if (buffers->rb_send_index < buffers->rb_recv_index) {
1269 dprintk("RPC: %s: %d extra receives outstanding (ok)\n",
1270 __func__,
1271 buffers->rb_recv_index - buffers->rb_send_index);
1272 req->rl_reply = NULL;
1273 } else {
1274 req->rl_reply = buffers->rb_recv_bufs[buffers->rb_recv_index];
1275 buffers->rb_recv_bufs[buffers->rb_recv_index++] = NULL;
1276 }
1277 buffers->rb_send_bufs[buffers->rb_send_index++] = NULL;
Chuck Leverddb6beb2014-07-29 17:24:54 -04001278
1279 INIT_LIST_HEAD(&stale);
Chuck Leverc2922c02014-07-29 17:24:36 -04001280 switch (ia->ri_memreg_strategy) {
1281 case RPCRDMA_FRMR:
Chuck Leverddb6beb2014-07-29 17:24:54 -04001282 req = rpcrdma_buffer_get_frmrs(req, buffers, &stale);
1283 break;
Chuck Leverc2922c02014-07-29 17:24:36 -04001284 case RPCRDMA_MTHCAFMR:
Chuck Leverddb6beb2014-07-29 17:24:54 -04001285 req = rpcrdma_buffer_get_fmrs(req, buffers);
Chuck Leverc2922c02014-07-29 17:24:36 -04001286 break;
1287 default:
1288 break;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001289 }
1290 spin_unlock_irqrestore(&buffers->rb_lock, flags);
Chuck Leverddb6beb2014-07-29 17:24:54 -04001291 if (!list_empty(&stale))
1292 rpcrdma_retry_flushed_linv(&stale, buffers);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001293 return req;
1294}
1295
1296/*
1297 * Put request/reply buffers back into pool.
1298 * Pre-decrement counter/array index.
1299 */
1300void
1301rpcrdma_buffer_put(struct rpcrdma_req *req)
1302{
1303 struct rpcrdma_buffer *buffers = req->rl_buffer;
1304 struct rpcrdma_ia *ia = rdmab_to_ia(buffers);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001305 unsigned long flags;
1306
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001307 spin_lock_irqsave(&buffers->rb_lock, flags);
Chuck Leverc2922c02014-07-29 17:24:36 -04001308 rpcrdma_buffer_put_sendbuf(req, buffers);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001309 switch (ia->ri_memreg_strategy) {
Tom Talpey3197d3092008-10-09 15:00:20 -04001310 case RPCRDMA_FRMR:
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001311 case RPCRDMA_MTHCAFMR:
Chuck Leverc2922c02014-07-29 17:24:36 -04001312 rpcrdma_buffer_put_mrs(req, buffers);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001313 break;
1314 default:
1315 break;
1316 }
1317 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1318}
1319
1320/*
1321 * Recover reply buffers from pool.
1322 * This happens when recovering from error conditions.
1323 * Post-increment counter/array index.
1324 */
1325void
1326rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1327{
1328 struct rpcrdma_buffer *buffers = req->rl_buffer;
1329 unsigned long flags;
1330
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001331 spin_lock_irqsave(&buffers->rb_lock, flags);
1332 if (buffers->rb_recv_index < buffers->rb_max_requests) {
1333 req->rl_reply = buffers->rb_recv_bufs[buffers->rb_recv_index];
1334 buffers->rb_recv_bufs[buffers->rb_recv_index++] = NULL;
1335 }
1336 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1337}
1338
1339/*
1340 * Put reply buffers back into pool when not attached to
Chuck Leverb45ccfd2014-05-28 10:32:34 -04001341 * request. This happens in error conditions.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001342 */
1343void
1344rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1345{
1346 struct rpcrdma_buffer *buffers = rep->rr_buffer;
1347 unsigned long flags;
1348
1349 rep->rr_func = NULL;
1350 spin_lock_irqsave(&buffers->rb_lock, flags);
1351 buffers->rb_recv_bufs[--buffers->rb_recv_index] = rep;
1352 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1353}
1354
1355/*
1356 * Wrappers for internal-use kmalloc memory registration, used by buffer code.
1357 */
1358
Chuck Leverd6547882015-03-30 14:35:44 -04001359void
1360rpcrdma_mapping_error(struct rpcrdma_mr_seg *seg)
1361{
1362 dprintk("RPC: map_one: offset %p iova %llx len %zu\n",
1363 seg->mr_offset,
1364 (unsigned long long)seg->mr_dma, seg->mr_dmalen);
1365}
1366
Chuck Leverdf515ca2015-01-21 11:04:41 -05001367static int
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001368rpcrdma_register_internal(struct rpcrdma_ia *ia, void *va, int len,
1369 struct ib_mr **mrp, struct ib_sge *iov)
1370{
1371 struct ib_phys_buf ipb;
1372 struct ib_mr *mr;
1373 int rc;
1374
1375 /*
1376 * All memory passed here was kmalloc'ed, therefore phys-contiguous.
1377 */
1378 iov->addr = ib_dma_map_single(ia->ri_id->device,
1379 va, len, DMA_BIDIRECTIONAL);
Yan Burmanbf858ab2014-06-19 16:06:30 +03001380 if (ib_dma_mapping_error(ia->ri_id->device, iov->addr))
1381 return -ENOMEM;
1382
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001383 iov->length = len;
1384
Tom Talpeybd7ed1d2008-10-09 15:00:09 -04001385 if (ia->ri_have_dma_lkey) {
1386 *mrp = NULL;
1387 iov->lkey = ia->ri_dma_lkey;
1388 return 0;
1389 } else if (ia->ri_bind_mem != NULL) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001390 *mrp = NULL;
1391 iov->lkey = ia->ri_bind_mem->lkey;
1392 return 0;
1393 }
1394
1395 ipb.addr = iov->addr;
1396 ipb.size = iov->length;
1397 mr = ib_reg_phys_mr(ia->ri_pd, &ipb, 1,
1398 IB_ACCESS_LOCAL_WRITE, &iov->addr);
1399
1400 dprintk("RPC: %s: phys convert: 0x%llx "
1401 "registered 0x%llx length %d\n",
Andrew Mortona56daeb2007-10-16 01:29:57 -07001402 __func__, (unsigned long long)ipb.addr,
1403 (unsigned long long)iov->addr, len);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001404
1405 if (IS_ERR(mr)) {
1406 *mrp = NULL;
1407 rc = PTR_ERR(mr);
1408 dprintk("RPC: %s: failed with %i\n", __func__, rc);
1409 } else {
1410 *mrp = mr;
1411 iov->lkey = mr->lkey;
1412 rc = 0;
1413 }
1414
1415 return rc;
1416}
1417
Chuck Leverdf515ca2015-01-21 11:04:41 -05001418static int
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001419rpcrdma_deregister_internal(struct rpcrdma_ia *ia,
1420 struct ib_mr *mr, struct ib_sge *iov)
1421{
1422 int rc;
1423
1424 ib_dma_unmap_single(ia->ri_id->device,
1425 iov->addr, iov->length, DMA_BIDIRECTIONAL);
1426
1427 if (NULL == mr)
1428 return 0;
1429
1430 rc = ib_dereg_mr(mr);
1431 if (rc)
1432 dprintk("RPC: %s: ib_dereg_mr failed %i\n", __func__, rc);
1433 return rc;
1434}
1435
Chuck Lever9128c3e2015-01-21 11:04:00 -05001436/**
1437 * rpcrdma_alloc_regbuf - kmalloc and register memory for SEND/RECV buffers
1438 * @ia: controlling rpcrdma_ia
1439 * @size: size of buffer to be allocated, in bytes
1440 * @flags: GFP flags
1441 *
1442 * Returns pointer to private header of an area of internally
1443 * registered memory, or an ERR_PTR. The registered buffer follows
1444 * the end of the private header.
1445 *
1446 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
1447 * receiving the payload of RDMA RECV operations. regbufs are not
1448 * used for RDMA READ/WRITE operations, thus are registered only for
1449 * LOCAL access.
1450 */
1451struct rpcrdma_regbuf *
1452rpcrdma_alloc_regbuf(struct rpcrdma_ia *ia, size_t size, gfp_t flags)
1453{
1454 struct rpcrdma_regbuf *rb;
1455 int rc;
1456
1457 rc = -ENOMEM;
1458 rb = kmalloc(sizeof(*rb) + size, flags);
1459 if (rb == NULL)
1460 goto out;
1461
1462 rb->rg_size = size;
1463 rb->rg_owner = NULL;
1464 rc = rpcrdma_register_internal(ia, rb->rg_base, size,
1465 &rb->rg_mr, &rb->rg_iov);
1466 if (rc)
1467 goto out_free;
1468
1469 return rb;
1470
1471out_free:
1472 kfree(rb);
1473out:
1474 return ERR_PTR(rc);
1475}
1476
1477/**
1478 * rpcrdma_free_regbuf - deregister and free registered buffer
1479 * @ia: controlling rpcrdma_ia
1480 * @rb: regbuf to be deregistered and freed
1481 */
1482void
1483rpcrdma_free_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1484{
1485 if (rb) {
1486 rpcrdma_deregister_internal(ia, rb->rg_mr, &rb->rg_iov);
1487 kfree(rb);
1488 }
1489}
1490
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001491/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001492 * Prepost any receive buffer, then post send.
1493 *
1494 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1495 */
1496int
1497rpcrdma_ep_post(struct rpcrdma_ia *ia,
1498 struct rpcrdma_ep *ep,
1499 struct rpcrdma_req *req)
1500{
1501 struct ib_send_wr send_wr, *send_wr_fail;
1502 struct rpcrdma_rep *rep = req->rl_reply;
1503 int rc;
1504
1505 if (rep) {
1506 rc = rpcrdma_ep_post_recv(ia, ep, rep);
1507 if (rc)
1508 goto out;
1509 req->rl_reply = NULL;
1510 }
1511
1512 send_wr.next = NULL;
Chuck Levere46ac342015-03-30 14:35:35 -04001513 send_wr.wr_id = RPCRDMA_IGNORE_COMPLETION;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001514 send_wr.sg_list = req->rl_send_iov;
1515 send_wr.num_sge = req->rl_niovs;
1516 send_wr.opcode = IB_WR_SEND;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001517 if (send_wr.num_sge == 4) /* no need to sync any pad (constant) */
1518 ib_dma_sync_single_for_device(ia->ri_id->device,
1519 req->rl_send_iov[3].addr, req->rl_send_iov[3].length,
1520 DMA_TO_DEVICE);
1521 ib_dma_sync_single_for_device(ia->ri_id->device,
1522 req->rl_send_iov[1].addr, req->rl_send_iov[1].length,
1523 DMA_TO_DEVICE);
1524 ib_dma_sync_single_for_device(ia->ri_id->device,
1525 req->rl_send_iov[0].addr, req->rl_send_iov[0].length,
1526 DMA_TO_DEVICE);
1527
1528 if (DECR_CQCOUNT(ep) > 0)
1529 send_wr.send_flags = 0;
1530 else { /* Provider must take a send completion every now and then */
1531 INIT_CQCOUNT(ep);
1532 send_wr.send_flags = IB_SEND_SIGNALED;
1533 }
1534
1535 rc = ib_post_send(ia->ri_id->qp, &send_wr, &send_wr_fail);
1536 if (rc)
1537 dprintk("RPC: %s: ib_post_send returned %i\n", __func__,
1538 rc);
1539out:
1540 return rc;
1541}
1542
1543/*
1544 * (Re)post a receive buffer.
1545 */
1546int
1547rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
1548 struct rpcrdma_ep *ep,
1549 struct rpcrdma_rep *rep)
1550{
1551 struct ib_recv_wr recv_wr, *recv_wr_fail;
1552 int rc;
1553
1554 recv_wr.next = NULL;
1555 recv_wr.wr_id = (u64) (unsigned long) rep;
Chuck Lever6b1184c2015-01-21 11:04:25 -05001556 recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001557 recv_wr.num_sge = 1;
1558
1559 ib_dma_sync_single_for_cpu(ia->ri_id->device,
Chuck Lever6b1184c2015-01-21 11:04:25 -05001560 rdmab_addr(rep->rr_rdmabuf),
1561 rdmab_length(rep->rr_rdmabuf),
1562 DMA_BIDIRECTIONAL);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001563
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001564 rc = ib_post_recv(ia->ri_id->qp, &recv_wr, &recv_wr_fail);
1565
1566 if (rc)
1567 dprintk("RPC: %s: ib_post_recv returned %i\n", __func__,
1568 rc);
1569 return rc;
1570}
Chuck Lever43e95982014-07-29 17:23:34 -04001571
Chuck Lever1c9351e2015-03-30 14:34:30 -04001572/* How many chunk list items fit within our inline buffers?
Chuck Lever43e95982014-07-29 17:23:34 -04001573 */
Chuck Lever1c9351e2015-03-30 14:34:30 -04001574unsigned int
1575rpcrdma_max_segments(struct rpcrdma_xprt *r_xprt)
Chuck Lever43e95982014-07-29 17:23:34 -04001576{
1577 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Lever1c9351e2015-03-30 14:34:30 -04001578 int bytes, segments;
Chuck Lever43e95982014-07-29 17:23:34 -04001579
Chuck Lever1c9351e2015-03-30 14:34:30 -04001580 bytes = min_t(unsigned int, cdata->inline_wsize, cdata->inline_rsize);
1581 bytes -= RPCRDMA_HDRLEN_MIN;
1582 if (bytes < sizeof(struct rpcrdma_segment) * 2) {
1583 pr_warn("RPC: %s: inline threshold too small\n",
1584 __func__);
1585 return 0;
Chuck Lever43e95982014-07-29 17:23:34 -04001586 }
Chuck Lever1c9351e2015-03-30 14:34:30 -04001587
1588 segments = 1 << (fls(bytes / sizeof(struct rpcrdma_segment)) - 1);
1589 dprintk("RPC: %s: max chunk list size = %d segments\n",
1590 __func__, segments);
1591 return segments;
Chuck Lever43e95982014-07-29 17:23:34 -04001592}