blob: 05779f48745b701355813d6f0dd7e0e5b780ee52 [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;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040072
Chuck Leverfe97b472015-10-24 17:27:10 -040073int
74rpcrdma_alloc_wq(void)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040075{
Chuck Leverfe97b472015-10-24 17:27:10 -040076 struct workqueue_struct *recv_wq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040077
Chuck Leverfe97b472015-10-24 17:27:10 -040078 recv_wq = alloc_workqueue("xprtrdma_receive",
79 WQ_MEM_RECLAIM | WQ_UNBOUND | WQ_HIGHPRI,
80 0);
81 if (!recv_wq)
82 return -ENOMEM;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040083
Chuck Leverfe97b472015-10-24 17:27:10 -040084 rpcrdma_receive_wq = recv_wq;
85 return 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040086}
87
Chuck Leverfe97b472015-10-24 17:27:10 -040088void
89rpcrdma_destroy_wq(void)
Chuck Leverf1a03b72014-11-08 20:14:37 -050090{
Chuck Leverfe97b472015-10-24 17:27:10 -040091 struct workqueue_struct *wq;
Chuck Leverf1a03b72014-11-08 20:14:37 -050092
Chuck Leverfe97b472015-10-24 17:27:10 -040093 if (rpcrdma_receive_wq) {
94 wq = rpcrdma_receive_wq;
95 rpcrdma_receive_wq = NULL;
96 destroy_workqueue(wq);
97 }
Chuck Leverf1a03b72014-11-08 20:14:37 -050098}
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
Chuck Lever23826c72016-03-04 11:28:27 -0500193/* Perform basic sanity checking to avoid using garbage
194 * to update the credit grant value.
195 */
196static void
197rpcrdma_update_granted_credits(struct rpcrdma_rep *rep)
198{
199 struct rpcrdma_msg *rmsgp = rdmab_to_msg(rep->rr_rdmabuf);
200 struct rpcrdma_buffer *buffer = &rep->rr_rxprt->rx_buf;
201 u32 credits;
202
203 if (rep->rr_len < RPCRDMA_HDRLEN_ERR)
204 return;
205
206 credits = be32_to_cpu(rmsgp->rm_credit);
207 if (credits == 0)
208 credits = 1; /* don't deadlock */
209 else if (credits > buffer->rb_max_requests)
210 credits = buffer->rb_max_requests;
211
212 atomic_set(&buffer->rb_credits, credits);
213}
214
Chuck Lever552bf222016-03-04 11:28:36 -0500215/**
216 * rpcrdma_receive_wc - Invoked by RDMA provider for each polled Receive WC
217 * @cq: completion queue (ignored)
218 * @wc: completed WR
219 *
220 */
Chuck Leverfe97b472015-10-24 17:27:10 -0400221static void
Chuck Lever552bf222016-03-04 11:28:36 -0500222rpcrdma_receive_wc(struct ib_cq *cq, struct ib_wc *wc)
Chuck Leverfc664482014-05-28 10:33:25 -0400223{
Chuck Lever552bf222016-03-04 11:28:36 -0500224 struct ib_cqe *cqe = wc->wr_cqe;
225 struct rpcrdma_rep *rep = container_of(cqe, struct rpcrdma_rep,
226 rr_cqe);
Chuck Leverfc664482014-05-28 10:33:25 -0400227
Chuck Lever85024272015-01-21 11:02:04 -0500228 /* WARNING: Only wr_id and status are reliable at this point */
229 if (wc->status != IB_WC_SUCCESS)
230 goto out_fail;
Chuck Leverfc664482014-05-28 10:33:25 -0400231
Chuck Lever85024272015-01-21 11:02:04 -0500232 /* status == SUCCESS means all fields in wc are trustworthy */
Chuck Leverfc664482014-05-28 10:33:25 -0400233 if (wc->opcode != IB_WC_RECV)
234 return;
235
Chuck Lever85024272015-01-21 11:02:04 -0500236 dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n",
237 __func__, rep, wc->byte_len);
238
Chuck Leverfc664482014-05-28 10:33:25 -0400239 rep->rr_len = wc->byte_len;
Chuck Lever89e0d1122015-05-26 11:51:56 -0400240 ib_dma_sync_single_for_cpu(rep->rr_device,
Chuck Lever6b1184c2015-01-21 11:04:25 -0500241 rdmab_addr(rep->rr_rdmabuf),
242 rep->rr_len, DMA_FROM_DEVICE);
Chuck Lever23826c72016-03-04 11:28:27 -0500243
244 rpcrdma_update_granted_credits(rep);
Chuck Leverfc664482014-05-28 10:33:25 -0400245
246out_schedule:
Chuck Leverfe97b472015-10-24 17:27:10 -0400247 queue_work(rpcrdma_receive_wq, &rep->rr_work);
Chuck Lever85024272015-01-21 11:02:04 -0500248 return;
Chuck Leverfe97b472015-10-24 17:27:10 -0400249
Chuck Lever85024272015-01-21 11:02:04 -0500250out_fail:
251 if (wc->status != IB_WC_WR_FLUSH_ERR)
Chuck Lever552bf222016-03-04 11:28:36 -0500252 pr_err("rpcrdma: Recv: %s (%u/0x%x)\n",
253 ib_wc_status_msg(wc->status),
254 wc->status, wc->vendor_err);
Chuck Leverb0e178a2015-10-24 17:26:54 -0400255 rep->rr_len = RPCRDMA_BAD_LEN;
Chuck Lever85024272015-01-21 11:02:04 -0500256 goto out_schedule;
Chuck Leverfc664482014-05-28 10:33:25 -0400257}
258
Chuck Levera7bc2112014-07-29 17:23:52 -0400259static void
260rpcrdma_flush_cqs(struct rpcrdma_ep *ep)
261{
Chuck Lever5c166bef2014-11-08 20:14:45 -0500262 struct ib_wc wc;
Chuck Lever5c166bef2014-11-08 20:14:45 -0500263
264 while (ib_poll_cq(ep->rep_attr.recv_cq, 1, &wc) > 0)
Chuck Lever552bf222016-03-04 11:28:36 -0500265 rpcrdma_receive_wc(NULL, &wc);
Chuck Lever5c166bef2014-11-08 20:14:45 -0500266 while (ib_poll_cq(ep->rep_attr.send_cq, 1, &wc) > 0)
267 rpcrdma_sendcq_process_wc(&wc);
Chuck Levera7bc2112014-07-29 17:23:52 -0400268}
269
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400270static int
271rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
272{
273 struct rpcrdma_xprt *xprt = id->context;
274 struct rpcrdma_ia *ia = &xprt->rx_ia;
275 struct rpcrdma_ep *ep = &xprt->rx_ep;
Jeff Laytonf895b252014-11-17 16:58:04 -0500276#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400277 struct sockaddr *sap = (struct sockaddr *)&ep->rep_remote_addr;
Ingo Molnarff0db042008-11-25 16:58:42 -0800278#endif
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500279 struct ib_qp_attr *attr = &ia->ri_qp_attr;
280 struct ib_qp_init_attr *iattr = &ia->ri_qp_init_attr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400281 int connstate = 0;
282
283 switch (event->event) {
284 case RDMA_CM_EVENT_ADDR_RESOLVED:
285 case RDMA_CM_EVENT_ROUTE_RESOLVED:
Tom Talpey5675add2008-10-09 15:01:41 -0400286 ia->ri_async_rc = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400287 complete(&ia->ri_done);
288 break;
289 case RDMA_CM_EVENT_ADDR_ERROR:
290 ia->ri_async_rc = -EHOSTUNREACH;
291 dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
292 __func__, ep);
293 complete(&ia->ri_done);
294 break;
295 case RDMA_CM_EVENT_ROUTE_ERROR:
296 ia->ri_async_rc = -ENETUNREACH;
297 dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
298 __func__, ep);
299 complete(&ia->ri_done);
300 break;
301 case RDMA_CM_EVENT_ESTABLISHED:
302 connstate = 1;
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500303 ib_query_qp(ia->ri_id->qp, attr,
304 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_MAX_DEST_RD_ATOMIC,
305 iattr);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400306 dprintk("RPC: %s: %d responder resources"
307 " (%d initiator)\n",
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500308 __func__, attr->max_dest_rd_atomic,
309 attr->max_rd_atomic);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400310 goto connected;
311 case RDMA_CM_EVENT_CONNECT_ERROR:
312 connstate = -ENOTCONN;
313 goto connected;
314 case RDMA_CM_EVENT_UNREACHABLE:
315 connstate = -ENETDOWN;
316 goto connected;
317 case RDMA_CM_EVENT_REJECTED:
318 connstate = -ECONNREFUSED;
319 goto connected;
320 case RDMA_CM_EVENT_DISCONNECTED:
321 connstate = -ECONNABORTED;
322 goto connected;
323 case RDMA_CM_EVENT_DEVICE_REMOVAL:
324 connstate = -ENODEV;
325connected:
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400326 dprintk("RPC: %s: %sconnected\n",
327 __func__, connstate > 0 ? "" : "dis");
Chuck Lever23826c72016-03-04 11:28:27 -0500328 atomic_set(&xprt->rx_buf.rb_credits, 1);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400329 ep->rep_connected = connstate;
Chuck Leverafadc462015-01-21 11:03:11 -0500330 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400331 wake_up_all(&ep->rep_connect_wait);
Chuck Lever8079fb72014-07-29 17:26:12 -0400332 /*FALLTHROUGH*/
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400333 default:
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400334 dprintk("RPC: %s: %pIS:%u (ep 0x%p): %s\n",
335 __func__, sap, rpc_get_port(sap), ep,
Sagi Grimberg76357c72015-05-18 13:40:32 +0300336 rdma_event_msg(event->event));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400337 break;
338 }
339
Jeff Laytonf895b252014-11-17 16:58:04 -0500340#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400341 if (connstate == 1) {
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500342 int ird = attr->max_dest_rd_atomic;
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400343 int tird = ep->rep_remote_cma.responder_resources;
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400344
Chuck Levera0ce85f2015-03-30 14:34:21 -0400345 pr_info("rpcrdma: connection to %pIS:%u on %s, memreg '%s', %d credits, %d responders%s\n",
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400346 sap, rpc_get_port(sap),
Chuck Lever89e0d1122015-05-26 11:51:56 -0400347 ia->ri_device->name,
Chuck Levera0ce85f2015-03-30 14:34:21 -0400348 ia->ri_ops->ro_displayname,
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400349 xprt->rx_buf.rb_max_requests,
350 ird, ird < 4 && ird < tird / 2 ? " (low!)" : "");
351 } else if (connstate < 0) {
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400352 pr_info("rpcrdma: connection to %pIS:%u closed (%d)\n",
353 sap, rpc_get_port(sap), connstate);
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400354 }
355#endif
356
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400357 return 0;
358}
359
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400360static void rpcrdma_destroy_id(struct rdma_cm_id *id)
361{
362 if (id) {
363 module_put(id->device->owner);
364 rdma_destroy_id(id);
365 }
366}
367
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400368static struct rdma_cm_id *
369rpcrdma_create_id(struct rpcrdma_xprt *xprt,
370 struct rpcrdma_ia *ia, struct sockaddr *addr)
371{
372 struct rdma_cm_id *id;
373 int rc;
374
Tom Talpey1a954052008-10-09 15:01:31 -0400375 init_completion(&ia->ri_done);
376
Guy Shapirofa201052015-10-22 15:20:10 +0300377 id = rdma_create_id(&init_net, rpcrdma_conn_upcall, xprt, RDMA_PS_TCP,
378 IB_QPT_RC);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400379 if (IS_ERR(id)) {
380 rc = PTR_ERR(id);
381 dprintk("RPC: %s: rdma_create_id() failed %i\n",
382 __func__, rc);
383 return id;
384 }
385
Tom Talpey5675add2008-10-09 15:01:41 -0400386 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400387 rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
388 if (rc) {
389 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
390 __func__, rc);
391 goto out;
392 }
Tom Talpey5675add2008-10-09 15:01:41 -0400393 wait_for_completion_interruptible_timeout(&ia->ri_done,
394 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400395
396 /* FIXME:
397 * Until xprtrdma supports DEVICE_REMOVAL, the provider must
398 * be pinned while there are active NFS/RDMA mounts to prevent
399 * hangs and crashes at umount time.
400 */
401 if (!ia->ri_async_rc && !try_module_get(id->device->owner)) {
402 dprintk("RPC: %s: Failed to get device module\n",
403 __func__);
404 ia->ri_async_rc = -ENODEV;
405 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400406 rc = ia->ri_async_rc;
407 if (rc)
408 goto out;
409
Tom Talpey5675add2008-10-09 15:01:41 -0400410 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400411 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
412 if (rc) {
413 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
414 __func__, rc);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400415 goto put;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400416 }
Tom Talpey5675add2008-10-09 15:01:41 -0400417 wait_for_completion_interruptible_timeout(&ia->ri_done,
418 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400419 rc = ia->ri_async_rc;
420 if (rc)
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400421 goto put;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400422
423 return id;
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400424put:
425 module_put(id->device->owner);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400426out:
427 rdma_destroy_id(id);
428 return ERR_PTR(rc);
429}
430
431/*
432 * Drain any cq, prior to teardown.
433 */
434static void
435rpcrdma_clean_cq(struct ib_cq *cq)
436{
437 struct ib_wc wc;
438 int count = 0;
439
440 while (1 == ib_poll_cq(cq, 1, &wc))
441 ++count;
442
443 if (count)
444 dprintk("RPC: %s: flushed %d events (last 0x%x)\n",
445 __func__, count, wc.opcode);
446}
447
448/*
449 * Exported functions.
450 */
451
452/*
453 * Open and initialize an Interface Adapter.
454 * o initializes fields of struct rpcrdma_ia, including
455 * interface and provider attributes and protection zone.
456 */
457int
458rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr, int memreg)
459{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400460 struct rpcrdma_ia *ia = &xprt->rx_ia;
Chuck Leverd1ed8572015-08-03 13:03:30 -0400461 int rc;
462
463 ia->ri_dma_mr = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400464
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400465 ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
466 if (IS_ERR(ia->ri_id)) {
467 rc = PTR_ERR(ia->ri_id);
468 goto out1;
469 }
Chuck Lever89e0d1122015-05-26 11:51:56 -0400470 ia->ri_device = ia->ri_id->device;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400471
Chuck Lever89e0d1122015-05-26 11:51:56 -0400472 ia->ri_pd = ib_alloc_pd(ia->ri_device);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400473 if (IS_ERR(ia->ri_pd)) {
474 rc = PTR_ERR(ia->ri_pd);
475 dprintk("RPC: %s: ib_alloc_pd() failed %i\n",
476 __func__, rc);
477 goto out2;
478 }
479
Chuck Leverf10eafd2014-05-28 10:32:51 -0400480 if (memreg == RPCRDMA_FRMR) {
Or Gerlitze3e45b12015-12-18 10:59:48 +0200481 if (!(ia->ri_device->attrs.device_cap_flags &
482 IB_DEVICE_MEM_MGT_EXTENSIONS) ||
483 (ia->ri_device->attrs.max_fast_reg_page_list_len == 0)) {
Tom Talpey3197d3092008-10-09 15:00:20 -0400484 dprintk("RPC: %s: FRMR registration "
Chuck Leverf10eafd2014-05-28 10:32:51 -0400485 "not supported by HCA\n", __func__);
486 memreg = RPCRDMA_MTHCAFMR;
Tom Talpey3197d3092008-10-09 15:00:20 -0400487 }
Chuck Leverf10eafd2014-05-28 10:32:51 -0400488 }
489 if (memreg == RPCRDMA_MTHCAFMR) {
Chuck Lever89e0d1122015-05-26 11:51:56 -0400490 if (!ia->ri_device->alloc_fmr) {
Chuck Leverf10eafd2014-05-28 10:32:51 -0400491 dprintk("RPC: %s: MTHCAFMR registration "
492 "not supported by HCA\n", __func__);
Sagi Grimbergf022fa82015-10-06 19:52:37 +0300493 rc = -EINVAL;
Chuck Leverd2310932015-08-03 13:03:09 -0400494 goto out3;
Chuck Leverf10eafd2014-05-28 10:32:51 -0400495 }
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400496 }
497
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400498 switch (memreg) {
Tom Talpey3197d3092008-10-09 15:00:20 -0400499 case RPCRDMA_FRMR:
Chuck Levera0ce85f2015-03-30 14:34:21 -0400500 ia->ri_ops = &rpcrdma_frwr_memreg_ops;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400501 break;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400502 case RPCRDMA_ALLPHYSICAL:
Chuck Levera0ce85f2015-03-30 14:34:21 -0400503 ia->ri_ops = &rpcrdma_physical_memreg_ops;
Chuck Leverd1ed8572015-08-03 13:03:30 -0400504 break;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400505 case RPCRDMA_MTHCAFMR:
Chuck Levera0ce85f2015-03-30 14:34:21 -0400506 ia->ri_ops = &rpcrdma_fmr_memreg_ops;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400507 break;
508 default:
Chuck Levercdd9ade2014-05-28 10:33:00 -0400509 printk(KERN_ERR "RPC: Unsupported memory "
510 "registration mode: %d\n", memreg);
511 rc = -ENOMEM;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500512 goto out3;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400513 }
Chuck Levera0ce85f2015-03-30 14:34:21 -0400514 dprintk("RPC: %s: memory registration strategy is '%s'\n",
515 __func__, ia->ri_ops->ro_displayname);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400516
Chuck Lever73806c82014-07-29 17:23:25 -0400517 rwlock_init(&ia->ri_qplock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400518 return 0;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500519
520out3:
521 ib_dealloc_pd(ia->ri_pd);
522 ia->ri_pd = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400523out2:
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400524 rpcrdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400525 ia->ri_id = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400526out1:
527 return rc;
528}
529
530/*
531 * Clean up/close an IA.
532 * o if event handles and PD have been initialized, free them.
533 * o close the IA
534 */
535void
536rpcrdma_ia_close(struct rpcrdma_ia *ia)
537{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400538 dprintk("RPC: %s: entering\n", __func__);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400539 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
540 if (ia->ri_id->qp)
541 rdma_destroy_qp(ia->ri_id);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400542 rpcrdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400543 ia->ri_id = NULL;
544 }
Chuck Lever6d446982015-05-26 11:51:27 -0400545
546 /* If the pd is still busy, xprtrdma missed freeing a resource */
547 if (ia->ri_pd && !IS_ERR(ia->ri_pd))
Jason Gunthorpe7dd78642015-08-05 14:34:31 -0600548 ib_dealloc_pd(ia->ri_pd);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400549}
550
551/*
552 * Create unconnected endpoint.
553 */
554int
555rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
556 struct rpcrdma_create_data_internal *cdata)
557{
Chuck Leverfc664482014-05-28 10:33:25 -0400558 struct ib_cq *sendcq, *recvcq;
Matan Barak8e372102015-06-11 16:35:21 +0300559 struct ib_cq_init_attr cq_attr = {};
Chuck Lever124fa172015-10-24 17:27:51 -0400560 unsigned int max_qp_wr;
Chuck Lever5d40a8a2007-10-26 13:30:54 -0400561 int rc, err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400562
Or Gerlitze3e45b12015-12-18 10:59:48 +0200563 if (ia->ri_device->attrs.max_sge < RPCRDMA_MAX_IOVS) {
Chuck Leverb3221d62015-08-03 13:03:39 -0400564 dprintk("RPC: %s: insufficient sge's available\n",
565 __func__);
566 return -ENOMEM;
567 }
568
Or Gerlitze3e45b12015-12-18 10:59:48 +0200569 if (ia->ri_device->attrs.max_qp_wr <= RPCRDMA_BACKWARD_WRS) {
Chuck Lever124fa172015-10-24 17:27:51 -0400570 dprintk("RPC: %s: insufficient wqe's available\n",
571 __func__);
572 return -ENOMEM;
573 }
Or Gerlitze3e45b12015-12-18 10:59:48 +0200574 max_qp_wr = ia->ri_device->attrs.max_qp_wr - RPCRDMA_BACKWARD_WRS;
Chuck Lever124fa172015-10-24 17:27:51 -0400575
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400576 /* check provider's send/recv wr limits */
Chuck Lever124fa172015-10-24 17:27:51 -0400577 if (cdata->max_requests > max_qp_wr)
578 cdata->max_requests = max_qp_wr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400579
580 ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
581 ep->rep_attr.qp_context = ep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400582 ep->rep_attr.srq = NULL;
583 ep->rep_attr.cap.max_send_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400584 ep->rep_attr.cap.max_send_wr += RPCRDMA_BACKWARD_WRS;
Chuck Lever3968cb52015-03-30 14:35:26 -0400585 rc = ia->ri_ops->ro_open(ia, ep, cdata);
586 if (rc)
587 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400588 ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400589 ep->rep_attr.cap.max_recv_wr += RPCRDMA_BACKWARD_WRS;
Chuck Leverb3221d62015-08-03 13:03:39 -0400590 ep->rep_attr.cap.max_send_sge = RPCRDMA_MAX_IOVS;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400591 ep->rep_attr.cap.max_recv_sge = 1;
592 ep->rep_attr.cap.max_inline_data = 0;
593 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
594 ep->rep_attr.qp_type = IB_QPT_RC;
595 ep->rep_attr.port_num = ~0;
596
597 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
598 "iovs: send %d recv %d\n",
599 __func__,
600 ep->rep_attr.cap.max_send_wr,
601 ep->rep_attr.cap.max_recv_wr,
602 ep->rep_attr.cap.max_send_sge,
603 ep->rep_attr.cap.max_recv_sge);
604
605 /* set trigger for requesting send completion */
Chuck Leverfc664482014-05-28 10:33:25 -0400606 ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
Chuck Lever26ae9d12015-12-16 17:23:20 -0500607 if (ep->rep_cqinit <= 2)
608 ep->rep_cqinit = 0; /* always signal? */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400609 INIT_CQCOUNT(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400610 init_waitqueue_head(&ep->rep_connect_wait);
Chuck Lever254f91e2014-05-28 10:32:17 -0400611 INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400612
Matan Barak8e372102015-06-11 16:35:21 +0300613 cq_attr.cqe = ep->rep_attr.cap.max_send_wr + 1;
Chuck Lever89e0d1122015-05-26 11:51:56 -0400614 sendcq = ib_create_cq(ia->ri_device, rpcrdma_sendcq_upcall,
Chuck Lever4220a072015-10-24 17:26:45 -0400615 rpcrdma_cq_async_error_upcall, NULL, &cq_attr);
Chuck Leverfc664482014-05-28 10:33:25 -0400616 if (IS_ERR(sendcq)) {
617 rc = PTR_ERR(sendcq);
618 dprintk("RPC: %s: failed to create send CQ: %i\n",
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400619 __func__, rc);
620 goto out1;
621 }
622
Chuck Leverfc664482014-05-28 10:33:25 -0400623 rc = ib_req_notify_cq(sendcq, IB_CQ_NEXT_COMP);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400624 if (rc) {
625 dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
626 __func__, rc);
627 goto out2;
628 }
629
Chuck Lever552bf222016-03-04 11:28:36 -0500630 recvcq = ib_alloc_cq(ia->ri_device, NULL,
631 ep->rep_attr.cap.max_recv_wr + 1,
632 0, IB_POLL_SOFTIRQ);
Chuck Leverfc664482014-05-28 10:33:25 -0400633 if (IS_ERR(recvcq)) {
634 rc = PTR_ERR(recvcq);
635 dprintk("RPC: %s: failed to create recv CQ: %i\n",
636 __func__, rc);
637 goto out2;
638 }
639
Chuck Leverfc664482014-05-28 10:33:25 -0400640 ep->rep_attr.send_cq = sendcq;
641 ep->rep_attr.recv_cq = recvcq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400642
643 /* Initialize cma parameters */
644
645 /* RPC/RDMA does not use private data */
646 ep->rep_remote_cma.private_data = NULL;
647 ep->rep_remote_cma.private_data_len = 0;
648
649 /* Client offers RDMA Read but does not initiate */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400650 ep->rep_remote_cma.initiator_depth = 0;
Or Gerlitze3e45b12015-12-18 10:59:48 +0200651 if (ia->ri_device->attrs.max_qp_rd_atom > 32) /* arbitrary but <= 255 */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400652 ep->rep_remote_cma.responder_resources = 32;
653 else
Chuck Lever7bc79722015-01-21 11:03:27 -0500654 ep->rep_remote_cma.responder_resources =
Or Gerlitze3e45b12015-12-18 10:59:48 +0200655 ia->ri_device->attrs.max_qp_rd_atom;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400656
657 ep->rep_remote_cma.retry_count = 7;
658 ep->rep_remote_cma.flow_control = 0;
659 ep->rep_remote_cma.rnr_retry_count = 0;
660
661 return 0;
662
663out2:
Chuck Leverfc664482014-05-28 10:33:25 -0400664 err = ib_destroy_cq(sendcq);
Chuck Lever5d40a8a2007-10-26 13:30:54 -0400665 if (err)
666 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
667 __func__, err);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400668out1:
Chuck Leverd1ed8572015-08-03 13:03:30 -0400669 if (ia->ri_dma_mr)
670 ib_dereg_mr(ia->ri_dma_mr);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400671 return rc;
672}
673
674/*
675 * rpcrdma_ep_destroy
676 *
677 * Disconnect and destroy endpoint. After this, the only
678 * valid operations on the ep are to free it (if dynamically
679 * allocated) or re-create it.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400680 */
Chuck Lever7f1d5412014-05-28 10:33:16 -0400681void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400682rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
683{
684 int rc;
685
686 dprintk("RPC: %s: entering, connected is %d\n",
687 __func__, ep->rep_connected);
688
Chuck Lever254f91e2014-05-28 10:32:17 -0400689 cancel_delayed_work_sync(&ep->rep_connect_worker);
690
Steve Wise72c02172015-09-21 12:24:23 -0500691 if (ia->ri_id->qp)
Chuck Lever282191c2014-07-29 17:25:55 -0400692 rpcrdma_ep_disconnect(ep, ia);
Steve Wise72c02172015-09-21 12:24:23 -0500693
694 rpcrdma_clean_cq(ep->rep_attr.recv_cq);
695 rpcrdma_clean_cq(ep->rep_attr.send_cq);
696
697 if (ia->ri_id->qp) {
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400698 rdma_destroy_qp(ia->ri_id);
699 ia->ri_id->qp = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400700 }
701
Chuck Lever552bf222016-03-04 11:28:36 -0500702 ib_free_cq(ep->rep_attr.recv_cq);
Chuck Leverfc664482014-05-28 10:33:25 -0400703
Chuck Leverfc664482014-05-28 10:33:25 -0400704 rc = ib_destroy_cq(ep->rep_attr.send_cq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400705 if (rc)
706 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
707 __func__, rc);
Chuck Leverd1ed8572015-08-03 13:03:30 -0400708
709 if (ia->ri_dma_mr) {
710 rc = ib_dereg_mr(ia->ri_dma_mr);
711 dprintk("RPC: %s: ib_dereg_mr returned %i\n",
712 __func__, rc);
713 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400714}
715
716/*
717 * Connect unconnected endpoint.
718 */
719int
720rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
721{
Chuck Lever73806c82014-07-29 17:23:25 -0400722 struct rdma_cm_id *id, *old;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400723 int rc = 0;
724 int retry_count = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400725
Tom Talpeyc0555512008-10-10 11:32:45 -0400726 if (ep->rep_connected != 0) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400727 struct rpcrdma_xprt *xprt;
728retry:
Chuck Leverec62f402014-05-28 10:34:07 -0400729 dprintk("RPC: %s: reconnecting...\n", __func__);
Chuck Lever282191c2014-07-29 17:25:55 -0400730
731 rpcrdma_ep_disconnect(ep, ia);
Chuck Levera7bc2112014-07-29 17:23:52 -0400732 rpcrdma_flush_cqs(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400733
734 xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
735 id = rpcrdma_create_id(xprt, ia,
736 (struct sockaddr *)&xprt->rx_data.addr);
737 if (IS_ERR(id)) {
Chuck Leverec62f402014-05-28 10:34:07 -0400738 rc = -EHOSTUNREACH;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400739 goto out;
740 }
741 /* TEMP TEMP TEMP - fail if new device:
742 * Deregister/remarshal *all* requests!
743 * Close and recreate adapter, pd, etc!
744 * Re-determine all attributes still sane!
745 * More stuff I haven't thought of!
746 * Rrrgh!
747 */
Chuck Lever89e0d1122015-05-26 11:51:56 -0400748 if (ia->ri_device != id->device) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400749 printk("RPC: %s: can't reconnect on "
750 "different device!\n", __func__);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400751 rpcrdma_destroy_id(id);
Chuck Leverec62f402014-05-28 10:34:07 -0400752 rc = -ENETUNREACH;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400753 goto out;
754 }
755 /* END TEMP */
Chuck Leverec62f402014-05-28 10:34:07 -0400756 rc = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
757 if (rc) {
758 dprintk("RPC: %s: rdma_create_qp failed %i\n",
759 __func__, rc);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400760 rpcrdma_destroy_id(id);
Chuck Leverec62f402014-05-28 10:34:07 -0400761 rc = -ENETUNREACH;
762 goto out;
763 }
Chuck Lever73806c82014-07-29 17:23:25 -0400764
765 write_lock(&ia->ri_qplock);
766 old = ia->ri_id;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400767 ia->ri_id = id;
Chuck Lever73806c82014-07-29 17:23:25 -0400768 write_unlock(&ia->ri_qplock);
769
770 rdma_destroy_qp(old);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400771 rpcrdma_destroy_id(old);
Chuck Leverec62f402014-05-28 10:34:07 -0400772 } else {
773 dprintk("RPC: %s: connecting...\n", __func__);
774 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
775 if (rc) {
776 dprintk("RPC: %s: rdma_create_qp failed %i\n",
777 __func__, rc);
778 /* do not update ep->rep_connected */
779 return -ENETUNREACH;
780 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400781 }
782
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400783 ep->rep_connected = 0;
784
785 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
786 if (rc) {
787 dprintk("RPC: %s: rdma_connect() failed with %i\n",
788 __func__, rc);
789 goto out;
790 }
791
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400792 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
793
794 /*
795 * Check state. A non-peer reject indicates no listener
796 * (ECONNREFUSED), which may be a transient state. All
797 * others indicate a transport condition which has already
798 * undergone a best-effort.
799 */
Joe Perchesf64f9e72009-11-29 16:55:45 -0800800 if (ep->rep_connected == -ECONNREFUSED &&
801 ++retry_count <= RDMA_CONNECT_RETRY_MAX) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400802 dprintk("RPC: %s: non-peer_reject, retry\n", __func__);
803 goto retry;
804 }
805 if (ep->rep_connected <= 0) {
806 /* Sometimes, the only way to reliably connect to remote
807 * CMs is to use same nonzero values for ORD and IRD. */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400808 if (retry_count++ <= RDMA_CONNECT_RETRY_MAX + 1 &&
809 (ep->rep_remote_cma.responder_resources == 0 ||
810 ep->rep_remote_cma.initiator_depth !=
811 ep->rep_remote_cma.responder_resources)) {
812 if (ep->rep_remote_cma.responder_resources == 0)
813 ep->rep_remote_cma.responder_resources = 1;
814 ep->rep_remote_cma.initiator_depth =
815 ep->rep_remote_cma.responder_resources;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400816 goto retry;
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400817 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400818 rc = ep->rep_connected;
819 } else {
Chuck Leverf531a5d2015-10-24 17:27:43 -0400820 struct rpcrdma_xprt *r_xprt;
821 unsigned int extras;
822
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400823 dprintk("RPC: %s: connected\n", __func__);
Chuck Leverf531a5d2015-10-24 17:27:43 -0400824
825 r_xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
826 extras = r_xprt->rx_buf.rb_bc_srv_max_requests;
827
828 if (extras) {
829 rc = rpcrdma_ep_post_extra_recv(r_xprt, extras);
Dan Carpenter38b95bc2015-11-05 11:37:08 +0300830 if (rc) {
Chuck Leverf531a5d2015-10-24 17:27:43 -0400831 pr_warn("%s: rpcrdma_ep_post_extra_recv: %i\n",
832 __func__, rc);
833 rc = 0;
Dan Carpenter38b95bc2015-11-05 11:37:08 +0300834 }
Chuck Leverf531a5d2015-10-24 17:27:43 -0400835 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400836 }
837
838out:
839 if (rc)
840 ep->rep_connected = rc;
841 return rc;
842}
843
844/*
845 * rpcrdma_ep_disconnect
846 *
847 * This is separate from destroy to facilitate the ability
848 * to reconnect without recreating the endpoint.
849 *
850 * This call is not reentrant, and must not be made in parallel
851 * on the same endpoint.
852 */
Chuck Lever282191c2014-07-29 17:25:55 -0400853void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400854rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
855{
856 int rc;
857
Chuck Levera7bc2112014-07-29 17:23:52 -0400858 rpcrdma_flush_cqs(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400859 rc = rdma_disconnect(ia->ri_id);
860 if (!rc) {
861 /* returns without wait if not connected */
862 wait_event_interruptible(ep->rep_connect_wait,
863 ep->rep_connected != 1);
864 dprintk("RPC: %s: after wait, %sconnected\n", __func__,
865 (ep->rep_connected == 1) ? "still " : "dis");
866 } else {
867 dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
868 ep->rep_connected = rc;
869 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400870}
871
Chuck Leverf531a5d2015-10-24 17:27:43 -0400872struct rpcrdma_req *
Chuck Lever13924022015-01-21 11:03:52 -0500873rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
874{
Chuck Leverf531a5d2015-10-24 17:27:43 -0400875 struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
Chuck Lever13924022015-01-21 11:03:52 -0500876 struct rpcrdma_req *req;
Chuck Lever13924022015-01-21 11:03:52 -0500877
Chuck Lever85275c82015-01-21 11:04:16 -0500878 req = kzalloc(sizeof(*req), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500879 if (req == NULL)
Chuck Lever85275c82015-01-21 11:04:16 -0500880 return ERR_PTR(-ENOMEM);
Chuck Lever13924022015-01-21 11:03:52 -0500881
Chuck Leverf531a5d2015-10-24 17:27:43 -0400882 INIT_LIST_HEAD(&req->rl_free);
883 spin_lock(&buffer->rb_reqslock);
884 list_add(&req->rl_all, &buffer->rb_allreqs);
885 spin_unlock(&buffer->rb_reqslock);
Chuck Lever13924022015-01-21 11:03:52 -0500886 req->rl_buffer = &r_xprt->rx_buf;
887 return req;
Chuck Lever13924022015-01-21 11:03:52 -0500888}
889
Chuck Leverf531a5d2015-10-24 17:27:43 -0400890struct rpcrdma_rep *
Chuck Lever13924022015-01-21 11:03:52 -0500891rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt)
892{
893 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Lever13924022015-01-21 11:03:52 -0500894 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
895 struct rpcrdma_rep *rep;
896 int rc;
897
898 rc = -ENOMEM;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500899 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500900 if (rep == NULL)
901 goto out;
Chuck Lever13924022015-01-21 11:03:52 -0500902
Chuck Lever6b1184c2015-01-21 11:04:25 -0500903 rep->rr_rdmabuf = rpcrdma_alloc_regbuf(ia, cdata->inline_rsize,
904 GFP_KERNEL);
905 if (IS_ERR(rep->rr_rdmabuf)) {
906 rc = PTR_ERR(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -0500907 goto out_free;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500908 }
Chuck Lever13924022015-01-21 11:03:52 -0500909
Chuck Lever89e0d1122015-05-26 11:51:56 -0400910 rep->rr_device = ia->ri_device;
Chuck Lever552bf222016-03-04 11:28:36 -0500911 rep->rr_cqe.done = rpcrdma_receive_wc;
Chuck Leverfed171b2015-05-26 11:51:37 -0400912 rep->rr_rxprt = r_xprt;
Chuck Leverfe97b472015-10-24 17:27:10 -0400913 INIT_WORK(&rep->rr_work, rpcrdma_receive_worker);
Chuck Lever13924022015-01-21 11:03:52 -0500914 return rep;
915
916out_free:
917 kfree(rep);
918out:
919 return ERR_PTR(rc);
920}
921
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400922int
Chuck Leverac920d02015-01-21 11:03:44 -0500923rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400924{
Chuck Leverac920d02015-01-21 11:03:44 -0500925 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
926 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400927 int i, rc;
928
Chuck Lever1e465fd2015-10-24 17:27:02 -0400929 buf->rb_max_requests = r_xprt->rx_data.max_requests;
Chuck Leverf531a5d2015-10-24 17:27:43 -0400930 buf->rb_bc_srv_max_requests = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400931 spin_lock_init(&buf->rb_lock);
Chuck Lever23826c72016-03-04 11:28:27 -0500932 atomic_set(&buf->rb_credits, 1);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400933
Chuck Lever91e70e72015-03-30 14:34:58 -0400934 rc = ia->ri_ops->ro_init(r_xprt);
935 if (rc)
936 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400937
Chuck Lever1e465fd2015-10-24 17:27:02 -0400938 INIT_LIST_HEAD(&buf->rb_send_bufs);
Chuck Leverf531a5d2015-10-24 17:27:43 -0400939 INIT_LIST_HEAD(&buf->rb_allreqs);
940 spin_lock_init(&buf->rb_reqslock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400941 for (i = 0; i < buf->rb_max_requests; i++) {
942 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400943
Chuck Lever13924022015-01-21 11:03:52 -0500944 req = rpcrdma_create_req(r_xprt);
945 if (IS_ERR(req)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400946 dprintk("RPC: %s: request buffer %d alloc"
947 " failed\n", __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -0500948 rc = PTR_ERR(req);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400949 goto out;
950 }
Chuck Leverf531a5d2015-10-24 17:27:43 -0400951 req->rl_backchannel = false;
Chuck Lever1e465fd2015-10-24 17:27:02 -0400952 list_add(&req->rl_free, &buf->rb_send_bufs);
953 }
954
955 INIT_LIST_HEAD(&buf->rb_recv_bufs);
956 for (i = 0; i < buf->rb_max_requests + 2; i++) {
957 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400958
Chuck Lever13924022015-01-21 11:03:52 -0500959 rep = rpcrdma_create_rep(r_xprt);
960 if (IS_ERR(rep)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400961 dprintk("RPC: %s: reply buffer %d alloc failed\n",
962 __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -0500963 rc = PTR_ERR(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400964 goto out;
965 }
Chuck Lever1e465fd2015-10-24 17:27:02 -0400966 list_add(&rep->rr_list, &buf->rb_recv_bufs);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400967 }
Chuck Lever13924022015-01-21 11:03:52 -0500968
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400969 return 0;
970out:
971 rpcrdma_buffer_destroy(buf);
972 return rc;
973}
974
Chuck Lever1e465fd2015-10-24 17:27:02 -0400975static struct rpcrdma_req *
976rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf)
977{
978 struct rpcrdma_req *req;
979
980 req = list_first_entry(&buf->rb_send_bufs,
981 struct rpcrdma_req, rl_free);
982 list_del(&req->rl_free);
983 return req;
984}
985
986static struct rpcrdma_rep *
987rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf)
988{
989 struct rpcrdma_rep *rep;
990
991 rep = list_first_entry(&buf->rb_recv_bufs,
992 struct rpcrdma_rep, rr_list);
993 list_del(&rep->rr_list);
994 return rep;
995}
996
Chuck Lever2e845222014-07-29 17:25:38 -0400997static void
Chuck Lever13924022015-01-21 11:03:52 -0500998rpcrdma_destroy_rep(struct rpcrdma_ia *ia, struct rpcrdma_rep *rep)
999{
Chuck Lever6b1184c2015-01-21 11:04:25 -05001000 rpcrdma_free_regbuf(ia, rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001001 kfree(rep);
1002}
1003
Chuck Leverf531a5d2015-10-24 17:27:43 -04001004void
Chuck Lever13924022015-01-21 11:03:52 -05001005rpcrdma_destroy_req(struct rpcrdma_ia *ia, struct rpcrdma_req *req)
1006{
Chuck Lever0ca77dc2015-01-21 11:04:08 -05001007 rpcrdma_free_regbuf(ia, req->rl_sendbuf);
Chuck Lever85275c82015-01-21 11:04:16 -05001008 rpcrdma_free_regbuf(ia, req->rl_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001009 kfree(req);
1010}
1011
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001012void
1013rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1014{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001015 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
1016
Chuck Lever1e465fd2015-10-24 17:27:02 -04001017 while (!list_empty(&buf->rb_recv_bufs)) {
1018 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001019
Chuck Lever1e465fd2015-10-24 17:27:02 -04001020 rep = rpcrdma_buffer_get_rep_locked(buf);
1021 rpcrdma_destroy_rep(ia, rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001022 }
1023
Chuck Leverf531a5d2015-10-24 17:27:43 -04001024 spin_lock(&buf->rb_reqslock);
1025 while (!list_empty(&buf->rb_allreqs)) {
Chuck Lever1e465fd2015-10-24 17:27:02 -04001026 struct rpcrdma_req *req;
Allen Andrews4034ba02014-05-28 10:32:09 -04001027
Chuck Leverf531a5d2015-10-24 17:27:43 -04001028 req = list_first_entry(&buf->rb_allreqs,
1029 struct rpcrdma_req, rl_all);
1030 list_del(&req->rl_all);
1031
1032 spin_unlock(&buf->rb_reqslock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001033 rpcrdma_destroy_req(ia, req);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001034 spin_lock(&buf->rb_reqslock);
Chuck Lever9f9d8022014-07-29 17:24:45 -04001035 }
Chuck Leverf531a5d2015-10-24 17:27:43 -04001036 spin_unlock(&buf->rb_reqslock);
Chuck Lever9f9d8022014-07-29 17:24:45 -04001037
1038 ia->ri_ops->ro_destroy(buf);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001039}
1040
Chuck Lever346aa662015-05-26 11:52:06 -04001041struct rpcrdma_mw *
1042rpcrdma_get_mw(struct rpcrdma_xprt *r_xprt)
Chuck Leverc2922c02014-07-29 17:24:36 -04001043{
Chuck Lever346aa662015-05-26 11:52:06 -04001044 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1045 struct rpcrdma_mw *mw = NULL;
Chuck Lever346aa662015-05-26 11:52:06 -04001046
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001047 spin_lock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001048 if (!list_empty(&buf->rb_mws)) {
1049 mw = list_first_entry(&buf->rb_mws,
1050 struct rpcrdma_mw, mw_list);
1051 list_del_init(&mw->mw_list);
Chuck Leverc2922c02014-07-29 17:24:36 -04001052 }
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001053 spin_unlock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001054
1055 if (!mw)
1056 pr_err("RPC: %s: no MWs available\n", __func__);
1057 return mw;
Chuck Leverc2922c02014-07-29 17:24:36 -04001058}
1059
Chuck Lever346aa662015-05-26 11:52:06 -04001060void
1061rpcrdma_put_mw(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mw *mw)
Chuck Leverc2922c02014-07-29 17:24:36 -04001062{
Chuck Lever346aa662015-05-26 11:52:06 -04001063 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
Chuck Leverc2922c02014-07-29 17:24:36 -04001064
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001065 spin_lock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001066 list_add_tail(&mw->mw_list, &buf->rb_mws);
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001067 spin_unlock(&buf->rb_mwlock);
Chuck Leverc2922c02014-07-29 17:24:36 -04001068}
1069
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001070/*
1071 * Get a set of request/reply buffers.
1072 *
Chuck Lever1e465fd2015-10-24 17:27:02 -04001073 * Reply buffer (if available) is attached to send buffer upon return.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001074 */
1075struct rpcrdma_req *
1076rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1077{
1078 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001079
Chuck Levera5b027e2015-10-24 17:27:27 -04001080 spin_lock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001081 if (list_empty(&buffers->rb_send_bufs))
1082 goto out_reqbuf;
1083 req = rpcrdma_buffer_get_req_locked(buffers);
1084 if (list_empty(&buffers->rb_recv_bufs))
1085 goto out_repbuf;
1086 req->rl_reply = rpcrdma_buffer_get_rep_locked(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001087 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001088 return req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001089
Chuck Lever1e465fd2015-10-24 17:27:02 -04001090out_reqbuf:
Chuck Levera5b027e2015-10-24 17:27:27 -04001091 spin_unlock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001092 pr_warn("RPC: %s: out of request buffers\n", __func__);
1093 return NULL;
1094out_repbuf:
Chuck Levera5b027e2015-10-24 17:27:27 -04001095 spin_unlock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001096 pr_warn("RPC: %s: out of reply buffers\n", __func__);
1097 req->rl_reply = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001098 return req;
1099}
1100
1101/*
1102 * Put request/reply buffers back into pool.
1103 * Pre-decrement counter/array index.
1104 */
1105void
1106rpcrdma_buffer_put(struct rpcrdma_req *req)
1107{
1108 struct rpcrdma_buffer *buffers = req->rl_buffer;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001109 struct rpcrdma_rep *rep = req->rl_reply;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001110
Chuck Lever1e465fd2015-10-24 17:27:02 -04001111 req->rl_niovs = 0;
1112 req->rl_reply = NULL;
1113
Chuck Levera5b027e2015-10-24 17:27:27 -04001114 spin_lock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001115 list_add_tail(&req->rl_free, &buffers->rb_send_bufs);
1116 if (rep)
1117 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Levera5b027e2015-10-24 17:27:27 -04001118 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001119}
1120
1121/*
1122 * Recover reply buffers from pool.
Chuck Lever1e465fd2015-10-24 17:27:02 -04001123 * This happens when recovering from disconnect.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001124 */
1125void
1126rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1127{
1128 struct rpcrdma_buffer *buffers = req->rl_buffer;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001129
Chuck Levera5b027e2015-10-24 17:27:27 -04001130 spin_lock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001131 if (!list_empty(&buffers->rb_recv_bufs))
1132 req->rl_reply = rpcrdma_buffer_get_rep_locked(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001133 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001134}
1135
1136/*
1137 * Put reply buffers back into pool when not attached to
1138 * request. This happens in error conditions.
1139 */
1140void
1141rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1142{
Chuck Leverfed171b2015-05-26 11:51:37 -04001143 struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001144
Chuck Levera5b027e2015-10-24 17:27:27 -04001145 spin_lock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001146 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Levera5b027e2015-10-24 17:27:27 -04001147 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001148}
1149
1150/*
1151 * Wrappers for internal-use kmalloc memory registration, used by buffer code.
1152 */
1153
Chuck Leverd6547882015-03-30 14:35:44 -04001154void
1155rpcrdma_mapping_error(struct rpcrdma_mr_seg *seg)
1156{
1157 dprintk("RPC: map_one: offset %p iova %llx len %zu\n",
1158 seg->mr_offset,
1159 (unsigned long long)seg->mr_dma, seg->mr_dmalen);
1160}
1161
Chuck Lever9128c3e2015-01-21 11:04:00 -05001162/**
1163 * rpcrdma_alloc_regbuf - kmalloc and register memory for SEND/RECV buffers
1164 * @ia: controlling rpcrdma_ia
1165 * @size: size of buffer to be allocated, in bytes
1166 * @flags: GFP flags
1167 *
1168 * Returns pointer to private header of an area of internally
1169 * registered memory, or an ERR_PTR. The registered buffer follows
1170 * the end of the private header.
1171 *
1172 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
1173 * receiving the payload of RDMA RECV operations. regbufs are not
1174 * used for RDMA READ/WRITE operations, thus are registered only for
1175 * LOCAL access.
1176 */
1177struct rpcrdma_regbuf *
1178rpcrdma_alloc_regbuf(struct rpcrdma_ia *ia, size_t size, gfp_t flags)
1179{
1180 struct rpcrdma_regbuf *rb;
Chuck Levere531dca2015-08-03 13:03:20 -04001181 struct ib_sge *iov;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001182
Chuck Lever9128c3e2015-01-21 11:04:00 -05001183 rb = kmalloc(sizeof(*rb) + size, flags);
1184 if (rb == NULL)
1185 goto out;
1186
Chuck Levere531dca2015-08-03 13:03:20 -04001187 iov = &rb->rg_iov;
1188 iov->addr = ib_dma_map_single(ia->ri_device,
1189 (void *)rb->rg_base, size,
1190 DMA_BIDIRECTIONAL);
1191 if (ib_dma_mapping_error(ia->ri_device, iov->addr))
Chuck Lever9128c3e2015-01-21 11:04:00 -05001192 goto out_free;
1193
Chuck Levere531dca2015-08-03 13:03:20 -04001194 iov->length = size;
Chuck Leverbb6c96d2015-09-24 10:34:21 +03001195 iov->lkey = ia->ri_pd->local_dma_lkey;
Chuck Levere531dca2015-08-03 13:03:20 -04001196 rb->rg_size = size;
1197 rb->rg_owner = NULL;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001198 return rb;
1199
1200out_free:
1201 kfree(rb);
1202out:
Chuck Levere531dca2015-08-03 13:03:20 -04001203 return ERR_PTR(-ENOMEM);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001204}
1205
1206/**
1207 * rpcrdma_free_regbuf - deregister and free registered buffer
1208 * @ia: controlling rpcrdma_ia
1209 * @rb: regbuf to be deregistered and freed
1210 */
1211void
1212rpcrdma_free_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1213{
Chuck Levere531dca2015-08-03 13:03:20 -04001214 struct ib_sge *iov;
1215
1216 if (!rb)
1217 return;
1218
1219 iov = &rb->rg_iov;
1220 ib_dma_unmap_single(ia->ri_device,
1221 iov->addr, iov->length, DMA_BIDIRECTIONAL);
1222 kfree(rb);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001223}
1224
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001225/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001226 * Prepost any receive buffer, then post send.
1227 *
1228 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1229 */
1230int
1231rpcrdma_ep_post(struct rpcrdma_ia *ia,
1232 struct rpcrdma_ep *ep,
1233 struct rpcrdma_req *req)
1234{
Chuck Leverb3221d62015-08-03 13:03:39 -04001235 struct ib_device *device = ia->ri_device;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001236 struct ib_send_wr send_wr, *send_wr_fail;
1237 struct rpcrdma_rep *rep = req->rl_reply;
Chuck Leverb3221d62015-08-03 13:03:39 -04001238 struct ib_sge *iov = req->rl_send_iov;
1239 int i, rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001240
1241 if (rep) {
1242 rc = rpcrdma_ep_post_recv(ia, ep, rep);
1243 if (rc)
1244 goto out;
1245 req->rl_reply = NULL;
1246 }
1247
1248 send_wr.next = NULL;
Chuck Levere46ac342015-03-30 14:35:35 -04001249 send_wr.wr_id = RPCRDMA_IGNORE_COMPLETION;
Chuck Leverb3221d62015-08-03 13:03:39 -04001250 send_wr.sg_list = iov;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001251 send_wr.num_sge = req->rl_niovs;
1252 send_wr.opcode = IB_WR_SEND;
Chuck Leverb3221d62015-08-03 13:03:39 -04001253
1254 for (i = 0; i < send_wr.num_sge; i++)
1255 ib_dma_sync_single_for_device(device, iov[i].addr,
1256 iov[i].length, DMA_TO_DEVICE);
1257 dprintk("RPC: %s: posting %d s/g entries\n",
1258 __func__, send_wr.num_sge);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001259
1260 if (DECR_CQCOUNT(ep) > 0)
1261 send_wr.send_flags = 0;
1262 else { /* Provider must take a send completion every now and then */
1263 INIT_CQCOUNT(ep);
1264 send_wr.send_flags = IB_SEND_SIGNALED;
1265 }
1266
1267 rc = ib_post_send(ia->ri_id->qp, &send_wr, &send_wr_fail);
1268 if (rc)
1269 dprintk("RPC: %s: ib_post_send returned %i\n", __func__,
1270 rc);
1271out:
1272 return rc;
1273}
1274
1275/*
1276 * (Re)post a receive buffer.
1277 */
1278int
1279rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
1280 struct rpcrdma_ep *ep,
1281 struct rpcrdma_rep *rep)
1282{
1283 struct ib_recv_wr recv_wr, *recv_wr_fail;
1284 int rc;
1285
1286 recv_wr.next = NULL;
Chuck Lever552bf222016-03-04 11:28:36 -05001287 recv_wr.wr_cqe = &rep->rr_cqe;
Chuck Lever6b1184c2015-01-21 11:04:25 -05001288 recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001289 recv_wr.num_sge = 1;
1290
Chuck Lever89e0d1122015-05-26 11:51:56 -04001291 ib_dma_sync_single_for_cpu(ia->ri_device,
Chuck Lever6b1184c2015-01-21 11:04:25 -05001292 rdmab_addr(rep->rr_rdmabuf),
1293 rdmab_length(rep->rr_rdmabuf),
1294 DMA_BIDIRECTIONAL);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001295
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001296 rc = ib_post_recv(ia->ri_id->qp, &recv_wr, &recv_wr_fail);
1297
1298 if (rc)
1299 dprintk("RPC: %s: ib_post_recv returned %i\n", __func__,
1300 rc);
1301 return rc;
1302}
Chuck Lever43e95982014-07-29 17:23:34 -04001303
Chuck Leverf531a5d2015-10-24 17:27:43 -04001304/**
1305 * rpcrdma_ep_post_extra_recv - Post buffers for incoming backchannel requests
1306 * @r_xprt: transport associated with these backchannel resources
1307 * @min_reqs: minimum number of incoming requests expected
1308 *
1309 * Returns zero if all requested buffers were posted, or a negative errno.
1310 */
1311int
1312rpcrdma_ep_post_extra_recv(struct rpcrdma_xprt *r_xprt, unsigned int count)
1313{
1314 struct rpcrdma_buffer *buffers = &r_xprt->rx_buf;
1315 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
1316 struct rpcrdma_ep *ep = &r_xprt->rx_ep;
1317 struct rpcrdma_rep *rep;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001318 int rc;
1319
1320 while (count--) {
Chuck Lever9b066882015-12-16 17:22:06 -05001321 spin_lock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001322 if (list_empty(&buffers->rb_recv_bufs))
1323 goto out_reqbuf;
1324 rep = rpcrdma_buffer_get_rep_locked(buffers);
Chuck Lever9b066882015-12-16 17:22:06 -05001325 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001326
1327 rc = rpcrdma_ep_post_recv(ia, ep, rep);
1328 if (rc)
1329 goto out_rc;
1330 }
1331
1332 return 0;
1333
1334out_reqbuf:
Chuck Lever9b066882015-12-16 17:22:06 -05001335 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001336 pr_warn("%s: no extra receive buffers\n", __func__);
1337 return -ENOMEM;
1338
1339out_rc:
1340 rpcrdma_recv_buffer_put(rep);
1341 return rc;
1342}
1343
Chuck Lever1c9351e2015-03-30 14:34:30 -04001344/* How many chunk list items fit within our inline buffers?
Chuck Lever43e95982014-07-29 17:23:34 -04001345 */
Chuck Lever1c9351e2015-03-30 14:34:30 -04001346unsigned int
1347rpcrdma_max_segments(struct rpcrdma_xprt *r_xprt)
Chuck Lever43e95982014-07-29 17:23:34 -04001348{
1349 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Lever1c9351e2015-03-30 14:34:30 -04001350 int bytes, segments;
Chuck Lever43e95982014-07-29 17:23:34 -04001351
Chuck Lever1c9351e2015-03-30 14:34:30 -04001352 bytes = min_t(unsigned int, cdata->inline_wsize, cdata->inline_rsize);
1353 bytes -= RPCRDMA_HDRLEN_MIN;
1354 if (bytes < sizeof(struct rpcrdma_segment) * 2) {
1355 pr_warn("RPC: %s: inline threshold too small\n",
1356 __func__);
1357 return 0;
Chuck Lever43e95982014-07-29 17:23:34 -04001358 }
Chuck Lever1c9351e2015-03-30 14:34:30 -04001359
1360 segments = 1 << (fls(bytes / sizeof(struct rpcrdma_segment)) - 1);
1361 dprintk("RPC: %s: max chunk list size = %d segments\n",
1362 __func__, segments);
1363 return segments;
Chuck Lever43e95982014-07-29 17:23:34 -04001364}