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