Commit | Line | Data |
---|---|---|
f58851e6 | 1 | /* |
62b56a67 | 2 | * Copyright (c) 2014-2017 Oracle. All rights reserved. |
c56c65fb TT |
3 | * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved. |
4 | * | |
5 | * This software is available to you under a choice of one of two | |
6 | * licenses. You may choose to be licensed under the terms of the GNU | |
7 | * General Public License (GPL) Version 2, available from the file | |
8 | * COPYING in the main directory of this source tree, or the BSD-type | |
9 | * license below: | |
10 | * | |
11 | * Redistribution and use in source and binary forms, with or without | |
12 | * modification, are permitted provided that the following conditions | |
13 | * are met: | |
14 | * | |
15 | * Redistributions of source code must retain the above copyright | |
16 | * notice, this list of conditions and the following disclaimer. | |
17 | * | |
18 | * Redistributions in binary form must reproduce the above | |
19 | * copyright notice, this list of conditions and the following | |
20 | * disclaimer in the documentation and/or other materials provided | |
21 | * with the distribution. | |
22 | * | |
23 | * Neither the name of the Network Appliance, Inc. nor the names of | |
24 | * its contributors may be used to endorse or promote products | |
25 | * derived from this software without specific prior written | |
26 | * permission. | |
27 | * | |
28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
29 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
30 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
31 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
32 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
33 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
34 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
35 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
36 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
37 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
38 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
f58851e6 TT |
39 | */ |
40 | ||
c56c65fb TT |
41 | /* |
42 | * verbs.c | |
43 | * | |
44 | * Encapsulates the major functions managing: | |
45 | * o adapters | |
46 | * o endpoints | |
47 | * o connections | |
48 | * o buffer memory | |
49 | */ | |
50 | ||
a6b7a407 | 51 | #include <linux/interrupt.h> |
5a0e3ad6 | 52 | #include <linux/slab.h> |
0dd39cae | 53 | #include <linux/sunrpc/addr.h> |
05c97466 | 54 | #include <linux/sunrpc/svc_rdma.h> |
ae72950a CL |
55 | |
56 | #include <asm-generic/barrier.h> | |
65866f82 | 57 | #include <asm/bitops.h> |
56a6bd15 | 58 | |
0a90487b | 59 | #include <rdma/ib_cm.h> |
c56c65fb | 60 | |
f58851e6 TT |
61 | #include "xprt_rdma.h" |
62 | ||
c56c65fb TT |
63 | /* |
64 | * Globals/Macros | |
65 | */ | |
66 | ||
f895b252 | 67 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) |
c56c65fb TT |
68 | # define RPCDBG_FACILITY RPCDBG_TRANS |
69 | #endif | |
70 | ||
71 | /* | |
72 | * internal functions | |
73 | */ | |
96ceddea CL |
74 | static void rpcrdma_mrs_create(struct rpcrdma_xprt *r_xprt); |
75 | static void rpcrdma_mrs_destroy(struct rpcrdma_buffer *buf); | |
bebd0318 | 76 | static void rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb); |
c56c65fb | 77 | |
d8f532d2 | 78 | struct workqueue_struct *rpcrdma_receive_wq __read_mostly; |
c56c65fb | 79 | |
fe97b47c CL |
80 | int |
81 | rpcrdma_alloc_wq(void) | |
c56c65fb | 82 | { |
fe97b47c | 83 | struct workqueue_struct *recv_wq; |
c56c65fb | 84 | |
fe97b47c | 85 | recv_wq = alloc_workqueue("xprtrdma_receive", |
ccede759 | 86 | WQ_MEM_RECLAIM | WQ_HIGHPRI, |
fe97b47c CL |
87 | 0); |
88 | if (!recv_wq) | |
89 | return -ENOMEM; | |
c56c65fb | 90 | |
fe97b47c CL |
91 | rpcrdma_receive_wq = recv_wq; |
92 | return 0; | |
c56c65fb TT |
93 | } |
94 | ||
fe97b47c CL |
95 | void |
96 | rpcrdma_destroy_wq(void) | |
f1a03b76 | 97 | { |
fe97b47c | 98 | struct workqueue_struct *wq; |
f1a03b76 | 99 | |
fe97b47c CL |
100 | if (rpcrdma_receive_wq) { |
101 | wq = rpcrdma_receive_wq; | |
102 | rpcrdma_receive_wq = NULL; | |
103 | destroy_workqueue(wq); | |
104 | } | |
f1a03b76 CL |
105 | } |
106 | ||
c56c65fb TT |
107 | static void |
108 | rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context) | |
109 | { | |
110 | struct rpcrdma_ep *ep = context; | |
643cf323 CL |
111 | struct rpcrdma_xprt *r_xprt = container_of(ep, struct rpcrdma_xprt, |
112 | rx_ep); | |
c56c65fb | 113 | |
643cf323 | 114 | trace_xprtrdma_qp_error(r_xprt, event); |
2f6922ca CL |
115 | pr_err("rpcrdma: %s on device %s ep %p\n", |
116 | ib_event_msg(event->event), event->device->name, context); | |
117 | ||
c56c65fb TT |
118 | if (ep->rep_connected == 1) { |
119 | ep->rep_connected = -EIO; | |
afadc468 | 120 | rpcrdma_conn_func(ep); |
c56c65fb TT |
121 | wake_up_all(&ep->rep_connect_wait); |
122 | } | |
123 | } | |
124 | ||
2fa8f88d CL |
125 | /** |
126 | * rpcrdma_wc_send - Invoked by RDMA provider for each polled Send WC | |
127 | * @cq: completion queue (ignored) | |
128 | * @wc: completed WR | |
129 | * | |
fc664485 CL |
130 | */ |
131 | static void | |
2fa8f88d | 132 | rpcrdma_wc_send(struct ib_cq *cq, struct ib_wc *wc) |
fc664485 | 133 | { |
ae72950a CL |
134 | struct ib_cqe *cqe = wc->wr_cqe; |
135 | struct rpcrdma_sendctx *sc = | |
136 | container_of(cqe, struct rpcrdma_sendctx, sc_cqe); | |
137 | ||
2fa8f88d | 138 | /* WARNING: Only wr_cqe and status are reliable at this point */ |
ab03eff5 | 139 | trace_xprtrdma_wc_send(sc, wc); |
2fa8f88d CL |
140 | if (wc->status != IB_WC_SUCCESS && wc->status != IB_WC_WR_FLUSH_ERR) |
141 | pr_err("rpcrdma: Send: %s (%u/0x%x)\n", | |
142 | ib_wc_status_msg(wc->status), | |
143 | wc->status, wc->vendor_err); | |
ae72950a CL |
144 | |
145 | rpcrdma_sendctx_put_locked(sc); | |
fc664485 | 146 | } |
c56c65fb | 147 | |
552bf225 | 148 | /** |
1519e969 | 149 | * rpcrdma_wc_receive - Invoked by RDMA provider for each polled Receive WC |
552bf225 CL |
150 | * @cq: completion queue (ignored) |
151 | * @wc: completed WR | |
152 | * | |
153 | */ | |
fc664485 | 154 | static void |
1519e969 | 155 | rpcrdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc) |
fc664485 | 156 | { |
552bf225 CL |
157 | struct ib_cqe *cqe = wc->wr_cqe; |
158 | struct rpcrdma_rep *rep = container_of(cqe, struct rpcrdma_rep, | |
159 | rr_cqe); | |
fc664485 | 160 | |
8502427c | 161 | /* WARNING: Only wr_id and status are reliable at this point */ |
b4a7f91c | 162 | trace_xprtrdma_wc_receive(rep, wc); |
8502427c CL |
163 | if (wc->status != IB_WC_SUCCESS) |
164 | goto out_fail; | |
fc664485 | 165 | |
8502427c | 166 | /* status == SUCCESS means all fields in wc are trustworthy */ |
96f8778f | 167 | rpcrdma_set_xdrlen(&rep->rr_hdrbuf, wc->byte_len); |
c8b920bb CL |
168 | rep->rr_wc_flags = wc->wc_flags; |
169 | rep->rr_inv_rkey = wc->ex.invalidate_rkey; | |
170 | ||
91a10c52 | 171 | ib_dma_sync_single_for_cpu(rdmab_device(rep->rr_rdmabuf), |
6b1184cd | 172 | rdmab_addr(rep->rr_rdmabuf), |
e2a67190 | 173 | wc->byte_len, DMA_FROM_DEVICE); |
23826c7a | 174 | |
fc664485 | 175 | out_schedule: |
d8f532d2 | 176 | rpcrdma_reply_handler(rep); |
8502427c | 177 | return; |
fe97b47c | 178 | |
8502427c CL |
179 | out_fail: |
180 | if (wc->status != IB_WC_WR_FLUSH_ERR) | |
552bf225 CL |
181 | pr_err("rpcrdma: Recv: %s (%u/0x%x)\n", |
182 | ib_wc_status_msg(wc->status), | |
183 | wc->status, wc->vendor_err); | |
e2a67190 | 184 | rpcrdma_set_xdrlen(&rep->rr_hdrbuf, 0); |
8502427c | 185 | goto out_schedule; |
fc664485 CL |
186 | } |
187 | ||
87cfb9a0 CL |
188 | static void |
189 | rpcrdma_update_connect_private(struct rpcrdma_xprt *r_xprt, | |
190 | struct rdma_conn_param *param) | |
191 | { | |
192 | struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data; | |
193 | const struct rpcrdma_connect_private *pmsg = param->private_data; | |
194 | unsigned int rsize, wsize; | |
195 | ||
c8b920bb | 196 | /* Default settings for RPC-over-RDMA Version One */ |
b5f0afbe | 197 | r_xprt->rx_ia.ri_implicit_roundup = xprt_rdma_pad_optimize; |
87cfb9a0 CL |
198 | rsize = RPCRDMA_V1_DEF_INLINE_SIZE; |
199 | wsize = RPCRDMA_V1_DEF_INLINE_SIZE; | |
200 | ||
201 | if (pmsg && | |
202 | pmsg->cp_magic == rpcrdma_cmp_magic && | |
203 | pmsg->cp_version == RPCRDMA_CMP_VERSION) { | |
c95a3c6b | 204 | r_xprt->rx_ia.ri_implicit_roundup = true; |
87cfb9a0 CL |
205 | rsize = rpcrdma_decode_buffer_size(pmsg->cp_send_size); |
206 | wsize = rpcrdma_decode_buffer_size(pmsg->cp_recv_size); | |
207 | } | |
208 | ||
209 | if (rsize < cdata->inline_rsize) | |
210 | cdata->inline_rsize = rsize; | |
211 | if (wsize < cdata->inline_wsize) | |
212 | cdata->inline_wsize = wsize; | |
6d6bf72d CL |
213 | dprintk("RPC: %s: max send %u, max recv %u\n", |
214 | __func__, cdata->inline_wsize, cdata->inline_rsize); | |
87cfb9a0 CL |
215 | rpcrdma_set_max_header_sizes(r_xprt); |
216 | } | |
217 | ||
c56c65fb TT |
218 | static int |
219 | rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event) | |
220 | { | |
221 | struct rpcrdma_xprt *xprt = id->context; | |
222 | struct rpcrdma_ia *ia = &xprt->rx_ia; | |
223 | struct rpcrdma_ep *ep = &xprt->rx_ep; | |
c56c65fb TT |
224 | int connstate = 0; |
225 | ||
b4744e00 | 226 | trace_xprtrdma_conn_upcall(xprt, event); |
c56c65fb TT |
227 | switch (event->event) { |
228 | case RDMA_CM_EVENT_ADDR_RESOLVED: | |
229 | case RDMA_CM_EVENT_ROUTE_RESOLVED: | |
5675add3 | 230 | ia->ri_async_rc = 0; |
c56c65fb TT |
231 | complete(&ia->ri_done); |
232 | break; | |
233 | case RDMA_CM_EVENT_ADDR_ERROR: | |
234 | ia->ri_async_rc = -EHOSTUNREACH; | |
c56c65fb TT |
235 | complete(&ia->ri_done); |
236 | break; | |
237 | case RDMA_CM_EVENT_ROUTE_ERROR: | |
238 | ia->ri_async_rc = -ENETUNREACH; | |
c56c65fb TT |
239 | complete(&ia->ri_done); |
240 | break; | |
bebd0318 CL |
241 | case RDMA_CM_EVENT_DEVICE_REMOVAL: |
242 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) | |
d461f1f2 | 243 | pr_info("rpcrdma: removing device %s for %s:%s\n", |
173b8f49 | 244 | ia->ri_device->name, |
d461f1f2 | 245 | rpcrdma_addrstr(xprt), rpcrdma_portstr(xprt)); |
bebd0318 CL |
246 | #endif |
247 | set_bit(RPCRDMA_IAF_REMOVING, &ia->ri_flags); | |
248 | ep->rep_connected = -ENODEV; | |
249 | xprt_force_disconnect(&xprt->rx_xprt); | |
250 | wait_for_completion(&ia->ri_remove_done); | |
251 | ||
252 | ia->ri_id = NULL; | |
253 | ia->ri_pd = NULL; | |
254 | ia->ri_device = NULL; | |
255 | /* Return 1 to ensure the core destroys the id. */ | |
256 | return 1; | |
c56c65fb | 257 | case RDMA_CM_EVENT_ESTABLISHED: |
8a14793e | 258 | ++xprt->rx_xprt.connect_cookie; |
c56c65fb | 259 | connstate = 1; |
87cfb9a0 | 260 | rpcrdma_update_connect_private(xprt, &event->param.conn); |
c56c65fb TT |
261 | goto connected; |
262 | case RDMA_CM_EVENT_CONNECT_ERROR: | |
263 | connstate = -ENOTCONN; | |
264 | goto connected; | |
265 | case RDMA_CM_EVENT_UNREACHABLE: | |
266 | connstate = -ENETDOWN; | |
267 | goto connected; | |
268 | case RDMA_CM_EVENT_REJECTED: | |
d461f1f2 CL |
269 | dprintk("rpcrdma: connection to %s:%s rejected: %s\n", |
270 | rpcrdma_addrstr(xprt), rpcrdma_portstr(xprt), | |
0a90487b | 271 | rdma_reject_msg(id, event->status)); |
c56c65fb | 272 | connstate = -ECONNREFUSED; |
0a90487b CL |
273 | if (event->status == IB_CM_REJ_STALE_CONN) |
274 | connstate = -EAGAIN; | |
c56c65fb TT |
275 | goto connected; |
276 | case RDMA_CM_EVENT_DISCONNECTED: | |
8a14793e | 277 | ++xprt->rx_xprt.connect_cookie; |
c56c65fb | 278 | connstate = -ECONNABORTED; |
c56c65fb | 279 | connected: |
be798f90 | 280 | xprt->rx_buf.rb_credits = 1; |
c56c65fb | 281 | ep->rep_connected = connstate; |
afadc468 | 282 | rpcrdma_conn_func(ep); |
c56c65fb | 283 | wake_up_all(&ep->rep_connect_wait); |
8079fb78 | 284 | /*FALLTHROUGH*/ |
c56c65fb | 285 | default: |
d461f1f2 CL |
286 | dprintk("RPC: %s: %s:%s on %s/%s (ep 0x%p): %s\n", |
287 | __func__, | |
288 | rpcrdma_addrstr(xprt), rpcrdma_portstr(xprt), | |
173b8f49 CL |
289 | ia->ri_device->name, ia->ri_ops->ro_displayname, |
290 | ep, rdma_event_msg(event->event)); | |
c56c65fb TT |
291 | break; |
292 | } | |
293 | ||
294 | return 0; | |
295 | } | |
296 | ||
297 | static struct rdma_cm_id * | |
dd229cee | 298 | rpcrdma_create_id(struct rpcrdma_xprt *xprt, struct rpcrdma_ia *ia) |
c56c65fb | 299 | { |
109b88ab | 300 | unsigned long wtimeout = msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1; |
c56c65fb TT |
301 | struct rdma_cm_id *id; |
302 | int rc; | |
303 | ||
b4744e00 CL |
304 | trace_xprtrdma_conn_start(xprt); |
305 | ||
1a954051 | 306 | init_completion(&ia->ri_done); |
bebd0318 | 307 | init_completion(&ia->ri_remove_done); |
1a954051 | 308 | |
fa20105e GS |
309 | id = rdma_create_id(&init_net, rpcrdma_conn_upcall, xprt, RDMA_PS_TCP, |
310 | IB_QPT_RC); | |
c56c65fb TT |
311 | if (IS_ERR(id)) { |
312 | rc = PTR_ERR(id); | |
313 | dprintk("RPC: %s: rdma_create_id() failed %i\n", | |
314 | __func__, rc); | |
315 | return id; | |
316 | } | |
317 | ||
5675add3 | 318 | ia->ri_async_rc = -ETIMEDOUT; |
dd229cee CL |
319 | rc = rdma_resolve_addr(id, NULL, |
320 | (struct sockaddr *)&xprt->rx_xprt.addr, | |
321 | RDMA_RESOLVE_TIMEOUT); | |
c56c65fb TT |
322 | if (rc) { |
323 | dprintk("RPC: %s: rdma_resolve_addr() failed %i\n", | |
324 | __func__, rc); | |
325 | goto out; | |
326 | } | |
109b88ab CL |
327 | rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout); |
328 | if (rc < 0) { | |
b4744e00 | 329 | trace_xprtrdma_conn_tout(xprt); |
109b88ab CL |
330 | goto out; |
331 | } | |
d0f36c46 | 332 | |
c56c65fb TT |
333 | rc = ia->ri_async_rc; |
334 | if (rc) | |
335 | goto out; | |
336 | ||
5675add3 | 337 | ia->ri_async_rc = -ETIMEDOUT; |
c56c65fb TT |
338 | rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT); |
339 | if (rc) { | |
340 | dprintk("RPC: %s: rdma_resolve_route() failed %i\n", | |
341 | __func__, rc); | |
56a6bd15 | 342 | goto out; |
c56c65fb | 343 | } |
109b88ab CL |
344 | rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout); |
345 | if (rc < 0) { | |
b4744e00 | 346 | trace_xprtrdma_conn_tout(xprt); |
56a6bd15 | 347 | goto out; |
109b88ab | 348 | } |
c56c65fb TT |
349 | rc = ia->ri_async_rc; |
350 | if (rc) | |
56a6bd15 | 351 | goto out; |
c56c65fb TT |
352 | |
353 | return id; | |
56a6bd15 | 354 | |
c56c65fb TT |
355 | out: |
356 | rdma_destroy_id(id); | |
357 | return ERR_PTR(rc); | |
358 | } | |
359 | ||
c56c65fb TT |
360 | /* |
361 | * Exported functions. | |
362 | */ | |
363 | ||
fff09594 CL |
364 | /** |
365 | * rpcrdma_ia_open - Open and initialize an Interface Adapter. | |
dd229cee | 366 | * @xprt: transport with IA to (re)initialize |
fff09594 CL |
367 | * |
368 | * Returns 0 on success, negative errno if an appropriate | |
369 | * Interface Adapter could not be found and opened. | |
c56c65fb TT |
370 | */ |
371 | int | |
dd229cee | 372 | rpcrdma_ia_open(struct rpcrdma_xprt *xprt) |
c56c65fb | 373 | { |
c56c65fb | 374 | struct rpcrdma_ia *ia = &xprt->rx_ia; |
d1ed857e CL |
375 | int rc; |
376 | ||
dd229cee | 377 | ia->ri_id = rpcrdma_create_id(xprt, ia); |
c56c65fb TT |
378 | if (IS_ERR(ia->ri_id)) { |
379 | rc = PTR_ERR(ia->ri_id); | |
fff09594 | 380 | goto out_err; |
c56c65fb | 381 | } |
89e0d112 | 382 | ia->ri_device = ia->ri_id->device; |
c56c65fb | 383 | |
ed082d36 | 384 | ia->ri_pd = ib_alloc_pd(ia->ri_device, 0); |
c56c65fb TT |
385 | if (IS_ERR(ia->ri_pd)) { |
386 | rc = PTR_ERR(ia->ri_pd); | |
b54054ca | 387 | pr_err("rpcrdma: ib_alloc_pd() returned %d\n", rc); |
fff09594 | 388 | goto out_err; |
c56c65fb TT |
389 | } |
390 | ||
fff09594 | 391 | switch (xprt_rdma_memreg_strategy) { |
ce5b3717 | 392 | case RPCRDMA_FRWR: |
b54054ca CL |
393 | if (frwr_is_supported(ia)) { |
394 | ia->ri_ops = &rpcrdma_frwr_memreg_ops; | |
395 | break; | |
396 | } | |
397 | /*FALLTHROUGH*/ | |
bd7ed1d1 | 398 | case RPCRDMA_MTHCAFMR: |
b54054ca CL |
399 | if (fmr_is_supported(ia)) { |
400 | ia->ri_ops = &rpcrdma_fmr_memreg_ops; | |
401 | break; | |
402 | } | |
403 | /*FALLTHROUGH*/ | |
bd7ed1d1 | 404 | default: |
fff09594 CL |
405 | pr_err("rpcrdma: Device %s does not support memreg mode %d\n", |
406 | ia->ri_device->name, xprt_rdma_memreg_strategy); | |
b54054ca | 407 | rc = -EINVAL; |
fff09594 | 408 | goto out_err; |
c56c65fb TT |
409 | } |
410 | ||
c56c65fb | 411 | return 0; |
5ae711a2 | 412 | |
fff09594 CL |
413 | out_err: |
414 | rpcrdma_ia_close(ia); | |
c56c65fb TT |
415 | return rc; |
416 | } | |
417 | ||
bebd0318 CL |
418 | /** |
419 | * rpcrdma_ia_remove - Handle device driver unload | |
420 | * @ia: interface adapter being removed | |
421 | * | |
422 | * Divest transport H/W resources associated with this adapter, | |
423 | * but allow it to be restored later. | |
424 | */ | |
425 | void | |
426 | rpcrdma_ia_remove(struct rpcrdma_ia *ia) | |
427 | { | |
428 | struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt, | |
429 | rx_ia); | |
430 | struct rpcrdma_ep *ep = &r_xprt->rx_ep; | |
431 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; | |
432 | struct rpcrdma_req *req; | |
433 | struct rpcrdma_rep *rep; | |
434 | ||
435 | cancel_delayed_work_sync(&buf->rb_refresh_worker); | |
436 | ||
437 | /* This is similar to rpcrdma_ep_destroy, but: | |
438 | * - Don't cancel the connect worker. | |
439 | * - Don't call rpcrdma_ep_disconnect, which waits | |
440 | * for another conn upcall, which will deadlock. | |
441 | * - rdma_disconnect is unneeded, the underlying | |
442 | * connection is already gone. | |
443 | */ | |
444 | if (ia->ri_id->qp) { | |
445 | ib_drain_qp(ia->ri_id->qp); | |
446 | rdma_destroy_qp(ia->ri_id); | |
447 | ia->ri_id->qp = NULL; | |
448 | } | |
449 | ib_free_cq(ep->rep_attr.recv_cq); | |
450 | ib_free_cq(ep->rep_attr.send_cq); | |
451 | ||
452 | /* The ULP is responsible for ensuring all DMA | |
453 | * mappings and MRs are gone. | |
454 | */ | |
455 | list_for_each_entry(rep, &buf->rb_recv_bufs, rr_list) | |
456 | rpcrdma_dma_unmap_regbuf(rep->rr_rdmabuf); | |
457 | list_for_each_entry(req, &buf->rb_allreqs, rl_all) { | |
458 | rpcrdma_dma_unmap_regbuf(req->rl_rdmabuf); | |
459 | rpcrdma_dma_unmap_regbuf(req->rl_sendbuf); | |
460 | rpcrdma_dma_unmap_regbuf(req->rl_recvbuf); | |
461 | } | |
96ceddea | 462 | rpcrdma_mrs_destroy(buf); |
bebd0318 CL |
463 | |
464 | /* Allow waiters to continue */ | |
465 | complete(&ia->ri_remove_done); | |
b4744e00 CL |
466 | |
467 | trace_xprtrdma_remove(r_xprt); | |
bebd0318 CL |
468 | } |
469 | ||
fff09594 CL |
470 | /** |
471 | * rpcrdma_ia_close - Clean up/close an IA. | |
472 | * @ia: interface adapter to close | |
473 | * | |
c56c65fb TT |
474 | */ |
475 | void | |
476 | rpcrdma_ia_close(struct rpcrdma_ia *ia) | |
477 | { | |
fee08caf TT |
478 | if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) { |
479 | if (ia->ri_id->qp) | |
480 | rdma_destroy_qp(ia->ri_id); | |
56a6bd15 | 481 | rdma_destroy_id(ia->ri_id); |
fee08caf | 482 | } |
fff09594 CL |
483 | ia->ri_id = NULL; |
484 | ia->ri_device = NULL; | |
6d44698d CL |
485 | |
486 | /* If the pd is still busy, xprtrdma missed freeing a resource */ | |
487 | if (ia->ri_pd && !IS_ERR(ia->ri_pd)) | |
7dd78647 | 488 | ib_dealloc_pd(ia->ri_pd); |
fff09594 | 489 | ia->ri_pd = NULL; |
c56c65fb TT |
490 | } |
491 | ||
492 | /* | |
493 | * Create unconnected endpoint. | |
494 | */ | |
495 | int | |
496 | rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia, | |
16f906d6 | 497 | struct rpcrdma_create_data_internal *cdata) |
c56c65fb | 498 | { |
87cfb9a0 | 499 | struct rpcrdma_connect_private *pmsg = &ep->rep_cm_private; |
16f906d6 | 500 | unsigned int max_qp_wr, max_sge; |
fc664485 | 501 | struct ib_cq *sendcq, *recvcq; |
2fa8f88d | 502 | int rc; |
c56c65fb | 503 | |
eed50879 CL |
504 | max_sge = min_t(unsigned int, ia->ri_device->attrs.max_sge, |
505 | RPCRDMA_MAX_SEND_SGES); | |
16f906d6 CL |
506 | if (max_sge < RPCRDMA_MIN_SEND_SGES) { |
507 | pr_warn("rpcrdma: HCA provides only %d send SGEs\n", max_sge); | |
b3221d6a CL |
508 | return -ENOMEM; |
509 | } | |
1179e2c2 | 510 | ia->ri_max_send_sges = max_sge; |
b3221d6a | 511 | |
e3e45b1b | 512 | if (ia->ri_device->attrs.max_qp_wr <= RPCRDMA_BACKWARD_WRS) { |
124fa17d CL |
513 | dprintk("RPC: %s: insufficient wqe's available\n", |
514 | __func__); | |
515 | return -ENOMEM; | |
516 | } | |
550d7502 | 517 | max_qp_wr = ia->ri_device->attrs.max_qp_wr - RPCRDMA_BACKWARD_WRS - 1; |
124fa17d | 518 | |
c56c65fb | 519 | /* check provider's send/recv wr limits */ |
124fa17d CL |
520 | if (cdata->max_requests > max_qp_wr) |
521 | cdata->max_requests = max_qp_wr; | |
c56c65fb TT |
522 | |
523 | ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall; | |
524 | ep->rep_attr.qp_context = ep; | |
c56c65fb TT |
525 | ep->rep_attr.srq = NULL; |
526 | ep->rep_attr.cap.max_send_wr = cdata->max_requests; | |
124fa17d | 527 | ep->rep_attr.cap.max_send_wr += RPCRDMA_BACKWARD_WRS; |
550d7502 | 528 | ep->rep_attr.cap.max_send_wr += 1; /* drain cqe */ |
3968cb58 CL |
529 | rc = ia->ri_ops->ro_open(ia, ep, cdata); |
530 | if (rc) | |
531 | return rc; | |
c56c65fb | 532 | ep->rep_attr.cap.max_recv_wr = cdata->max_requests; |
124fa17d | 533 | ep->rep_attr.cap.max_recv_wr += RPCRDMA_BACKWARD_WRS; |
550d7502 | 534 | ep->rep_attr.cap.max_recv_wr += 1; /* drain cqe */ |
16f906d6 | 535 | ep->rep_attr.cap.max_send_sge = max_sge; |
c56c65fb TT |
536 | ep->rep_attr.cap.max_recv_sge = 1; |
537 | ep->rep_attr.cap.max_inline_data = 0; | |
538 | ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR; | |
539 | ep->rep_attr.qp_type = IB_QPT_RC; | |
540 | ep->rep_attr.port_num = ~0; | |
541 | ||
542 | dprintk("RPC: %s: requested max: dtos: send %d recv %d; " | |
543 | "iovs: send %d recv %d\n", | |
544 | __func__, | |
545 | ep->rep_attr.cap.max_send_wr, | |
546 | ep->rep_attr.cap.max_recv_wr, | |
547 | ep->rep_attr.cap.max_send_sge, | |
548 | ep->rep_attr.cap.max_recv_sge); | |
549 | ||
550 | /* set trigger for requesting send completion */ | |
ae72950a CL |
551 | ep->rep_send_batch = min_t(unsigned int, RPCRDMA_MAX_SEND_BATCH, |
552 | cdata->max_requests >> 2); | |
553 | ep->rep_send_count = ep->rep_send_batch; | |
c56c65fb | 554 | init_waitqueue_head(&ep->rep_connect_wait); |
254f91e2 | 555 | INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker); |
c56c65fb | 556 | |
2fa8f88d CL |
557 | sendcq = ib_alloc_cq(ia->ri_device, NULL, |
558 | ep->rep_attr.cap.max_send_wr + 1, | |
a4699f56 | 559 | 1, IB_POLL_WORKQUEUE); |
fc664485 CL |
560 | if (IS_ERR(sendcq)) { |
561 | rc = PTR_ERR(sendcq); | |
562 | dprintk("RPC: %s: failed to create send CQ: %i\n", | |
c56c65fb TT |
563 | __func__, rc); |
564 | goto out1; | |
565 | } | |
566 | ||
552bf225 CL |
567 | recvcq = ib_alloc_cq(ia->ri_device, NULL, |
568 | ep->rep_attr.cap.max_recv_wr + 1, | |
d8f532d2 | 569 | 0, IB_POLL_WORKQUEUE); |
fc664485 CL |
570 | if (IS_ERR(recvcq)) { |
571 | rc = PTR_ERR(recvcq); | |
572 | dprintk("RPC: %s: failed to create recv CQ: %i\n", | |
573 | __func__, rc); | |
574 | goto out2; | |
575 | } | |
576 | ||
fc664485 CL |
577 | ep->rep_attr.send_cq = sendcq; |
578 | ep->rep_attr.recv_cq = recvcq; | |
c56c65fb TT |
579 | |
580 | /* Initialize cma parameters */ | |
b2dde94b | 581 | memset(&ep->rep_remote_cma, 0, sizeof(ep->rep_remote_cma)); |
c56c65fb | 582 | |
87cfb9a0 CL |
583 | /* Prepare RDMA-CM private message */ |
584 | pmsg->cp_magic = rpcrdma_cmp_magic; | |
585 | pmsg->cp_version = RPCRDMA_CMP_VERSION; | |
c8b920bb | 586 | pmsg->cp_flags |= ia->ri_ops->ro_send_w_inv_ok; |
87cfb9a0 CL |
587 | pmsg->cp_send_size = rpcrdma_encode_buffer_size(cdata->inline_wsize); |
588 | pmsg->cp_recv_size = rpcrdma_encode_buffer_size(cdata->inline_rsize); | |
589 | ep->rep_remote_cma.private_data = pmsg; | |
590 | ep->rep_remote_cma.private_data_len = sizeof(*pmsg); | |
c56c65fb TT |
591 | |
592 | /* Client offers RDMA Read but does not initiate */ | |
b334eaab | 593 | ep->rep_remote_cma.initiator_depth = 0; |
b7e85fff CL |
594 | ep->rep_remote_cma.responder_resources = |
595 | min_t(int, U8_MAX, ia->ri_device->attrs.max_qp_rd_atom); | |
c56c65fb | 596 | |
b2dde94b CL |
597 | /* Limit transport retries so client can detect server |
598 | * GID changes quickly. RPC layer handles re-establishing | |
599 | * transport connection and retransmission. | |
600 | */ | |
601 | ep->rep_remote_cma.retry_count = 6; | |
602 | ||
603 | /* RPC-over-RDMA handles its own flow control. In addition, | |
604 | * make all RNR NAKs visible so we know that RPC-over-RDMA | |
605 | * flow control is working correctly (no NAKs should be seen). | |
606 | */ | |
c56c65fb TT |
607 | ep->rep_remote_cma.flow_control = 0; |
608 | ep->rep_remote_cma.rnr_retry_count = 0; | |
609 | ||
610 | return 0; | |
611 | ||
612 | out2: | |
2fa8f88d | 613 | ib_free_cq(sendcq); |
c56c65fb TT |
614 | out1: |
615 | return rc; | |
616 | } | |
617 | ||
618 | /* | |
619 | * rpcrdma_ep_destroy | |
620 | * | |
621 | * Disconnect and destroy endpoint. After this, the only | |
622 | * valid operations on the ep are to free it (if dynamically | |
623 | * allocated) or re-create it. | |
c56c65fb | 624 | */ |
7f1d5419 | 625 | void |
c56c65fb TT |
626 | rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia) |
627 | { | |
254f91e2 CL |
628 | cancel_delayed_work_sync(&ep->rep_connect_worker); |
629 | ||
72c02173 | 630 | if (ia->ri_id->qp) { |
550d7502 | 631 | rpcrdma_ep_disconnect(ep, ia); |
fee08caf TT |
632 | rdma_destroy_qp(ia->ri_id); |
633 | ia->ri_id->qp = NULL; | |
c56c65fb TT |
634 | } |
635 | ||
552bf225 | 636 | ib_free_cq(ep->rep_attr.recv_cq); |
2fa8f88d | 637 | ib_free_cq(ep->rep_attr.send_cq); |
c56c65fb TT |
638 | } |
639 | ||
a9b0e381 CL |
640 | /* Re-establish a connection after a device removal event. |
641 | * Unlike a normal reconnection, a fresh PD and a new set | |
642 | * of MRs and buffers is needed. | |
643 | */ | |
644 | static int | |
645 | rpcrdma_ep_recreate_xprt(struct rpcrdma_xprt *r_xprt, | |
646 | struct rpcrdma_ep *ep, struct rpcrdma_ia *ia) | |
647 | { | |
a9b0e381 CL |
648 | int rc, err; |
649 | ||
b4744e00 | 650 | trace_xprtrdma_reinsert(r_xprt); |
a9b0e381 CL |
651 | |
652 | rc = -EHOSTUNREACH; | |
dd229cee | 653 | if (rpcrdma_ia_open(r_xprt)) |
a9b0e381 CL |
654 | goto out1; |
655 | ||
656 | rc = -ENOMEM; | |
657 | err = rpcrdma_ep_create(ep, ia, &r_xprt->rx_data); | |
658 | if (err) { | |
659 | pr_err("rpcrdma: rpcrdma_ep_create returned %d\n", err); | |
660 | goto out2; | |
661 | } | |
662 | ||
663 | rc = -ENETUNREACH; | |
664 | err = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr); | |
665 | if (err) { | |
666 | pr_err("rpcrdma: rdma_create_qp returned %d\n", err); | |
667 | goto out3; | |
668 | } | |
669 | ||
96ceddea | 670 | rpcrdma_mrs_create(r_xprt); |
a9b0e381 CL |
671 | return 0; |
672 | ||
673 | out3: | |
674 | rpcrdma_ep_destroy(ep, ia); | |
675 | out2: | |
676 | rpcrdma_ia_close(ia); | |
677 | out1: | |
678 | return rc; | |
679 | } | |
680 | ||
1890896b CL |
681 | static int |
682 | rpcrdma_ep_reconnect(struct rpcrdma_xprt *r_xprt, struct rpcrdma_ep *ep, | |
683 | struct rpcrdma_ia *ia) | |
684 | { | |
1890896b CL |
685 | struct rdma_cm_id *id, *old; |
686 | int err, rc; | |
687 | ||
b4744e00 | 688 | trace_xprtrdma_reconnect(r_xprt); |
1890896b CL |
689 | |
690 | rpcrdma_ep_disconnect(ep, ia); | |
691 | ||
692 | rc = -EHOSTUNREACH; | |
dd229cee | 693 | id = rpcrdma_create_id(r_xprt, ia); |
1890896b CL |
694 | if (IS_ERR(id)) |
695 | goto out; | |
696 | ||
697 | /* As long as the new ID points to the same device as the | |
698 | * old ID, we can reuse the transport's existing PD and all | |
699 | * previously allocated MRs. Also, the same device means | |
700 | * the transport's previous DMA mappings are still valid. | |
701 | * | |
702 | * This is a sanity check only. There should be no way these | |
703 | * point to two different devices here. | |
704 | */ | |
705 | old = id; | |
706 | rc = -ENETUNREACH; | |
707 | if (ia->ri_device != id->device) { | |
708 | pr_err("rpcrdma: can't reconnect on different device!\n"); | |
709 | goto out_destroy; | |
710 | } | |
711 | ||
712 | err = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr); | |
713 | if (err) { | |
714 | dprintk("RPC: %s: rdma_create_qp returned %d\n", | |
715 | __func__, err); | |
716 | goto out_destroy; | |
717 | } | |
718 | ||
719 | /* Atomically replace the transport's ID and QP. */ | |
720 | rc = 0; | |
721 | old = ia->ri_id; | |
722 | ia->ri_id = id; | |
723 | rdma_destroy_qp(old); | |
724 | ||
725 | out_destroy: | |
56a6bd15 | 726 | rdma_destroy_id(old); |
1890896b CL |
727 | out: |
728 | return rc; | |
729 | } | |
730 | ||
c56c65fb TT |
731 | /* |
732 | * Connect unconnected endpoint. | |
733 | */ | |
734 | int | |
735 | rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia) | |
736 | { | |
0a90487b CL |
737 | struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt, |
738 | rx_ia); | |
0a90487b | 739 | unsigned int extras; |
1890896b | 740 | int rc; |
c56c65fb | 741 | |
c56c65fb | 742 | retry: |
1890896b CL |
743 | switch (ep->rep_connected) { |
744 | case 0: | |
ec62f40d CL |
745 | dprintk("RPC: %s: connecting...\n", __func__); |
746 | rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr); | |
747 | if (rc) { | |
748 | dprintk("RPC: %s: rdma_create_qp failed %i\n", | |
749 | __func__, rc); | |
1890896b CL |
750 | rc = -ENETUNREACH; |
751 | goto out_noupdate; | |
ec62f40d | 752 | } |
1890896b | 753 | break; |
a9b0e381 CL |
754 | case -ENODEV: |
755 | rc = rpcrdma_ep_recreate_xprt(r_xprt, ep, ia); | |
756 | if (rc) | |
757 | goto out_noupdate; | |
758 | break; | |
1890896b CL |
759 | default: |
760 | rc = rpcrdma_ep_reconnect(r_xprt, ep, ia); | |
761 | if (rc) | |
762 | goto out; | |
c56c65fb TT |
763 | } |
764 | ||
c56c65fb TT |
765 | ep->rep_connected = 0; |
766 | ||
767 | rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma); | |
768 | if (rc) { | |
769 | dprintk("RPC: %s: rdma_connect() failed with %i\n", | |
770 | __func__, rc); | |
771 | goto out; | |
772 | } | |
773 | ||
c56c65fb | 774 | wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0); |
c56c65fb | 775 | if (ep->rep_connected <= 0) { |
0a90487b | 776 | if (ep->rep_connected == -EAGAIN) |
c56c65fb TT |
777 | goto retry; |
778 | rc = ep->rep_connected; | |
0a90487b | 779 | goto out; |
c56c65fb TT |
780 | } |
781 | ||
0a90487b CL |
782 | dprintk("RPC: %s: connected\n", __func__); |
783 | extras = r_xprt->rx_buf.rb_bc_srv_max_requests; | |
784 | if (extras) | |
785 | rpcrdma_ep_post_extra_recv(r_xprt, extras); | |
786 | ||
c56c65fb TT |
787 | out: |
788 | if (rc) | |
789 | ep->rep_connected = rc; | |
1890896b CL |
790 | |
791 | out_noupdate: | |
c56c65fb TT |
792 | return rc; |
793 | } | |
794 | ||
795 | /* | |
796 | * rpcrdma_ep_disconnect | |
797 | * | |
798 | * This is separate from destroy to facilitate the ability | |
799 | * to reconnect without recreating the endpoint. | |
800 | * | |
801 | * This call is not reentrant, and must not be made in parallel | |
802 | * on the same endpoint. | |
803 | */ | |
282191cb | 804 | void |
c56c65fb TT |
805 | rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia) |
806 | { | |
807 | int rc; | |
808 | ||
c56c65fb | 809 | rc = rdma_disconnect(ia->ri_id); |
b4744e00 | 810 | if (!rc) |
c56c65fb TT |
811 | /* returns without wait if not connected */ |
812 | wait_event_interruptible(ep->rep_connect_wait, | |
813 | ep->rep_connected != 1); | |
b4744e00 | 814 | else |
c56c65fb | 815 | ep->rep_connected = rc; |
b4744e00 CL |
816 | trace_xprtrdma_disconnect(container_of(ep, struct rpcrdma_xprt, |
817 | rx_ep), rc); | |
550d7502 CL |
818 | |
819 | ib_drain_qp(ia->ri_id->qp); | |
c56c65fb TT |
820 | } |
821 | ||
ae72950a CL |
822 | /* Fixed-size circular FIFO queue. This implementation is wait-free and |
823 | * lock-free. | |
824 | * | |
825 | * Consumer is the code path that posts Sends. This path dequeues a | |
826 | * sendctx for use by a Send operation. Multiple consumer threads | |
827 | * are serialized by the RPC transport lock, which allows only one | |
828 | * ->send_request call at a time. | |
829 | * | |
830 | * Producer is the code path that handles Send completions. This path | |
831 | * enqueues a sendctx that has been completed. Multiple producer | |
832 | * threads are serialized by the ib_poll_cq() function. | |
833 | */ | |
834 | ||
835 | /* rpcrdma_sendctxs_destroy() assumes caller has already quiesced | |
836 | * queue activity, and ib_drain_qp has flushed all remaining Send | |
837 | * requests. | |
838 | */ | |
839 | static void rpcrdma_sendctxs_destroy(struct rpcrdma_buffer *buf) | |
840 | { | |
841 | unsigned long i; | |
842 | ||
843 | for (i = 0; i <= buf->rb_sc_last; i++) | |
844 | kfree(buf->rb_sc_ctxs[i]); | |
845 | kfree(buf->rb_sc_ctxs); | |
846 | } | |
847 | ||
848 | static struct rpcrdma_sendctx *rpcrdma_sendctx_create(struct rpcrdma_ia *ia) | |
849 | { | |
850 | struct rpcrdma_sendctx *sc; | |
851 | ||
852 | sc = kzalloc(sizeof(*sc) + | |
853 | ia->ri_max_send_sges * sizeof(struct ib_sge), | |
854 | GFP_KERNEL); | |
855 | if (!sc) | |
856 | return NULL; | |
857 | ||
858 | sc->sc_wr.wr_cqe = &sc->sc_cqe; | |
859 | sc->sc_wr.sg_list = sc->sc_sges; | |
860 | sc->sc_wr.opcode = IB_WR_SEND; | |
861 | sc->sc_cqe.done = rpcrdma_wc_send; | |
862 | return sc; | |
863 | } | |
864 | ||
865 | static int rpcrdma_sendctxs_create(struct rpcrdma_xprt *r_xprt) | |
866 | { | |
867 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; | |
868 | struct rpcrdma_sendctx *sc; | |
869 | unsigned long i; | |
870 | ||
871 | /* Maximum number of concurrent outstanding Send WRs. Capping | |
872 | * the circular queue size stops Send Queue overflow by causing | |
873 | * the ->send_request call to fail temporarily before too many | |
874 | * Sends are posted. | |
875 | */ | |
876 | i = buf->rb_max_requests + RPCRDMA_MAX_BC_REQUESTS; | |
877 | dprintk("RPC: %s: allocating %lu send_ctxs\n", __func__, i); | |
878 | buf->rb_sc_ctxs = kcalloc(i, sizeof(sc), GFP_KERNEL); | |
879 | if (!buf->rb_sc_ctxs) | |
880 | return -ENOMEM; | |
881 | ||
882 | buf->rb_sc_last = i - 1; | |
883 | for (i = 0; i <= buf->rb_sc_last; i++) { | |
884 | sc = rpcrdma_sendctx_create(&r_xprt->rx_ia); | |
885 | if (!sc) | |
886 | goto out_destroy; | |
887 | ||
888 | sc->sc_xprt = r_xprt; | |
889 | buf->rb_sc_ctxs[i] = sc; | |
890 | } | |
891 | ||
892 | return 0; | |
893 | ||
894 | out_destroy: | |
895 | rpcrdma_sendctxs_destroy(buf); | |
896 | return -ENOMEM; | |
897 | } | |
898 | ||
899 | /* The sendctx queue is not guaranteed to have a size that is a | |
900 | * power of two, thus the helpers in circ_buf.h cannot be used. | |
901 | * The other option is to use modulus (%), which can be expensive. | |
902 | */ | |
903 | static unsigned long rpcrdma_sendctx_next(struct rpcrdma_buffer *buf, | |
904 | unsigned long item) | |
905 | { | |
906 | return likely(item < buf->rb_sc_last) ? item + 1 : 0; | |
907 | } | |
908 | ||
909 | /** | |
910 | * rpcrdma_sendctx_get_locked - Acquire a send context | |
911 | * @buf: transport buffers from which to acquire an unused context | |
912 | * | |
913 | * Returns pointer to a free send completion context; or NULL if | |
914 | * the queue is empty. | |
915 | * | |
916 | * Usage: Called to acquire an SGE array before preparing a Send WR. | |
917 | * | |
918 | * The caller serializes calls to this function (per rpcrdma_buffer), | |
919 | * and provides an effective memory barrier that flushes the new value | |
920 | * of rb_sc_head. | |
921 | */ | |
922 | struct rpcrdma_sendctx *rpcrdma_sendctx_get_locked(struct rpcrdma_buffer *buf) | |
923 | { | |
924 | struct rpcrdma_xprt *r_xprt; | |
925 | struct rpcrdma_sendctx *sc; | |
926 | unsigned long next_head; | |
927 | ||
928 | next_head = rpcrdma_sendctx_next(buf, buf->rb_sc_head); | |
929 | ||
930 | if (next_head == READ_ONCE(buf->rb_sc_tail)) | |
931 | goto out_emptyq; | |
932 | ||
933 | /* ORDER: item must be accessed _before_ head is updated */ | |
934 | sc = buf->rb_sc_ctxs[next_head]; | |
935 | ||
936 | /* Releasing the lock in the caller acts as a memory | |
937 | * barrier that flushes rb_sc_head. | |
938 | */ | |
939 | buf->rb_sc_head = next_head; | |
940 | ||
941 | return sc; | |
942 | ||
943 | out_emptyq: | |
944 | /* The queue is "empty" if there have not been enough Send | |
945 | * completions recently. This is a sign the Send Queue is | |
946 | * backing up. Cause the caller to pause and try again. | |
947 | */ | |
948 | dprintk("RPC: %s: empty sendctx queue\n", __func__); | |
949 | r_xprt = container_of(buf, struct rpcrdma_xprt, rx_buf); | |
950 | r_xprt->rx_stats.empty_sendctx_q++; | |
951 | return NULL; | |
952 | } | |
953 | ||
954 | /** | |
955 | * rpcrdma_sendctx_put_locked - Release a send context | |
956 | * @sc: send context to release | |
957 | * | |
958 | * Usage: Called from Send completion to return a sendctxt | |
959 | * to the queue. | |
960 | * | |
961 | * The caller serializes calls to this function (per rpcrdma_buffer). | |
962 | */ | |
963 | void rpcrdma_sendctx_put_locked(struct rpcrdma_sendctx *sc) | |
964 | { | |
965 | struct rpcrdma_buffer *buf = &sc->sc_xprt->rx_buf; | |
966 | unsigned long next_tail; | |
967 | ||
968 | /* Unmap SGEs of previously completed by unsignaled | |
969 | * Sends by walking up the queue until @sc is found. | |
970 | */ | |
971 | next_tail = buf->rb_sc_tail; | |
972 | do { | |
973 | next_tail = rpcrdma_sendctx_next(buf, next_tail); | |
974 | ||
975 | /* ORDER: item must be accessed _before_ tail is updated */ | |
976 | rpcrdma_unmap_sendctx(buf->rb_sc_ctxs[next_tail]); | |
977 | ||
978 | } while (buf->rb_sc_ctxs[next_tail] != sc); | |
979 | ||
980 | /* Paired with READ_ONCE */ | |
981 | smp_store_release(&buf->rb_sc_tail, next_tail); | |
982 | } | |
983 | ||
505bbe64 CL |
984 | static void |
985 | rpcrdma_mr_recovery_worker(struct work_struct *work) | |
986 | { | |
987 | struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer, | |
988 | rb_recovery_worker.work); | |
96ceddea | 989 | struct rpcrdma_mr *mr; |
505bbe64 CL |
990 | |
991 | spin_lock(&buf->rb_recovery_lock); | |
992 | while (!list_empty(&buf->rb_stale_mrs)) { | |
96ceddea | 993 | mr = rpcrdma_mr_pop(&buf->rb_stale_mrs); |
505bbe64 CL |
994 | spin_unlock(&buf->rb_recovery_lock); |
995 | ||
1c443eff | 996 | trace_xprtrdma_recover_mr(mr); |
96ceddea | 997 | mr->mr_xprt->rx_ia.ri_ops->ro_recover_mr(mr); |
505bbe64 CL |
998 | |
999 | spin_lock(&buf->rb_recovery_lock); | |
53d78523 | 1000 | } |
505bbe64 CL |
1001 | spin_unlock(&buf->rb_recovery_lock); |
1002 | } | |
1003 | ||
1004 | void | |
96ceddea | 1005 | rpcrdma_mr_defer_recovery(struct rpcrdma_mr *mr) |
505bbe64 | 1006 | { |
96ceddea | 1007 | struct rpcrdma_xprt *r_xprt = mr->mr_xprt; |
505bbe64 CL |
1008 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; |
1009 | ||
1010 | spin_lock(&buf->rb_recovery_lock); | |
96ceddea | 1011 | rpcrdma_mr_push(mr, &buf->rb_stale_mrs); |
505bbe64 CL |
1012 | spin_unlock(&buf->rb_recovery_lock); |
1013 | ||
1014 | schedule_delayed_work(&buf->rb_recovery_worker, 0); | |
1015 | } | |
1016 | ||
e2ac236c | 1017 | static void |
96ceddea | 1018 | rpcrdma_mrs_create(struct rpcrdma_xprt *r_xprt) |
e2ac236c CL |
1019 | { |
1020 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; | |
1021 | struct rpcrdma_ia *ia = &r_xprt->rx_ia; | |
1022 | unsigned int count; | |
1023 | LIST_HEAD(free); | |
1024 | LIST_HEAD(all); | |
1025 | ||
ae741a85 | 1026 | for (count = 0; count < 3; count++) { |
96ceddea | 1027 | struct rpcrdma_mr *mr; |
e2ac236c CL |
1028 | int rc; |
1029 | ||
96ceddea CL |
1030 | mr = kzalloc(sizeof(*mr), GFP_KERNEL); |
1031 | if (!mr) | |
e2ac236c CL |
1032 | break; |
1033 | ||
96ceddea | 1034 | rc = ia->ri_ops->ro_init_mr(ia, mr); |
e2ac236c | 1035 | if (rc) { |
96ceddea | 1036 | kfree(mr); |
e2ac236c CL |
1037 | break; |
1038 | } | |
1039 | ||
96ceddea | 1040 | mr->mr_xprt = r_xprt; |
e2ac236c | 1041 | |
96ceddea CL |
1042 | list_add(&mr->mr_list, &free); |
1043 | list_add(&mr->mr_all, &all); | |
e2ac236c CL |
1044 | } |
1045 | ||
96ceddea CL |
1046 | spin_lock(&buf->rb_mrlock); |
1047 | list_splice(&free, &buf->rb_mrs); | |
e2ac236c CL |
1048 | list_splice(&all, &buf->rb_all); |
1049 | r_xprt->rx_stats.mrs_allocated += count; | |
96ceddea | 1050 | spin_unlock(&buf->rb_mrlock); |
1c443eff | 1051 | trace_xprtrdma_createmrs(r_xprt, count); |
9e679d5e CL |
1052 | |
1053 | xprt_write_space(&r_xprt->rx_xprt); | |
e2ac236c CL |
1054 | } |
1055 | ||
1056 | static void | |
1057 | rpcrdma_mr_refresh_worker(struct work_struct *work) | |
1058 | { | |
1059 | struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer, | |
1060 | rb_refresh_worker.work); | |
1061 | struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt, | |
1062 | rx_buf); | |
1063 | ||
96ceddea | 1064 | rpcrdma_mrs_create(r_xprt); |
e2ac236c CL |
1065 | } |
1066 | ||
f531a5db | 1067 | struct rpcrdma_req * |
1392402c CL |
1068 | rpcrdma_create_req(struct rpcrdma_xprt *r_xprt) |
1069 | { | |
f531a5db | 1070 | struct rpcrdma_buffer *buffer = &r_xprt->rx_buf; |
2dd4a012 | 1071 | struct rpcrdma_regbuf *rb; |
1392402c | 1072 | struct rpcrdma_req *req; |
1392402c | 1073 | |
85275c87 | 1074 | req = kzalloc(sizeof(*req), GFP_KERNEL); |
1392402c | 1075 | if (req == NULL) |
85275c87 | 1076 | return ERR_PTR(-ENOMEM); |
1392402c | 1077 | |
2dd4a012 CL |
1078 | rb = rpcrdma_alloc_regbuf(RPCRDMA_HDRBUF_SIZE, |
1079 | DMA_TO_DEVICE, GFP_KERNEL); | |
1080 | if (IS_ERR(rb)) { | |
1081 | kfree(req); | |
1082 | return ERR_PTR(-ENOMEM); | |
1083 | } | |
1084 | req->rl_rdmabuf = rb; | |
1085 | xdr_buf_init(&req->rl_hdrbuf, rb->rg_base, rdmab_length(rb)); | |
1086 | req->rl_buffer = buffer; | |
1087 | INIT_LIST_HEAD(&req->rl_registered); | |
1088 | ||
f531a5db CL |
1089 | spin_lock(&buffer->rb_reqslock); |
1090 | list_add(&req->rl_all, &buffer->rb_allreqs); | |
1091 | spin_unlock(&buffer->rb_reqslock); | |
1392402c | 1092 | return req; |
1392402c CL |
1093 | } |
1094 | ||
d698c4a0 CL |
1095 | /** |
1096 | * rpcrdma_create_rep - Allocate an rpcrdma_rep object | |
1097 | * @r_xprt: controlling transport | |
1098 | * | |
1099 | * Returns 0 on success or a negative errno on failure. | |
1100 | */ | |
1101 | int | |
1392402c CL |
1102 | rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt) |
1103 | { | |
1104 | struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data; | |
d698c4a0 | 1105 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; |
1392402c CL |
1106 | struct rpcrdma_rep *rep; |
1107 | int rc; | |
1108 | ||
1109 | rc = -ENOMEM; | |
6b1184cd | 1110 | rep = kzalloc(sizeof(*rep), GFP_KERNEL); |
1392402c CL |
1111 | if (rep == NULL) |
1112 | goto out; | |
1392402c | 1113 | |
13650c23 | 1114 | rep->rr_rdmabuf = rpcrdma_alloc_regbuf(cdata->inline_rsize, |
99ef4db3 | 1115 | DMA_FROM_DEVICE, GFP_KERNEL); |
6b1184cd CL |
1116 | if (IS_ERR(rep->rr_rdmabuf)) { |
1117 | rc = PTR_ERR(rep->rr_rdmabuf); | |
1392402c | 1118 | goto out_free; |
6b1184cd | 1119 | } |
96f8778f CL |
1120 | xdr_buf_init(&rep->rr_hdrbuf, rep->rr_rdmabuf->rg_base, |
1121 | rdmab_length(rep->rr_rdmabuf)); | |
1392402c | 1122 | |
1519e969 | 1123 | rep->rr_cqe.done = rpcrdma_wc_receive; |
fed171b3 | 1124 | rep->rr_rxprt = r_xprt; |
d8f532d2 | 1125 | INIT_WORK(&rep->rr_work, rpcrdma_deferred_completion); |
6ea8e711 CL |
1126 | rep->rr_recv_wr.next = NULL; |
1127 | rep->rr_recv_wr.wr_cqe = &rep->rr_cqe; | |
1128 | rep->rr_recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov; | |
1129 | rep->rr_recv_wr.num_sge = 1; | |
d698c4a0 CL |
1130 | |
1131 | spin_lock(&buf->rb_lock); | |
1132 | list_add(&rep->rr_list, &buf->rb_recv_bufs); | |
1133 | spin_unlock(&buf->rb_lock); | |
1134 | return 0; | |
1392402c CL |
1135 | |
1136 | out_free: | |
1137 | kfree(rep); | |
1138 | out: | |
d698c4a0 CL |
1139 | dprintk("RPC: %s: reply buffer %d alloc failed\n", |
1140 | __func__, rc); | |
1141 | return rc; | |
1392402c CL |
1142 | } |
1143 | ||
c56c65fb | 1144 | int |
ac920d04 | 1145 | rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt) |
c56c65fb | 1146 | { |
ac920d04 | 1147 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; |
c56c65fb TT |
1148 | int i, rc; |
1149 | ||
1e465fd4 | 1150 | buf->rb_max_requests = r_xprt->rx_data.max_requests; |
f531a5db | 1151 | buf->rb_bc_srv_max_requests = 0; |
96ceddea | 1152 | spin_lock_init(&buf->rb_mrlock); |
505bbe64 CL |
1153 | spin_lock_init(&buf->rb_lock); |
1154 | spin_lock_init(&buf->rb_recovery_lock); | |
96ceddea | 1155 | INIT_LIST_HEAD(&buf->rb_mrs); |
e2ac236c | 1156 | INIT_LIST_HEAD(&buf->rb_all); |
505bbe64 | 1157 | INIT_LIST_HEAD(&buf->rb_stale_mrs); |
e2ac236c CL |
1158 | INIT_DELAYED_WORK(&buf->rb_refresh_worker, |
1159 | rpcrdma_mr_refresh_worker); | |
505bbe64 CL |
1160 | INIT_DELAYED_WORK(&buf->rb_recovery_worker, |
1161 | rpcrdma_mr_recovery_worker); | |
c56c65fb | 1162 | |
96ceddea | 1163 | rpcrdma_mrs_create(r_xprt); |
c56c65fb | 1164 | |
1e465fd4 | 1165 | INIT_LIST_HEAD(&buf->rb_send_bufs); |
f531a5db CL |
1166 | INIT_LIST_HEAD(&buf->rb_allreqs); |
1167 | spin_lock_init(&buf->rb_reqslock); | |
c56c65fb TT |
1168 | for (i = 0; i < buf->rb_max_requests; i++) { |
1169 | struct rpcrdma_req *req; | |
c56c65fb | 1170 | |
1392402c CL |
1171 | req = rpcrdma_create_req(r_xprt); |
1172 | if (IS_ERR(req)) { | |
c56c65fb TT |
1173 | dprintk("RPC: %s: request buffer %d alloc" |
1174 | " failed\n", __func__, i); | |
1392402c | 1175 | rc = PTR_ERR(req); |
c56c65fb TT |
1176 | goto out; |
1177 | } | |
a80d66c9 | 1178 | list_add(&req->rl_list, &buf->rb_send_bufs); |
1e465fd4 CL |
1179 | } |
1180 | ||
1181 | INIT_LIST_HEAD(&buf->rb_recv_bufs); | |
d698c4a0 CL |
1182 | for (i = 0; i <= buf->rb_max_requests; i++) { |
1183 | rc = rpcrdma_create_rep(r_xprt); | |
1184 | if (rc) | |
c56c65fb | 1185 | goto out; |
c56c65fb | 1186 | } |
1392402c | 1187 | |
ae72950a CL |
1188 | rc = rpcrdma_sendctxs_create(r_xprt); |
1189 | if (rc) | |
1190 | goto out; | |
1191 | ||
c56c65fb TT |
1192 | return 0; |
1193 | out: | |
1194 | rpcrdma_buffer_destroy(buf); | |
1195 | return rc; | |
1196 | } | |
1197 | ||
1e465fd4 CL |
1198 | static struct rpcrdma_req * |
1199 | rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf) | |
1200 | { | |
1201 | struct rpcrdma_req *req; | |
1202 | ||
1203 | req = list_first_entry(&buf->rb_send_bufs, | |
a80d66c9 | 1204 | struct rpcrdma_req, rl_list); |
431af645 | 1205 | list_del_init(&req->rl_list); |
1e465fd4 CL |
1206 | return req; |
1207 | } | |
1208 | ||
1209 | static struct rpcrdma_rep * | |
1210 | rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf) | |
1211 | { | |
1212 | struct rpcrdma_rep *rep; | |
1213 | ||
1214 | rep = list_first_entry(&buf->rb_recv_bufs, | |
1215 | struct rpcrdma_rep, rr_list); | |
1216 | list_del(&rep->rr_list); | |
1217 | return rep; | |
1218 | } | |
1219 | ||
1392402c | 1220 | static void |
13650c23 | 1221 | rpcrdma_destroy_rep(struct rpcrdma_rep *rep) |
1392402c | 1222 | { |
13650c23 | 1223 | rpcrdma_free_regbuf(rep->rr_rdmabuf); |
1392402c CL |
1224 | kfree(rep); |
1225 | } | |
1226 | ||
f531a5db | 1227 | void |
13650c23 | 1228 | rpcrdma_destroy_req(struct rpcrdma_req *req) |
1392402c | 1229 | { |
13650c23 CL |
1230 | rpcrdma_free_regbuf(req->rl_recvbuf); |
1231 | rpcrdma_free_regbuf(req->rl_sendbuf); | |
1232 | rpcrdma_free_regbuf(req->rl_rdmabuf); | |
1392402c CL |
1233 | kfree(req); |
1234 | } | |
1235 | ||
e2ac236c | 1236 | static void |
96ceddea | 1237 | rpcrdma_mrs_destroy(struct rpcrdma_buffer *buf) |
e2ac236c CL |
1238 | { |
1239 | struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt, | |
1240 | rx_buf); | |
1241 | struct rpcrdma_ia *ia = rdmab_to_ia(buf); | |
96ceddea | 1242 | struct rpcrdma_mr *mr; |
e2ac236c CL |
1243 | unsigned int count; |
1244 | ||
1245 | count = 0; | |
96ceddea | 1246 | spin_lock(&buf->rb_mrlock); |
e2ac236c | 1247 | while (!list_empty(&buf->rb_all)) { |
96ceddea CL |
1248 | mr = list_entry(buf->rb_all.next, struct rpcrdma_mr, mr_all); |
1249 | list_del(&mr->mr_all); | |
e2ac236c | 1250 | |
96ceddea CL |
1251 | spin_unlock(&buf->rb_mrlock); |
1252 | ia->ri_ops->ro_release_mr(mr); | |
e2ac236c | 1253 | count++; |
96ceddea | 1254 | spin_lock(&buf->rb_mrlock); |
e2ac236c | 1255 | } |
96ceddea | 1256 | spin_unlock(&buf->rb_mrlock); |
e2ac236c CL |
1257 | r_xprt->rx_stats.mrs_allocated = 0; |
1258 | ||
1259 | dprintk("RPC: %s: released %u MRs\n", __func__, count); | |
1260 | } | |
1261 | ||
c56c65fb TT |
1262 | void |
1263 | rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf) | |
1264 | { | |
505bbe64 | 1265 | cancel_delayed_work_sync(&buf->rb_recovery_worker); |
9378b274 | 1266 | cancel_delayed_work_sync(&buf->rb_refresh_worker); |
505bbe64 | 1267 | |
ae72950a CL |
1268 | rpcrdma_sendctxs_destroy(buf); |
1269 | ||
1e465fd4 CL |
1270 | while (!list_empty(&buf->rb_recv_bufs)) { |
1271 | struct rpcrdma_rep *rep; | |
c56c65fb | 1272 | |
1e465fd4 | 1273 | rep = rpcrdma_buffer_get_rep_locked(buf); |
13650c23 | 1274 | rpcrdma_destroy_rep(rep); |
c56c65fb | 1275 | } |
05c97466 | 1276 | buf->rb_send_count = 0; |
c56c65fb | 1277 | |
f531a5db CL |
1278 | spin_lock(&buf->rb_reqslock); |
1279 | while (!list_empty(&buf->rb_allreqs)) { | |
1e465fd4 | 1280 | struct rpcrdma_req *req; |
4034ba04 | 1281 | |
f531a5db CL |
1282 | req = list_first_entry(&buf->rb_allreqs, |
1283 | struct rpcrdma_req, rl_all); | |
1284 | list_del(&req->rl_all); | |
1285 | ||
1286 | spin_unlock(&buf->rb_reqslock); | |
13650c23 | 1287 | rpcrdma_destroy_req(req); |
f531a5db | 1288 | spin_lock(&buf->rb_reqslock); |
1e465fd4 | 1289 | } |
f531a5db | 1290 | spin_unlock(&buf->rb_reqslock); |
05c97466 | 1291 | buf->rb_recv_count = 0; |
4034ba04 | 1292 | |
96ceddea | 1293 | rpcrdma_mrs_destroy(buf); |
c56c65fb TT |
1294 | } |
1295 | ||
96ceddea CL |
1296 | /** |
1297 | * rpcrdma_mr_get - Allocate an rpcrdma_mr object | |
1298 | * @r_xprt: controlling transport | |
1299 | * | |
1300 | * Returns an initialized rpcrdma_mr or NULL if no free | |
1301 | * rpcrdma_mr objects are available. | |
1302 | */ | |
1303 | struct rpcrdma_mr * | |
1304 | rpcrdma_mr_get(struct rpcrdma_xprt *r_xprt) | |
c2922c02 | 1305 | { |
346aa66b | 1306 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; |
96ceddea | 1307 | struct rpcrdma_mr *mr = NULL; |
346aa66b | 1308 | |
96ceddea CL |
1309 | spin_lock(&buf->rb_mrlock); |
1310 | if (!list_empty(&buf->rb_mrs)) | |
1311 | mr = rpcrdma_mr_pop(&buf->rb_mrs); | |
1312 | spin_unlock(&buf->rb_mrlock); | |
346aa66b | 1313 | |
96ceddea CL |
1314 | if (!mr) |
1315 | goto out_nomrs; | |
1316 | return mr; | |
e2ac236c | 1317 | |
96ceddea | 1318 | out_nomrs: |
1c443eff | 1319 | trace_xprtrdma_nomrs(r_xprt); |
bebd0318 CL |
1320 | if (r_xprt->rx_ep.rep_connected != -ENODEV) |
1321 | schedule_delayed_work(&buf->rb_refresh_worker, 0); | |
e2ac236c CL |
1322 | |
1323 | /* Allow the reply handler and refresh worker to run */ | |
1324 | cond_resched(); | |
1325 | ||
1326 | return NULL; | |
c2922c02 CL |
1327 | } |
1328 | ||
ec12e479 CL |
1329 | static void |
1330 | __rpcrdma_mr_put(struct rpcrdma_buffer *buf, struct rpcrdma_mr *mr) | |
1331 | { | |
1332 | spin_lock(&buf->rb_mrlock); | |
1333 | rpcrdma_mr_push(mr, &buf->rb_mrs); | |
1334 | spin_unlock(&buf->rb_mrlock); | |
1335 | } | |
1336 | ||
96ceddea CL |
1337 | /** |
1338 | * rpcrdma_mr_put - Release an rpcrdma_mr object | |
1339 | * @mr: object to release | |
1340 | * | |
1341 | */ | |
346aa66b | 1342 | void |
96ceddea | 1343 | rpcrdma_mr_put(struct rpcrdma_mr *mr) |
ec12e479 CL |
1344 | { |
1345 | __rpcrdma_mr_put(&mr->mr_xprt->rx_buf, mr); | |
1346 | } | |
1347 | ||
1348 | /** | |
1349 | * rpcrdma_mr_unmap_and_put - DMA unmap an MR and release it | |
1350 | * @mr: object to release | |
1351 | * | |
1352 | */ | |
1353 | void | |
1354 | rpcrdma_mr_unmap_and_put(struct rpcrdma_mr *mr) | |
c2922c02 | 1355 | { |
96ceddea | 1356 | struct rpcrdma_xprt *r_xprt = mr->mr_xprt; |
c2922c02 | 1357 | |
2937fede | 1358 | trace_xprtrdma_dma_unmap(mr); |
ec12e479 CL |
1359 | ib_dma_unmap_sg(r_xprt->rx_ia.ri_device, |
1360 | mr->mr_sg, mr->mr_nents, mr->mr_dir); | |
1361 | __rpcrdma_mr_put(&r_xprt->rx_buf, mr); | |
c2922c02 CL |
1362 | } |
1363 | ||
05c97466 CL |
1364 | static struct rpcrdma_rep * |
1365 | rpcrdma_buffer_get_rep(struct rpcrdma_buffer *buffers) | |
1366 | { | |
1367 | /* If an RPC previously completed without a reply (say, a | |
1368 | * credential problem or a soft timeout occurs) then hold off | |
1369 | * on supplying more Receive buffers until the number of new | |
1370 | * pending RPCs catches up to the number of posted Receives. | |
1371 | */ | |
1372 | if (unlikely(buffers->rb_send_count < buffers->rb_recv_count)) | |
1373 | return NULL; | |
1374 | ||
1375 | if (unlikely(list_empty(&buffers->rb_recv_bufs))) | |
1376 | return NULL; | |
1377 | buffers->rb_recv_count++; | |
1378 | return rpcrdma_buffer_get_rep_locked(buffers); | |
1379 | } | |
1380 | ||
c56c65fb TT |
1381 | /* |
1382 | * Get a set of request/reply buffers. | |
78d506e1 CL |
1383 | * |
1384 | * Reply buffer (if available) is attached to send buffer upon return. | |
c56c65fb TT |
1385 | */ |
1386 | struct rpcrdma_req * | |
1387 | rpcrdma_buffer_get(struct rpcrdma_buffer *buffers) | |
1388 | { | |
1389 | struct rpcrdma_req *req; | |
c14d86e5 | 1390 | |
a5b027e1 | 1391 | spin_lock(&buffers->rb_lock); |
1e465fd4 CL |
1392 | if (list_empty(&buffers->rb_send_bufs)) |
1393 | goto out_reqbuf; | |
05c97466 | 1394 | buffers->rb_send_count++; |
1e465fd4 | 1395 | req = rpcrdma_buffer_get_req_locked(buffers); |
05c97466 | 1396 | req->rl_reply = rpcrdma_buffer_get_rep(buffers); |
a5b027e1 | 1397 | spin_unlock(&buffers->rb_lock); |
ae724676 | 1398 | |
1e465fd4 | 1399 | return req; |
ddb6bebc | 1400 | |
1e465fd4 | 1401 | out_reqbuf: |
a5b027e1 | 1402 | spin_unlock(&buffers->rb_lock); |
1e465fd4 | 1403 | return NULL; |
c56c65fb TT |
1404 | } |
1405 | ||
1406 | /* | |
1407 | * Put request/reply buffers back into pool. | |
1408 | * Pre-decrement counter/array index. | |
1409 | */ | |
1410 | void | |
1411 | rpcrdma_buffer_put(struct rpcrdma_req *req) | |
1412 | { | |
1413 | struct rpcrdma_buffer *buffers = req->rl_buffer; | |
1e465fd4 | 1414 | struct rpcrdma_rep *rep = req->rl_reply; |
c56c65fb | 1415 | |
1e465fd4 CL |
1416 | req->rl_reply = NULL; |
1417 | ||
a5b027e1 | 1418 | spin_lock(&buffers->rb_lock); |
05c97466 | 1419 | buffers->rb_send_count--; |
a80d66c9 | 1420 | list_add_tail(&req->rl_list, &buffers->rb_send_bufs); |
05c97466 CL |
1421 | if (rep) { |
1422 | buffers->rb_recv_count--; | |
1e465fd4 | 1423 | list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs); |
05c97466 | 1424 | } |
a5b027e1 | 1425 | spin_unlock(&buffers->rb_lock); |
c56c65fb TT |
1426 | } |
1427 | ||
1428 | /* | |
1429 | * Recover reply buffers from pool. | |
1e465fd4 | 1430 | * This happens when recovering from disconnect. |
c56c65fb TT |
1431 | */ |
1432 | void | |
1433 | rpcrdma_recv_buffer_get(struct rpcrdma_req *req) | |
1434 | { | |
1435 | struct rpcrdma_buffer *buffers = req->rl_buffer; | |
c56c65fb | 1436 | |
a5b027e1 | 1437 | spin_lock(&buffers->rb_lock); |
05c97466 | 1438 | req->rl_reply = rpcrdma_buffer_get_rep(buffers); |
a5b027e1 | 1439 | spin_unlock(&buffers->rb_lock); |
c56c65fb TT |
1440 | } |
1441 | ||
1442 | /* | |
1443 | * Put reply buffers back into pool when not attached to | |
b45ccfd2 | 1444 | * request. This happens in error conditions. |
c56c65fb TT |
1445 | */ |
1446 | void | |
1447 | rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep) | |
1448 | { | |
fed171b3 | 1449 | struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf; |
c56c65fb | 1450 | |
a5b027e1 | 1451 | spin_lock(&buffers->rb_lock); |
05c97466 | 1452 | buffers->rb_recv_count--; |
1e465fd4 | 1453 | list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs); |
a5b027e1 | 1454 | spin_unlock(&buffers->rb_lock); |
c56c65fb TT |
1455 | } |
1456 | ||
9128c3e7 | 1457 | /** |
99ef4db3 | 1458 | * rpcrdma_alloc_regbuf - allocate and DMA-map memory for SEND/RECV buffers |
9128c3e7 | 1459 | * @size: size of buffer to be allocated, in bytes |
99ef4db3 | 1460 | * @direction: direction of data movement |
9128c3e7 CL |
1461 | * @flags: GFP flags |
1462 | * | |
54cbd6b0 CL |
1463 | * Returns an ERR_PTR, or a pointer to a regbuf, a buffer that |
1464 | * can be persistently DMA-mapped for I/O. | |
9128c3e7 CL |
1465 | * |
1466 | * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for | |
99ef4db3 CL |
1467 | * receiving the payload of RDMA RECV operations. During Long Calls |
1468 | * or Replies they may be registered externally via ro_map. | |
9128c3e7 CL |
1469 | */ |
1470 | struct rpcrdma_regbuf * | |
13650c23 CL |
1471 | rpcrdma_alloc_regbuf(size_t size, enum dma_data_direction direction, |
1472 | gfp_t flags) | |
9128c3e7 CL |
1473 | { |
1474 | struct rpcrdma_regbuf *rb; | |
9128c3e7 | 1475 | |
9128c3e7 CL |
1476 | rb = kmalloc(sizeof(*rb) + size, flags); |
1477 | if (rb == NULL) | |
54cbd6b0 | 1478 | return ERR_PTR(-ENOMEM); |
9128c3e7 | 1479 | |
54cbd6b0 | 1480 | rb->rg_device = NULL; |
99ef4db3 | 1481 | rb->rg_direction = direction; |
54cbd6b0 | 1482 | rb->rg_iov.length = size; |
9128c3e7 CL |
1483 | |
1484 | return rb; | |
54cbd6b0 | 1485 | } |
9128c3e7 | 1486 | |
54cbd6b0 CL |
1487 | /** |
1488 | * __rpcrdma_map_regbuf - DMA-map a regbuf | |
1489 | * @ia: controlling rpcrdma_ia | |
1490 | * @rb: regbuf to be mapped | |
1491 | */ | |
1492 | bool | |
1493 | __rpcrdma_dma_map_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb) | |
1494 | { | |
91a10c52 CL |
1495 | struct ib_device *device = ia->ri_device; |
1496 | ||
54cbd6b0 CL |
1497 | if (rb->rg_direction == DMA_NONE) |
1498 | return false; | |
1499 | ||
91a10c52 | 1500 | rb->rg_iov.addr = ib_dma_map_single(device, |
54cbd6b0 CL |
1501 | (void *)rb->rg_base, |
1502 | rdmab_length(rb), | |
1503 | rb->rg_direction); | |
91a10c52 | 1504 | if (ib_dma_mapping_error(device, rdmab_addr(rb))) |
54cbd6b0 CL |
1505 | return false; |
1506 | ||
91a10c52 | 1507 | rb->rg_device = device; |
54cbd6b0 CL |
1508 | rb->rg_iov.lkey = ia->ri_pd->local_dma_lkey; |
1509 | return true; | |
1510 | } | |
1511 | ||
1512 | static void | |
1513 | rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb) | |
1514 | { | |
e89e8d8f CL |
1515 | if (!rb) |
1516 | return; | |
1517 | ||
54cbd6b0 CL |
1518 | if (!rpcrdma_regbuf_is_mapped(rb)) |
1519 | return; | |
1520 | ||
1521 | ib_dma_unmap_single(rb->rg_device, rdmab_addr(rb), | |
1522 | rdmab_length(rb), rb->rg_direction); | |
1523 | rb->rg_device = NULL; | |
9128c3e7 CL |
1524 | } |
1525 | ||
1526 | /** | |
1527 | * rpcrdma_free_regbuf - deregister and free registered buffer | |
9128c3e7 CL |
1528 | * @rb: regbuf to be deregistered and freed |
1529 | */ | |
1530 | void | |
13650c23 | 1531 | rpcrdma_free_regbuf(struct rpcrdma_regbuf *rb) |
9128c3e7 | 1532 | { |
54cbd6b0 | 1533 | rpcrdma_dma_unmap_regbuf(rb); |
e531dcab | 1534 | kfree(rb); |
9128c3e7 CL |
1535 | } |
1536 | ||
c56c65fb TT |
1537 | /* |
1538 | * Prepost any receive buffer, then post send. | |
1539 | * | |
1540 | * Receive buffer is donated to hardware, reclaimed upon recv completion. | |
1541 | */ | |
1542 | int | |
1543 | rpcrdma_ep_post(struct rpcrdma_ia *ia, | |
1544 | struct rpcrdma_ep *ep, | |
1545 | struct rpcrdma_req *req) | |
1546 | { | |
ae72950a | 1547 | struct ib_send_wr *send_wr = &req->rl_sendctx->sc_wr; |
655fec69 | 1548 | int rc; |
c56c65fb | 1549 | |
90aab602 CL |
1550 | if (req->rl_reply) { |
1551 | rc = rpcrdma_ep_post_recv(ia, req->rl_reply); | |
c56c65fb | 1552 | if (rc) |
7a89f9c6 | 1553 | return rc; |
c56c65fb TT |
1554 | req->rl_reply = NULL; |
1555 | } | |
1556 | ||
01bb35c8 CL |
1557 | if (!ep->rep_send_count || |
1558 | test_bit(RPCRDMA_REQ_F_TX_RESOURCES, &req->rl_flags)) { | |
ae72950a CL |
1559 | send_wr->send_flags |= IB_SEND_SIGNALED; |
1560 | ep->rep_send_count = ep->rep_send_batch; | |
1561 | } else { | |
1562 | send_wr->send_flags &= ~IB_SEND_SIGNALED; | |
1563 | --ep->rep_send_count; | |
1564 | } | |
ab03eff5 | 1565 | |
f2877623 | 1566 | rc = ia->ri_ops->ro_send(ia, req); |
ab03eff5 | 1567 | trace_xprtrdma_post_send(req, rc); |
c56c65fb | 1568 | if (rc) |
ab03eff5 | 1569 | return -ENOTCONN; |
7a89f9c6 | 1570 | return 0; |
c56c65fb TT |
1571 | } |
1572 | ||
c56c65fb TT |
1573 | int |
1574 | rpcrdma_ep_post_recv(struct rpcrdma_ia *ia, | |
c56c65fb TT |
1575 | struct rpcrdma_rep *rep) |
1576 | { | |
6ea8e711 | 1577 | struct ib_recv_wr *recv_wr_fail; |
c56c65fb TT |
1578 | int rc; |
1579 | ||
54cbd6b0 CL |
1580 | if (!rpcrdma_dma_map_regbuf(ia, rep->rr_rdmabuf)) |
1581 | goto out_map; | |
6ea8e711 | 1582 | rc = ib_post_recv(ia->ri_id->qp, &rep->rr_recv_wr, &recv_wr_fail); |
b4a7f91c | 1583 | trace_xprtrdma_post_recv(rep, rc); |
c56c65fb | 1584 | if (rc) |
b4a7f91c | 1585 | return -ENOTCONN; |
7a89f9c6 CL |
1586 | return 0; |
1587 | ||
54cbd6b0 CL |
1588 | out_map: |
1589 | pr_err("rpcrdma: failed to DMA map the Receive buffer\n"); | |
1590 | return -EIO; | |
c56c65fb | 1591 | } |
43e95988 | 1592 | |
f531a5db CL |
1593 | /** |
1594 | * rpcrdma_ep_post_extra_recv - Post buffers for incoming backchannel requests | |
1595 | * @r_xprt: transport associated with these backchannel resources | |
9ab6d89e | 1596 | * @count: minimum number of incoming requests expected |
f531a5db CL |
1597 | * |
1598 | * Returns zero if all requested buffers were posted, or a negative errno. | |
1599 | */ | |
1600 | int | |
1601 | rpcrdma_ep_post_extra_recv(struct rpcrdma_xprt *r_xprt, unsigned int count) | |
1602 | { | |
1603 | struct rpcrdma_buffer *buffers = &r_xprt->rx_buf; | |
1604 | struct rpcrdma_ia *ia = &r_xprt->rx_ia; | |
f531a5db | 1605 | struct rpcrdma_rep *rep; |
f531a5db CL |
1606 | int rc; |
1607 | ||
1608 | while (count--) { | |
9b06688b | 1609 | spin_lock(&buffers->rb_lock); |
f531a5db CL |
1610 | if (list_empty(&buffers->rb_recv_bufs)) |
1611 | goto out_reqbuf; | |
1612 | rep = rpcrdma_buffer_get_rep_locked(buffers); | |
9b06688b | 1613 | spin_unlock(&buffers->rb_lock); |
f531a5db | 1614 | |
b157380a | 1615 | rc = rpcrdma_ep_post_recv(ia, rep); |
f531a5db CL |
1616 | if (rc) |
1617 | goto out_rc; | |
1618 | } | |
1619 | ||
1620 | return 0; | |
1621 | ||
1622 | out_reqbuf: | |
9b06688b | 1623 | spin_unlock(&buffers->rb_lock); |
ae724676 | 1624 | trace_xprtrdma_noreps(r_xprt); |
f531a5db CL |
1625 | return -ENOMEM; |
1626 | ||
1627 | out_rc: | |
1628 | rpcrdma_recv_buffer_put(rep); | |
1629 | return rc; | |
1630 | } |