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