blob: c09f1b6c3f0a1c6eec56e95292066c7598fe6070 [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>
Devesh Sharmad0f36c42015-08-03 13:05:04 -040055#include <linux/module.h> /* try_module_get()/module_put() */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040056
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -040057#include "xprt_rdma.h"
58
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040059/*
60 * Globals/Macros
61 */
62
Jeff Laytonf895b252014-11-17 16:58:04 -050063#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040064# define RPCDBG_FACILITY RPCDBG_TRANS
65#endif
66
67/*
68 * internal functions
69 */
70
71/*
72 * handle replies in tasklet context, using a single, global list
73 * rdma tasklet function -- just turn around and call the func
74 * for all replies on the list
75 */
76
77static DEFINE_SPINLOCK(rpcrdma_tk_lock_g);
78static LIST_HEAD(rpcrdma_tasklets_g);
79
80static void
81rpcrdma_run_tasklet(unsigned long data)
82{
83 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040084 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);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040092 spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags);
93
Chuck Lever494ae302015-05-26 11:51:46 -040094 rpcrdma_reply_handler(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040095
96 spin_lock_irqsave(&rpcrdma_tk_lock_g, flags);
97 }
98 spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags);
99}
100
101static DECLARE_TASKLET(rpcrdma_tasklet_g, rpcrdma_run_tasklet, 0UL);
102
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400103static void
Chuck Leverf1a03b72014-11-08 20:14:37 -0500104rpcrdma_schedule_tasklet(struct list_head *sched_list)
105{
106 unsigned long flags;
107
108 spin_lock_irqsave(&rpcrdma_tk_lock_g, flags);
109 list_splice_tail(sched_list, &rpcrdma_tasklets_g);
110 spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags);
111 tasklet_schedule(&rpcrdma_tasklet_g);
112}
113
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400114static void
115rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context)
116{
117 struct rpcrdma_ep *ep = context;
118
Chuck Lever7ff11de2014-11-08 20:15:01 -0500119 pr_err("RPC: %s: %s on device %s ep %p\n",
Sagi Grimberg76357c72015-05-18 13:40:32 +0300120 __func__, ib_event_msg(event->event),
Chuck Lever7ff11de2014-11-08 20:15:01 -0500121 event->device->name, context);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400122 if (ep->rep_connected == 1) {
123 ep->rep_connected = -EIO;
Chuck Leverafadc462015-01-21 11:03:11 -0500124 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400125 wake_up_all(&ep->rep_connect_wait);
126 }
127}
128
129static void
130rpcrdma_cq_async_error_upcall(struct ib_event *event, void *context)
131{
132 struct rpcrdma_ep *ep = context;
133
Chuck Lever7ff11de2014-11-08 20:15:01 -0500134 pr_err("RPC: %s: %s on device %s ep %p\n",
Sagi Grimberg76357c72015-05-18 13:40:32 +0300135 __func__, ib_event_msg(event->event),
Chuck Lever7ff11de2014-11-08 20:15:01 -0500136 event->device->name, context);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400137 if (ep->rep_connected == 1) {
138 ep->rep_connected = -EIO;
Chuck Leverafadc462015-01-21 11:03:11 -0500139 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400140 wake_up_all(&ep->rep_connect_wait);
141 }
142}
143
Chuck Leverfc664482014-05-28 10:33:25 -0400144static void
145rpcrdma_sendcq_process_wc(struct ib_wc *wc)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400146{
Chuck Lever85024272015-01-21 11:02:04 -0500147 /* WARNING: Only wr_id and status are reliable at this point */
Chuck Levere46ac342015-03-30 14:35:35 -0400148 if (wc->wr_id == RPCRDMA_IGNORE_COMPLETION) {
149 if (wc->status != IB_WC_SUCCESS &&
150 wc->status != IB_WC_WR_FLUSH_ERR)
Chuck Lever85024272015-01-21 11:02:04 -0500151 pr_err("RPC: %s: SEND: %s\n",
Sagi Grimberg76357c72015-05-18 13:40:32 +0300152 __func__, ib_wc_status_msg(wc->status));
Chuck Lever85024272015-01-21 11:02:04 -0500153 } else {
154 struct rpcrdma_mw *r;
155
156 r = (struct rpcrdma_mw *)(unsigned long)wc->wr_id;
Chuck Levere46ac342015-03-30 14:35:35 -0400157 r->mw_sendcompletion(wc);
Chuck Lever85024272015-01-21 11:02:04 -0500158 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400159}
160
Chuck Lever4220a072015-10-24 17:26:45 -0400161/* The common case is a single send completion is waiting. By
162 * passing two WC entries to ib_poll_cq, a return code of 1
163 * means there is exactly one WC waiting and no more. We don't
164 * have to invoke ib_poll_cq again to know that the CQ has been
165 * properly drained.
166 */
167static void
168rpcrdma_sendcq_poll(struct ib_cq *cq)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400169{
Chuck Lever4220a072015-10-24 17:26:45 -0400170 struct ib_wc *pos, wcs[2];
171 int count, rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400172
Chuck Lever1c00dd02014-05-28 10:33:42 -0400173 do {
Chuck Lever4220a072015-10-24 17:26:45 -0400174 pos = wcs;
Chuck Lever1c00dd02014-05-28 10:33:42 -0400175
Chuck Lever4220a072015-10-24 17:26:45 -0400176 rc = ib_poll_cq(cq, ARRAY_SIZE(wcs), pos);
177 if (rc < 0)
178 break;
Chuck Lever1c00dd02014-05-28 10:33:42 -0400179
180 count = rc;
181 while (count-- > 0)
Chuck Lever4220a072015-10-24 17:26:45 -0400182 rpcrdma_sendcq_process_wc(pos++);
183 } while (rc == ARRAY_SIZE(wcs));
184 return;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400185}
186
Chuck Lever7b3d7702015-10-24 17:26:37 -0400187/* Handle provider send completion upcalls.
Chuck Leverfc664482014-05-28 10:33:25 -0400188 */
189static void
190rpcrdma_sendcq_upcall(struct ib_cq *cq, void *cq_context)
191{
Chuck Lever7b3d7702015-10-24 17:26:37 -0400192 do {
Chuck Lever4220a072015-10-24 17:26:45 -0400193 rpcrdma_sendcq_poll(cq);
Chuck Lever7b3d7702015-10-24 17:26:37 -0400194 } while (ib_req_notify_cq(cq, IB_CQ_NEXT_COMP |
195 IB_CQ_REPORT_MISSED_EVENTS) > 0);
Chuck Leverfc664482014-05-28 10:33:25 -0400196}
197
198static void
Chuck Leverbb961932014-07-29 17:25:46 -0400199rpcrdma_recvcq_process_wc(struct ib_wc *wc, struct list_head *sched_list)
Chuck Leverfc664482014-05-28 10:33:25 -0400200{
201 struct rpcrdma_rep *rep =
202 (struct rpcrdma_rep *)(unsigned long)wc->wr_id;
203
Chuck Lever85024272015-01-21 11:02:04 -0500204 /* WARNING: Only wr_id and status are reliable at this point */
205 if (wc->status != IB_WC_SUCCESS)
206 goto out_fail;
Chuck Leverfc664482014-05-28 10:33:25 -0400207
Chuck Lever85024272015-01-21 11:02:04 -0500208 /* status == SUCCESS means all fields in wc are trustworthy */
Chuck Leverfc664482014-05-28 10:33:25 -0400209 if (wc->opcode != IB_WC_RECV)
210 return;
211
Chuck Lever85024272015-01-21 11:02:04 -0500212 dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n",
213 __func__, rep, wc->byte_len);
214
Chuck Leverfc664482014-05-28 10:33:25 -0400215 rep->rr_len = wc->byte_len;
Chuck Lever89e0d1122015-05-26 11:51:56 -0400216 ib_dma_sync_single_for_cpu(rep->rr_device,
Chuck Lever6b1184c2015-01-21 11:04:25 -0500217 rdmab_addr(rep->rr_rdmabuf),
218 rep->rr_len, DMA_FROM_DEVICE);
219 prefetch(rdmab_to_msg(rep->rr_rdmabuf));
Chuck Leverfc664482014-05-28 10:33:25 -0400220
221out_schedule:
Chuck Leverbb961932014-07-29 17:25:46 -0400222 list_add_tail(&rep->rr_list, sched_list);
Chuck Lever85024272015-01-21 11:02:04 -0500223 return;
224out_fail:
225 if (wc->status != IB_WC_WR_FLUSH_ERR)
226 pr_err("RPC: %s: rep %p: %s\n",
Sagi Grimberg76357c72015-05-18 13:40:32 +0300227 __func__, rep, ib_wc_status_msg(wc->status));
Chuck Leverb0e178a2015-10-24 17:26:54 -0400228 rep->rr_len = RPCRDMA_BAD_LEN;
Chuck Lever85024272015-01-21 11:02:04 -0500229 goto out_schedule;
Chuck Leverfc664482014-05-28 10:33:25 -0400230}
231
Chuck Lever4220a072015-10-24 17:26:45 -0400232/* The wc array is on stack: automatic memory is always CPU-local.
233 *
234 * struct ib_wc is 64 bytes, making the poll array potentially
235 * large. But this is at the bottom of the call chain. Further
236 * substantial work is done in another thread.
237 */
238static void
239rpcrdma_recvcq_poll(struct ib_cq *cq)
Chuck Leverfc664482014-05-28 10:33:25 -0400240{
Chuck Lever4220a072015-10-24 17:26:45 -0400241 struct ib_wc *pos, wcs[4];
242 LIST_HEAD(sched_list);
243 int count, rc;
Chuck Leverfc664482014-05-28 10:33:25 -0400244
Chuck Lever1c00dd02014-05-28 10:33:42 -0400245 do {
Chuck Lever4220a072015-10-24 17:26:45 -0400246 pos = wcs;
Chuck Lever1c00dd02014-05-28 10:33:42 -0400247
Chuck Lever4220a072015-10-24 17:26:45 -0400248 rc = ib_poll_cq(cq, ARRAY_SIZE(wcs), pos);
249 if (rc < 0)
250 break;
Chuck Lever1c00dd02014-05-28 10:33:42 -0400251
252 count = rc;
253 while (count-- > 0)
Chuck Lever4220a072015-10-24 17:26:45 -0400254 rpcrdma_recvcq_process_wc(pos++, &sched_list);
255 } while (rc == ARRAY_SIZE(wcs));
Chuck Leverbb961932014-07-29 17:25:46 -0400256
Chuck Leverf1a03b72014-11-08 20:14:37 -0500257 rpcrdma_schedule_tasklet(&sched_list);
Chuck Leverfc664482014-05-28 10:33:25 -0400258}
259
Chuck Lever7b3d7702015-10-24 17:26:37 -0400260/* Handle provider receive completion upcalls.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400261 */
262static void
Chuck Leverfc664482014-05-28 10:33:25 -0400263rpcrdma_recvcq_upcall(struct ib_cq *cq, void *cq_context)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400264{
Chuck Lever7b3d7702015-10-24 17:26:37 -0400265 do {
Chuck Lever4220a072015-10-24 17:26:45 -0400266 rpcrdma_recvcq_poll(cq);
Chuck Lever7b3d7702015-10-24 17:26:37 -0400267 } while (ib_req_notify_cq(cq, IB_CQ_NEXT_COMP |
268 IB_CQ_REPORT_MISSED_EVENTS) > 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400269}
270
Chuck Levera7bc2112014-07-29 17:23:52 -0400271static void
272rpcrdma_flush_cqs(struct rpcrdma_ep *ep)
273{
Chuck Lever5c166bef2014-11-08 20:14:45 -0500274 struct ib_wc wc;
275 LIST_HEAD(sched_list);
276
277 while (ib_poll_cq(ep->rep_attr.recv_cq, 1, &wc) > 0)
278 rpcrdma_recvcq_process_wc(&wc, &sched_list);
279 if (!list_empty(&sched_list))
280 rpcrdma_schedule_tasklet(&sched_list);
281 while (ib_poll_cq(ep->rep_attr.send_cq, 1, &wc) > 0)
282 rpcrdma_sendcq_process_wc(&wc);
Chuck Levera7bc2112014-07-29 17:23:52 -0400283}
284
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400285static int
286rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
287{
288 struct rpcrdma_xprt *xprt = id->context;
289 struct rpcrdma_ia *ia = &xprt->rx_ia;
290 struct rpcrdma_ep *ep = &xprt->rx_ep;
Jeff Laytonf895b252014-11-17 16:58:04 -0500291#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400292 struct sockaddr *sap = (struct sockaddr *)&ep->rep_remote_addr;
Ingo Molnarff0db042008-11-25 16:58:42 -0800293#endif
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500294 struct ib_qp_attr *attr = &ia->ri_qp_attr;
295 struct ib_qp_init_attr *iattr = &ia->ri_qp_init_attr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400296 int connstate = 0;
297
298 switch (event->event) {
299 case RDMA_CM_EVENT_ADDR_RESOLVED:
300 case RDMA_CM_EVENT_ROUTE_RESOLVED:
Tom Talpey5675add2008-10-09 15:01:41 -0400301 ia->ri_async_rc = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400302 complete(&ia->ri_done);
303 break;
304 case RDMA_CM_EVENT_ADDR_ERROR:
305 ia->ri_async_rc = -EHOSTUNREACH;
306 dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
307 __func__, ep);
308 complete(&ia->ri_done);
309 break;
310 case RDMA_CM_EVENT_ROUTE_ERROR:
311 ia->ri_async_rc = -ENETUNREACH;
312 dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
313 __func__, ep);
314 complete(&ia->ri_done);
315 break;
316 case RDMA_CM_EVENT_ESTABLISHED:
317 connstate = 1;
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500318 ib_query_qp(ia->ri_id->qp, attr,
319 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_MAX_DEST_RD_ATOMIC,
320 iattr);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400321 dprintk("RPC: %s: %d responder resources"
322 " (%d initiator)\n",
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500323 __func__, attr->max_dest_rd_atomic,
324 attr->max_rd_atomic);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400325 goto connected;
326 case RDMA_CM_EVENT_CONNECT_ERROR:
327 connstate = -ENOTCONN;
328 goto connected;
329 case RDMA_CM_EVENT_UNREACHABLE:
330 connstate = -ENETDOWN;
331 goto connected;
332 case RDMA_CM_EVENT_REJECTED:
333 connstate = -ECONNREFUSED;
334 goto connected;
335 case RDMA_CM_EVENT_DISCONNECTED:
336 connstate = -ECONNABORTED;
337 goto connected;
338 case RDMA_CM_EVENT_DEVICE_REMOVAL:
339 connstate = -ENODEV;
340connected:
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400341 dprintk("RPC: %s: %sconnected\n",
342 __func__, connstate > 0 ? "" : "dis");
343 ep->rep_connected = connstate;
Chuck Leverafadc462015-01-21 11:03:11 -0500344 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400345 wake_up_all(&ep->rep_connect_wait);
Chuck Lever8079fb72014-07-29 17:26:12 -0400346 /*FALLTHROUGH*/
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400347 default:
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400348 dprintk("RPC: %s: %pIS:%u (ep 0x%p): %s\n",
349 __func__, sap, rpc_get_port(sap), ep,
Sagi Grimberg76357c72015-05-18 13:40:32 +0300350 rdma_event_msg(event->event));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400351 break;
352 }
353
Jeff Laytonf895b252014-11-17 16:58:04 -0500354#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400355 if (connstate == 1) {
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500356 int ird = attr->max_dest_rd_atomic;
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400357 int tird = ep->rep_remote_cma.responder_resources;
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400358
Chuck Levera0ce85f2015-03-30 14:34:21 -0400359 pr_info("rpcrdma: connection to %pIS:%u on %s, memreg '%s', %d credits, %d responders%s\n",
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400360 sap, rpc_get_port(sap),
Chuck Lever89e0d1122015-05-26 11:51:56 -0400361 ia->ri_device->name,
Chuck Levera0ce85f2015-03-30 14:34:21 -0400362 ia->ri_ops->ro_displayname,
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400363 xprt->rx_buf.rb_max_requests,
364 ird, ird < 4 && ird < tird / 2 ? " (low!)" : "");
365 } else if (connstate < 0) {
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400366 pr_info("rpcrdma: connection to %pIS:%u closed (%d)\n",
367 sap, rpc_get_port(sap), connstate);
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400368 }
369#endif
370
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400371 return 0;
372}
373
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400374static void rpcrdma_destroy_id(struct rdma_cm_id *id)
375{
376 if (id) {
377 module_put(id->device->owner);
378 rdma_destroy_id(id);
379 }
380}
381
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400382static struct rdma_cm_id *
383rpcrdma_create_id(struct rpcrdma_xprt *xprt,
384 struct rpcrdma_ia *ia, struct sockaddr *addr)
385{
386 struct rdma_cm_id *id;
387 int rc;
388
Tom Talpey1a954052008-10-09 15:01:31 -0400389 init_completion(&ia->ri_done);
390
Sean Heftyb26f9b92010-04-01 17:08:41 +0000391 id = rdma_create_id(rpcrdma_conn_upcall, xprt, RDMA_PS_TCP, IB_QPT_RC);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400392 if (IS_ERR(id)) {
393 rc = PTR_ERR(id);
394 dprintk("RPC: %s: rdma_create_id() failed %i\n",
395 __func__, rc);
396 return id;
397 }
398
Tom Talpey5675add2008-10-09 15:01:41 -0400399 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400400 rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
401 if (rc) {
402 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
403 __func__, rc);
404 goto out;
405 }
Tom Talpey5675add2008-10-09 15:01:41 -0400406 wait_for_completion_interruptible_timeout(&ia->ri_done,
407 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400408
409 /* FIXME:
410 * Until xprtrdma supports DEVICE_REMOVAL, the provider must
411 * be pinned while there are active NFS/RDMA mounts to prevent
412 * hangs and crashes at umount time.
413 */
414 if (!ia->ri_async_rc && !try_module_get(id->device->owner)) {
415 dprintk("RPC: %s: Failed to get device module\n",
416 __func__);
417 ia->ri_async_rc = -ENODEV;
418 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400419 rc = ia->ri_async_rc;
420 if (rc)
421 goto out;
422
Tom Talpey5675add2008-10-09 15:01:41 -0400423 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400424 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
425 if (rc) {
426 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
427 __func__, rc);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400428 goto put;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400429 }
Tom Talpey5675add2008-10-09 15:01:41 -0400430 wait_for_completion_interruptible_timeout(&ia->ri_done,
431 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400432 rc = ia->ri_async_rc;
433 if (rc)
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400434 goto put;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400435
436 return id;
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400437put:
438 module_put(id->device->owner);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400439out:
440 rdma_destroy_id(id);
441 return ERR_PTR(rc);
442}
443
444/*
445 * Drain any cq, prior to teardown.
446 */
447static void
448rpcrdma_clean_cq(struct ib_cq *cq)
449{
450 struct ib_wc wc;
451 int count = 0;
452
453 while (1 == ib_poll_cq(cq, 1, &wc))
454 ++count;
455
456 if (count)
457 dprintk("RPC: %s: flushed %d events (last 0x%x)\n",
458 __func__, count, wc.opcode);
459}
460
461/*
462 * Exported functions.
463 */
464
465/*
466 * Open and initialize an Interface Adapter.
467 * o initializes fields of struct rpcrdma_ia, including
468 * interface and provider attributes and protection zone.
469 */
470int
471rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr, int memreg)
472{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400473 struct rpcrdma_ia *ia = &xprt->rx_ia;
Chuck Lever7bc79722015-01-21 11:03:27 -0500474 struct ib_device_attr *devattr = &ia->ri_devattr;
Chuck Leverd1ed8572015-08-03 13:03:30 -0400475 int rc;
476
477 ia->ri_dma_mr = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400478
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400479 ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
480 if (IS_ERR(ia->ri_id)) {
481 rc = PTR_ERR(ia->ri_id);
482 goto out1;
483 }
Chuck Lever89e0d1122015-05-26 11:51:56 -0400484 ia->ri_device = ia->ri_id->device;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400485
Chuck Lever89e0d1122015-05-26 11:51:56 -0400486 ia->ri_pd = ib_alloc_pd(ia->ri_device);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400487 if (IS_ERR(ia->ri_pd)) {
488 rc = PTR_ERR(ia->ri_pd);
489 dprintk("RPC: %s: ib_alloc_pd() failed %i\n",
490 __func__, rc);
491 goto out2;
492 }
493
Chuck Lever89e0d1122015-05-26 11:51:56 -0400494 rc = ib_query_device(ia->ri_device, devattr);
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400495 if (rc) {
496 dprintk("RPC: %s: ib_query_device failed %d\n",
497 __func__, rc);
Chuck Lever5ae711a2015-01-21 11:03:19 -0500498 goto out3;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400499 }
500
Chuck Leverf10eafd2014-05-28 10:32:51 -0400501 if (memreg == RPCRDMA_FRMR) {
Sagi Grimbergf022fa82015-10-06 19:52:37 +0300502 if (!(devattr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) ||
503 (devattr->max_fast_reg_page_list_len == 0)) {
Tom Talpey3197d3092008-10-09 15:00:20 -0400504 dprintk("RPC: %s: FRMR registration "
Chuck Leverf10eafd2014-05-28 10:32:51 -0400505 "not supported by HCA\n", __func__);
506 memreg = RPCRDMA_MTHCAFMR;
Tom Talpey3197d3092008-10-09 15:00:20 -0400507 }
Chuck Leverf10eafd2014-05-28 10:32:51 -0400508 }
509 if (memreg == RPCRDMA_MTHCAFMR) {
Chuck Lever89e0d1122015-05-26 11:51:56 -0400510 if (!ia->ri_device->alloc_fmr) {
Chuck Leverf10eafd2014-05-28 10:32:51 -0400511 dprintk("RPC: %s: MTHCAFMR registration "
512 "not supported by HCA\n", __func__);
Sagi Grimbergf022fa82015-10-06 19:52:37 +0300513 rc = -EINVAL;
Chuck Leverd2310932015-08-03 13:03:09 -0400514 goto out3;
Chuck Leverf10eafd2014-05-28 10:32:51 -0400515 }
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400516 }
517
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400518 switch (memreg) {
Tom Talpey3197d3092008-10-09 15:00:20 -0400519 case RPCRDMA_FRMR:
Chuck Levera0ce85f2015-03-30 14:34:21 -0400520 ia->ri_ops = &rpcrdma_frwr_memreg_ops;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400521 break;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400522 case RPCRDMA_ALLPHYSICAL:
Chuck Levera0ce85f2015-03-30 14:34:21 -0400523 ia->ri_ops = &rpcrdma_physical_memreg_ops;
Chuck Leverd1ed8572015-08-03 13:03:30 -0400524 break;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400525 case RPCRDMA_MTHCAFMR:
Chuck Levera0ce85f2015-03-30 14:34:21 -0400526 ia->ri_ops = &rpcrdma_fmr_memreg_ops;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400527 break;
528 default:
Chuck Levercdd9ade2014-05-28 10:33:00 -0400529 printk(KERN_ERR "RPC: Unsupported memory "
530 "registration mode: %d\n", memreg);
531 rc = -ENOMEM;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500532 goto out3;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400533 }
Chuck Levera0ce85f2015-03-30 14:34:21 -0400534 dprintk("RPC: %s: memory registration strategy is '%s'\n",
535 __func__, ia->ri_ops->ro_displayname);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400536
Chuck Lever73806c82014-07-29 17:23:25 -0400537 rwlock_init(&ia->ri_qplock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400538 return 0;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500539
540out3:
541 ib_dealloc_pd(ia->ri_pd);
542 ia->ri_pd = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400543out2:
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400544 rpcrdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400545 ia->ri_id = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400546out1:
547 return rc;
548}
549
550/*
551 * Clean up/close an IA.
552 * o if event handles and PD have been initialized, free them.
553 * o close the IA
554 */
555void
556rpcrdma_ia_close(struct rpcrdma_ia *ia)
557{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400558 dprintk("RPC: %s: entering\n", __func__);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400559 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
560 if (ia->ri_id->qp)
561 rdma_destroy_qp(ia->ri_id);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400562 rpcrdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400563 ia->ri_id = NULL;
564 }
Chuck Lever6d446982015-05-26 11:51:27 -0400565
566 /* If the pd is still busy, xprtrdma missed freeing a resource */
567 if (ia->ri_pd && !IS_ERR(ia->ri_pd))
Jason Gunthorpe7dd78642015-08-05 14:34:31 -0600568 ib_dealloc_pd(ia->ri_pd);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400569}
570
571/*
572 * Create unconnected endpoint.
573 */
574int
575rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
576 struct rpcrdma_create_data_internal *cdata)
577{
Chuck Lever7bc79722015-01-21 11:03:27 -0500578 struct ib_device_attr *devattr = &ia->ri_devattr;
Chuck Leverfc664482014-05-28 10:33:25 -0400579 struct ib_cq *sendcq, *recvcq;
Matan Barak8e372102015-06-11 16:35:21 +0300580 struct ib_cq_init_attr cq_attr = {};
Chuck Lever5d40a8a2007-10-26 13:30:54 -0400581 int rc, err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400582
Chuck Leverb3221d62015-08-03 13:03:39 -0400583 if (devattr->max_sge < RPCRDMA_MAX_IOVS) {
584 dprintk("RPC: %s: insufficient sge's available\n",
585 __func__);
586 return -ENOMEM;
587 }
588
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400589 /* check provider's send/recv wr limits */
Chuck Lever7bc79722015-01-21 11:03:27 -0500590 if (cdata->max_requests > devattr->max_qp_wr)
591 cdata->max_requests = devattr->max_qp_wr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400592
593 ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
594 ep->rep_attr.qp_context = ep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400595 ep->rep_attr.srq = NULL;
596 ep->rep_attr.cap.max_send_wr = cdata->max_requests;
Chuck Lever3968cb52015-03-30 14:35:26 -0400597 rc = ia->ri_ops->ro_open(ia, ep, cdata);
598 if (rc)
599 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400600 ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
Chuck Leverb3221d62015-08-03 13:03:39 -0400601 ep->rep_attr.cap.max_send_sge = RPCRDMA_MAX_IOVS;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400602 ep->rep_attr.cap.max_recv_sge = 1;
603 ep->rep_attr.cap.max_inline_data = 0;
604 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
605 ep->rep_attr.qp_type = IB_QPT_RC;
606 ep->rep_attr.port_num = ~0;
607
608 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
609 "iovs: send %d recv %d\n",
610 __func__,
611 ep->rep_attr.cap.max_send_wr,
612 ep->rep_attr.cap.max_recv_wr,
613 ep->rep_attr.cap.max_send_sge,
614 ep->rep_attr.cap.max_recv_sge);
615
616 /* set trigger for requesting send completion */
Chuck Leverfc664482014-05-28 10:33:25 -0400617 ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
Chuck Levere7104a22014-11-08 20:14:20 -0500618 if (ep->rep_cqinit > RPCRDMA_MAX_UNSIGNALED_SENDS)
619 ep->rep_cqinit = RPCRDMA_MAX_UNSIGNALED_SENDS;
620 else if (ep->rep_cqinit <= 2)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400621 ep->rep_cqinit = 0;
622 INIT_CQCOUNT(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400623 init_waitqueue_head(&ep->rep_connect_wait);
Chuck Lever254f91e2014-05-28 10:32:17 -0400624 INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400625
Matan Barak8e372102015-06-11 16:35:21 +0300626 cq_attr.cqe = ep->rep_attr.cap.max_send_wr + 1;
Chuck Lever89e0d1122015-05-26 11:51:56 -0400627 sendcq = ib_create_cq(ia->ri_device, rpcrdma_sendcq_upcall,
Chuck Lever4220a072015-10-24 17:26:45 -0400628 rpcrdma_cq_async_error_upcall, NULL, &cq_attr);
Chuck Leverfc664482014-05-28 10:33:25 -0400629 if (IS_ERR(sendcq)) {
630 rc = PTR_ERR(sendcq);
631 dprintk("RPC: %s: failed to create send CQ: %i\n",
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400632 __func__, rc);
633 goto out1;
634 }
635
Chuck Leverfc664482014-05-28 10:33:25 -0400636 rc = ib_req_notify_cq(sendcq, IB_CQ_NEXT_COMP);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400637 if (rc) {
638 dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
639 __func__, rc);
640 goto out2;
641 }
642
Matan Barak8e372102015-06-11 16:35:21 +0300643 cq_attr.cqe = ep->rep_attr.cap.max_recv_wr + 1;
Chuck Lever89e0d1122015-05-26 11:51:56 -0400644 recvcq = ib_create_cq(ia->ri_device, rpcrdma_recvcq_upcall,
Chuck Lever4220a072015-10-24 17:26:45 -0400645 rpcrdma_cq_async_error_upcall, NULL, &cq_attr);
Chuck Leverfc664482014-05-28 10:33:25 -0400646 if (IS_ERR(recvcq)) {
647 rc = PTR_ERR(recvcq);
648 dprintk("RPC: %s: failed to create recv CQ: %i\n",
649 __func__, rc);
650 goto out2;
651 }
652
653 rc = ib_req_notify_cq(recvcq, IB_CQ_NEXT_COMP);
654 if (rc) {
655 dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
656 __func__, rc);
657 ib_destroy_cq(recvcq);
658 goto out2;
659 }
660
661 ep->rep_attr.send_cq = sendcq;
662 ep->rep_attr.recv_cq = recvcq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400663
664 /* Initialize cma parameters */
665
666 /* RPC/RDMA does not use private data */
667 ep->rep_remote_cma.private_data = NULL;
668 ep->rep_remote_cma.private_data_len = 0;
669
670 /* Client offers RDMA Read but does not initiate */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400671 ep->rep_remote_cma.initiator_depth = 0;
Chuck Lever7bc79722015-01-21 11:03:27 -0500672 if (devattr->max_qp_rd_atom > 32) /* arbitrary but <= 255 */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400673 ep->rep_remote_cma.responder_resources = 32;
674 else
Chuck Lever7bc79722015-01-21 11:03:27 -0500675 ep->rep_remote_cma.responder_resources =
676 devattr->max_qp_rd_atom;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400677
678 ep->rep_remote_cma.retry_count = 7;
679 ep->rep_remote_cma.flow_control = 0;
680 ep->rep_remote_cma.rnr_retry_count = 0;
681
682 return 0;
683
684out2:
Chuck Leverfc664482014-05-28 10:33:25 -0400685 err = ib_destroy_cq(sendcq);
Chuck Lever5d40a8a2007-10-26 13:30:54 -0400686 if (err)
687 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
688 __func__, err);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400689out1:
Chuck Leverd1ed8572015-08-03 13:03:30 -0400690 if (ia->ri_dma_mr)
691 ib_dereg_mr(ia->ri_dma_mr);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400692 return rc;
693}
694
695/*
696 * rpcrdma_ep_destroy
697 *
698 * Disconnect and destroy endpoint. After this, the only
699 * valid operations on the ep are to free it (if dynamically
700 * allocated) or re-create it.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400701 */
Chuck Lever7f1d5412014-05-28 10:33:16 -0400702void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400703rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
704{
705 int rc;
706
707 dprintk("RPC: %s: entering, connected is %d\n",
708 __func__, ep->rep_connected);
709
Chuck Lever254f91e2014-05-28 10:32:17 -0400710 cancel_delayed_work_sync(&ep->rep_connect_worker);
711
Steve Wise72c02172015-09-21 12:24:23 -0500712 if (ia->ri_id->qp)
Chuck Lever282191c2014-07-29 17:25:55 -0400713 rpcrdma_ep_disconnect(ep, ia);
Steve Wise72c02172015-09-21 12:24:23 -0500714
715 rpcrdma_clean_cq(ep->rep_attr.recv_cq);
716 rpcrdma_clean_cq(ep->rep_attr.send_cq);
717
718 if (ia->ri_id->qp) {
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400719 rdma_destroy_qp(ia->ri_id);
720 ia->ri_id->qp = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400721 }
722
Chuck Leverfc664482014-05-28 10:33:25 -0400723 rc = ib_destroy_cq(ep->rep_attr.recv_cq);
724 if (rc)
725 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
726 __func__, rc);
727
Chuck Leverfc664482014-05-28 10:33:25 -0400728 rc = ib_destroy_cq(ep->rep_attr.send_cq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400729 if (rc)
730 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
731 __func__, rc);
Chuck Leverd1ed8572015-08-03 13:03:30 -0400732
733 if (ia->ri_dma_mr) {
734 rc = ib_dereg_mr(ia->ri_dma_mr);
735 dprintk("RPC: %s: ib_dereg_mr returned %i\n",
736 __func__, rc);
737 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400738}
739
740/*
741 * Connect unconnected endpoint.
742 */
743int
744rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
745{
Chuck Lever73806c82014-07-29 17:23:25 -0400746 struct rdma_cm_id *id, *old;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400747 int rc = 0;
748 int retry_count = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400749
Tom Talpeyc0555512008-10-10 11:32:45 -0400750 if (ep->rep_connected != 0) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400751 struct rpcrdma_xprt *xprt;
752retry:
Chuck Leverec62f402014-05-28 10:34:07 -0400753 dprintk("RPC: %s: reconnecting...\n", __func__);
Chuck Lever282191c2014-07-29 17:25:55 -0400754
755 rpcrdma_ep_disconnect(ep, ia);
Chuck Levera7bc2112014-07-29 17:23:52 -0400756 rpcrdma_flush_cqs(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400757
758 xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
759 id = rpcrdma_create_id(xprt, ia,
760 (struct sockaddr *)&xprt->rx_data.addr);
761 if (IS_ERR(id)) {
Chuck Leverec62f402014-05-28 10:34:07 -0400762 rc = -EHOSTUNREACH;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400763 goto out;
764 }
765 /* TEMP TEMP TEMP - fail if new device:
766 * Deregister/remarshal *all* requests!
767 * Close and recreate adapter, pd, etc!
768 * Re-determine all attributes still sane!
769 * More stuff I haven't thought of!
770 * Rrrgh!
771 */
Chuck Lever89e0d1122015-05-26 11:51:56 -0400772 if (ia->ri_device != id->device) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400773 printk("RPC: %s: can't reconnect on "
774 "different device!\n", __func__);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400775 rpcrdma_destroy_id(id);
Chuck Leverec62f402014-05-28 10:34:07 -0400776 rc = -ENETUNREACH;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400777 goto out;
778 }
779 /* END TEMP */
Chuck Leverec62f402014-05-28 10:34:07 -0400780 rc = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
781 if (rc) {
782 dprintk("RPC: %s: rdma_create_qp failed %i\n",
783 __func__, rc);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400784 rpcrdma_destroy_id(id);
Chuck Leverec62f402014-05-28 10:34:07 -0400785 rc = -ENETUNREACH;
786 goto out;
787 }
Chuck Lever73806c82014-07-29 17:23:25 -0400788
789 write_lock(&ia->ri_qplock);
790 old = ia->ri_id;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400791 ia->ri_id = id;
Chuck Lever73806c82014-07-29 17:23:25 -0400792 write_unlock(&ia->ri_qplock);
793
794 rdma_destroy_qp(old);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400795 rpcrdma_destroy_id(old);
Chuck Leverec62f402014-05-28 10:34:07 -0400796 } else {
797 dprintk("RPC: %s: connecting...\n", __func__);
798 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
799 if (rc) {
800 dprintk("RPC: %s: rdma_create_qp failed %i\n",
801 __func__, rc);
802 /* do not update ep->rep_connected */
803 return -ENETUNREACH;
804 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400805 }
806
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400807 ep->rep_connected = 0;
808
809 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
810 if (rc) {
811 dprintk("RPC: %s: rdma_connect() failed with %i\n",
812 __func__, rc);
813 goto out;
814 }
815
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400816 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
817
818 /*
819 * Check state. A non-peer reject indicates no listener
820 * (ECONNREFUSED), which may be a transient state. All
821 * others indicate a transport condition which has already
822 * undergone a best-effort.
823 */
Joe Perchesf64f9e72009-11-29 16:55:45 -0800824 if (ep->rep_connected == -ECONNREFUSED &&
825 ++retry_count <= RDMA_CONNECT_RETRY_MAX) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400826 dprintk("RPC: %s: non-peer_reject, retry\n", __func__);
827 goto retry;
828 }
829 if (ep->rep_connected <= 0) {
830 /* Sometimes, the only way to reliably connect to remote
831 * CMs is to use same nonzero values for ORD and IRD. */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400832 if (retry_count++ <= RDMA_CONNECT_RETRY_MAX + 1 &&
833 (ep->rep_remote_cma.responder_resources == 0 ||
834 ep->rep_remote_cma.initiator_depth !=
835 ep->rep_remote_cma.responder_resources)) {
836 if (ep->rep_remote_cma.responder_resources == 0)
837 ep->rep_remote_cma.responder_resources = 1;
838 ep->rep_remote_cma.initiator_depth =
839 ep->rep_remote_cma.responder_resources;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400840 goto retry;
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400841 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400842 rc = ep->rep_connected;
843 } else {
844 dprintk("RPC: %s: connected\n", __func__);
845 }
846
847out:
848 if (rc)
849 ep->rep_connected = rc;
850 return rc;
851}
852
853/*
854 * rpcrdma_ep_disconnect
855 *
856 * This is separate from destroy to facilitate the ability
857 * to reconnect without recreating the endpoint.
858 *
859 * This call is not reentrant, and must not be made in parallel
860 * on the same endpoint.
861 */
Chuck Lever282191c2014-07-29 17:25:55 -0400862void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400863rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
864{
865 int rc;
866
Chuck Levera7bc2112014-07-29 17:23:52 -0400867 rpcrdma_flush_cqs(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400868 rc = rdma_disconnect(ia->ri_id);
869 if (!rc) {
870 /* returns without wait if not connected */
871 wait_event_interruptible(ep->rep_connect_wait,
872 ep->rep_connected != 1);
873 dprintk("RPC: %s: after wait, %sconnected\n", __func__,
874 (ep->rep_connected == 1) ? "still " : "dis");
875 } else {
876 dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
877 ep->rep_connected = rc;
878 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400879}
880
Chuck Lever13924022015-01-21 11:03:52 -0500881static struct rpcrdma_req *
882rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
883{
Chuck Lever13924022015-01-21 11:03:52 -0500884 struct rpcrdma_req *req;
Chuck Lever13924022015-01-21 11:03:52 -0500885
Chuck Lever85275c82015-01-21 11:04:16 -0500886 req = kzalloc(sizeof(*req), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500887 if (req == NULL)
Chuck Lever85275c82015-01-21 11:04:16 -0500888 return ERR_PTR(-ENOMEM);
Chuck Lever13924022015-01-21 11:03:52 -0500889
Chuck Lever13924022015-01-21 11:03:52 -0500890 req->rl_buffer = &r_xprt->rx_buf;
891 return req;
Chuck Lever13924022015-01-21 11:03:52 -0500892}
893
894static struct rpcrdma_rep *
895rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt)
896{
897 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Lever13924022015-01-21 11:03:52 -0500898 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
899 struct rpcrdma_rep *rep;
900 int rc;
901
902 rc = -ENOMEM;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500903 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500904 if (rep == NULL)
905 goto out;
Chuck Lever13924022015-01-21 11:03:52 -0500906
Chuck Lever6b1184c2015-01-21 11:04:25 -0500907 rep->rr_rdmabuf = rpcrdma_alloc_regbuf(ia, cdata->inline_rsize,
908 GFP_KERNEL);
909 if (IS_ERR(rep->rr_rdmabuf)) {
910 rc = PTR_ERR(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -0500911 goto out_free;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500912 }
Chuck Lever13924022015-01-21 11:03:52 -0500913
Chuck Lever89e0d1122015-05-26 11:51:56 -0400914 rep->rr_device = ia->ri_device;
Chuck Leverfed171b2015-05-26 11:51:37 -0400915 rep->rr_rxprt = r_xprt;
Chuck Lever13924022015-01-21 11:03:52 -0500916 return rep;
917
918out_free:
919 kfree(rep);
920out:
921 return ERR_PTR(rc);
922}
923
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400924int
Chuck Leverac920d02015-01-21 11:03:44 -0500925rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400926{
Chuck Leverac920d02015-01-21 11:03:44 -0500927 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
928 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400929 int i, rc;
930
Chuck Lever1e465fd2015-10-24 17:27:02 -0400931 buf->rb_max_requests = r_xprt->rx_data.max_requests;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400932 spin_lock_init(&buf->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400933
Chuck Lever91e70e72015-03-30 14:34:58 -0400934 rc = ia->ri_ops->ro_init(r_xprt);
935 if (rc)
936 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400937
Chuck Lever1e465fd2015-10-24 17:27:02 -0400938 INIT_LIST_HEAD(&buf->rb_send_bufs);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400939 for (i = 0; i < buf->rb_max_requests; i++) {
940 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400941
Chuck Lever13924022015-01-21 11:03:52 -0500942 req = rpcrdma_create_req(r_xprt);
943 if (IS_ERR(req)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400944 dprintk("RPC: %s: request buffer %d alloc"
945 " failed\n", __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -0500946 rc = PTR_ERR(req);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400947 goto out;
948 }
Chuck Lever1e465fd2015-10-24 17:27:02 -0400949 list_add(&req->rl_free, &buf->rb_send_bufs);
950 }
951
952 INIT_LIST_HEAD(&buf->rb_recv_bufs);
953 for (i = 0; i < buf->rb_max_requests + 2; i++) {
954 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400955
Chuck Lever13924022015-01-21 11:03:52 -0500956 rep = rpcrdma_create_rep(r_xprt);
957 if (IS_ERR(rep)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400958 dprintk("RPC: %s: reply buffer %d alloc failed\n",
959 __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -0500960 rc = PTR_ERR(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400961 goto out;
962 }
Chuck Lever1e465fd2015-10-24 17:27:02 -0400963 list_add(&rep->rr_list, &buf->rb_recv_bufs);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400964 }
Chuck Lever13924022015-01-21 11:03:52 -0500965
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400966 return 0;
967out:
968 rpcrdma_buffer_destroy(buf);
969 return rc;
970}
971
Chuck Lever1e465fd2015-10-24 17:27:02 -0400972static struct rpcrdma_req *
973rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf)
974{
975 struct rpcrdma_req *req;
976
977 req = list_first_entry(&buf->rb_send_bufs,
978 struct rpcrdma_req, rl_free);
979 list_del(&req->rl_free);
980 return req;
981}
982
983static struct rpcrdma_rep *
984rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf)
985{
986 struct rpcrdma_rep *rep;
987
988 rep = list_first_entry(&buf->rb_recv_bufs,
989 struct rpcrdma_rep, rr_list);
990 list_del(&rep->rr_list);
991 return rep;
992}
993
Chuck Lever2e845222014-07-29 17:25:38 -0400994static void
Chuck Lever13924022015-01-21 11:03:52 -0500995rpcrdma_destroy_rep(struct rpcrdma_ia *ia, struct rpcrdma_rep *rep)
996{
997 if (!rep)
998 return;
999
Chuck Lever6b1184c2015-01-21 11:04:25 -05001000 rpcrdma_free_regbuf(ia, rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001001 kfree(rep);
1002}
1003
1004static void
1005rpcrdma_destroy_req(struct rpcrdma_ia *ia, struct rpcrdma_req *req)
1006{
1007 if (!req)
1008 return;
1009
Chuck Lever0ca77dc2015-01-21 11:04:08 -05001010 rpcrdma_free_regbuf(ia, req->rl_sendbuf);
Chuck Lever85275c82015-01-21 11:04:16 -05001011 rpcrdma_free_regbuf(ia, req->rl_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001012 kfree(req);
1013}
1014
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001015void
1016rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1017{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001018 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
1019
Chuck Lever1e465fd2015-10-24 17:27:02 -04001020 while (!list_empty(&buf->rb_recv_bufs)) {
1021 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001022
Chuck Lever1e465fd2015-10-24 17:27:02 -04001023 rep = rpcrdma_buffer_get_rep_locked(buf);
1024 rpcrdma_destroy_rep(ia, rep);
1025 }
1026
1027 while (!list_empty(&buf->rb_send_bufs)) {
1028 struct rpcrdma_req *req;
1029
1030 req = rpcrdma_buffer_get_req_locked(buf);
1031 rpcrdma_destroy_req(ia, req);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001032 }
1033
Chuck Lever4561f342015-03-30 14:35:17 -04001034 ia->ri_ops->ro_destroy(buf);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001035}
1036
Chuck Lever346aa662015-05-26 11:52:06 -04001037struct rpcrdma_mw *
1038rpcrdma_get_mw(struct rpcrdma_xprt *r_xprt)
Chuck Leverc2922c02014-07-29 17:24:36 -04001039{
Chuck Lever346aa662015-05-26 11:52:06 -04001040 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1041 struct rpcrdma_mw *mw = NULL;
Chuck Lever346aa662015-05-26 11:52:06 -04001042
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001043 spin_lock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001044 if (!list_empty(&buf->rb_mws)) {
1045 mw = list_first_entry(&buf->rb_mws,
1046 struct rpcrdma_mw, mw_list);
1047 list_del_init(&mw->mw_list);
Chuck Leverc2922c02014-07-29 17:24:36 -04001048 }
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001049 spin_unlock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001050
1051 if (!mw)
1052 pr_err("RPC: %s: no MWs available\n", __func__);
1053 return mw;
Chuck Leverc2922c02014-07-29 17:24:36 -04001054}
1055
Chuck Lever346aa662015-05-26 11:52:06 -04001056void
1057rpcrdma_put_mw(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mw *mw)
Chuck Leverc2922c02014-07-29 17:24:36 -04001058{
Chuck Lever346aa662015-05-26 11:52:06 -04001059 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
Chuck Leverc2922c02014-07-29 17:24:36 -04001060
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001061 spin_lock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001062 list_add_tail(&mw->mw_list, &buf->rb_mws);
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001063 spin_unlock(&buf->rb_mwlock);
Chuck Leverc2922c02014-07-29 17:24:36 -04001064}
1065
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001066/*
1067 * Get a set of request/reply buffers.
1068 *
Chuck Lever1e465fd2015-10-24 17:27:02 -04001069 * Reply buffer (if available) is attached to send buffer upon return.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001070 */
1071struct rpcrdma_req *
1072rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1073{
1074 struct rpcrdma_req *req;
1075 unsigned long flags;
1076
1077 spin_lock_irqsave(&buffers->rb_lock, flags);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001078 if (list_empty(&buffers->rb_send_bufs))
1079 goto out_reqbuf;
1080 req = rpcrdma_buffer_get_req_locked(buffers);
1081 if (list_empty(&buffers->rb_recv_bufs))
1082 goto out_repbuf;
1083 req->rl_reply = rpcrdma_buffer_get_rep_locked(buffers);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001084 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1085 return req;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001086
1087out_reqbuf:
1088 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1089 pr_warn("RPC: %s: out of request buffers\n", __func__);
1090 return NULL;
1091out_repbuf:
1092 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1093 pr_warn("RPC: %s: out of reply buffers\n", __func__);
1094 req->rl_reply = NULL;
1095 return req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001096}
1097
1098/*
1099 * Put request/reply buffers back into pool.
1100 * Pre-decrement counter/array index.
1101 */
1102void
1103rpcrdma_buffer_put(struct rpcrdma_req *req)
1104{
1105 struct rpcrdma_buffer *buffers = req->rl_buffer;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001106 struct rpcrdma_rep *rep = req->rl_reply;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001107 unsigned long flags;
1108
Chuck Lever1e465fd2015-10-24 17:27:02 -04001109 req->rl_niovs = 0;
1110 req->rl_reply = NULL;
1111
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001112 spin_lock_irqsave(&buffers->rb_lock, flags);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001113 list_add_tail(&req->rl_free, &buffers->rb_send_bufs);
1114 if (rep)
1115 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001116 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1117}
1118
1119/*
1120 * Recover reply buffers from pool.
Chuck Lever1e465fd2015-10-24 17:27:02 -04001121 * This happens when recovering from disconnect.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001122 */
1123void
1124rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1125{
1126 struct rpcrdma_buffer *buffers = req->rl_buffer;
1127 unsigned long flags;
1128
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001129 spin_lock_irqsave(&buffers->rb_lock, flags);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001130 if (!list_empty(&buffers->rb_recv_bufs))
1131 req->rl_reply = rpcrdma_buffer_get_rep_locked(buffers);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001132 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1133}
1134
1135/*
1136 * Put reply buffers back into pool when not attached to
Chuck Leverb45ccfd2014-05-28 10:32:34 -04001137 * request. This happens in error conditions.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001138 */
1139void
1140rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1141{
Chuck Leverfed171b2015-05-26 11:51:37 -04001142 struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001143 unsigned long flags;
1144
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001145 spin_lock_irqsave(&buffers->rb_lock, flags);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001146 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001147 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1148}
1149
1150/*
1151 * Wrappers for internal-use kmalloc memory registration, used by buffer code.
1152 */
1153
Chuck Leverd6547882015-03-30 14:35:44 -04001154void
1155rpcrdma_mapping_error(struct rpcrdma_mr_seg *seg)
1156{
1157 dprintk("RPC: map_one: offset %p iova %llx len %zu\n",
1158 seg->mr_offset,
1159 (unsigned long long)seg->mr_dma, seg->mr_dmalen);
1160}
1161
Chuck Lever9128c3e2015-01-21 11:04:00 -05001162/**
1163 * rpcrdma_alloc_regbuf - kmalloc and register memory for SEND/RECV buffers
1164 * @ia: controlling rpcrdma_ia
1165 * @size: size of buffer to be allocated, in bytes
1166 * @flags: GFP flags
1167 *
1168 * Returns pointer to private header of an area of internally
1169 * registered memory, or an ERR_PTR. The registered buffer follows
1170 * the end of the private header.
1171 *
1172 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
1173 * receiving the payload of RDMA RECV operations. regbufs are not
1174 * used for RDMA READ/WRITE operations, thus are registered only for
1175 * LOCAL access.
1176 */
1177struct rpcrdma_regbuf *
1178rpcrdma_alloc_regbuf(struct rpcrdma_ia *ia, size_t size, gfp_t flags)
1179{
1180 struct rpcrdma_regbuf *rb;
Chuck Levere531dca2015-08-03 13:03:20 -04001181 struct ib_sge *iov;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001182
Chuck Lever9128c3e2015-01-21 11:04:00 -05001183 rb = kmalloc(sizeof(*rb) + size, flags);
1184 if (rb == NULL)
1185 goto out;
1186
Chuck Levere531dca2015-08-03 13:03:20 -04001187 iov = &rb->rg_iov;
1188 iov->addr = ib_dma_map_single(ia->ri_device,
1189 (void *)rb->rg_base, size,
1190 DMA_BIDIRECTIONAL);
1191 if (ib_dma_mapping_error(ia->ri_device, iov->addr))
Chuck Lever9128c3e2015-01-21 11:04:00 -05001192 goto out_free;
1193
Chuck Levere531dca2015-08-03 13:03:20 -04001194 iov->length = size;
Chuck Leverbb6c96d2015-09-24 10:34:21 +03001195 iov->lkey = ia->ri_pd->local_dma_lkey;
Chuck Levere531dca2015-08-03 13:03:20 -04001196 rb->rg_size = size;
1197 rb->rg_owner = NULL;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001198 return rb;
1199
1200out_free:
1201 kfree(rb);
1202out:
Chuck Levere531dca2015-08-03 13:03:20 -04001203 return ERR_PTR(-ENOMEM);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001204}
1205
1206/**
1207 * rpcrdma_free_regbuf - deregister and free registered buffer
1208 * @ia: controlling rpcrdma_ia
1209 * @rb: regbuf to be deregistered and freed
1210 */
1211void
1212rpcrdma_free_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1213{
Chuck Levere531dca2015-08-03 13:03:20 -04001214 struct ib_sge *iov;
1215
1216 if (!rb)
1217 return;
1218
1219 iov = &rb->rg_iov;
1220 ib_dma_unmap_single(ia->ri_device,
1221 iov->addr, iov->length, DMA_BIDIRECTIONAL);
1222 kfree(rb);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001223}
1224
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001225/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001226 * Prepost any receive buffer, then post send.
1227 *
1228 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1229 */
1230int
1231rpcrdma_ep_post(struct rpcrdma_ia *ia,
1232 struct rpcrdma_ep *ep,
1233 struct rpcrdma_req *req)
1234{
Chuck Leverb3221d62015-08-03 13:03:39 -04001235 struct ib_device *device = ia->ri_device;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001236 struct ib_send_wr send_wr, *send_wr_fail;
1237 struct rpcrdma_rep *rep = req->rl_reply;
Chuck Leverb3221d62015-08-03 13:03:39 -04001238 struct ib_sge *iov = req->rl_send_iov;
1239 int i, rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001240
1241 if (rep) {
1242 rc = rpcrdma_ep_post_recv(ia, ep, rep);
1243 if (rc)
1244 goto out;
1245 req->rl_reply = NULL;
1246 }
1247
1248 send_wr.next = NULL;
Chuck Levere46ac342015-03-30 14:35:35 -04001249 send_wr.wr_id = RPCRDMA_IGNORE_COMPLETION;
Chuck Leverb3221d62015-08-03 13:03:39 -04001250 send_wr.sg_list = iov;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001251 send_wr.num_sge = req->rl_niovs;
1252 send_wr.opcode = IB_WR_SEND;
Chuck Leverb3221d62015-08-03 13:03:39 -04001253
1254 for (i = 0; i < send_wr.num_sge; i++)
1255 ib_dma_sync_single_for_device(device, iov[i].addr,
1256 iov[i].length, DMA_TO_DEVICE);
1257 dprintk("RPC: %s: posting %d s/g entries\n",
1258 __func__, send_wr.num_sge);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001259
1260 if (DECR_CQCOUNT(ep) > 0)
1261 send_wr.send_flags = 0;
1262 else { /* Provider must take a send completion every now and then */
1263 INIT_CQCOUNT(ep);
1264 send_wr.send_flags = IB_SEND_SIGNALED;
1265 }
1266
1267 rc = ib_post_send(ia->ri_id->qp, &send_wr, &send_wr_fail);
1268 if (rc)
1269 dprintk("RPC: %s: ib_post_send returned %i\n", __func__,
1270 rc);
1271out:
1272 return rc;
1273}
1274
1275/*
1276 * (Re)post a receive buffer.
1277 */
1278int
1279rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
1280 struct rpcrdma_ep *ep,
1281 struct rpcrdma_rep *rep)
1282{
1283 struct ib_recv_wr recv_wr, *recv_wr_fail;
1284 int rc;
1285
1286 recv_wr.next = NULL;
1287 recv_wr.wr_id = (u64) (unsigned long) rep;
Chuck Lever6b1184c2015-01-21 11:04:25 -05001288 recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001289 recv_wr.num_sge = 1;
1290
Chuck Lever89e0d1122015-05-26 11:51:56 -04001291 ib_dma_sync_single_for_cpu(ia->ri_device,
Chuck Lever6b1184c2015-01-21 11:04:25 -05001292 rdmab_addr(rep->rr_rdmabuf),
1293 rdmab_length(rep->rr_rdmabuf),
1294 DMA_BIDIRECTIONAL);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001295
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001296 rc = ib_post_recv(ia->ri_id->qp, &recv_wr, &recv_wr_fail);
1297
1298 if (rc)
1299 dprintk("RPC: %s: ib_post_recv returned %i\n", __func__,
1300 rc);
1301 return rc;
1302}
Chuck Lever43e95982014-07-29 17:23:34 -04001303
Chuck Lever1c9351e2015-03-30 14:34:30 -04001304/* How many chunk list items fit within our inline buffers?
Chuck Lever43e95982014-07-29 17:23:34 -04001305 */
Chuck Lever1c9351e2015-03-30 14:34:30 -04001306unsigned int
1307rpcrdma_max_segments(struct rpcrdma_xprt *r_xprt)
Chuck Lever43e95982014-07-29 17:23:34 -04001308{
1309 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Lever1c9351e2015-03-30 14:34:30 -04001310 int bytes, segments;
Chuck Lever43e95982014-07-29 17:23:34 -04001311
Chuck Lever1c9351e2015-03-30 14:34:30 -04001312 bytes = min_t(unsigned int, cdata->inline_wsize, cdata->inline_rsize);
1313 bytes -= RPCRDMA_HDRLEN_MIN;
1314 if (bytes < sizeof(struct rpcrdma_segment) * 2) {
1315 pr_warn("RPC: %s: inline threshold too small\n",
1316 __func__);
1317 return 0;
Chuck Lever43e95982014-07-29 17:23:34 -04001318 }
Chuck Lever1c9351e2015-03-30 14:34:30 -04001319
1320 segments = 1 << (fls(bytes / sizeof(struct rpcrdma_segment)) - 1);
1321 dprintk("RPC: %s: max chunk list size = %d segments\n",
1322 __func__, segments);
1323 return segments;
Chuck Lever43e95982014-07-29 17:23:34 -04001324}