blob: fc1ef5f144b866f752000db2603bf0b668af2fad [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 Leverfe97b472015-10-24 17:27:10 -0400215static void
216rpcrdma_recvcq_process_wc(struct ib_wc *wc)
Chuck Leverfc664482014-05-28 10:33:25 -0400217{
218 struct rpcrdma_rep *rep =
219 (struct rpcrdma_rep *)(unsigned long)wc->wr_id;
220
Chuck Lever85024272015-01-21 11:02:04 -0500221 /* WARNING: Only wr_id and status are reliable at this point */
222 if (wc->status != IB_WC_SUCCESS)
223 goto out_fail;
Chuck Leverfc664482014-05-28 10:33:25 -0400224
Chuck Lever85024272015-01-21 11:02:04 -0500225 /* status == SUCCESS means all fields in wc are trustworthy */
Chuck Leverfc664482014-05-28 10:33:25 -0400226 if (wc->opcode != IB_WC_RECV)
227 return;
228
Chuck Lever85024272015-01-21 11:02:04 -0500229 dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n",
230 __func__, rep, wc->byte_len);
231
Chuck Leverfc664482014-05-28 10:33:25 -0400232 rep->rr_len = wc->byte_len;
Chuck Lever89e0d1122015-05-26 11:51:56 -0400233 ib_dma_sync_single_for_cpu(rep->rr_device,
Chuck Lever6b1184c2015-01-21 11:04:25 -0500234 rdmab_addr(rep->rr_rdmabuf),
235 rep->rr_len, DMA_FROM_DEVICE);
Chuck Lever23826c72016-03-04 11:28:27 -0500236
237 rpcrdma_update_granted_credits(rep);
Chuck Leverfc664482014-05-28 10:33:25 -0400238
239out_schedule:
Chuck Leverfe97b472015-10-24 17:27:10 -0400240 queue_work(rpcrdma_receive_wq, &rep->rr_work);
Chuck Lever85024272015-01-21 11:02:04 -0500241 return;
Chuck Leverfe97b472015-10-24 17:27:10 -0400242
Chuck Lever85024272015-01-21 11:02:04 -0500243out_fail:
244 if (wc->status != IB_WC_WR_FLUSH_ERR)
245 pr_err("RPC: %s: rep %p: %s\n",
Sagi Grimberg76357c72015-05-18 13:40:32 +0300246 __func__, rep, ib_wc_status_msg(wc->status));
Chuck Leverb0e178a2015-10-24 17:26:54 -0400247 rep->rr_len = RPCRDMA_BAD_LEN;
Chuck Lever85024272015-01-21 11:02:04 -0500248 goto out_schedule;
Chuck Leverfc664482014-05-28 10:33:25 -0400249}
250
Chuck Lever4220a072015-10-24 17:26:45 -0400251/* The wc array is on stack: automatic memory is always CPU-local.
252 *
253 * struct ib_wc is 64 bytes, making the poll array potentially
254 * large. But this is at the bottom of the call chain. Further
255 * substantial work is done in another thread.
256 */
257static void
258rpcrdma_recvcq_poll(struct ib_cq *cq)
Chuck Leverfc664482014-05-28 10:33:25 -0400259{
Chuck Lever4220a072015-10-24 17:26:45 -0400260 struct ib_wc *pos, wcs[4];
Chuck Lever4220a072015-10-24 17:26:45 -0400261 int count, rc;
Chuck Leverfc664482014-05-28 10:33:25 -0400262
Chuck Lever1c00dd02014-05-28 10:33:42 -0400263 do {
Chuck Lever4220a072015-10-24 17:26:45 -0400264 pos = wcs;
Chuck Lever1c00dd02014-05-28 10:33:42 -0400265
Chuck Lever4220a072015-10-24 17:26:45 -0400266 rc = ib_poll_cq(cq, ARRAY_SIZE(wcs), pos);
267 if (rc < 0)
268 break;
Chuck Lever1c00dd02014-05-28 10:33:42 -0400269
270 count = rc;
271 while (count-- > 0)
Chuck Leverfe97b472015-10-24 17:27:10 -0400272 rpcrdma_recvcq_process_wc(pos++);
Chuck Lever4220a072015-10-24 17:26:45 -0400273 } while (rc == ARRAY_SIZE(wcs));
Chuck Leverfc664482014-05-28 10:33:25 -0400274}
275
Chuck Lever7b3d7702015-10-24 17:26:37 -0400276/* Handle provider receive completion upcalls.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400277 */
278static void
Chuck Leverfc664482014-05-28 10:33:25 -0400279rpcrdma_recvcq_upcall(struct ib_cq *cq, void *cq_context)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400280{
Chuck Lever7b3d7702015-10-24 17:26:37 -0400281 do {
Chuck Lever4220a072015-10-24 17:26:45 -0400282 rpcrdma_recvcq_poll(cq);
Chuck Lever7b3d7702015-10-24 17:26:37 -0400283 } while (ib_req_notify_cq(cq, IB_CQ_NEXT_COMP |
284 IB_CQ_REPORT_MISSED_EVENTS) > 0);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400285}
286
Chuck Levera7bc2112014-07-29 17:23:52 -0400287static void
288rpcrdma_flush_cqs(struct rpcrdma_ep *ep)
289{
Chuck Lever5c166be2014-11-08 20:14:45 -0500290 struct ib_wc wc;
Chuck Lever5c166be2014-11-08 20:14:45 -0500291
292 while (ib_poll_cq(ep->rep_attr.recv_cq, 1, &wc) > 0)
Chuck Leverfe97b472015-10-24 17:27:10 -0400293 rpcrdma_recvcq_process_wc(&wc);
Chuck Lever5c166be2014-11-08 20:14:45 -0500294 while (ib_poll_cq(ep->rep_attr.send_cq, 1, &wc) > 0)
295 rpcrdma_sendcq_process_wc(&wc);
Chuck Levera7bc2112014-07-29 17:23:52 -0400296}
297
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400298static int
299rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event)
300{
301 struct rpcrdma_xprt *xprt = id->context;
302 struct rpcrdma_ia *ia = &xprt->rx_ia;
303 struct rpcrdma_ep *ep = &xprt->rx_ep;
Jeff Laytonf895b252014-11-17 16:58:04 -0500304#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400305 struct sockaddr *sap = (struct sockaddr *)&ep->rep_remote_addr;
Ingo Molnarff0db042008-11-25 16:58:42 -0800306#endif
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500307 struct ib_qp_attr *attr = &ia->ri_qp_attr;
308 struct ib_qp_init_attr *iattr = &ia->ri_qp_init_attr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400309 int connstate = 0;
310
311 switch (event->event) {
312 case RDMA_CM_EVENT_ADDR_RESOLVED:
313 case RDMA_CM_EVENT_ROUTE_RESOLVED:
Tom Talpey5675add2008-10-09 15:01:41 -0400314 ia->ri_async_rc = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400315 complete(&ia->ri_done);
316 break;
317 case RDMA_CM_EVENT_ADDR_ERROR:
318 ia->ri_async_rc = -EHOSTUNREACH;
319 dprintk("RPC: %s: CM address resolution error, ep 0x%p\n",
320 __func__, ep);
321 complete(&ia->ri_done);
322 break;
323 case RDMA_CM_EVENT_ROUTE_ERROR:
324 ia->ri_async_rc = -ENETUNREACH;
325 dprintk("RPC: %s: CM route resolution error, ep 0x%p\n",
326 __func__, ep);
327 complete(&ia->ri_done);
328 break;
329 case RDMA_CM_EVENT_ESTABLISHED:
330 connstate = 1;
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500331 ib_query_qp(ia->ri_id->qp, attr,
332 IB_QP_MAX_QP_RD_ATOMIC | IB_QP_MAX_DEST_RD_ATOMIC,
333 iattr);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400334 dprintk("RPC: %s: %d responder resources"
335 " (%d initiator)\n",
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500336 __func__, attr->max_dest_rd_atomic,
337 attr->max_rd_atomic);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400338 goto connected;
339 case RDMA_CM_EVENT_CONNECT_ERROR:
340 connstate = -ENOTCONN;
341 goto connected;
342 case RDMA_CM_EVENT_UNREACHABLE:
343 connstate = -ENETDOWN;
344 goto connected;
345 case RDMA_CM_EVENT_REJECTED:
346 connstate = -ECONNREFUSED;
347 goto connected;
348 case RDMA_CM_EVENT_DISCONNECTED:
349 connstate = -ECONNABORTED;
350 goto connected;
351 case RDMA_CM_EVENT_DEVICE_REMOVAL:
352 connstate = -ENODEV;
353connected:
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400354 dprintk("RPC: %s: %sconnected\n",
355 __func__, connstate > 0 ? "" : "dis");
Chuck Lever23826c72016-03-04 11:28:27 -0500356 atomic_set(&xprt->rx_buf.rb_credits, 1);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400357 ep->rep_connected = connstate;
Chuck Leverafadc462015-01-21 11:03:11 -0500358 rpcrdma_conn_func(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400359 wake_up_all(&ep->rep_connect_wait);
Chuck Lever8079fb72014-07-29 17:26:12 -0400360 /*FALLTHROUGH*/
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400361 default:
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400362 dprintk("RPC: %s: %pIS:%u (ep 0x%p): %s\n",
363 __func__, sap, rpc_get_port(sap), ep,
Sagi Grimberg76357c72015-05-18 13:40:32 +0300364 rdma_event_msg(event->event));
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400365 break;
366 }
367
Jeff Laytonf895b252014-11-17 16:58:04 -0500368#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400369 if (connstate == 1) {
Chuck Leverce1ab9a2015-01-21 11:03:35 -0500370 int ird = attr->max_dest_rd_atomic;
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400371 int tird = ep->rep_remote_cma.responder_resources;
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400372
Chuck Levera0ce85f2015-03-30 14:34:21 -0400373 pr_info("rpcrdma: connection to %pIS:%u on %s, memreg '%s', %d credits, %d responders%s\n",
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400374 sap, rpc_get_port(sap),
Chuck Lever89e0d1122015-05-26 11:51:56 -0400375 ia->ri_device->name,
Chuck Levera0ce85f2015-03-30 14:34:21 -0400376 ia->ri_ops->ro_displayname,
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400377 xprt->rx_buf.rb_max_requests,
378 ird, ird < 4 && ird < tird / 2 ? " (low!)" : "");
379 } else if (connstate < 0) {
Chuck Lever0dd39ca2015-03-30 14:33:43 -0400380 pr_info("rpcrdma: connection to %pIS:%u closed (%d)\n",
381 sap, rpc_get_port(sap), connstate);
Tom Talpeyb3cd8d42008-10-09 15:02:02 -0400382 }
383#endif
384
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400385 return 0;
386}
387
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400388static void rpcrdma_destroy_id(struct rdma_cm_id *id)
389{
390 if (id) {
391 module_put(id->device->owner);
392 rdma_destroy_id(id);
393 }
394}
395
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400396static struct rdma_cm_id *
397rpcrdma_create_id(struct rpcrdma_xprt *xprt,
398 struct rpcrdma_ia *ia, struct sockaddr *addr)
399{
400 struct rdma_cm_id *id;
401 int rc;
402
Tom Talpey1a954052008-10-09 15:01:31 -0400403 init_completion(&ia->ri_done);
404
Guy Shapirofa201052015-10-22 15:20:10 +0300405 id = rdma_create_id(&init_net, rpcrdma_conn_upcall, xprt, RDMA_PS_TCP,
406 IB_QPT_RC);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400407 if (IS_ERR(id)) {
408 rc = PTR_ERR(id);
409 dprintk("RPC: %s: rdma_create_id() failed %i\n",
410 __func__, rc);
411 return id;
412 }
413
Tom Talpey5675add2008-10-09 15:01:41 -0400414 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400415 rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT);
416 if (rc) {
417 dprintk("RPC: %s: rdma_resolve_addr() failed %i\n",
418 __func__, rc);
419 goto out;
420 }
Tom Talpey5675add2008-10-09 15:01:41 -0400421 wait_for_completion_interruptible_timeout(&ia->ri_done,
422 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400423
424 /* FIXME:
425 * Until xprtrdma supports DEVICE_REMOVAL, the provider must
426 * be pinned while there are active NFS/RDMA mounts to prevent
427 * hangs and crashes at umount time.
428 */
429 if (!ia->ri_async_rc && !try_module_get(id->device->owner)) {
430 dprintk("RPC: %s: Failed to get device module\n",
431 __func__);
432 ia->ri_async_rc = -ENODEV;
433 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400434 rc = ia->ri_async_rc;
435 if (rc)
436 goto out;
437
Tom Talpey5675add2008-10-09 15:01:41 -0400438 ia->ri_async_rc = -ETIMEDOUT;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400439 rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
440 if (rc) {
441 dprintk("RPC: %s: rdma_resolve_route() failed %i\n",
442 __func__, rc);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400443 goto put;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400444 }
Tom Talpey5675add2008-10-09 15:01:41 -0400445 wait_for_completion_interruptible_timeout(&ia->ri_done,
446 msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400447 rc = ia->ri_async_rc;
448 if (rc)
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400449 goto put;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400450
451 return id;
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400452put:
453 module_put(id->device->owner);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400454out:
455 rdma_destroy_id(id);
456 return ERR_PTR(rc);
457}
458
459/*
460 * Drain any cq, prior to teardown.
461 */
462static void
463rpcrdma_clean_cq(struct ib_cq *cq)
464{
465 struct ib_wc wc;
466 int count = 0;
467
468 while (1 == ib_poll_cq(cq, 1, &wc))
469 ++count;
470
471 if (count)
472 dprintk("RPC: %s: flushed %d events (last 0x%x)\n",
473 __func__, count, wc.opcode);
474}
475
476/*
477 * Exported functions.
478 */
479
480/*
481 * Open and initialize an Interface Adapter.
482 * o initializes fields of struct rpcrdma_ia, including
483 * interface and provider attributes and protection zone.
484 */
485int
486rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr, int memreg)
487{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400488 struct rpcrdma_ia *ia = &xprt->rx_ia;
Chuck Leverd1ed8572015-08-03 13:03:30 -0400489 int rc;
490
491 ia->ri_dma_mr = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400492
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400493 ia->ri_id = rpcrdma_create_id(xprt, ia, addr);
494 if (IS_ERR(ia->ri_id)) {
495 rc = PTR_ERR(ia->ri_id);
496 goto out1;
497 }
Chuck Lever89e0d1122015-05-26 11:51:56 -0400498 ia->ri_device = ia->ri_id->device;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400499
Chuck Lever89e0d1122015-05-26 11:51:56 -0400500 ia->ri_pd = ib_alloc_pd(ia->ri_device);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400501 if (IS_ERR(ia->ri_pd)) {
502 rc = PTR_ERR(ia->ri_pd);
503 dprintk("RPC: %s: ib_alloc_pd() failed %i\n",
504 __func__, rc);
505 goto out2;
506 }
507
Chuck Leverf10eafd2014-05-28 10:32:51 -0400508 if (memreg == RPCRDMA_FRMR) {
Or Gerlitze3e45b12015-12-18 10:59:48 +0200509 if (!(ia->ri_device->attrs.device_cap_flags &
510 IB_DEVICE_MEM_MGT_EXTENSIONS) ||
511 (ia->ri_device->attrs.max_fast_reg_page_list_len == 0)) {
Tom Talpey3197d3092008-10-09 15:00:20 -0400512 dprintk("RPC: %s: FRMR registration "
Chuck Leverf10eafd2014-05-28 10:32:51 -0400513 "not supported by HCA\n", __func__);
514 memreg = RPCRDMA_MTHCAFMR;
Tom Talpey3197d3092008-10-09 15:00:20 -0400515 }
Chuck Leverf10eafd2014-05-28 10:32:51 -0400516 }
517 if (memreg == RPCRDMA_MTHCAFMR) {
Chuck Lever89e0d1122015-05-26 11:51:56 -0400518 if (!ia->ri_device->alloc_fmr) {
Chuck Leverf10eafd2014-05-28 10:32:51 -0400519 dprintk("RPC: %s: MTHCAFMR registration "
520 "not supported by HCA\n", __func__);
Sagi Grimbergf022fa82015-10-06 19:52:37 +0300521 rc = -EINVAL;
Chuck Leverd2310932015-08-03 13:03:09 -0400522 goto out3;
Chuck Leverf10eafd2014-05-28 10:32:51 -0400523 }
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400524 }
525
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400526 switch (memreg) {
Tom Talpey3197d3092008-10-09 15:00:20 -0400527 case RPCRDMA_FRMR:
Chuck Levera0ce85f2015-03-30 14:34:21 -0400528 ia->ri_ops = &rpcrdma_frwr_memreg_ops;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400529 break;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400530 case RPCRDMA_ALLPHYSICAL:
Chuck Levera0ce85f2015-03-30 14:34:21 -0400531 ia->ri_ops = &rpcrdma_physical_memreg_ops;
Chuck Leverd1ed8572015-08-03 13:03:30 -0400532 break;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400533 case RPCRDMA_MTHCAFMR:
Chuck Levera0ce85f2015-03-30 14:34:21 -0400534 ia->ri_ops = &rpcrdma_fmr_memreg_ops;
Tom Talpeybd7ed1d2008-10-09 15:00:09 -0400535 break;
536 default:
Chuck Levercdd9ade2014-05-28 10:33:00 -0400537 printk(KERN_ERR "RPC: Unsupported memory "
538 "registration mode: %d\n", memreg);
539 rc = -ENOMEM;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500540 goto out3;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400541 }
Chuck Levera0ce85f2015-03-30 14:34:21 -0400542 dprintk("RPC: %s: memory registration strategy is '%s'\n",
543 __func__, ia->ri_ops->ro_displayname);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400544
Chuck Lever73806c82014-07-29 17:23:25 -0400545 rwlock_init(&ia->ri_qplock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400546 return 0;
Chuck Lever5ae711a2015-01-21 11:03:19 -0500547
548out3:
549 ib_dealloc_pd(ia->ri_pd);
550 ia->ri_pd = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400551out2:
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400552 rpcrdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400553 ia->ri_id = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400554out1:
555 return rc;
556}
557
558/*
559 * Clean up/close an IA.
560 * o if event handles and PD have been initialized, free them.
561 * o close the IA
562 */
563void
564rpcrdma_ia_close(struct rpcrdma_ia *ia)
565{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400566 dprintk("RPC: %s: entering\n", __func__);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400567 if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) {
568 if (ia->ri_id->qp)
569 rdma_destroy_qp(ia->ri_id);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400570 rpcrdma_destroy_id(ia->ri_id);
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400571 ia->ri_id = NULL;
572 }
Chuck Lever6d446982015-05-26 11:51:27 -0400573
574 /* If the pd is still busy, xprtrdma missed freeing a resource */
575 if (ia->ri_pd && !IS_ERR(ia->ri_pd))
Jason Gunthorpe7dd78642015-08-05 14:34:31 -0600576 ib_dealloc_pd(ia->ri_pd);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400577}
578
579/*
580 * Create unconnected endpoint.
581 */
582int
583rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia,
584 struct rpcrdma_create_data_internal *cdata)
585{
Chuck Leverfc664482014-05-28 10:33:25 -0400586 struct ib_cq *sendcq, *recvcq;
Matan Barak8e372102015-06-11 16:35:21 +0300587 struct ib_cq_init_attr cq_attr = {};
Chuck Lever124fa172015-10-24 17:27:51 -0400588 unsigned int max_qp_wr;
Chuck Lever5d40a8a2007-10-26 13:30:54 -0400589 int rc, err;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400590
Or Gerlitze3e45b12015-12-18 10:59:48 +0200591 if (ia->ri_device->attrs.max_sge < RPCRDMA_MAX_IOVS) {
Chuck Leverb3221d62015-08-03 13:03:39 -0400592 dprintk("RPC: %s: insufficient sge's available\n",
593 __func__);
594 return -ENOMEM;
595 }
596
Or Gerlitze3e45b12015-12-18 10:59:48 +0200597 if (ia->ri_device->attrs.max_qp_wr <= RPCRDMA_BACKWARD_WRS) {
Chuck Lever124fa172015-10-24 17:27:51 -0400598 dprintk("RPC: %s: insufficient wqe's available\n",
599 __func__);
600 return -ENOMEM;
601 }
Or Gerlitze3e45b12015-12-18 10:59:48 +0200602 max_qp_wr = ia->ri_device->attrs.max_qp_wr - RPCRDMA_BACKWARD_WRS;
Chuck Lever124fa172015-10-24 17:27:51 -0400603
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400604 /* check provider's send/recv wr limits */
Chuck Lever124fa172015-10-24 17:27:51 -0400605 if (cdata->max_requests > max_qp_wr)
606 cdata->max_requests = max_qp_wr;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400607
608 ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall;
609 ep->rep_attr.qp_context = ep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400610 ep->rep_attr.srq = NULL;
611 ep->rep_attr.cap.max_send_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400612 ep->rep_attr.cap.max_send_wr += RPCRDMA_BACKWARD_WRS;
Chuck Lever3968cb52015-03-30 14:35:26 -0400613 rc = ia->ri_ops->ro_open(ia, ep, cdata);
614 if (rc)
615 return rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400616 ep->rep_attr.cap.max_recv_wr = cdata->max_requests;
Chuck Lever124fa172015-10-24 17:27:51 -0400617 ep->rep_attr.cap.max_recv_wr += RPCRDMA_BACKWARD_WRS;
Chuck Leverb3221d62015-08-03 13:03:39 -0400618 ep->rep_attr.cap.max_send_sge = RPCRDMA_MAX_IOVS;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400619 ep->rep_attr.cap.max_recv_sge = 1;
620 ep->rep_attr.cap.max_inline_data = 0;
621 ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
622 ep->rep_attr.qp_type = IB_QPT_RC;
623 ep->rep_attr.port_num = ~0;
624
625 dprintk("RPC: %s: requested max: dtos: send %d recv %d; "
626 "iovs: send %d recv %d\n",
627 __func__,
628 ep->rep_attr.cap.max_send_wr,
629 ep->rep_attr.cap.max_recv_wr,
630 ep->rep_attr.cap.max_send_sge,
631 ep->rep_attr.cap.max_recv_sge);
632
633 /* set trigger for requesting send completion */
Chuck Leverfc664482014-05-28 10:33:25 -0400634 ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1;
Chuck Lever26ae9d12015-12-16 17:23:20 -0500635 if (ep->rep_cqinit <= 2)
636 ep->rep_cqinit = 0; /* always signal? */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400637 INIT_CQCOUNT(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400638 init_waitqueue_head(&ep->rep_connect_wait);
Chuck Lever254f91e2014-05-28 10:32:17 -0400639 INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400640
Matan Barak8e372102015-06-11 16:35:21 +0300641 cq_attr.cqe = ep->rep_attr.cap.max_send_wr + 1;
Chuck Lever89e0d1122015-05-26 11:51:56 -0400642 sendcq = ib_create_cq(ia->ri_device, rpcrdma_sendcq_upcall,
Chuck Lever4220a072015-10-24 17:26:45 -0400643 rpcrdma_cq_async_error_upcall, NULL, &cq_attr);
Chuck Leverfc664482014-05-28 10:33:25 -0400644 if (IS_ERR(sendcq)) {
645 rc = PTR_ERR(sendcq);
646 dprintk("RPC: %s: failed to create send CQ: %i\n",
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400647 __func__, rc);
648 goto out1;
649 }
650
Chuck Leverfc664482014-05-28 10:33:25 -0400651 rc = ib_req_notify_cq(sendcq, IB_CQ_NEXT_COMP);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400652 if (rc) {
653 dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
654 __func__, rc);
655 goto out2;
656 }
657
Matan Barak8e372102015-06-11 16:35:21 +0300658 cq_attr.cqe = ep->rep_attr.cap.max_recv_wr + 1;
Chuck Lever89e0d1122015-05-26 11:51:56 -0400659 recvcq = ib_create_cq(ia->ri_device, rpcrdma_recvcq_upcall,
Chuck Lever4220a072015-10-24 17:26:45 -0400660 rpcrdma_cq_async_error_upcall, NULL, &cq_attr);
Chuck Leverfc664482014-05-28 10:33:25 -0400661 if (IS_ERR(recvcq)) {
662 rc = PTR_ERR(recvcq);
663 dprintk("RPC: %s: failed to create recv CQ: %i\n",
664 __func__, rc);
665 goto out2;
666 }
667
668 rc = ib_req_notify_cq(recvcq, IB_CQ_NEXT_COMP);
669 if (rc) {
670 dprintk("RPC: %s: ib_req_notify_cq failed: %i\n",
671 __func__, rc);
672 ib_destroy_cq(recvcq);
673 goto out2;
674 }
675
676 ep->rep_attr.send_cq = sendcq;
677 ep->rep_attr.recv_cq = recvcq;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400678
679 /* Initialize cma parameters */
680
681 /* RPC/RDMA does not use private data */
682 ep->rep_remote_cma.private_data = NULL;
683 ep->rep_remote_cma.private_data_len = 0;
684
685 /* Client offers RDMA Read but does not initiate */
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400686 ep->rep_remote_cma.initiator_depth = 0;
Or Gerlitze3e45b12015-12-18 10:59:48 +0200687 if (ia->ri_device->attrs.max_qp_rd_atom > 32) /* arbitrary but <= 255 */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400688 ep->rep_remote_cma.responder_resources = 32;
689 else
Chuck Lever7bc79722015-01-21 11:03:27 -0500690 ep->rep_remote_cma.responder_resources =
Or Gerlitze3e45b12015-12-18 10:59:48 +0200691 ia->ri_device->attrs.max_qp_rd_atom;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400692
693 ep->rep_remote_cma.retry_count = 7;
694 ep->rep_remote_cma.flow_control = 0;
695 ep->rep_remote_cma.rnr_retry_count = 0;
696
697 return 0;
698
699out2:
Chuck Leverfc664482014-05-28 10:33:25 -0400700 err = ib_destroy_cq(sendcq);
Chuck Lever5d40a8a2007-10-26 13:30:54 -0400701 if (err)
702 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
703 __func__, err);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400704out1:
Chuck Leverd1ed8572015-08-03 13:03:30 -0400705 if (ia->ri_dma_mr)
706 ib_dereg_mr(ia->ri_dma_mr);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400707 return rc;
708}
709
710/*
711 * rpcrdma_ep_destroy
712 *
713 * Disconnect and destroy endpoint. After this, the only
714 * valid operations on the ep are to free it (if dynamically
715 * allocated) or re-create it.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400716 */
Chuck Lever7f1d5412014-05-28 10:33:16 -0400717void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400718rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
719{
720 int rc;
721
722 dprintk("RPC: %s: entering, connected is %d\n",
723 __func__, ep->rep_connected);
724
Chuck Lever254f91e2014-05-28 10:32:17 -0400725 cancel_delayed_work_sync(&ep->rep_connect_worker);
726
Steve Wise72c02172015-09-21 12:24:23 -0500727 if (ia->ri_id->qp)
Chuck Lever282191c2014-07-29 17:25:55 -0400728 rpcrdma_ep_disconnect(ep, ia);
Steve Wise72c02172015-09-21 12:24:23 -0500729
730 rpcrdma_clean_cq(ep->rep_attr.recv_cq);
731 rpcrdma_clean_cq(ep->rep_attr.send_cq);
732
733 if (ia->ri_id->qp) {
Tom Talpeyfee08ca2008-10-09 15:01:00 -0400734 rdma_destroy_qp(ia->ri_id);
735 ia->ri_id->qp = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400736 }
737
Chuck Leverfc664482014-05-28 10:33:25 -0400738 rc = ib_destroy_cq(ep->rep_attr.recv_cq);
739 if (rc)
740 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
741 __func__, rc);
742
Chuck Leverfc664482014-05-28 10:33:25 -0400743 rc = ib_destroy_cq(ep->rep_attr.send_cq);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400744 if (rc)
745 dprintk("RPC: %s: ib_destroy_cq returned %i\n",
746 __func__, rc);
Chuck Leverd1ed8572015-08-03 13:03:30 -0400747
748 if (ia->ri_dma_mr) {
749 rc = ib_dereg_mr(ia->ri_dma_mr);
750 dprintk("RPC: %s: ib_dereg_mr returned %i\n",
751 __func__, rc);
752 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400753}
754
755/*
756 * Connect unconnected endpoint.
757 */
758int
759rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
760{
Chuck Lever73806c82014-07-29 17:23:25 -0400761 struct rdma_cm_id *id, *old;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400762 int rc = 0;
763 int retry_count = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400764
Tom Talpeyc0555512008-10-10 11:32:45 -0400765 if (ep->rep_connected != 0) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400766 struct rpcrdma_xprt *xprt;
767retry:
Chuck Leverec62f402014-05-28 10:34:07 -0400768 dprintk("RPC: %s: reconnecting...\n", __func__);
Chuck Lever282191c2014-07-29 17:25:55 -0400769
770 rpcrdma_ep_disconnect(ep, ia);
Chuck Levera7bc2112014-07-29 17:23:52 -0400771 rpcrdma_flush_cqs(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400772
773 xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
774 id = rpcrdma_create_id(xprt, ia,
775 (struct sockaddr *)&xprt->rx_data.addr);
776 if (IS_ERR(id)) {
Chuck Leverec62f402014-05-28 10:34:07 -0400777 rc = -EHOSTUNREACH;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400778 goto out;
779 }
780 /* TEMP TEMP TEMP - fail if new device:
781 * Deregister/remarshal *all* requests!
782 * Close and recreate adapter, pd, etc!
783 * Re-determine all attributes still sane!
784 * More stuff I haven't thought of!
785 * Rrrgh!
786 */
Chuck Lever89e0d1122015-05-26 11:51:56 -0400787 if (ia->ri_device != id->device) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400788 printk("RPC: %s: can't reconnect on "
789 "different device!\n", __func__);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400790 rpcrdma_destroy_id(id);
Chuck Leverec62f402014-05-28 10:34:07 -0400791 rc = -ENETUNREACH;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400792 goto out;
793 }
794 /* END TEMP */
Chuck Leverec62f402014-05-28 10:34:07 -0400795 rc = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr);
796 if (rc) {
797 dprintk("RPC: %s: rdma_create_qp failed %i\n",
798 __func__, rc);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400799 rpcrdma_destroy_id(id);
Chuck Leverec62f402014-05-28 10:34:07 -0400800 rc = -ENETUNREACH;
801 goto out;
802 }
Chuck Lever73806c82014-07-29 17:23:25 -0400803
804 write_lock(&ia->ri_qplock);
805 old = ia->ri_id;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400806 ia->ri_id = id;
Chuck Lever73806c82014-07-29 17:23:25 -0400807 write_unlock(&ia->ri_qplock);
808
809 rdma_destroy_qp(old);
Devesh Sharmad0f36c42015-08-03 13:05:04 -0400810 rpcrdma_destroy_id(old);
Chuck Leverec62f402014-05-28 10:34:07 -0400811 } else {
812 dprintk("RPC: %s: connecting...\n", __func__);
813 rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr);
814 if (rc) {
815 dprintk("RPC: %s: rdma_create_qp failed %i\n",
816 __func__, rc);
817 /* do not update ep->rep_connected */
818 return -ENETUNREACH;
819 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400820 }
821
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400822 ep->rep_connected = 0;
823
824 rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma);
825 if (rc) {
826 dprintk("RPC: %s: rdma_connect() failed with %i\n",
827 __func__, rc);
828 goto out;
829 }
830
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400831 wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0);
832
833 /*
834 * Check state. A non-peer reject indicates no listener
835 * (ECONNREFUSED), which may be a transient state. All
836 * others indicate a transport condition which has already
837 * undergone a best-effort.
838 */
Joe Perchesf64f9e72009-11-29 16:55:45 -0800839 if (ep->rep_connected == -ECONNREFUSED &&
840 ++retry_count <= RDMA_CONNECT_RETRY_MAX) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400841 dprintk("RPC: %s: non-peer_reject, retry\n", __func__);
842 goto retry;
843 }
844 if (ep->rep_connected <= 0) {
845 /* Sometimes, the only way to reliably connect to remote
846 * CMs is to use same nonzero values for ORD and IRD. */
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400847 if (retry_count++ <= RDMA_CONNECT_RETRY_MAX + 1 &&
848 (ep->rep_remote_cma.responder_resources == 0 ||
849 ep->rep_remote_cma.initiator_depth !=
850 ep->rep_remote_cma.responder_resources)) {
851 if (ep->rep_remote_cma.responder_resources == 0)
852 ep->rep_remote_cma.responder_resources = 1;
853 ep->rep_remote_cma.initiator_depth =
854 ep->rep_remote_cma.responder_resources;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400855 goto retry;
Tom Tuckerb334eaa2008-10-09 15:00:30 -0400856 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400857 rc = ep->rep_connected;
858 } else {
Chuck Leverf531a5d2015-10-24 17:27:43 -0400859 struct rpcrdma_xprt *r_xprt;
860 unsigned int extras;
861
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400862 dprintk("RPC: %s: connected\n", __func__);
Chuck Leverf531a5d2015-10-24 17:27:43 -0400863
864 r_xprt = container_of(ia, struct rpcrdma_xprt, rx_ia);
865 extras = r_xprt->rx_buf.rb_bc_srv_max_requests;
866
867 if (extras) {
868 rc = rpcrdma_ep_post_extra_recv(r_xprt, extras);
Dan Carpenter38b95bc2015-11-05 11:37:08 +0300869 if (rc) {
Chuck Leverf531a5d2015-10-24 17:27:43 -0400870 pr_warn("%s: rpcrdma_ep_post_extra_recv: %i\n",
871 __func__, rc);
872 rc = 0;
Dan Carpenter38b95bc2015-11-05 11:37:08 +0300873 }
Chuck Leverf531a5d2015-10-24 17:27:43 -0400874 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400875 }
876
877out:
878 if (rc)
879 ep->rep_connected = rc;
880 return rc;
881}
882
883/*
884 * rpcrdma_ep_disconnect
885 *
886 * This is separate from destroy to facilitate the ability
887 * to reconnect without recreating the endpoint.
888 *
889 * This call is not reentrant, and must not be made in parallel
890 * on the same endpoint.
891 */
Chuck Lever282191c2014-07-29 17:25:55 -0400892void
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400893rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia)
894{
895 int rc;
896
Chuck Levera7bc2112014-07-29 17:23:52 -0400897 rpcrdma_flush_cqs(ep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400898 rc = rdma_disconnect(ia->ri_id);
899 if (!rc) {
900 /* returns without wait if not connected */
901 wait_event_interruptible(ep->rep_connect_wait,
902 ep->rep_connected != 1);
903 dprintk("RPC: %s: after wait, %sconnected\n", __func__,
904 (ep->rep_connected == 1) ? "still " : "dis");
905 } else {
906 dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc);
907 ep->rep_connected = rc;
908 }
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400909}
910
Chuck Leverf531a5d2015-10-24 17:27:43 -0400911struct rpcrdma_req *
Chuck Lever13924022015-01-21 11:03:52 -0500912rpcrdma_create_req(struct rpcrdma_xprt *r_xprt)
913{
Chuck Leverf531a5d2015-10-24 17:27:43 -0400914 struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
Chuck Lever13924022015-01-21 11:03:52 -0500915 struct rpcrdma_req *req;
Chuck Lever13924022015-01-21 11:03:52 -0500916
Chuck Lever85275c82015-01-21 11:04:16 -0500917 req = kzalloc(sizeof(*req), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500918 if (req == NULL)
Chuck Lever85275c82015-01-21 11:04:16 -0500919 return ERR_PTR(-ENOMEM);
Chuck Lever13924022015-01-21 11:03:52 -0500920
Chuck Leverf531a5d2015-10-24 17:27:43 -0400921 INIT_LIST_HEAD(&req->rl_free);
922 spin_lock(&buffer->rb_reqslock);
923 list_add(&req->rl_all, &buffer->rb_allreqs);
924 spin_unlock(&buffer->rb_reqslock);
Chuck Lever13924022015-01-21 11:03:52 -0500925 req->rl_buffer = &r_xprt->rx_buf;
926 return req;
Chuck Lever13924022015-01-21 11:03:52 -0500927}
928
Chuck Leverf531a5d2015-10-24 17:27:43 -0400929struct rpcrdma_rep *
Chuck Lever13924022015-01-21 11:03:52 -0500930rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt)
931{
932 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Lever13924022015-01-21 11:03:52 -0500933 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
934 struct rpcrdma_rep *rep;
935 int rc;
936
937 rc = -ENOMEM;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500938 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
Chuck Lever13924022015-01-21 11:03:52 -0500939 if (rep == NULL)
940 goto out;
Chuck Lever13924022015-01-21 11:03:52 -0500941
Chuck Lever6b1184c2015-01-21 11:04:25 -0500942 rep->rr_rdmabuf = rpcrdma_alloc_regbuf(ia, cdata->inline_rsize,
943 GFP_KERNEL);
944 if (IS_ERR(rep->rr_rdmabuf)) {
945 rc = PTR_ERR(rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -0500946 goto out_free;
Chuck Lever6b1184c2015-01-21 11:04:25 -0500947 }
Chuck Lever13924022015-01-21 11:03:52 -0500948
Chuck Lever89e0d1122015-05-26 11:51:56 -0400949 rep->rr_device = ia->ri_device;
Chuck Leverfed171b2015-05-26 11:51:37 -0400950 rep->rr_rxprt = r_xprt;
Chuck Leverfe97b472015-10-24 17:27:10 -0400951 INIT_WORK(&rep->rr_work, rpcrdma_receive_worker);
Chuck Lever13924022015-01-21 11:03:52 -0500952 return rep;
953
954out_free:
955 kfree(rep);
956out:
957 return ERR_PTR(rc);
958}
959
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400960int
Chuck Leverac920d02015-01-21 11:03:44 -0500961rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt)
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400962{
Chuck Leverac920d02015-01-21 11:03:44 -0500963 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
964 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400965 int i, rc;
966
Chuck Lever1e465fd2015-10-24 17:27:02 -0400967 buf->rb_max_requests = r_xprt->rx_data.max_requests;
Chuck Leverf531a5d2015-10-24 17:27:43 -0400968 buf->rb_bc_srv_max_requests = 0;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400969 spin_lock_init(&buf->rb_lock);
Chuck Lever23826c72016-03-04 11:28:27 -0500970 atomic_set(&buf->rb_credits, 1);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400971
Chuck Lever91e70e72015-03-30 14:34:58 -0400972 rc = ia->ri_ops->ro_init(r_xprt);
973 if (rc)
974 goto out;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400975
Chuck Lever1e465fd2015-10-24 17:27:02 -0400976 INIT_LIST_HEAD(&buf->rb_send_bufs);
Chuck Leverf531a5d2015-10-24 17:27:43 -0400977 INIT_LIST_HEAD(&buf->rb_allreqs);
978 spin_lock_init(&buf->rb_reqslock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400979 for (i = 0; i < buf->rb_max_requests; i++) {
980 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400981
Chuck Lever13924022015-01-21 11:03:52 -0500982 req = rpcrdma_create_req(r_xprt);
983 if (IS_ERR(req)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400984 dprintk("RPC: %s: request buffer %d alloc"
985 " failed\n", __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -0500986 rc = PTR_ERR(req);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400987 goto out;
988 }
Chuck Leverf531a5d2015-10-24 17:27:43 -0400989 req->rl_backchannel = false;
Chuck Lever1e465fd2015-10-24 17:27:02 -0400990 list_add(&req->rl_free, &buf->rb_send_bufs);
991 }
992
993 INIT_LIST_HEAD(&buf->rb_recv_bufs);
994 for (i = 0; i < buf->rb_max_requests + 2; i++) {
995 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400996
Chuck Lever13924022015-01-21 11:03:52 -0500997 rep = rpcrdma_create_rep(r_xprt);
998 if (IS_ERR(rep)) {
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -0400999 dprintk("RPC: %s: reply buffer %d alloc failed\n",
1000 __func__, i);
Chuck Lever13924022015-01-21 11:03:52 -05001001 rc = PTR_ERR(rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001002 goto out;
1003 }
Chuck Lever1e465fd2015-10-24 17:27:02 -04001004 list_add(&rep->rr_list, &buf->rb_recv_bufs);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001005 }
Chuck Lever13924022015-01-21 11:03:52 -05001006
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001007 return 0;
1008out:
1009 rpcrdma_buffer_destroy(buf);
1010 return rc;
1011}
1012
Chuck Lever1e465fd2015-10-24 17:27:02 -04001013static struct rpcrdma_req *
1014rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf)
1015{
1016 struct rpcrdma_req *req;
1017
1018 req = list_first_entry(&buf->rb_send_bufs,
1019 struct rpcrdma_req, rl_free);
1020 list_del(&req->rl_free);
1021 return req;
1022}
1023
1024static struct rpcrdma_rep *
1025rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf)
1026{
1027 struct rpcrdma_rep *rep;
1028
1029 rep = list_first_entry(&buf->rb_recv_bufs,
1030 struct rpcrdma_rep, rr_list);
1031 list_del(&rep->rr_list);
1032 return rep;
1033}
1034
Chuck Lever2e845222014-07-29 17:25:38 -04001035static void
Chuck Lever13924022015-01-21 11:03:52 -05001036rpcrdma_destroy_rep(struct rpcrdma_ia *ia, struct rpcrdma_rep *rep)
1037{
Chuck Lever6b1184c2015-01-21 11:04:25 -05001038 rpcrdma_free_regbuf(ia, rep->rr_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001039 kfree(rep);
1040}
1041
Chuck Leverf531a5d2015-10-24 17:27:43 -04001042void
Chuck Lever13924022015-01-21 11:03:52 -05001043rpcrdma_destroy_req(struct rpcrdma_ia *ia, struct rpcrdma_req *req)
1044{
Chuck Lever0ca77dc2015-01-21 11:04:08 -05001045 rpcrdma_free_regbuf(ia, req->rl_sendbuf);
Chuck Lever85275c82015-01-21 11:04:16 -05001046 rpcrdma_free_regbuf(ia, req->rl_rdmabuf);
Chuck Lever13924022015-01-21 11:03:52 -05001047 kfree(req);
1048}
1049
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001050void
1051rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf)
1052{
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001053 struct rpcrdma_ia *ia = rdmab_to_ia(buf);
1054
Chuck Lever1e465fd2015-10-24 17:27:02 -04001055 while (!list_empty(&buf->rb_recv_bufs)) {
1056 struct rpcrdma_rep *rep;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001057
Chuck Lever1e465fd2015-10-24 17:27:02 -04001058 rep = rpcrdma_buffer_get_rep_locked(buf);
1059 rpcrdma_destroy_rep(ia, rep);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001060 }
1061
Chuck Leverf531a5d2015-10-24 17:27:43 -04001062 spin_lock(&buf->rb_reqslock);
1063 while (!list_empty(&buf->rb_allreqs)) {
Chuck Lever1e465fd2015-10-24 17:27:02 -04001064 struct rpcrdma_req *req;
Allen Andrews4034ba02014-05-28 10:32:09 -04001065
Chuck Leverf531a5d2015-10-24 17:27:43 -04001066 req = list_first_entry(&buf->rb_allreqs,
1067 struct rpcrdma_req, rl_all);
1068 list_del(&req->rl_all);
1069
1070 spin_unlock(&buf->rb_reqslock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001071 rpcrdma_destroy_req(ia, req);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001072 spin_lock(&buf->rb_reqslock);
Chuck Lever9f9d8022014-07-29 17:24:45 -04001073 }
Chuck Leverf531a5d2015-10-24 17:27:43 -04001074 spin_unlock(&buf->rb_reqslock);
Chuck Lever9f9d8022014-07-29 17:24:45 -04001075
1076 ia->ri_ops->ro_destroy(buf);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001077}
1078
Chuck Lever346aa662015-05-26 11:52:06 -04001079struct rpcrdma_mw *
1080rpcrdma_get_mw(struct rpcrdma_xprt *r_xprt)
Chuck Leverc2922c02014-07-29 17:24:36 -04001081{
Chuck Lever346aa662015-05-26 11:52:06 -04001082 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
1083 struct rpcrdma_mw *mw = NULL;
Chuck Lever346aa662015-05-26 11:52:06 -04001084
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001085 spin_lock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001086 if (!list_empty(&buf->rb_mws)) {
1087 mw = list_first_entry(&buf->rb_mws,
1088 struct rpcrdma_mw, mw_list);
1089 list_del_init(&mw->mw_list);
Chuck Leverc2922c02014-07-29 17:24:36 -04001090 }
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001091 spin_unlock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001092
1093 if (!mw)
1094 pr_err("RPC: %s: no MWs available\n", __func__);
1095 return mw;
Chuck Leverc2922c02014-07-29 17:24:36 -04001096}
1097
Chuck Lever346aa662015-05-26 11:52:06 -04001098void
1099rpcrdma_put_mw(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mw *mw)
Chuck Leverc2922c02014-07-29 17:24:36 -04001100{
Chuck Lever346aa662015-05-26 11:52:06 -04001101 struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
Chuck Leverc2922c02014-07-29 17:24:36 -04001102
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001103 spin_lock(&buf->rb_mwlock);
Chuck Lever346aa662015-05-26 11:52:06 -04001104 list_add_tail(&mw->mw_list, &buf->rb_mws);
Chuck Lever58d1dcf2015-05-26 11:53:13 -04001105 spin_unlock(&buf->rb_mwlock);
Chuck Leverc2922c02014-07-29 17:24:36 -04001106}
1107
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001108/*
1109 * Get a set of request/reply buffers.
1110 *
Chuck Lever1e465fd2015-10-24 17:27:02 -04001111 * Reply buffer (if available) is attached to send buffer upon return.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001112 */
1113struct rpcrdma_req *
1114rpcrdma_buffer_get(struct rpcrdma_buffer *buffers)
1115{
1116 struct rpcrdma_req *req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001117
Chuck Levera5b027e2015-10-24 17:27:27 -04001118 spin_lock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001119 if (list_empty(&buffers->rb_send_bufs))
1120 goto out_reqbuf;
1121 req = rpcrdma_buffer_get_req_locked(buffers);
1122 if (list_empty(&buffers->rb_recv_bufs))
1123 goto out_repbuf;
1124 req->rl_reply = rpcrdma_buffer_get_rep_locked(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001125 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001126 return req;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001127
Chuck Lever1e465fd2015-10-24 17:27:02 -04001128out_reqbuf:
Chuck Levera5b027e2015-10-24 17:27:27 -04001129 spin_unlock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001130 pr_warn("RPC: %s: out of request buffers\n", __func__);
1131 return NULL;
1132out_repbuf:
Chuck Levera5b027e2015-10-24 17:27:27 -04001133 spin_unlock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001134 pr_warn("RPC: %s: out of reply buffers\n", __func__);
1135 req->rl_reply = NULL;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001136 return req;
1137}
1138
1139/*
1140 * Put request/reply buffers back into pool.
1141 * Pre-decrement counter/array index.
1142 */
1143void
1144rpcrdma_buffer_put(struct rpcrdma_req *req)
1145{
1146 struct rpcrdma_buffer *buffers = req->rl_buffer;
Chuck Lever1e465fd2015-10-24 17:27:02 -04001147 struct rpcrdma_rep *rep = req->rl_reply;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001148
Chuck Lever1e465fd2015-10-24 17:27:02 -04001149 req->rl_niovs = 0;
1150 req->rl_reply = NULL;
1151
Chuck Levera5b027e2015-10-24 17:27:27 -04001152 spin_lock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001153 list_add_tail(&req->rl_free, &buffers->rb_send_bufs);
1154 if (rep)
1155 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Levera5b027e2015-10-24 17:27:27 -04001156 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001157}
1158
1159/*
1160 * Recover reply buffers from pool.
Chuck Lever1e465fd2015-10-24 17:27:02 -04001161 * This happens when recovering from disconnect.
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001162 */
1163void
1164rpcrdma_recv_buffer_get(struct rpcrdma_req *req)
1165{
1166 struct rpcrdma_buffer *buffers = req->rl_buffer;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001167
Chuck Levera5b027e2015-10-24 17:27:27 -04001168 spin_lock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001169 if (!list_empty(&buffers->rb_recv_bufs))
1170 req->rl_reply = rpcrdma_buffer_get_rep_locked(buffers);
Chuck Levera5b027e2015-10-24 17:27:27 -04001171 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001172}
1173
1174/*
1175 * Put reply buffers back into pool when not attached to
1176 * request. This happens in error conditions.
1177 */
1178void
1179rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep)
1180{
Chuck Leverfed171b2015-05-26 11:51:37 -04001181 struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001182
Chuck Levera5b027e2015-10-24 17:27:27 -04001183 spin_lock(&buffers->rb_lock);
Chuck Lever1e465fd2015-10-24 17:27:02 -04001184 list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs);
Chuck Levera5b027e2015-10-24 17:27:27 -04001185 spin_unlock(&buffers->rb_lock);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001186}
1187
1188/*
1189 * Wrappers for internal-use kmalloc memory registration, used by buffer code.
1190 */
1191
Chuck Leverd6547882015-03-30 14:35:44 -04001192void
1193rpcrdma_mapping_error(struct rpcrdma_mr_seg *seg)
1194{
1195 dprintk("RPC: map_one: offset %p iova %llx len %zu\n",
1196 seg->mr_offset,
1197 (unsigned long long)seg->mr_dma, seg->mr_dmalen);
1198}
1199
Chuck Lever9128c3e2015-01-21 11:04:00 -05001200/**
1201 * rpcrdma_alloc_regbuf - kmalloc and register memory for SEND/RECV buffers
1202 * @ia: controlling rpcrdma_ia
1203 * @size: size of buffer to be allocated, in bytes
1204 * @flags: GFP flags
1205 *
1206 * Returns pointer to private header of an area of internally
1207 * registered memory, or an ERR_PTR. The registered buffer follows
1208 * the end of the private header.
1209 *
1210 * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for
1211 * receiving the payload of RDMA RECV operations. regbufs are not
1212 * used for RDMA READ/WRITE operations, thus are registered only for
1213 * LOCAL access.
1214 */
1215struct rpcrdma_regbuf *
1216rpcrdma_alloc_regbuf(struct rpcrdma_ia *ia, size_t size, gfp_t flags)
1217{
1218 struct rpcrdma_regbuf *rb;
Chuck Levere531dca2015-08-03 13:03:20 -04001219 struct ib_sge *iov;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001220
Chuck Lever9128c3e2015-01-21 11:04:00 -05001221 rb = kmalloc(sizeof(*rb) + size, flags);
1222 if (rb == NULL)
1223 goto out;
1224
Chuck Levere531dca2015-08-03 13:03:20 -04001225 iov = &rb->rg_iov;
1226 iov->addr = ib_dma_map_single(ia->ri_device,
1227 (void *)rb->rg_base, size,
1228 DMA_BIDIRECTIONAL);
1229 if (ib_dma_mapping_error(ia->ri_device, iov->addr))
Chuck Lever9128c3e2015-01-21 11:04:00 -05001230 goto out_free;
1231
Chuck Levere531dca2015-08-03 13:03:20 -04001232 iov->length = size;
Chuck Leverbb6c96d2015-09-24 10:34:21 +03001233 iov->lkey = ia->ri_pd->local_dma_lkey;
Chuck Levere531dca2015-08-03 13:03:20 -04001234 rb->rg_size = size;
1235 rb->rg_owner = NULL;
Chuck Lever9128c3e2015-01-21 11:04:00 -05001236 return rb;
1237
1238out_free:
1239 kfree(rb);
1240out:
Chuck Levere531dca2015-08-03 13:03:20 -04001241 return ERR_PTR(-ENOMEM);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001242}
1243
1244/**
1245 * rpcrdma_free_regbuf - deregister and free registered buffer
1246 * @ia: controlling rpcrdma_ia
1247 * @rb: regbuf to be deregistered and freed
1248 */
1249void
1250rpcrdma_free_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb)
1251{
Chuck Levere531dca2015-08-03 13:03:20 -04001252 struct ib_sge *iov;
1253
1254 if (!rb)
1255 return;
1256
1257 iov = &rb->rg_iov;
1258 ib_dma_unmap_single(ia->ri_device,
1259 iov->addr, iov->length, DMA_BIDIRECTIONAL);
1260 kfree(rb);
Chuck Lever9128c3e2015-01-21 11:04:00 -05001261}
1262
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001263/*
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001264 * Prepost any receive buffer, then post send.
1265 *
1266 * Receive buffer is donated to hardware, reclaimed upon recv completion.
1267 */
1268int
1269rpcrdma_ep_post(struct rpcrdma_ia *ia,
1270 struct rpcrdma_ep *ep,
1271 struct rpcrdma_req *req)
1272{
Chuck Leverb3221d62015-08-03 13:03:39 -04001273 struct ib_device *device = ia->ri_device;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001274 struct ib_send_wr send_wr, *send_wr_fail;
1275 struct rpcrdma_rep *rep = req->rl_reply;
Chuck Leverb3221d62015-08-03 13:03:39 -04001276 struct ib_sge *iov = req->rl_send_iov;
1277 int i, rc;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001278
1279 if (rep) {
1280 rc = rpcrdma_ep_post_recv(ia, ep, rep);
1281 if (rc)
1282 goto out;
1283 req->rl_reply = NULL;
1284 }
1285
1286 send_wr.next = NULL;
Chuck Levere46ac342015-03-30 14:35:35 -04001287 send_wr.wr_id = RPCRDMA_IGNORE_COMPLETION;
Chuck Leverb3221d62015-08-03 13:03:39 -04001288 send_wr.sg_list = iov;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001289 send_wr.num_sge = req->rl_niovs;
1290 send_wr.opcode = IB_WR_SEND;
Chuck Leverb3221d62015-08-03 13:03:39 -04001291
1292 for (i = 0; i < send_wr.num_sge; i++)
1293 ib_dma_sync_single_for_device(device, iov[i].addr,
1294 iov[i].length, DMA_TO_DEVICE);
1295 dprintk("RPC: %s: posting %d s/g entries\n",
1296 __func__, send_wr.num_sge);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001297
1298 if (DECR_CQCOUNT(ep) > 0)
1299 send_wr.send_flags = 0;
1300 else { /* Provider must take a send completion every now and then */
1301 INIT_CQCOUNT(ep);
1302 send_wr.send_flags = IB_SEND_SIGNALED;
1303 }
1304
1305 rc = ib_post_send(ia->ri_id->qp, &send_wr, &send_wr_fail);
1306 if (rc)
1307 dprintk("RPC: %s: ib_post_send returned %i\n", __func__,
1308 rc);
1309out:
1310 return rc;
1311}
1312
1313/*
1314 * (Re)post a receive buffer.
1315 */
1316int
1317rpcrdma_ep_post_recv(struct rpcrdma_ia *ia,
1318 struct rpcrdma_ep *ep,
1319 struct rpcrdma_rep *rep)
1320{
1321 struct ib_recv_wr recv_wr, *recv_wr_fail;
1322 int rc;
1323
1324 recv_wr.next = NULL;
1325 recv_wr.wr_id = (u64) (unsigned long) rep;
Chuck Lever6b1184c2015-01-21 11:04:25 -05001326 recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov;
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001327 recv_wr.num_sge = 1;
1328
Chuck Lever89e0d1122015-05-26 11:51:56 -04001329 ib_dma_sync_single_for_cpu(ia->ri_device,
Chuck Lever6b1184c2015-01-21 11:04:25 -05001330 rdmab_addr(rep->rr_rdmabuf),
1331 rdmab_length(rep->rr_rdmabuf),
1332 DMA_BIDIRECTIONAL);
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001333
\"Talpey, Thomas\c56c65f2007-09-10 13:51:18 -04001334 rc = ib_post_recv(ia->ri_id->qp, &recv_wr, &recv_wr_fail);
1335
1336 if (rc)
1337 dprintk("RPC: %s: ib_post_recv returned %i\n", __func__,
1338 rc);
1339 return rc;
1340}
Chuck Lever43e95982014-07-29 17:23:34 -04001341
Chuck Leverf531a5d2015-10-24 17:27:43 -04001342/**
1343 * rpcrdma_ep_post_extra_recv - Post buffers for incoming backchannel requests
1344 * @r_xprt: transport associated with these backchannel resources
1345 * @min_reqs: minimum number of incoming requests expected
1346 *
1347 * Returns zero if all requested buffers were posted, or a negative errno.
1348 */
1349int
1350rpcrdma_ep_post_extra_recv(struct rpcrdma_xprt *r_xprt, unsigned int count)
1351{
1352 struct rpcrdma_buffer *buffers = &r_xprt->rx_buf;
1353 struct rpcrdma_ia *ia = &r_xprt->rx_ia;
1354 struct rpcrdma_ep *ep = &r_xprt->rx_ep;
1355 struct rpcrdma_rep *rep;
Chuck Leverf531a5d2015-10-24 17:27:43 -04001356 int rc;
1357
1358 while (count--) {
Chuck Lever9b066882015-12-16 17:22:06 -05001359 spin_lock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001360 if (list_empty(&buffers->rb_recv_bufs))
1361 goto out_reqbuf;
1362 rep = rpcrdma_buffer_get_rep_locked(buffers);
Chuck Lever9b066882015-12-16 17:22:06 -05001363 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001364
1365 rc = rpcrdma_ep_post_recv(ia, ep, rep);
1366 if (rc)
1367 goto out_rc;
1368 }
1369
1370 return 0;
1371
1372out_reqbuf:
Chuck Lever9b066882015-12-16 17:22:06 -05001373 spin_unlock(&buffers->rb_lock);
Chuck Leverf531a5d2015-10-24 17:27:43 -04001374 pr_warn("%s: no extra receive buffers\n", __func__);
1375 return -ENOMEM;
1376
1377out_rc:
1378 rpcrdma_recv_buffer_put(rep);
1379 return rc;
1380}
1381
Chuck Lever1c9351e2015-03-30 14:34:30 -04001382/* How many chunk list items fit within our inline buffers?
Chuck Lever43e95982014-07-29 17:23:34 -04001383 */
Chuck Lever1c9351e2015-03-30 14:34:30 -04001384unsigned int
1385rpcrdma_max_segments(struct rpcrdma_xprt *r_xprt)
Chuck Lever43e95982014-07-29 17:23:34 -04001386{
1387 struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
Chuck Lever1c9351e2015-03-30 14:34:30 -04001388 int bytes, segments;
Chuck Lever43e95982014-07-29 17:23:34 -04001389
Chuck Lever1c9351e2015-03-30 14:34:30 -04001390 bytes = min_t(unsigned int, cdata->inline_wsize, cdata->inline_rsize);
1391 bytes -= RPCRDMA_HDRLEN_MIN;
1392 if (bytes < sizeof(struct rpcrdma_segment) * 2) {
1393 pr_warn("RPC: %s: inline threshold too small\n",
1394 __func__);
1395 return 0;
Chuck Lever43e95982014-07-29 17:23:34 -04001396 }
Chuck Lever1c9351e2015-03-30 14:34:30 -04001397
1398 segments = 1 << (fls(bytes / sizeof(struct rpcrdma_segment)) - 1);
1399 dprintk("RPC: %s: max chunk list size = %d segments\n",
1400 __func__, segments);
1401 return segments;
Chuck Lever43e95982014-07-29 17:23:34 -04001402}