\"Talpey, Thomas\ | f58851e | 2007-09-10 13:50:12 -0400 | [diff] [blame] | 1 | /* |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 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. |
\"Talpey, Thomas\ | f58851e | 2007-09-10 13:50:12 -0400 | [diff] [blame] | 38 | */ |
| 39 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 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 | |
Alexey Dobriyan | a6b7a40 | 2011-06-06 10:43:46 +0000 | [diff] [blame] | 50 | #include <linux/interrupt.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 51 | #include <linux/slab.h> |
Chuck Lever | eba8ff6 | 2015-01-21 11:03:02 -0500 | [diff] [blame] | 52 | #include <linux/prefetch.h> |
Chuck Lever | 65866f8 | 2014-05-28 10:33:59 -0400 | [diff] [blame] | 53 | #include <asm/bitops.h> |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 54 | |
\"Talpey, Thomas\ | f58851e | 2007-09-10 13:50:12 -0400 | [diff] [blame] | 55 | #include "xprt_rdma.h" |
| 56 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 57 | /* |
| 58 | * Globals/Macros |
| 59 | */ |
| 60 | |
Jeff Layton | f895b25 | 2014-11-17 16:58:04 -0500 | [diff] [blame] | 61 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 62 | # define RPCDBG_FACILITY RPCDBG_TRANS |
| 63 | #endif |
| 64 | |
Chuck Lever | 9f9d802 | 2014-07-29 17:24:45 -0400 | [diff] [blame] | 65 | static void rpcrdma_reset_frmrs(struct rpcrdma_ia *); |
Chuck Lever | 467c967 | 2014-11-08 20:14:29 -0500 | [diff] [blame] | 66 | static void rpcrdma_reset_fmrs(struct rpcrdma_ia *); |
Chuck Lever | 9f9d802 | 2014-07-29 17:24:45 -0400 | [diff] [blame] | 67 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 68 | /* |
| 69 | * internal functions |
| 70 | */ |
| 71 | |
| 72 | /* |
| 73 | * handle replies in tasklet context, using a single, global list |
| 74 | * rdma tasklet function -- just turn around and call the func |
| 75 | * for all replies on the list |
| 76 | */ |
| 77 | |
| 78 | static DEFINE_SPINLOCK(rpcrdma_tk_lock_g); |
| 79 | static LIST_HEAD(rpcrdma_tasklets_g); |
| 80 | |
| 81 | static void |
| 82 | rpcrdma_run_tasklet(unsigned long data) |
| 83 | { |
| 84 | struct rpcrdma_rep *rep; |
| 85 | void (*func)(struct rpcrdma_rep *); |
| 86 | unsigned long flags; |
| 87 | |
| 88 | data = data; |
| 89 | spin_lock_irqsave(&rpcrdma_tk_lock_g, flags); |
| 90 | while (!list_empty(&rpcrdma_tasklets_g)) { |
| 91 | rep = list_entry(rpcrdma_tasklets_g.next, |
| 92 | struct rpcrdma_rep, rr_list); |
| 93 | list_del(&rep->rr_list); |
| 94 | func = rep->rr_func; |
| 95 | rep->rr_func = NULL; |
| 96 | spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags); |
| 97 | |
| 98 | if (func) |
| 99 | func(rep); |
| 100 | else |
| 101 | rpcrdma_recv_buffer_put(rep); |
| 102 | |
| 103 | spin_lock_irqsave(&rpcrdma_tk_lock_g, flags); |
| 104 | } |
| 105 | spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags); |
| 106 | } |
| 107 | |
| 108 | static DECLARE_TASKLET(rpcrdma_tasklet_g, rpcrdma_run_tasklet, 0UL); |
| 109 | |
Chuck Lever | 7ff11de | 2014-11-08 20:15:01 -0500 | [diff] [blame] | 110 | static const char * const async_event[] = { |
| 111 | "CQ error", |
| 112 | "QP fatal error", |
| 113 | "QP request error", |
| 114 | "QP access error", |
| 115 | "communication established", |
| 116 | "send queue drained", |
| 117 | "path migration successful", |
| 118 | "path mig error", |
| 119 | "device fatal error", |
| 120 | "port active", |
| 121 | "port error", |
| 122 | "LID change", |
| 123 | "P_key change", |
| 124 | "SM change", |
| 125 | "SRQ error", |
| 126 | "SRQ limit reached", |
| 127 | "last WQE reached", |
| 128 | "client reregister", |
| 129 | "GID change", |
| 130 | }; |
| 131 | |
| 132 | #define ASYNC_MSG(status) \ |
| 133 | ((status) < ARRAY_SIZE(async_event) ? \ |
| 134 | async_event[(status)] : "unknown async error") |
| 135 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 136 | static void |
Chuck Lever | f1a03b7 | 2014-11-08 20:14:37 -0500 | [diff] [blame] | 137 | rpcrdma_schedule_tasklet(struct list_head *sched_list) |
| 138 | { |
| 139 | unsigned long flags; |
| 140 | |
| 141 | spin_lock_irqsave(&rpcrdma_tk_lock_g, flags); |
| 142 | list_splice_tail(sched_list, &rpcrdma_tasklets_g); |
| 143 | spin_unlock_irqrestore(&rpcrdma_tk_lock_g, flags); |
| 144 | tasklet_schedule(&rpcrdma_tasklet_g); |
| 145 | } |
| 146 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 147 | static void |
| 148 | rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context) |
| 149 | { |
| 150 | struct rpcrdma_ep *ep = context; |
| 151 | |
Chuck Lever | 7ff11de | 2014-11-08 20:15:01 -0500 | [diff] [blame] | 152 | pr_err("RPC: %s: %s on device %s ep %p\n", |
| 153 | __func__, ASYNC_MSG(event->event), |
| 154 | event->device->name, context); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 155 | if (ep->rep_connected == 1) { |
| 156 | ep->rep_connected = -EIO; |
Chuck Lever | afadc46 | 2015-01-21 11:03:11 -0500 | [diff] [blame] | 157 | rpcrdma_conn_func(ep); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 158 | wake_up_all(&ep->rep_connect_wait); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | static void |
| 163 | rpcrdma_cq_async_error_upcall(struct ib_event *event, void *context) |
| 164 | { |
| 165 | struct rpcrdma_ep *ep = context; |
| 166 | |
Chuck Lever | 7ff11de | 2014-11-08 20:15:01 -0500 | [diff] [blame] | 167 | pr_err("RPC: %s: %s on device %s ep %p\n", |
| 168 | __func__, ASYNC_MSG(event->event), |
| 169 | event->device->name, context); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 170 | if (ep->rep_connected == 1) { |
| 171 | ep->rep_connected = -EIO; |
Chuck Lever | afadc46 | 2015-01-21 11:03:11 -0500 | [diff] [blame] | 172 | rpcrdma_conn_func(ep); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 173 | wake_up_all(&ep->rep_connect_wait); |
| 174 | } |
| 175 | } |
| 176 | |
Chuck Lever | 8502427 | 2015-01-21 11:02:04 -0500 | [diff] [blame] | 177 | static const char * const wc_status[] = { |
| 178 | "success", |
| 179 | "local length error", |
| 180 | "local QP operation error", |
| 181 | "local EE context operation error", |
| 182 | "local protection error", |
| 183 | "WR flushed", |
| 184 | "memory management operation error", |
| 185 | "bad response error", |
| 186 | "local access error", |
| 187 | "remote invalid request error", |
| 188 | "remote access error", |
| 189 | "remote operation error", |
| 190 | "transport retry counter exceeded", |
| 191 | "RNR retrycounter exceeded", |
| 192 | "local RDD violation error", |
| 193 | "remove invalid RD request", |
| 194 | "operation aborted", |
| 195 | "invalid EE context number", |
| 196 | "invalid EE context state", |
| 197 | "fatal error", |
| 198 | "response timeout error", |
| 199 | "general error", |
| 200 | }; |
| 201 | |
| 202 | #define COMPLETION_MSG(status) \ |
| 203 | ((status) < ARRAY_SIZE(wc_status) ? \ |
| 204 | wc_status[(status)] : "unexpected completion error") |
| 205 | |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 206 | static void |
| 207 | rpcrdma_sendcq_process_wc(struct ib_wc *wc) |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 208 | { |
Chuck Lever | 8502427 | 2015-01-21 11:02:04 -0500 | [diff] [blame] | 209 | if (likely(wc->status == IB_WC_SUCCESS)) |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 210 | return; |
Chuck Lever | 8502427 | 2015-01-21 11:02:04 -0500 | [diff] [blame] | 211 | |
| 212 | /* WARNING: Only wr_id and status are reliable at this point */ |
| 213 | if (wc->wr_id == 0ULL) { |
| 214 | if (wc->status != IB_WC_WR_FLUSH_ERR) |
| 215 | pr_err("RPC: %s: SEND: %s\n", |
| 216 | __func__, COMPLETION_MSG(wc->status)); |
| 217 | } else { |
| 218 | struct rpcrdma_mw *r; |
| 219 | |
| 220 | r = (struct rpcrdma_mw *)(unsigned long)wc->wr_id; |
| 221 | r->r.frmr.fr_state = FRMR_IS_STALE; |
| 222 | pr_err("RPC: %s: frmr %p (stale): %s\n", |
| 223 | __func__, r, COMPLETION_MSG(wc->status)); |
| 224 | } |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 225 | } |
| 226 | |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 227 | static int |
Chuck Lever | 1c00dd0 | 2014-05-28 10:33:42 -0400 | [diff] [blame] | 228 | rpcrdma_sendcq_poll(struct ib_cq *cq, struct rpcrdma_ep *ep) |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 229 | { |
Chuck Lever | 1c00dd0 | 2014-05-28 10:33:42 -0400 | [diff] [blame] | 230 | struct ib_wc *wcs; |
Chuck Lever | 8301a2c | 2014-05-28 10:33:51 -0400 | [diff] [blame] | 231 | int budget, count, rc; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 232 | |
Chuck Lever | 8301a2c | 2014-05-28 10:33:51 -0400 | [diff] [blame] | 233 | budget = RPCRDMA_WC_BUDGET / RPCRDMA_POLLSIZE; |
Chuck Lever | 1c00dd0 | 2014-05-28 10:33:42 -0400 | [diff] [blame] | 234 | do { |
| 235 | wcs = ep->rep_send_wcs; |
| 236 | |
| 237 | rc = ib_poll_cq(cq, RPCRDMA_POLLSIZE, wcs); |
| 238 | if (rc <= 0) |
| 239 | return rc; |
| 240 | |
| 241 | count = rc; |
| 242 | while (count-- > 0) |
| 243 | rpcrdma_sendcq_process_wc(wcs++); |
Chuck Lever | 8301a2c | 2014-05-28 10:33:51 -0400 | [diff] [blame] | 244 | } while (rc == RPCRDMA_POLLSIZE && --budget); |
Chuck Lever | 1c00dd0 | 2014-05-28 10:33:42 -0400 | [diff] [blame] | 245 | return 0; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /* |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 249 | * Handle send, fast_reg_mr, and local_inv completions. |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 250 | * |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 251 | * Send events are typically suppressed and thus do not result |
| 252 | * in an upcall. Occasionally one is signaled, however. This |
| 253 | * prevents the provider's completion queue from wrapping and |
| 254 | * losing a completion. |
| 255 | */ |
| 256 | static void |
| 257 | rpcrdma_sendcq_upcall(struct ib_cq *cq, void *cq_context) |
| 258 | { |
Chuck Lever | 1c00dd0 | 2014-05-28 10:33:42 -0400 | [diff] [blame] | 259 | struct rpcrdma_ep *ep = (struct rpcrdma_ep *)cq_context; |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 260 | int rc; |
| 261 | |
Chuck Lever | 1c00dd0 | 2014-05-28 10:33:42 -0400 | [diff] [blame] | 262 | rc = rpcrdma_sendcq_poll(cq, ep); |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 263 | if (rc) { |
| 264 | dprintk("RPC: %s: ib_poll_cq failed: %i\n", |
| 265 | __func__, rc); |
| 266 | return; |
| 267 | } |
| 268 | |
Chuck Lever | 7f23f6f | 2014-05-28 10:33:34 -0400 | [diff] [blame] | 269 | rc = ib_req_notify_cq(cq, |
| 270 | IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS); |
| 271 | if (rc == 0) |
| 272 | return; |
| 273 | if (rc < 0) { |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 274 | dprintk("RPC: %s: ib_req_notify_cq failed: %i\n", |
| 275 | __func__, rc); |
| 276 | return; |
| 277 | } |
| 278 | |
Chuck Lever | 1c00dd0 | 2014-05-28 10:33:42 -0400 | [diff] [blame] | 279 | rpcrdma_sendcq_poll(cq, ep); |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | static void |
Chuck Lever | bb96193 | 2014-07-29 17:25:46 -0400 | [diff] [blame] | 283 | rpcrdma_recvcq_process_wc(struct ib_wc *wc, struct list_head *sched_list) |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 284 | { |
| 285 | struct rpcrdma_rep *rep = |
| 286 | (struct rpcrdma_rep *)(unsigned long)wc->wr_id; |
| 287 | |
Chuck Lever | 8502427 | 2015-01-21 11:02:04 -0500 | [diff] [blame] | 288 | /* WARNING: Only wr_id and status are reliable at this point */ |
| 289 | if (wc->status != IB_WC_SUCCESS) |
| 290 | goto out_fail; |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 291 | |
Chuck Lever | 8502427 | 2015-01-21 11:02:04 -0500 | [diff] [blame] | 292 | /* status == SUCCESS means all fields in wc are trustworthy */ |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 293 | if (wc->opcode != IB_WC_RECV) |
| 294 | return; |
| 295 | |
Chuck Lever | 8502427 | 2015-01-21 11:02:04 -0500 | [diff] [blame] | 296 | dprintk("RPC: %s: rep %p opcode 'recv', length %u: success\n", |
| 297 | __func__, rep, wc->byte_len); |
| 298 | |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 299 | rep->rr_len = wc->byte_len; |
| 300 | ib_dma_sync_single_for_cpu(rdmab_to_ia(rep->rr_buffer)->ri_id->device, |
| 301 | rep->rr_iov.addr, rep->rr_len, DMA_FROM_DEVICE); |
Chuck Lever | eba8ff6 | 2015-01-21 11:03:02 -0500 | [diff] [blame] | 302 | prefetch(rep->rr_base); |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 303 | |
| 304 | out_schedule: |
Chuck Lever | bb96193 | 2014-07-29 17:25:46 -0400 | [diff] [blame] | 305 | list_add_tail(&rep->rr_list, sched_list); |
Chuck Lever | 8502427 | 2015-01-21 11:02:04 -0500 | [diff] [blame] | 306 | return; |
| 307 | out_fail: |
| 308 | if (wc->status != IB_WC_WR_FLUSH_ERR) |
| 309 | pr_err("RPC: %s: rep %p: %s\n", |
| 310 | __func__, rep, COMPLETION_MSG(wc->status)); |
| 311 | rep->rr_len = ~0U; |
| 312 | goto out_schedule; |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | static int |
Chuck Lever | 1c00dd0 | 2014-05-28 10:33:42 -0400 | [diff] [blame] | 316 | rpcrdma_recvcq_poll(struct ib_cq *cq, struct rpcrdma_ep *ep) |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 317 | { |
Chuck Lever | bb96193 | 2014-07-29 17:25:46 -0400 | [diff] [blame] | 318 | struct list_head sched_list; |
Chuck Lever | 1c00dd0 | 2014-05-28 10:33:42 -0400 | [diff] [blame] | 319 | struct ib_wc *wcs; |
Chuck Lever | 8301a2c | 2014-05-28 10:33:51 -0400 | [diff] [blame] | 320 | int budget, count, rc; |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 321 | |
Chuck Lever | bb96193 | 2014-07-29 17:25:46 -0400 | [diff] [blame] | 322 | INIT_LIST_HEAD(&sched_list); |
Chuck Lever | 8301a2c | 2014-05-28 10:33:51 -0400 | [diff] [blame] | 323 | budget = RPCRDMA_WC_BUDGET / RPCRDMA_POLLSIZE; |
Chuck Lever | 1c00dd0 | 2014-05-28 10:33:42 -0400 | [diff] [blame] | 324 | do { |
| 325 | wcs = ep->rep_recv_wcs; |
| 326 | |
| 327 | rc = ib_poll_cq(cq, RPCRDMA_POLLSIZE, wcs); |
| 328 | if (rc <= 0) |
Chuck Lever | bb96193 | 2014-07-29 17:25:46 -0400 | [diff] [blame] | 329 | goto out_schedule; |
Chuck Lever | 1c00dd0 | 2014-05-28 10:33:42 -0400 | [diff] [blame] | 330 | |
| 331 | count = rc; |
| 332 | while (count-- > 0) |
Chuck Lever | bb96193 | 2014-07-29 17:25:46 -0400 | [diff] [blame] | 333 | rpcrdma_recvcq_process_wc(wcs++, &sched_list); |
Chuck Lever | 8301a2c | 2014-05-28 10:33:51 -0400 | [diff] [blame] | 334 | } while (rc == RPCRDMA_POLLSIZE && --budget); |
Chuck Lever | bb96193 | 2014-07-29 17:25:46 -0400 | [diff] [blame] | 335 | rc = 0; |
| 336 | |
| 337 | out_schedule: |
Chuck Lever | f1a03b7 | 2014-11-08 20:14:37 -0500 | [diff] [blame] | 338 | rpcrdma_schedule_tasklet(&sched_list); |
Chuck Lever | bb96193 | 2014-07-29 17:25:46 -0400 | [diff] [blame] | 339 | return rc; |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /* |
| 343 | * Handle receive completions. |
| 344 | * |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 345 | * It is reentrant but processes single events in order to maintain |
| 346 | * ordering of receives to keep server credits. |
| 347 | * |
| 348 | * It is the responsibility of the scheduled tasklet to return |
| 349 | * recv buffers to the pool. NOTE: this affects synchronization of |
| 350 | * connection shutdown. That is, the structures required for |
| 351 | * the completion of the reply handler must remain intact until |
| 352 | * all memory has been reclaimed. |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 353 | */ |
| 354 | static void |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 355 | rpcrdma_recvcq_upcall(struct ib_cq *cq, void *cq_context) |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 356 | { |
Chuck Lever | 1c00dd0 | 2014-05-28 10:33:42 -0400 | [diff] [blame] | 357 | struct rpcrdma_ep *ep = (struct rpcrdma_ep *)cq_context; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 358 | int rc; |
| 359 | |
Chuck Lever | 1c00dd0 | 2014-05-28 10:33:42 -0400 | [diff] [blame] | 360 | rc = rpcrdma_recvcq_poll(cq, ep); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 361 | if (rc) { |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 362 | dprintk("RPC: %s: ib_poll_cq failed: %i\n", |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 363 | __func__, rc); |
| 364 | return; |
| 365 | } |
| 366 | |
Chuck Lever | 7f23f6f | 2014-05-28 10:33:34 -0400 | [diff] [blame] | 367 | rc = ib_req_notify_cq(cq, |
| 368 | IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS); |
| 369 | if (rc == 0) |
| 370 | return; |
| 371 | if (rc < 0) { |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 372 | dprintk("RPC: %s: ib_req_notify_cq failed: %i\n", |
| 373 | __func__, rc); |
| 374 | return; |
| 375 | } |
| 376 | |
Chuck Lever | 1c00dd0 | 2014-05-28 10:33:42 -0400 | [diff] [blame] | 377 | rpcrdma_recvcq_poll(cq, ep); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 378 | } |
| 379 | |
Chuck Lever | a7bc211 | 2014-07-29 17:23:52 -0400 | [diff] [blame] | 380 | static void |
| 381 | rpcrdma_flush_cqs(struct rpcrdma_ep *ep) |
| 382 | { |
Chuck Lever | 5c166be | 2014-11-08 20:14:45 -0500 | [diff] [blame] | 383 | struct ib_wc wc; |
| 384 | LIST_HEAD(sched_list); |
| 385 | |
| 386 | while (ib_poll_cq(ep->rep_attr.recv_cq, 1, &wc) > 0) |
| 387 | rpcrdma_recvcq_process_wc(&wc, &sched_list); |
| 388 | if (!list_empty(&sched_list)) |
| 389 | rpcrdma_schedule_tasklet(&sched_list); |
| 390 | while (ib_poll_cq(ep->rep_attr.send_cq, 1, &wc) > 0) |
| 391 | rpcrdma_sendcq_process_wc(&wc); |
Chuck Lever | a7bc211 | 2014-07-29 17:23:52 -0400 | [diff] [blame] | 392 | } |
| 393 | |
Jeff Layton | f895b25 | 2014-11-17 16:58:04 -0500 | [diff] [blame] | 394 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 395 | static const char * const conn[] = { |
| 396 | "address resolved", |
| 397 | "address error", |
| 398 | "route resolved", |
| 399 | "route error", |
| 400 | "connect request", |
| 401 | "connect response", |
| 402 | "connect error", |
| 403 | "unreachable", |
| 404 | "rejected", |
| 405 | "established", |
| 406 | "disconnected", |
Chuck Lever | 8079fb7 | 2014-07-29 17:26:12 -0400 | [diff] [blame] | 407 | "device removal", |
| 408 | "multicast join", |
| 409 | "multicast error", |
| 410 | "address change", |
| 411 | "timewait exit", |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 412 | }; |
Chuck Lever | 8079fb7 | 2014-07-29 17:26:12 -0400 | [diff] [blame] | 413 | |
| 414 | #define CONNECTION_MSG(status) \ |
| 415 | ((status) < ARRAY_SIZE(conn) ? \ |
| 416 | conn[(status)] : "unrecognized connection error") |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 417 | #endif |
| 418 | |
| 419 | static int |
| 420 | rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event) |
| 421 | { |
| 422 | struct rpcrdma_xprt *xprt = id->context; |
| 423 | struct rpcrdma_ia *ia = &xprt->rx_ia; |
| 424 | struct rpcrdma_ep *ep = &xprt->rx_ep; |
Jeff Layton | f895b25 | 2014-11-17 16:58:04 -0500 | [diff] [blame] | 425 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 426 | struct sockaddr_in *addr = (struct sockaddr_in *) &ep->rep_remote_addr; |
Ingo Molnar | ff0db04 | 2008-11-25 16:58:42 -0800 | [diff] [blame] | 427 | #endif |
Chuck Lever | ce1ab9a | 2015-01-21 11:03:35 -0500 | [diff] [blame] | 428 | struct ib_qp_attr *attr = &ia->ri_qp_attr; |
| 429 | struct ib_qp_init_attr *iattr = &ia->ri_qp_init_attr; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 430 | int connstate = 0; |
| 431 | |
| 432 | switch (event->event) { |
| 433 | case RDMA_CM_EVENT_ADDR_RESOLVED: |
| 434 | case RDMA_CM_EVENT_ROUTE_RESOLVED: |
Tom Talpey | 5675add | 2008-10-09 15:01:41 -0400 | [diff] [blame] | 435 | ia->ri_async_rc = 0; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 436 | complete(&ia->ri_done); |
| 437 | break; |
| 438 | case RDMA_CM_EVENT_ADDR_ERROR: |
| 439 | ia->ri_async_rc = -EHOSTUNREACH; |
| 440 | dprintk("RPC: %s: CM address resolution error, ep 0x%p\n", |
| 441 | __func__, ep); |
| 442 | complete(&ia->ri_done); |
| 443 | break; |
| 444 | case RDMA_CM_EVENT_ROUTE_ERROR: |
| 445 | ia->ri_async_rc = -ENETUNREACH; |
| 446 | dprintk("RPC: %s: CM route resolution error, ep 0x%p\n", |
| 447 | __func__, ep); |
| 448 | complete(&ia->ri_done); |
| 449 | break; |
| 450 | case RDMA_CM_EVENT_ESTABLISHED: |
| 451 | connstate = 1; |
Chuck Lever | ce1ab9a | 2015-01-21 11:03:35 -0500 | [diff] [blame] | 452 | ib_query_qp(ia->ri_id->qp, attr, |
| 453 | IB_QP_MAX_QP_RD_ATOMIC | IB_QP_MAX_DEST_RD_ATOMIC, |
| 454 | iattr); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 455 | dprintk("RPC: %s: %d responder resources" |
| 456 | " (%d initiator)\n", |
Chuck Lever | ce1ab9a | 2015-01-21 11:03:35 -0500 | [diff] [blame] | 457 | __func__, attr->max_dest_rd_atomic, |
| 458 | attr->max_rd_atomic); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 459 | goto connected; |
| 460 | case RDMA_CM_EVENT_CONNECT_ERROR: |
| 461 | connstate = -ENOTCONN; |
| 462 | goto connected; |
| 463 | case RDMA_CM_EVENT_UNREACHABLE: |
| 464 | connstate = -ENETDOWN; |
| 465 | goto connected; |
| 466 | case RDMA_CM_EVENT_REJECTED: |
| 467 | connstate = -ECONNREFUSED; |
| 468 | goto connected; |
| 469 | case RDMA_CM_EVENT_DISCONNECTED: |
| 470 | connstate = -ECONNABORTED; |
| 471 | goto connected; |
| 472 | case RDMA_CM_EVENT_DEVICE_REMOVAL: |
| 473 | connstate = -ENODEV; |
| 474 | connected: |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 475 | dprintk("RPC: %s: %sconnected\n", |
| 476 | __func__, connstate > 0 ? "" : "dis"); |
| 477 | ep->rep_connected = connstate; |
Chuck Lever | afadc46 | 2015-01-21 11:03:11 -0500 | [diff] [blame] | 478 | rpcrdma_conn_func(ep); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 479 | wake_up_all(&ep->rep_connect_wait); |
Chuck Lever | 8079fb7 | 2014-07-29 17:26:12 -0400 | [diff] [blame] | 480 | /*FALLTHROUGH*/ |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 481 | default: |
Chuck Lever | 8079fb7 | 2014-07-29 17:26:12 -0400 | [diff] [blame] | 482 | dprintk("RPC: %s: %pI4:%u (ep 0x%p): %s\n", |
| 483 | __func__, &addr->sin_addr.s_addr, |
| 484 | ntohs(addr->sin_port), ep, |
| 485 | CONNECTION_MSG(event->event)); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 486 | break; |
| 487 | } |
| 488 | |
Jeff Layton | f895b25 | 2014-11-17 16:58:04 -0500 | [diff] [blame] | 489 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) |
Tom Talpey | b3cd8d4 | 2008-10-09 15:02:02 -0400 | [diff] [blame] | 490 | if (connstate == 1) { |
Chuck Lever | ce1ab9a | 2015-01-21 11:03:35 -0500 | [diff] [blame] | 491 | int ird = attr->max_dest_rd_atomic; |
Tom Talpey | b3cd8d4 | 2008-10-09 15:02:02 -0400 | [diff] [blame] | 492 | int tird = ep->rep_remote_cma.responder_resources; |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 493 | printk(KERN_INFO "rpcrdma: connection to %pI4:%u " |
Tom Talpey | b3cd8d4 | 2008-10-09 15:02:02 -0400 | [diff] [blame] | 494 | "on %s, memreg %d slots %d ird %d%s\n", |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 495 | &addr->sin_addr.s_addr, |
Tom Talpey | b3cd8d4 | 2008-10-09 15:02:02 -0400 | [diff] [blame] | 496 | ntohs(addr->sin_port), |
| 497 | ia->ri_id->device->name, |
| 498 | ia->ri_memreg_strategy, |
| 499 | xprt->rx_buf.rb_max_requests, |
| 500 | ird, ird < 4 && ird < tird / 2 ? " (low!)" : ""); |
| 501 | } else if (connstate < 0) { |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 502 | printk(KERN_INFO "rpcrdma: connection to %pI4:%u closed (%d)\n", |
| 503 | &addr->sin_addr.s_addr, |
Tom Talpey | b3cd8d4 | 2008-10-09 15:02:02 -0400 | [diff] [blame] | 504 | ntohs(addr->sin_port), |
| 505 | connstate); |
| 506 | } |
| 507 | #endif |
| 508 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | static struct rdma_cm_id * |
| 513 | rpcrdma_create_id(struct rpcrdma_xprt *xprt, |
| 514 | struct rpcrdma_ia *ia, struct sockaddr *addr) |
| 515 | { |
| 516 | struct rdma_cm_id *id; |
| 517 | int rc; |
| 518 | |
Tom Talpey | 1a95405 | 2008-10-09 15:01:31 -0400 | [diff] [blame] | 519 | init_completion(&ia->ri_done); |
| 520 | |
Sean Hefty | b26f9b9 | 2010-04-01 17:08:41 +0000 | [diff] [blame] | 521 | id = rdma_create_id(rpcrdma_conn_upcall, xprt, RDMA_PS_TCP, IB_QPT_RC); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 522 | if (IS_ERR(id)) { |
| 523 | rc = PTR_ERR(id); |
| 524 | dprintk("RPC: %s: rdma_create_id() failed %i\n", |
| 525 | __func__, rc); |
| 526 | return id; |
| 527 | } |
| 528 | |
Tom Talpey | 5675add | 2008-10-09 15:01:41 -0400 | [diff] [blame] | 529 | ia->ri_async_rc = -ETIMEDOUT; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 530 | rc = rdma_resolve_addr(id, NULL, addr, RDMA_RESOLVE_TIMEOUT); |
| 531 | if (rc) { |
| 532 | dprintk("RPC: %s: rdma_resolve_addr() failed %i\n", |
| 533 | __func__, rc); |
| 534 | goto out; |
| 535 | } |
Tom Talpey | 5675add | 2008-10-09 15:01:41 -0400 | [diff] [blame] | 536 | wait_for_completion_interruptible_timeout(&ia->ri_done, |
| 537 | msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 538 | rc = ia->ri_async_rc; |
| 539 | if (rc) |
| 540 | goto out; |
| 541 | |
Tom Talpey | 5675add | 2008-10-09 15:01:41 -0400 | [diff] [blame] | 542 | ia->ri_async_rc = -ETIMEDOUT; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 543 | rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT); |
| 544 | if (rc) { |
| 545 | dprintk("RPC: %s: rdma_resolve_route() failed %i\n", |
| 546 | __func__, rc); |
| 547 | goto out; |
| 548 | } |
Tom Talpey | 5675add | 2008-10-09 15:01:41 -0400 | [diff] [blame] | 549 | wait_for_completion_interruptible_timeout(&ia->ri_done, |
| 550 | msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 551 | rc = ia->ri_async_rc; |
| 552 | if (rc) |
| 553 | goto out; |
| 554 | |
| 555 | return id; |
| 556 | |
| 557 | out: |
| 558 | rdma_destroy_id(id); |
| 559 | return ERR_PTR(rc); |
| 560 | } |
| 561 | |
| 562 | /* |
| 563 | * Drain any cq, prior to teardown. |
| 564 | */ |
| 565 | static void |
| 566 | rpcrdma_clean_cq(struct ib_cq *cq) |
| 567 | { |
| 568 | struct ib_wc wc; |
| 569 | int count = 0; |
| 570 | |
| 571 | while (1 == ib_poll_cq(cq, 1, &wc)) |
| 572 | ++count; |
| 573 | |
| 574 | if (count) |
| 575 | dprintk("RPC: %s: flushed %d events (last 0x%x)\n", |
| 576 | __func__, count, wc.opcode); |
| 577 | } |
| 578 | |
| 579 | /* |
| 580 | * Exported functions. |
| 581 | */ |
| 582 | |
| 583 | /* |
| 584 | * Open and initialize an Interface Adapter. |
| 585 | * o initializes fields of struct rpcrdma_ia, including |
| 586 | * interface and provider attributes and protection zone. |
| 587 | */ |
| 588 | int |
| 589 | rpcrdma_ia_open(struct rpcrdma_xprt *xprt, struct sockaddr *addr, int memreg) |
| 590 | { |
Tom Talpey | bd7ed1d | 2008-10-09 15:00:09 -0400 | [diff] [blame] | 591 | int rc, mem_priv; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 592 | struct rpcrdma_ia *ia = &xprt->rx_ia; |
Chuck Lever | 7bc7972 | 2015-01-21 11:03:27 -0500 | [diff] [blame] | 593 | struct ib_device_attr *devattr = &ia->ri_devattr; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 594 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 595 | ia->ri_id = rpcrdma_create_id(xprt, ia, addr); |
| 596 | if (IS_ERR(ia->ri_id)) { |
| 597 | rc = PTR_ERR(ia->ri_id); |
| 598 | goto out1; |
| 599 | } |
| 600 | |
| 601 | ia->ri_pd = ib_alloc_pd(ia->ri_id->device); |
| 602 | if (IS_ERR(ia->ri_pd)) { |
| 603 | rc = PTR_ERR(ia->ri_pd); |
| 604 | dprintk("RPC: %s: ib_alloc_pd() failed %i\n", |
| 605 | __func__, rc); |
| 606 | goto out2; |
| 607 | } |
| 608 | |
Chuck Lever | 7bc7972 | 2015-01-21 11:03:27 -0500 | [diff] [blame] | 609 | rc = ib_query_device(ia->ri_id->device, devattr); |
Tom Talpey | bd7ed1d | 2008-10-09 15:00:09 -0400 | [diff] [blame] | 610 | if (rc) { |
| 611 | dprintk("RPC: %s: ib_query_device failed %d\n", |
| 612 | __func__, rc); |
Chuck Lever | 5ae711a | 2015-01-21 11:03:19 -0500 | [diff] [blame] | 613 | goto out3; |
Tom Talpey | bd7ed1d | 2008-10-09 15:00:09 -0400 | [diff] [blame] | 614 | } |
| 615 | |
Chuck Lever | 7bc7972 | 2015-01-21 11:03:27 -0500 | [diff] [blame] | 616 | if (devattr->device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY) { |
Tom Talpey | bd7ed1d | 2008-10-09 15:00:09 -0400 | [diff] [blame] | 617 | ia->ri_have_dma_lkey = 1; |
| 618 | ia->ri_dma_lkey = ia->ri_id->device->local_dma_lkey; |
| 619 | } |
| 620 | |
Chuck Lever | f10eafd | 2014-05-28 10:32:51 -0400 | [diff] [blame] | 621 | if (memreg == RPCRDMA_FRMR) { |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 622 | /* Requires both frmr reg and local dma lkey */ |
Chuck Lever | 7bc7972 | 2015-01-21 11:03:27 -0500 | [diff] [blame] | 623 | if ((devattr->device_cap_flags & |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 624 | (IB_DEVICE_MEM_MGT_EXTENSIONS|IB_DEVICE_LOCAL_DMA_LKEY)) != |
| 625 | (IB_DEVICE_MEM_MGT_EXTENSIONS|IB_DEVICE_LOCAL_DMA_LKEY)) { |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 626 | dprintk("RPC: %s: FRMR registration " |
Chuck Lever | f10eafd | 2014-05-28 10:32:51 -0400 | [diff] [blame] | 627 | "not supported by HCA\n", __func__); |
| 628 | memreg = RPCRDMA_MTHCAFMR; |
Steve Wise | 0fc6c4e | 2014-05-28 10:32:00 -0400 | [diff] [blame] | 629 | } else { |
| 630 | /* Mind the ia limit on FRMR page list depth */ |
| 631 | ia->ri_max_frmr_depth = min_t(unsigned int, |
| 632 | RPCRDMA_MAX_DATA_SEGS, |
Chuck Lever | 7bc7972 | 2015-01-21 11:03:27 -0500 | [diff] [blame] | 633 | devattr->max_fast_reg_page_list_len); |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 634 | } |
Chuck Lever | f10eafd | 2014-05-28 10:32:51 -0400 | [diff] [blame] | 635 | } |
| 636 | if (memreg == RPCRDMA_MTHCAFMR) { |
| 637 | if (!ia->ri_id->device->alloc_fmr) { |
| 638 | dprintk("RPC: %s: MTHCAFMR registration " |
| 639 | "not supported by HCA\n", __func__); |
Chuck Lever | f10eafd | 2014-05-28 10:32:51 -0400 | [diff] [blame] | 640 | memreg = RPCRDMA_ALLPHYSICAL; |
Chuck Lever | f10eafd | 2014-05-28 10:32:51 -0400 | [diff] [blame] | 641 | } |
Tom Talpey | bd7ed1d | 2008-10-09 15:00:09 -0400 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | /* |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 645 | * Optionally obtain an underlying physical identity mapping in |
| 646 | * order to do a memory window-based bind. This base registration |
| 647 | * is protected from remote access - that is enabled only by binding |
| 648 | * for the specific bytes targeted during each RPC operation, and |
| 649 | * revoked after the corresponding completion similar to a storage |
| 650 | * adapter. |
| 651 | */ |
Tom Talpey | bd7ed1d | 2008-10-09 15:00:09 -0400 | [diff] [blame] | 652 | switch (memreg) { |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 653 | case RPCRDMA_FRMR: |
Tom Talpey | bd7ed1d | 2008-10-09 15:00:09 -0400 | [diff] [blame] | 654 | break; |
Tom Talpey | bd7ed1d | 2008-10-09 15:00:09 -0400 | [diff] [blame] | 655 | case RPCRDMA_ALLPHYSICAL: |
| 656 | mem_priv = IB_ACCESS_LOCAL_WRITE | |
| 657 | IB_ACCESS_REMOTE_WRITE | |
| 658 | IB_ACCESS_REMOTE_READ; |
| 659 | goto register_setup; |
Tom Talpey | bd7ed1d | 2008-10-09 15:00:09 -0400 | [diff] [blame] | 660 | case RPCRDMA_MTHCAFMR: |
| 661 | if (ia->ri_have_dma_lkey) |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 662 | break; |
Tom Talpey | bd7ed1d | 2008-10-09 15:00:09 -0400 | [diff] [blame] | 663 | mem_priv = IB_ACCESS_LOCAL_WRITE; |
| 664 | register_setup: |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 665 | ia->ri_bind_mem = ib_get_dma_mr(ia->ri_pd, mem_priv); |
| 666 | if (IS_ERR(ia->ri_bind_mem)) { |
| 667 | printk(KERN_ALERT "%s: ib_get_dma_mr for " |
Chuck Lever | 0ac531c | 2014-05-28 10:32:43 -0400 | [diff] [blame] | 668 | "phys register failed with %lX\n", |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 669 | __func__, PTR_ERR(ia->ri_bind_mem)); |
Chuck Lever | 0ac531c | 2014-05-28 10:32:43 -0400 | [diff] [blame] | 670 | rc = -ENOMEM; |
Chuck Lever | 5ae711a | 2015-01-21 11:03:19 -0500 | [diff] [blame] | 671 | goto out3; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 672 | } |
Tom Talpey | bd7ed1d | 2008-10-09 15:00:09 -0400 | [diff] [blame] | 673 | break; |
| 674 | default: |
Chuck Lever | cdd9ade | 2014-05-28 10:33:00 -0400 | [diff] [blame] | 675 | printk(KERN_ERR "RPC: Unsupported memory " |
| 676 | "registration mode: %d\n", memreg); |
| 677 | rc = -ENOMEM; |
Chuck Lever | 5ae711a | 2015-01-21 11:03:19 -0500 | [diff] [blame] | 678 | goto out3; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 679 | } |
Tom Talpey | bd7ed1d | 2008-10-09 15:00:09 -0400 | [diff] [blame] | 680 | dprintk("RPC: %s: memory registration strategy is %d\n", |
| 681 | __func__, memreg); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 682 | |
| 683 | /* Else will do memory reg/dereg for each chunk */ |
| 684 | ia->ri_memreg_strategy = memreg; |
| 685 | |
Chuck Lever | 73806c8 | 2014-07-29 17:23:25 -0400 | [diff] [blame] | 686 | rwlock_init(&ia->ri_qplock); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 687 | return 0; |
Chuck Lever | 5ae711a | 2015-01-21 11:03:19 -0500 | [diff] [blame] | 688 | |
| 689 | out3: |
| 690 | ib_dealloc_pd(ia->ri_pd); |
| 691 | ia->ri_pd = NULL; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 692 | out2: |
| 693 | rdma_destroy_id(ia->ri_id); |
Tom Talpey | fee08ca | 2008-10-09 15:01:00 -0400 | [diff] [blame] | 694 | ia->ri_id = NULL; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 695 | out1: |
| 696 | return rc; |
| 697 | } |
| 698 | |
| 699 | /* |
| 700 | * Clean up/close an IA. |
| 701 | * o if event handles and PD have been initialized, free them. |
| 702 | * o close the IA |
| 703 | */ |
| 704 | void |
| 705 | rpcrdma_ia_close(struct rpcrdma_ia *ia) |
| 706 | { |
| 707 | int rc; |
| 708 | |
| 709 | dprintk("RPC: %s: entering\n", __func__); |
| 710 | if (ia->ri_bind_mem != NULL) { |
| 711 | rc = ib_dereg_mr(ia->ri_bind_mem); |
| 712 | dprintk("RPC: %s: ib_dereg_mr returned %i\n", |
| 713 | __func__, rc); |
| 714 | } |
Tom Talpey | fee08ca | 2008-10-09 15:01:00 -0400 | [diff] [blame] | 715 | if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) { |
| 716 | if (ia->ri_id->qp) |
| 717 | rdma_destroy_qp(ia->ri_id); |
| 718 | rdma_destroy_id(ia->ri_id); |
| 719 | ia->ri_id = NULL; |
| 720 | } |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 721 | if (ia->ri_pd != NULL && !IS_ERR(ia->ri_pd)) { |
| 722 | rc = ib_dealloc_pd(ia->ri_pd); |
| 723 | dprintk("RPC: %s: ib_dealloc_pd returned %i\n", |
| 724 | __func__, rc); |
| 725 | } |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | /* |
| 729 | * Create unconnected endpoint. |
| 730 | */ |
| 731 | int |
| 732 | rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia, |
| 733 | struct rpcrdma_create_data_internal *cdata) |
| 734 | { |
Chuck Lever | 7bc7972 | 2015-01-21 11:03:27 -0500 | [diff] [blame] | 735 | struct ib_device_attr *devattr = &ia->ri_devattr; |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 736 | struct ib_cq *sendcq, *recvcq; |
Chuck Lever | 5d40a8a | 2007-10-26 13:30:54 -0400 | [diff] [blame] | 737 | int rc, err; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 738 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 739 | /* check provider's send/recv wr limits */ |
Chuck Lever | 7bc7972 | 2015-01-21 11:03:27 -0500 | [diff] [blame] | 740 | if (cdata->max_requests > devattr->max_qp_wr) |
| 741 | cdata->max_requests = devattr->max_qp_wr; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 742 | |
| 743 | ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall; |
| 744 | ep->rep_attr.qp_context = ep; |
| 745 | /* send_cq and recv_cq initialized below */ |
| 746 | ep->rep_attr.srq = NULL; |
| 747 | ep->rep_attr.cap.max_send_wr = cdata->max_requests; |
| 748 | switch (ia->ri_memreg_strategy) { |
Steve Wise | 0fc6c4e | 2014-05-28 10:32:00 -0400 | [diff] [blame] | 749 | case RPCRDMA_FRMR: { |
| 750 | int depth = 7; |
| 751 | |
Tom Tucker | 15cdc644 | 2010-08-11 12:47:24 -0400 | [diff] [blame] | 752 | /* Add room for frmr register and invalidate WRs. |
| 753 | * 1. FRMR reg WR for head |
| 754 | * 2. FRMR invalidate WR for head |
Steve Wise | 0fc6c4e | 2014-05-28 10:32:00 -0400 | [diff] [blame] | 755 | * 3. N FRMR reg WRs for pagelist |
| 756 | * 4. N FRMR invalidate WRs for pagelist |
Tom Tucker | 15cdc644 | 2010-08-11 12:47:24 -0400 | [diff] [blame] | 757 | * 5. FRMR reg WR for tail |
| 758 | * 6. FRMR invalidate WR for tail |
| 759 | * 7. The RDMA_SEND WR |
| 760 | */ |
Steve Wise | 0fc6c4e | 2014-05-28 10:32:00 -0400 | [diff] [blame] | 761 | |
| 762 | /* Calculate N if the device max FRMR depth is smaller than |
| 763 | * RPCRDMA_MAX_DATA_SEGS. |
| 764 | */ |
| 765 | if (ia->ri_max_frmr_depth < RPCRDMA_MAX_DATA_SEGS) { |
| 766 | int delta = RPCRDMA_MAX_DATA_SEGS - |
| 767 | ia->ri_max_frmr_depth; |
| 768 | |
| 769 | do { |
| 770 | depth += 2; /* FRMR reg + invalidate */ |
| 771 | delta -= ia->ri_max_frmr_depth; |
| 772 | } while (delta > 0); |
| 773 | |
| 774 | } |
| 775 | ep->rep_attr.cap.max_send_wr *= depth; |
Chuck Lever | 7bc7972 | 2015-01-21 11:03:27 -0500 | [diff] [blame] | 776 | if (ep->rep_attr.cap.max_send_wr > devattr->max_qp_wr) { |
| 777 | cdata->max_requests = devattr->max_qp_wr / depth; |
Tom Tucker | 15cdc644 | 2010-08-11 12:47:24 -0400 | [diff] [blame] | 778 | if (!cdata->max_requests) |
| 779 | return -EINVAL; |
Steve Wise | 0fc6c4e | 2014-05-28 10:32:00 -0400 | [diff] [blame] | 780 | ep->rep_attr.cap.max_send_wr = cdata->max_requests * |
| 781 | depth; |
Tom Tucker | 15cdc644 | 2010-08-11 12:47:24 -0400 | [diff] [blame] | 782 | } |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 783 | break; |
Steve Wise | 0fc6c4e | 2014-05-28 10:32:00 -0400 | [diff] [blame] | 784 | } |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 785 | default: |
| 786 | break; |
| 787 | } |
| 788 | ep->rep_attr.cap.max_recv_wr = cdata->max_requests; |
| 789 | ep->rep_attr.cap.max_send_sge = (cdata->padding ? 4 : 2); |
| 790 | ep->rep_attr.cap.max_recv_sge = 1; |
| 791 | ep->rep_attr.cap.max_inline_data = 0; |
| 792 | ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR; |
| 793 | ep->rep_attr.qp_type = IB_QPT_RC; |
| 794 | ep->rep_attr.port_num = ~0; |
| 795 | |
| 796 | dprintk("RPC: %s: requested max: dtos: send %d recv %d; " |
| 797 | "iovs: send %d recv %d\n", |
| 798 | __func__, |
| 799 | ep->rep_attr.cap.max_send_wr, |
| 800 | ep->rep_attr.cap.max_recv_wr, |
| 801 | ep->rep_attr.cap.max_send_sge, |
| 802 | ep->rep_attr.cap.max_recv_sge); |
| 803 | |
| 804 | /* set trigger for requesting send completion */ |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 805 | ep->rep_cqinit = ep->rep_attr.cap.max_send_wr/2 - 1; |
Chuck Lever | e7104a2 | 2014-11-08 20:14:20 -0500 | [diff] [blame] | 806 | if (ep->rep_cqinit > RPCRDMA_MAX_UNSIGNALED_SENDS) |
| 807 | ep->rep_cqinit = RPCRDMA_MAX_UNSIGNALED_SENDS; |
| 808 | else if (ep->rep_cqinit <= 2) |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 809 | ep->rep_cqinit = 0; |
| 810 | INIT_CQCOUNT(ep); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 811 | init_waitqueue_head(&ep->rep_connect_wait); |
Chuck Lever | 254f91e | 2014-05-28 10:32:17 -0400 | [diff] [blame] | 812 | INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 813 | |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 814 | sendcq = ib_create_cq(ia->ri_id->device, rpcrdma_sendcq_upcall, |
Chuck Lever | 1c00dd0 | 2014-05-28 10:33:42 -0400 | [diff] [blame] | 815 | rpcrdma_cq_async_error_upcall, ep, |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 816 | ep->rep_attr.cap.max_send_wr + 1, 0); |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 817 | if (IS_ERR(sendcq)) { |
| 818 | rc = PTR_ERR(sendcq); |
| 819 | dprintk("RPC: %s: failed to create send CQ: %i\n", |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 820 | __func__, rc); |
| 821 | goto out1; |
| 822 | } |
| 823 | |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 824 | rc = ib_req_notify_cq(sendcq, IB_CQ_NEXT_COMP); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 825 | if (rc) { |
| 826 | dprintk("RPC: %s: ib_req_notify_cq failed: %i\n", |
| 827 | __func__, rc); |
| 828 | goto out2; |
| 829 | } |
| 830 | |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 831 | recvcq = ib_create_cq(ia->ri_id->device, rpcrdma_recvcq_upcall, |
Chuck Lever | 1c00dd0 | 2014-05-28 10:33:42 -0400 | [diff] [blame] | 832 | rpcrdma_cq_async_error_upcall, ep, |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 833 | ep->rep_attr.cap.max_recv_wr + 1, 0); |
| 834 | if (IS_ERR(recvcq)) { |
| 835 | rc = PTR_ERR(recvcq); |
| 836 | dprintk("RPC: %s: failed to create recv CQ: %i\n", |
| 837 | __func__, rc); |
| 838 | goto out2; |
| 839 | } |
| 840 | |
| 841 | rc = ib_req_notify_cq(recvcq, IB_CQ_NEXT_COMP); |
| 842 | if (rc) { |
| 843 | dprintk("RPC: %s: ib_req_notify_cq failed: %i\n", |
| 844 | __func__, rc); |
| 845 | ib_destroy_cq(recvcq); |
| 846 | goto out2; |
| 847 | } |
| 848 | |
| 849 | ep->rep_attr.send_cq = sendcq; |
| 850 | ep->rep_attr.recv_cq = recvcq; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 851 | |
| 852 | /* Initialize cma parameters */ |
| 853 | |
| 854 | /* RPC/RDMA does not use private data */ |
| 855 | ep->rep_remote_cma.private_data = NULL; |
| 856 | ep->rep_remote_cma.private_data_len = 0; |
| 857 | |
| 858 | /* Client offers RDMA Read but does not initiate */ |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 859 | ep->rep_remote_cma.initiator_depth = 0; |
Chuck Lever | 7bc7972 | 2015-01-21 11:03:27 -0500 | [diff] [blame] | 860 | if (devattr->max_qp_rd_atom > 32) /* arbitrary but <= 255 */ |
Tom Tucker | b334eaa | 2008-10-09 15:00:30 -0400 | [diff] [blame] | 861 | ep->rep_remote_cma.responder_resources = 32; |
| 862 | else |
Chuck Lever | 7bc7972 | 2015-01-21 11:03:27 -0500 | [diff] [blame] | 863 | ep->rep_remote_cma.responder_resources = |
| 864 | devattr->max_qp_rd_atom; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 865 | |
| 866 | ep->rep_remote_cma.retry_count = 7; |
| 867 | ep->rep_remote_cma.flow_control = 0; |
| 868 | ep->rep_remote_cma.rnr_retry_count = 0; |
| 869 | |
| 870 | return 0; |
| 871 | |
| 872 | out2: |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 873 | err = ib_destroy_cq(sendcq); |
Chuck Lever | 5d40a8a | 2007-10-26 13:30:54 -0400 | [diff] [blame] | 874 | if (err) |
| 875 | dprintk("RPC: %s: ib_destroy_cq returned %i\n", |
| 876 | __func__, err); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 877 | out1: |
| 878 | return rc; |
| 879 | } |
| 880 | |
| 881 | /* |
| 882 | * rpcrdma_ep_destroy |
| 883 | * |
| 884 | * Disconnect and destroy endpoint. After this, the only |
| 885 | * valid operations on the ep are to free it (if dynamically |
| 886 | * allocated) or re-create it. |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 887 | */ |
Chuck Lever | 7f1d541 | 2014-05-28 10:33:16 -0400 | [diff] [blame] | 888 | void |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 889 | rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia) |
| 890 | { |
| 891 | int rc; |
| 892 | |
| 893 | dprintk("RPC: %s: entering, connected is %d\n", |
| 894 | __func__, ep->rep_connected); |
| 895 | |
Chuck Lever | 254f91e | 2014-05-28 10:32:17 -0400 | [diff] [blame] | 896 | cancel_delayed_work_sync(&ep->rep_connect_worker); |
| 897 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 898 | if (ia->ri_id->qp) { |
Chuck Lever | 282191c | 2014-07-29 17:25:55 -0400 | [diff] [blame] | 899 | rpcrdma_ep_disconnect(ep, ia); |
Tom Talpey | fee08ca | 2008-10-09 15:01:00 -0400 | [diff] [blame] | 900 | rdma_destroy_qp(ia->ri_id); |
| 901 | ia->ri_id->qp = NULL; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 902 | } |
| 903 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 904 | /* padding - could be done in rpcrdma_buffer_destroy... */ |
| 905 | if (ep->rep_pad_mr) { |
| 906 | rpcrdma_deregister_internal(ia, ep->rep_pad_mr, &ep->rep_pad); |
| 907 | ep->rep_pad_mr = NULL; |
| 908 | } |
| 909 | |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 910 | rpcrdma_clean_cq(ep->rep_attr.recv_cq); |
| 911 | rc = ib_destroy_cq(ep->rep_attr.recv_cq); |
| 912 | if (rc) |
| 913 | dprintk("RPC: %s: ib_destroy_cq returned %i\n", |
| 914 | __func__, rc); |
| 915 | |
| 916 | rpcrdma_clean_cq(ep->rep_attr.send_cq); |
| 917 | rc = ib_destroy_cq(ep->rep_attr.send_cq); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 918 | if (rc) |
| 919 | dprintk("RPC: %s: ib_destroy_cq returned %i\n", |
| 920 | __func__, rc); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | /* |
| 924 | * Connect unconnected endpoint. |
| 925 | */ |
| 926 | int |
| 927 | rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia) |
| 928 | { |
Chuck Lever | 73806c8 | 2014-07-29 17:23:25 -0400 | [diff] [blame] | 929 | struct rdma_cm_id *id, *old; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 930 | int rc = 0; |
| 931 | int retry_count = 0; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 932 | |
Tom Talpey | c055551 | 2008-10-10 11:32:45 -0400 | [diff] [blame] | 933 | if (ep->rep_connected != 0) { |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 934 | struct rpcrdma_xprt *xprt; |
| 935 | retry: |
Chuck Lever | ec62f40 | 2014-05-28 10:34:07 -0400 | [diff] [blame] | 936 | dprintk("RPC: %s: reconnecting...\n", __func__); |
Chuck Lever | 282191c | 2014-07-29 17:25:55 -0400 | [diff] [blame] | 937 | |
| 938 | rpcrdma_ep_disconnect(ep, ia); |
Chuck Lever | a7bc211 | 2014-07-29 17:23:52 -0400 | [diff] [blame] | 939 | rpcrdma_flush_cqs(ep); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 940 | |
Chuck Lever | 467c967 | 2014-11-08 20:14:29 -0500 | [diff] [blame] | 941 | switch (ia->ri_memreg_strategy) { |
| 942 | case RPCRDMA_FRMR: |
Chuck Lever | 9f9d802 | 2014-07-29 17:24:45 -0400 | [diff] [blame] | 943 | rpcrdma_reset_frmrs(ia); |
Chuck Lever | 467c967 | 2014-11-08 20:14:29 -0500 | [diff] [blame] | 944 | break; |
| 945 | case RPCRDMA_MTHCAFMR: |
| 946 | rpcrdma_reset_fmrs(ia); |
| 947 | break; |
| 948 | case RPCRDMA_ALLPHYSICAL: |
| 949 | break; |
| 950 | default: |
| 951 | rc = -EIO; |
| 952 | goto out; |
| 953 | } |
Chuck Lever | 9f9d802 | 2014-07-29 17:24:45 -0400 | [diff] [blame] | 954 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 955 | xprt = container_of(ia, struct rpcrdma_xprt, rx_ia); |
| 956 | id = rpcrdma_create_id(xprt, ia, |
| 957 | (struct sockaddr *)&xprt->rx_data.addr); |
| 958 | if (IS_ERR(id)) { |
Chuck Lever | ec62f40 | 2014-05-28 10:34:07 -0400 | [diff] [blame] | 959 | rc = -EHOSTUNREACH; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 960 | goto out; |
| 961 | } |
| 962 | /* TEMP TEMP TEMP - fail if new device: |
| 963 | * Deregister/remarshal *all* requests! |
| 964 | * Close and recreate adapter, pd, etc! |
| 965 | * Re-determine all attributes still sane! |
| 966 | * More stuff I haven't thought of! |
| 967 | * Rrrgh! |
| 968 | */ |
| 969 | if (ia->ri_id->device != id->device) { |
| 970 | printk("RPC: %s: can't reconnect on " |
| 971 | "different device!\n", __func__); |
| 972 | rdma_destroy_id(id); |
Chuck Lever | ec62f40 | 2014-05-28 10:34:07 -0400 | [diff] [blame] | 973 | rc = -ENETUNREACH; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 974 | goto out; |
| 975 | } |
| 976 | /* END TEMP */ |
Chuck Lever | ec62f40 | 2014-05-28 10:34:07 -0400 | [diff] [blame] | 977 | rc = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr); |
| 978 | if (rc) { |
| 979 | dprintk("RPC: %s: rdma_create_qp failed %i\n", |
| 980 | __func__, rc); |
| 981 | rdma_destroy_id(id); |
| 982 | rc = -ENETUNREACH; |
| 983 | goto out; |
| 984 | } |
Chuck Lever | 73806c8 | 2014-07-29 17:23:25 -0400 | [diff] [blame] | 985 | |
| 986 | write_lock(&ia->ri_qplock); |
| 987 | old = ia->ri_id; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 988 | ia->ri_id = id; |
Chuck Lever | 73806c8 | 2014-07-29 17:23:25 -0400 | [diff] [blame] | 989 | write_unlock(&ia->ri_qplock); |
| 990 | |
| 991 | rdma_destroy_qp(old); |
| 992 | rdma_destroy_id(old); |
Chuck Lever | ec62f40 | 2014-05-28 10:34:07 -0400 | [diff] [blame] | 993 | } else { |
| 994 | dprintk("RPC: %s: connecting...\n", __func__); |
| 995 | rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr); |
| 996 | if (rc) { |
| 997 | dprintk("RPC: %s: rdma_create_qp failed %i\n", |
| 998 | __func__, rc); |
| 999 | /* do not update ep->rep_connected */ |
| 1000 | return -ENETUNREACH; |
| 1001 | } |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1002 | } |
| 1003 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1004 | ep->rep_connected = 0; |
| 1005 | |
| 1006 | rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma); |
| 1007 | if (rc) { |
| 1008 | dprintk("RPC: %s: rdma_connect() failed with %i\n", |
| 1009 | __func__, rc); |
| 1010 | goto out; |
| 1011 | } |
| 1012 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1013 | wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0); |
| 1014 | |
| 1015 | /* |
| 1016 | * Check state. A non-peer reject indicates no listener |
| 1017 | * (ECONNREFUSED), which may be a transient state. All |
| 1018 | * others indicate a transport condition which has already |
| 1019 | * undergone a best-effort. |
| 1020 | */ |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 1021 | if (ep->rep_connected == -ECONNREFUSED && |
| 1022 | ++retry_count <= RDMA_CONNECT_RETRY_MAX) { |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1023 | dprintk("RPC: %s: non-peer_reject, retry\n", __func__); |
| 1024 | goto retry; |
| 1025 | } |
| 1026 | if (ep->rep_connected <= 0) { |
| 1027 | /* Sometimes, the only way to reliably connect to remote |
| 1028 | * CMs is to use same nonzero values for ORD and IRD. */ |
Tom Tucker | b334eaa | 2008-10-09 15:00:30 -0400 | [diff] [blame] | 1029 | if (retry_count++ <= RDMA_CONNECT_RETRY_MAX + 1 && |
| 1030 | (ep->rep_remote_cma.responder_resources == 0 || |
| 1031 | ep->rep_remote_cma.initiator_depth != |
| 1032 | ep->rep_remote_cma.responder_resources)) { |
| 1033 | if (ep->rep_remote_cma.responder_resources == 0) |
| 1034 | ep->rep_remote_cma.responder_resources = 1; |
| 1035 | ep->rep_remote_cma.initiator_depth = |
| 1036 | ep->rep_remote_cma.responder_resources; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1037 | goto retry; |
Tom Tucker | b334eaa | 2008-10-09 15:00:30 -0400 | [diff] [blame] | 1038 | } |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1039 | rc = ep->rep_connected; |
| 1040 | } else { |
| 1041 | dprintk("RPC: %s: connected\n", __func__); |
| 1042 | } |
| 1043 | |
| 1044 | out: |
| 1045 | if (rc) |
| 1046 | ep->rep_connected = rc; |
| 1047 | return rc; |
| 1048 | } |
| 1049 | |
| 1050 | /* |
| 1051 | * rpcrdma_ep_disconnect |
| 1052 | * |
| 1053 | * This is separate from destroy to facilitate the ability |
| 1054 | * to reconnect without recreating the endpoint. |
| 1055 | * |
| 1056 | * This call is not reentrant, and must not be made in parallel |
| 1057 | * on the same endpoint. |
| 1058 | */ |
Chuck Lever | 282191c | 2014-07-29 17:25:55 -0400 | [diff] [blame] | 1059 | void |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1060 | rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia) |
| 1061 | { |
| 1062 | int rc; |
| 1063 | |
Chuck Lever | a7bc211 | 2014-07-29 17:23:52 -0400 | [diff] [blame] | 1064 | rpcrdma_flush_cqs(ep); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1065 | rc = rdma_disconnect(ia->ri_id); |
| 1066 | if (!rc) { |
| 1067 | /* returns without wait if not connected */ |
| 1068 | wait_event_interruptible(ep->rep_connect_wait, |
| 1069 | ep->rep_connected != 1); |
| 1070 | dprintk("RPC: %s: after wait, %sconnected\n", __func__, |
| 1071 | (ep->rep_connected == 1) ? "still " : "dis"); |
| 1072 | } else { |
| 1073 | dprintk("RPC: %s: rdma_disconnect %i\n", __func__, rc); |
| 1074 | ep->rep_connected = rc; |
| 1075 | } |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1076 | } |
| 1077 | |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1078 | static struct rpcrdma_req * |
| 1079 | rpcrdma_create_req(struct rpcrdma_xprt *r_xprt) |
| 1080 | { |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1081 | struct rpcrdma_req *req; |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1082 | |
Chuck Lever | 85275c8 | 2015-01-21 11:04:16 -0500 | [diff] [blame^] | 1083 | req = kzalloc(sizeof(*req), GFP_KERNEL); |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1084 | if (req == NULL) |
Chuck Lever | 85275c8 | 2015-01-21 11:04:16 -0500 | [diff] [blame^] | 1085 | return ERR_PTR(-ENOMEM); |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1086 | |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1087 | req->rl_buffer = &r_xprt->rx_buf; |
| 1088 | return req; |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | static struct rpcrdma_rep * |
| 1092 | rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt) |
| 1093 | { |
| 1094 | struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data; |
| 1095 | size_t rlen = 1 << fls(cdata->inline_rsize + |
| 1096 | sizeof(struct rpcrdma_rep)); |
| 1097 | struct rpcrdma_ia *ia = &r_xprt->rx_ia; |
| 1098 | struct rpcrdma_rep *rep; |
| 1099 | int rc; |
| 1100 | |
| 1101 | rc = -ENOMEM; |
| 1102 | rep = kmalloc(rlen, GFP_KERNEL); |
| 1103 | if (rep == NULL) |
| 1104 | goto out; |
Chuck Lever | 0ca77dc | 2015-01-21 11:04:08 -0500 | [diff] [blame] | 1105 | memset(rep, 0, sizeof(*rep)); |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1106 | |
| 1107 | rc = rpcrdma_register_internal(ia, rep->rr_base, rlen - |
| 1108 | offsetof(struct rpcrdma_rep, rr_base), |
| 1109 | &rep->rr_handle, &rep->rr_iov); |
| 1110 | if (rc) |
| 1111 | goto out_free; |
| 1112 | |
| 1113 | rep->rr_buffer = &r_xprt->rx_buf; |
| 1114 | return rep; |
| 1115 | |
| 1116 | out_free: |
| 1117 | kfree(rep); |
| 1118 | out: |
| 1119 | return ERR_PTR(rc); |
| 1120 | } |
| 1121 | |
Chuck Lever | 2e84522 | 2014-07-29 17:25:38 -0400 | [diff] [blame] | 1122 | static int |
| 1123 | rpcrdma_init_fmrs(struct rpcrdma_ia *ia, struct rpcrdma_buffer *buf) |
| 1124 | { |
| 1125 | int mr_access_flags = IB_ACCESS_REMOTE_WRITE | IB_ACCESS_REMOTE_READ; |
| 1126 | struct ib_fmr_attr fmr_attr = { |
| 1127 | .max_pages = RPCRDMA_MAX_DATA_SEGS, |
| 1128 | .max_maps = 1, |
| 1129 | .page_shift = PAGE_SHIFT |
| 1130 | }; |
| 1131 | struct rpcrdma_mw *r; |
| 1132 | int i, rc; |
| 1133 | |
| 1134 | i = (buf->rb_max_requests + 1) * RPCRDMA_MAX_SEGS; |
| 1135 | dprintk("RPC: %s: initalizing %d FMRs\n", __func__, i); |
| 1136 | |
| 1137 | while (i--) { |
| 1138 | r = kzalloc(sizeof(*r), GFP_KERNEL); |
| 1139 | if (r == NULL) |
| 1140 | return -ENOMEM; |
| 1141 | |
| 1142 | r->r.fmr = ib_alloc_fmr(ia->ri_pd, mr_access_flags, &fmr_attr); |
| 1143 | if (IS_ERR(r->r.fmr)) { |
| 1144 | rc = PTR_ERR(r->r.fmr); |
| 1145 | dprintk("RPC: %s: ib_alloc_fmr failed %i\n", |
| 1146 | __func__, rc); |
| 1147 | goto out_free; |
| 1148 | } |
| 1149 | |
| 1150 | list_add(&r->mw_list, &buf->rb_mws); |
| 1151 | list_add(&r->mw_all, &buf->rb_all); |
| 1152 | } |
| 1153 | return 0; |
| 1154 | |
| 1155 | out_free: |
| 1156 | kfree(r); |
| 1157 | return rc; |
| 1158 | } |
| 1159 | |
| 1160 | static int |
| 1161 | rpcrdma_init_frmrs(struct rpcrdma_ia *ia, struct rpcrdma_buffer *buf) |
| 1162 | { |
| 1163 | struct rpcrdma_frmr *f; |
| 1164 | struct rpcrdma_mw *r; |
| 1165 | int i, rc; |
| 1166 | |
| 1167 | i = (buf->rb_max_requests + 1) * RPCRDMA_MAX_SEGS; |
| 1168 | dprintk("RPC: %s: initalizing %d FRMRs\n", __func__, i); |
| 1169 | |
| 1170 | while (i--) { |
| 1171 | r = kzalloc(sizeof(*r), GFP_KERNEL); |
| 1172 | if (r == NULL) |
| 1173 | return -ENOMEM; |
| 1174 | f = &r->r.frmr; |
| 1175 | |
| 1176 | f->fr_mr = ib_alloc_fast_reg_mr(ia->ri_pd, |
| 1177 | ia->ri_max_frmr_depth); |
| 1178 | if (IS_ERR(f->fr_mr)) { |
| 1179 | rc = PTR_ERR(f->fr_mr); |
| 1180 | dprintk("RPC: %s: ib_alloc_fast_reg_mr " |
| 1181 | "failed %i\n", __func__, rc); |
| 1182 | goto out_free; |
| 1183 | } |
| 1184 | |
| 1185 | f->fr_pgl = ib_alloc_fast_reg_page_list(ia->ri_id->device, |
| 1186 | ia->ri_max_frmr_depth); |
| 1187 | if (IS_ERR(f->fr_pgl)) { |
| 1188 | rc = PTR_ERR(f->fr_pgl); |
| 1189 | dprintk("RPC: %s: ib_alloc_fast_reg_page_list " |
| 1190 | "failed %i\n", __func__, rc); |
| 1191 | |
| 1192 | ib_dereg_mr(f->fr_mr); |
| 1193 | goto out_free; |
| 1194 | } |
| 1195 | |
| 1196 | list_add(&r->mw_list, &buf->rb_mws); |
| 1197 | list_add(&r->mw_all, &buf->rb_all); |
| 1198 | } |
| 1199 | |
| 1200 | return 0; |
| 1201 | |
| 1202 | out_free: |
| 1203 | kfree(r); |
| 1204 | return rc; |
| 1205 | } |
| 1206 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1207 | int |
Chuck Lever | ac920d0 | 2015-01-21 11:03:44 -0500 | [diff] [blame] | 1208 | rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt) |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1209 | { |
Chuck Lever | ac920d0 | 2015-01-21 11:03:44 -0500 | [diff] [blame] | 1210 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; |
| 1211 | struct rpcrdma_ia *ia = &r_xprt->rx_ia; |
| 1212 | struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1213 | char *p; |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1214 | size_t len; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1215 | int i, rc; |
| 1216 | |
| 1217 | buf->rb_max_requests = cdata->max_requests; |
| 1218 | spin_lock_init(&buf->rb_lock); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1219 | |
| 1220 | /* Need to allocate: |
| 1221 | * 1. arrays for send and recv pointers |
| 1222 | * 2. arrays of struct rpcrdma_req to fill in pointers |
| 1223 | * 3. array of struct rpcrdma_rep for replies |
| 1224 | * 4. padding, if any |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1225 | * Send/recv buffers in req/rep need to be registered |
| 1226 | */ |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1227 | len = buf->rb_max_requests * |
| 1228 | (sizeof(struct rpcrdma_req *) + sizeof(struct rpcrdma_rep *)); |
| 1229 | len += cdata->padding; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1230 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1231 | p = kzalloc(len, GFP_KERNEL); |
| 1232 | if (p == NULL) { |
| 1233 | dprintk("RPC: %s: req_t/rep_t/pad kzalloc(%zd) failed\n", |
| 1234 | __func__, len); |
| 1235 | rc = -ENOMEM; |
| 1236 | goto out; |
| 1237 | } |
| 1238 | buf->rb_pool = p; /* for freeing it later */ |
| 1239 | |
| 1240 | buf->rb_send_bufs = (struct rpcrdma_req **) p; |
| 1241 | p = (char *) &buf->rb_send_bufs[buf->rb_max_requests]; |
| 1242 | buf->rb_recv_bufs = (struct rpcrdma_rep **) p; |
| 1243 | p = (char *) &buf->rb_recv_bufs[buf->rb_max_requests]; |
| 1244 | |
| 1245 | /* |
| 1246 | * Register the zeroed pad buffer, if any. |
| 1247 | */ |
| 1248 | if (cdata->padding) { |
Chuck Lever | ac920d0 | 2015-01-21 11:03:44 -0500 | [diff] [blame] | 1249 | struct rpcrdma_ep *ep = &r_xprt->rx_ep; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1250 | rc = rpcrdma_register_internal(ia, p, cdata->padding, |
| 1251 | &ep->rep_pad_mr, &ep->rep_pad); |
| 1252 | if (rc) |
| 1253 | goto out; |
| 1254 | } |
| 1255 | p += cdata->padding; |
| 1256 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1257 | INIT_LIST_HEAD(&buf->rb_mws); |
Chuck Lever | 3111d72 | 2014-07-29 17:24:28 -0400 | [diff] [blame] | 1258 | INIT_LIST_HEAD(&buf->rb_all); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1259 | switch (ia->ri_memreg_strategy) { |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 1260 | case RPCRDMA_FRMR: |
Chuck Lever | 2e84522 | 2014-07-29 17:25:38 -0400 | [diff] [blame] | 1261 | rc = rpcrdma_init_frmrs(ia, buf); |
| 1262 | if (rc) |
| 1263 | goto out; |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 1264 | break; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1265 | case RPCRDMA_MTHCAFMR: |
Chuck Lever | 2e84522 | 2014-07-29 17:25:38 -0400 | [diff] [blame] | 1266 | rc = rpcrdma_init_fmrs(ia, buf); |
| 1267 | if (rc) |
| 1268 | goto out; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1269 | break; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1270 | default: |
| 1271 | break; |
| 1272 | } |
| 1273 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1274 | for (i = 0; i < buf->rb_max_requests; i++) { |
| 1275 | struct rpcrdma_req *req; |
| 1276 | struct rpcrdma_rep *rep; |
| 1277 | |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1278 | req = rpcrdma_create_req(r_xprt); |
| 1279 | if (IS_ERR(req)) { |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1280 | dprintk("RPC: %s: request buffer %d alloc" |
| 1281 | " failed\n", __func__, i); |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1282 | rc = PTR_ERR(req); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1283 | goto out; |
| 1284 | } |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1285 | buf->rb_send_bufs[i] = req; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1286 | |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1287 | rep = rpcrdma_create_rep(r_xprt); |
| 1288 | if (IS_ERR(rep)) { |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1289 | dprintk("RPC: %s: reply buffer %d alloc failed\n", |
| 1290 | __func__, i); |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1291 | rc = PTR_ERR(rep); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1292 | goto out; |
| 1293 | } |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1294 | buf->rb_recv_bufs[i] = rep; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1295 | } |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1296 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1297 | return 0; |
| 1298 | out: |
| 1299 | rpcrdma_buffer_destroy(buf); |
| 1300 | return rc; |
| 1301 | } |
| 1302 | |
Chuck Lever | 2e84522 | 2014-07-29 17:25:38 -0400 | [diff] [blame] | 1303 | static void |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1304 | rpcrdma_destroy_rep(struct rpcrdma_ia *ia, struct rpcrdma_rep *rep) |
| 1305 | { |
| 1306 | if (!rep) |
| 1307 | return; |
| 1308 | |
| 1309 | rpcrdma_deregister_internal(ia, rep->rr_handle, &rep->rr_iov); |
| 1310 | kfree(rep); |
| 1311 | } |
| 1312 | |
| 1313 | static void |
| 1314 | rpcrdma_destroy_req(struct rpcrdma_ia *ia, struct rpcrdma_req *req) |
| 1315 | { |
| 1316 | if (!req) |
| 1317 | return; |
| 1318 | |
Chuck Lever | 0ca77dc | 2015-01-21 11:04:08 -0500 | [diff] [blame] | 1319 | rpcrdma_free_regbuf(ia, req->rl_sendbuf); |
Chuck Lever | 85275c8 | 2015-01-21 11:04:16 -0500 | [diff] [blame^] | 1320 | rpcrdma_free_regbuf(ia, req->rl_rdmabuf); |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1321 | kfree(req); |
| 1322 | } |
| 1323 | |
| 1324 | static void |
Chuck Lever | 2e84522 | 2014-07-29 17:25:38 -0400 | [diff] [blame] | 1325 | rpcrdma_destroy_fmrs(struct rpcrdma_buffer *buf) |
| 1326 | { |
| 1327 | struct rpcrdma_mw *r; |
| 1328 | int rc; |
| 1329 | |
| 1330 | while (!list_empty(&buf->rb_all)) { |
| 1331 | r = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all); |
| 1332 | list_del(&r->mw_all); |
| 1333 | list_del(&r->mw_list); |
| 1334 | |
| 1335 | rc = ib_dealloc_fmr(r->r.fmr); |
| 1336 | if (rc) |
| 1337 | dprintk("RPC: %s: ib_dealloc_fmr failed %i\n", |
| 1338 | __func__, rc); |
| 1339 | |
| 1340 | kfree(r); |
| 1341 | } |
| 1342 | } |
| 1343 | |
| 1344 | static void |
| 1345 | rpcrdma_destroy_frmrs(struct rpcrdma_buffer *buf) |
| 1346 | { |
| 1347 | struct rpcrdma_mw *r; |
| 1348 | int rc; |
| 1349 | |
| 1350 | while (!list_empty(&buf->rb_all)) { |
| 1351 | r = list_entry(buf->rb_all.next, struct rpcrdma_mw, mw_all); |
| 1352 | list_del(&r->mw_all); |
| 1353 | list_del(&r->mw_list); |
| 1354 | |
| 1355 | rc = ib_dereg_mr(r->r.frmr.fr_mr); |
| 1356 | if (rc) |
| 1357 | dprintk("RPC: %s: ib_dereg_mr failed %i\n", |
| 1358 | __func__, rc); |
| 1359 | ib_free_fast_reg_page_list(r->r.frmr.fr_pgl); |
| 1360 | |
| 1361 | kfree(r); |
| 1362 | } |
| 1363 | } |
| 1364 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1365 | void |
| 1366 | rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf) |
| 1367 | { |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1368 | struct rpcrdma_ia *ia = rdmab_to_ia(buf); |
Chuck Lever | 2e84522 | 2014-07-29 17:25:38 -0400 | [diff] [blame] | 1369 | int i; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1370 | |
| 1371 | /* clean up in reverse order from create |
| 1372 | * 1. recv mr memory (mr free, then kfree) |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1373 | * 2. send mr memory (mr free, then kfree) |
Chuck Lever | 2e84522 | 2014-07-29 17:25:38 -0400 | [diff] [blame] | 1374 | * 3. MWs |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1375 | */ |
| 1376 | dprintk("RPC: %s: entering\n", __func__); |
| 1377 | |
| 1378 | for (i = 0; i < buf->rb_max_requests; i++) { |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1379 | if (buf->rb_recv_bufs) |
| 1380 | rpcrdma_destroy_rep(ia, buf->rb_recv_bufs[i]); |
| 1381 | if (buf->rb_send_bufs) |
| 1382 | rpcrdma_destroy_req(ia, buf->rb_send_bufs[i]); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1383 | } |
| 1384 | |
Chuck Lever | 2e84522 | 2014-07-29 17:25:38 -0400 | [diff] [blame] | 1385 | switch (ia->ri_memreg_strategy) { |
| 1386 | case RPCRDMA_FRMR: |
| 1387 | rpcrdma_destroy_frmrs(buf); |
| 1388 | break; |
| 1389 | case RPCRDMA_MTHCAFMR: |
| 1390 | rpcrdma_destroy_fmrs(buf); |
| 1391 | break; |
| 1392 | default: |
| 1393 | break; |
Allen Andrews | 4034ba0 | 2014-05-28 10:32:09 -0400 | [diff] [blame] | 1394 | } |
| 1395 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1396 | kfree(buf->rb_pool); |
| 1397 | } |
| 1398 | |
Chuck Lever | 467c967 | 2014-11-08 20:14:29 -0500 | [diff] [blame] | 1399 | /* After a disconnect, unmap all FMRs. |
| 1400 | * |
| 1401 | * This is invoked only in the transport connect worker in order |
| 1402 | * to serialize with rpcrdma_register_fmr_external(). |
| 1403 | */ |
| 1404 | static void |
| 1405 | rpcrdma_reset_fmrs(struct rpcrdma_ia *ia) |
| 1406 | { |
| 1407 | struct rpcrdma_xprt *r_xprt = |
| 1408 | container_of(ia, struct rpcrdma_xprt, rx_ia); |
| 1409 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; |
| 1410 | struct list_head *pos; |
| 1411 | struct rpcrdma_mw *r; |
| 1412 | LIST_HEAD(l); |
| 1413 | int rc; |
| 1414 | |
| 1415 | list_for_each(pos, &buf->rb_all) { |
| 1416 | r = list_entry(pos, struct rpcrdma_mw, mw_all); |
| 1417 | |
| 1418 | INIT_LIST_HEAD(&l); |
| 1419 | list_add(&r->r.fmr->list, &l); |
| 1420 | rc = ib_unmap_fmr(&l); |
| 1421 | if (rc) |
| 1422 | dprintk("RPC: %s: ib_unmap_fmr failed %i\n", |
| 1423 | __func__, rc); |
| 1424 | } |
| 1425 | } |
| 1426 | |
Chuck Lever | 9f9d802 | 2014-07-29 17:24:45 -0400 | [diff] [blame] | 1427 | /* After a disconnect, a flushed FAST_REG_MR can leave an FRMR in |
| 1428 | * an unusable state. Find FRMRs in this state and dereg / reg |
| 1429 | * each. FRMRs that are VALID and attached to an rpcrdma_req are |
| 1430 | * also torn down. |
| 1431 | * |
| 1432 | * This gives all in-use FRMRs a fresh rkey and leaves them INVALID. |
| 1433 | * |
| 1434 | * This is invoked only in the transport connect worker in order |
| 1435 | * to serialize with rpcrdma_register_frmr_external(). |
| 1436 | */ |
| 1437 | static void |
| 1438 | rpcrdma_reset_frmrs(struct rpcrdma_ia *ia) |
| 1439 | { |
| 1440 | struct rpcrdma_xprt *r_xprt = |
| 1441 | container_of(ia, struct rpcrdma_xprt, rx_ia); |
| 1442 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; |
| 1443 | struct list_head *pos; |
| 1444 | struct rpcrdma_mw *r; |
| 1445 | int rc; |
| 1446 | |
| 1447 | list_for_each(pos, &buf->rb_all) { |
| 1448 | r = list_entry(pos, struct rpcrdma_mw, mw_all); |
| 1449 | |
| 1450 | if (r->r.frmr.fr_state == FRMR_IS_INVALID) |
| 1451 | continue; |
| 1452 | |
| 1453 | rc = ib_dereg_mr(r->r.frmr.fr_mr); |
| 1454 | if (rc) |
| 1455 | dprintk("RPC: %s: ib_dereg_mr failed %i\n", |
| 1456 | __func__, rc); |
| 1457 | ib_free_fast_reg_page_list(r->r.frmr.fr_pgl); |
| 1458 | |
| 1459 | r->r.frmr.fr_mr = ib_alloc_fast_reg_mr(ia->ri_pd, |
| 1460 | ia->ri_max_frmr_depth); |
| 1461 | if (IS_ERR(r->r.frmr.fr_mr)) { |
| 1462 | rc = PTR_ERR(r->r.frmr.fr_mr); |
| 1463 | dprintk("RPC: %s: ib_alloc_fast_reg_mr" |
| 1464 | " failed %i\n", __func__, rc); |
| 1465 | continue; |
| 1466 | } |
| 1467 | r->r.frmr.fr_pgl = ib_alloc_fast_reg_page_list( |
| 1468 | ia->ri_id->device, |
| 1469 | ia->ri_max_frmr_depth); |
| 1470 | if (IS_ERR(r->r.frmr.fr_pgl)) { |
| 1471 | rc = PTR_ERR(r->r.frmr.fr_pgl); |
| 1472 | dprintk("RPC: %s: " |
| 1473 | "ib_alloc_fast_reg_page_list " |
| 1474 | "failed %i\n", __func__, rc); |
| 1475 | |
| 1476 | ib_dereg_mr(r->r.frmr.fr_mr); |
| 1477 | continue; |
| 1478 | } |
| 1479 | r->r.frmr.fr_state = FRMR_IS_INVALID; |
| 1480 | } |
| 1481 | } |
| 1482 | |
Chuck Lever | c2922c0 | 2014-07-29 17:24:36 -0400 | [diff] [blame] | 1483 | /* "*mw" can be NULL when rpcrdma_buffer_get_mrs() fails, leaving |
| 1484 | * some req segments uninitialized. |
| 1485 | */ |
| 1486 | static void |
| 1487 | rpcrdma_buffer_put_mr(struct rpcrdma_mw **mw, struct rpcrdma_buffer *buf) |
| 1488 | { |
| 1489 | if (*mw) { |
| 1490 | list_add_tail(&(*mw)->mw_list, &buf->rb_mws); |
| 1491 | *mw = NULL; |
| 1492 | } |
| 1493 | } |
| 1494 | |
| 1495 | /* Cycle mw's back in reverse order, and "spin" them. |
| 1496 | * This delays and scrambles reuse as much as possible. |
| 1497 | */ |
| 1498 | static void |
| 1499 | rpcrdma_buffer_put_mrs(struct rpcrdma_req *req, struct rpcrdma_buffer *buf) |
| 1500 | { |
| 1501 | struct rpcrdma_mr_seg *seg = req->rl_segments; |
| 1502 | struct rpcrdma_mr_seg *seg1 = seg; |
| 1503 | int i; |
| 1504 | |
| 1505 | for (i = 1, seg++; i < RPCRDMA_MAX_SEGS; seg++, i++) |
Chuck Lever | 3eb3581 | 2015-01-21 11:02:54 -0500 | [diff] [blame] | 1506 | rpcrdma_buffer_put_mr(&seg->rl_mw, buf); |
| 1507 | rpcrdma_buffer_put_mr(&seg1->rl_mw, buf); |
Chuck Lever | c2922c0 | 2014-07-29 17:24:36 -0400 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | static void |
| 1511 | rpcrdma_buffer_put_sendbuf(struct rpcrdma_req *req, struct rpcrdma_buffer *buf) |
| 1512 | { |
| 1513 | buf->rb_send_bufs[--buf->rb_send_index] = req; |
| 1514 | req->rl_niovs = 0; |
| 1515 | if (req->rl_reply) { |
| 1516 | buf->rb_recv_bufs[--buf->rb_recv_index] = req->rl_reply; |
| 1517 | req->rl_reply->rr_func = NULL; |
| 1518 | req->rl_reply = NULL; |
| 1519 | } |
| 1520 | } |
| 1521 | |
Chuck Lever | ddb6beb | 2014-07-29 17:24:54 -0400 | [diff] [blame] | 1522 | /* rpcrdma_unmap_one() was already done by rpcrdma_deregister_frmr_external(). |
| 1523 | * Redo only the ib_post_send(). |
| 1524 | */ |
| 1525 | static void |
| 1526 | rpcrdma_retry_local_inv(struct rpcrdma_mw *r, struct rpcrdma_ia *ia) |
| 1527 | { |
| 1528 | struct rpcrdma_xprt *r_xprt = |
| 1529 | container_of(ia, struct rpcrdma_xprt, rx_ia); |
| 1530 | struct ib_send_wr invalidate_wr, *bad_wr; |
| 1531 | int rc; |
| 1532 | |
| 1533 | dprintk("RPC: %s: FRMR %p is stale\n", __func__, r); |
| 1534 | |
| 1535 | /* When this FRMR is re-inserted into rb_mws, it is no longer stale */ |
Chuck Lever | dab7e3b | 2014-07-29 17:25:20 -0400 | [diff] [blame] | 1536 | r->r.frmr.fr_state = FRMR_IS_INVALID; |
Chuck Lever | ddb6beb | 2014-07-29 17:24:54 -0400 | [diff] [blame] | 1537 | |
| 1538 | memset(&invalidate_wr, 0, sizeof(invalidate_wr)); |
| 1539 | invalidate_wr.wr_id = (unsigned long)(void *)r; |
| 1540 | invalidate_wr.opcode = IB_WR_LOCAL_INV; |
Chuck Lever | ddb6beb | 2014-07-29 17:24:54 -0400 | [diff] [blame] | 1541 | invalidate_wr.ex.invalidate_rkey = r->r.frmr.fr_mr->rkey; |
| 1542 | DECR_CQCOUNT(&r_xprt->rx_ep); |
| 1543 | |
| 1544 | dprintk("RPC: %s: frmr %p invalidating rkey %08x\n", |
| 1545 | __func__, r, r->r.frmr.fr_mr->rkey); |
| 1546 | |
| 1547 | read_lock(&ia->ri_qplock); |
| 1548 | rc = ib_post_send(ia->ri_id->qp, &invalidate_wr, &bad_wr); |
| 1549 | read_unlock(&ia->ri_qplock); |
| 1550 | if (rc) { |
| 1551 | /* Force rpcrdma_buffer_get() to retry */ |
| 1552 | r->r.frmr.fr_state = FRMR_IS_STALE; |
| 1553 | dprintk("RPC: %s: ib_post_send failed, %i\n", |
| 1554 | __func__, rc); |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | static void |
| 1559 | rpcrdma_retry_flushed_linv(struct list_head *stale, |
| 1560 | struct rpcrdma_buffer *buf) |
| 1561 | { |
| 1562 | struct rpcrdma_ia *ia = rdmab_to_ia(buf); |
| 1563 | struct list_head *pos; |
| 1564 | struct rpcrdma_mw *r; |
| 1565 | unsigned long flags; |
| 1566 | |
| 1567 | list_for_each(pos, stale) { |
| 1568 | r = list_entry(pos, struct rpcrdma_mw, mw_list); |
| 1569 | rpcrdma_retry_local_inv(r, ia); |
| 1570 | } |
| 1571 | |
| 1572 | spin_lock_irqsave(&buf->rb_lock, flags); |
| 1573 | list_splice_tail(stale, &buf->rb_mws); |
| 1574 | spin_unlock_irqrestore(&buf->rb_lock, flags); |
| 1575 | } |
| 1576 | |
Chuck Lever | c2922c0 | 2014-07-29 17:24:36 -0400 | [diff] [blame] | 1577 | static struct rpcrdma_req * |
Chuck Lever | ddb6beb | 2014-07-29 17:24:54 -0400 | [diff] [blame] | 1578 | rpcrdma_buffer_get_frmrs(struct rpcrdma_req *req, struct rpcrdma_buffer *buf, |
| 1579 | struct list_head *stale) |
| 1580 | { |
| 1581 | struct rpcrdma_mw *r; |
| 1582 | int i; |
| 1583 | |
| 1584 | i = RPCRDMA_MAX_SEGS - 1; |
| 1585 | while (!list_empty(&buf->rb_mws)) { |
| 1586 | r = list_entry(buf->rb_mws.next, |
| 1587 | struct rpcrdma_mw, mw_list); |
| 1588 | list_del(&r->mw_list); |
| 1589 | if (r->r.frmr.fr_state == FRMR_IS_STALE) { |
| 1590 | list_add(&r->mw_list, stale); |
| 1591 | continue; |
| 1592 | } |
Chuck Lever | 3eb3581 | 2015-01-21 11:02:54 -0500 | [diff] [blame] | 1593 | req->rl_segments[i].rl_mw = r; |
Chuck Lever | ddb6beb | 2014-07-29 17:24:54 -0400 | [diff] [blame] | 1594 | if (unlikely(i-- == 0)) |
| 1595 | return req; /* Success */ |
| 1596 | } |
| 1597 | |
| 1598 | /* Not enough entries on rb_mws for this req */ |
| 1599 | rpcrdma_buffer_put_sendbuf(req, buf); |
| 1600 | rpcrdma_buffer_put_mrs(req, buf); |
| 1601 | return NULL; |
| 1602 | } |
| 1603 | |
| 1604 | static struct rpcrdma_req * |
| 1605 | rpcrdma_buffer_get_fmrs(struct rpcrdma_req *req, struct rpcrdma_buffer *buf) |
Chuck Lever | c2922c0 | 2014-07-29 17:24:36 -0400 | [diff] [blame] | 1606 | { |
| 1607 | struct rpcrdma_mw *r; |
| 1608 | int i; |
| 1609 | |
| 1610 | i = RPCRDMA_MAX_SEGS - 1; |
| 1611 | while (!list_empty(&buf->rb_mws)) { |
| 1612 | r = list_entry(buf->rb_mws.next, |
| 1613 | struct rpcrdma_mw, mw_list); |
| 1614 | list_del(&r->mw_list); |
Chuck Lever | 3eb3581 | 2015-01-21 11:02:54 -0500 | [diff] [blame] | 1615 | req->rl_segments[i].rl_mw = r; |
Chuck Lever | c2922c0 | 2014-07-29 17:24:36 -0400 | [diff] [blame] | 1616 | if (unlikely(i-- == 0)) |
| 1617 | return req; /* Success */ |
| 1618 | } |
| 1619 | |
| 1620 | /* Not enough entries on rb_mws for this req */ |
| 1621 | rpcrdma_buffer_put_sendbuf(req, buf); |
| 1622 | rpcrdma_buffer_put_mrs(req, buf); |
| 1623 | return NULL; |
| 1624 | } |
| 1625 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1626 | /* |
| 1627 | * Get a set of request/reply buffers. |
| 1628 | * |
| 1629 | * Reply buffer (if needed) is attached to send buffer upon return. |
| 1630 | * Rule: |
| 1631 | * rb_send_index and rb_recv_index MUST always be pointing to the |
| 1632 | * *next* available buffer (non-NULL). They are incremented after |
| 1633 | * removing buffers, and decremented *before* returning them. |
| 1634 | */ |
| 1635 | struct rpcrdma_req * |
| 1636 | rpcrdma_buffer_get(struct rpcrdma_buffer *buffers) |
| 1637 | { |
Chuck Lever | c2922c0 | 2014-07-29 17:24:36 -0400 | [diff] [blame] | 1638 | struct rpcrdma_ia *ia = rdmab_to_ia(buffers); |
Chuck Lever | ddb6beb | 2014-07-29 17:24:54 -0400 | [diff] [blame] | 1639 | struct list_head stale; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1640 | struct rpcrdma_req *req; |
| 1641 | unsigned long flags; |
| 1642 | |
| 1643 | spin_lock_irqsave(&buffers->rb_lock, flags); |
| 1644 | if (buffers->rb_send_index == buffers->rb_max_requests) { |
| 1645 | spin_unlock_irqrestore(&buffers->rb_lock, flags); |
| 1646 | dprintk("RPC: %s: out of request buffers\n", __func__); |
| 1647 | return ((struct rpcrdma_req *)NULL); |
| 1648 | } |
| 1649 | |
| 1650 | req = buffers->rb_send_bufs[buffers->rb_send_index]; |
| 1651 | if (buffers->rb_send_index < buffers->rb_recv_index) { |
| 1652 | dprintk("RPC: %s: %d extra receives outstanding (ok)\n", |
| 1653 | __func__, |
| 1654 | buffers->rb_recv_index - buffers->rb_send_index); |
| 1655 | req->rl_reply = NULL; |
| 1656 | } else { |
| 1657 | req->rl_reply = buffers->rb_recv_bufs[buffers->rb_recv_index]; |
| 1658 | buffers->rb_recv_bufs[buffers->rb_recv_index++] = NULL; |
| 1659 | } |
| 1660 | buffers->rb_send_bufs[buffers->rb_send_index++] = NULL; |
Chuck Lever | ddb6beb | 2014-07-29 17:24:54 -0400 | [diff] [blame] | 1661 | |
| 1662 | INIT_LIST_HEAD(&stale); |
Chuck Lever | c2922c0 | 2014-07-29 17:24:36 -0400 | [diff] [blame] | 1663 | switch (ia->ri_memreg_strategy) { |
| 1664 | case RPCRDMA_FRMR: |
Chuck Lever | ddb6beb | 2014-07-29 17:24:54 -0400 | [diff] [blame] | 1665 | req = rpcrdma_buffer_get_frmrs(req, buffers, &stale); |
| 1666 | break; |
Chuck Lever | c2922c0 | 2014-07-29 17:24:36 -0400 | [diff] [blame] | 1667 | case RPCRDMA_MTHCAFMR: |
Chuck Lever | ddb6beb | 2014-07-29 17:24:54 -0400 | [diff] [blame] | 1668 | req = rpcrdma_buffer_get_fmrs(req, buffers); |
Chuck Lever | c2922c0 | 2014-07-29 17:24:36 -0400 | [diff] [blame] | 1669 | break; |
| 1670 | default: |
| 1671 | break; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1672 | } |
| 1673 | spin_unlock_irqrestore(&buffers->rb_lock, flags); |
Chuck Lever | ddb6beb | 2014-07-29 17:24:54 -0400 | [diff] [blame] | 1674 | if (!list_empty(&stale)) |
| 1675 | rpcrdma_retry_flushed_linv(&stale, buffers); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1676 | return req; |
| 1677 | } |
| 1678 | |
| 1679 | /* |
| 1680 | * Put request/reply buffers back into pool. |
| 1681 | * Pre-decrement counter/array index. |
| 1682 | */ |
| 1683 | void |
| 1684 | rpcrdma_buffer_put(struct rpcrdma_req *req) |
| 1685 | { |
| 1686 | struct rpcrdma_buffer *buffers = req->rl_buffer; |
| 1687 | struct rpcrdma_ia *ia = rdmab_to_ia(buffers); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1688 | unsigned long flags; |
| 1689 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1690 | spin_lock_irqsave(&buffers->rb_lock, flags); |
Chuck Lever | c2922c0 | 2014-07-29 17:24:36 -0400 | [diff] [blame] | 1691 | rpcrdma_buffer_put_sendbuf(req, buffers); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1692 | switch (ia->ri_memreg_strategy) { |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 1693 | case RPCRDMA_FRMR: |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1694 | case RPCRDMA_MTHCAFMR: |
Chuck Lever | c2922c0 | 2014-07-29 17:24:36 -0400 | [diff] [blame] | 1695 | rpcrdma_buffer_put_mrs(req, buffers); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1696 | break; |
| 1697 | default: |
| 1698 | break; |
| 1699 | } |
| 1700 | spin_unlock_irqrestore(&buffers->rb_lock, flags); |
| 1701 | } |
| 1702 | |
| 1703 | /* |
| 1704 | * Recover reply buffers from pool. |
| 1705 | * This happens when recovering from error conditions. |
| 1706 | * Post-increment counter/array index. |
| 1707 | */ |
| 1708 | void |
| 1709 | rpcrdma_recv_buffer_get(struct rpcrdma_req *req) |
| 1710 | { |
| 1711 | struct rpcrdma_buffer *buffers = req->rl_buffer; |
| 1712 | unsigned long flags; |
| 1713 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1714 | spin_lock_irqsave(&buffers->rb_lock, flags); |
| 1715 | if (buffers->rb_recv_index < buffers->rb_max_requests) { |
| 1716 | req->rl_reply = buffers->rb_recv_bufs[buffers->rb_recv_index]; |
| 1717 | buffers->rb_recv_bufs[buffers->rb_recv_index++] = NULL; |
| 1718 | } |
| 1719 | spin_unlock_irqrestore(&buffers->rb_lock, flags); |
| 1720 | } |
| 1721 | |
| 1722 | /* |
| 1723 | * Put reply buffers back into pool when not attached to |
Chuck Lever | b45ccfd | 2014-05-28 10:32:34 -0400 | [diff] [blame] | 1724 | * request. This happens in error conditions. |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1725 | */ |
| 1726 | void |
| 1727 | rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep) |
| 1728 | { |
| 1729 | struct rpcrdma_buffer *buffers = rep->rr_buffer; |
| 1730 | unsigned long flags; |
| 1731 | |
| 1732 | rep->rr_func = NULL; |
| 1733 | spin_lock_irqsave(&buffers->rb_lock, flags); |
| 1734 | buffers->rb_recv_bufs[--buffers->rb_recv_index] = rep; |
| 1735 | spin_unlock_irqrestore(&buffers->rb_lock, flags); |
| 1736 | } |
| 1737 | |
| 1738 | /* |
| 1739 | * Wrappers for internal-use kmalloc memory registration, used by buffer code. |
| 1740 | */ |
| 1741 | |
| 1742 | int |
| 1743 | rpcrdma_register_internal(struct rpcrdma_ia *ia, void *va, int len, |
| 1744 | struct ib_mr **mrp, struct ib_sge *iov) |
| 1745 | { |
| 1746 | struct ib_phys_buf ipb; |
| 1747 | struct ib_mr *mr; |
| 1748 | int rc; |
| 1749 | |
| 1750 | /* |
| 1751 | * All memory passed here was kmalloc'ed, therefore phys-contiguous. |
| 1752 | */ |
| 1753 | iov->addr = ib_dma_map_single(ia->ri_id->device, |
| 1754 | va, len, DMA_BIDIRECTIONAL); |
Yan Burman | bf858ab | 2014-06-19 16:06:30 +0300 | [diff] [blame] | 1755 | if (ib_dma_mapping_error(ia->ri_id->device, iov->addr)) |
| 1756 | return -ENOMEM; |
| 1757 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1758 | iov->length = len; |
| 1759 | |
Tom Talpey | bd7ed1d | 2008-10-09 15:00:09 -0400 | [diff] [blame] | 1760 | if (ia->ri_have_dma_lkey) { |
| 1761 | *mrp = NULL; |
| 1762 | iov->lkey = ia->ri_dma_lkey; |
| 1763 | return 0; |
| 1764 | } else if (ia->ri_bind_mem != NULL) { |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1765 | *mrp = NULL; |
| 1766 | iov->lkey = ia->ri_bind_mem->lkey; |
| 1767 | return 0; |
| 1768 | } |
| 1769 | |
| 1770 | ipb.addr = iov->addr; |
| 1771 | ipb.size = iov->length; |
| 1772 | mr = ib_reg_phys_mr(ia->ri_pd, &ipb, 1, |
| 1773 | IB_ACCESS_LOCAL_WRITE, &iov->addr); |
| 1774 | |
| 1775 | dprintk("RPC: %s: phys convert: 0x%llx " |
| 1776 | "registered 0x%llx length %d\n", |
Andrew Morton | a56daeb | 2007-10-16 01:29:57 -0700 | [diff] [blame] | 1777 | __func__, (unsigned long long)ipb.addr, |
| 1778 | (unsigned long long)iov->addr, len); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1779 | |
| 1780 | if (IS_ERR(mr)) { |
| 1781 | *mrp = NULL; |
| 1782 | rc = PTR_ERR(mr); |
| 1783 | dprintk("RPC: %s: failed with %i\n", __func__, rc); |
| 1784 | } else { |
| 1785 | *mrp = mr; |
| 1786 | iov->lkey = mr->lkey; |
| 1787 | rc = 0; |
| 1788 | } |
| 1789 | |
| 1790 | return rc; |
| 1791 | } |
| 1792 | |
| 1793 | int |
| 1794 | rpcrdma_deregister_internal(struct rpcrdma_ia *ia, |
| 1795 | struct ib_mr *mr, struct ib_sge *iov) |
| 1796 | { |
| 1797 | int rc; |
| 1798 | |
| 1799 | ib_dma_unmap_single(ia->ri_id->device, |
| 1800 | iov->addr, iov->length, DMA_BIDIRECTIONAL); |
| 1801 | |
| 1802 | if (NULL == mr) |
| 1803 | return 0; |
| 1804 | |
| 1805 | rc = ib_dereg_mr(mr); |
| 1806 | if (rc) |
| 1807 | dprintk("RPC: %s: ib_dereg_mr failed %i\n", __func__, rc); |
| 1808 | return rc; |
| 1809 | } |
| 1810 | |
Chuck Lever | 9128c3e | 2015-01-21 11:04:00 -0500 | [diff] [blame] | 1811 | /** |
| 1812 | * rpcrdma_alloc_regbuf - kmalloc and register memory for SEND/RECV buffers |
| 1813 | * @ia: controlling rpcrdma_ia |
| 1814 | * @size: size of buffer to be allocated, in bytes |
| 1815 | * @flags: GFP flags |
| 1816 | * |
| 1817 | * Returns pointer to private header of an area of internally |
| 1818 | * registered memory, or an ERR_PTR. The registered buffer follows |
| 1819 | * the end of the private header. |
| 1820 | * |
| 1821 | * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for |
| 1822 | * receiving the payload of RDMA RECV operations. regbufs are not |
| 1823 | * used for RDMA READ/WRITE operations, thus are registered only for |
| 1824 | * LOCAL access. |
| 1825 | */ |
| 1826 | struct rpcrdma_regbuf * |
| 1827 | rpcrdma_alloc_regbuf(struct rpcrdma_ia *ia, size_t size, gfp_t flags) |
| 1828 | { |
| 1829 | struct rpcrdma_regbuf *rb; |
| 1830 | int rc; |
| 1831 | |
| 1832 | rc = -ENOMEM; |
| 1833 | rb = kmalloc(sizeof(*rb) + size, flags); |
| 1834 | if (rb == NULL) |
| 1835 | goto out; |
| 1836 | |
| 1837 | rb->rg_size = size; |
| 1838 | rb->rg_owner = NULL; |
| 1839 | rc = rpcrdma_register_internal(ia, rb->rg_base, size, |
| 1840 | &rb->rg_mr, &rb->rg_iov); |
| 1841 | if (rc) |
| 1842 | goto out_free; |
| 1843 | |
| 1844 | return rb; |
| 1845 | |
| 1846 | out_free: |
| 1847 | kfree(rb); |
| 1848 | out: |
| 1849 | return ERR_PTR(rc); |
| 1850 | } |
| 1851 | |
| 1852 | /** |
| 1853 | * rpcrdma_free_regbuf - deregister and free registered buffer |
| 1854 | * @ia: controlling rpcrdma_ia |
| 1855 | * @rb: regbuf to be deregistered and freed |
| 1856 | */ |
| 1857 | void |
| 1858 | rpcrdma_free_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb) |
| 1859 | { |
| 1860 | if (rb) { |
| 1861 | rpcrdma_deregister_internal(ia, rb->rg_mr, &rb->rg_iov); |
| 1862 | kfree(rb); |
| 1863 | } |
| 1864 | } |
| 1865 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1866 | /* |
| 1867 | * Wrappers for chunk registration, shared by read/write chunk code. |
| 1868 | */ |
| 1869 | |
| 1870 | static void |
| 1871 | rpcrdma_map_one(struct rpcrdma_ia *ia, struct rpcrdma_mr_seg *seg, int writing) |
| 1872 | { |
| 1873 | seg->mr_dir = writing ? DMA_FROM_DEVICE : DMA_TO_DEVICE; |
| 1874 | seg->mr_dmalen = seg->mr_len; |
| 1875 | if (seg->mr_page) |
| 1876 | seg->mr_dma = ib_dma_map_page(ia->ri_id->device, |
| 1877 | seg->mr_page, offset_in_page(seg->mr_offset), |
| 1878 | seg->mr_dmalen, seg->mr_dir); |
| 1879 | else |
| 1880 | seg->mr_dma = ib_dma_map_single(ia->ri_id->device, |
| 1881 | seg->mr_offset, |
| 1882 | seg->mr_dmalen, seg->mr_dir); |
Tom Tucker | 5c635e0 | 2011-02-09 19:45:34 +0000 | [diff] [blame] | 1883 | if (ib_dma_mapping_error(ia->ri_id->device, seg->mr_dma)) { |
| 1884 | dprintk("RPC: %s: mr_dma %llx mr_offset %p mr_dma_len %zu\n", |
| 1885 | __func__, |
Randy Dunlap | 986d4ab | 2011-03-15 17:11:59 -0700 | [diff] [blame] | 1886 | (unsigned long long)seg->mr_dma, |
| 1887 | seg->mr_offset, seg->mr_dmalen); |
Tom Tucker | 5c635e0 | 2011-02-09 19:45:34 +0000 | [diff] [blame] | 1888 | } |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | static void |
| 1892 | rpcrdma_unmap_one(struct rpcrdma_ia *ia, struct rpcrdma_mr_seg *seg) |
| 1893 | { |
| 1894 | if (seg->mr_page) |
| 1895 | ib_dma_unmap_page(ia->ri_id->device, |
| 1896 | seg->mr_dma, seg->mr_dmalen, seg->mr_dir); |
| 1897 | else |
| 1898 | ib_dma_unmap_single(ia->ri_id->device, |
| 1899 | seg->mr_dma, seg->mr_dmalen, seg->mr_dir); |
| 1900 | } |
| 1901 | |
Tom Talpey | 8d4ba03 | 2008-10-09 14:59:49 -0400 | [diff] [blame] | 1902 | static int |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 1903 | rpcrdma_register_frmr_external(struct rpcrdma_mr_seg *seg, |
| 1904 | int *nsegs, int writing, struct rpcrdma_ia *ia, |
| 1905 | struct rpcrdma_xprt *r_xprt) |
| 1906 | { |
| 1907 | struct rpcrdma_mr_seg *seg1 = seg; |
Chuck Lever | 3eb3581 | 2015-01-21 11:02:54 -0500 | [diff] [blame] | 1908 | struct rpcrdma_mw *mw = seg1->rl_mw; |
Chuck Lever | 0dbb410 | 2014-07-29 17:24:09 -0400 | [diff] [blame] | 1909 | struct rpcrdma_frmr *frmr = &mw->r.frmr; |
| 1910 | struct ib_mr *mr = frmr->fr_mr; |
Chuck Lever | f590e87 | 2014-07-29 17:25:29 -0400 | [diff] [blame] | 1911 | struct ib_send_wr fastreg_wr, *bad_wr; |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 1912 | u8 key; |
| 1913 | int len, pageoff; |
| 1914 | int i, rc; |
Tom Tucker | 9b78145 | 2012-02-20 13:07:57 -0600 | [diff] [blame] | 1915 | int seg_len; |
| 1916 | u64 pa; |
| 1917 | int page_no; |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 1918 | |
| 1919 | pageoff = offset_in_page(seg1->mr_offset); |
| 1920 | seg1->mr_offset -= pageoff; /* start of page */ |
| 1921 | seg1->mr_len += pageoff; |
| 1922 | len = -pageoff; |
Steve Wise | 0fc6c4e | 2014-05-28 10:32:00 -0400 | [diff] [blame] | 1923 | if (*nsegs > ia->ri_max_frmr_depth) |
| 1924 | *nsegs = ia->ri_max_frmr_depth; |
Tom Tucker | 9b78145 | 2012-02-20 13:07:57 -0600 | [diff] [blame] | 1925 | for (page_no = i = 0; i < *nsegs;) { |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 1926 | rpcrdma_map_one(ia, seg, writing); |
Tom Tucker | 9b78145 | 2012-02-20 13:07:57 -0600 | [diff] [blame] | 1927 | pa = seg->mr_dma; |
| 1928 | for (seg_len = seg->mr_len; seg_len > 0; seg_len -= PAGE_SIZE) { |
Chuck Lever | 0dbb410 | 2014-07-29 17:24:09 -0400 | [diff] [blame] | 1929 | frmr->fr_pgl->page_list[page_no++] = pa; |
Tom Tucker | 9b78145 | 2012-02-20 13:07:57 -0600 | [diff] [blame] | 1930 | pa += PAGE_SIZE; |
| 1931 | } |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 1932 | len += seg->mr_len; |
| 1933 | ++seg; |
| 1934 | ++i; |
| 1935 | /* Check for holes */ |
| 1936 | if ((i < *nsegs && offset_in_page(seg->mr_offset)) || |
| 1937 | offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len)) |
| 1938 | break; |
| 1939 | } |
| 1940 | dprintk("RPC: %s: Using frmr %p to map %d segments\n", |
Chuck Lever | 0dbb410 | 2014-07-29 17:24:09 -0400 | [diff] [blame] | 1941 | __func__, mw, i); |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 1942 | |
Chuck Lever | 0505572 | 2014-07-29 17:25:12 -0400 | [diff] [blame] | 1943 | frmr->fr_state = FRMR_IS_VALID; |
| 1944 | |
Chuck Lever | f590e87 | 2014-07-29 17:25:29 -0400 | [diff] [blame] | 1945 | memset(&fastreg_wr, 0, sizeof(fastreg_wr)); |
| 1946 | fastreg_wr.wr_id = (unsigned long)(void *)mw; |
| 1947 | fastreg_wr.opcode = IB_WR_FAST_REG_MR; |
| 1948 | fastreg_wr.wr.fast_reg.iova_start = seg1->mr_dma; |
| 1949 | fastreg_wr.wr.fast_reg.page_list = frmr->fr_pgl; |
| 1950 | fastreg_wr.wr.fast_reg.page_list_len = page_no; |
| 1951 | fastreg_wr.wr.fast_reg.page_shift = PAGE_SHIFT; |
| 1952 | fastreg_wr.wr.fast_reg.length = page_no << PAGE_SHIFT; |
| 1953 | if (fastreg_wr.wr.fast_reg.length < len) { |
Chuck Lever | 5fc83f4 | 2014-07-29 17:23:17 -0400 | [diff] [blame] | 1954 | rc = -EIO; |
| 1955 | goto out_err; |
Chuck Lever | c977dea | 2014-05-28 10:35:06 -0400 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | /* Bump the key */ |
Chuck Lever | 0dbb410 | 2014-07-29 17:24:09 -0400 | [diff] [blame] | 1959 | key = (u8)(mr->rkey & 0x000000FF); |
| 1960 | ib_update_fast_reg_key(mr, ++key); |
Chuck Lever | c977dea | 2014-05-28 10:35:06 -0400 | [diff] [blame] | 1961 | |
Chuck Lever | f590e87 | 2014-07-29 17:25:29 -0400 | [diff] [blame] | 1962 | fastreg_wr.wr.fast_reg.access_flags = (writing ? |
Vu Pham | 6874308 | 2009-05-26 14:51:00 -0400 | [diff] [blame] | 1963 | IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE : |
| 1964 | IB_ACCESS_REMOTE_READ); |
Chuck Lever | f590e87 | 2014-07-29 17:25:29 -0400 | [diff] [blame] | 1965 | fastreg_wr.wr.fast_reg.rkey = mr->rkey; |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 1966 | DECR_CQCOUNT(&r_xprt->rx_ep); |
| 1967 | |
Chuck Lever | f590e87 | 2014-07-29 17:25:29 -0400 | [diff] [blame] | 1968 | rc = ib_post_send(ia->ri_id->qp, &fastreg_wr, &bad_wr); |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 1969 | if (rc) { |
| 1970 | dprintk("RPC: %s: failed ib_post_send for register," |
| 1971 | " status %i\n", __func__, rc); |
Chuck Lever | c93e986 | 2014-07-29 17:24:19 -0400 | [diff] [blame] | 1972 | ib_update_fast_reg_key(mr, --key); |
Chuck Lever | 5fc83f4 | 2014-07-29 17:23:17 -0400 | [diff] [blame] | 1973 | goto out_err; |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 1974 | } else { |
Chuck Lever | 0dbb410 | 2014-07-29 17:24:09 -0400 | [diff] [blame] | 1975 | seg1->mr_rkey = mr->rkey; |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 1976 | seg1->mr_base = seg1->mr_dma + pageoff; |
| 1977 | seg1->mr_nsegs = i; |
| 1978 | seg1->mr_len = len; |
| 1979 | } |
| 1980 | *nsegs = i; |
Chuck Lever | 5fc83f4 | 2014-07-29 17:23:17 -0400 | [diff] [blame] | 1981 | return 0; |
| 1982 | out_err: |
Chuck Lever | 0505572 | 2014-07-29 17:25:12 -0400 | [diff] [blame] | 1983 | frmr->fr_state = FRMR_IS_INVALID; |
Chuck Lever | 5fc83f4 | 2014-07-29 17:23:17 -0400 | [diff] [blame] | 1984 | while (i--) |
| 1985 | rpcrdma_unmap_one(ia, --seg); |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 1986 | return rc; |
| 1987 | } |
| 1988 | |
| 1989 | static int |
| 1990 | rpcrdma_deregister_frmr_external(struct rpcrdma_mr_seg *seg, |
| 1991 | struct rpcrdma_ia *ia, struct rpcrdma_xprt *r_xprt) |
| 1992 | { |
| 1993 | struct rpcrdma_mr_seg *seg1 = seg; |
| 1994 | struct ib_send_wr invalidate_wr, *bad_wr; |
| 1995 | int rc; |
| 1996 | |
Chuck Lever | 3eb3581 | 2015-01-21 11:02:54 -0500 | [diff] [blame] | 1997 | seg1->rl_mw->r.frmr.fr_state = FRMR_IS_INVALID; |
Chuck Lever | dab7e3b | 2014-07-29 17:25:20 -0400 | [diff] [blame] | 1998 | |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 1999 | memset(&invalidate_wr, 0, sizeof invalidate_wr); |
Chuck Lever | 3eb3581 | 2015-01-21 11:02:54 -0500 | [diff] [blame] | 2000 | invalidate_wr.wr_id = (unsigned long)(void *)seg1->rl_mw; |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 2001 | invalidate_wr.opcode = IB_WR_LOCAL_INV; |
Chuck Lever | 3eb3581 | 2015-01-21 11:02:54 -0500 | [diff] [blame] | 2002 | invalidate_wr.ex.invalidate_rkey = seg1->rl_mw->r.frmr.fr_mr->rkey; |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 2003 | DECR_CQCOUNT(&r_xprt->rx_ep); |
| 2004 | |
Chuck Lever | 73806c8 | 2014-07-29 17:23:25 -0400 | [diff] [blame] | 2005 | read_lock(&ia->ri_qplock); |
| 2006 | while (seg1->mr_nsegs--) |
| 2007 | rpcrdma_unmap_one(ia, seg++); |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 2008 | rc = ib_post_send(ia->ri_id->qp, &invalidate_wr, &bad_wr); |
Chuck Lever | 73806c8 | 2014-07-29 17:23:25 -0400 | [diff] [blame] | 2009 | read_unlock(&ia->ri_qplock); |
Chuck Lever | dab7e3b | 2014-07-29 17:25:20 -0400 | [diff] [blame] | 2010 | if (rc) { |
| 2011 | /* Force rpcrdma_buffer_get() to retry */ |
Chuck Lever | 3eb3581 | 2015-01-21 11:02:54 -0500 | [diff] [blame] | 2012 | seg1->rl_mw->r.frmr.fr_state = FRMR_IS_STALE; |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 2013 | dprintk("RPC: %s: failed ib_post_send for invalidate," |
| 2014 | " status %i\n", __func__, rc); |
Chuck Lever | dab7e3b | 2014-07-29 17:25:20 -0400 | [diff] [blame] | 2015 | } |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 2016 | return rc; |
| 2017 | } |
| 2018 | |
| 2019 | static int |
Tom Talpey | 8d4ba03 | 2008-10-09 14:59:49 -0400 | [diff] [blame] | 2020 | rpcrdma_register_fmr_external(struct rpcrdma_mr_seg *seg, |
| 2021 | int *nsegs, int writing, struct rpcrdma_ia *ia) |
| 2022 | { |
| 2023 | struct rpcrdma_mr_seg *seg1 = seg; |
| 2024 | u64 physaddrs[RPCRDMA_MAX_DATA_SEGS]; |
| 2025 | int len, pageoff, i, rc; |
| 2026 | |
| 2027 | pageoff = offset_in_page(seg1->mr_offset); |
| 2028 | seg1->mr_offset -= pageoff; /* start of page */ |
| 2029 | seg1->mr_len += pageoff; |
| 2030 | len = -pageoff; |
| 2031 | if (*nsegs > RPCRDMA_MAX_DATA_SEGS) |
| 2032 | *nsegs = RPCRDMA_MAX_DATA_SEGS; |
| 2033 | for (i = 0; i < *nsegs;) { |
| 2034 | rpcrdma_map_one(ia, seg, writing); |
| 2035 | physaddrs[i] = seg->mr_dma; |
| 2036 | len += seg->mr_len; |
| 2037 | ++seg; |
| 2038 | ++i; |
| 2039 | /* Check for holes */ |
| 2040 | if ((i < *nsegs && offset_in_page(seg->mr_offset)) || |
| 2041 | offset_in_page((seg-1)->mr_offset + (seg-1)->mr_len)) |
| 2042 | break; |
| 2043 | } |
Chuck Lever | 3eb3581 | 2015-01-21 11:02:54 -0500 | [diff] [blame] | 2044 | rc = ib_map_phys_fmr(seg1->rl_mw->r.fmr, physaddrs, i, seg1->mr_dma); |
Tom Talpey | 8d4ba03 | 2008-10-09 14:59:49 -0400 | [diff] [blame] | 2045 | if (rc) { |
| 2046 | dprintk("RPC: %s: failed ib_map_phys_fmr " |
| 2047 | "%u@0x%llx+%i (%d)... status %i\n", __func__, |
| 2048 | len, (unsigned long long)seg1->mr_dma, |
| 2049 | pageoff, i, rc); |
| 2050 | while (i--) |
| 2051 | rpcrdma_unmap_one(ia, --seg); |
| 2052 | } else { |
Chuck Lever | 3eb3581 | 2015-01-21 11:02:54 -0500 | [diff] [blame] | 2053 | seg1->mr_rkey = seg1->rl_mw->r.fmr->rkey; |
Tom Talpey | 8d4ba03 | 2008-10-09 14:59:49 -0400 | [diff] [blame] | 2054 | seg1->mr_base = seg1->mr_dma + pageoff; |
| 2055 | seg1->mr_nsegs = i; |
| 2056 | seg1->mr_len = len; |
| 2057 | } |
| 2058 | *nsegs = i; |
| 2059 | return rc; |
| 2060 | } |
| 2061 | |
| 2062 | static int |
| 2063 | rpcrdma_deregister_fmr_external(struct rpcrdma_mr_seg *seg, |
| 2064 | struct rpcrdma_ia *ia) |
| 2065 | { |
| 2066 | struct rpcrdma_mr_seg *seg1 = seg; |
| 2067 | LIST_HEAD(l); |
| 2068 | int rc; |
| 2069 | |
Chuck Lever | 3eb3581 | 2015-01-21 11:02:54 -0500 | [diff] [blame] | 2070 | list_add(&seg1->rl_mw->r.fmr->list, &l); |
Tom Talpey | 8d4ba03 | 2008-10-09 14:59:49 -0400 | [diff] [blame] | 2071 | rc = ib_unmap_fmr(&l); |
Chuck Lever | 73806c8 | 2014-07-29 17:23:25 -0400 | [diff] [blame] | 2072 | read_lock(&ia->ri_qplock); |
Tom Talpey | 8d4ba03 | 2008-10-09 14:59:49 -0400 | [diff] [blame] | 2073 | while (seg1->mr_nsegs--) |
| 2074 | rpcrdma_unmap_one(ia, seg++); |
Chuck Lever | 73806c8 | 2014-07-29 17:23:25 -0400 | [diff] [blame] | 2075 | read_unlock(&ia->ri_qplock); |
Tom Talpey | 8d4ba03 | 2008-10-09 14:59:49 -0400 | [diff] [blame] | 2076 | if (rc) |
| 2077 | dprintk("RPC: %s: failed ib_unmap_fmr," |
| 2078 | " status %i\n", __func__, rc); |
| 2079 | return rc; |
| 2080 | } |
| 2081 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 2082 | int |
| 2083 | rpcrdma_register_external(struct rpcrdma_mr_seg *seg, |
| 2084 | int nsegs, int writing, struct rpcrdma_xprt *r_xprt) |
| 2085 | { |
| 2086 | struct rpcrdma_ia *ia = &r_xprt->rx_ia; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 2087 | int rc = 0; |
| 2088 | |
| 2089 | switch (ia->ri_memreg_strategy) { |
| 2090 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 2091 | case RPCRDMA_ALLPHYSICAL: |
| 2092 | rpcrdma_map_one(ia, seg, writing); |
| 2093 | seg->mr_rkey = ia->ri_bind_mem->rkey; |
| 2094 | seg->mr_base = seg->mr_dma; |
| 2095 | seg->mr_nsegs = 1; |
| 2096 | nsegs = 1; |
| 2097 | break; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 2098 | |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 2099 | /* Registration using frmr registration */ |
| 2100 | case RPCRDMA_FRMR: |
| 2101 | rc = rpcrdma_register_frmr_external(seg, &nsegs, writing, ia, r_xprt); |
| 2102 | break; |
| 2103 | |
Tom Talpey | 8d4ba03 | 2008-10-09 14:59:49 -0400 | [diff] [blame] | 2104 | /* Registration using fmr memory registration */ |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 2105 | case RPCRDMA_MTHCAFMR: |
Tom Talpey | 8d4ba03 | 2008-10-09 14:59:49 -0400 | [diff] [blame] | 2106 | rc = rpcrdma_register_fmr_external(seg, &nsegs, writing, ia); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 2107 | break; |
| 2108 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 2109 | default: |
Chuck Lever | 92b9836 | 2014-11-08 20:14:12 -0500 | [diff] [blame] | 2110 | return -EIO; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 2111 | } |
| 2112 | if (rc) |
Chuck Lever | 92b9836 | 2014-11-08 20:14:12 -0500 | [diff] [blame] | 2113 | return rc; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 2114 | |
| 2115 | return nsegs; |
| 2116 | } |
| 2117 | |
| 2118 | int |
| 2119 | rpcrdma_deregister_external(struct rpcrdma_mr_seg *seg, |
Chuck Lever | 13c9ff8 | 2014-05-28 10:33:08 -0400 | [diff] [blame] | 2120 | struct rpcrdma_xprt *r_xprt) |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 2121 | { |
| 2122 | struct rpcrdma_ia *ia = &r_xprt->rx_ia; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 2123 | int nsegs = seg->mr_nsegs, rc; |
| 2124 | |
| 2125 | switch (ia->ri_memreg_strategy) { |
| 2126 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 2127 | case RPCRDMA_ALLPHYSICAL: |
Chuck Lever | 73806c8 | 2014-07-29 17:23:25 -0400 | [diff] [blame] | 2128 | read_lock(&ia->ri_qplock); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 2129 | rpcrdma_unmap_one(ia, seg); |
Chuck Lever | 73806c8 | 2014-07-29 17:23:25 -0400 | [diff] [blame] | 2130 | read_unlock(&ia->ri_qplock); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 2131 | break; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 2132 | |
Tom Talpey | 3197d309 | 2008-10-09 15:00:20 -0400 | [diff] [blame] | 2133 | case RPCRDMA_FRMR: |
| 2134 | rc = rpcrdma_deregister_frmr_external(seg, ia, r_xprt); |
| 2135 | break; |
| 2136 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 2137 | case RPCRDMA_MTHCAFMR: |
Tom Talpey | 8d4ba03 | 2008-10-09 14:59:49 -0400 | [diff] [blame] | 2138 | rc = rpcrdma_deregister_fmr_external(seg, ia); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 2139 | break; |
| 2140 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 2141 | default: |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 2142 | break; |
| 2143 | } |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 2144 | return nsegs; |
| 2145 | } |
| 2146 | |
| 2147 | /* |
| 2148 | * Prepost any receive buffer, then post send. |
| 2149 | * |
| 2150 | * Receive buffer is donated to hardware, reclaimed upon recv completion. |
| 2151 | */ |
| 2152 | int |
| 2153 | rpcrdma_ep_post(struct rpcrdma_ia *ia, |
| 2154 | struct rpcrdma_ep *ep, |
| 2155 | struct rpcrdma_req *req) |
| 2156 | { |
| 2157 | struct ib_send_wr send_wr, *send_wr_fail; |
| 2158 | struct rpcrdma_rep *rep = req->rl_reply; |
| 2159 | int rc; |
| 2160 | |
| 2161 | if (rep) { |
| 2162 | rc = rpcrdma_ep_post_recv(ia, ep, rep); |
| 2163 | if (rc) |
| 2164 | goto out; |
| 2165 | req->rl_reply = NULL; |
| 2166 | } |
| 2167 | |
| 2168 | send_wr.next = NULL; |
| 2169 | send_wr.wr_id = 0ULL; /* no send cookie */ |
| 2170 | send_wr.sg_list = req->rl_send_iov; |
| 2171 | send_wr.num_sge = req->rl_niovs; |
| 2172 | send_wr.opcode = IB_WR_SEND; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 2173 | if (send_wr.num_sge == 4) /* no need to sync any pad (constant) */ |
| 2174 | ib_dma_sync_single_for_device(ia->ri_id->device, |
| 2175 | req->rl_send_iov[3].addr, req->rl_send_iov[3].length, |
| 2176 | DMA_TO_DEVICE); |
| 2177 | ib_dma_sync_single_for_device(ia->ri_id->device, |
| 2178 | req->rl_send_iov[1].addr, req->rl_send_iov[1].length, |
| 2179 | DMA_TO_DEVICE); |
| 2180 | ib_dma_sync_single_for_device(ia->ri_id->device, |
| 2181 | req->rl_send_iov[0].addr, req->rl_send_iov[0].length, |
| 2182 | DMA_TO_DEVICE); |
| 2183 | |
| 2184 | if (DECR_CQCOUNT(ep) > 0) |
| 2185 | send_wr.send_flags = 0; |
| 2186 | else { /* Provider must take a send completion every now and then */ |
| 2187 | INIT_CQCOUNT(ep); |
| 2188 | send_wr.send_flags = IB_SEND_SIGNALED; |
| 2189 | } |
| 2190 | |
| 2191 | rc = ib_post_send(ia->ri_id->qp, &send_wr, &send_wr_fail); |
| 2192 | if (rc) |
| 2193 | dprintk("RPC: %s: ib_post_send returned %i\n", __func__, |
| 2194 | rc); |
| 2195 | out: |
| 2196 | return rc; |
| 2197 | } |
| 2198 | |
| 2199 | /* |
| 2200 | * (Re)post a receive buffer. |
| 2201 | */ |
| 2202 | int |
| 2203 | rpcrdma_ep_post_recv(struct rpcrdma_ia *ia, |
| 2204 | struct rpcrdma_ep *ep, |
| 2205 | struct rpcrdma_rep *rep) |
| 2206 | { |
| 2207 | struct ib_recv_wr recv_wr, *recv_wr_fail; |
| 2208 | int rc; |
| 2209 | |
| 2210 | recv_wr.next = NULL; |
| 2211 | recv_wr.wr_id = (u64) (unsigned long) rep; |
| 2212 | recv_wr.sg_list = &rep->rr_iov; |
| 2213 | recv_wr.num_sge = 1; |
| 2214 | |
| 2215 | ib_dma_sync_single_for_cpu(ia->ri_id->device, |
| 2216 | rep->rr_iov.addr, rep->rr_iov.length, DMA_BIDIRECTIONAL); |
| 2217 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 2218 | rc = ib_post_recv(ia->ri_id->qp, &recv_wr, &recv_wr_fail); |
| 2219 | |
| 2220 | if (rc) |
| 2221 | dprintk("RPC: %s: ib_post_recv returned %i\n", __func__, |
| 2222 | rc); |
| 2223 | return rc; |
| 2224 | } |
Chuck Lever | 43e9598 | 2014-07-29 17:23:34 -0400 | [diff] [blame] | 2225 | |
| 2226 | /* Physical mapping means one Read/Write list entry per-page. |
| 2227 | * All list entries must fit within an inline buffer |
| 2228 | * |
| 2229 | * NB: The server must return a Write list for NFS READ, |
| 2230 | * which has the same constraint. Factor in the inline |
| 2231 | * rsize as well. |
| 2232 | */ |
| 2233 | static size_t |
| 2234 | rpcrdma_physical_max_payload(struct rpcrdma_xprt *r_xprt) |
| 2235 | { |
| 2236 | struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data; |
| 2237 | unsigned int inline_size, pages; |
| 2238 | |
| 2239 | inline_size = min_t(unsigned int, |
| 2240 | cdata->inline_wsize, cdata->inline_rsize); |
| 2241 | inline_size -= RPCRDMA_HDRLEN_MIN; |
| 2242 | pages = inline_size / sizeof(struct rpcrdma_segment); |
| 2243 | return pages << PAGE_SHIFT; |
| 2244 | } |
| 2245 | |
| 2246 | static size_t |
| 2247 | rpcrdma_mr_max_payload(struct rpcrdma_xprt *r_xprt) |
| 2248 | { |
| 2249 | return RPCRDMA_MAX_DATA_SEGS << PAGE_SHIFT; |
| 2250 | } |
| 2251 | |
| 2252 | size_t |
| 2253 | rpcrdma_max_payload(struct rpcrdma_xprt *r_xprt) |
| 2254 | { |
| 2255 | size_t result; |
| 2256 | |
| 2257 | switch (r_xprt->rx_ia.ri_memreg_strategy) { |
| 2258 | case RPCRDMA_ALLPHYSICAL: |
| 2259 | result = rpcrdma_physical_max_payload(r_xprt); |
| 2260 | break; |
| 2261 | default: |
| 2262 | result = rpcrdma_mr_max_payload(r_xprt); |
| 2263 | } |
| 2264 | return result; |
| 2265 | } |