Commit | Line | Data |
---|---|---|
f58851e6 | 1 | /* |
c56c65fb TT |
2 | * 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. | |
f58851e6 TT |
38 | */ |
39 | ||
c56c65fb TT |
40 | /* |
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 | ||
a6b7a407 | 50 | #include <linux/interrupt.h> |
5a0e3ad6 | 51 | #include <linux/slab.h> |
eba8ff66 | 52 | #include <linux/prefetch.h> |
0dd39cae | 53 | #include <linux/sunrpc/addr.h> |
05c97466 | 54 | #include <linux/sunrpc/svc_rdma.h> |
65866f82 | 55 | #include <asm/bitops.h> |
d0f36c46 | 56 | #include <linux/module.h> /* try_module_get()/module_put() */ |
c56c65fb | 57 | |
f58851e6 TT |
58 | #include "xprt_rdma.h" |
59 | ||
c56c65fb TT |
60 | /* |
61 | * Globals/Macros | |
62 | */ | |
63 | ||
f895b252 | 64 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) |
c56c65fb TT |
65 | # define RPCDBG_FACILITY RPCDBG_TRANS |
66 | #endif | |
67 | ||
68 | /* | |
69 | * internal functions | |
70 | */ | |
71 | ||
fe97b47c | 72 | static struct workqueue_struct *rpcrdma_receive_wq; |
c56c65fb | 73 | |
fe97b47c CL |
74 | int |
75 | rpcrdma_alloc_wq(void) | |
c56c65fb | 76 | { |
fe97b47c | 77 | struct workqueue_struct *recv_wq; |
c56c65fb | 78 | |
fe97b47c CL |
79 | recv_wq = alloc_workqueue("xprtrdma_receive", |
80 | WQ_MEM_RECLAIM | WQ_UNBOUND | WQ_HIGHPRI, | |
81 | 0); | |
82 | if (!recv_wq) | |
83 | return -ENOMEM; | |
c56c65fb | 84 | |
fe97b47c CL |
85 | rpcrdma_receive_wq = recv_wq; |
86 | return 0; | |
c56c65fb TT |
87 | } |
88 | ||
fe97b47c CL |
89 | void |
90 | rpcrdma_destroy_wq(void) | |
f1a03b76 | 91 | { |
fe97b47c | 92 | struct workqueue_struct *wq; |
f1a03b76 | 93 | |
fe97b47c CL |
94 | if (rpcrdma_receive_wq) { |
95 | wq = rpcrdma_receive_wq; | |
96 | rpcrdma_receive_wq = NULL; | |
97 | destroy_workqueue(wq); | |
98 | } | |
f1a03b76 CL |
99 | } |
100 | ||
c56c65fb TT |
101 | static void |
102 | rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context) | |
103 | { | |
104 | struct rpcrdma_ep *ep = context; | |
105 | ||
7ff11de1 | 106 | pr_err("RPC: %s: %s on device %s ep %p\n", |
76357c71 | 107 | __func__, ib_event_msg(event->event), |
7ff11de1 | 108 | event->device->name, context); |
c56c65fb TT |
109 | if (ep->rep_connected == 1) { |
110 | ep->rep_connected = -EIO; | |
afadc468 | 111 | rpcrdma_conn_func(ep); |
c56c65fb TT |
112 | wake_up_all(&ep->rep_connect_wait); |
113 | } | |
114 | } | |
115 | ||
2fa8f88d CL |
116 | /** |
117 | * rpcrdma_wc_send - Invoked by RDMA provider for each polled Send WC | |
118 | * @cq: completion queue (ignored) | |
119 | * @wc: completed WR | |
120 | * | |
fc664485 CL |
121 | */ |
122 | static void | |
2fa8f88d | 123 | rpcrdma_wc_send(struct ib_cq *cq, struct ib_wc *wc) |
fc664485 | 124 | { |
2fa8f88d CL |
125 | /* WARNING: Only wr_cqe and status are reliable at this point */ |
126 | if (wc->status != IB_WC_SUCCESS && wc->status != IB_WC_WR_FLUSH_ERR) | |
127 | pr_err("rpcrdma: Send: %s (%u/0x%x)\n", | |
128 | ib_wc_status_msg(wc->status), | |
129 | wc->status, wc->vendor_err); | |
fc664485 | 130 | } |
c56c65fb | 131 | |
fc664485 | 132 | static void |
fe97b47c CL |
133 | rpcrdma_receive_worker(struct work_struct *work) |
134 | { | |
135 | struct rpcrdma_rep *rep = | |
136 | container_of(work, struct rpcrdma_rep, rr_work); | |
fc664485 | 137 | |
fe97b47c | 138 | rpcrdma_reply_handler(rep); |
fc664485 CL |
139 | } |
140 | ||
23826c7a CL |
141 | /* Perform basic sanity checking to avoid using garbage |
142 | * to update the credit grant value. | |
143 | */ | |
144 | static void | |
145 | rpcrdma_update_granted_credits(struct rpcrdma_rep *rep) | |
146 | { | |
147 | struct rpcrdma_msg *rmsgp = rdmab_to_msg(rep->rr_rdmabuf); | |
148 | struct rpcrdma_buffer *buffer = &rep->rr_rxprt->rx_buf; | |
149 | u32 credits; | |
150 | ||
151 | if (rep->rr_len < RPCRDMA_HDRLEN_ERR) | |
152 | return; | |
153 | ||
154 | credits = be32_to_cpu(rmsgp->rm_credit); | |
155 | if (credits == 0) | |
156 | credits = 1; /* don't deadlock */ | |
157 | else if (credits > buffer->rb_max_requests) | |
158 | credits = buffer->rb_max_requests; | |
159 | ||
160 | atomic_set(&buffer->rb_credits, credits); | |
161 | } | |
162 | ||
552bf225 CL |
163 | /** |
164 | * rpcrdma_receive_wc - Invoked by RDMA provider for each polled Receive WC | |
165 | * @cq: completion queue (ignored) | |
166 | * @wc: completed WR | |
167 | * | |
168 | */ | |
fc664485 | 169 | static void |
552bf225 | 170 | rpcrdma_receive_wc(struct ib_cq *cq, struct ib_wc *wc) |
fc664485 | 171 | { |
552bf225 CL |
172 | struct ib_cqe *cqe = wc->wr_cqe; |
173 | struct rpcrdma_rep *rep = container_of(cqe, struct rpcrdma_rep, | |
174 | rr_cqe); | |
fc664485 | 175 | |
8502427c CL |
176 | /* WARNING: Only wr_id and status are reliable at this point */ |
177 | if (wc->status != IB_WC_SUCCESS) | |
178 | goto out_fail; | |
fc664485 | 179 | |
8502427c | 180 | /* status == SUCCESS means all fields in wc are trustworthy */ |
fc664485 CL |
181 | if (wc->opcode != IB_WC_RECV) |
182 | return; | |
183 | ||
8502427c CL |
184 | dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n", |
185 | __func__, rep, wc->byte_len); | |
186 | ||
fc664485 | 187 | rep->rr_len = wc->byte_len; |
89e0d112 | 188 | ib_dma_sync_single_for_cpu(rep->rr_device, |
6b1184cd CL |
189 | rdmab_addr(rep->rr_rdmabuf), |
190 | rep->rr_len, DMA_FROM_DEVICE); | |
23826c7a CL |
191 | |
192 | rpcrdma_update_granted_credits(rep); | |
fc664485 CL |
193 | |
194 | out_schedule: | |
fe97b47c | 195 | queue_work(rpcrdma_receive_wq, &rep->rr_work); |
8502427c | 196 | return; |
fe97b47c | 197 | |
8502427c CL |
198 | out_fail: |
199 | if (wc->status != IB_WC_WR_FLUSH_ERR) | |
552bf225 CL |
200 | pr_err("rpcrdma: Recv: %s (%u/0x%x)\n", |
201 | ib_wc_status_msg(wc->status), | |
202 | wc->status, wc->vendor_err); | |
b0e178a2 | 203 | rep->rr_len = RPCRDMA_BAD_LEN; |
8502427c | 204 | goto out_schedule; |
fc664485 CL |
205 | } |
206 | ||
c56c65fb TT |
207 | static int |
208 | rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event) | |
209 | { | |
210 | struct rpcrdma_xprt *xprt = id->context; | |
211 | struct rpcrdma_ia *ia = &xprt->rx_ia; | |
212 | struct rpcrdma_ep *ep = &xprt->rx_ep; | |
f895b252 | 213 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) |
0dd39cae | 214 | struct sockaddr *sap = (struct sockaddr *)&ep->rep_remote_addr; |
ff0db049 | 215 | #endif |
ce1ab9ab CL |
216 | struct ib_qp_attr *attr = &ia->ri_qp_attr; |
217 | struct ib_qp_init_attr *iattr = &ia->ri_qp_init_attr; | |
c56c65fb TT |
218 | int connstate = 0; |
219 | ||
220 | switch (event->event) { | |
221 | case RDMA_CM_EVENT_ADDR_RESOLVED: | |
222 | case RDMA_CM_EVENT_ROUTE_RESOLVED: | |
5675add3 | 223 | ia->ri_async_rc = 0; |
c56c65fb TT |
224 | complete(&ia->ri_done); |
225 | break; | |
226 | case RDMA_CM_EVENT_ADDR_ERROR: | |
227 | ia->ri_async_rc = -EHOSTUNREACH; | |
228 | dprintk("RPC: %s: CM address resolution error, ep 0x%p\n", | |
229 | __func__, ep); | |
230 | complete(&ia->ri_done); | |
231 | break; | |
232 | case RDMA_CM_EVENT_ROUTE_ERROR: | |
233 | ia->ri_async_rc = -ENETUNREACH; | |
234 | dprintk("RPC: %s: CM route resolution error, ep 0x%p\n", | |
235 | __func__, ep); | |
236 | complete(&ia->ri_done); | |
237 | break; | |
238 | case RDMA_CM_EVENT_ESTABLISHED: | |
239 | connstate = 1; | |
ce1ab9ab CL |
240 | ib_query_qp(ia->ri_id->qp, attr, |
241 | IB_QP_MAX_QP_RD_ATOMIC | IB_QP_MAX_DEST_RD_ATOMIC, | |
242 | iattr); | |
c56c65fb TT |
243 | dprintk("RPC: %s: %d responder resources" |
244 | " (%d initiator)\n", | |
ce1ab9ab CL |
245 | __func__, attr->max_dest_rd_atomic, |
246 | attr->max_rd_atomic); | |
c56c65fb TT |
247 | goto connected; |
248 | case RDMA_CM_EVENT_CONNECT_ERROR: | |
249 | connstate = -ENOTCONN; | |
250 | goto connected; | |
251 | case RDMA_CM_EVENT_UNREACHABLE: | |
252 | connstate = -ENETDOWN; | |
253 | goto connected; | |
254 | case RDMA_CM_EVENT_REJECTED: | |
255 | connstate = -ECONNREFUSED; | |
256 | goto connected; | |
257 | case RDMA_CM_EVENT_DISCONNECTED: | |
258 | connstate = -ECONNABORTED; | |
259 | goto connected; | |
260 | case RDMA_CM_EVENT_DEVICE_REMOVAL: | |
261 | connstate = -ENODEV; | |
262 | connected: | |
c56c65fb TT |
263 | dprintk("RPC: %s: %sconnected\n", |
264 | __func__, connstate > 0 ? "" : "dis"); | |
23826c7a | 265 | atomic_set(&xprt->rx_buf.rb_credits, 1); |
c56c65fb | 266 | ep->rep_connected = connstate; |
afadc468 | 267 | rpcrdma_conn_func(ep); |
c56c65fb | 268 | wake_up_all(&ep->rep_connect_wait); |
8079fb78 | 269 | /*FALLTHROUGH*/ |
c56c65fb | 270 | default: |
0dd39cae CL |
271 | dprintk("RPC: %s: %pIS:%u (ep 0x%p): %s\n", |
272 | __func__, sap, rpc_get_port(sap), ep, | |
76357c71 | 273 | rdma_event_msg(event->event)); |
c56c65fb TT |
274 | break; |
275 | } | |
276 | ||
f895b252 | 277 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) |
b3cd8d45 | 278 | if (connstate == 1) { |
ce1ab9ab | 279 | int ird = attr->max_dest_rd_atomic; |
b3cd8d45 | 280 | int tird = ep->rep_remote_cma.responder_resources; |
0dd39cae | 281 | |
a0ce85f5 | 282 | pr_info("rpcrdma: connection to %pIS:%u on %s, memreg '%s', %d credits, %d responders%s\n", |
0dd39cae | 283 | sap, rpc_get_port(sap), |
89e0d112 | 284 | ia->ri_device->name, |
a0ce85f5 | 285 | ia->ri_ops->ro_displayname, |
b3cd8d45 TT |
286 | xprt->rx_buf.rb_max_requests, |
287 | ird, ird < 4 && ird < tird / 2 ? " (low!)" : ""); | |
288 | } else if (connstate < 0) { | |
0dd39cae CL |
289 | pr_info("rpcrdma: connection to %pIS:%u closed (%d)\n", |
290 | sap, rpc_get_port(sap), connstate); | |
b3cd8d45 TT |
291 | } |
292 | #endif | |
293 | ||
c56c65fb TT |
294 | return 0; |
295 | } | |
296 | ||
d0f36c46 DS |
297 | static void rpcrdma_destroy_id(struct rdma_cm_id *id) |
298 | { | |
299 | if (id) { | |
300 | module_put(id->device->owner); | |
301 | rdma_destroy_id(id); | |
302 | } | |
303 | } | |
304 | ||
c56c65fb TT |
305 | static struct rdma_cm_id * |
306 | rpcrdma_create_id(struct rpcrdma_xprt *xprt, | |
307 | struct rpcrdma_ia *ia, struct sockaddr *addr) | |
308 | { | |
309 | struct rdma_cm_id *id; | |
310 | int rc; | |
311 | ||
1a954051 TT |
312 | init_completion(&ia->ri_done); |
313 | ||
fa20105e GS |
314 | id = rdma_create_id(&init_net, rpcrdma_conn_upcall, xprt, RDMA_PS_TCP, |
315 | IB_QPT_RC); | |
c56c65fb TT |
316 | if (IS_ERR(id)) { |
317 | rc = PTR_ERR(id); | |
318 | dprintk("RPC: %s: rdma_create_id() failed %i\n", | |
319 | __func__, rc); | |
320 | return id; | |
321 | } | |
322 | ||
5675add3 | 323 | ia->ri_async_rc = -ETIMEDOUT; |
c56c65fb TT |
324 | rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT); |
325 | if (rc) { | |
326 | dprintk("RPC: %s: rdma_resolve_addr() failed %i\n", | |
327 | __func__, rc); | |
328 | goto out; | |
329 | } | |
5675add3 TT |
330 | wait_for_completion_interruptible_timeout(&ia->ri_done, |
331 | msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1); | |
d0f36c46 DS |
332 | |
333 | /* FIXME: | |
334 | * Until xprtrdma supports DEVICE_REMOVAL, the provider must | |
335 | * be pinned while there are active NFS/RDMA mounts to prevent | |
336 | * hangs and crashes at umount time. | |
337 | */ | |
338 | if (!ia->ri_async_rc && !try_module_get(id->device->owner)) { | |
339 | dprintk("RPC: %s: Failed to get device module\n", | |
340 | __func__); | |
341 | ia->ri_async_rc = -ENODEV; | |
342 | } | |
c56c65fb TT |
343 | rc = ia->ri_async_rc; |
344 | if (rc) | |
345 | goto out; | |
346 | ||
5675add3 | 347 | ia->ri_async_rc = -ETIMEDOUT; |
c56c65fb TT |
348 | rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT); |
349 | if (rc) { | |
350 | dprintk("RPC: %s: rdma_resolve_route() failed %i\n", | |
351 | __func__, rc); | |
d0f36c46 | 352 | goto put; |
c56c65fb | 353 | } |
5675add3 TT |
354 | wait_for_completion_interruptible_timeout(&ia->ri_done, |
355 | msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1); | |
c56c65fb TT |
356 | rc = ia->ri_async_rc; |
357 | if (rc) | |
d0f36c46 | 358 | goto put; |
c56c65fb TT |
359 | |
360 | return id; | |
d0f36c46 DS |
361 | put: |
362 | module_put(id->device->owner); | |
c56c65fb TT |
363 | out: |
364 | rdma_destroy_id(id); | |
365 | return ERR_PTR(rc); | |
366 | } | |
367 | ||
c56c65fb TT |
368 | /* |
369 | * Exported functions. | |
370 | */ | |
371 | ||
372 | /* | |
373 | * Open and initialize an Interface Adapter. | |
374 | * o initializes fields of struct rpcrdma_ia, including | |
375 | * interface and provider attributes and protection zone. | |
376 | */ | |
377 | int | |
378 | rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr, int memreg) | |
379 | { | |
c56c65fb | 380 | struct rpcrdma_ia *ia = &xprt->rx_ia; |
d1ed857e CL |
381 | int rc; |
382 | ||
c56c65fb TT |
383 | ia->ri_id = rpcrdma_create_id(xprt, ia, addr); |
384 | if (IS_ERR(ia->ri_id)) { | |
385 | rc = PTR_ERR(ia->ri_id); | |
386 | goto out1; | |
387 | } | |
89e0d112 | 388 | ia->ri_device = ia->ri_id->device; |
c56c65fb | 389 | |
89e0d112 | 390 | ia->ri_pd = ib_alloc_pd(ia->ri_device); |
c56c65fb TT |
391 | if (IS_ERR(ia->ri_pd)) { |
392 | rc = PTR_ERR(ia->ri_pd); | |
b54054ca | 393 | pr_err("rpcrdma: ib_alloc_pd() returned %d\n", rc); |
c56c65fb TT |
394 | goto out2; |
395 | } | |
396 | ||
bd7ed1d1 | 397 | switch (memreg) { |
3197d309 | 398 | case RPCRDMA_FRMR: |
b54054ca CL |
399 | if (frwr_is_supported(ia)) { |
400 | ia->ri_ops = &rpcrdma_frwr_memreg_ops; | |
401 | break; | |
402 | } | |
403 | /*FALLTHROUGH*/ | |
bd7ed1d1 | 404 | case RPCRDMA_MTHCAFMR: |
b54054ca CL |
405 | if (fmr_is_supported(ia)) { |
406 | ia->ri_ops = &rpcrdma_fmr_memreg_ops; | |
407 | break; | |
408 | } | |
409 | /*FALLTHROUGH*/ | |
bd7ed1d1 | 410 | default: |
b54054ca CL |
411 | pr_err("rpcrdma: Unsupported memory registration mode: %d\n", |
412 | memreg); | |
413 | rc = -EINVAL; | |
5ae711a2 | 414 | goto out3; |
c56c65fb TT |
415 | } |
416 | ||
c56c65fb | 417 | return 0; |
5ae711a2 CL |
418 | |
419 | out3: | |
420 | ib_dealloc_pd(ia->ri_pd); | |
421 | ia->ri_pd = NULL; | |
c56c65fb | 422 | out2: |
d0f36c46 | 423 | rpcrdma_destroy_id(ia->ri_id); |
fee08caf | 424 | ia->ri_id = NULL; |
c56c65fb TT |
425 | out1: |
426 | return rc; | |
427 | } | |
428 | ||
429 | /* | |
430 | * Clean up/close an IA. | |
431 | * o if event handles and PD have been initialized, free them. | |
432 | * o close the IA | |
433 | */ | |
434 | void | |
435 | rpcrdma_ia_close(struct rpcrdma_ia *ia) | |
436 | { | |
c56c65fb | 437 | dprintk("RPC: %s: entering\n", __func__); |
fee08caf TT |
438 | if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) { |
439 | if (ia->ri_id->qp) | |
440 | rdma_destroy_qp(ia->ri_id); | |
d0f36c46 | 441 | rpcrdma_destroy_id(ia->ri_id); |
fee08caf TT |
442 | ia->ri_id = NULL; |
443 | } | |
6d44698d CL |
444 | |
445 | /* If the pd is still busy, xprtrdma missed freeing a resource */ | |
446 | if (ia->ri_pd && !IS_ERR(ia->ri_pd)) | |
7dd78647 | 447 | ib_dealloc_pd(ia->ri_pd); |
c56c65fb TT |
448 | } |
449 | ||
450 | /* | |
451 | * Create unconnected endpoint. | |
452 | */ | |
453 | int | |
454 | rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia, | |
455 | struct rpcrdma_create_data_internal *cdata) | |
456 | { | |
fc664485 | 457 | struct ib_cq *sendcq, *recvcq; |
124fa17d | 458 | unsigned int max_qp_wr; |
2fa8f88d | 459 | int rc; |
c56c65fb | 460 | |
e3e45b1b | 461 | if (ia->ri_device->attrs.max_sge < RPCRDMA_MAX_IOVS) { |
b3221d6a CL |
462 | dprintk("RPC: %s: insufficient sge's available\n", |
463 | __func__); | |
464 | return -ENOMEM; | |
465 | } | |
466 | ||
e3e45b1b | 467 | if (ia->ri_device->attrs.max_qp_wr <= RPCRDMA_BACKWARD_WRS) { |
124fa17d CL |
468 | dprintk("RPC: %s: insufficient wqe's available\n", |
469 | __func__); | |
470 | return -ENOMEM; | |
471 | } | |
550d7502 | 472 | max_qp_wr = ia->ri_device->attrs.max_qp_wr - RPCRDMA_BACKWARD_WRS - 1; |
124fa17d | 473 | |
c56c65fb | 474 | /* check provider's send/recv wr limits */ |
124fa17d CL |
475 | if (cdata->max_requests > max_qp_wr) |
476 | cdata->max_requests = max_qp_wr; | |
c56c65fb TT |
477 | |
478 | ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall; | |
479 | ep->rep_attr.qp_context = ep; | |
c56c65fb TT |
480 | ep->rep_attr.srq = NULL; |
481 | ep->rep_attr.cap.max_send_wr = cdata->max_requests; | |
124fa17d | 482 | ep->rep_attr.cap.max_send_wr += RPCRDMA_BACKWARD_WRS; |
550d7502 | 483 | ep->rep_attr.cap.max_send_wr += 1; /* drain cqe */ |
3968cb58 CL |
484 | rc = ia->ri_ops->ro_open(ia, ep, cdata); |
485 | if (rc) | |
486 | return rc; | |
c56c65fb | 487 | ep->rep_attr.cap.max_recv_wr = cdata->max_requests; |
124fa17d | 488 | ep->rep_attr.cap.max_recv_wr += RPCRDMA_BACKWARD_WRS; |
550d7502 | 489 | ep->rep_attr.cap.max_recv_wr += 1; /* drain cqe */ |
b3221d6a | 490 | ep->rep_attr.cap.max_send_sge = RPCRDMA_MAX_IOVS; |
c56c65fb TT |
491 | ep->rep_attr.cap.max_recv_sge = 1; |
492 | ep->rep_attr.cap.max_inline_data = 0; | |
493 | ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR; | |
494 | ep->rep_attr.qp_type = IB_QPT_RC; | |
495 | ep->rep_attr.port_num = ~0; | |
496 | ||
497 | dprintk("RPC: %s: requested max: dtos: send %d recv %d; " | |
498 | "iovs: send %d recv %d\n", | |
499 | __func__, | |
500 | ep->rep_attr.cap.max_send_wr, | |
501 | ep->rep_attr.cap.max_recv_wr, | |
502 | ep->rep_attr.cap.max_send_sge, | |
503 | ep->rep_attr.cap.max_recv_sge); | |
504 | ||
505 | /* set trigger for requesting send completion */ | |
fc664485 | 506 | ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1; |
26ae9d1c CL |
507 | if (ep->rep_cqinit <= 2) |
508 | ep->rep_cqinit = 0; /* always signal? */ | |
c56c65fb | 509 | INIT_CQCOUNT(ep); |
c56c65fb | 510 | init_waitqueue_head(&ep->rep_connect_wait); |
254f91e2 | 511 | INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker); |
c56c65fb | 512 | |
2fa8f88d CL |
513 | sendcq = ib_alloc_cq(ia->ri_device, NULL, |
514 | ep->rep_attr.cap.max_send_wr + 1, | |
515 | 0, IB_POLL_SOFTIRQ); | |
fc664485 CL |
516 | if (IS_ERR(sendcq)) { |
517 | rc = PTR_ERR(sendcq); | |
518 | dprintk("RPC: %s: failed to create send CQ: %i\n", | |
c56c65fb TT |
519 | __func__, rc); |
520 | goto out1; | |
521 | } | |
522 | ||
552bf225 CL |
523 | recvcq = ib_alloc_cq(ia->ri_device, NULL, |
524 | ep->rep_attr.cap.max_recv_wr + 1, | |
525 | 0, IB_POLL_SOFTIRQ); | |
fc664485 CL |
526 | if (IS_ERR(recvcq)) { |
527 | rc = PTR_ERR(recvcq); | |
528 | dprintk("RPC: %s: failed to create recv CQ: %i\n", | |
529 | __func__, rc); | |
530 | goto out2; | |
531 | } | |
532 | ||
fc664485 CL |
533 | ep->rep_attr.send_cq = sendcq; |
534 | ep->rep_attr.recv_cq = recvcq; | |
c56c65fb TT |
535 | |
536 | /* Initialize cma parameters */ | |
b2dde94b | 537 | memset(&ep->rep_remote_cma, 0, sizeof(ep->rep_remote_cma)); |
c56c65fb TT |
538 | |
539 | /* RPC/RDMA does not use private data */ | |
540 | ep->rep_remote_cma.private_data = NULL; | |
541 | ep->rep_remote_cma.private_data_len = 0; | |
542 | ||
543 | /* Client offers RDMA Read but does not initiate */ | |
b334eaab | 544 | ep->rep_remote_cma.initiator_depth = 0; |
e3e45b1b | 545 | if (ia->ri_device->attrs.max_qp_rd_atom > 32) /* arbitrary but <= 255 */ |
b334eaab TT |
546 | ep->rep_remote_cma.responder_resources = 32; |
547 | else | |
7bc7972c | 548 | ep->rep_remote_cma.responder_resources = |
e3e45b1b | 549 | ia->ri_device->attrs.max_qp_rd_atom; |
c56c65fb | 550 | |
b2dde94b CL |
551 | /* Limit transport retries so client can detect server |
552 | * GID changes quickly. RPC layer handles re-establishing | |
553 | * transport connection and retransmission. | |
554 | */ | |
555 | ep->rep_remote_cma.retry_count = 6; | |
556 | ||
557 | /* RPC-over-RDMA handles its own flow control. In addition, | |
558 | * make all RNR NAKs visible so we know that RPC-over-RDMA | |
559 | * flow control is working correctly (no NAKs should be seen). | |
560 | */ | |
c56c65fb TT |
561 | ep->rep_remote_cma.flow_control = 0; |
562 | ep->rep_remote_cma.rnr_retry_count = 0; | |
563 | ||
564 | return 0; | |
565 | ||
566 | out2: | |
2fa8f88d | 567 | ib_free_cq(sendcq); |
c56c65fb TT |
568 | out1: |
569 | return rc; | |
570 | } | |
571 | ||
572 | /* | |
573 | * rpcrdma_ep_destroy | |
574 | * | |
575 | * Disconnect and destroy endpoint. After this, the only | |
576 | * valid operations on the ep are to free it (if dynamically | |
577 | * allocated) or re-create it. | |
c56c65fb | 578 | */ |
7f1d5419 | 579 | void |
c56c65fb TT |
580 | rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia) |
581 | { | |
c56c65fb TT |
582 | dprintk("RPC: %s: entering, connected is %d\n", |
583 | __func__, ep->rep_connected); | |
584 | ||
254f91e2 CL |
585 | cancel_delayed_work_sync(&ep->rep_connect_worker); |
586 | ||
72c02173 | 587 | if (ia->ri_id->qp) { |
550d7502 | 588 | rpcrdma_ep_disconnect(ep, ia); |
fee08caf TT |
589 | rdma_destroy_qp(ia->ri_id); |
590 | ia->ri_id->qp = NULL; | |
c56c65fb TT |
591 | } |
592 | ||
552bf225 | 593 | ib_free_cq(ep->rep_attr.recv_cq); |
2fa8f88d | 594 | ib_free_cq(ep->rep_attr.send_cq); |
c56c65fb TT |
595 | } |
596 | ||
597 | /* | |
598 | * Connect unconnected endpoint. | |
599 | */ | |
600 | int | |
601 | rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia) | |
602 | { | |
73806c88 | 603 | struct rdma_cm_id *id, *old; |
c56c65fb TT |
604 | int rc = 0; |
605 | int retry_count = 0; | |
c56c65fb | 606 | |
c055551e | 607 | if (ep->rep_connected != 0) { |
c56c65fb TT |
608 | struct rpcrdma_xprt *xprt; |
609 | retry: | |
ec62f40d | 610 | dprintk("RPC: %s: reconnecting...\n", __func__); |
282191cb CL |
611 | |
612 | rpcrdma_ep_disconnect(ep, ia); | |
c56c65fb TT |
613 | |
614 | xprt = container_of(ia, struct rpcrdma_xprt, rx_ia); | |
615 | id = rpcrdma_create_id(xprt, ia, | |
616 | (struct sockaddr *)&xprt->rx_data.addr); | |
617 | if (IS_ERR(id)) { | |
ec62f40d | 618 | rc = -EHOSTUNREACH; |
c56c65fb TT |
619 | goto out; |
620 | } | |
621 | /* TEMP TEMP TEMP - fail if new device: | |
622 | * Deregister/remarshal *all* requests! | |
623 | * Close and recreate adapter, pd, etc! | |
624 | * Re-determine all attributes still sane! | |
625 | * More stuff I haven't thought of! | |
626 | * Rrrgh! | |
627 | */ | |
89e0d112 | 628 | if (ia->ri_device != id->device) { |
c56c65fb TT |
629 | printk("RPC: %s: can't reconnect on " |
630 | "different device!\n", __func__); | |
d0f36c46 | 631 | rpcrdma_destroy_id(id); |
ec62f40d | 632 | rc = -ENETUNREACH; |
c56c65fb TT |
633 | goto out; |
634 | } | |
635 | /* END TEMP */ | |
ec62f40d CL |
636 | rc = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr); |
637 | if (rc) { | |
638 | dprintk("RPC: %s: rdma_create_qp failed %i\n", | |
639 | __func__, rc); | |
d0f36c46 | 640 | rpcrdma_destroy_id(id); |
ec62f40d CL |
641 | rc = -ENETUNREACH; |
642 | goto out; | |
643 | } | |
73806c88 | 644 | |
73806c88 | 645 | old = ia->ri_id; |
c56c65fb | 646 | ia->ri_id = id; |
73806c88 CL |
647 | |
648 | rdma_destroy_qp(old); | |
d0f36c46 | 649 | rpcrdma_destroy_id(old); |
ec62f40d CL |
650 | } else { |
651 | dprintk("RPC: %s: connecting...\n", __func__); | |
652 | rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr); | |
653 | if (rc) { | |
654 | dprintk("RPC: %s: rdma_create_qp failed %i\n", | |
655 | __func__, rc); | |
656 | /* do not update ep->rep_connected */ | |
657 | return -ENETUNREACH; | |
658 | } | |
c56c65fb TT |
659 | } |
660 | ||
c56c65fb TT |
661 | ep->rep_connected = 0; |
662 | ||
663 | rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma); | |
664 | if (rc) { | |
665 | dprintk("RPC: %s: rdma_connect() failed with %i\n", | |
666 | __func__, rc); | |
667 | goto out; | |
668 | } | |
669 | ||
c56c65fb TT |
670 | wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0); |
671 | ||
672 | /* | |
673 | * Check state. A non-peer reject indicates no listener | |
674 | * (ECONNREFUSED), which may be a transient state. All | |
675 | * others indicate a transport condition which has already | |
676 | * undergone a best-effort. | |
677 | */ | |
f64f9e71 JP |
678 | if (ep->rep_connected == -ECONNREFUSED && |
679 | ++retry_count <= RDMA_CONNECT_RETRY_MAX) { | |
c56c65fb TT |
680 | dprintk("RPC: %s: non-peer_reject, retry\n", __func__); |
681 | goto retry; | |
682 | } | |
683 | if (ep->rep_connected <= 0) { | |
684 | /* Sometimes, the only way to reliably connect to remote | |
685 | * CMs is to use same nonzero values for ORD and IRD. */ | |
b334eaab TT |
686 | if (retry_count++ <= RDMA_CONNECT_RETRY_MAX + 1 && |
687 | (ep->rep_remote_cma.responder_resources == 0 || | |
688 | ep->rep_remote_cma.initiator_depth != | |
689 | ep->rep_remote_cma.responder_resources)) { | |
690 | if (ep->rep_remote_cma.responder_resources == 0) | |
691 | ep->rep_remote_cma.responder_resources = 1; | |
692 | ep->rep_remote_cma.initiator_depth = | |
693 | ep->rep_remote_cma.responder_resources; | |
c56c65fb | 694 | goto retry; |
b334eaab | 695 | } |
c56c65fb TT |
696 | rc = ep->rep_connected; |
697 | } else { | |
f531a5db CL |
698 | struct rpcrdma_xprt *r_xprt; |
699 | unsigned int extras; | |
700 | ||
c56c65fb | 701 | dprintk("RPC: %s: connected\n", __func__); |
f531a5db CL |
702 | |
703 | r_xprt = container_of(ia, struct rpcrdma_xprt, rx_ia); | |
704 | extras = r_xprt->rx_buf.rb_bc_srv_max_requests; | |
705 | ||
706 | if (extras) { | |
707 | rc = rpcrdma_ep_post_extra_recv(r_xprt, extras); | |
38b95bcf | 708 | if (rc) { |
f531a5db CL |
709 | pr_warn("%s: rpcrdma_ep_post_extra_recv: %i\n", |
710 | __func__, rc); | |
711 | rc = 0; | |
38b95bcf | 712 | } |
f531a5db | 713 | } |
c56c65fb TT |
714 | } |
715 | ||
716 | out: | |
717 | if (rc) | |
718 | ep->rep_connected = rc; | |
719 | return rc; | |
720 | } | |
721 | ||
722 | /* | |
723 | * rpcrdma_ep_disconnect | |
724 | * | |
725 | * This is separate from destroy to facilitate the ability | |
726 | * to reconnect without recreating the endpoint. | |
727 | * | |
728 | * This call is not reentrant, and must not be made in parallel | |
729 | * on the same endpoint. | |
730 | */ | |
282191cb | 731 | void |
c56c65fb TT |
732 | rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia) |
733 | { | |
734 | int rc; | |
735 | ||
c56c65fb TT |
736 | rc = rdma_disconnect(ia->ri_id); |
737 | if (!rc) { | |
738 | /* returns without wait if not connected */ | |
739 | wait_event_interruptible(ep->rep_connect_wait, | |
740 | ep->rep_connected != 1); | |
741 | dprintk("RPC: %s: after wait, %sconnected\n", __func__, | |
742 | (ep->rep_connected == 1) ? "still " : "dis"); | |
743 | } else { | |
744 | dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc); | |
745 | ep->rep_connected = rc; | |
746 | } | |
550d7502 CL |
747 | |
748 | ib_drain_qp(ia->ri_id->qp); | |
c56c65fb TT |
749 | } |
750 | ||
505bbe64 CL |
751 | static void |
752 | rpcrdma_mr_recovery_worker(struct work_struct *work) | |
753 | { | |
754 | struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer, | |
755 | rb_recovery_worker.work); | |
756 | struct rpcrdma_mw *mw; | |
757 | ||
758 | spin_lock(&buf->rb_recovery_lock); | |
759 | while (!list_empty(&buf->rb_stale_mrs)) { | |
760 | mw = list_first_entry(&buf->rb_stale_mrs, | |
761 | struct rpcrdma_mw, mw_list); | |
762 | list_del_init(&mw->mw_list); | |
763 | spin_unlock(&buf->rb_recovery_lock); | |
764 | ||
765 | dprintk("RPC: %s: recovering MR %p\n", __func__, mw); | |
766 | mw->mw_xprt->rx_ia.ri_ops->ro_recover_mr(mw); | |
767 | ||
768 | spin_lock(&buf->rb_recovery_lock); | |
53d78523 | 769 | } |
505bbe64 CL |
770 | spin_unlock(&buf->rb_recovery_lock); |
771 | } | |
772 | ||
773 | void | |
774 | rpcrdma_defer_mr_recovery(struct rpcrdma_mw *mw) | |
775 | { | |
776 | struct rpcrdma_xprt *r_xprt = mw->mw_xprt; | |
777 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; | |
778 | ||
779 | spin_lock(&buf->rb_recovery_lock); | |
780 | list_add(&mw->mw_list, &buf->rb_stale_mrs); | |
781 | spin_unlock(&buf->rb_recovery_lock); | |
782 | ||
783 | schedule_delayed_work(&buf->rb_recovery_worker, 0); | |
784 | } | |
785 | ||
e2ac236c CL |
786 | static void |
787 | rpcrdma_create_mrs(struct rpcrdma_xprt *r_xprt) | |
788 | { | |
789 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; | |
790 | struct rpcrdma_ia *ia = &r_xprt->rx_ia; | |
791 | unsigned int count; | |
792 | LIST_HEAD(free); | |
793 | LIST_HEAD(all); | |
794 | ||
795 | for (count = 0; count < 32; count++) { | |
796 | struct rpcrdma_mw *mw; | |
797 | int rc; | |
798 | ||
799 | mw = kzalloc(sizeof(*mw), GFP_KERNEL); | |
800 | if (!mw) | |
801 | break; | |
802 | ||
803 | rc = ia->ri_ops->ro_init_mr(ia, mw); | |
804 | if (rc) { | |
805 | kfree(mw); | |
806 | break; | |
807 | } | |
808 | ||
809 | mw->mw_xprt = r_xprt; | |
810 | ||
811 | list_add(&mw->mw_list, &free); | |
812 | list_add(&mw->mw_all, &all); | |
813 | } | |
814 | ||
815 | spin_lock(&buf->rb_mwlock); | |
816 | list_splice(&free, &buf->rb_mws); | |
817 | list_splice(&all, &buf->rb_all); | |
818 | r_xprt->rx_stats.mrs_allocated += count; | |
819 | spin_unlock(&buf->rb_mwlock); | |
820 | ||
821 | dprintk("RPC: %s: created %u MRs\n", __func__, count); | |
822 | } | |
823 | ||
824 | static void | |
825 | rpcrdma_mr_refresh_worker(struct work_struct *work) | |
826 | { | |
827 | struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer, | |
828 | rb_refresh_worker.work); | |
829 | struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt, | |
830 | rx_buf); | |
831 | ||
832 | rpcrdma_create_mrs(r_xprt); | |
833 | } | |
834 | ||
f531a5db | 835 | struct rpcrdma_req * |
1392402c CL |
836 | rpcrdma_create_req(struct rpcrdma_xprt *r_xprt) |
837 | { | |
f531a5db | 838 | struct rpcrdma_buffer *buffer = &r_xprt->rx_buf; |
1392402c | 839 | struct rpcrdma_req *req; |
1392402c | 840 | |
85275c87 | 841 | req = kzalloc(sizeof(*req), GFP_KERNEL); |
1392402c | 842 | if (req == NULL) |
85275c87 | 843 | return ERR_PTR(-ENOMEM); |
1392402c | 844 | |
f531a5db CL |
845 | INIT_LIST_HEAD(&req->rl_free); |
846 | spin_lock(&buffer->rb_reqslock); | |
847 | list_add(&req->rl_all, &buffer->rb_allreqs); | |
848 | spin_unlock(&buffer->rb_reqslock); | |
2fa8f88d | 849 | req->rl_cqe.done = rpcrdma_wc_send; |
1392402c | 850 | req->rl_buffer = &r_xprt->rx_buf; |
9d6b0409 | 851 | INIT_LIST_HEAD(&req->rl_registered); |
1392402c | 852 | return req; |
1392402c CL |
853 | } |
854 | ||
f531a5db | 855 | struct rpcrdma_rep * |
1392402c CL |
856 | rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt) |
857 | { | |
858 | struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data; | |
1392402c CL |
859 | struct rpcrdma_ia *ia = &r_xprt->rx_ia; |
860 | struct rpcrdma_rep *rep; | |
861 | int rc; | |
862 | ||
863 | rc = -ENOMEM; | |
6b1184cd | 864 | rep = kzalloc(sizeof(*rep), GFP_KERNEL); |
1392402c CL |
865 | if (rep == NULL) |
866 | goto out; | |
1392402c | 867 | |
13650c23 | 868 | rep->rr_rdmabuf = rpcrdma_alloc_regbuf(cdata->inline_rsize, |
99ef4db3 | 869 | DMA_FROM_DEVICE, GFP_KERNEL); |
6b1184cd CL |
870 | if (IS_ERR(rep->rr_rdmabuf)) { |
871 | rc = PTR_ERR(rep->rr_rdmabuf); | |
1392402c | 872 | goto out_free; |
6b1184cd | 873 | } |
1392402c | 874 | |
89e0d112 | 875 | rep->rr_device = ia->ri_device; |
552bf225 | 876 | rep->rr_cqe.done = rpcrdma_receive_wc; |
fed171b3 | 877 | rep->rr_rxprt = r_xprt; |
fe97b47c | 878 | INIT_WORK(&rep->rr_work, rpcrdma_receive_worker); |
1392402c CL |
879 | return rep; |
880 | ||
881 | out_free: | |
882 | kfree(rep); | |
883 | out: | |
884 | return ERR_PTR(rc); | |
885 | } | |
886 | ||
c56c65fb | 887 | int |
ac920d04 | 888 | rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt) |
c56c65fb | 889 | { |
ac920d04 | 890 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; |
c56c65fb TT |
891 | int i, rc; |
892 | ||
1e465fd4 | 893 | buf->rb_max_requests = r_xprt->rx_data.max_requests; |
f531a5db | 894 | buf->rb_bc_srv_max_requests = 0; |
23826c7a | 895 | atomic_set(&buf->rb_credits, 1); |
e2ac236c | 896 | spin_lock_init(&buf->rb_mwlock); |
505bbe64 CL |
897 | spin_lock_init(&buf->rb_lock); |
898 | spin_lock_init(&buf->rb_recovery_lock); | |
e2ac236c CL |
899 | INIT_LIST_HEAD(&buf->rb_mws); |
900 | INIT_LIST_HEAD(&buf->rb_all); | |
505bbe64 | 901 | INIT_LIST_HEAD(&buf->rb_stale_mrs); |
e2ac236c CL |
902 | INIT_DELAYED_WORK(&buf->rb_refresh_worker, |
903 | rpcrdma_mr_refresh_worker); | |
505bbe64 CL |
904 | INIT_DELAYED_WORK(&buf->rb_recovery_worker, |
905 | rpcrdma_mr_recovery_worker); | |
c56c65fb | 906 | |
e2ac236c | 907 | rpcrdma_create_mrs(r_xprt); |
c56c65fb | 908 | |
1e465fd4 | 909 | INIT_LIST_HEAD(&buf->rb_send_bufs); |
f531a5db CL |
910 | INIT_LIST_HEAD(&buf->rb_allreqs); |
911 | spin_lock_init(&buf->rb_reqslock); | |
c56c65fb TT |
912 | for (i = 0; i < buf->rb_max_requests; i++) { |
913 | struct rpcrdma_req *req; | |
c56c65fb | 914 | |
1392402c CL |
915 | req = rpcrdma_create_req(r_xprt); |
916 | if (IS_ERR(req)) { | |
c56c65fb TT |
917 | dprintk("RPC: %s: request buffer %d alloc" |
918 | " failed\n", __func__, i); | |
1392402c | 919 | rc = PTR_ERR(req); |
c56c65fb TT |
920 | goto out; |
921 | } | |
f531a5db | 922 | req->rl_backchannel = false; |
1e465fd4 CL |
923 | list_add(&req->rl_free, &buf->rb_send_bufs); |
924 | } | |
925 | ||
926 | INIT_LIST_HEAD(&buf->rb_recv_bufs); | |
05c97466 | 927 | for (i = 0; i < buf->rb_max_requests + RPCRDMA_MAX_BC_REQUESTS; i++) { |
1e465fd4 | 928 | struct rpcrdma_rep *rep; |
c56c65fb | 929 | |
1392402c CL |
930 | rep = rpcrdma_create_rep(r_xprt); |
931 | if (IS_ERR(rep)) { | |
c56c65fb TT |
932 | dprintk("RPC: %s: reply buffer %d alloc failed\n", |
933 | __func__, i); | |
1392402c | 934 | rc = PTR_ERR(rep); |
c56c65fb TT |
935 | goto out; |
936 | } | |
1e465fd4 | 937 | list_add(&rep->rr_list, &buf->rb_recv_bufs); |
c56c65fb | 938 | } |
1392402c | 939 | |
c56c65fb TT |
940 | return 0; |
941 | out: | |
942 | rpcrdma_buffer_destroy(buf); | |
943 | return rc; | |
944 | } | |
945 | ||
1e465fd4 CL |
946 | static struct rpcrdma_req * |
947 | rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf) | |
948 | { | |
949 | struct rpcrdma_req *req; | |
950 | ||
951 | req = list_first_entry(&buf->rb_send_bufs, | |
952 | struct rpcrdma_req, rl_free); | |
953 | list_del(&req->rl_free); | |
954 | return req; | |
955 | } | |
956 | ||
957 | static struct rpcrdma_rep * | |
958 | rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf) | |
959 | { | |
960 | struct rpcrdma_rep *rep; | |
961 | ||
962 | rep = list_first_entry(&buf->rb_recv_bufs, | |
963 | struct rpcrdma_rep, rr_list); | |
964 | list_del(&rep->rr_list); | |
965 | return rep; | |
966 | } | |
967 | ||
1392402c | 968 | static void |
13650c23 | 969 | rpcrdma_destroy_rep(struct rpcrdma_rep *rep) |
1392402c | 970 | { |
13650c23 | 971 | rpcrdma_free_regbuf(rep->rr_rdmabuf); |
1392402c CL |
972 | kfree(rep); |
973 | } | |
974 | ||
f531a5db | 975 | void |
13650c23 | 976 | rpcrdma_destroy_req(struct rpcrdma_req *req) |
1392402c | 977 | { |
13650c23 CL |
978 | rpcrdma_free_regbuf(req->rl_recvbuf); |
979 | rpcrdma_free_regbuf(req->rl_sendbuf); | |
980 | rpcrdma_free_regbuf(req->rl_rdmabuf); | |
1392402c CL |
981 | kfree(req); |
982 | } | |
983 | ||
e2ac236c CL |
984 | static void |
985 | rpcrdma_destroy_mrs(struct rpcrdma_buffer *buf) | |
986 | { | |
987 | struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt, | |
988 | rx_buf); | |
989 | struct rpcrdma_ia *ia = rdmab_to_ia(buf); | |
990 | struct rpcrdma_mw *mw; | |
991 | unsigned int count; | |
992 | ||
993 | count = 0; | |
994 | spin_lock(&buf->rb_mwlock); | |
995 | while (!list_empty(&buf->rb_all)) { | |
996 | mw = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all); | |
997 | list_del(&mw->mw_all); | |
998 | ||
999 | spin_unlock(&buf->rb_mwlock); | |
1000 | ia->ri_ops->ro_release_mr(mw); | |
1001 | count++; | |
1002 | spin_lock(&buf->rb_mwlock); | |
1003 | } | |
1004 | spin_unlock(&buf->rb_mwlock); | |
1005 | r_xprt->rx_stats.mrs_allocated = 0; | |
1006 | ||
1007 | dprintk("RPC: %s: released %u MRs\n", __func__, count); | |
1008 | } | |
1009 | ||
c56c65fb TT |
1010 | void |
1011 | rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf) | |
1012 | { | |
505bbe64 CL |
1013 | cancel_delayed_work_sync(&buf->rb_recovery_worker); |
1014 | ||
1e465fd4 CL |
1015 | while (!list_empty(&buf->rb_recv_bufs)) { |
1016 | struct rpcrdma_rep *rep; | |
c56c65fb | 1017 | |
1e465fd4 | 1018 | rep = rpcrdma_buffer_get_rep_locked(buf); |
13650c23 | 1019 | rpcrdma_destroy_rep(rep); |
c56c65fb | 1020 | } |
05c97466 | 1021 | buf->rb_send_count = 0; |
c56c65fb | 1022 | |
f531a5db CL |
1023 | spin_lock(&buf->rb_reqslock); |
1024 | while (!list_empty(&buf->rb_allreqs)) { | |
1e465fd4 | 1025 | struct rpcrdma_req *req; |
4034ba04 | 1026 | |
f531a5db CL |
1027 | req = list_first_entry(&buf->rb_allreqs, |
1028 | struct rpcrdma_req, rl_all); | |
1029 | list_del(&req->rl_all); | |
1030 | ||
1031 | spin_unlock(&buf->rb_reqslock); | |
13650c23 | 1032 | rpcrdma_destroy_req(req); |
f531a5db | 1033 | spin_lock(&buf->rb_reqslock); |
1e465fd4 | 1034 | } |
f531a5db | 1035 | spin_unlock(&buf->rb_reqslock); |
05c97466 | 1036 | buf->rb_recv_count = 0; |
4034ba04 | 1037 | |
e2ac236c | 1038 | rpcrdma_destroy_mrs(buf); |
c56c65fb TT |
1039 | } |
1040 | ||
346aa66b CL |
1041 | struct rpcrdma_mw * |
1042 | rpcrdma_get_mw(struct rpcrdma_xprt *r_xprt) | |
c2922c02 | 1043 | { |
346aa66b CL |
1044 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; |
1045 | struct rpcrdma_mw *mw = NULL; | |
346aa66b | 1046 | |
58d1dcf5 | 1047 | spin_lock(&buf->rb_mwlock); |
346aa66b CL |
1048 | if (!list_empty(&buf->rb_mws)) { |
1049 | mw = list_first_entry(&buf->rb_mws, | |
1050 | struct rpcrdma_mw, mw_list); | |
1051 | list_del_init(&mw->mw_list); | |
c2922c02 | 1052 | } |
58d1dcf5 | 1053 | spin_unlock(&buf->rb_mwlock); |
346aa66b CL |
1054 | |
1055 | if (!mw) | |
e2ac236c | 1056 | goto out_nomws; |
346aa66b | 1057 | return mw; |
e2ac236c CL |
1058 | |
1059 | out_nomws: | |
1060 | dprintk("RPC: %s: no MWs available\n", __func__); | |
1061 | schedule_delayed_work(&buf->rb_refresh_worker, 0); | |
1062 | ||
1063 | /* Allow the reply handler and refresh worker to run */ | |
1064 | cond_resched(); | |
1065 | ||
1066 | return NULL; | |
c2922c02 CL |
1067 | } |
1068 | ||
346aa66b CL |
1069 | void |
1070 | rpcrdma_put_mw(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mw *mw) | |
c2922c02 | 1071 | { |
346aa66b | 1072 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; |
c2922c02 | 1073 | |
58d1dcf5 | 1074 | spin_lock(&buf->rb_mwlock); |
346aa66b | 1075 | list_add_tail(&mw->mw_list, &buf->rb_mws); |
58d1dcf5 | 1076 | spin_unlock(&buf->rb_mwlock); |
c2922c02 CL |
1077 | } |
1078 | ||
05c97466 CL |
1079 | static struct rpcrdma_rep * |
1080 | rpcrdma_buffer_get_rep(struct rpcrdma_buffer *buffers) | |
1081 | { | |
1082 | /* If an RPC previously completed without a reply (say, a | |
1083 | * credential problem or a soft timeout occurs) then hold off | |
1084 | * on supplying more Receive buffers until the number of new | |
1085 | * pending RPCs catches up to the number of posted Receives. | |
1086 | */ | |
1087 | if (unlikely(buffers->rb_send_count < buffers->rb_recv_count)) | |
1088 | return NULL; | |
1089 | ||
1090 | if (unlikely(list_empty(&buffers->rb_recv_bufs))) | |
1091 | return NULL; | |
1092 | buffers->rb_recv_count++; | |
1093 | return rpcrdma_buffer_get_rep_locked(buffers); | |
1094 | } | |
1095 | ||
c56c65fb TT |
1096 | /* |
1097 | * Get a set of request/reply buffers. | |
78d506e1 CL |
1098 | * |
1099 | * Reply buffer (if available) is attached to send buffer upon return. | |
c56c65fb TT |
1100 | */ |
1101 | struct rpcrdma_req * | |
1102 | rpcrdma_buffer_get(struct rpcrdma_buffer *buffers) | |
1103 | { | |
1104 | struct rpcrdma_req *req; | |
c14d86e5 | 1105 | |
a5b027e1 | 1106 | spin_lock(&buffers->rb_lock); |
1e465fd4 CL |
1107 | if (list_empty(&buffers->rb_send_bufs)) |
1108 | goto out_reqbuf; | |
05c97466 | 1109 | buffers->rb_send_count++; |
1e465fd4 | 1110 | req = rpcrdma_buffer_get_req_locked(buffers); |
05c97466 | 1111 | req->rl_reply = rpcrdma_buffer_get_rep(buffers); |
a5b027e1 | 1112 | spin_unlock(&buffers->rb_lock); |
1e465fd4 | 1113 | return req; |
ddb6bebc | 1114 | |
1e465fd4 | 1115 | out_reqbuf: |
a5b027e1 | 1116 | spin_unlock(&buffers->rb_lock); |
78d506e1 | 1117 | pr_warn("RPC: %s: out of request buffers\n", __func__); |
1e465fd4 | 1118 | return NULL; |
c56c65fb TT |
1119 | } |
1120 | ||
1121 | /* | |
1122 | * Put request/reply buffers back into pool. | |
1123 | * Pre-decrement counter/array index. | |
1124 | */ | |
1125 | void | |
1126 | rpcrdma_buffer_put(struct rpcrdma_req *req) | |
1127 | { | |
1128 | struct rpcrdma_buffer *buffers = req->rl_buffer; | |
1e465fd4 | 1129 | struct rpcrdma_rep *rep = req->rl_reply; |
c56c65fb | 1130 | |
1e465fd4 CL |
1131 | req->rl_niovs = 0; |
1132 | req->rl_reply = NULL; | |
1133 | ||
a5b027e1 | 1134 | spin_lock(&buffers->rb_lock); |
05c97466 | 1135 | buffers->rb_send_count--; |
1e465fd4 | 1136 | list_add_tail(&req->rl_free, &buffers->rb_send_bufs); |
05c97466 CL |
1137 | if (rep) { |
1138 | buffers->rb_recv_count--; | |
1e465fd4 | 1139 | list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs); |
05c97466 | 1140 | } |
a5b027e1 | 1141 | spin_unlock(&buffers->rb_lock); |
c56c65fb TT |
1142 | } |
1143 | ||
1144 | /* | |
1145 | * Recover reply buffers from pool. | |
1e465fd4 | 1146 | * This happens when recovering from disconnect. |
c56c65fb TT |
1147 | */ |
1148 | void | |
1149 | rpcrdma_recv_buffer_get(struct rpcrdma_req *req) | |
1150 | { | |
1151 | struct rpcrdma_buffer *buffers = req->rl_buffer; | |
c56c65fb | 1152 | |
a5b027e1 | 1153 | spin_lock(&buffers->rb_lock); |
05c97466 | 1154 | req->rl_reply = rpcrdma_buffer_get_rep(buffers); |
a5b027e1 | 1155 | spin_unlock(&buffers->rb_lock); |
c56c65fb TT |
1156 | } |
1157 | ||
1158 | /* | |
1159 | * Put reply buffers back into pool when not attached to | |
b45ccfd2 | 1160 | * request. This happens in error conditions. |
c56c65fb TT |
1161 | */ |
1162 | void | |
1163 | rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep) | |
1164 | { | |
fed171b3 | 1165 | struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf; |
c56c65fb | 1166 | |
a5b027e1 | 1167 | spin_lock(&buffers->rb_lock); |
05c97466 | 1168 | buffers->rb_recv_count--; |
1e465fd4 | 1169 | list_add_tail(&rep->rr_list, &buffers->rb_recv_bufs); |
a5b027e1 | 1170 | spin_unlock(&buffers->rb_lock); |
c56c65fb TT |
1171 | } |
1172 | ||
9128c3e7 | 1173 | /** |
99ef4db3 | 1174 | * rpcrdma_alloc_regbuf - allocate and DMA-map memory for SEND/RECV buffers |
9128c3e7 | 1175 | * @size: size of buffer to be allocated, in bytes |
99ef4db3 | 1176 | * @direction: direction of data movement |
9128c3e7 CL |
1177 | * @flags: GFP flags |
1178 | * | |
54cbd6b0 CL |
1179 | * Returns an ERR_PTR, or a pointer to a regbuf, a buffer that |
1180 | * can be persistently DMA-mapped for I/O. | |
9128c3e7 CL |
1181 | * |
1182 | * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for | |
99ef4db3 CL |
1183 | * receiving the payload of RDMA RECV operations. During Long Calls |
1184 | * or Replies they may be registered externally via ro_map. | |
9128c3e7 CL |
1185 | */ |
1186 | struct rpcrdma_regbuf * | |
13650c23 CL |
1187 | rpcrdma_alloc_regbuf(size_t size, enum dma_data_direction direction, |
1188 | gfp_t flags) | |
9128c3e7 CL |
1189 | { |
1190 | struct rpcrdma_regbuf *rb; | |
9128c3e7 | 1191 | |
9128c3e7 CL |
1192 | rb = kmalloc(sizeof(*rb) + size, flags); |
1193 | if (rb == NULL) | |
54cbd6b0 | 1194 | return ERR_PTR(-ENOMEM); |
9128c3e7 | 1195 | |
54cbd6b0 | 1196 | rb->rg_device = NULL; |
99ef4db3 | 1197 | rb->rg_direction = direction; |
54cbd6b0 | 1198 | rb->rg_iov.length = size; |
99ef4db3 | 1199 | |
9128c3e7 | 1200 | return rb; |
54cbd6b0 | 1201 | } |
9128c3e7 | 1202 | |
54cbd6b0 CL |
1203 | /** |
1204 | * __rpcrdma_map_regbuf - DMA-map a regbuf | |
1205 | * @ia: controlling rpcrdma_ia | |
1206 | * @rb: regbuf to be mapped | |
1207 | */ | |
1208 | bool | |
1209 | __rpcrdma_dma_map_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb) | |
1210 | { | |
1211 | if (rb->rg_direction == DMA_NONE) | |
1212 | return false; | |
1213 | ||
1214 | rb->rg_iov.addr = ib_dma_map_single(ia->ri_device, | |
1215 | (void *)rb->rg_base, | |
1216 | rdmab_length(rb), | |
1217 | rb->rg_direction); | |
1218 | if (ib_dma_mapping_error(ia->ri_device, rdmab_addr(rb))) | |
1219 | return false; | |
1220 | ||
1221 | rb->rg_device = ia->ri_device; | |
1222 | rb->rg_iov.lkey = ia->ri_pd->local_dma_lkey; | |
1223 | return true; | |
1224 | } | |
1225 | ||
1226 | static void | |
1227 | rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb) | |
1228 | { | |
1229 | if (!rpcrdma_regbuf_is_mapped(rb)) | |
1230 | return; | |
1231 | ||
1232 | ib_dma_unmap_single(rb->rg_device, rdmab_addr(rb), | |
1233 | rdmab_length(rb), rb->rg_direction); | |
1234 | rb->rg_device = NULL; | |
9128c3e7 CL |
1235 | } |
1236 | ||
1237 | /** | |
1238 | * rpcrdma_free_regbuf - deregister and free registered buffer | |
9128c3e7 CL |
1239 | * @rb: regbuf to be deregistered and freed |
1240 | */ | |
1241 | void | |
13650c23 | 1242 | rpcrdma_free_regbuf(struct rpcrdma_regbuf *rb) |
9128c3e7 | 1243 | { |
e531dcab CL |
1244 | if (!rb) |
1245 | return; | |
1246 | ||
54cbd6b0 | 1247 | rpcrdma_dma_unmap_regbuf(rb); |
e531dcab | 1248 | kfree(rb); |
9128c3e7 CL |
1249 | } |
1250 | ||
c56c65fb TT |
1251 | /* |
1252 | * Prepost any receive buffer, then post send. | |
1253 | * | |
1254 | * Receive buffer is donated to hardware, reclaimed upon recv completion. | |
1255 | */ | |
1256 | int | |
1257 | rpcrdma_ep_post(struct rpcrdma_ia *ia, | |
1258 | struct rpcrdma_ep *ep, | |
1259 | struct rpcrdma_req *req) | |
1260 | { | |
b3221d6a | 1261 | struct ib_device *device = ia->ri_device; |
c56c65fb TT |
1262 | struct ib_send_wr send_wr, *send_wr_fail; |
1263 | struct rpcrdma_rep *rep = req->rl_reply; | |
b3221d6a CL |
1264 | struct ib_sge *iov = req->rl_send_iov; |
1265 | int i, rc; | |
c56c65fb TT |
1266 | |
1267 | if (rep) { | |
b157380a | 1268 | rc = rpcrdma_ep_post_recv(ia, rep); |
c56c65fb | 1269 | if (rc) |
7a89f9c6 | 1270 | return rc; |
c56c65fb TT |
1271 | req->rl_reply = NULL; |
1272 | } | |
1273 | ||
1274 | send_wr.next = NULL; | |
2fa8f88d | 1275 | send_wr.wr_cqe = &req->rl_cqe; |
b3221d6a | 1276 | send_wr.sg_list = iov; |
c56c65fb TT |
1277 | send_wr.num_sge = req->rl_niovs; |
1278 | send_wr.opcode = IB_WR_SEND; | |
b3221d6a CL |
1279 | |
1280 | for (i = 0; i < send_wr.num_sge; i++) | |
1281 | ib_dma_sync_single_for_device(device, iov[i].addr, | |
1282 | iov[i].length, DMA_TO_DEVICE); | |
1283 | dprintk("RPC: %s: posting %d s/g entries\n", | |
1284 | __func__, send_wr.num_sge); | |
c56c65fb TT |
1285 | |
1286 | if (DECR_CQCOUNT(ep) > 0) | |
1287 | send_wr.send_flags = 0; | |
1288 | else { /* Provider must take a send completion every now and then */ | |
1289 | INIT_CQCOUNT(ep); | |
1290 | send_wr.send_flags = IB_SEND_SIGNALED; | |
1291 | } | |
1292 | ||
1293 | rc = ib_post_send(ia->ri_id->qp, &send_wr, &send_wr_fail); | |
1294 | if (rc) | |
7a89f9c6 CL |
1295 | goto out_postsend_err; |
1296 | return 0; | |
1297 | ||
1298 | out_postsend_err: | |
1299 | pr_err("rpcrdma: RDMA Send ib_post_send returned %i\n", rc); | |
1300 | return -ENOTCONN; | |
c56c65fb TT |
1301 | } |
1302 | ||
c56c65fb TT |
1303 | int |
1304 | rpcrdma_ep_post_recv(struct rpcrdma_ia *ia, | |
c56c65fb TT |
1305 | struct rpcrdma_rep *rep) |
1306 | { | |
1307 | struct ib_recv_wr recv_wr, *recv_wr_fail; | |
1308 | int rc; | |
1309 | ||
1310 | recv_wr.next = NULL; | |
552bf225 | 1311 | recv_wr.wr_cqe = &rep->rr_cqe; |
6b1184cd | 1312 | recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov; |
c56c65fb TT |
1313 | recv_wr.num_sge = 1; |
1314 | ||
54cbd6b0 CL |
1315 | if (!rpcrdma_dma_map_regbuf(ia, rep->rr_rdmabuf)) |
1316 | goto out_map; | |
c56c65fb | 1317 | rc = ib_post_recv(ia->ri_id->qp, &recv_wr, &recv_wr_fail); |
c56c65fb | 1318 | if (rc) |
7a89f9c6 CL |
1319 | goto out_postrecv; |
1320 | return 0; | |
1321 | ||
54cbd6b0 CL |
1322 | out_map: |
1323 | pr_err("rpcrdma: failed to DMA map the Receive buffer\n"); | |
1324 | return -EIO; | |
1325 | ||
7a89f9c6 CL |
1326 | out_postrecv: |
1327 | pr_err("rpcrdma: ib_post_recv returned %i\n", rc); | |
1328 | return -ENOTCONN; | |
c56c65fb | 1329 | } |
43e95988 | 1330 | |
f531a5db CL |
1331 | /** |
1332 | * rpcrdma_ep_post_extra_recv - Post buffers for incoming backchannel requests | |
1333 | * @r_xprt: transport associated with these backchannel resources | |
1334 | * @min_reqs: minimum number of incoming requests expected | |
1335 | * | |
1336 | * Returns zero if all requested buffers were posted, or a negative errno. | |
1337 | */ | |
1338 | int | |
1339 | rpcrdma_ep_post_extra_recv(struct rpcrdma_xprt *r_xprt, unsigned int count) | |
1340 | { | |
1341 | struct rpcrdma_buffer *buffers = &r_xprt->rx_buf; | |
1342 | struct rpcrdma_ia *ia = &r_xprt->rx_ia; | |
f531a5db | 1343 | struct rpcrdma_rep *rep; |
f531a5db CL |
1344 | int rc; |
1345 | ||
1346 | while (count--) { | |
9b06688b | 1347 | spin_lock(&buffers->rb_lock); |
f531a5db CL |
1348 | if (list_empty(&buffers->rb_recv_bufs)) |
1349 | goto out_reqbuf; | |
1350 | rep = rpcrdma_buffer_get_rep_locked(buffers); | |
9b06688b | 1351 | spin_unlock(&buffers->rb_lock); |
f531a5db | 1352 | |
b157380a | 1353 | rc = rpcrdma_ep_post_recv(ia, rep); |
f531a5db CL |
1354 | if (rc) |
1355 | goto out_rc; | |
1356 | } | |
1357 | ||
1358 | return 0; | |
1359 | ||
1360 | out_reqbuf: | |
9b06688b | 1361 | spin_unlock(&buffers->rb_lock); |
f531a5db CL |
1362 | pr_warn("%s: no extra receive buffers\n", __func__); |
1363 | return -ENOMEM; | |
1364 | ||
1365 | out_rc: | |
1366 | rpcrdma_recv_buffer_put(rep); | |
1367 | return rc; | |
1368 | } |