blob: 6661b1b95758cf4918008c0c04181279bfd46e06 [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 Lever85024272015-01-21 11:02:04 -0500228 rep->rr_len = ~0U;
229 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 Lever5c166be2014-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;
929 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400930 char *p;
Chuck Lever13924022015-01-21 11:03:52 -0500931 size_t len;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400932 int i, rc;
933
934 buf->rb_max_requests = cdata->max_requests;
935 spin_lock_init(&buf->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400936
937 /* Need to allocate:
938 * 1. arrays for send and recv pointers
939 * 2. arrays of struct rpcrdma_req to fill in pointers
940 * 3. array of struct rpcrdma_rep for replies
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400941 * Send/recv buffers in req/rep need to be registered
942 */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400943 len = buf->rb_max_requests *
944 (sizeof(struct rpcrdma_req *) + sizeof(struct rpcrdma_rep *));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400945
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400946 p = kzalloc(len, GFP_KERNEL);
947 if (p == NULL) {
948 dprintk("RPC: %s: req_t/rep_t/pad kzalloc(%zd) failed\n",
949 __func__, len);
950 rc = -ENOMEM;
951 goto out;
952 }
953 buf->rb_pool = p; /* for freeing it later */
954
955 buf->rb_send_bufs = (struct rpcrdma_req **) p;
956 p = (char *) &buf->rb_send_bufs[buf->rb_max_requests];
957 buf->rb_recv_bufs = (struct rpcrdma_rep **) p;
958 p = (char *) &buf->rb_recv_bufs[buf->rb_max_requests];
959
Chuck Lever91e70e72015-03-30 14:34:58 -0400960 rc = ia->ri_ops->ro_init(r_xprt);
961 if (rc)
962 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400963
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400964 for (i = 0; i < buf->rb_max_requests; i++) {
965 struct rpcrdma_req *req;
966 struct rpcrdma_rep *rep;
967
Chuck Lever13924022015-01-21 11:03:52 -0500968 req = rpcrdma_create_req(r_xprt);
969 if (IS_ERR(req)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400970 dprintk("RPC: %s: request buffer %d alloc"
971 " failed\n", __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -0500972 rc = PTR_ERR(req);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400973 goto out;
974 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400975 buf->rb_send_bufs[i] = req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400976
Chuck Lever13924022015-01-21 11:03:52 -0500977 rep = rpcrdma_create_rep(r_xprt);
978 if (IS_ERR(rep)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400979 dprintk("RPC: %s: reply buffer %d alloc failed\n",
980 __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -0500981 rc = PTR_ERR(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400982 goto out;
983 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400984 buf->rb_recv_bufs[i] = rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400985 }
Chuck Lever13924022015-01-21 11:03:52 -0500986
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400987 return 0;
988out:
989 rpcrdma_buffer_destroy(buf);
990 return rc;
991}
992
Chuck Lever2e845222014-07-29 17:25:38 -0400993static void
Chuck Lever13924022015-01-21 11:03:52 -0500994rpcrdma_destroy_rep(struct rpcrdma_ia *ia, struct rpcrdma_rep *rep)
995{
996 if (!rep)
997 return;
998
Chuck Lever6b1184c2015-01-21 11:04:25 -0500999 rpcrdma_free_regbuf(ia, rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001000 kfree(rep);
1001}
1002
1003static void
1004rpcrdma_destroy_req(struct rpcrdma_ia *ia, struct rpcrdma_req *req)
1005{
1006 if (!req)
1007 return;
1008
Chuck Lever0ca77dc2015-01-21 11:04:08 -05001009 rpcrdma_free_regbuf(ia, req->rl_sendbuf);
Chuck Lever85275c82015-01-21 11:04:16 -05001010 rpcrdma_free_regbuf(ia, req->rl_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001011 kfree(req);
1012}
1013
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001014void
1015rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1016{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001017 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
Chuck Lever2e845222014-07-29 17:25:38 -04001018 int i;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001019
1020 /* clean up in reverse order from create
1021 * 1. recv mr memory (mr free, then kfree)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001022 * 2. send mr memory (mr free, then kfree)
Chuck Lever2e845222014-07-29 17:25:38 -04001023 * 3. MWs
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001024 */
1025 dprintk("RPC: %s: entering\n", __func__);
1026
1027 for (i = 0; i < buf->rb_max_requests; i++) {
Chuck Lever13924022015-01-21 11:03:52 -05001028 if (buf->rb_recv_bufs)
1029 rpcrdma_destroy_rep(ia, buf->rb_recv_bufs[i]);
1030 if (buf->rb_send_bufs)
1031 rpcrdma_destroy_req(ia, buf->rb_send_bufs[i]);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001032 }
1033
Chuck Lever4561f342015-03-30 14:35:17 -04001034 ia->ri_ops->ro_destroy(buf);
Allen Andrews4034ba02014-05-28 10:32:09 -04001035
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001036 kfree(buf->rb_pool);
1037}
1038
Chuck Lever346aa662015-05-26 11:52:06 -04001039struct rpcrdma_mw *
1040rpcrdma_get_mw(struct rpcrdma_xprt *r_xprt)
Chuck Leverc2922c02014-07-29 17:24:36 -04001041{
Chuck Lever346aa662015-05-26 11:52:06 -04001042 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1043 struct rpcrdma_mw *mw = NULL;
Chuck Lever346aa662015-05-26 11:52:06 -04001044
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001045 spin_lock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001046 if (!list_empty(&buf->rb_mws)) {
1047 mw = list_first_entry(&buf->rb_mws,
1048 struct rpcrdma_mw, mw_list);
1049 list_del_init(&mw->mw_list);
Chuck Leverc2922c02014-07-29 17:24:36 -04001050 }
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001051 spin_unlock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001052
1053 if (!mw)
1054 pr_err("RPC: %s: no MWs available\n", __func__);
1055 return mw;
Chuck Leverc2922c02014-07-29 17:24:36 -04001056}
1057
Chuck Lever346aa662015-05-26 11:52:06 -04001058void
1059rpcrdma_put_mw(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mw *mw)
Chuck Leverc2922c02014-07-29 17:24:36 -04001060{
Chuck Lever346aa662015-05-26 11:52:06 -04001061 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
Chuck Leverc2922c02014-07-29 17:24:36 -04001062
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001063 spin_lock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001064 list_add_tail(&mw->mw_list, &buf->rb_mws);
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001065 spin_unlock(&buf->rb_mwlock);
Chuck Leverc2922c02014-07-29 17:24:36 -04001066}
1067
1068static void
1069rpcrdma_buffer_put_sendbuf(struct rpcrdma_req *req, struct rpcrdma_buffer *buf)
1070{
1071 buf->rb_send_bufs[--buf->rb_send_index] = req;
1072 req->rl_niovs = 0;
1073 if (req->rl_reply) {
1074 buf->rb_recv_bufs[--buf->rb_recv_index] = req->rl_reply;
Chuck Leverc2922c02014-07-29 17:24:36 -04001075 req->rl_reply = NULL;
1076 }
1077}
1078
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001079/*
1080 * Get a set of request/reply buffers.
1081 *
1082 * Reply buffer (if needed) is attached to send buffer upon return.
1083 * Rule:
1084 * rb_send_index and rb_recv_index MUST always be pointing to the
1085 * *next* available buffer (non-NULL). They are incremented after
1086 * removing buffers, and decremented *before* returning them.
1087 */
1088struct rpcrdma_req *
1089rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1090{
1091 struct rpcrdma_req *req;
1092 unsigned long flags;
1093
1094 spin_lock_irqsave(&buffers->rb_lock, flags);
Chuck Leverc14d86e2015-05-26 11:52:35 -04001095
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001096 if (buffers->rb_send_index == buffers->rb_max_requests) {
1097 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1098 dprintk("RPC: %s: out of request buffers\n", __func__);
1099 return ((struct rpcrdma_req *)NULL);
1100 }
1101
1102 req = buffers->rb_send_bufs[buffers->rb_send_index];
1103 if (buffers->rb_send_index < buffers->rb_recv_index) {
1104 dprintk("RPC: %s: %d extra receives outstanding (ok)\n",
1105 __func__,
1106 buffers->rb_recv_index - buffers->rb_send_index);
1107 req->rl_reply = NULL;
1108 } else {
1109 req->rl_reply = buffers->rb_recv_bufs[buffers->rb_recv_index];
1110 buffers->rb_recv_bufs[buffers->rb_recv_index++] = NULL;
1111 }
1112 buffers->rb_send_bufs[buffers->rb_send_index++] = NULL;
Chuck Leverddb6beb2014-07-29 17:24:54 -04001113
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001114 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1115 return req;
1116}
1117
1118/*
1119 * Put request/reply buffers back into pool.
1120 * Pre-decrement counter/array index.
1121 */
1122void
1123rpcrdma_buffer_put(struct rpcrdma_req *req)
1124{
1125 struct rpcrdma_buffer *buffers = req->rl_buffer;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001126 unsigned long flags;
1127
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001128 spin_lock_irqsave(&buffers->rb_lock, flags);
Chuck Leverc2922c02014-07-29 17:24:36 -04001129 rpcrdma_buffer_put_sendbuf(req, buffers);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001130 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1131}
1132
1133/*
1134 * Recover reply buffers from pool.
1135 * This happens when recovering from error conditions.
1136 * Post-increment counter/array index.
1137 */
1138void
1139rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1140{
1141 struct rpcrdma_buffer *buffers = req->rl_buffer;
1142 unsigned long flags;
1143
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001144 spin_lock_irqsave(&buffers->rb_lock, flags);
1145 if (buffers->rb_recv_index < buffers->rb_max_requests) {
1146 req->rl_reply = buffers->rb_recv_bufs[buffers->rb_recv_index];
1147 buffers->rb_recv_bufs[buffers->rb_recv_index++] = NULL;
1148 }
1149 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1150}
1151
1152/*
1153 * Put reply buffers back into pool when not attached to
Chuck Leverb45ccfd2014-05-28 10:32:34 -04001154 * request. This happens in error conditions.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001155 */
1156void
1157rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1158{
Chuck Leverfed171b2015-05-26 11:51:37 -04001159 struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001160 unsigned long flags;
1161
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001162 spin_lock_irqsave(&buffers->rb_lock, flags);
1163 buffers->rb_recv_bufs[--buffers->rb_recv_index] = rep;
1164 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1165}
1166
1167/*
1168 * Wrappers for internal-use kmalloc memory registration, used by buffer code.
1169 */
1170
Chuck Leverd6547882015-03-30 14:35:44 -04001171void
1172rpcrdma_mapping_error(struct rpcrdma_mr_seg *seg)
1173{
1174 dprintk("RPC: map_one: offset %p iova %llx len %zu\n",
1175 seg->mr_offset,
1176 (unsigned long long)seg->mr_dma, seg->mr_dmalen);
1177}
1178
Chuck Lever9128c3e2015-01-21 11:04:00 -05001179/**
1180 * rpcrdma_alloc_regbuf - kmalloc and register memory for SEND/RECV buffers
1181 * @ia: controlling rpcrdma_ia
1182 * @size: size of buffer to be allocated, in bytes
1183 * @flags: GFP flags
1184 *
1185 * Returns pointer to private header of an area of internally
1186 * registered memory, or an ERR_PTR. The registered buffer follows
1187 * the end of the private header.
1188 *
1189 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
1190 * receiving the payload of RDMA RECV operations. regbufs are not
1191 * used for RDMA READ/WRITE operations, thus are registered only for
1192 * LOCAL access.
1193 */
1194struct rpcrdma_regbuf *
1195rpcrdma_alloc_regbuf(struct rpcrdma_ia *ia, size_t size, gfp_t flags)
1196{
1197 struct rpcrdma_regbuf *rb;
Chuck Levere531dca2015-08-03 13:03:20 -04001198 struct ib_sge *iov;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001199
Chuck Lever9128c3e2015-01-21 11:04:00 -05001200 rb = kmalloc(sizeof(*rb) + size, flags);
1201 if (rb == NULL)
1202 goto out;
1203
Chuck Levere531dca2015-08-03 13:03:20 -04001204 iov = &rb->rg_iov;
1205 iov->addr = ib_dma_map_single(ia->ri_device,
1206 (void *)rb->rg_base, size,
1207 DMA_BIDIRECTIONAL);
1208 if (ib_dma_mapping_error(ia->ri_device, iov->addr))
Chuck Lever9128c3e2015-01-21 11:04:00 -05001209 goto out_free;
1210
Chuck Levere531dca2015-08-03 13:03:20 -04001211 iov->length = size;
Chuck Leverbb6c96d2015-09-24 10:34:21 +03001212 iov->lkey = ia->ri_pd->local_dma_lkey;
Chuck Levere531dca2015-08-03 13:03:20 -04001213 rb->rg_size = size;
1214 rb->rg_owner = NULL;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001215 return rb;
1216
1217out_free:
1218 kfree(rb);
1219out:
Chuck Levere531dca2015-08-03 13:03:20 -04001220 return ERR_PTR(-ENOMEM);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001221}
1222
1223/**
1224 * rpcrdma_free_regbuf - deregister and free registered buffer
1225 * @ia: controlling rpcrdma_ia
1226 * @rb: regbuf to be deregistered and freed
1227 */
1228void
1229rpcrdma_free_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1230{
Chuck Levere531dca2015-08-03 13:03:20 -04001231 struct ib_sge *iov;
1232
1233 if (!rb)
1234 return;
1235
1236 iov = &rb->rg_iov;
1237 ib_dma_unmap_single(ia->ri_device,
1238 iov->addr, iov->length, DMA_BIDIRECTIONAL);
1239 kfree(rb);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001240}
1241
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001242/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001243 * Prepost any receive buffer, then post send.
1244 *
1245 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1246 */
1247int
1248rpcrdma_ep_post(struct rpcrdma_ia *ia,
1249 struct rpcrdma_ep *ep,
1250 struct rpcrdma_req *req)
1251{
Chuck Leverb3221d62015-08-03 13:03:39 -04001252 struct ib_device *device = ia->ri_device;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001253 struct ib_send_wr send_wr, *send_wr_fail;
1254 struct rpcrdma_rep *rep = req->rl_reply;
Chuck Leverb3221d62015-08-03 13:03:39 -04001255 struct ib_sge *iov = req->rl_send_iov;
1256 int i, rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001257
1258 if (rep) {
1259 rc = rpcrdma_ep_post_recv(ia, ep, rep);
1260 if (rc)
1261 goto out;
1262 req->rl_reply = NULL;
1263 }
1264
1265 send_wr.next = NULL;
Chuck Levere46ac342015-03-30 14:35:35 -04001266 send_wr.wr_id = RPCRDMA_IGNORE_COMPLETION;
Chuck Leverb3221d62015-08-03 13:03:39 -04001267 send_wr.sg_list = iov;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001268 send_wr.num_sge = req->rl_niovs;
1269 send_wr.opcode = IB_WR_SEND;
Chuck Leverb3221d62015-08-03 13:03:39 -04001270
1271 for (i = 0; i < send_wr.num_sge; i++)
1272 ib_dma_sync_single_for_device(device, iov[i].addr,
1273 iov[i].length, DMA_TO_DEVICE);
1274 dprintk("RPC: %s: posting %d s/g entries\n",
1275 __func__, send_wr.num_sge);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001276
1277 if (DECR_CQCOUNT(ep) > 0)
1278 send_wr.send_flags = 0;
1279 else { /* Provider must take a send completion every now and then */
1280 INIT_CQCOUNT(ep);
1281 send_wr.send_flags = IB_SEND_SIGNALED;
1282 }
1283
1284 rc = ib_post_send(ia->ri_id->qp, &send_wr, &send_wr_fail);
1285 if (rc)
1286 dprintk("RPC: %s: ib_post_send returned %i\n", __func__,
1287 rc);
1288out:
1289 return rc;
1290}
1291
1292/*
1293 * (Re)post a receive buffer.
1294 */
1295int
1296rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
1297 struct rpcrdma_ep *ep,
1298 struct rpcrdma_rep *rep)
1299{
1300 struct ib_recv_wr recv_wr, *recv_wr_fail;
1301 int rc;
1302
1303 recv_wr.next = NULL;
1304 recv_wr.wr_id = (u64) (unsigned long) rep;
Chuck Lever6b1184c2015-01-21 11:04:25 -05001305 recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001306 recv_wr.num_sge = 1;
1307
Chuck Lever89e0d1122015-05-26 11:51:56 -04001308 ib_dma_sync_single_for_cpu(ia->ri_device,
Chuck Lever6b1184c2015-01-21 11:04:25 -05001309 rdmab_addr(rep->rr_rdmabuf),
1310 rdmab_length(rep->rr_rdmabuf),
1311 DMA_BIDIRECTIONAL);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001312
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001313 rc = ib_post_recv(ia->ri_id->qp, &recv_wr, &recv_wr_fail);
1314
1315 if (rc)
1316 dprintk("RPC: %s: ib_post_recv returned %i\n", __func__,
1317 rc);
1318 return rc;
1319}
Chuck Lever43e95982014-07-29 17:23:34 -04001320
Chuck Lever1c9351e2015-03-30 14:34:30 -04001321/* How many chunk list items fit within our inline buffers?
Chuck Lever43e95982014-07-29 17:23:34 -04001322 */
Chuck Lever1c9351e2015-03-30 14:34:30 -04001323unsigned int
1324rpcrdma_max_segments(struct rpcrdma_xprt *r_xprt)
Chuck Lever43e95982014-07-29 17:23:34 -04001325{
1326 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Lever1c9351e2015-03-30 14:34:30 -04001327 int bytes, segments;
Chuck Lever43e95982014-07-29 17:23:34 -04001328
Chuck Lever1c9351e2015-03-30 14:34:30 -04001329 bytes = min_t(unsigned int, cdata->inline_wsize, cdata->inline_rsize);
1330 bytes -= RPCRDMA_HDRLEN_MIN;
1331 if (bytes < sizeof(struct rpcrdma_segment) * 2) {
1332 pr_warn("RPC: %s: inline threshold too small\n",
1333 __func__);
1334 return 0;
Chuck Lever43e95982014-07-29 17:23:34 -04001335 }
Chuck Lever1c9351e2015-03-30 14:34:30 -04001336
1337 segments = 1 << (fls(bytes / sizeof(struct rpcrdma_segment)) - 1);
1338 dprintk("RPC: %s: max chunk list size = %d segments\n",
1339 __func__, segments);
1340 return segments;
Chuck Lever43e95982014-07-29 17:23:34 -04001341}