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