blob: 61eea73557fff0778a43acc0208f6c9cd57e5feb [file] [log] [blame]
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -04001/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04002 * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the BSD-type
8 * license below:
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
17 * Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials provided
20 * with the distribution.
21 *
22 * Neither the name of the Network Appliance, Inc. nor the names of
23 * its contributors may be used to endorse or promote products
24 * derived from this software without specific prior written
25 * permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -040038 */
39
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040040/*
41 * verbs.c
42 *
43 * Encapsulates the major functions managing:
44 * o adapters
45 * o endpoints
46 * o connections
47 * o buffer memory
48 */
49
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000050#include <linux/interrupt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090051#include <linux/slab.h>
Chuck Levereba8ff62015-01-21 11:03:02 -050052#include <linux/prefetch.h>
Chuck Lever0dd39ca2015-03-30 14:33:43 -040053#include <linux/sunrpc/addr.h>
Chuck Lever65866f82014-05-28 10:33:59 -040054#include <asm/bitops.h>
Devesh Sharmad0f36c42015-08-03 13:05:04 -040055#include <linux/module.h> /* try_module_get()/module_put() */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040056
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -040057#include "xprt_rdma.h"
58
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040059/*
60 * Globals/Macros
61 */
62
Jeff Laytonf895b252014-11-17 16:58:04 -050063#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040064# define RPCDBG_FACILITY RPCDBG_TRANS
65#endif
66
67/*
68 * internal functions
69 */
70
71/*
72 * handle replies in tasklet context, using a single, global list
73 * rdma tasklet function -- just turn around and call the func
74 * for all replies on the list
75 */
76
77static DEFINE_SPINLOCK(rpcrdma_tk_lock_g);
78static LIST_HEAD(rpcrdma_tasklets_g);
79
80static void
81rpcrdma_run_tasklet(unsigned long data)
82{
83 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040084 unsigned long flags;
85
86 data = data;
87 spin_lock_irqsave(&rpcrdma_tk_lock_g, flags);
88 while (!list_empty(&rpcrdma_tasklets_g)) {
89 rep = list_entry(rpcrdma_tasklets_g.next,
90 struct rpcrdma_rep, rr_list);
91 list_del(&rep->rr_list);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040092 spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags);
93
Chuck Lever494ae302015-05-26 11:51:46 -040094 rpcrdma_reply_handler(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -040095
96 spin_lock_irqsave(&rpcrdma_tk_lock_g, flags);
97 }
98 spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags);
99}
100
101static DECLARE_TASKLET(rpcrdma_tasklet_g, rpcrdma_run_tasklet, 0UL);
102
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400103static void
Chuck Leverf1a03b72014-11-08 20:14:37 -0500104rpcrdma_schedule_tasklet(struct list_head *sched_list)
105{
106 unsigned long flags;
107
108 spin_lock_irqsave(&rpcrdma_tk_lock_g, flags);
109 list_splice_tail(sched_list, &rpcrdma_tasklets_g);
110 spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags);
111 tasklet_schedule(&rpcrdma_tasklet_g);
112}
113
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400114static void
115rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context)
116{
117 struct rpcrdma_ep *ep = context;
118
Chuck Lever7ff11de2014-11-08 20:15:01 -0500119 pr_err("RPC: %s: %s on device %s ep %p\n",
Sagi Grimberg76357c72015-05-18 13:40:32 +0300120 __func__, ib_event_msg(event->event),
Chuck Lever7ff11de2014-11-08 20:15:01 -0500121 event->device->name, context);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400122 if (ep->rep_connected == 1) {
123 ep->rep_connected = -EIO;
Chuck Leverafadc462015-01-21 11:03:11 -0500124 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400125 wake_up_all(&ep->rep_connect_wait);
126 }
127}
128
129static void
130rpcrdma_cq_async_error_upcall(struct ib_event *event, void *context)
131{
132 struct rpcrdma_ep *ep = context;
133
Chuck Lever7ff11de2014-11-08 20:15:01 -0500134 pr_err("RPC: %s: %s on device %s ep %p\n",
Sagi Grimberg76357c72015-05-18 13:40:32 +0300135 __func__, ib_event_msg(event->event),
Chuck Lever7ff11de2014-11-08 20:15:01 -0500136 event->device->name, context);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400137 if (ep->rep_connected == 1) {
138 ep->rep_connected = -EIO;
Chuck Leverafadc462015-01-21 11:03:11 -0500139 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400140 wake_up_all(&ep->rep_connect_wait);
141 }
142}
143
Chuck Leverfc664482014-05-28 10:33:25 -0400144static void
145rpcrdma_sendcq_process_wc(struct ib_wc *wc)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400146{
Chuck Lever85024272015-01-21 11:02:04 -0500147 /* WARNING: Only wr_id and status are reliable at this point */
Chuck Levere46ac342015-03-30 14:35:35 -0400148 if (wc->wr_id == RPCRDMA_IGNORE_COMPLETION) {
149 if (wc->status != IB_WC_SUCCESS &&
150 wc->status != IB_WC_WR_FLUSH_ERR)
Chuck Lever85024272015-01-21 11:02:04 -0500151 pr_err("RPC: %s: SEND: %s\n",
Sagi Grimberg76357c72015-05-18 13:40:32 +0300152 __func__, ib_wc_status_msg(wc->status));
Chuck Lever85024272015-01-21 11:02:04 -0500153 } else {
154 struct rpcrdma_mw *r;
155
156 r = (struct rpcrdma_mw *)(unsigned long)wc->wr_id;
Chuck Levere46ac342015-03-30 14:35:35 -0400157 r->mw_sendcompletion(wc);
Chuck Lever85024272015-01-21 11:02:04 -0500158 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400159}
160
Chuck Leverfc664482014-05-28 10:33:25 -0400161static int
Chuck Lever1c00dd02014-05-28 10:33:42 -0400162rpcrdma_sendcq_poll(struct ib_cq *cq, struct rpcrdma_ep *ep)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400163{
Chuck Lever1c00dd02014-05-28 10:33:42 -0400164 struct ib_wc *wcs;
Chuck Lever8301a2c2014-05-28 10:33:51 -0400165 int budget, count, rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400166
Chuck Lever8301a2c2014-05-28 10:33:51 -0400167 budget = RPCRDMA_WC_BUDGET / RPCRDMA_POLLSIZE;
Chuck Lever1c00dd02014-05-28 10:33:42 -0400168 do {
169 wcs = ep->rep_send_wcs;
170
171 rc = ib_poll_cq(cq, RPCRDMA_POLLSIZE, wcs);
172 if (rc <= 0)
173 return rc;
174
175 count = rc;
176 while (count-- > 0)
177 rpcrdma_sendcq_process_wc(wcs++);
Chuck Lever8301a2c2014-05-28 10:33:51 -0400178 } while (rc == RPCRDMA_POLLSIZE && --budget);
Chuck Lever1c00dd02014-05-28 10:33:42 -0400179 return 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400180}
181
Chuck Lever7b3d7702015-10-24 17:26:37 -0400182/* Handle provider send completion upcalls.
Chuck Leverfc664482014-05-28 10:33:25 -0400183 */
184static void
185rpcrdma_sendcq_upcall(struct ib_cq *cq, void *cq_context)
186{
Chuck Lever1c00dd02014-05-28 10:33:42 -0400187 struct rpcrdma_ep *ep = (struct rpcrdma_ep *)cq_context;
Chuck Leverfc664482014-05-28 10:33:25 -0400188
Chuck Lever7b3d7702015-10-24 17:26:37 -0400189 do {
190 rpcrdma_sendcq_poll(cq, ep);
191 } while (ib_req_notify_cq(cq, IB_CQ_NEXT_COMP |
192 IB_CQ_REPORT_MISSED_EVENTS) > 0);
Chuck Leverfc664482014-05-28 10:33:25 -0400193}
194
195static void
Chuck Leverbb961932014-07-29 17:25:46 -0400196rpcrdma_recvcq_process_wc(struct ib_wc *wc, struct list_head *sched_list)
Chuck Leverfc664482014-05-28 10:33:25 -0400197{
198 struct rpcrdma_rep *rep =
199 (struct rpcrdma_rep *)(unsigned long)wc->wr_id;
200
Chuck Lever85024272015-01-21 11:02:04 -0500201 /* WARNING: Only wr_id and status are reliable at this point */
202 if (wc->status != IB_WC_SUCCESS)
203 goto out_fail;
Chuck Leverfc664482014-05-28 10:33:25 -0400204
Chuck Lever85024272015-01-21 11:02:04 -0500205 /* status == SUCCESS means all fields in wc are trustworthy */
Chuck Leverfc664482014-05-28 10:33:25 -0400206 if (wc->opcode != IB_WC_RECV)
207 return;
208
Chuck Lever85024272015-01-21 11:02:04 -0500209 dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n",
210 __func__, rep, wc->byte_len);
211
Chuck Leverfc664482014-05-28 10:33:25 -0400212 rep->rr_len = wc->byte_len;
Chuck Lever89e0d1122015-05-26 11:51:56 -0400213 ib_dma_sync_single_for_cpu(rep->rr_device,
Chuck Lever6b1184c2015-01-21 11:04:25 -0500214 rdmab_addr(rep->rr_rdmabuf),
215 rep->rr_len, DMA_FROM_DEVICE);
216 prefetch(rdmab_to_msg(rep->rr_rdmabuf));
Chuck Leverfc664482014-05-28 10:33:25 -0400217
218out_schedule:
Chuck Leverbb961932014-07-29 17:25:46 -0400219 list_add_tail(&rep->rr_list, sched_list);
Chuck Lever85024272015-01-21 11:02:04 -0500220 return;
221out_fail:
222 if (wc->status != IB_WC_WR_FLUSH_ERR)
223 pr_err("RPC: %s: rep %p: %s\n",
Sagi Grimberg76357c72015-05-18 13:40:32 +0300224 __func__, rep, ib_wc_status_msg(wc->status));
Chuck Lever85024272015-01-21 11:02:04 -0500225 rep->rr_len = ~0U;
226 goto out_schedule;
Chuck Leverfc664482014-05-28 10:33:25 -0400227}
228
229static int
Chuck Lever1c00dd02014-05-28 10:33:42 -0400230rpcrdma_recvcq_poll(struct ib_cq *cq, struct rpcrdma_ep *ep)
Chuck Leverfc664482014-05-28 10:33:25 -0400231{
Chuck Leverbb961932014-07-29 17:25:46 -0400232 struct list_head sched_list;
Chuck Lever1c00dd02014-05-28 10:33:42 -0400233 struct ib_wc *wcs;
Chuck Lever8301a2c2014-05-28 10:33:51 -0400234 int budget, count, rc;
Chuck Leverfc664482014-05-28 10:33:25 -0400235
Chuck Leverbb961932014-07-29 17:25:46 -0400236 INIT_LIST_HEAD(&sched_list);
Chuck Lever8301a2c2014-05-28 10:33:51 -0400237 budget = RPCRDMA_WC_BUDGET / RPCRDMA_POLLSIZE;
Chuck Lever1c00dd02014-05-28 10:33:42 -0400238 do {
239 wcs = ep->rep_recv_wcs;
240
241 rc = ib_poll_cq(cq, RPCRDMA_POLLSIZE, wcs);
242 if (rc <= 0)
Chuck Leverbb961932014-07-29 17:25:46 -0400243 goto out_schedule;
Chuck Lever1c00dd02014-05-28 10:33:42 -0400244
245 count = rc;
246 while (count-- > 0)
Chuck Leverbb961932014-07-29 17:25:46 -0400247 rpcrdma_recvcq_process_wc(wcs++, &sched_list);
Chuck Lever8301a2c2014-05-28 10:33:51 -0400248 } while (rc == RPCRDMA_POLLSIZE && --budget);
Chuck Leverbb961932014-07-29 17:25:46 -0400249 rc = 0;
250
251out_schedule:
Chuck Leverf1a03b72014-11-08 20:14:37 -0500252 rpcrdma_schedule_tasklet(&sched_list);
Chuck Leverbb961932014-07-29 17:25:46 -0400253 return rc;
Chuck Leverfc664482014-05-28 10:33:25 -0400254}
255
Chuck Lever7b3d7702015-10-24 17:26:37 -0400256/* Handle provider receive completion upcalls.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400257 */
258static void
Chuck Leverfc664482014-05-28 10:33:25 -0400259rpcrdma_recvcq_upcall(struct ib_cq *cq, void *cq_context)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400260{
Chuck Lever1c00dd02014-05-28 10:33:42 -0400261 struct rpcrdma_ep *ep = (struct rpcrdma_ep *)cq_context;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400262
Chuck Lever7b3d7702015-10-24 17:26:37 -0400263 do {
264 rpcrdma_recvcq_poll(cq, ep);
265 } while (ib_req_notify_cq(cq, IB_CQ_NEXT_COMP |
266 IB_CQ_REPORT_MISSED_EVENTS) > 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400267}
268
Chuck Levera7bc2112014-07-29 17:23:52 -0400269static void
270rpcrdma_flush_cqs(struct rpcrdma_ep *ep)
271{
Chuck Lever5c166be2014-11-08 20:14:45 -0500272 struct ib_wc wc;
273 LIST_HEAD(sched_list);
274
275 while (ib_poll_cq(ep->rep_attr.recv_cq, 1, &wc) > 0)
276 rpcrdma_recvcq_process_wc(&wc, &sched_list);
277 if (!list_empty(&sched_list))
278 rpcrdma_schedule_tasklet(&sched_list);
279 while (ib_poll_cq(ep->rep_attr.send_cq, 1, &wc) > 0)
280 rpcrdma_sendcq_process_wc(&wc);
Chuck Levera7bc2112014-07-29 17:23:52 -0400281}
282
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400283static int
284rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
285{
286 struct rpcrdma_xprt *xprt = id->context;
287 struct rpcrdma_ia *ia = &xprt->rx_ia;
288 struct rpcrdma_ep *ep = &xprt->rx_ep;
Jeff Laytonf895b252014-11-17 16:58:04 -0500289#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400290 struct sockaddr *sap = (struct sockaddr *)&ep->rep_remote_addr;
Ingo Molnarff0db042008-11-25 16:58:42 -0800291#endif
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500292 struct ib_qp_attr *attr = &ia->ri_qp_attr;
293 struct ib_qp_init_attr *iattr = &ia->ri_qp_init_attr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400294 int connstate = 0;
295
296 switch (event->event) {
297 case RDMA_CM_EVENT_ADDR_RESOLVED:
298 case RDMA_CM_EVENT_ROUTE_RESOLVED:
Tom Talpey5675add2008-10-09 15:01:41 -0400299 ia->ri_async_rc = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400300 complete(&ia->ri_done);
301 break;
302 case RDMA_CM_EVENT_ADDR_ERROR:
303 ia->ri_async_rc = -EHOSTUNREACH;
304 dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
305 __func__, ep);
306 complete(&ia->ri_done);
307 break;
308 case RDMA_CM_EVENT_ROUTE_ERROR:
309 ia->ri_async_rc = -ENETUNREACH;
310 dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
311 __func__, ep);
312 complete(&ia->ri_done);
313 break;
314 case RDMA_CM_EVENT_ESTABLISHED:
315 connstate = 1;
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500316 ib_query_qp(ia->ri_id->qp, attr,
317 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_MAX_DEST_RD_ATOMIC,
318 iattr);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400319 dprintk("RPC: %s: %d responder resources"
320 " (%d initiator)\n",
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500321 __func__, attr->max_dest_rd_atomic,
322 attr->max_rd_atomic);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400323 goto connected;
324 case RDMA_CM_EVENT_CONNECT_ERROR:
325 connstate = -ENOTCONN;
326 goto connected;
327 case RDMA_CM_EVENT_UNREACHABLE:
328 connstate = -ENETDOWN;
329 goto connected;
330 case RDMA_CM_EVENT_REJECTED:
331 connstate = -ECONNREFUSED;
332 goto connected;
333 case RDMA_CM_EVENT_DISCONNECTED:
334 connstate = -ECONNABORTED;
335 goto connected;
336 case RDMA_CM_EVENT_DEVICE_REMOVAL:
337 connstate = -ENODEV;
338connected:
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400339 dprintk("RPC: %s: %sconnected\n",
340 __func__, connstate > 0 ? "" : "dis");
341 ep->rep_connected = connstate;
Chuck Leverafadc462015-01-21 11:03:11 -0500342 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400343 wake_up_all(&ep->rep_connect_wait);
Chuck Lever8079fb72014-07-29 17:26:12 -0400344 /*FALLTHROUGH*/
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400345 default:
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400346 dprintk("RPC: %s: %pIS:%u (ep 0x%p): %s\n",
347 __func__, sap, rpc_get_port(sap), ep,
Sagi Grimberg76357c72015-05-18 13:40:32 +0300348 rdma_event_msg(event->event));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400349 break;
350 }
351
Jeff Laytonf895b252014-11-17 16:58:04 -0500352#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400353 if (connstate == 1) {
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500354 int ird = attr->max_dest_rd_atomic;
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400355 int tird = ep->rep_remote_cma.responder_resources;
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400356
Chuck Levera0ce85f2015-03-30 14:34:21 -0400357 pr_info("rpcrdma: connection to %pIS:%u on %s, memreg '%s', %d credits, %d responders%s\n",
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400358 sap, rpc_get_port(sap),
Chuck Lever89e0d1122015-05-26 11:51:56 -0400359 ia->ri_device->name,
Chuck Levera0ce85f2015-03-30 14:34:21 -0400360 ia->ri_ops->ro_displayname,
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400361 xprt->rx_buf.rb_max_requests,
362 ird, ird < 4 && ird < tird / 2 ? " (low!)" : "");
363 } else if (connstate < 0) {
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400364 pr_info("rpcrdma: connection to %pIS:%u closed (%d)\n",
365 sap, rpc_get_port(sap), connstate);
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400366 }
367#endif
368
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400369 return 0;
370}
371
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400372static void rpcrdma_destroy_id(struct rdma_cm_id *id)
373{
374 if (id) {
375 module_put(id->device->owner);
376 rdma_destroy_id(id);
377 }
378}
379
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400380static struct rdma_cm_id *
381rpcrdma_create_id(struct rpcrdma_xprt *xprt,
382 struct rpcrdma_ia *ia, struct sockaddr *addr)
383{
384 struct rdma_cm_id *id;
385 int rc;
386
Tom Talpey1a954052008-10-09 15:01:31 -0400387 init_completion(&ia->ri_done);
388
Sean Heftyb26f9b92010-04-01 17:08:41 +0000389 id = rdma_create_id(rpcrdma_conn_upcall, xprt, RDMA_PS_TCP, IB_QPT_RC);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400390 if (IS_ERR(id)) {
391 rc = PTR_ERR(id);
392 dprintk("RPC: %s: rdma_create_id() failed %i\n",
393 __func__, rc);
394 return id;
395 }
396
Tom Talpey5675add2008-10-09 15:01:41 -0400397 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400398 rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
399 if (rc) {
400 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
401 __func__, rc);
402 goto out;
403 }
Tom Talpey5675add2008-10-09 15:01:41 -0400404 wait_for_completion_interruptible_timeout(&ia->ri_done,
405 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400406
407 /* FIXME:
408 * Until xprtrdma supports DEVICE_REMOVAL, the provider must
409 * be pinned while there are active NFS/RDMA mounts to prevent
410 * hangs and crashes at umount time.
411 */
412 if (!ia->ri_async_rc && !try_module_get(id->device->owner)) {
413 dprintk("RPC: %s: Failed to get device module\n",
414 __func__);
415 ia->ri_async_rc = -ENODEV;
416 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400417 rc = ia->ri_async_rc;
418 if (rc)
419 goto out;
420
Tom Talpey5675add2008-10-09 15:01:41 -0400421 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400422 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
423 if (rc) {
424 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
425 __func__, rc);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400426 goto put;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400427 }
Tom Talpey5675add2008-10-09 15:01:41 -0400428 wait_for_completion_interruptible_timeout(&ia->ri_done,
429 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400430 rc = ia->ri_async_rc;
431 if (rc)
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400432 goto put;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400433
434 return id;
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400435put:
436 module_put(id->device->owner);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400437out:
438 rdma_destroy_id(id);
439 return ERR_PTR(rc);
440}
441
442/*
443 * Drain any cq, prior to teardown.
444 */
445static void
446rpcrdma_clean_cq(struct ib_cq *cq)
447{
448 struct ib_wc wc;
449 int count = 0;
450
451 while (1 == ib_poll_cq(cq, 1, &wc))
452 ++count;
453
454 if (count)
455 dprintk("RPC: %s: flushed %d events (last 0x%x)\n",
456 __func__, count, wc.opcode);
457}
458
459/*
460 * Exported functions.
461 */
462
463/*
464 * Open and initialize an Interface Adapter.
465 * o initializes fields of struct rpcrdma_ia, including
466 * interface and provider attributes and protection zone.
467 */
468int
469rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr, int memreg)
470{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400471 struct rpcrdma_ia *ia = &xprt->rx_ia;
Chuck Lever7bc79722015-01-21 11:03:27 -0500472 struct ib_device_attr *devattr = &ia->ri_devattr;
Chuck Leverd1ed8572015-08-03 13:03:30 -0400473 int rc;
474
475 ia->ri_dma_mr = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400476
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400477 ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
478 if (IS_ERR(ia->ri_id)) {
479 rc = PTR_ERR(ia->ri_id);
480 goto out1;
481 }
Chuck Lever89e0d1122015-05-26 11:51:56 -0400482 ia->ri_device = ia->ri_id->device;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400483
Chuck Lever89e0d1122015-05-26 11:51:56 -0400484 ia->ri_pd = ib_alloc_pd(ia->ri_device);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400485 if (IS_ERR(ia->ri_pd)) {
486 rc = PTR_ERR(ia->ri_pd);
487 dprintk("RPC: %s: ib_alloc_pd() failed %i\n",
488 __func__, rc);
489 goto out2;
490 }
491
Chuck Lever89e0d1122015-05-26 11:51:56 -0400492 rc = ib_query_device(ia->ri_device, devattr);
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400493 if (rc) {
494 dprintk("RPC: %s: ib_query_device failed %d\n",
495 __func__, rc);
Chuck Lever5ae711a2015-01-21 11:03:19 -0500496 goto out3;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400497 }
498
Chuck Leverf10eafd2014-05-28 10:32:51 -0400499 if (memreg == RPCRDMA_FRMR) {
Sagi Grimbergf022fa82015-10-06 19:52:37 +0300500 if (!(devattr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) ||
501 (devattr->max_fast_reg_page_list_len == 0)) {
Tom Talpey3197d3092008-10-09 15:00:20 -0400502 dprintk("RPC: %s: FRMR registration "
Chuck Leverf10eafd2014-05-28 10:32:51 -0400503 "not supported by HCA\n", __func__);
504 memreg = RPCRDMA_MTHCAFMR;
Tom Talpey3197d3092008-10-09 15:00:20 -0400505 }
Chuck Leverf10eafd2014-05-28 10:32:51 -0400506 }
507 if (memreg == RPCRDMA_MTHCAFMR) {
Chuck Lever89e0d1122015-05-26 11:51:56 -0400508 if (!ia->ri_device->alloc_fmr) {
Chuck Leverf10eafd2014-05-28 10:32:51 -0400509 dprintk("RPC: %s: MTHCAFMR registration "
510 "not supported by HCA\n", __func__);
Sagi Grimbergf022fa82015-10-06 19:52:37 +0300511 rc = -EINVAL;
Chuck Leverd2310932015-08-03 13:03:09 -0400512 goto out3;
Chuck Leverf10eafd2014-05-28 10:32:51 -0400513 }
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400514 }
515
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400516 switch (memreg) {
Tom Talpey3197d3092008-10-09 15:00:20 -0400517 case RPCRDMA_FRMR:
Chuck Levera0ce85f2015-03-30 14:34:21 -0400518 ia->ri_ops = &rpcrdma_frwr_memreg_ops;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400519 break;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400520 case RPCRDMA_ALLPHYSICAL:
Chuck Levera0ce85f2015-03-30 14:34:21 -0400521 ia->ri_ops = &rpcrdma_physical_memreg_ops;
Chuck Leverd1ed8572015-08-03 13:03:30 -0400522 break;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400523 case RPCRDMA_MTHCAFMR:
Chuck Levera0ce85f2015-03-30 14:34:21 -0400524 ia->ri_ops = &rpcrdma_fmr_memreg_ops;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400525 break;
526 default:
Chuck Levercdd9ade2014-05-28 10:33:00 -0400527 printk(KERN_ERR "RPC: Unsupported memory "
528 "registration mode: %d\n", memreg);
529 rc = -ENOMEM;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500530 goto out3;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400531 }
Chuck Levera0ce85f2015-03-30 14:34:21 -0400532 dprintk("RPC: %s: memory registration strategy is '%s'\n",
533 __func__, ia->ri_ops->ro_displayname);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400534
Chuck Lever73806c82014-07-29 17:23:25 -0400535 rwlock_init(&ia->ri_qplock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400536 return 0;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500537
538out3:
539 ib_dealloc_pd(ia->ri_pd);
540 ia->ri_pd = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400541out2:
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;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400544out1:
545 return rc;
546}
547
548/*
549 * Clean up/close an IA.
550 * o if event handles and PD have been initialized, free them.
551 * o close the IA
552 */
553void
554rpcrdma_ia_close(struct rpcrdma_ia *ia)
555{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400556 dprintk("RPC: %s: entering\n", __func__);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400557 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
558 if (ia->ri_id->qp)
559 rdma_destroy_qp(ia->ri_id);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400560 rpcrdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400561 ia->ri_id = NULL;
562 }
Chuck Lever6d446982015-05-26 11:51:27 -0400563
564 /* If the pd is still busy, xprtrdma missed freeing a resource */
565 if (ia->ri_pd && !IS_ERR(ia->ri_pd))
Jason Gunthorpe7dd78642015-08-05 14:34:31 -0600566 ib_dealloc_pd(ia->ri_pd);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400567}
568
569/*
570 * Create unconnected endpoint.
571 */
572int
573rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
574 struct rpcrdma_create_data_internal *cdata)
575{
Chuck Lever7bc79722015-01-21 11:03:27 -0500576 struct ib_device_attr *devattr = &ia->ri_devattr;
Chuck Leverfc664482014-05-28 10:33:25 -0400577 struct ib_cq *sendcq, *recvcq;
Matan Barak8e372102015-06-11 16:35:21 +0300578 struct ib_cq_init_attr cq_attr = {};
Chuck Lever5d40a8a2007-10-26 13:30:54 -0400579 int rc, err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400580
Chuck Leverb3221d62015-08-03 13:03:39 -0400581 if (devattr->max_sge < RPCRDMA_MAX_IOVS) {
582 dprintk("RPC: %s: insufficient sge's available\n",
583 __func__);
584 return -ENOMEM;
585 }
586
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400587 /* check provider's send/recv wr limits */
Chuck Lever7bc79722015-01-21 11:03:27 -0500588 if (cdata->max_requests > devattr->max_qp_wr)
589 cdata->max_requests = devattr->max_qp_wr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400590
591 ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
592 ep->rep_attr.qp_context = ep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400593 ep->rep_attr.srq = NULL;
594 ep->rep_attr.cap.max_send_wr = cdata->max_requests;
Chuck Lever3968cb52015-03-30 14:35:26 -0400595 rc = ia->ri_ops->ro_open(ia, ep, cdata);
596 if (rc)
597 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400598 ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
Chuck Leverb3221d62015-08-03 13:03:39 -0400599 ep->rep_attr.cap.max_send_sge = RPCRDMA_MAX_IOVS;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400600 ep->rep_attr.cap.max_recv_sge = 1;
601 ep->rep_attr.cap.max_inline_data = 0;
602 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
603 ep->rep_attr.qp_type = IB_QPT_RC;
604 ep->rep_attr.port_num = ~0;
605
606 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
607 "iovs: send %d recv %d\n",
608 __func__,
609 ep->rep_attr.cap.max_send_wr,
610 ep->rep_attr.cap.max_recv_wr,
611 ep->rep_attr.cap.max_send_sge,
612 ep->rep_attr.cap.max_recv_sge);
613
614 /* set trigger for requesting send completion */
Chuck Leverfc664482014-05-28 10:33:25 -0400615 ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
Chuck Levere7104a22014-11-08 20:14:20 -0500616 if (ep->rep_cqinit > RPCRDMA_MAX_UNSIGNALED_SENDS)
617 ep->rep_cqinit = RPCRDMA_MAX_UNSIGNALED_SENDS;
618 else if (ep->rep_cqinit <= 2)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400619 ep->rep_cqinit = 0;
620 INIT_CQCOUNT(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400621 init_waitqueue_head(&ep->rep_connect_wait);
Chuck Lever254f91e2014-05-28 10:32:17 -0400622 INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400623
Matan Barak8e372102015-06-11 16:35:21 +0300624 cq_attr.cqe = ep->rep_attr.cap.max_send_wr + 1;
Chuck Lever89e0d1122015-05-26 11:51:56 -0400625 sendcq = ib_create_cq(ia->ri_device, rpcrdma_sendcq_upcall,
Linus Torvalds8688d952015-07-02 11:32:23 -0700626 rpcrdma_cq_async_error_upcall, ep, &cq_attr);
Chuck Leverfc664482014-05-28 10:33:25 -0400627 if (IS_ERR(sendcq)) {
628 rc = PTR_ERR(sendcq);
629 dprintk("RPC: %s: failed to create send CQ: %i\n",
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400630 __func__, rc);
631 goto out1;
632 }
633
Chuck Leverfc664482014-05-28 10:33:25 -0400634 rc = ib_req_notify_cq(sendcq, IB_CQ_NEXT_COMP);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400635 if (rc) {
636 dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
637 __func__, rc);
638 goto out2;
639 }
640
Matan Barak8e372102015-06-11 16:35:21 +0300641 cq_attr.cqe = ep->rep_attr.cap.max_recv_wr + 1;
Chuck Lever89e0d1122015-05-26 11:51:56 -0400642 recvcq = ib_create_cq(ia->ri_device, rpcrdma_recvcq_upcall,
Linus Torvalds8688d952015-07-02 11:32:23 -0700643 rpcrdma_cq_async_error_upcall, ep, &cq_attr);
Chuck Leverfc664482014-05-28 10:33:25 -0400644 if (IS_ERR(recvcq)) {
645 rc = PTR_ERR(recvcq);
646 dprintk("RPC: %s: failed to create recv CQ: %i\n",
647 __func__, rc);
648 goto out2;
649 }
650
651 rc = ib_req_notify_cq(recvcq, IB_CQ_NEXT_COMP);
652 if (rc) {
653 dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
654 __func__, rc);
655 ib_destroy_cq(recvcq);
656 goto out2;
657 }
658
659 ep->rep_attr.send_cq = sendcq;
660 ep->rep_attr.recv_cq = recvcq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400661
662 /* Initialize cma parameters */
663
664 /* RPC/RDMA does not use private data */
665 ep->rep_remote_cma.private_data = NULL;
666 ep->rep_remote_cma.private_data_len = 0;
667
668 /* Client offers RDMA Read but does not initiate */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400669 ep->rep_remote_cma.initiator_depth = 0;
Chuck Lever7bc79722015-01-21 11:03:27 -0500670 if (devattr->max_qp_rd_atom > 32) /* arbitrary but <= 255 */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400671 ep->rep_remote_cma.responder_resources = 32;
672 else
Chuck Lever7bc79722015-01-21 11:03:27 -0500673 ep->rep_remote_cma.responder_resources =
674 devattr->max_qp_rd_atom;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400675
676 ep->rep_remote_cma.retry_count = 7;
677 ep->rep_remote_cma.flow_control = 0;
678 ep->rep_remote_cma.rnr_retry_count = 0;
679
680 return 0;
681
682out2:
Chuck Leverfc664482014-05-28 10:33:25 -0400683 err = ib_destroy_cq(sendcq);
Chuck Lever5d40a8a2007-10-26 13:30:54 -0400684 if (err)
685 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
686 __func__, err);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400687out1:
Chuck Leverd1ed8572015-08-03 13:03:30 -0400688 if (ia->ri_dma_mr)
689 ib_dereg_mr(ia->ri_dma_mr);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400690 return rc;
691}
692
693/*
694 * rpcrdma_ep_destroy
695 *
696 * Disconnect and destroy endpoint. After this, the only
697 * valid operations on the ep are to free it (if dynamically
698 * allocated) or re-create it.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400699 */
Chuck Lever7f1d5412014-05-28 10:33:16 -0400700void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400701rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
702{
703 int rc;
704
705 dprintk("RPC: %s: entering, connected is %d\n",
706 __func__, ep->rep_connected);
707
Chuck Lever254f91e2014-05-28 10:32:17 -0400708 cancel_delayed_work_sync(&ep->rep_connect_worker);
709
Steve Wise72c02172015-09-21 12:24:23 -0500710 if (ia->ri_id->qp)
Chuck Lever282191c2014-07-29 17:25:55 -0400711 rpcrdma_ep_disconnect(ep, ia);
Steve Wise72c02172015-09-21 12:24:23 -0500712
713 rpcrdma_clean_cq(ep->rep_attr.recv_cq);
714 rpcrdma_clean_cq(ep->rep_attr.send_cq);
715
716 if (ia->ri_id->qp) {
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400717 rdma_destroy_qp(ia->ri_id);
718 ia->ri_id->qp = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400719 }
720
Chuck Leverfc664482014-05-28 10:33:25 -0400721 rc = ib_destroy_cq(ep->rep_attr.recv_cq);
722 if (rc)
723 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
724 __func__, rc);
725
Chuck Leverfc664482014-05-28 10:33:25 -0400726 rc = ib_destroy_cq(ep->rep_attr.send_cq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400727 if (rc)
728 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
729 __func__, rc);
Chuck Leverd1ed8572015-08-03 13:03:30 -0400730
731 if (ia->ri_dma_mr) {
732 rc = ib_dereg_mr(ia->ri_dma_mr);
733 dprintk("RPC: %s: ib_dereg_mr returned %i\n",
734 __func__, rc);
735 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400736}
737
738/*
739 * Connect unconnected endpoint.
740 */
741int
742rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
743{
Chuck Lever73806c82014-07-29 17:23:25 -0400744 struct rdma_cm_id *id, *old;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400745 int rc = 0;
746 int retry_count = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400747
Tom Talpeyc0555512008-10-10 11:32:45 -0400748 if (ep->rep_connected != 0) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400749 struct rpcrdma_xprt *xprt;
750retry:
Chuck Leverec62f402014-05-28 10:34:07 -0400751 dprintk("RPC: %s: reconnecting...\n", __func__);
Chuck Lever282191c2014-07-29 17:25:55 -0400752
753 rpcrdma_ep_disconnect(ep, ia);
Chuck Levera7bc2112014-07-29 17:23:52 -0400754 rpcrdma_flush_cqs(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400755
756 xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
757 id = rpcrdma_create_id(xprt, ia,
758 (struct sockaddr *)&xprt->rx_data.addr);
759 if (IS_ERR(id)) {
Chuck Leverec62f402014-05-28 10:34:07 -0400760 rc = -EHOSTUNREACH;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400761 goto out;
762 }
763 /* TEMP TEMP TEMP - fail if new device:
764 * Deregister/remarshal *all* requests!
765 * Close and recreate adapter, pd, etc!
766 * Re-determine all attributes still sane!
767 * More stuff I haven't thought of!
768 * Rrrgh!
769 */
Chuck Lever89e0d1122015-05-26 11:51:56 -0400770 if (ia->ri_device != id->device) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400771 printk("RPC: %s: can't reconnect on "
772 "different device!\n", __func__);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400773 rpcrdma_destroy_id(id);
Chuck Leverec62f402014-05-28 10:34:07 -0400774 rc = -ENETUNREACH;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400775 goto out;
776 }
777 /* END TEMP */
Chuck Leverec62f402014-05-28 10:34:07 -0400778 rc = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
779 if (rc) {
780 dprintk("RPC: %s: rdma_create_qp failed %i\n",
781 __func__, rc);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400782 rpcrdma_destroy_id(id);
Chuck Leverec62f402014-05-28 10:34:07 -0400783 rc = -ENETUNREACH;
784 goto out;
785 }
Chuck Lever73806c82014-07-29 17:23:25 -0400786
787 write_lock(&ia->ri_qplock);
788 old = ia->ri_id;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400789 ia->ri_id = id;
Chuck Lever73806c82014-07-29 17:23:25 -0400790 write_unlock(&ia->ri_qplock);
791
792 rdma_destroy_qp(old);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400793 rpcrdma_destroy_id(old);
Chuck Leverec62f402014-05-28 10:34:07 -0400794 } else {
795 dprintk("RPC: %s: connecting...\n", __func__);
796 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
797 if (rc) {
798 dprintk("RPC: %s: rdma_create_qp failed %i\n",
799 __func__, rc);
800 /* do not update ep->rep_connected */
801 return -ENETUNREACH;
802 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400803 }
804
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400805 ep->rep_connected = 0;
806
807 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
808 if (rc) {
809 dprintk("RPC: %s: rdma_connect() failed with %i\n",
810 __func__, rc);
811 goto out;
812 }
813
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400814 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
815
816 /*
817 * Check state. A non-peer reject indicates no listener
818 * (ECONNREFUSED), which may be a transient state. All
819 * others indicate a transport condition which has already
820 * undergone a best-effort.
821 */
Joe Perchesf64f9e72009-11-29 16:55:45 -0800822 if (ep->rep_connected == -ECONNREFUSED &&
823 ++retry_count <= RDMA_CONNECT_RETRY_MAX) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400824 dprintk("RPC: %s: non-peer_reject, retry\n", __func__);
825 goto retry;
826 }
827 if (ep->rep_connected <= 0) {
828 /* Sometimes, the only way to reliably connect to remote
829 * CMs is to use same nonzero values for ORD and IRD. */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400830 if (retry_count++ <= RDMA_CONNECT_RETRY_MAX + 1 &&
831 (ep->rep_remote_cma.responder_resources == 0 ||
832 ep->rep_remote_cma.initiator_depth !=
833 ep->rep_remote_cma.responder_resources)) {
834 if (ep->rep_remote_cma.responder_resources == 0)
835 ep->rep_remote_cma.responder_resources = 1;
836 ep->rep_remote_cma.initiator_depth =
837 ep->rep_remote_cma.responder_resources;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400838 goto retry;
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400839 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400840 rc = ep->rep_connected;
841 } else {
842 dprintk("RPC: %s: connected\n", __func__);
843 }
844
845out:
846 if (rc)
847 ep->rep_connected = rc;
848 return rc;
849}
850
851/*
852 * rpcrdma_ep_disconnect
853 *
854 * This is separate from destroy to facilitate the ability
855 * to reconnect without recreating the endpoint.
856 *
857 * This call is not reentrant, and must not be made in parallel
858 * on the same endpoint.
859 */
Chuck Lever282191c2014-07-29 17:25:55 -0400860void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400861rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
862{
863 int rc;
864
Chuck Levera7bc2112014-07-29 17:23:52 -0400865 rpcrdma_flush_cqs(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400866 rc = rdma_disconnect(ia->ri_id);
867 if (!rc) {
868 /* returns without wait if not connected */
869 wait_event_interruptible(ep->rep_connect_wait,
870 ep->rep_connected != 1);
871 dprintk("RPC: %s: after wait, %sconnected\n", __func__,
872 (ep->rep_connected == 1) ? "still " : "dis");
873 } else {
874 dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
875 ep->rep_connected = rc;
876 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400877}
878
Chuck Lever13924022015-01-21 11:03:52 -0500879static struct rpcrdma_req *
880rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
881{
Chuck Lever13924022015-01-21 11:03:52 -0500882 struct rpcrdma_req *req;
Chuck Lever13924022015-01-21 11:03:52 -0500883
Chuck Lever85275c82015-01-21 11:04:16 -0500884 req = kzalloc(sizeof(*req), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500885 if (req == NULL)
Chuck Lever85275c82015-01-21 11:04:16 -0500886 return ERR_PTR(-ENOMEM);
Chuck Lever13924022015-01-21 11:03:52 -0500887
Chuck Lever13924022015-01-21 11:03:52 -0500888 req->rl_buffer = &r_xprt->rx_buf;
889 return req;
Chuck Lever13924022015-01-21 11:03:52 -0500890}
891
892static struct rpcrdma_rep *
893rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt)
894{
895 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Lever13924022015-01-21 11:03:52 -0500896 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
897 struct rpcrdma_rep *rep;
898 int rc;
899
900 rc = -ENOMEM;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500901 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500902 if (rep == NULL)
903 goto out;
Chuck Lever13924022015-01-21 11:03:52 -0500904
Chuck Lever6b1184c2015-01-21 11:04:25 -0500905 rep->rr_rdmabuf = rpcrdma_alloc_regbuf(ia, cdata->inline_rsize,
906 GFP_KERNEL);
907 if (IS_ERR(rep->rr_rdmabuf)) {
908 rc = PTR_ERR(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -0500909 goto out_free;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500910 }
Chuck Lever13924022015-01-21 11:03:52 -0500911
Chuck Lever89e0d1122015-05-26 11:51:56 -0400912 rep->rr_device = ia->ri_device;
Chuck Leverfed171b2015-05-26 11:51:37 -0400913 rep->rr_rxprt = r_xprt;
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;
927 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400928 char *p;
Chuck Lever13924022015-01-21 11:03:52 -0500929 size_t len;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400930 int i, rc;
931
932 buf->rb_max_requests = cdata->max_requests;
933 spin_lock_init(&buf->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400934
935 /* Need to allocate:
936 * 1. arrays for send and recv pointers
937 * 2. arrays of struct rpcrdma_req to fill in pointers
938 * 3. array of struct rpcrdma_rep for replies
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400939 * Send/recv buffers in req/rep need to be registered
940 */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400941 len = buf->rb_max_requests *
942 (sizeof(struct rpcrdma_req *) + sizeof(struct rpcrdma_rep *));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400943
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400944 p = kzalloc(len, GFP_KERNEL);
945 if (p == NULL) {
946 dprintk("RPC: %s: req_t/rep_t/pad kzalloc(%zd) failed\n",
947 __func__, len);
948 rc = -ENOMEM;
949 goto out;
950 }
951 buf->rb_pool = p; /* for freeing it later */
952
953 buf->rb_send_bufs = (struct rpcrdma_req **) p;
954 p = (char *) &buf->rb_send_bufs[buf->rb_max_requests];
955 buf->rb_recv_bufs = (struct rpcrdma_rep **) p;
956 p = (char *) &buf->rb_recv_bufs[buf->rb_max_requests];
957
Chuck Lever91e70e72015-03-30 14:34:58 -0400958 rc = ia->ri_ops->ro_init(r_xprt);
959 if (rc)
960 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400961
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400962 for (i = 0; i < buf->rb_max_requests; i++) {
963 struct rpcrdma_req *req;
964 struct rpcrdma_rep *rep;
965
Chuck Lever13924022015-01-21 11:03:52 -0500966 req = rpcrdma_create_req(r_xprt);
967 if (IS_ERR(req)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400968 dprintk("RPC: %s: request buffer %d alloc"
969 " failed\n", __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -0500970 rc = PTR_ERR(req);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400971 goto out;
972 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400973 buf->rb_send_bufs[i] = req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400974
Chuck Lever13924022015-01-21 11:03:52 -0500975 rep = rpcrdma_create_rep(r_xprt);
976 if (IS_ERR(rep)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400977 dprintk("RPC: %s: reply buffer %d alloc failed\n",
978 __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -0500979 rc = PTR_ERR(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400980 goto out;
981 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400982 buf->rb_recv_bufs[i] = rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400983 }
Chuck Lever13924022015-01-21 11:03:52 -0500984
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400985 return 0;
986out:
987 rpcrdma_buffer_destroy(buf);
988 return rc;
989}
990
Chuck Lever2e845222014-07-29 17:25:38 -0400991static void
Chuck Lever13924022015-01-21 11:03:52 -0500992rpcrdma_destroy_rep(struct rpcrdma_ia *ia, struct rpcrdma_rep *rep)
993{
994 if (!rep)
995 return;
996
Chuck Lever6b1184c2015-01-21 11:04:25 -0500997 rpcrdma_free_regbuf(ia, rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -0500998 kfree(rep);
999}
1000
1001static void
1002rpcrdma_destroy_req(struct rpcrdma_ia *ia, struct rpcrdma_req *req)
1003{
1004 if (!req)
1005 return;
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);
Chuck Lever2e845222014-07-29 17:25:38 -04001016 int i;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001017
1018 /* clean up in reverse order from create
1019 * 1. recv mr memory (mr free, then kfree)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001020 * 2. send mr memory (mr free, then kfree)
Chuck Lever2e845222014-07-29 17:25:38 -04001021 * 3. MWs
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001022 */
1023 dprintk("RPC: %s: entering\n", __func__);
1024
1025 for (i = 0; i < buf->rb_max_requests; i++) {
Chuck Lever13924022015-01-21 11:03:52 -05001026 if (buf->rb_recv_bufs)
1027 rpcrdma_destroy_rep(ia, buf->rb_recv_bufs[i]);
1028 if (buf->rb_send_bufs)
1029 rpcrdma_destroy_req(ia, buf->rb_send_bufs[i]);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001030 }
1031
Chuck Lever4561f342015-03-30 14:35:17 -04001032 ia->ri_ops->ro_destroy(buf);
Allen Andrews4034ba02014-05-28 10:32:09 -04001033
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001034 kfree(buf->rb_pool);
1035}
1036
Chuck Lever346aa662015-05-26 11:52:06 -04001037struct rpcrdma_mw *
1038rpcrdma_get_mw(struct rpcrdma_xprt *r_xprt)
Chuck Leverc2922c02014-07-29 17:24:36 -04001039{
Chuck Lever346aa662015-05-26 11:52:06 -04001040 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1041 struct rpcrdma_mw *mw = NULL;
Chuck Lever346aa662015-05-26 11:52:06 -04001042
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001043 spin_lock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001044 if (!list_empty(&buf->rb_mws)) {
1045 mw = list_first_entry(&buf->rb_mws,
1046 struct rpcrdma_mw, mw_list);
1047 list_del_init(&mw->mw_list);
Chuck Leverc2922c02014-07-29 17:24:36 -04001048 }
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001049 spin_unlock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001050
1051 if (!mw)
1052 pr_err("RPC: %s: no MWs available\n", __func__);
1053 return mw;
Chuck Leverc2922c02014-07-29 17:24:36 -04001054}
1055
Chuck Lever346aa662015-05-26 11:52:06 -04001056void
1057rpcrdma_put_mw(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mw *mw)
Chuck Leverc2922c02014-07-29 17:24:36 -04001058{
Chuck Lever346aa662015-05-26 11:52:06 -04001059 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
Chuck Leverc2922c02014-07-29 17:24:36 -04001060
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001061 spin_lock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001062 list_add_tail(&mw->mw_list, &buf->rb_mws);
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001063 spin_unlock(&buf->rb_mwlock);
Chuck Leverc2922c02014-07-29 17:24:36 -04001064}
1065
1066static void
1067rpcrdma_buffer_put_sendbuf(struct rpcrdma_req *req, struct rpcrdma_buffer *buf)
1068{
1069 buf->rb_send_bufs[--buf->rb_send_index] = req;
1070 req->rl_niovs = 0;
1071 if (req->rl_reply) {
1072 buf->rb_recv_bufs[--buf->rb_recv_index] = req->rl_reply;
Chuck Leverc2922c02014-07-29 17:24:36 -04001073 req->rl_reply = NULL;
1074 }
1075}
1076
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001077/*
1078 * Get a set of request/reply buffers.
1079 *
1080 * Reply buffer (if needed) is attached to send buffer upon return.
1081 * Rule:
1082 * rb_send_index and rb_recv_index MUST always be pointing to the
1083 * *next* available buffer (non-NULL). They are incremented after
1084 * removing buffers, and decremented *before* returning them.
1085 */
1086struct rpcrdma_req *
1087rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1088{
1089 struct rpcrdma_req *req;
1090 unsigned long flags;
1091
1092 spin_lock_irqsave(&buffers->rb_lock, flags);
Chuck Leverc14d86e2015-05-26 11:52:35 -04001093
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001094 if (buffers->rb_send_index == buffers->rb_max_requests) {
1095 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1096 dprintk("RPC: %s: out of request buffers\n", __func__);
1097 return ((struct rpcrdma_req *)NULL);
1098 }
1099
1100 req = buffers->rb_send_bufs[buffers->rb_send_index];
1101 if (buffers->rb_send_index < buffers->rb_recv_index) {
1102 dprintk("RPC: %s: %d extra receives outstanding (ok)\n",
1103 __func__,
1104 buffers->rb_recv_index - buffers->rb_send_index);
1105 req->rl_reply = NULL;
1106 } else {
1107 req->rl_reply = buffers->rb_recv_bufs[buffers->rb_recv_index];
1108 buffers->rb_recv_bufs[buffers->rb_recv_index++] = NULL;
1109 }
1110 buffers->rb_send_bufs[buffers->rb_send_index++] = NULL;
Chuck Leverddb6beb2014-07-29 17:24:54 -04001111
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001112 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1113 return req;
1114}
1115
1116/*
1117 * Put request/reply buffers back into pool.
1118 * Pre-decrement counter/array index.
1119 */
1120void
1121rpcrdma_buffer_put(struct rpcrdma_req *req)
1122{
1123 struct rpcrdma_buffer *buffers = req->rl_buffer;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001124 unsigned long flags;
1125
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001126 spin_lock_irqsave(&buffers->rb_lock, flags);
Chuck Leverc2922c02014-07-29 17:24:36 -04001127 rpcrdma_buffer_put_sendbuf(req, buffers);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001128 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1129}
1130
1131/*
1132 * Recover reply buffers from pool.
1133 * This happens when recovering from error conditions.
1134 * Post-increment counter/array index.
1135 */
1136void
1137rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1138{
1139 struct rpcrdma_buffer *buffers = req->rl_buffer;
1140 unsigned long flags;
1141
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001142 spin_lock_irqsave(&buffers->rb_lock, flags);
1143 if (buffers->rb_recv_index < buffers->rb_max_requests) {
1144 req->rl_reply = buffers->rb_recv_bufs[buffers->rb_recv_index];
1145 buffers->rb_recv_bufs[buffers->rb_recv_index++] = NULL;
1146 }
1147 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1148}
1149
1150/*
1151 * Put reply buffers back into pool when not attached to
Chuck Leverb45ccfd2014-05-28 10:32:34 -04001152 * request. This happens in error conditions.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001153 */
1154void
1155rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1156{
Chuck Leverfed171b2015-05-26 11:51:37 -04001157 struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001158 unsigned long flags;
1159
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001160 spin_lock_irqsave(&buffers->rb_lock, flags);
1161 buffers->rb_recv_bufs[--buffers->rb_recv_index] = rep;
1162 spin_unlock_irqrestore(&buffers->rb_lock, flags);
1163}
1164
1165/*
1166 * Wrappers for internal-use kmalloc memory registration, used by buffer code.
1167 */
1168
Chuck Leverd6547882015-03-30 14:35:44 -04001169void
1170rpcrdma_mapping_error(struct rpcrdma_mr_seg *seg)
1171{
1172 dprintk("RPC: map_one: offset %p iova %llx len %zu\n",
1173 seg->mr_offset,
1174 (unsigned long long)seg->mr_dma, seg->mr_dmalen);
1175}
1176
Chuck Lever9128c3e2015-01-21 11:04:00 -05001177/**
1178 * rpcrdma_alloc_regbuf - kmalloc and register memory for SEND/RECV buffers
1179 * @ia: controlling rpcrdma_ia
1180 * @size: size of buffer to be allocated, in bytes
1181 * @flags: GFP flags
1182 *
1183 * Returns pointer to private header of an area of internally
1184 * registered memory, or an ERR_PTR. The registered buffer follows
1185 * the end of the private header.
1186 *
1187 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
1188 * receiving the payload of RDMA RECV operations. regbufs are not
1189 * used for RDMA READ/WRITE operations, thus are registered only for
1190 * LOCAL access.
1191 */
1192struct rpcrdma_regbuf *
1193rpcrdma_alloc_regbuf(struct rpcrdma_ia *ia, size_t size, gfp_t flags)
1194{
1195 struct rpcrdma_regbuf *rb;
Chuck Levere531dca2015-08-03 13:03:20 -04001196 struct ib_sge *iov;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001197
Chuck Lever9128c3e2015-01-21 11:04:00 -05001198 rb = kmalloc(sizeof(*rb) + size, flags);
1199 if (rb == NULL)
1200 goto out;
1201
Chuck Levere531dca2015-08-03 13:03:20 -04001202 iov = &rb->rg_iov;
1203 iov->addr = ib_dma_map_single(ia->ri_device,
1204 (void *)rb->rg_base, size,
1205 DMA_BIDIRECTIONAL);
1206 if (ib_dma_mapping_error(ia->ri_device, iov->addr))
Chuck Lever9128c3e2015-01-21 11:04:00 -05001207 goto out_free;
1208
Chuck Levere531dca2015-08-03 13:03:20 -04001209 iov->length = size;
Chuck Leverbb6c96d2015-09-24 10:34:21 +03001210 iov->lkey = ia->ri_pd->local_dma_lkey;
Chuck Levere531dca2015-08-03 13:03:20 -04001211 rb->rg_size = size;
1212 rb->rg_owner = NULL;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001213 return rb;
1214
1215out_free:
1216 kfree(rb);
1217out:
Chuck Levere531dca2015-08-03 13:03:20 -04001218 return ERR_PTR(-ENOMEM);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001219}
1220
1221/**
1222 * rpcrdma_free_regbuf - deregister and free registered buffer
1223 * @ia: controlling rpcrdma_ia
1224 * @rb: regbuf to be deregistered and freed
1225 */
1226void
1227rpcrdma_free_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1228{
Chuck Levere531dca2015-08-03 13:03:20 -04001229 struct ib_sge *iov;
1230
1231 if (!rb)
1232 return;
1233
1234 iov = &rb->rg_iov;
1235 ib_dma_unmap_single(ia->ri_device,
1236 iov->addr, iov->length, DMA_BIDIRECTIONAL);
1237 kfree(rb);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001238}
1239
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001240/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001241 * Prepost any receive buffer, then post send.
1242 *
1243 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1244 */
1245int
1246rpcrdma_ep_post(struct rpcrdma_ia *ia,
1247 struct rpcrdma_ep *ep,
1248 struct rpcrdma_req *req)
1249{
Chuck Leverb3221d62015-08-03 13:03:39 -04001250 struct ib_device *device = ia->ri_device;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001251 struct ib_send_wr send_wr, *send_wr_fail;
1252 struct rpcrdma_rep *rep = req->rl_reply;
Chuck Leverb3221d62015-08-03 13:03:39 -04001253 struct ib_sge *iov = req->rl_send_iov;
1254 int i, rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001255
1256 if (rep) {
1257 rc = rpcrdma_ep_post_recv(ia, ep, rep);
1258 if (rc)
1259 goto out;
1260 req->rl_reply = NULL;
1261 }
1262
1263 send_wr.next = NULL;
Chuck Levere46ac342015-03-30 14:35:35 -04001264 send_wr.wr_id = RPCRDMA_IGNORE_COMPLETION;
Chuck Leverb3221d62015-08-03 13:03:39 -04001265 send_wr.sg_list = iov;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001266 send_wr.num_sge = req->rl_niovs;
1267 send_wr.opcode = IB_WR_SEND;
Chuck Leverb3221d62015-08-03 13:03:39 -04001268
1269 for (i = 0; i < send_wr.num_sge; i++)
1270 ib_dma_sync_single_for_device(device, iov[i].addr,
1271 iov[i].length, DMA_TO_DEVICE);
1272 dprintk("RPC: %s: posting %d s/g entries\n",
1273 __func__, send_wr.num_sge);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001274
1275 if (DECR_CQCOUNT(ep) > 0)
1276 send_wr.send_flags = 0;
1277 else { /* Provider must take a send completion every now and then */
1278 INIT_CQCOUNT(ep);
1279 send_wr.send_flags = IB_SEND_SIGNALED;
1280 }
1281
1282 rc = ib_post_send(ia->ri_id->qp, &send_wr, &send_wr_fail);
1283 if (rc)
1284 dprintk("RPC: %s: ib_post_send returned %i\n", __func__,
1285 rc);
1286out:
1287 return rc;
1288}
1289
1290/*
1291 * (Re)post a receive buffer.
1292 */
1293int
1294rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
1295 struct rpcrdma_ep *ep,
1296 struct rpcrdma_rep *rep)
1297{
1298 struct ib_recv_wr recv_wr, *recv_wr_fail;
1299 int rc;
1300
1301 recv_wr.next = NULL;
1302 recv_wr.wr_id = (u64) (unsigned long) rep;
Chuck Lever6b1184c2015-01-21 11:04:25 -05001303 recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001304 recv_wr.num_sge = 1;
1305
Chuck Lever89e0d1122015-05-26 11:51:56 -04001306 ib_dma_sync_single_for_cpu(ia->ri_device,
Chuck Lever6b1184c2015-01-21 11:04:25 -05001307 rdmab_addr(rep->rr_rdmabuf),
1308 rdmab_length(rep->rr_rdmabuf),
1309 DMA_BIDIRECTIONAL);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001310
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001311 rc = ib_post_recv(ia->ri_id->qp, &recv_wr, &recv_wr_fail);
1312
1313 if (rc)
1314 dprintk("RPC: %s: ib_post_recv returned %i\n", __func__,
1315 rc);
1316 return rc;
1317}
Chuck Lever43e95982014-07-29 17:23:34 -04001318
Chuck Lever1c9351e2015-03-30 14:34:30 -04001319/* How many chunk list items fit within our inline buffers?
Chuck Lever43e95982014-07-29 17:23:34 -04001320 */
Chuck Lever1c9351e2015-03-30 14:34:30 -04001321unsigned int
1322rpcrdma_max_segments(struct rpcrdma_xprt *r_xprt)
Chuck Lever43e95982014-07-29 17:23:34 -04001323{
1324 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Lever1c9351e2015-03-30 14:34:30 -04001325 int bytes, segments;
Chuck Lever43e95982014-07-29 17:23:34 -04001326
Chuck Lever1c9351e2015-03-30 14:34:30 -04001327 bytes = min_t(unsigned int, cdata->inline_wsize, cdata->inline_rsize);
1328 bytes -= RPCRDMA_HDRLEN_MIN;
1329 if (bytes < sizeof(struct rpcrdma_segment) * 2) {
1330 pr_warn("RPC: %s: inline threshold too small\n",
1331 __func__);
1332 return 0;
Chuck Lever43e95982014-07-29 17:23:34 -04001333 }
Chuck Lever1c9351e2015-03-30 14:34:30 -04001334
1335 segments = 1 << (fls(bytes / sizeof(struct rpcrdma_segment)) - 1);
1336 dprintk("RPC: %s: max chunk list size = %d segments\n",
1337 __func__, segments);
1338 return segments;
Chuck Lever43e95982014-07-29 17:23:34 -04001339}