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