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> |
c56c65fb | 55 | |
f58851e6 TT |
56 | #include "xprt_rdma.h" |
57 | ||
c56c65fb TT |
58 | /* |
59 | * Globals/Macros | |
60 | */ | |
61 | ||
f895b252 | 62 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) |
c56c65fb TT |
63 | # define RPCDBG_FACILITY RPCDBG_TRANS |
64 | #endif | |
65 | ||
66 | /* | |
67 | * internal functions | |
68 | */ | |
69 | ||
70 | /* | |
71 | * handle replies in tasklet context, using a single, global list | |
72 | * rdma tasklet function -- just turn around and call the func | |
73 | * for all replies on the list | |
74 | */ | |
75 | ||
76 | static DEFINE_SPINLOCK(rpcrdma_tk_lock_g); | |
77 | static LIST_HEAD(rpcrdma_tasklets_g); | |
78 | ||
79 | static void | |
80 | rpcrdma_run_tasklet(unsigned long data) | |
81 | { | |
82 | struct rpcrdma_rep *rep; | |
c56c65fb TT |
83 | unsigned long flags; |
84 | ||
85 | data = data; | |
86 | spin_lock_irqsave(&rpcrdma_tk_lock_g, flags); | |
87 | while (!list_empty(&rpcrdma_tasklets_g)) { | |
88 | rep = list_entry(rpcrdma_tasklets_g.next, | |
89 | struct rpcrdma_rep, rr_list); | |
90 | list_del(&rep->rr_list); | |
c56c65fb TT |
91 | spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags); |
92 | ||
494ae30d | 93 | rpcrdma_reply_handler(rep); |
c56c65fb TT |
94 | |
95 | spin_lock_irqsave(&rpcrdma_tk_lock_g, flags); | |
96 | } | |
97 | spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags); | |
98 | } | |
99 | ||
100 | static DECLARE_TASKLET(rpcrdma_tasklet_g, rpcrdma_run_tasklet, 0UL); | |
101 | ||
f1a03b76 CL |
102 | static void |
103 | rpcrdma_schedule_tasklet(struct list_head *sched_list) | |
104 | { | |
105 | unsigned long flags; | |
106 | ||
107 | spin_lock_irqsave(&rpcrdma_tk_lock_g, flags); | |
108 | list_splice_tail(sched_list, &rpcrdma_tasklets_g); | |
109 | spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags); | |
110 | tasklet_schedule(&rpcrdma_tasklet_g); | |
111 | } | |
112 | ||
c56c65fb TT |
113 | static void |
114 | rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context) | |
115 | { | |
116 | struct rpcrdma_ep *ep = context; | |
117 | ||
7ff11de1 | 118 | pr_err("RPC: %s: %s on device %s ep %p\n", |
76357c71 | 119 | __func__, ib_event_msg(event->event), |
7ff11de1 | 120 | event->device->name, context); |
c56c65fb TT |
121 | if (ep->rep_connected == 1) { |
122 | ep->rep_connected = -EIO; | |
afadc468 | 123 | rpcrdma_conn_func(ep); |
c56c65fb TT |
124 | wake_up_all(&ep->rep_connect_wait); |
125 | } | |
126 | } | |
127 | ||
128 | static void | |
129 | rpcrdma_cq_async_error_upcall(struct ib_event *event, void *context) | |
130 | { | |
131 | struct rpcrdma_ep *ep = context; | |
132 | ||
7ff11de1 | 133 | pr_err("RPC: %s: %s on device %s ep %p\n", |
76357c71 | 134 | __func__, ib_event_msg(event->event), |
7ff11de1 | 135 | event->device->name, context); |
c56c65fb TT |
136 | if (ep->rep_connected == 1) { |
137 | ep->rep_connected = -EIO; | |
afadc468 | 138 | rpcrdma_conn_func(ep); |
c56c65fb TT |
139 | wake_up_all(&ep->rep_connect_wait); |
140 | } | |
141 | } | |
142 | ||
fc664485 CL |
143 | static void |
144 | rpcrdma_sendcq_process_wc(struct ib_wc *wc) | |
c56c65fb | 145 | { |
8502427c | 146 | /* WARNING: Only wr_id and status are reliable at this point */ |
e46ac34c CL |
147 | if (wc->wr_id == RPCRDMA_IGNORE_COMPLETION) { |
148 | if (wc->status != IB_WC_SUCCESS && | |
149 | wc->status != IB_WC_WR_FLUSH_ERR) | |
8502427c | 150 | pr_err("RPC: %s: SEND: %s\n", |
76357c71 | 151 | __func__, ib_wc_status_msg(wc->status)); |
8502427c CL |
152 | } else { |
153 | struct rpcrdma_mw *r; | |
154 | ||
155 | r = (struct rpcrdma_mw *)(unsigned long)wc->wr_id; | |
e46ac34c | 156 | r->mw_sendcompletion(wc); |
8502427c | 157 | } |
c56c65fb TT |
158 | } |
159 | ||
fc664485 | 160 | static int |
1c00dd07 | 161 | rpcrdma_sendcq_poll(struct ib_cq *cq, struct rpcrdma_ep *ep) |
c56c65fb | 162 | { |
1c00dd07 | 163 | struct ib_wc *wcs; |
8301a2c0 | 164 | int budget, count, rc; |
c56c65fb | 165 | |
8301a2c0 | 166 | budget = RPCRDMA_WC_BUDGET / RPCRDMA_POLLSIZE; |
1c00dd07 CL |
167 | do { |
168 | wcs = ep->rep_send_wcs; | |
169 | ||
170 | rc = ib_poll_cq(cq, RPCRDMA_POLLSIZE, wcs); | |
171 | if (rc <= 0) | |
172 | return rc; | |
173 | ||
174 | count = rc; | |
175 | while (count-- > 0) | |
176 | rpcrdma_sendcq_process_wc(wcs++); | |
8301a2c0 | 177 | } while (rc == RPCRDMA_POLLSIZE && --budget); |
1c00dd07 | 178 | return 0; |
fc664485 | 179 | } |
c56c65fb | 180 | |
fc664485 CL |
181 | /* |
182 | * Handle send, fast_reg_mr, and local_inv completions. | |
183 | * | |
184 | * Send events are typically suppressed and thus do not result | |
185 | * in an upcall. Occasionally one is signaled, however. This | |
186 | * prevents the provider's completion queue from wrapping and | |
187 | * losing a completion. | |
188 | */ | |
189 | static void | |
190 | rpcrdma_sendcq_upcall(struct ib_cq *cq, void *cq_context) | |
191 | { | |
1c00dd07 | 192 | struct rpcrdma_ep *ep = (struct rpcrdma_ep *)cq_context; |
fc664485 CL |
193 | int rc; |
194 | ||
1c00dd07 | 195 | rc = rpcrdma_sendcq_poll(cq, ep); |
fc664485 CL |
196 | if (rc) { |
197 | dprintk("RPC: %s: ib_poll_cq failed: %i\n", | |
198 | __func__, rc); | |
199 | return; | |
c56c65fb TT |
200 | } |
201 | ||
7f23f6f6 CL |
202 | rc = ib_req_notify_cq(cq, |
203 | IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS); | |
204 | if (rc == 0) | |
205 | return; | |
206 | if (rc < 0) { | |
fc664485 CL |
207 | dprintk("RPC: %s: ib_req_notify_cq failed: %i\n", |
208 | __func__, rc); | |
209 | return; | |
210 | } | |
211 | ||
1c00dd07 | 212 | rpcrdma_sendcq_poll(cq, ep); |
fc664485 CL |
213 | } |
214 | ||
215 | static void | |
bb96193d | 216 | rpcrdma_recvcq_process_wc(struct ib_wc *wc, struct list_head *sched_list) |
fc664485 CL |
217 | { |
218 | struct rpcrdma_rep *rep = | |
219 | (struct rpcrdma_rep *)(unsigned long)wc->wr_id; | |
220 | ||
8502427c CL |
221 | /* WARNING: Only wr_id and status are reliable at this point */ |
222 | if (wc->status != IB_WC_SUCCESS) | |
223 | goto out_fail; | |
fc664485 | 224 | |
8502427c | 225 | /* status == SUCCESS means all fields in wc are trustworthy */ |
fc664485 CL |
226 | if (wc->opcode != IB_WC_RECV) |
227 | return; | |
228 | ||
8502427c CL |
229 | dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n", |
230 | __func__, rep, wc->byte_len); | |
231 | ||
fc664485 | 232 | rep->rr_len = wc->byte_len; |
89e0d112 | 233 | ib_dma_sync_single_for_cpu(rep->rr_device, |
6b1184cd CL |
234 | rdmab_addr(rep->rr_rdmabuf), |
235 | rep->rr_len, DMA_FROM_DEVICE); | |
236 | prefetch(rdmab_to_msg(rep->rr_rdmabuf)); | |
fc664485 CL |
237 | |
238 | out_schedule: | |
bb96193d | 239 | list_add_tail(&rep->rr_list, sched_list); |
8502427c CL |
240 | return; |
241 | out_fail: | |
242 | if (wc->status != IB_WC_WR_FLUSH_ERR) | |
243 | pr_err("RPC: %s: rep %p: %s\n", | |
76357c71 | 244 | __func__, rep, ib_wc_status_msg(wc->status)); |
8502427c CL |
245 | rep->rr_len = ~0U; |
246 | goto out_schedule; | |
fc664485 CL |
247 | } |
248 | ||
249 | static int | |
1c00dd07 | 250 | rpcrdma_recvcq_poll(struct ib_cq *cq, struct rpcrdma_ep *ep) |
fc664485 | 251 | { |
bb96193d | 252 | struct list_head sched_list; |
1c00dd07 | 253 | struct ib_wc *wcs; |
8301a2c0 | 254 | int budget, count, rc; |
fc664485 | 255 | |
bb96193d | 256 | INIT_LIST_HEAD(&sched_list); |
8301a2c0 | 257 | budget = RPCRDMA_WC_BUDGET / RPCRDMA_POLLSIZE; |
1c00dd07 CL |
258 | do { |
259 | wcs = ep->rep_recv_wcs; | |
260 | ||
261 | rc = ib_poll_cq(cq, RPCRDMA_POLLSIZE, wcs); | |
262 | if (rc <= 0) | |
bb96193d | 263 | goto out_schedule; |
1c00dd07 CL |
264 | |
265 | count = rc; | |
266 | while (count-- > 0) | |
bb96193d | 267 | rpcrdma_recvcq_process_wc(wcs++, &sched_list); |
8301a2c0 | 268 | } while (rc == RPCRDMA_POLLSIZE && --budget); |
bb96193d CL |
269 | rc = 0; |
270 | ||
271 | out_schedule: | |
f1a03b76 | 272 | rpcrdma_schedule_tasklet(&sched_list); |
bb96193d | 273 | return rc; |
c56c65fb TT |
274 | } |
275 | ||
276 | /* | |
fc664485 | 277 | * Handle receive completions. |
c56c65fb | 278 | * |
c56c65fb TT |
279 | * It is reentrant but processes single events in order to maintain |
280 | * ordering of receives to keep server credits. | |
281 | * | |
282 | * It is the responsibility of the scheduled tasklet to return | |
283 | * recv buffers to the pool. NOTE: this affects synchronization of | |
284 | * connection shutdown. That is, the structures required for | |
285 | * the completion of the reply handler must remain intact until | |
286 | * all memory has been reclaimed. | |
c56c65fb TT |
287 | */ |
288 | static void | |
fc664485 | 289 | rpcrdma_recvcq_upcall(struct ib_cq *cq, void *cq_context) |
c56c65fb | 290 | { |
1c00dd07 | 291 | struct rpcrdma_ep *ep = (struct rpcrdma_ep *)cq_context; |
c56c65fb TT |
292 | int rc; |
293 | ||
1c00dd07 | 294 | rc = rpcrdma_recvcq_poll(cq, ep); |
fc664485 CL |
295 | if (rc) { |
296 | dprintk("RPC: %s: ib_poll_cq failed: %i\n", | |
297 | __func__, rc); | |
c56c65fb | 298 | return; |
fc664485 | 299 | } |
c56c65fb | 300 | |
7f23f6f6 CL |
301 | rc = ib_req_notify_cq(cq, |
302 | IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS); | |
303 | if (rc == 0) | |
304 | return; | |
305 | if (rc < 0) { | |
fc664485 | 306 | dprintk("RPC: %s: ib_req_notify_cq failed: %i\n", |
c56c65fb TT |
307 | __func__, rc); |
308 | return; | |
309 | } | |
310 | ||
1c00dd07 | 311 | rpcrdma_recvcq_poll(cq, ep); |
c56c65fb TT |
312 | } |
313 | ||
a7bc211a CL |
314 | static void |
315 | rpcrdma_flush_cqs(struct rpcrdma_ep *ep) | |
316 | { | |
5c166bef CL |
317 | struct ib_wc wc; |
318 | LIST_HEAD(sched_list); | |
319 | ||
320 | while (ib_poll_cq(ep->rep_attr.recv_cq, 1, &wc) > 0) | |
321 | rpcrdma_recvcq_process_wc(&wc, &sched_list); | |
322 | if (!list_empty(&sched_list)) | |
323 | rpcrdma_schedule_tasklet(&sched_list); | |
324 | while (ib_poll_cq(ep->rep_attr.send_cq, 1, &wc) > 0) | |
325 | rpcrdma_sendcq_process_wc(&wc); | |
a7bc211a CL |
326 | } |
327 | ||
c56c65fb TT |
328 | static int |
329 | rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event) | |
330 | { | |
331 | struct rpcrdma_xprt *xprt = id->context; | |
332 | struct rpcrdma_ia *ia = &xprt->rx_ia; | |
333 | struct rpcrdma_ep *ep = &xprt->rx_ep; | |
f895b252 | 334 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) |
0dd39cae | 335 | struct sockaddr *sap = (struct sockaddr *)&ep->rep_remote_addr; |
ff0db049 | 336 | #endif |
ce1ab9ab CL |
337 | struct ib_qp_attr *attr = &ia->ri_qp_attr; |
338 | struct ib_qp_init_attr *iattr = &ia->ri_qp_init_attr; | |
c56c65fb TT |
339 | int connstate = 0; |
340 | ||
341 | switch (event->event) { | |
342 | case RDMA_CM_EVENT_ADDR_RESOLVED: | |
343 | case RDMA_CM_EVENT_ROUTE_RESOLVED: | |
5675add3 | 344 | ia->ri_async_rc = 0; |
c56c65fb TT |
345 | complete(&ia->ri_done); |
346 | break; | |
347 | case RDMA_CM_EVENT_ADDR_ERROR: | |
348 | ia->ri_async_rc = -EHOSTUNREACH; | |
349 | dprintk("RPC: %s: CM address resolution error, ep 0x%p\n", | |
350 | __func__, ep); | |
351 | complete(&ia->ri_done); | |
352 | break; | |
353 | case RDMA_CM_EVENT_ROUTE_ERROR: | |
354 | ia->ri_async_rc = -ENETUNREACH; | |
355 | dprintk("RPC: %s: CM route resolution error, ep 0x%p\n", | |
356 | __func__, ep); | |
357 | complete(&ia->ri_done); | |
358 | break; | |
359 | case RDMA_CM_EVENT_ESTABLISHED: | |
360 | connstate = 1; | |
ce1ab9ab CL |
361 | ib_query_qp(ia->ri_id->qp, attr, |
362 | IB_QP_MAX_QP_RD_ATOMIC | IB_QP_MAX_DEST_RD_ATOMIC, | |
363 | iattr); | |
c56c65fb TT |
364 | dprintk("RPC: %s: %d responder resources" |
365 | " (%d initiator)\n", | |
ce1ab9ab CL |
366 | __func__, attr->max_dest_rd_atomic, |
367 | attr->max_rd_atomic); | |
c56c65fb TT |
368 | goto connected; |
369 | case RDMA_CM_EVENT_CONNECT_ERROR: | |
370 | connstate = -ENOTCONN; | |
371 | goto connected; | |
372 | case RDMA_CM_EVENT_UNREACHABLE: | |
373 | connstate = -ENETDOWN; | |
374 | goto connected; | |
375 | case RDMA_CM_EVENT_REJECTED: | |
376 | connstate = -ECONNREFUSED; | |
377 | goto connected; | |
378 | case RDMA_CM_EVENT_DISCONNECTED: | |
379 | connstate = -ECONNABORTED; | |
380 | goto connected; | |
381 | case RDMA_CM_EVENT_DEVICE_REMOVAL: | |
382 | connstate = -ENODEV; | |
383 | connected: | |
c56c65fb TT |
384 | dprintk("RPC: %s: %sconnected\n", |
385 | __func__, connstate > 0 ? "" : "dis"); | |
386 | ep->rep_connected = connstate; | |
afadc468 | 387 | rpcrdma_conn_func(ep); |
c56c65fb | 388 | wake_up_all(&ep->rep_connect_wait); |
8079fb78 | 389 | /*FALLTHROUGH*/ |
c56c65fb | 390 | default: |
0dd39cae CL |
391 | dprintk("RPC: %s: %pIS:%u (ep 0x%p): %s\n", |
392 | __func__, sap, rpc_get_port(sap), ep, | |
76357c71 | 393 | rdma_event_msg(event->event)); |
c56c65fb TT |
394 | break; |
395 | } | |
396 | ||
f895b252 | 397 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) |
b3cd8d45 | 398 | if (connstate == 1) { |
ce1ab9ab | 399 | int ird = attr->max_dest_rd_atomic; |
b3cd8d45 | 400 | int tird = ep->rep_remote_cma.responder_resources; |
0dd39cae | 401 | |
a0ce85f5 | 402 | pr_info("rpcrdma: connection to %pIS:%u on %s, memreg '%s', %d credits, %d responders%s\n", |
0dd39cae | 403 | sap, rpc_get_port(sap), |
89e0d112 | 404 | ia->ri_device->name, |
a0ce85f5 | 405 | ia->ri_ops->ro_displayname, |
b3cd8d45 TT |
406 | xprt->rx_buf.rb_max_requests, |
407 | ird, ird < 4 && ird < tird / 2 ? " (low!)" : ""); | |
408 | } else if (connstate < 0) { | |
0dd39cae CL |
409 | pr_info("rpcrdma: connection to %pIS:%u closed (%d)\n", |
410 | sap, rpc_get_port(sap), connstate); | |
b3cd8d45 TT |
411 | } |
412 | #endif | |
413 | ||
c56c65fb TT |
414 | return 0; |
415 | } | |
416 | ||
417 | static struct rdma_cm_id * | |
418 | rpcrdma_create_id(struct rpcrdma_xprt *xprt, | |
419 | struct rpcrdma_ia *ia, struct sockaddr *addr) | |
420 | { | |
421 | struct rdma_cm_id *id; | |
422 | int rc; | |
423 | ||
1a954051 TT |
424 | init_completion(&ia->ri_done); |
425 | ||
b26f9b99 | 426 | id = rdma_create_id(rpcrdma_conn_upcall, xprt, RDMA_PS_TCP, IB_QPT_RC); |
c56c65fb TT |
427 | if (IS_ERR(id)) { |
428 | rc = PTR_ERR(id); | |
429 | dprintk("RPC: %s: rdma_create_id() failed %i\n", | |
430 | __func__, rc); | |
431 | return id; | |
432 | } | |
433 | ||
5675add3 | 434 | ia->ri_async_rc = -ETIMEDOUT; |
c56c65fb TT |
435 | rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT); |
436 | if (rc) { | |
437 | dprintk("RPC: %s: rdma_resolve_addr() failed %i\n", | |
438 | __func__, rc); | |
439 | goto out; | |
440 | } | |
5675add3 TT |
441 | wait_for_completion_interruptible_timeout(&ia->ri_done, |
442 | msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1); | |
c56c65fb TT |
443 | rc = ia->ri_async_rc; |
444 | if (rc) | |
445 | goto out; | |
446 | ||
5675add3 | 447 | ia->ri_async_rc = -ETIMEDOUT; |
c56c65fb TT |
448 | rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT); |
449 | if (rc) { | |
450 | dprintk("RPC: %s: rdma_resolve_route() failed %i\n", | |
451 | __func__, rc); | |
452 | goto out; | |
453 | } | |
5675add3 TT |
454 | wait_for_completion_interruptible_timeout(&ia->ri_done, |
455 | msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1); | |
c56c65fb TT |
456 | rc = ia->ri_async_rc; |
457 | if (rc) | |
458 | goto out; | |
459 | ||
460 | return id; | |
461 | ||
462 | out: | |
463 | rdma_destroy_id(id); | |
464 | return ERR_PTR(rc); | |
465 | } | |
466 | ||
467 | /* | |
468 | * Drain any cq, prior to teardown. | |
469 | */ | |
470 | static void | |
471 | rpcrdma_clean_cq(struct ib_cq *cq) | |
472 | { | |
473 | struct ib_wc wc; | |
474 | int count = 0; | |
475 | ||
476 | while (1 == ib_poll_cq(cq, 1, &wc)) | |
477 | ++count; | |
478 | ||
479 | if (count) | |
480 | dprintk("RPC: %s: flushed %d events (last 0x%x)\n", | |
481 | __func__, count, wc.opcode); | |
482 | } | |
483 | ||
484 | /* | |
485 | * Exported functions. | |
486 | */ | |
487 | ||
488 | /* | |
489 | * Open and initialize an Interface Adapter. | |
490 | * o initializes fields of struct rpcrdma_ia, including | |
491 | * interface and provider attributes and protection zone. | |
492 | */ | |
493 | int | |
494 | rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr, int memreg) | |
495 | { | |
c56c65fb | 496 | struct rpcrdma_ia *ia = &xprt->rx_ia; |
7bc7972c | 497 | struct ib_device_attr *devattr = &ia->ri_devattr; |
d1ed857e CL |
498 | int rc; |
499 | ||
500 | ia->ri_dma_mr = NULL; | |
c56c65fb | 501 | |
c56c65fb TT |
502 | ia->ri_id = rpcrdma_create_id(xprt, ia, addr); |
503 | if (IS_ERR(ia->ri_id)) { | |
504 | rc = PTR_ERR(ia->ri_id); | |
505 | goto out1; | |
506 | } | |
89e0d112 | 507 | ia->ri_device = ia->ri_id->device; |
c56c65fb | 508 | |
89e0d112 | 509 | ia->ri_pd = ib_alloc_pd(ia->ri_device); |
c56c65fb TT |
510 | if (IS_ERR(ia->ri_pd)) { |
511 | rc = PTR_ERR(ia->ri_pd); | |
512 | dprintk("RPC: %s: ib_alloc_pd() failed %i\n", | |
513 | __func__, rc); | |
514 | goto out2; | |
515 | } | |
516 | ||
89e0d112 | 517 | rc = ib_query_device(ia->ri_device, devattr); |
bd7ed1d1 TT |
518 | if (rc) { |
519 | dprintk("RPC: %s: ib_query_device failed %d\n", | |
520 | __func__, rc); | |
5ae711a2 | 521 | goto out3; |
bd7ed1d1 TT |
522 | } |
523 | ||
f10eafd3 | 524 | if (memreg == RPCRDMA_FRMR) { |
3197d309 | 525 | /* Requires both frmr reg and local dma lkey */ |
41f97028 | 526 | if (((devattr->device_cap_flags & |
3197d309 | 527 | (IB_DEVICE_MEM_MGT_EXTENSIONS|IB_DEVICE_LOCAL_DMA_LKEY)) != |
41f97028 CL |
528 | (IB_DEVICE_MEM_MGT_EXTENSIONS|IB_DEVICE_LOCAL_DMA_LKEY)) || |
529 | (devattr->max_fast_reg_page_list_len == 0)) { | |
3197d309 | 530 | dprintk("RPC: %s: FRMR registration " |
f10eafd3 CL |
531 | "not supported by HCA\n", __func__); |
532 | memreg = RPCRDMA_MTHCAFMR; | |
bd7ed1d1 | 533 | } |
f10eafd3 CL |
534 | } |
535 | if (memreg == RPCRDMA_MTHCAFMR) { | |
89e0d112 | 536 | if (!ia->ri_device->alloc_fmr) { |
f10eafd3 CL |
537 | dprintk("RPC: %s: MTHCAFMR registration " |
538 | "not supported by HCA\n", __func__); | |
d2310930 | 539 | goto out3; |
f10eafd3 | 540 | } |
bd7ed1d1 TT |
541 | } |
542 | ||
bd7ed1d1 | 543 | switch (memreg) { |
3197d309 | 544 | case RPCRDMA_FRMR: |
a0ce85f5 | 545 | ia->ri_ops = &rpcrdma_frwr_memreg_ops; |
bd7ed1d1 | 546 | break; |
bd7ed1d1 | 547 | case RPCRDMA_ALLPHYSICAL: |
a0ce85f5 | 548 | ia->ri_ops = &rpcrdma_physical_memreg_ops; |
d1ed857e | 549 | break; |
bd7ed1d1 | 550 | case RPCRDMA_MTHCAFMR: |
a0ce85f5 | 551 | ia->ri_ops = &rpcrdma_fmr_memreg_ops; |
bd7ed1d1 TT |
552 | break; |
553 | default: | |
cdd9ade7 CL |
554 | printk(KERN_ERR "RPC: Unsupported memory " |
555 | "registration mode: %d\n", memreg); | |
556 | rc = -ENOMEM; | |
5ae711a2 | 557 | goto out3; |
c56c65fb | 558 | } |
a0ce85f5 CL |
559 | dprintk("RPC: %s: memory registration strategy is '%s'\n", |
560 | __func__, ia->ri_ops->ro_displayname); | |
c56c65fb | 561 | |
73806c88 | 562 | rwlock_init(&ia->ri_qplock); |
c56c65fb | 563 | return 0; |
5ae711a2 CL |
564 | |
565 | out3: | |
566 | ib_dealloc_pd(ia->ri_pd); | |
567 | ia->ri_pd = NULL; | |
c56c65fb TT |
568 | out2: |
569 | rdma_destroy_id(ia->ri_id); | |
fee08caf | 570 | ia->ri_id = NULL; |
c56c65fb TT |
571 | out1: |
572 | return rc; | |
573 | } | |
574 | ||
575 | /* | |
576 | * Clean up/close an IA. | |
577 | * o if event handles and PD have been initialized, free them. | |
578 | * o close the IA | |
579 | */ | |
580 | void | |
581 | rpcrdma_ia_close(struct rpcrdma_ia *ia) | |
582 | { | |
c56c65fb | 583 | dprintk("RPC: %s: entering\n", __func__); |
fee08caf TT |
584 | if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) { |
585 | if (ia->ri_id->qp) | |
586 | rdma_destroy_qp(ia->ri_id); | |
587 | rdma_destroy_id(ia->ri_id); | |
588 | ia->ri_id = NULL; | |
589 | } | |
6d44698d CL |
590 | |
591 | /* If the pd is still busy, xprtrdma missed freeing a resource */ | |
592 | if (ia->ri_pd && !IS_ERR(ia->ri_pd)) | |
593 | WARN_ON(ib_dealloc_pd(ia->ri_pd)); | |
c56c65fb TT |
594 | } |
595 | ||
596 | /* | |
597 | * Create unconnected endpoint. | |
598 | */ | |
599 | int | |
600 | rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia, | |
601 | struct rpcrdma_create_data_internal *cdata) | |
602 | { | |
7bc7972c | 603 | struct ib_device_attr *devattr = &ia->ri_devattr; |
fc664485 | 604 | struct ib_cq *sendcq, *recvcq; |
8e37210b | 605 | struct ib_cq_init_attr cq_attr = {}; |
5d40a8a5 | 606 | int rc, err; |
c56c65fb | 607 | |
c56c65fb | 608 | /* check provider's send/recv wr limits */ |
7bc7972c CL |
609 | if (cdata->max_requests > devattr->max_qp_wr) |
610 | cdata->max_requests = devattr->max_qp_wr; | |
c56c65fb TT |
611 | |
612 | ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall; | |
613 | ep->rep_attr.qp_context = ep; | |
c56c65fb TT |
614 | ep->rep_attr.srq = NULL; |
615 | ep->rep_attr.cap.max_send_wr = cdata->max_requests; | |
3968cb58 CL |
616 | rc = ia->ri_ops->ro_open(ia, ep, cdata); |
617 | if (rc) | |
618 | return rc; | |
c56c65fb TT |
619 | ep->rep_attr.cap.max_recv_wr = cdata->max_requests; |
620 | ep->rep_attr.cap.max_send_sge = (cdata->padding ? 4 : 2); | |
621 | ep->rep_attr.cap.max_recv_sge = 1; | |
622 | ep->rep_attr.cap.max_inline_data = 0; | |
623 | ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR; | |
624 | ep->rep_attr.qp_type = IB_QPT_RC; | |
625 | ep->rep_attr.port_num = ~0; | |
626 | ||
c05fbb5a CL |
627 | if (cdata->padding) { |
628 | ep->rep_padbuf = rpcrdma_alloc_regbuf(ia, cdata->padding, | |
629 | GFP_KERNEL); | |
d1ed857e CL |
630 | if (IS_ERR(ep->rep_padbuf)) { |
631 | rc = PTR_ERR(ep->rep_padbuf); | |
632 | goto out0; | |
633 | } | |
c05fbb5a CL |
634 | } else |
635 | ep->rep_padbuf = NULL; | |
636 | ||
c56c65fb TT |
637 | dprintk("RPC: %s: requested max: dtos: send %d recv %d; " |
638 | "iovs: send %d recv %d\n", | |
639 | __func__, | |
640 | ep->rep_attr.cap.max_send_wr, | |
641 | ep->rep_attr.cap.max_recv_wr, | |
642 | ep->rep_attr.cap.max_send_sge, | |
643 | ep->rep_attr.cap.max_recv_sge); | |
644 | ||
645 | /* set trigger for requesting send completion */ | |
fc664485 | 646 | ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1; |
e7104a2a CL |
647 | if (ep->rep_cqinit > RPCRDMA_MAX_UNSIGNALED_SENDS) |
648 | ep->rep_cqinit = RPCRDMA_MAX_UNSIGNALED_SENDS; | |
649 | else if (ep->rep_cqinit <= 2) | |
c56c65fb TT |
650 | ep->rep_cqinit = 0; |
651 | INIT_CQCOUNT(ep); | |
c56c65fb | 652 | init_waitqueue_head(&ep->rep_connect_wait); |
254f91e2 | 653 | INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker); |
c56c65fb | 654 | |
8e37210b | 655 | cq_attr.cqe = ep->rep_attr.cap.max_send_wr + 1; |
89e0d112 | 656 | sendcq = ib_create_cq(ia->ri_device, rpcrdma_sendcq_upcall, |
8688d954 | 657 | rpcrdma_cq_async_error_upcall, ep, &cq_attr); |
fc664485 CL |
658 | if (IS_ERR(sendcq)) { |
659 | rc = PTR_ERR(sendcq); | |
660 | dprintk("RPC: %s: failed to create send CQ: %i\n", | |
c56c65fb TT |
661 | __func__, rc); |
662 | goto out1; | |
663 | } | |
664 | ||
fc664485 | 665 | rc = ib_req_notify_cq(sendcq, IB_CQ_NEXT_COMP); |
c56c65fb TT |
666 | if (rc) { |
667 | dprintk("RPC: %s: ib_req_notify_cq failed: %i\n", | |
668 | __func__, rc); | |
669 | goto out2; | |
670 | } | |
671 | ||
8e37210b | 672 | cq_attr.cqe = ep->rep_attr.cap.max_recv_wr + 1; |
89e0d112 | 673 | recvcq = ib_create_cq(ia->ri_device, rpcrdma_recvcq_upcall, |
8688d954 | 674 | rpcrdma_cq_async_error_upcall, ep, &cq_attr); |
fc664485 CL |
675 | if (IS_ERR(recvcq)) { |
676 | rc = PTR_ERR(recvcq); | |
677 | dprintk("RPC: %s: failed to create recv CQ: %i\n", | |
678 | __func__, rc); | |
679 | goto out2; | |
680 | } | |
681 | ||
682 | rc = ib_req_notify_cq(recvcq, IB_CQ_NEXT_COMP); | |
683 | if (rc) { | |
684 | dprintk("RPC: %s: ib_req_notify_cq failed: %i\n", | |
685 | __func__, rc); | |
686 | ib_destroy_cq(recvcq); | |
687 | goto out2; | |
688 | } | |
689 | ||
690 | ep->rep_attr.send_cq = sendcq; | |
691 | ep->rep_attr.recv_cq = recvcq; | |
c56c65fb TT |
692 | |
693 | /* Initialize cma parameters */ | |
694 | ||
695 | /* RPC/RDMA does not use private data */ | |
696 | ep->rep_remote_cma.private_data = NULL; | |
697 | ep->rep_remote_cma.private_data_len = 0; | |
698 | ||
699 | /* Client offers RDMA Read but does not initiate */ | |
b334eaab | 700 | ep->rep_remote_cma.initiator_depth = 0; |
7bc7972c | 701 | if (devattr->max_qp_rd_atom > 32) /* arbitrary but <= 255 */ |
b334eaab TT |
702 | ep->rep_remote_cma.responder_resources = 32; |
703 | else | |
7bc7972c CL |
704 | ep->rep_remote_cma.responder_resources = |
705 | devattr->max_qp_rd_atom; | |
c56c65fb TT |
706 | |
707 | ep->rep_remote_cma.retry_count = 7; | |
708 | ep->rep_remote_cma.flow_control = 0; | |
709 | ep->rep_remote_cma.rnr_retry_count = 0; | |
710 | ||
711 | return 0; | |
712 | ||
713 | out2: | |
fc664485 | 714 | err = ib_destroy_cq(sendcq); |
5d40a8a5 CL |
715 | if (err) |
716 | dprintk("RPC: %s: ib_destroy_cq returned %i\n", | |
717 | __func__, err); | |
c56c65fb | 718 | out1: |
c05fbb5a | 719 | rpcrdma_free_regbuf(ia, ep->rep_padbuf); |
d1ed857e CL |
720 | out0: |
721 | if (ia->ri_dma_mr) | |
722 | ib_dereg_mr(ia->ri_dma_mr); | |
c56c65fb TT |
723 | return rc; |
724 | } | |
725 | ||
726 | /* | |
727 | * rpcrdma_ep_destroy | |
728 | * | |
729 | * Disconnect and destroy endpoint. After this, the only | |
730 | * valid operations on the ep are to free it (if dynamically | |
731 | * allocated) or re-create it. | |
c56c65fb | 732 | */ |
7f1d5419 | 733 | void |
c56c65fb TT |
734 | rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia) |
735 | { | |
736 | int rc; | |
737 | ||
738 | dprintk("RPC: %s: entering, connected is %d\n", | |
739 | __func__, ep->rep_connected); | |
740 | ||
254f91e2 CL |
741 | cancel_delayed_work_sync(&ep->rep_connect_worker); |
742 | ||
c56c65fb | 743 | if (ia->ri_id->qp) { |
282191cb | 744 | rpcrdma_ep_disconnect(ep, ia); |
fee08caf TT |
745 | rdma_destroy_qp(ia->ri_id); |
746 | ia->ri_id->qp = NULL; | |
c56c65fb TT |
747 | } |
748 | ||
c05fbb5a | 749 | rpcrdma_free_regbuf(ia, ep->rep_padbuf); |
c56c65fb | 750 | |
fc664485 CL |
751 | rpcrdma_clean_cq(ep->rep_attr.recv_cq); |
752 | rc = ib_destroy_cq(ep->rep_attr.recv_cq); | |
753 | if (rc) | |
754 | dprintk("RPC: %s: ib_destroy_cq returned %i\n", | |
755 | __func__, rc); | |
756 | ||
757 | rpcrdma_clean_cq(ep->rep_attr.send_cq); | |
758 | rc = ib_destroy_cq(ep->rep_attr.send_cq); | |
c56c65fb TT |
759 | if (rc) |
760 | dprintk("RPC: %s: ib_destroy_cq returned %i\n", | |
761 | __func__, rc); | |
d1ed857e CL |
762 | |
763 | if (ia->ri_dma_mr) { | |
764 | rc = ib_dereg_mr(ia->ri_dma_mr); | |
765 | dprintk("RPC: %s: ib_dereg_mr returned %i\n", | |
766 | __func__, rc); | |
767 | } | |
c56c65fb TT |
768 | } |
769 | ||
770 | /* | |
771 | * Connect unconnected endpoint. | |
772 | */ | |
773 | int | |
774 | rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia) | |
775 | { | |
73806c88 | 776 | struct rdma_cm_id *id, *old; |
c56c65fb TT |
777 | int rc = 0; |
778 | int retry_count = 0; | |
c56c65fb | 779 | |
c055551e | 780 | if (ep->rep_connected != 0) { |
c56c65fb TT |
781 | struct rpcrdma_xprt *xprt; |
782 | retry: | |
ec62f40d | 783 | dprintk("RPC: %s: reconnecting...\n", __func__); |
282191cb CL |
784 | |
785 | rpcrdma_ep_disconnect(ep, ia); | |
a7bc211a | 786 | rpcrdma_flush_cqs(ep); |
c56c65fb TT |
787 | |
788 | xprt = container_of(ia, struct rpcrdma_xprt, rx_ia); | |
789 | id = rpcrdma_create_id(xprt, ia, | |
790 | (struct sockaddr *)&xprt->rx_data.addr); | |
791 | if (IS_ERR(id)) { | |
ec62f40d | 792 | rc = -EHOSTUNREACH; |
c56c65fb TT |
793 | goto out; |
794 | } | |
795 | /* TEMP TEMP TEMP - fail if new device: | |
796 | * Deregister/remarshal *all* requests! | |
797 | * Close and recreate adapter, pd, etc! | |
798 | * Re-determine all attributes still sane! | |
799 | * More stuff I haven't thought of! | |
800 | * Rrrgh! | |
801 | */ | |
89e0d112 | 802 | if (ia->ri_device != id->device) { |
c56c65fb TT |
803 | printk("RPC: %s: can't reconnect on " |
804 | "different device!\n", __func__); | |
805 | rdma_destroy_id(id); | |
ec62f40d | 806 | rc = -ENETUNREACH; |
c56c65fb TT |
807 | goto out; |
808 | } | |
809 | /* END TEMP */ | |
ec62f40d CL |
810 | rc = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr); |
811 | if (rc) { | |
812 | dprintk("RPC: %s: rdma_create_qp failed %i\n", | |
813 | __func__, rc); | |
814 | rdma_destroy_id(id); | |
815 | rc = -ENETUNREACH; | |
816 | goto out; | |
817 | } | |
73806c88 CL |
818 | |
819 | write_lock(&ia->ri_qplock); | |
820 | old = ia->ri_id; | |
c56c65fb | 821 | ia->ri_id = id; |
73806c88 CL |
822 | write_unlock(&ia->ri_qplock); |
823 | ||
824 | rdma_destroy_qp(old); | |
825 | rdma_destroy_id(old); | |
ec62f40d CL |
826 | } else { |
827 | dprintk("RPC: %s: connecting...\n", __func__); | |
828 | rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr); | |
829 | if (rc) { | |
830 | dprintk("RPC: %s: rdma_create_qp failed %i\n", | |
831 | __func__, rc); | |
832 | /* do not update ep->rep_connected */ | |
833 | return -ENETUNREACH; | |
834 | } | |
c56c65fb TT |
835 | } |
836 | ||
c56c65fb TT |
837 | ep->rep_connected = 0; |
838 | ||
839 | rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma); | |
840 | if (rc) { | |
841 | dprintk("RPC: %s: rdma_connect() failed with %i\n", | |
842 | __func__, rc); | |
843 | goto out; | |
844 | } | |
845 | ||
c56c65fb TT |
846 | wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0); |
847 | ||
848 | /* | |
849 | * Check state. A non-peer reject indicates no listener | |
850 | * (ECONNREFUSED), which may be a transient state. All | |
851 | * others indicate a transport condition which has already | |
852 | * undergone a best-effort. | |
853 | */ | |
f64f9e71 JP |
854 | if (ep->rep_connected == -ECONNREFUSED && |
855 | ++retry_count <= RDMA_CONNECT_RETRY_MAX) { | |
c56c65fb TT |
856 | dprintk("RPC: %s: non-peer_reject, retry\n", __func__); |
857 | goto retry; | |
858 | } | |
859 | if (ep->rep_connected <= 0) { | |
860 | /* Sometimes, the only way to reliably connect to remote | |
861 | * CMs is to use same nonzero values for ORD and IRD. */ | |
b334eaab TT |
862 | if (retry_count++ <= RDMA_CONNECT_RETRY_MAX + 1 && |
863 | (ep->rep_remote_cma.responder_resources == 0 || | |
864 | ep->rep_remote_cma.initiator_depth != | |
865 | ep->rep_remote_cma.responder_resources)) { | |
866 | if (ep->rep_remote_cma.responder_resources == 0) | |
867 | ep->rep_remote_cma.responder_resources = 1; | |
868 | ep->rep_remote_cma.initiator_depth = | |
869 | ep->rep_remote_cma.responder_resources; | |
c56c65fb | 870 | goto retry; |
b334eaab | 871 | } |
c56c65fb TT |
872 | rc = ep->rep_connected; |
873 | } else { | |
874 | dprintk("RPC: %s: connected\n", __func__); | |
875 | } | |
876 | ||
877 | out: | |
878 | if (rc) | |
879 | ep->rep_connected = rc; | |
880 | return rc; | |
881 | } | |
882 | ||
883 | /* | |
884 | * rpcrdma_ep_disconnect | |
885 | * | |
886 | * This is separate from destroy to facilitate the ability | |
887 | * to reconnect without recreating the endpoint. | |
888 | * | |
889 | * This call is not reentrant, and must not be made in parallel | |
890 | * on the same endpoint. | |
891 | */ | |
282191cb | 892 | void |
c56c65fb TT |
893 | rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia) |
894 | { | |
895 | int rc; | |
896 | ||
a7bc211a | 897 | rpcrdma_flush_cqs(ep); |
c56c65fb TT |
898 | rc = rdma_disconnect(ia->ri_id); |
899 | if (!rc) { | |
900 | /* returns without wait if not connected */ | |
901 | wait_event_interruptible(ep->rep_connect_wait, | |
902 | ep->rep_connected != 1); | |
903 | dprintk("RPC: %s: after wait, %sconnected\n", __func__, | |
904 | (ep->rep_connected == 1) ? "still " : "dis"); | |
905 | } else { | |
906 | dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc); | |
907 | ep->rep_connected = rc; | |
908 | } | |
c56c65fb TT |
909 | } |
910 | ||
1392402c CL |
911 | static struct rpcrdma_req * |
912 | rpcrdma_create_req(struct rpcrdma_xprt *r_xprt) | |
913 | { | |
1392402c | 914 | struct rpcrdma_req *req; |
1392402c | 915 | |
85275c87 | 916 | req = kzalloc(sizeof(*req), GFP_KERNEL); |
1392402c | 917 | if (req == NULL) |
85275c87 | 918 | return ERR_PTR(-ENOMEM); |
1392402c | 919 | |
1392402c CL |
920 | req->rl_buffer = &r_xprt->rx_buf; |
921 | return req; | |
1392402c CL |
922 | } |
923 | ||
924 | static struct rpcrdma_rep * | |
925 | rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt) | |
926 | { | |
927 | struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data; | |
1392402c CL |
928 | struct rpcrdma_ia *ia = &r_xprt->rx_ia; |
929 | struct rpcrdma_rep *rep; | |
930 | int rc; | |
931 | ||
932 | rc = -ENOMEM; | |
6b1184cd | 933 | rep = kzalloc(sizeof(*rep), GFP_KERNEL); |
1392402c CL |
934 | if (rep == NULL) |
935 | goto out; | |
1392402c | 936 | |
6b1184cd CL |
937 | rep->rr_rdmabuf = rpcrdma_alloc_regbuf(ia, cdata->inline_rsize, |
938 | GFP_KERNEL); | |
939 | if (IS_ERR(rep->rr_rdmabuf)) { | |
940 | rc = PTR_ERR(rep->rr_rdmabuf); | |
1392402c | 941 | goto out_free; |
6b1184cd | 942 | } |
1392402c | 943 | |
89e0d112 | 944 | rep->rr_device = ia->ri_device; |
fed171b3 | 945 | rep->rr_rxprt = r_xprt; |
1392402c CL |
946 | return rep; |
947 | ||
948 | out_free: | |
949 | kfree(rep); | |
950 | out: | |
951 | return ERR_PTR(rc); | |
952 | } | |
953 | ||
c56c65fb | 954 | int |
ac920d04 | 955 | rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt) |
c56c65fb | 956 | { |
ac920d04 CL |
957 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; |
958 | struct rpcrdma_ia *ia = &r_xprt->rx_ia; | |
959 | struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data; | |
c56c65fb | 960 | char *p; |
1392402c | 961 | size_t len; |
c56c65fb TT |
962 | int i, rc; |
963 | ||
964 | buf->rb_max_requests = cdata->max_requests; | |
965 | spin_lock_init(&buf->rb_lock); | |
c56c65fb TT |
966 | |
967 | /* Need to allocate: | |
968 | * 1. arrays for send and recv pointers | |
969 | * 2. arrays of struct rpcrdma_req to fill in pointers | |
970 | * 3. array of struct rpcrdma_rep for replies | |
c56c65fb TT |
971 | * Send/recv buffers in req/rep need to be registered |
972 | */ | |
c56c65fb TT |
973 | len = buf->rb_max_requests * |
974 | (sizeof(struct rpcrdma_req *) + sizeof(struct rpcrdma_rep *)); | |
c56c65fb | 975 | |
c56c65fb TT |
976 | p = kzalloc(len, GFP_KERNEL); |
977 | if (p == NULL) { | |
978 | dprintk("RPC: %s: req_t/rep_t/pad kzalloc(%zd) failed\n", | |
979 | __func__, len); | |
980 | rc = -ENOMEM; | |
981 | goto out; | |
982 | } | |
983 | buf->rb_pool = p; /* for freeing it later */ | |
984 | ||
985 | buf->rb_send_bufs = (struct rpcrdma_req **) p; | |
986 | p = (char *) &buf->rb_send_bufs[buf->rb_max_requests]; | |
987 | buf->rb_recv_bufs = (struct rpcrdma_rep **) p; | |
988 | p = (char *) &buf->rb_recv_bufs[buf->rb_max_requests]; | |
989 | ||
91e70e70 CL |
990 | rc = ia->ri_ops->ro_init(r_xprt); |
991 | if (rc) | |
992 | goto out; | |
c56c65fb | 993 | |
c56c65fb TT |
994 | for (i = 0; i < buf->rb_max_requests; i++) { |
995 | struct rpcrdma_req *req; | |
996 | struct rpcrdma_rep *rep; | |
997 | ||
1392402c CL |
998 | req = rpcrdma_create_req(r_xprt); |
999 | if (IS_ERR(req)) { | |
c56c65fb TT |
1000 | dprintk("RPC: %s: request buffer %d alloc" |
1001 | " failed\n", __func__, i); | |
1392402c | 1002 | rc = PTR_ERR(req); |
c56c65fb TT |
1003 | goto out; |
1004 | } | |
c56c65fb | 1005 | buf->rb_send_bufs[i] = req; |
c56c65fb | 1006 | |
1392402c CL |
1007 | rep = rpcrdma_create_rep(r_xprt); |
1008 | if (IS_ERR(rep)) { | |
c56c65fb TT |
1009 | dprintk("RPC: %s: reply buffer %d alloc failed\n", |
1010 | __func__, i); | |
1392402c | 1011 | rc = PTR_ERR(rep); |
c56c65fb TT |
1012 | goto out; |
1013 | } | |
c56c65fb | 1014 | buf->rb_recv_bufs[i] = rep; |
c56c65fb | 1015 | } |
1392402c | 1016 | |
c56c65fb TT |
1017 | return 0; |
1018 | out: | |
1019 | rpcrdma_buffer_destroy(buf); | |
1020 | return rc; | |
1021 | } | |
1022 | ||
1392402c CL |
1023 | static void |
1024 | rpcrdma_destroy_rep(struct rpcrdma_ia *ia, struct rpcrdma_rep *rep) | |
1025 | { | |
1026 | if (!rep) | |
1027 | return; | |
1028 | ||
6b1184cd | 1029 | rpcrdma_free_regbuf(ia, rep->rr_rdmabuf); |
1392402c CL |
1030 | kfree(rep); |
1031 | } | |
1032 | ||
1033 | static void | |
1034 | rpcrdma_destroy_req(struct rpcrdma_ia *ia, struct rpcrdma_req *req) | |
1035 | { | |
1036 | if (!req) | |
1037 | return; | |
1038 | ||
0ca77dc3 | 1039 | rpcrdma_free_regbuf(ia, req->rl_sendbuf); |
85275c87 | 1040 | rpcrdma_free_regbuf(ia, req->rl_rdmabuf); |
1392402c CL |
1041 | kfree(req); |
1042 | } | |
1043 | ||
c56c65fb TT |
1044 | void |
1045 | rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf) | |
1046 | { | |
c56c65fb | 1047 | struct rpcrdma_ia *ia = rdmab_to_ia(buf); |
2e84522c | 1048 | int i; |
c56c65fb TT |
1049 | |
1050 | /* clean up in reverse order from create | |
1051 | * 1. recv mr memory (mr free, then kfree) | |
c56c65fb | 1052 | * 2. send mr memory (mr free, then kfree) |
2e84522c | 1053 | * 3. MWs |
c56c65fb TT |
1054 | */ |
1055 | dprintk("RPC: %s: entering\n", __func__); | |
1056 | ||
1057 | for (i = 0; i < buf->rb_max_requests; i++) { | |
1392402c CL |
1058 | if (buf->rb_recv_bufs) |
1059 | rpcrdma_destroy_rep(ia, buf->rb_recv_bufs[i]); | |
1060 | if (buf->rb_send_bufs) | |
1061 | rpcrdma_destroy_req(ia, buf->rb_send_bufs[i]); | |
c56c65fb TT |
1062 | } |
1063 | ||
4561f347 | 1064 | ia->ri_ops->ro_destroy(buf); |
4034ba04 | 1065 | |
c56c65fb TT |
1066 | kfree(buf->rb_pool); |
1067 | } | |
1068 | ||
346aa66b CL |
1069 | struct rpcrdma_mw * |
1070 | rpcrdma_get_mw(struct rpcrdma_xprt *r_xprt) | |
c2922c02 | 1071 | { |
346aa66b CL |
1072 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; |
1073 | struct rpcrdma_mw *mw = NULL; | |
346aa66b | 1074 | |
58d1dcf5 | 1075 | spin_lock(&buf->rb_mwlock); |
346aa66b CL |
1076 | if (!list_empty(&buf->rb_mws)) { |
1077 | mw = list_first_entry(&buf->rb_mws, | |
1078 | struct rpcrdma_mw, mw_list); | |
1079 | list_del_init(&mw->mw_list); | |
c2922c02 | 1080 | } |
58d1dcf5 | 1081 | spin_unlock(&buf->rb_mwlock); |
346aa66b CL |
1082 | |
1083 | if (!mw) | |
1084 | pr_err("RPC: %s: no MWs available\n", __func__); | |
1085 | return mw; | |
c2922c02 CL |
1086 | } |
1087 | ||
346aa66b CL |
1088 | void |
1089 | rpcrdma_put_mw(struct rpcrdma_xprt *r_xprt, struct rpcrdma_mw *mw) | |
c2922c02 | 1090 | { |
346aa66b | 1091 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; |
c2922c02 | 1092 | |
58d1dcf5 | 1093 | spin_lock(&buf->rb_mwlock); |
346aa66b | 1094 | list_add_tail(&mw->mw_list, &buf->rb_mws); |
58d1dcf5 | 1095 | spin_unlock(&buf->rb_mwlock); |
c2922c02 CL |
1096 | } |
1097 | ||
1098 | static void | |
1099 | rpcrdma_buffer_put_sendbuf(struct rpcrdma_req *req, struct rpcrdma_buffer *buf) | |
1100 | { | |
1101 | buf->rb_send_bufs[--buf->rb_send_index] = req; | |
1102 | req->rl_niovs = 0; | |
1103 | if (req->rl_reply) { | |
1104 | buf->rb_recv_bufs[--buf->rb_recv_index] = req->rl_reply; | |
c2922c02 CL |
1105 | req->rl_reply = NULL; |
1106 | } | |
1107 | } | |
1108 | ||
c56c65fb TT |
1109 | /* |
1110 | * Get a set of request/reply buffers. | |
1111 | * | |
1112 | * Reply buffer (if needed) is attached to send buffer upon return. | |
1113 | * Rule: | |
1114 | * rb_send_index and rb_recv_index MUST always be pointing to the | |
1115 | * *next* available buffer (non-NULL). They are incremented after | |
1116 | * removing buffers, and decremented *before* returning them. | |
1117 | */ | |
1118 | struct rpcrdma_req * | |
1119 | rpcrdma_buffer_get(struct rpcrdma_buffer *buffers) | |
1120 | { | |
1121 | struct rpcrdma_req *req; | |
1122 | unsigned long flags; | |
1123 | ||
1124 | spin_lock_irqsave(&buffers->rb_lock, flags); | |
c14d86e5 | 1125 | |
c56c65fb TT |
1126 | if (buffers->rb_send_index == buffers->rb_max_requests) { |
1127 | spin_unlock_irqrestore(&buffers->rb_lock, flags); | |
1128 | dprintk("RPC: %s: out of request buffers\n", __func__); | |
1129 | return ((struct rpcrdma_req *)NULL); | |
1130 | } | |
1131 | ||
1132 | req = buffers->rb_send_bufs[buffers->rb_send_index]; | |
1133 | if (buffers->rb_send_index < buffers->rb_recv_index) { | |
1134 | dprintk("RPC: %s: %d extra receives outstanding (ok)\n", | |
1135 | __func__, | |
1136 | buffers->rb_recv_index - buffers->rb_send_index); | |
1137 | req->rl_reply = NULL; | |
1138 | } else { | |
1139 | req->rl_reply = buffers->rb_recv_bufs[buffers->rb_recv_index]; | |
1140 | buffers->rb_recv_bufs[buffers->rb_recv_index++] = NULL; | |
1141 | } | |
1142 | buffers->rb_send_bufs[buffers->rb_send_index++] = NULL; | |
ddb6bebc | 1143 | |
c56c65fb TT |
1144 | spin_unlock_irqrestore(&buffers->rb_lock, flags); |
1145 | return req; | |
1146 | } | |
1147 | ||
1148 | /* | |
1149 | * Put request/reply buffers back into pool. | |
1150 | * Pre-decrement counter/array index. | |
1151 | */ | |
1152 | void | |
1153 | rpcrdma_buffer_put(struct rpcrdma_req *req) | |
1154 | { | |
1155 | struct rpcrdma_buffer *buffers = req->rl_buffer; | |
c56c65fb TT |
1156 | unsigned long flags; |
1157 | ||
c56c65fb | 1158 | spin_lock_irqsave(&buffers->rb_lock, flags); |
c2922c02 | 1159 | rpcrdma_buffer_put_sendbuf(req, buffers); |
c56c65fb TT |
1160 | spin_unlock_irqrestore(&buffers->rb_lock, flags); |
1161 | } | |
1162 | ||
1163 | /* | |
1164 | * Recover reply buffers from pool. | |
1165 | * This happens when recovering from error conditions. | |
1166 | * Post-increment counter/array index. | |
1167 | */ | |
1168 | void | |
1169 | rpcrdma_recv_buffer_get(struct rpcrdma_req *req) | |
1170 | { | |
1171 | struct rpcrdma_buffer *buffers = req->rl_buffer; | |
1172 | unsigned long flags; | |
1173 | ||
c56c65fb TT |
1174 | spin_lock_irqsave(&buffers->rb_lock, flags); |
1175 | if (buffers->rb_recv_index < buffers->rb_max_requests) { | |
1176 | req->rl_reply = buffers->rb_recv_bufs[buffers->rb_recv_index]; | |
1177 | buffers->rb_recv_bufs[buffers->rb_recv_index++] = NULL; | |
1178 | } | |
1179 | spin_unlock_irqrestore(&buffers->rb_lock, flags); | |
1180 | } | |
1181 | ||
1182 | /* | |
1183 | * Put reply buffers back into pool when not attached to | |
b45ccfd2 | 1184 | * request. This happens in error conditions. |
c56c65fb TT |
1185 | */ |
1186 | void | |
1187 | rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep) | |
1188 | { | |
fed171b3 | 1189 | struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf; |
c56c65fb TT |
1190 | unsigned long flags; |
1191 | ||
c56c65fb TT |
1192 | spin_lock_irqsave(&buffers->rb_lock, flags); |
1193 | buffers->rb_recv_bufs[--buffers->rb_recv_index] = rep; | |
1194 | spin_unlock_irqrestore(&buffers->rb_lock, flags); | |
1195 | } | |
1196 | ||
1197 | /* | |
1198 | * Wrappers for internal-use kmalloc memory registration, used by buffer code. | |
1199 | */ | |
1200 | ||
d654788e CL |
1201 | void |
1202 | rpcrdma_mapping_error(struct rpcrdma_mr_seg *seg) | |
1203 | { | |
1204 | dprintk("RPC: map_one: offset %p iova %llx len %zu\n", | |
1205 | seg->mr_offset, | |
1206 | (unsigned long long)seg->mr_dma, seg->mr_dmalen); | |
1207 | } | |
1208 | ||
9128c3e7 CL |
1209 | /** |
1210 | * rpcrdma_alloc_regbuf - kmalloc and register memory for SEND/RECV buffers | |
1211 | * @ia: controlling rpcrdma_ia | |
1212 | * @size: size of buffer to be allocated, in bytes | |
1213 | * @flags: GFP flags | |
1214 | * | |
1215 | * Returns pointer to private header of an area of internally | |
1216 | * registered memory, or an ERR_PTR. The registered buffer follows | |
1217 | * the end of the private header. | |
1218 | * | |
1219 | * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for | |
1220 | * receiving the payload of RDMA RECV operations. regbufs are not | |
1221 | * used for RDMA READ/WRITE operations, thus are registered only for | |
1222 | * LOCAL access. | |
1223 | */ | |
1224 | struct rpcrdma_regbuf * | |
1225 | rpcrdma_alloc_regbuf(struct rpcrdma_ia *ia, size_t size, gfp_t flags) | |
1226 | { | |
1227 | struct rpcrdma_regbuf *rb; | |
e531dcab | 1228 | struct ib_sge *iov; |
9128c3e7 | 1229 | |
9128c3e7 CL |
1230 | rb = kmalloc(sizeof(*rb) + size, flags); |
1231 | if (rb == NULL) | |
1232 | goto out; | |
1233 | ||
e531dcab CL |
1234 | iov = &rb->rg_iov; |
1235 | iov->addr = ib_dma_map_single(ia->ri_device, | |
1236 | (void *)rb->rg_base, size, | |
1237 | DMA_BIDIRECTIONAL); | |
1238 | if (ib_dma_mapping_error(ia->ri_device, iov->addr)) | |
9128c3e7 CL |
1239 | goto out_free; |
1240 | ||
e531dcab | 1241 | iov->length = size; |
d1ed857e | 1242 | iov->lkey = ia->ri_dma_lkey; |
e531dcab CL |
1243 | rb->rg_size = size; |
1244 | rb->rg_owner = NULL; | |
9128c3e7 CL |
1245 | return rb; |
1246 | ||
1247 | out_free: | |
1248 | kfree(rb); | |
1249 | out: | |
e531dcab | 1250 | return ERR_PTR(-ENOMEM); |
9128c3e7 CL |
1251 | } |
1252 | ||
1253 | /** | |
1254 | * rpcrdma_free_regbuf - deregister and free registered buffer | |
1255 | * @ia: controlling rpcrdma_ia | |
1256 | * @rb: regbuf to be deregistered and freed | |
1257 | */ | |
1258 | void | |
1259 | rpcrdma_free_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb) | |
1260 | { | |
e531dcab CL |
1261 | struct ib_sge *iov; |
1262 | ||
1263 | if (!rb) | |
1264 | return; | |
1265 | ||
1266 | iov = &rb->rg_iov; | |
1267 | ib_dma_unmap_single(ia->ri_device, | |
1268 | iov->addr, iov->length, DMA_BIDIRECTIONAL); | |
1269 | kfree(rb); | |
9128c3e7 CL |
1270 | } |
1271 | ||
c56c65fb TT |
1272 | /* |
1273 | * Prepost any receive buffer, then post send. | |
1274 | * | |
1275 | * Receive buffer is donated to hardware, reclaimed upon recv completion. | |
1276 | */ | |
1277 | int | |
1278 | rpcrdma_ep_post(struct rpcrdma_ia *ia, | |
1279 | struct rpcrdma_ep *ep, | |
1280 | struct rpcrdma_req *req) | |
1281 | { | |
1282 | struct ib_send_wr send_wr, *send_wr_fail; | |
1283 | struct rpcrdma_rep *rep = req->rl_reply; | |
1284 | int rc; | |
1285 | ||
1286 | if (rep) { | |
1287 | rc = rpcrdma_ep_post_recv(ia, ep, rep); | |
1288 | if (rc) | |
1289 | goto out; | |
1290 | req->rl_reply = NULL; | |
1291 | } | |
1292 | ||
1293 | send_wr.next = NULL; | |
e46ac34c | 1294 | send_wr.wr_id = RPCRDMA_IGNORE_COMPLETION; |
c56c65fb TT |
1295 | send_wr.sg_list = req->rl_send_iov; |
1296 | send_wr.num_sge = req->rl_niovs; | |
1297 | send_wr.opcode = IB_WR_SEND; | |
c56c65fb | 1298 | if (send_wr.num_sge == 4) /* no need to sync any pad (constant) */ |
89e0d112 CL |
1299 | ib_dma_sync_single_for_device(ia->ri_device, |
1300 | req->rl_send_iov[3].addr, | |
1301 | req->rl_send_iov[3].length, | |
1302 | DMA_TO_DEVICE); | |
1303 | ib_dma_sync_single_for_device(ia->ri_device, | |
1304 | req->rl_send_iov[1].addr, | |
1305 | req->rl_send_iov[1].length, | |
1306 | DMA_TO_DEVICE); | |
1307 | ib_dma_sync_single_for_device(ia->ri_device, | |
1308 | req->rl_send_iov[0].addr, | |
1309 | req->rl_send_iov[0].length, | |
1310 | DMA_TO_DEVICE); | |
c56c65fb TT |
1311 | |
1312 | if (DECR_CQCOUNT(ep) > 0) | |
1313 | send_wr.send_flags = 0; | |
1314 | else { /* Provider must take a send completion every now and then */ | |
1315 | INIT_CQCOUNT(ep); | |
1316 | send_wr.send_flags = IB_SEND_SIGNALED; | |
1317 | } | |
1318 | ||
1319 | rc = ib_post_send(ia->ri_id->qp, &send_wr, &send_wr_fail); | |
1320 | if (rc) | |
1321 | dprintk("RPC: %s: ib_post_send returned %i\n", __func__, | |
1322 | rc); | |
1323 | out: | |
1324 | return rc; | |
1325 | } | |
1326 | ||
1327 | /* | |
1328 | * (Re)post a receive buffer. | |
1329 | */ | |
1330 | int | |
1331 | rpcrdma_ep_post_recv(struct rpcrdma_ia *ia, | |
1332 | struct rpcrdma_ep *ep, | |
1333 | struct rpcrdma_rep *rep) | |
1334 | { | |
1335 | struct ib_recv_wr recv_wr, *recv_wr_fail; | |
1336 | int rc; | |
1337 | ||
1338 | recv_wr.next = NULL; | |
1339 | recv_wr.wr_id = (u64) (unsigned long) rep; | |
6b1184cd | 1340 | recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov; |
c56c65fb TT |
1341 | recv_wr.num_sge = 1; |
1342 | ||
89e0d112 | 1343 | ib_dma_sync_single_for_cpu(ia->ri_device, |
6b1184cd CL |
1344 | rdmab_addr(rep->rr_rdmabuf), |
1345 | rdmab_length(rep->rr_rdmabuf), | |
1346 | DMA_BIDIRECTIONAL); | |
c56c65fb | 1347 | |
c56c65fb TT |
1348 | rc = ib_post_recv(ia->ri_id->qp, &recv_wr, &recv_wr_fail); |
1349 | ||
1350 | if (rc) | |
1351 | dprintk("RPC: %s: ib_post_recv returned %i\n", __func__, | |
1352 | rc); | |
1353 | return rc; | |
1354 | } | |
43e95988 | 1355 | |
1c9351ee | 1356 | /* How many chunk list items fit within our inline buffers? |
43e95988 | 1357 | */ |
1c9351ee CL |
1358 | unsigned int |
1359 | rpcrdma_max_segments(struct rpcrdma_xprt *r_xprt) | |
43e95988 CL |
1360 | { |
1361 | struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data; | |
1c9351ee | 1362 | int bytes, segments; |
43e95988 | 1363 | |
1c9351ee CL |
1364 | bytes = min_t(unsigned int, cdata->inline_wsize, cdata->inline_rsize); |
1365 | bytes -= RPCRDMA_HDRLEN_MIN; | |
1366 | if (bytes < sizeof(struct rpcrdma_segment) * 2) { | |
1367 | pr_warn("RPC: %s: inline threshold too small\n", | |
1368 | __func__); | |
1369 | return 0; | |
43e95988 | 1370 | } |
1c9351ee CL |
1371 | |
1372 | segments = 1 << (fls(bytes / sizeof(struct rpcrdma_segment)) - 1); | |
1373 | dprintk("RPC: %s: max chunk list size = %d segments\n", | |
1374 | __func__, segments); | |
1375 | return segments; | |
43e95988 | 1376 | } |