blob: 3dd5a7c951c5b66158f60ef6e6ac798a58b1263e [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
Chuck Leverfe97b472015-10-24 17:27:10 -040071static struct workqueue_struct *rpcrdma_receive_wq;
72
73int
74rpcrdma_alloc_wq(void)
75{
76 struct workqueue_struct *recv_wq;
77
78 recv_wq = alloc_workqueue("xprtrdma_receive",
79 WQ_MEM_RECLAIM | WQ_UNBOUND | WQ_HIGHPRI,
80 0);
81 if (!recv_wq)
82 return -ENOMEM;
83
84 rpcrdma_receive_wq = recv_wq;
85 return 0;
86}
87
88void
89rpcrdma_destroy_wq(void)
90{
91 struct workqueue_struct *wq;
92
93 if (rpcrdma_receive_wq) {
94 wq = rpcrdma_receive_wq;
95 rpcrdma_receive_wq = NULL;
96 destroy_workqueue(wq);
97 }
98}
99
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400100static void
101rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context)
102{
103 struct rpcrdma_ep *ep = context;
104
Chuck Lever7ff11de2014-11-08 20:15:01 -0500105 pr_err("RPC: %s: %s on device %s ep %p\n",
Sagi Grimberg76357c72015-05-18 13:40:32 +0300106 __func__, ib_event_msg(event->event),
Chuck Lever7ff11de2014-11-08 20:15:01 -0500107 event->device->name, context);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400108 if (ep->rep_connected == 1) {
109 ep->rep_connected = -EIO;
Chuck Leverafadc462015-01-21 11:03:11 -0500110 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400111 wake_up_all(&ep->rep_connect_wait);
112 }
113}
114
115static void
116rpcrdma_cq_async_error_upcall(struct ib_event *event, void *context)
117{
118 struct rpcrdma_ep *ep = context;
119
Chuck Lever7ff11de2014-11-08 20:15:01 -0500120 pr_err("RPC: %s: %s on device %s ep %p\n",
Sagi Grimberg76357c72015-05-18 13:40:32 +0300121 __func__, ib_event_msg(event->event),
Chuck Lever7ff11de2014-11-08 20:15:01 -0500122 event->device->name, context);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400123 if (ep->rep_connected == 1) {
124 ep->rep_connected = -EIO;
Chuck Leverafadc462015-01-21 11:03:11 -0500125 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400126 wake_up_all(&ep->rep_connect_wait);
127 }
128}
129
Chuck Leverfc664482014-05-28 10:33:25 -0400130static void
131rpcrdma_sendcq_process_wc(struct ib_wc *wc)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400132{
Chuck Lever85024272015-01-21 11:02:04 -0500133 /* WARNING: Only wr_id and status are reliable at this point */
Chuck Levere46ac342015-03-30 14:35:35 -0400134 if (wc->wr_id == RPCRDMA_IGNORE_COMPLETION) {
135 if (wc->status != IB_WC_SUCCESS &&
136 wc->status != IB_WC_WR_FLUSH_ERR)
Chuck Lever85024272015-01-21 11:02:04 -0500137 pr_err("RPC: %s: SEND: %s\n",
Sagi Grimberg76357c72015-05-18 13:40:32 +0300138 __func__, ib_wc_status_msg(wc->status));
Chuck Lever85024272015-01-21 11:02:04 -0500139 } else {
140 struct rpcrdma_mw *r;
141
142 r = (struct rpcrdma_mw *)(unsigned long)wc->wr_id;
Chuck Levere46ac342015-03-30 14:35:35 -0400143 r->mw_sendcompletion(wc);
Chuck Lever85024272015-01-21 11:02:04 -0500144 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400145}
146
Chuck Lever4220a072015-10-24 17:26:45 -0400147/* The common case is a single send completion is waiting. By
148 * passing two WC entries to ib_poll_cq, a return code of 1
149 * means there is exactly one WC waiting and no more. We don't
150 * have to invoke ib_poll_cq again to know that the CQ has been
151 * properly drained.
152 */
153static void
154rpcrdma_sendcq_poll(struct ib_cq *cq)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400155{
Chuck Lever4220a072015-10-24 17:26:45 -0400156 struct ib_wc *pos, wcs[2];
157 int count, rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400158
Chuck Lever1c00dd02014-05-28 10:33:42 -0400159 do {
Chuck Lever4220a072015-10-24 17:26:45 -0400160 pos = wcs;
Chuck Lever1c00dd02014-05-28 10:33:42 -0400161
Chuck Lever4220a072015-10-24 17:26:45 -0400162 rc = ib_poll_cq(cq, ARRAY_SIZE(wcs), pos);
163 if (rc < 0)
164 break;
Chuck Lever1c00dd02014-05-28 10:33:42 -0400165
166 count = rc;
167 while (count-- > 0)
Chuck Lever4220a072015-10-24 17:26:45 -0400168 rpcrdma_sendcq_process_wc(pos++);
169 } while (rc == ARRAY_SIZE(wcs));
170 return;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400171}
172
Chuck Lever7b3d7702015-10-24 17:26:37 -0400173/* Handle provider send completion upcalls.
Chuck Leverfc664482014-05-28 10:33:25 -0400174 */
175static void
176rpcrdma_sendcq_upcall(struct ib_cq *cq, void *cq_context)
177{
Chuck Lever7b3d7702015-10-24 17:26:37 -0400178 do {
Chuck Lever4220a072015-10-24 17:26:45 -0400179 rpcrdma_sendcq_poll(cq);
Chuck Lever7b3d7702015-10-24 17:26:37 -0400180 } while (ib_req_notify_cq(cq, IB_CQ_NEXT_COMP |
181 IB_CQ_REPORT_MISSED_EVENTS) > 0);
Chuck Leverfc664482014-05-28 10:33:25 -0400182}
183
184static void
Chuck Leverfe97b472015-10-24 17:27:10 -0400185rpcrdma_receive_worker(struct work_struct *work)
186{
187 struct rpcrdma_rep *rep =
188 container_of(work, struct rpcrdma_rep, rr_work);
189
190 rpcrdma_reply_handler(rep);
191}
192
193static void
194rpcrdma_recvcq_process_wc(struct ib_wc *wc)
Chuck Leverfc664482014-05-28 10:33:25 -0400195{
196 struct rpcrdma_rep *rep =
197 (struct rpcrdma_rep *)(unsigned long)wc->wr_id;
198
Chuck Lever85024272015-01-21 11:02:04 -0500199 /* WARNING: Only wr_id and status are reliable at this point */
200 if (wc->status != IB_WC_SUCCESS)
201 goto out_fail;
Chuck Leverfc664482014-05-28 10:33:25 -0400202
Chuck Lever85024272015-01-21 11:02:04 -0500203 /* status == SUCCESS means all fields in wc are trustworthy */
Chuck Leverfc664482014-05-28 10:33:25 -0400204 if (wc->opcode != IB_WC_RECV)
205 return;
206
Chuck Lever85024272015-01-21 11:02:04 -0500207 dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n",
208 __func__, rep, wc->byte_len);
209
Chuck Leverfc664482014-05-28 10:33:25 -0400210 rep->rr_len = wc->byte_len;
Chuck Lever89e0d1122015-05-26 11:51:56 -0400211 ib_dma_sync_single_for_cpu(rep->rr_device,
Chuck Lever6b1184c2015-01-21 11:04:25 -0500212 rdmab_addr(rep->rr_rdmabuf),
213 rep->rr_len, DMA_FROM_DEVICE);
214 prefetch(rdmab_to_msg(rep->rr_rdmabuf));
Chuck Leverfc664482014-05-28 10:33:25 -0400215
216out_schedule:
Chuck Leverfe97b472015-10-24 17:27:10 -0400217 queue_work(rpcrdma_receive_wq, &rep->rr_work);
Chuck Lever85024272015-01-21 11:02:04 -0500218 return;
Chuck Leverfe97b472015-10-24 17:27:10 -0400219
Chuck Lever85024272015-01-21 11:02:04 -0500220out_fail:
221 if (wc->status != IB_WC_WR_FLUSH_ERR)
222 pr_err("RPC: %s: rep %p: %s\n",
Sagi Grimberg76357c72015-05-18 13:40:32 +0300223 __func__, rep, ib_wc_status_msg(wc->status));
Chuck Leverb0e178a2015-10-24 17:26:54 -0400224 rep->rr_len = RPCRDMA_BAD_LEN;
Chuck Lever85024272015-01-21 11:02:04 -0500225 goto out_schedule;
Chuck Leverfc664482014-05-28 10:33:25 -0400226}
227
Chuck Lever4220a072015-10-24 17:26:45 -0400228/* The wc array is on stack: automatic memory is always CPU-local.
229 *
230 * struct ib_wc is 64 bytes, making the poll array potentially
231 * large. But this is at the bottom of the call chain. Further
232 * substantial work is done in another thread.
233 */
234static void
235rpcrdma_recvcq_poll(struct ib_cq *cq)
Chuck Leverfc664482014-05-28 10:33:25 -0400236{
Chuck Lever4220a072015-10-24 17:26:45 -0400237 struct ib_wc *pos, wcs[4];
Chuck Lever4220a072015-10-24 17:26:45 -0400238 int count, rc;
Chuck Leverfc664482014-05-28 10:33:25 -0400239
Chuck Lever1c00dd02014-05-28 10:33:42 -0400240 do {
Chuck Lever4220a072015-10-24 17:26:45 -0400241 pos = wcs;
Chuck Lever1c00dd02014-05-28 10:33:42 -0400242
Chuck Lever4220a072015-10-24 17:26:45 -0400243 rc = ib_poll_cq(cq, ARRAY_SIZE(wcs), pos);
244 if (rc < 0)
245 break;
Chuck Lever1c00dd02014-05-28 10:33:42 -0400246
247 count = rc;
248 while (count-- > 0)
Chuck Leverfe97b472015-10-24 17:27:10 -0400249 rpcrdma_recvcq_process_wc(pos++);
Chuck Lever4220a072015-10-24 17:26:45 -0400250 } while (rc == ARRAY_SIZE(wcs));
Chuck Leverfc664482014-05-28 10:33:25 -0400251}
252
Chuck Lever7b3d7702015-10-24 17:26:37 -0400253/* Handle provider receive completion upcalls.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400254 */
255static void
Chuck Leverfc664482014-05-28 10:33:25 -0400256rpcrdma_recvcq_upcall(struct ib_cq *cq, void *cq_context)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400257{
Chuck Lever7b3d7702015-10-24 17:26:37 -0400258 do {
Chuck Lever4220a072015-10-24 17:26:45 -0400259 rpcrdma_recvcq_poll(cq);
Chuck Lever7b3d7702015-10-24 17:26:37 -0400260 } while (ib_req_notify_cq(cq, IB_CQ_NEXT_COMP |
261 IB_CQ_REPORT_MISSED_EVENTS) > 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400262}
263
Chuck Levera7bc2112014-07-29 17:23:52 -0400264static void
265rpcrdma_flush_cqs(struct rpcrdma_ep *ep)
266{
Chuck Lever5c166be2014-11-08 20:14:45 -0500267 struct ib_wc wc;
Chuck Lever5c166be2014-11-08 20:14:45 -0500268
269 while (ib_poll_cq(ep->rep_attr.recv_cq, 1, &wc) > 0)
Chuck Leverfe97b472015-10-24 17:27:10 -0400270 rpcrdma_recvcq_process_wc(&wc);
Chuck Lever5c166be2014-11-08 20:14:45 -0500271 while (ib_poll_cq(ep->rep_attr.send_cq, 1, &wc) > 0)
272 rpcrdma_sendcq_process_wc(&wc);
Chuck Levera7bc2112014-07-29 17:23:52 -0400273}
274
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400275static int
276rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
277{
278 struct rpcrdma_xprt *xprt = id->context;
279 struct rpcrdma_ia *ia = &xprt->rx_ia;
280 struct rpcrdma_ep *ep = &xprt->rx_ep;
Jeff Laytonf895b252014-11-17 16:58:04 -0500281#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400282 struct sockaddr *sap = (struct sockaddr *)&ep->rep_remote_addr;
Ingo Molnarff0db042008-11-25 16:58:42 -0800283#endif
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500284 struct ib_qp_attr *attr = &ia->ri_qp_attr;
285 struct ib_qp_init_attr *iattr = &ia->ri_qp_init_attr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400286 int connstate = 0;
287
288 switch (event->event) {
289 case RDMA_CM_EVENT_ADDR_RESOLVED:
290 case RDMA_CM_EVENT_ROUTE_RESOLVED:
Tom Talpey5675add2008-10-09 15:01:41 -0400291 ia->ri_async_rc = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400292 complete(&ia->ri_done);
293 break;
294 case RDMA_CM_EVENT_ADDR_ERROR:
295 ia->ri_async_rc = -EHOSTUNREACH;
296 dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
297 __func__, ep);
298 complete(&ia->ri_done);
299 break;
300 case RDMA_CM_EVENT_ROUTE_ERROR:
301 ia->ri_async_rc = -ENETUNREACH;
302 dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
303 __func__, ep);
304 complete(&ia->ri_done);
305 break;
306 case RDMA_CM_EVENT_ESTABLISHED:
307 connstate = 1;
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500308 ib_query_qp(ia->ri_id->qp, attr,
309 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_MAX_DEST_RD_ATOMIC,
310 iattr);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400311 dprintk("RPC: %s: %d responder resources"
312 " (%d initiator)\n",
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500313 __func__, attr->max_dest_rd_atomic,
314 attr->max_rd_atomic);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400315 goto connected;
316 case RDMA_CM_EVENT_CONNECT_ERROR:
317 connstate = -ENOTCONN;
318 goto connected;
319 case RDMA_CM_EVENT_UNREACHABLE:
320 connstate = -ENETDOWN;
321 goto connected;
322 case RDMA_CM_EVENT_REJECTED:
323 connstate = -ECONNREFUSED;
324 goto connected;
325 case RDMA_CM_EVENT_DISCONNECTED:
326 connstate = -ECONNABORTED;
327 goto connected;
328 case RDMA_CM_EVENT_DEVICE_REMOVAL:
329 connstate = -ENODEV;
330connected:
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400331 dprintk("RPC: %s: %sconnected\n",
332 __func__, connstate > 0 ? "" : "dis");
333 ep->rep_connected = connstate;
Chuck Leverafadc462015-01-21 11:03:11 -0500334 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400335 wake_up_all(&ep->rep_connect_wait);
Chuck Lever8079fb72014-07-29 17:26:12 -0400336 /*FALLTHROUGH*/
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400337 default:
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400338 dprintk("RPC: %s: %pIS:%u (ep 0x%p): %s\n",
339 __func__, sap, rpc_get_port(sap), ep,
Sagi Grimberg76357c72015-05-18 13:40:32 +0300340 rdma_event_msg(event->event));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400341 break;
342 }
343
Jeff Laytonf895b252014-11-17 16:58:04 -0500344#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400345 if (connstate == 1) {
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500346 int ird = attr->max_dest_rd_atomic;
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400347 int tird = ep->rep_remote_cma.responder_resources;
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400348
Chuck Levera0ce85f2015-03-30 14:34:21 -0400349 pr_info("rpcrdma: connection to %pIS:%u on %s, memreg '%s', %d credits, %d responders%s\n",
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400350 sap, rpc_get_port(sap),
Chuck Lever89e0d1122015-05-26 11:51:56 -0400351 ia->ri_device->name,
Chuck Levera0ce85f2015-03-30 14:34:21 -0400352 ia->ri_ops->ro_displayname,
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400353 xprt->rx_buf.rb_max_requests,
354 ird, ird < 4 && ird < tird / 2 ? " (low!)" : "");
355 } else if (connstate < 0) {
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400356 pr_info("rpcrdma: connection to %pIS:%u closed (%d)\n",
357 sap, rpc_get_port(sap), connstate);
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400358 }
359#endif
360
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400361 return 0;
362}
363
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400364static void rpcrdma_destroy_id(struct rdma_cm_id *id)
365{
366 if (id) {
367 module_put(id->device->owner);
368 rdma_destroy_id(id);
369 }
370}
371
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400372static struct rdma_cm_id *
373rpcrdma_create_id(struct rpcrdma_xprt *xprt,
374 struct rpcrdma_ia *ia, struct sockaddr *addr)
375{
376 struct rdma_cm_id *id;
377 int rc;
378
Tom Talpey1a954052008-10-09 15:01:31 -0400379 init_completion(&ia->ri_done);
380
Sean Heftyb26f9b92010-04-01 17:08:41 +0000381 id = rdma_create_id(rpcrdma_conn_upcall, xprt, RDMA_PS_TCP, IB_QPT_RC);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400382 if (IS_ERR(id)) {
383 rc = PTR_ERR(id);
384 dprintk("RPC: %s: rdma_create_id() failed %i\n",
385 __func__, rc);
386 return id;
387 }
388
Tom Talpey5675add2008-10-09 15:01:41 -0400389 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400390 rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
391 if (rc) {
392 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
393 __func__, rc);
394 goto out;
395 }
Tom Talpey5675add2008-10-09 15:01:41 -0400396 wait_for_completion_interruptible_timeout(&ia->ri_done,
397 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400398
399 /* FIXME:
400 * Until xprtrdma supports DEVICE_REMOVAL, the provider must
401 * be pinned while there are active NFS/RDMA mounts to prevent
402 * hangs and crashes at umount time.
403 */
404 if (!ia->ri_async_rc && !try_module_get(id->device->owner)) {
405 dprintk("RPC: %s: Failed to get device module\n",
406 __func__);
407 ia->ri_async_rc = -ENODEV;
408 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400409 rc = ia->ri_async_rc;
410 if (rc)
411 goto out;
412
Tom Talpey5675add2008-10-09 15:01:41 -0400413 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400414 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
415 if (rc) {
416 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
417 __func__, rc);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400418 goto put;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400419 }
Tom Talpey5675add2008-10-09 15:01:41 -0400420 wait_for_completion_interruptible_timeout(&ia->ri_done,
421 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400422 rc = ia->ri_async_rc;
423 if (rc)
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400424 goto put;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400425
426 return id;
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400427put:
428 module_put(id->device->owner);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400429out:
430 rdma_destroy_id(id);
431 return ERR_PTR(rc);
432}
433
434/*
435 * Drain any cq, prior to teardown.
436 */
437static void
438rpcrdma_clean_cq(struct ib_cq *cq)
439{
440 struct ib_wc wc;
441 int count = 0;
442
443 while (1 == ib_poll_cq(cq, 1, &wc))
444 ++count;
445
446 if (count)
447 dprintk("RPC: %s: flushed %d events (last 0x%x)\n",
448 __func__, count, wc.opcode);
449}
450
451/*
452 * Exported functions.
453 */
454
455/*
456 * Open and initialize an Interface Adapter.
457 * o initializes fields of struct rpcrdma_ia, including
458 * interface and provider attributes and protection zone.
459 */
460int
461rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr, int memreg)
462{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400463 struct rpcrdma_ia *ia = &xprt->rx_ia;
Chuck Lever7bc79722015-01-21 11:03:27 -0500464 struct ib_device_attr *devattr = &ia->ri_devattr;
Chuck Leverd1ed8572015-08-03 13:03:30 -0400465 int rc;
466
467 ia->ri_dma_mr = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400468
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400469 ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
470 if (IS_ERR(ia->ri_id)) {
471 rc = PTR_ERR(ia->ri_id);
472 goto out1;
473 }
Chuck Lever89e0d1122015-05-26 11:51:56 -0400474 ia->ri_device = ia->ri_id->device;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400475
Chuck Lever89e0d1122015-05-26 11:51:56 -0400476 ia->ri_pd = ib_alloc_pd(ia->ri_device);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400477 if (IS_ERR(ia->ri_pd)) {
478 rc = PTR_ERR(ia->ri_pd);
479 dprintk("RPC: %s: ib_alloc_pd() failed %i\n",
480 __func__, rc);
481 goto out2;
482 }
483
Chuck Lever89e0d1122015-05-26 11:51:56 -0400484 rc = ib_query_device(ia->ri_device, devattr);
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400485 if (rc) {
486 dprintk("RPC: %s: ib_query_device failed %d\n",
487 __func__, rc);
Chuck Lever5ae711a2015-01-21 11:03:19 -0500488 goto out3;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400489 }
490
Chuck Leverf10eafd2014-05-28 10:32:51 -0400491 if (memreg == RPCRDMA_FRMR) {
Sagi Grimbergf022fa82015-10-06 19:52:37 +0300492 if (!(devattr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) ||
493 (devattr->max_fast_reg_page_list_len == 0)) {
Tom Talpey3197d3092008-10-09 15:00:20 -0400494 dprintk("RPC: %s: FRMR registration "
Chuck Leverf10eafd2014-05-28 10:32:51 -0400495 "not supported by HCA\n", __func__);
496 memreg = RPCRDMA_MTHCAFMR;
Tom Talpey3197d3092008-10-09 15:00:20 -0400497 }
Chuck Leverf10eafd2014-05-28 10:32:51 -0400498 }
499 if (memreg == RPCRDMA_MTHCAFMR) {
Chuck Lever89e0d1122015-05-26 11:51:56 -0400500 if (!ia->ri_device->alloc_fmr) {
Chuck Leverf10eafd2014-05-28 10:32:51 -0400501 dprintk("RPC: %s: MTHCAFMR registration "
502 "not supported by HCA\n", __func__);
Sagi Grimbergf022fa82015-10-06 19:52:37 +0300503 rc = -EINVAL;
Chuck Leverd2310932015-08-03 13:03:09 -0400504 goto out3;
Chuck Leverf10eafd2014-05-28 10:32:51 -0400505 }
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400506 }
507
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400508 switch (memreg) {
Tom Talpey3197d3092008-10-09 15:00:20 -0400509 case RPCRDMA_FRMR:
Chuck Levera0ce85f2015-03-30 14:34:21 -0400510 ia->ri_ops = &rpcrdma_frwr_memreg_ops;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400511 break;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400512 case RPCRDMA_ALLPHYSICAL:
Chuck Levera0ce85f2015-03-30 14:34:21 -0400513 ia->ri_ops = &rpcrdma_physical_memreg_ops;
Chuck Leverd1ed8572015-08-03 13:03:30 -0400514 break;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400515 case RPCRDMA_MTHCAFMR:
Chuck Levera0ce85f2015-03-30 14:34:21 -0400516 ia->ri_ops = &rpcrdma_fmr_memreg_ops;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400517 break;
518 default:
Chuck Levercdd9ade2014-05-28 10:33:00 -0400519 printk(KERN_ERR "RPC: Unsupported memory "
520 "registration mode: %d\n", memreg);
521 rc = -ENOMEM;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500522 goto out3;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400523 }
Chuck Levera0ce85f2015-03-30 14:34:21 -0400524 dprintk("RPC: %s: memory registration strategy is '%s'\n",
525 __func__, ia->ri_ops->ro_displayname);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400526
Chuck Lever73806c82014-07-29 17:23:25 -0400527 rwlock_init(&ia->ri_qplock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400528 return 0;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500529
530out3:
531 ib_dealloc_pd(ia->ri_pd);
532 ia->ri_pd = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400533out2:
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400534 rpcrdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400535 ia->ri_id = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400536out1:
537 return rc;
538}
539
540/*
541 * Clean up/close an IA.
542 * o if event handles and PD have been initialized, free them.
543 * o close the IA
544 */
545void
546rpcrdma_ia_close(struct rpcrdma_ia *ia)
547{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400548 dprintk("RPC: %s: entering\n", __func__);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400549 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
550 if (ia->ri_id->qp)
551 rdma_destroy_qp(ia->ri_id);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400552 rpcrdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400553 ia->ri_id = NULL;
554 }
Chuck Lever6d446982015-05-26 11:51:27 -0400555
556 /* If the pd is still busy, xprtrdma missed freeing a resource */
557 if (ia->ri_pd && !IS_ERR(ia->ri_pd))
Jason Gunthorpe7dd78642015-08-05 14:34:31 -0600558 ib_dealloc_pd(ia->ri_pd);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400559}
560
561/*
562 * Create unconnected endpoint.
563 */
564int
565rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
566 struct rpcrdma_create_data_internal *cdata)
567{
Chuck Lever7bc79722015-01-21 11:03:27 -0500568 struct ib_device_attr *devattr = &ia->ri_devattr;
Chuck Leverfc664482014-05-28 10:33:25 -0400569 struct ib_cq *sendcq, *recvcq;
Matan Barak8e372102015-06-11 16:35:21 +0300570 struct ib_cq_init_attr cq_attr = {};
Chuck Lever5d40a8a2007-10-26 13:30:54 -0400571 int rc, err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400572
Chuck Leverb3221d62015-08-03 13:03:39 -0400573 if (devattr->max_sge < RPCRDMA_MAX_IOVS) {
574 dprintk("RPC: %s: insufficient sge's available\n",
575 __func__);
576 return -ENOMEM;
577 }
578
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400579 /* check provider's send/recv wr limits */
Chuck Lever7bc79722015-01-21 11:03:27 -0500580 if (cdata->max_requests > devattr->max_qp_wr)
581 cdata->max_requests = devattr->max_qp_wr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400582
583 ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
584 ep->rep_attr.qp_context = ep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400585 ep->rep_attr.srq = NULL;
586 ep->rep_attr.cap.max_send_wr = cdata->max_requests;
Chuck Lever3968cb52015-03-30 14:35:26 -0400587 rc = ia->ri_ops->ro_open(ia, ep, cdata);
588 if (rc)
589 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400590 ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
Chuck Leverb3221d62015-08-03 13:03:39 -0400591 ep->rep_attr.cap.max_send_sge = RPCRDMA_MAX_IOVS;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400592 ep->rep_attr.cap.max_recv_sge = 1;
593 ep->rep_attr.cap.max_inline_data = 0;
594 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
595 ep->rep_attr.qp_type = IB_QPT_RC;
596 ep->rep_attr.port_num = ~0;
597
598 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
599 "iovs: send %d recv %d\n",
600 __func__,
601 ep->rep_attr.cap.max_send_wr,
602 ep->rep_attr.cap.max_recv_wr,
603 ep->rep_attr.cap.max_send_sge,
604 ep->rep_attr.cap.max_recv_sge);
605
606 /* set trigger for requesting send completion */
Chuck Leverfc664482014-05-28 10:33:25 -0400607 ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
Chuck Levere7104a22014-11-08 20:14:20 -0500608 if (ep->rep_cqinit > RPCRDMA_MAX_UNSIGNALED_SENDS)
609 ep->rep_cqinit = RPCRDMA_MAX_UNSIGNALED_SENDS;
610 else if (ep->rep_cqinit <= 2)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400611 ep->rep_cqinit = 0;
612 INIT_CQCOUNT(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400613 init_waitqueue_head(&ep->rep_connect_wait);
Chuck Lever254f91e2014-05-28 10:32:17 -0400614 INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400615
Matan Barak8e372102015-06-11 16:35:21 +0300616 cq_attr.cqe = ep->rep_attr.cap.max_send_wr + 1;
Chuck Lever89e0d1122015-05-26 11:51:56 -0400617 sendcq = ib_create_cq(ia->ri_device, rpcrdma_sendcq_upcall,
Chuck Lever4220a072015-10-24 17:26:45 -0400618 rpcrdma_cq_async_error_upcall, NULL, &cq_attr);
Chuck Leverfc664482014-05-28 10:33:25 -0400619 if (IS_ERR(sendcq)) {
620 rc = PTR_ERR(sendcq);
621 dprintk("RPC: %s: failed to create send CQ: %i\n",
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400622 __func__, rc);
623 goto out1;
624 }
625
Chuck Leverfc664482014-05-28 10:33:25 -0400626 rc = ib_req_notify_cq(sendcq, IB_CQ_NEXT_COMP);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400627 if (rc) {
628 dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
629 __func__, rc);
630 goto out2;
631 }
632
Matan Barak8e372102015-06-11 16:35:21 +0300633 cq_attr.cqe = ep->rep_attr.cap.max_recv_wr + 1;
Chuck Lever89e0d1122015-05-26 11:51:56 -0400634 recvcq = ib_create_cq(ia->ri_device, rpcrdma_recvcq_upcall,
Chuck Lever4220a072015-10-24 17:26:45 -0400635 rpcrdma_cq_async_error_upcall, NULL, &cq_attr);
Chuck Leverfc664482014-05-28 10:33:25 -0400636 if (IS_ERR(recvcq)) {
637 rc = PTR_ERR(recvcq);
638 dprintk("RPC: %s: failed to create recv CQ: %i\n",
639 __func__, rc);
640 goto out2;
641 }
642
643 rc = ib_req_notify_cq(recvcq, IB_CQ_NEXT_COMP);
644 if (rc) {
645 dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
646 __func__, rc);
647 ib_destroy_cq(recvcq);
648 goto out2;
649 }
650
651 ep->rep_attr.send_cq = sendcq;
652 ep->rep_attr.recv_cq = recvcq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400653
654 /* Initialize cma parameters */
655
656 /* RPC/RDMA does not use private data */
657 ep->rep_remote_cma.private_data = NULL;
658 ep->rep_remote_cma.private_data_len = 0;
659
660 /* Client offers RDMA Read but does not initiate */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400661 ep->rep_remote_cma.initiator_depth = 0;
Chuck Lever7bc79722015-01-21 11:03:27 -0500662 if (devattr->max_qp_rd_atom > 32) /* arbitrary but <= 255 */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400663 ep->rep_remote_cma.responder_resources = 32;
664 else
Chuck Lever7bc79722015-01-21 11:03:27 -0500665 ep->rep_remote_cma.responder_resources =
666 devattr->max_qp_rd_atom;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400667
668 ep->rep_remote_cma.retry_count = 7;
669 ep->rep_remote_cma.flow_control = 0;
670 ep->rep_remote_cma.rnr_retry_count = 0;
671
672 return 0;
673
674out2:
Chuck Leverfc664482014-05-28 10:33:25 -0400675 err = ib_destroy_cq(sendcq);
Chuck Lever5d40a8a2007-10-26 13:30:54 -0400676 if (err)
677 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
678 __func__, err);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400679out1:
Chuck Leverd1ed8572015-08-03 13:03:30 -0400680 if (ia->ri_dma_mr)
681 ib_dereg_mr(ia->ri_dma_mr);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400682 return rc;
683}
684
685/*
686 * rpcrdma_ep_destroy
687 *
688 * Disconnect and destroy endpoint. After this, the only
689 * valid operations on the ep are to free it (if dynamically
690 * allocated) or re-create it.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400691 */
Chuck Lever7f1d5412014-05-28 10:33:16 -0400692void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400693rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
694{
695 int rc;
696
697 dprintk("RPC: %s: entering, connected is %d\n",
698 __func__, ep->rep_connected);
699
Chuck Lever254f91e2014-05-28 10:32:17 -0400700 cancel_delayed_work_sync(&ep->rep_connect_worker);
701
Steve Wise72c02172015-09-21 12:24:23 -0500702 if (ia->ri_id->qp)
Chuck Lever282191c2014-07-29 17:25:55 -0400703 rpcrdma_ep_disconnect(ep, ia);
Steve Wise72c02172015-09-21 12:24:23 -0500704
705 rpcrdma_clean_cq(ep->rep_attr.recv_cq);
706 rpcrdma_clean_cq(ep->rep_attr.send_cq);
707
708 if (ia->ri_id->qp) {
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400709 rdma_destroy_qp(ia->ri_id);
710 ia->ri_id->qp = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400711 }
712
Chuck Leverfc664482014-05-28 10:33:25 -0400713 rc = ib_destroy_cq(ep->rep_attr.recv_cq);
714 if (rc)
715 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
716 __func__, rc);
717
Chuck Leverfc664482014-05-28 10:33:25 -0400718 rc = ib_destroy_cq(ep->rep_attr.send_cq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400719 if (rc)
720 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
721 __func__, rc);
Chuck Leverd1ed8572015-08-03 13:03:30 -0400722
723 if (ia->ri_dma_mr) {
724 rc = ib_dereg_mr(ia->ri_dma_mr);
725 dprintk("RPC: %s: ib_dereg_mr returned %i\n",
726 __func__, rc);
727 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400728}
729
730/*
731 * Connect unconnected endpoint.
732 */
733int
734rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
735{
Chuck Lever73806c82014-07-29 17:23:25 -0400736 struct rdma_cm_id *id, *old;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400737 int rc = 0;
738 int retry_count = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400739
Tom Talpeyc0555512008-10-10 11:32:45 -0400740 if (ep->rep_connected != 0) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400741 struct rpcrdma_xprt *xprt;
742retry:
Chuck Leverec62f402014-05-28 10:34:07 -0400743 dprintk("RPC: %s: reconnecting...\n", __func__);
Chuck Lever282191c2014-07-29 17:25:55 -0400744
745 rpcrdma_ep_disconnect(ep, ia);
Chuck Levera7bc2112014-07-29 17:23:52 -0400746 rpcrdma_flush_cqs(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400747
748 xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
749 id = rpcrdma_create_id(xprt, ia,
750 (struct sockaddr *)&xprt->rx_data.addr);
751 if (IS_ERR(id)) {
Chuck Leverec62f402014-05-28 10:34:07 -0400752 rc = -EHOSTUNREACH;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400753 goto out;
754 }
755 /* TEMP TEMP TEMP - fail if new device:
756 * Deregister/remarshal *all* requests!
757 * Close and recreate adapter, pd, etc!
758 * Re-determine all attributes still sane!
759 * More stuff I haven't thought of!
760 * Rrrgh!
761 */
Chuck Lever89e0d1122015-05-26 11:51:56 -0400762 if (ia->ri_device != id->device) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400763 printk("RPC: %s: can't reconnect on "
764 "different device!\n", __func__);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400765 rpcrdma_destroy_id(id);
Chuck Leverec62f402014-05-28 10:34:07 -0400766 rc = -ENETUNREACH;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400767 goto out;
768 }
769 /* END TEMP */
Chuck Leverec62f402014-05-28 10:34:07 -0400770 rc = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
771 if (rc) {
772 dprintk("RPC: %s: rdma_create_qp failed %i\n",
773 __func__, rc);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400774 rpcrdma_destroy_id(id);
Chuck Leverec62f402014-05-28 10:34:07 -0400775 rc = -ENETUNREACH;
776 goto out;
777 }
Chuck Lever73806c82014-07-29 17:23:25 -0400778
779 write_lock(&ia->ri_qplock);
780 old = ia->ri_id;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400781 ia->ri_id = id;
Chuck Lever73806c82014-07-29 17:23:25 -0400782 write_unlock(&ia->ri_qplock);
783
784 rdma_destroy_qp(old);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400785 rpcrdma_destroy_id(old);
Chuck Leverec62f402014-05-28 10:34:07 -0400786 } else {
787 dprintk("RPC: %s: connecting...\n", __func__);
788 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
789 if (rc) {
790 dprintk("RPC: %s: rdma_create_qp failed %i\n",
791 __func__, rc);
792 /* do not update ep->rep_connected */
793 return -ENETUNREACH;
794 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400795 }
796
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400797 ep->rep_connected = 0;
798
799 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
800 if (rc) {
801 dprintk("RPC: %s: rdma_connect() failed with %i\n",
802 __func__, rc);
803 goto out;
804 }
805
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400806 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
807
808 /*
809 * Check state. A non-peer reject indicates no listener
810 * (ECONNREFUSED), which may be a transient state. All
811 * others indicate a transport condition which has already
812 * undergone a best-effort.
813 */
Joe Perchesf64f9e72009-11-29 16:55:45 -0800814 if (ep->rep_connected == -ECONNREFUSED &&
815 ++retry_count <= RDMA_CONNECT_RETRY_MAX) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400816 dprintk("RPC: %s: non-peer_reject, retry\n", __func__);
817 goto retry;
818 }
819 if (ep->rep_connected <= 0) {
820 /* Sometimes, the only way to reliably connect to remote
821 * CMs is to use same nonzero values for ORD and IRD. */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400822 if (retry_count++ <= RDMA_CONNECT_RETRY_MAX + 1 &&
823 (ep->rep_remote_cma.responder_resources == 0 ||
824 ep->rep_remote_cma.initiator_depth !=
825 ep->rep_remote_cma.responder_resources)) {
826 if (ep->rep_remote_cma.responder_resources == 0)
827 ep->rep_remote_cma.responder_resources = 1;
828 ep->rep_remote_cma.initiator_depth =
829 ep->rep_remote_cma.responder_resources;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400830 goto retry;
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400831 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400832 rc = ep->rep_connected;
833 } else {
834 dprintk("RPC: %s: connected\n", __func__);
835 }
836
837out:
838 if (rc)
839 ep->rep_connected = rc;
840 return rc;
841}
842
843/*
844 * rpcrdma_ep_disconnect
845 *
846 * This is separate from destroy to facilitate the ability
847 * to reconnect without recreating the endpoint.
848 *
849 * This call is not reentrant, and must not be made in parallel
850 * on the same endpoint.
851 */
Chuck Lever282191c2014-07-29 17:25:55 -0400852void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400853rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
854{
855 int rc;
856
Chuck Levera7bc2112014-07-29 17:23:52 -0400857 rpcrdma_flush_cqs(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400858 rc = rdma_disconnect(ia->ri_id);
859 if (!rc) {
860 /* returns without wait if not connected */
861 wait_event_interruptible(ep->rep_connect_wait,
862 ep->rep_connected != 1);
863 dprintk("RPC: %s: after wait, %sconnected\n", __func__,
864 (ep->rep_connected == 1) ? "still " : "dis");
865 } else {
866 dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
867 ep->rep_connected = rc;
868 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400869}
870
Chuck Lever13924022015-01-21 11:03:52 -0500871static struct rpcrdma_req *
872rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
873{
Chuck Lever13924022015-01-21 11:03:52 -0500874 struct rpcrdma_req *req;
Chuck Lever13924022015-01-21 11:03:52 -0500875
Chuck Lever85275c82015-01-21 11:04:16 -0500876 req = kzalloc(sizeof(*req), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500877 if (req == NULL)
Chuck Lever85275c82015-01-21 11:04:16 -0500878 return ERR_PTR(-ENOMEM);
Chuck Lever13924022015-01-21 11:03:52 -0500879
Chuck Lever13924022015-01-21 11:03:52 -0500880 req->rl_buffer = &r_xprt->rx_buf;
881 return req;
Chuck Lever13924022015-01-21 11:03:52 -0500882}
883
884static struct rpcrdma_rep *
885rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt)
886{
887 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Lever13924022015-01-21 11:03:52 -0500888 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
889 struct rpcrdma_rep *rep;
890 int rc;
891
892 rc = -ENOMEM;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500893 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500894 if (rep == NULL)
895 goto out;
Chuck Lever13924022015-01-21 11:03:52 -0500896
Chuck Lever6b1184c2015-01-21 11:04:25 -0500897 rep->rr_rdmabuf = rpcrdma_alloc_regbuf(ia, cdata->inline_rsize,
898 GFP_KERNEL);
899 if (IS_ERR(rep->rr_rdmabuf)) {
900 rc = PTR_ERR(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -0500901 goto out_free;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500902 }
Chuck Lever13924022015-01-21 11:03:52 -0500903
Chuck Lever89e0d1122015-05-26 11:51:56 -0400904 rep->rr_device = ia->ri_device;
Chuck Leverfed171b2015-05-26 11:51:37 -0400905 rep->rr_rxprt = r_xprt;
Chuck Leverfe97b472015-10-24 17:27:10 -0400906 INIT_WORK(&rep->rr_work, rpcrdma_receive_worker);
Chuck Lever13924022015-01-21 11:03:52 -0500907 return rep;
908
909out_free:
910 kfree(rep);
911out:
912 return ERR_PTR(rc);
913}
914
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400915int
Chuck Leverac920d02015-01-21 11:03:44 -0500916rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400917{
Chuck Leverac920d02015-01-21 11:03:44 -0500918 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
919 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400920 int i, rc;
921
Chuck Lever1e465fd2015-10-24 17:27:02 -0400922 buf->rb_max_requests = r_xprt->rx_data.max_requests;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400923 spin_lock_init(&buf->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400924
Chuck Lever91e70e72015-03-30 14:34:58 -0400925 rc = ia->ri_ops->ro_init(r_xprt);
926 if (rc)
927 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400928
Chuck Lever1e465fd2015-10-24 17:27:02 -0400929 INIT_LIST_HEAD(&buf->rb_send_bufs);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400930 for (i = 0; i < buf->rb_max_requests; i++) {
931 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400932
Chuck Lever13924022015-01-21 11:03:52 -0500933 req = rpcrdma_create_req(r_xprt);
934 if (IS_ERR(req)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400935 dprintk("RPC: %s: request buffer %d alloc"
936 " failed\n", __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -0500937 rc = PTR_ERR(req);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400938 goto out;
939 }
Chuck Lever1e465fd2015-10-24 17:27:02 -0400940 list_add(&req->rl_free, &buf->rb_send_bufs);
941 }
942
943 INIT_LIST_HEAD(&buf->rb_recv_bufs);
944 for (i = 0; i < buf->rb_max_requests + 2; i++) {
945 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400946
Chuck Lever13924022015-01-21 11:03:52 -0500947 rep = rpcrdma_create_rep(r_xprt);
948 if (IS_ERR(rep)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400949 dprintk("RPC: %s: reply buffer %d alloc failed\n",
950 __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -0500951 rc = PTR_ERR(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400952 goto out;
953 }
Chuck Lever1e465fd2015-10-24 17:27:02 -0400954 list_add(&rep->rr_list, &buf->rb_recv_bufs);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400955 }
Chuck Lever13924022015-01-21 11:03:52 -0500956
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400957 return 0;
958out:
959 rpcrdma_buffer_destroy(buf);
960 return rc;
961}
962
Chuck Lever1e465fd2015-10-24 17:27:02 -0400963static struct rpcrdma_req *
964rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf)
965{
966 struct rpcrdma_req *req;
967
968 req = list_first_entry(&buf->rb_send_bufs,
969 struct rpcrdma_req, rl_free);
970 list_del(&req->rl_free);
971 return req;
972}
973
974static struct rpcrdma_rep *
975rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf)
976{
977 struct rpcrdma_rep *rep;
978
979 rep = list_first_entry(&buf->rb_recv_bufs,
980 struct rpcrdma_rep, rr_list);
981 list_del(&rep->rr_list);
982 return rep;
983}
984
Chuck Lever2e845222014-07-29 17:25:38 -0400985static void
Chuck Lever13924022015-01-21 11:03:52 -0500986rpcrdma_destroy_rep(struct rpcrdma_ia *ia, struct rpcrdma_rep *rep)
987{
988 if (!rep)
989 return;
990
Chuck Lever6b1184c2015-01-21 11:04:25 -0500991 rpcrdma_free_regbuf(ia, rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -0500992 kfree(rep);
993}
994
995static void
996rpcrdma_destroy_req(struct rpcrdma_ia *ia, struct rpcrdma_req *req)
997{
998 if (!req)
999 return;
1000
Chuck Lever0ca77dc2015-01-21 11:04:08 -05001001 rpcrdma_free_regbuf(ia, req->rl_sendbuf);
Chuck Lever85275c82015-01-21 11:04:16 -05001002 rpcrdma_free_regbuf(ia, req->rl_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001003 kfree(req);
1004}
1005
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001006void
1007rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1008{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001009 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
1010
Chuck Lever1e465fd2015-10-24 17:27:02 -04001011 while (!list_empty(&buf->rb_recv_bufs)) {
1012 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001013
Chuck Lever1e465fd2015-10-24 17:27:02 -04001014 rep = rpcrdma_buffer_get_rep_locked(buf);
1015 rpcrdma_destroy_rep(ia, rep);
1016 }
1017
1018 while (!list_empty(&buf->rb_send_bufs)) {
1019 struct rpcrdma_req *req;
1020
1021 req = rpcrdma_buffer_get_req_locked(buf);
1022 rpcrdma_destroy_req(ia, req);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001023 }
1024
Chuck Lever4561f342015-03-30 14:35:17 -04001025 ia->ri_ops->ro_destroy(buf);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001026}
1027
Chuck Lever346aa662015-05-26 11:52:06 -04001028struct rpcrdma_mw *
1029rpcrdma_get_mw(struct rpcrdma_xprt *r_xprt)
Chuck Leverc2922c02014-07-29 17:24:36 -04001030{
Chuck Lever346aa662015-05-26 11:52:06 -04001031 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1032 struct rpcrdma_mw *mw = NULL;
Chuck Lever346aa662015-05-26 11:52:06 -04001033
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001034 spin_lock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001035 if (!list_empty(&buf->rb_mws)) {
1036 mw = list_first_entry(&buf->rb_mws,
1037 struct rpcrdma_mw, mw_list);
1038 list_del_init(&mw->mw_list);
Chuck Leverc2922c02014-07-29 17:24:36 -04001039 }
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001040 spin_unlock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001041
1042 if (!mw)
1043 pr_err("RPC: %s: no MWs available\n", __func__);
1044 return mw;
Chuck Leverc2922c02014-07-29 17:24:36 -04001045}
1046
Chuck Lever346aa662015-05-26 11:52:06 -04001047void
1048rpcrdma_put_mw(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mw *mw)
Chuck Leverc2922c02014-07-29 17:24:36 -04001049{
Chuck Lever346aa662015-05-26 11:52:06 -04001050 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
Chuck Leverc2922c02014-07-29 17:24:36 -04001051
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001052 spin_lock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001053 list_add_tail(&mw->mw_list, &buf->rb_mws);
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001054 spin_unlock(&buf->rb_mwlock);
Chuck Leverc2922c02014-07-29 17:24:36 -04001055}
1056
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001057/*
1058 * Get a set of request/reply buffers.
1059 *
Chuck Lever1e465fd2015-10-24 17:27:02 -04001060 * Reply buffer (if available) is attached to send buffer upon return.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001061 */
1062struct rpcrdma_req *
1063rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1064{
1065 struct rpcrdma_req *req;
1066 unsigned long flags;
1067
1068 spin_lock_irqsave(&buffers->rb_lock, flags);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001069 if (list_empty(&buffers->rb_send_bufs))
1070 goto out_reqbuf;
1071 req = rpcrdma_buffer_get_req_locked(buffers);
1072 if (list_empty(&buffers->rb_recv_bufs))
1073 goto out_repbuf;
1074 req->rl_reply = rpcrdma_buffer_get_rep_locked(buffers);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001075 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1076 return req;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001077
1078out_reqbuf:
1079 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1080 pr_warn("RPC: %s: out of request buffers\n", __func__);
1081 return NULL;
1082out_repbuf:
1083 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1084 pr_warn("RPC: %s: out of reply buffers\n", __func__);
1085 req->rl_reply = NULL;
1086 return req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001087}
1088
1089/*
1090 * Put request/reply buffers back into pool.
1091 * Pre-decrement counter/array index.
1092 */
1093void
1094rpcrdma_buffer_put(struct rpcrdma_req *req)
1095{
1096 struct rpcrdma_buffer *buffers = req->rl_buffer;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001097 struct rpcrdma_rep *rep = req->rl_reply;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001098 unsigned long flags;
1099
Chuck Lever1e465fd2015-10-24 17:27:02 -04001100 req->rl_niovs = 0;
1101 req->rl_reply = NULL;
1102
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001103 spin_lock_irqsave(&buffers->rb_lock, flags);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001104 list_add_tail(&req->rl_free, &buffers->rb_send_bufs);
1105 if (rep)
1106 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001107 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1108}
1109
1110/*
1111 * Recover reply buffers from pool.
Chuck Lever1e465fd2015-10-24 17:27:02 -04001112 * This happens when recovering from disconnect.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001113 */
1114void
1115rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1116{
1117 struct rpcrdma_buffer *buffers = req->rl_buffer;
1118 unsigned long flags;
1119
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001120 spin_lock_irqsave(&buffers->rb_lock, flags);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001121 if (!list_empty(&buffers->rb_recv_bufs))
1122 req->rl_reply = rpcrdma_buffer_get_rep_locked(buffers);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001123 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1124}
1125
1126/*
1127 * Put reply buffers back into pool when not attached to
Chuck Leverb45ccfd2014-05-28 10:32:34 -04001128 * request. This happens in error conditions.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001129 */
1130void
1131rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1132{
Chuck Leverfed171b2015-05-26 11:51:37 -04001133 struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001134 unsigned long flags;
1135
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001136 spin_lock_irqsave(&buffers->rb_lock, flags);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001137 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001138 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1139}
1140
1141/*
1142 * Wrappers for internal-use kmalloc memory registration, used by buffer code.
1143 */
1144
Chuck Leverd6547882015-03-30 14:35:44 -04001145void
1146rpcrdma_mapping_error(struct rpcrdma_mr_seg *seg)
1147{
1148 dprintk("RPC: map_one: offset %p iova %llx len %zu\n",
1149 seg->mr_offset,
1150 (unsigned long long)seg->mr_dma, seg->mr_dmalen);
1151}
1152
Chuck Lever9128c3e2015-01-21 11:04:00 -05001153/**
1154 * rpcrdma_alloc_regbuf - kmalloc and register memory for SEND/RECV buffers
1155 * @ia: controlling rpcrdma_ia
1156 * @size: size of buffer to be allocated, in bytes
1157 * @flags: GFP flags
1158 *
1159 * Returns pointer to private header of an area of internally
1160 * registered memory, or an ERR_PTR. The registered buffer follows
1161 * the end of the private header.
1162 *
1163 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
1164 * receiving the payload of RDMA RECV operations. regbufs are not
1165 * used for RDMA READ/WRITE operations, thus are registered only for
1166 * LOCAL access.
1167 */
1168struct rpcrdma_regbuf *
1169rpcrdma_alloc_regbuf(struct rpcrdma_ia *ia, size_t size, gfp_t flags)
1170{
1171 struct rpcrdma_regbuf *rb;
Chuck Levere531dca2015-08-03 13:03:20 -04001172 struct ib_sge *iov;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001173
Chuck Lever9128c3e2015-01-21 11:04:00 -05001174 rb = kmalloc(sizeof(*rb) + size, flags);
1175 if (rb == NULL)
1176 goto out;
1177
Chuck Levere531dca2015-08-03 13:03:20 -04001178 iov = &rb->rg_iov;
1179 iov->addr = ib_dma_map_single(ia->ri_device,
1180 (void *)rb->rg_base, size,
1181 DMA_BIDIRECTIONAL);
1182 if (ib_dma_mapping_error(ia->ri_device, iov->addr))
Chuck Lever9128c3e2015-01-21 11:04:00 -05001183 goto out_free;
1184
Chuck Levere531dca2015-08-03 13:03:20 -04001185 iov->length = size;
Chuck Leverbb6c96d2015-09-24 10:34:21 +03001186 iov->lkey = ia->ri_pd->local_dma_lkey;
Chuck Levere531dca2015-08-03 13:03:20 -04001187 rb->rg_size = size;
1188 rb->rg_owner = NULL;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001189 return rb;
1190
1191out_free:
1192 kfree(rb);
1193out:
Chuck Levere531dca2015-08-03 13:03:20 -04001194 return ERR_PTR(-ENOMEM);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001195}
1196
1197/**
1198 * rpcrdma_free_regbuf - deregister and free registered buffer
1199 * @ia: controlling rpcrdma_ia
1200 * @rb: regbuf to be deregistered and freed
1201 */
1202void
1203rpcrdma_free_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1204{
Chuck Levere531dca2015-08-03 13:03:20 -04001205 struct ib_sge *iov;
1206
1207 if (!rb)
1208 return;
1209
1210 iov = &rb->rg_iov;
1211 ib_dma_unmap_single(ia->ri_device,
1212 iov->addr, iov->length, DMA_BIDIRECTIONAL);
1213 kfree(rb);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001214}
1215
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001216/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001217 * Prepost any receive buffer, then post send.
1218 *
1219 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1220 */
1221int
1222rpcrdma_ep_post(struct rpcrdma_ia *ia,
1223 struct rpcrdma_ep *ep,
1224 struct rpcrdma_req *req)
1225{
Chuck Leverb3221d62015-08-03 13:03:39 -04001226 struct ib_device *device = ia->ri_device;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001227 struct ib_send_wr send_wr, *send_wr_fail;
1228 struct rpcrdma_rep *rep = req->rl_reply;
Chuck Leverb3221d62015-08-03 13:03:39 -04001229 struct ib_sge *iov = req->rl_send_iov;
1230 int i, rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001231
1232 if (rep) {
1233 rc = rpcrdma_ep_post_recv(ia, ep, rep);
1234 if (rc)
1235 goto out;
1236 req->rl_reply = NULL;
1237 }
1238
1239 send_wr.next = NULL;
Chuck Levere46ac342015-03-30 14:35:35 -04001240 send_wr.wr_id = RPCRDMA_IGNORE_COMPLETION;
Chuck Leverb3221d62015-08-03 13:03:39 -04001241 send_wr.sg_list = iov;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001242 send_wr.num_sge = req->rl_niovs;
1243 send_wr.opcode = IB_WR_SEND;
Chuck Leverb3221d62015-08-03 13:03:39 -04001244
1245 for (i = 0; i < send_wr.num_sge; i++)
1246 ib_dma_sync_single_for_device(device, iov[i].addr,
1247 iov[i].length, DMA_TO_DEVICE);
1248 dprintk("RPC: %s: posting %d s/g entries\n",
1249 __func__, send_wr.num_sge);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001250
1251 if (DECR_CQCOUNT(ep) > 0)
1252 send_wr.send_flags = 0;
1253 else { /* Provider must take a send completion every now and then */
1254 INIT_CQCOUNT(ep);
1255 send_wr.send_flags = IB_SEND_SIGNALED;
1256 }
1257
1258 rc = ib_post_send(ia->ri_id->qp, &send_wr, &send_wr_fail);
1259 if (rc)
1260 dprintk("RPC: %s: ib_post_send returned %i\n", __func__,
1261 rc);
1262out:
1263 return rc;
1264}
1265
1266/*
1267 * (Re)post a receive buffer.
1268 */
1269int
1270rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
1271 struct rpcrdma_ep *ep,
1272 struct rpcrdma_rep *rep)
1273{
1274 struct ib_recv_wr recv_wr, *recv_wr_fail;
1275 int rc;
1276
1277 recv_wr.next = NULL;
1278 recv_wr.wr_id = (u64) (unsigned long) rep;
Chuck Lever6b1184c2015-01-21 11:04:25 -05001279 recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001280 recv_wr.num_sge = 1;
1281
Chuck Lever89e0d1122015-05-26 11:51:56 -04001282 ib_dma_sync_single_for_cpu(ia->ri_device,
Chuck Lever6b1184c2015-01-21 11:04:25 -05001283 rdmab_addr(rep->rr_rdmabuf),
1284 rdmab_length(rep->rr_rdmabuf),
1285 DMA_BIDIRECTIONAL);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001286
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001287 rc = ib_post_recv(ia->ri_id->qp, &recv_wr, &recv_wr_fail);
1288
1289 if (rc)
1290 dprintk("RPC: %s: ib_post_recv returned %i\n", __func__,
1291 rc);
1292 return rc;
1293}
Chuck Lever43e95982014-07-29 17:23:34 -04001294
Chuck Lever1c9351e2015-03-30 14:34:30 -04001295/* How many chunk list items fit within our inline buffers?
Chuck Lever43e95982014-07-29 17:23:34 -04001296 */
Chuck Lever1c9351e2015-03-30 14:34:30 -04001297unsigned int
1298rpcrdma_max_segments(struct rpcrdma_xprt *r_xprt)
Chuck Lever43e95982014-07-29 17:23:34 -04001299{
1300 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Lever1c9351e2015-03-30 14:34:30 -04001301 int bytes, segments;
Chuck Lever43e95982014-07-29 17:23:34 -04001302
Chuck Lever1c9351e2015-03-30 14:34:30 -04001303 bytes = min_t(unsigned int, cdata->inline_wsize, cdata->inline_rsize);
1304 bytes -= RPCRDMA_HDRLEN_MIN;
1305 if (bytes < sizeof(struct rpcrdma_segment) * 2) {
1306 pr_warn("RPC: %s: inline threshold too small\n",
1307 __func__);
1308 return 0;
Chuck Lever43e95982014-07-29 17:23:34 -04001309 }
Chuck Lever1c9351e2015-03-30 14:34:30 -04001310
1311 segments = 1 << (fls(bytes / sizeof(struct rpcrdma_segment)) - 1);
1312 dprintk("RPC: %s: max chunk list size = %d segments\n",
1313 __func__, segments);
1314 return segments;
Chuck Lever43e95982014-07-29 17:23:34 -04001315}