Chuck Lever | a2268cf | 2018-05-04 15:34:32 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause |
\"Talpey, Thomas\ | f58851e | 2007-09-10 13:50:12 -0400 | [diff] [blame] | 2 | /* |
Chuck Lever | 62b56a6 | 2017-10-30 16:22:14 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2017 Oracle. All rights reserved. |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 4 | * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved. |
| 5 | * |
| 6 | * This software is available to you under a choice of one of two |
| 7 | * licenses. You may choose to be licensed under the terms of the GNU |
| 8 | * General Public License (GPL) Version 2, available from the file |
| 9 | * COPYING in the main directory of this source tree, or the BSD-type |
| 10 | * license below: |
| 11 | * |
| 12 | * Redistribution and use in source and binary forms, with or without |
| 13 | * modification, are permitted provided that the following conditions |
| 14 | * are met: |
| 15 | * |
| 16 | * Redistributions of source code must retain the above copyright |
| 17 | * notice, this list of conditions and the following disclaimer. |
| 18 | * |
| 19 | * Redistributions in binary form must reproduce the above |
| 20 | * copyright notice, this list of conditions and the following |
| 21 | * disclaimer in the documentation and/or other materials provided |
| 22 | * with the distribution. |
| 23 | * |
| 24 | * Neither the name of the Network Appliance, Inc. nor the names of |
| 25 | * its contributors may be used to endorse or promote products |
| 26 | * derived from this software without specific prior written |
| 27 | * permission. |
| 28 | * |
| 29 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 30 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 31 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 32 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 33 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 34 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 35 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 36 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 37 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 38 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 39 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
\"Talpey, Thomas\ | f58851e | 2007-09-10 13:50:12 -0400 | [diff] [blame] | 40 | */ |
| 41 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 42 | /* |
| 43 | * verbs.c |
| 44 | * |
| 45 | * Encapsulates the major functions managing: |
| 46 | * o adapters |
| 47 | * o endpoints |
| 48 | * o connections |
| 49 | * o buffer memory |
| 50 | */ |
| 51 | |
Alexey Dobriyan | a6b7a40 | 2011-06-06 10:43:46 +0000 | [diff] [blame] | 52 | #include <linux/interrupt.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 53 | #include <linux/slab.h> |
Chuck Lever | 0dd39ca | 2015-03-30 14:33:43 -0400 | [diff] [blame] | 54 | #include <linux/sunrpc/addr.h> |
Chuck Lever | 05c9746 | 2016-09-06 11:22:58 -0400 | [diff] [blame] | 55 | #include <linux/sunrpc/svc_rdma.h> |
Chuck Lever | ae72950 | 2017-10-20 10:48:12 -0400 | [diff] [blame] | 56 | |
| 57 | #include <asm-generic/barrier.h> |
Chuck Lever | 65866f8 | 2014-05-28 10:33:59 -0400 | [diff] [blame] | 58 | #include <asm/bitops.h> |
Chuck Lever | 56a6bd1 | 2017-04-11 13:23:34 -0400 | [diff] [blame] | 59 | |
Chuck Lever | 0a90487 | 2017-02-08 17:00:35 -0500 | [diff] [blame] | 60 | #include <rdma/ib_cm.h> |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 61 | |
\"Talpey, Thomas\ | f58851e | 2007-09-10 13:50:12 -0400 | [diff] [blame] | 62 | #include "xprt_rdma.h" |
| 63 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 64 | /* |
| 65 | * Globals/Macros |
| 66 | */ |
| 67 | |
Jeff Layton | f895b25 | 2014-11-17 16:58:04 -0500 | [diff] [blame] | 68 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 69 | # define RPCDBG_FACILITY RPCDBG_TRANS |
| 70 | #endif |
| 71 | |
| 72 | /* |
| 73 | * internal functions |
| 74 | */ |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 75 | static void rpcrdma_mrs_create(struct rpcrdma_xprt *r_xprt); |
| 76 | static void rpcrdma_mrs_destroy(struct rpcrdma_buffer *buf); |
Chuck Lever | 7c8d9e7 | 2018-05-04 15:35:20 -0400 | [diff] [blame] | 77 | static int rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt, bool temp); |
Chuck Lever | bebd031 | 2017-04-11 13:23:10 -0400 | [diff] [blame] | 78 | static void rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 79 | |
Chuck Lever | d8f532d | 2017-10-16 15:01:30 -0400 | [diff] [blame] | 80 | struct workqueue_struct *rpcrdma_receive_wq __read_mostly; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 81 | |
Chuck Lever | fe97b47 | 2015-10-24 17:27:10 -0400 | [diff] [blame] | 82 | int |
| 83 | rpcrdma_alloc_wq(void) |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 84 | { |
Chuck Lever | fe97b47 | 2015-10-24 17:27:10 -0400 | [diff] [blame] | 85 | struct workqueue_struct *recv_wq; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 86 | |
Chuck Lever | fe97b47 | 2015-10-24 17:27:10 -0400 | [diff] [blame] | 87 | recv_wq = alloc_workqueue("xprtrdma_receive", |
Chuck Lever | ccede75 | 2017-12-04 14:04:04 -0500 | [diff] [blame] | 88 | WQ_MEM_RECLAIM | WQ_HIGHPRI, |
Chuck Lever | fe97b47 | 2015-10-24 17:27:10 -0400 | [diff] [blame] | 89 | 0); |
| 90 | if (!recv_wq) |
| 91 | return -ENOMEM; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 92 | |
Chuck Lever | fe97b47 | 2015-10-24 17:27:10 -0400 | [diff] [blame] | 93 | rpcrdma_receive_wq = recv_wq; |
| 94 | return 0; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 95 | } |
| 96 | |
Chuck Lever | fe97b47 | 2015-10-24 17:27:10 -0400 | [diff] [blame] | 97 | void |
| 98 | rpcrdma_destroy_wq(void) |
Chuck Lever | f1a03b7 | 2014-11-08 20:14:37 -0500 | [diff] [blame] | 99 | { |
Chuck Lever | fe97b47 | 2015-10-24 17:27:10 -0400 | [diff] [blame] | 100 | struct workqueue_struct *wq; |
Chuck Lever | f1a03b7 | 2014-11-08 20:14:37 -0500 | [diff] [blame] | 101 | |
Chuck Lever | fe97b47 | 2015-10-24 17:27:10 -0400 | [diff] [blame] | 102 | if (rpcrdma_receive_wq) { |
| 103 | wq = rpcrdma_receive_wq; |
| 104 | rpcrdma_receive_wq = NULL; |
| 105 | destroy_workqueue(wq); |
| 106 | } |
Chuck Lever | f1a03b7 | 2014-11-08 20:14:37 -0500 | [diff] [blame] | 107 | } |
| 108 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 109 | static void |
| 110 | rpcrdma_qp_async_error_upcall(struct ib_event *event, void *context) |
| 111 | { |
| 112 | struct rpcrdma_ep *ep = context; |
Chuck Lever | 643cf32 | 2017-12-20 16:31:45 -0500 | [diff] [blame] | 113 | struct rpcrdma_xprt *r_xprt = container_of(ep, struct rpcrdma_xprt, |
| 114 | rx_ep); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 115 | |
Chuck Lever | 643cf32 | 2017-12-20 16:31:45 -0500 | [diff] [blame] | 116 | trace_xprtrdma_qp_error(r_xprt, event); |
Chuck Lever | 2f6922c | 2016-11-29 10:53:21 -0500 | [diff] [blame] | 117 | pr_err("rpcrdma: %s on device %s ep %p\n", |
| 118 | ib_event_msg(event->event), event->device->name, context); |
| 119 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 120 | if (ep->rep_connected == 1) { |
| 121 | ep->rep_connected = -EIO; |
Chuck Lever | afadc46 | 2015-01-21 11:03:11 -0500 | [diff] [blame] | 122 | rpcrdma_conn_func(ep); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 123 | wake_up_all(&ep->rep_connect_wait); |
| 124 | } |
| 125 | } |
| 126 | |
Chuck Lever | 2fa8f88 | 2016-03-04 11:28:53 -0500 | [diff] [blame] | 127 | /** |
| 128 | * rpcrdma_wc_send - Invoked by RDMA provider for each polled Send WC |
| 129 | * @cq: completion queue (ignored) |
| 130 | * @wc: completed WR |
| 131 | * |
Chuck Lever | 4220a07 | 2015-10-24 17:26:45 -0400 | [diff] [blame] | 132 | */ |
| 133 | static void |
Chuck Lever | 2fa8f88 | 2016-03-04 11:28:53 -0500 | [diff] [blame] | 134 | rpcrdma_wc_send(struct ib_cq *cq, struct ib_wc *wc) |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 135 | { |
Chuck Lever | ae72950 | 2017-10-20 10:48:12 -0400 | [diff] [blame] | 136 | struct ib_cqe *cqe = wc->wr_cqe; |
| 137 | struct rpcrdma_sendctx *sc = |
| 138 | container_of(cqe, struct rpcrdma_sendctx, sc_cqe); |
| 139 | |
Chuck Lever | 2fa8f88 | 2016-03-04 11:28:53 -0500 | [diff] [blame] | 140 | /* WARNING: Only wr_cqe and status are reliable at this point */ |
Chuck Lever | ab03eff | 2017-12-20 16:30:40 -0500 | [diff] [blame] | 141 | trace_xprtrdma_wc_send(sc, wc); |
Chuck Lever | 2fa8f88 | 2016-03-04 11:28:53 -0500 | [diff] [blame] | 142 | if (wc->status != IB_WC_SUCCESS && wc->status != IB_WC_WR_FLUSH_ERR) |
| 143 | pr_err("rpcrdma: Send: %s (%u/0x%x)\n", |
| 144 | ib_wc_status_msg(wc->status), |
| 145 | wc->status, wc->vendor_err); |
Chuck Lever | ae72950 | 2017-10-20 10:48:12 -0400 | [diff] [blame] | 146 | |
| 147 | rpcrdma_sendctx_put_locked(sc); |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 148 | } |
| 149 | |
Chuck Lever | 552bf22 | 2016-03-04 11:28:36 -0500 | [diff] [blame] | 150 | /** |
Chuck Lever | 1519e96 | 2016-09-15 10:57:49 -0400 | [diff] [blame] | 151 | * rpcrdma_wc_receive - Invoked by RDMA provider for each polled Receive WC |
Chuck Lever | 552bf22 | 2016-03-04 11:28:36 -0500 | [diff] [blame] | 152 | * @cq: completion queue (ignored) |
| 153 | * @wc: completed WR |
| 154 | * |
| 155 | */ |
Chuck Lever | fe97b47 | 2015-10-24 17:27:10 -0400 | [diff] [blame] | 156 | static void |
Chuck Lever | 1519e96 | 2016-09-15 10:57:49 -0400 | [diff] [blame] | 157 | rpcrdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc) |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 158 | { |
Chuck Lever | 552bf22 | 2016-03-04 11:28:36 -0500 | [diff] [blame] | 159 | struct ib_cqe *cqe = wc->wr_cqe; |
| 160 | struct rpcrdma_rep *rep = container_of(cqe, struct rpcrdma_rep, |
| 161 | rr_cqe); |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 162 | |
Chuck Lever | 8502427 | 2015-01-21 11:02:04 -0500 | [diff] [blame] | 163 | /* WARNING: Only wr_id and status are reliable at this point */ |
Chuck Lever | 0e0b854 | 2018-05-04 15:35:14 -0400 | [diff] [blame] | 164 | trace_xprtrdma_wc_receive(wc); |
Chuck Lever | 8502427 | 2015-01-21 11:02:04 -0500 | [diff] [blame] | 165 | if (wc->status != IB_WC_SUCCESS) |
| 166 | goto out_fail; |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 167 | |
Chuck Lever | 8502427 | 2015-01-21 11:02:04 -0500 | [diff] [blame] | 168 | /* status == SUCCESS means all fields in wc are trustworthy */ |
Chuck Lever | 96f8778 | 2017-08-03 14:30:03 -0400 | [diff] [blame] | 169 | rpcrdma_set_xdrlen(&rep->rr_hdrbuf, wc->byte_len); |
Chuck Lever | c8b920b | 2016-09-15 10:57:16 -0400 | [diff] [blame] | 170 | rep->rr_wc_flags = wc->wc_flags; |
| 171 | rep->rr_inv_rkey = wc->ex.invalidate_rkey; |
| 172 | |
Chuck Lever | 91a10c5 | 2017-04-11 13:23:02 -0400 | [diff] [blame] | 173 | ib_dma_sync_single_for_cpu(rdmab_device(rep->rr_rdmabuf), |
Chuck Lever | 6b1184c | 2015-01-21 11:04:25 -0500 | [diff] [blame] | 174 | rdmab_addr(rep->rr_rdmabuf), |
Chuck Lever | e2a6719 | 2017-08-03 14:30:44 -0400 | [diff] [blame] | 175 | wc->byte_len, DMA_FROM_DEVICE); |
Chuck Lever | 23826c7 | 2016-03-04 11:28:27 -0500 | [diff] [blame] | 176 | |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 177 | out_schedule: |
Chuck Lever | d8f532d | 2017-10-16 15:01:30 -0400 | [diff] [blame] | 178 | rpcrdma_reply_handler(rep); |
Chuck Lever | 8502427 | 2015-01-21 11:02:04 -0500 | [diff] [blame] | 179 | return; |
Chuck Lever | fe97b47 | 2015-10-24 17:27:10 -0400 | [diff] [blame] | 180 | |
Chuck Lever | 8502427 | 2015-01-21 11:02:04 -0500 | [diff] [blame] | 181 | out_fail: |
| 182 | if (wc->status != IB_WC_WR_FLUSH_ERR) |
Chuck Lever | 552bf22 | 2016-03-04 11:28:36 -0500 | [diff] [blame] | 183 | pr_err("rpcrdma: Recv: %s (%u/0x%x)\n", |
| 184 | ib_wc_status_msg(wc->status), |
| 185 | wc->status, wc->vendor_err); |
Chuck Lever | e2a6719 | 2017-08-03 14:30:44 -0400 | [diff] [blame] | 186 | rpcrdma_set_xdrlen(&rep->rr_hdrbuf, 0); |
Chuck Lever | 8502427 | 2015-01-21 11:02:04 -0500 | [diff] [blame] | 187 | goto out_schedule; |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 188 | } |
| 189 | |
Chuck Lever | 87cfb9a | 2016-09-15 10:57:07 -0400 | [diff] [blame] | 190 | static void |
| 191 | rpcrdma_update_connect_private(struct rpcrdma_xprt *r_xprt, |
| 192 | struct rdma_conn_param *param) |
| 193 | { |
| 194 | struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data; |
| 195 | const struct rpcrdma_connect_private *pmsg = param->private_data; |
| 196 | unsigned int rsize, wsize; |
| 197 | |
Chuck Lever | c8b920b | 2016-09-15 10:57:16 -0400 | [diff] [blame] | 198 | /* Default settings for RPC-over-RDMA Version One */ |
Chuck Lever | b5f0afb | 2017-02-08 16:59:54 -0500 | [diff] [blame] | 199 | r_xprt->rx_ia.ri_implicit_roundup = xprt_rdma_pad_optimize; |
Chuck Lever | 87cfb9a | 2016-09-15 10:57:07 -0400 | [diff] [blame] | 200 | rsize = RPCRDMA_V1_DEF_INLINE_SIZE; |
| 201 | wsize = RPCRDMA_V1_DEF_INLINE_SIZE; |
| 202 | |
| 203 | if (pmsg && |
| 204 | pmsg->cp_magic == rpcrdma_cmp_magic && |
| 205 | pmsg->cp_version == RPCRDMA_CMP_VERSION) { |
Chuck Lever | c95a3c6 | 2017-02-08 17:00:02 -0500 | [diff] [blame] | 206 | r_xprt->rx_ia.ri_implicit_roundup = true; |
Chuck Lever | 87cfb9a | 2016-09-15 10:57:07 -0400 | [diff] [blame] | 207 | rsize = rpcrdma_decode_buffer_size(pmsg->cp_send_size); |
| 208 | wsize = rpcrdma_decode_buffer_size(pmsg->cp_recv_size); |
| 209 | } |
| 210 | |
| 211 | if (rsize < cdata->inline_rsize) |
| 212 | cdata->inline_rsize = rsize; |
| 213 | if (wsize < cdata->inline_wsize) |
| 214 | cdata->inline_wsize = wsize; |
Chuck Lever | 6d6bf72 | 2016-11-29 10:53:13 -0500 | [diff] [blame] | 215 | dprintk("RPC: %s: max send %u, max recv %u\n", |
| 216 | __func__, cdata->inline_wsize, cdata->inline_rsize); |
Chuck Lever | 87cfb9a | 2016-09-15 10:57:07 -0400 | [diff] [blame] | 217 | rpcrdma_set_max_header_sizes(r_xprt); |
| 218 | } |
| 219 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 220 | static int |
| 221 | rpcrdma_conn_upcall(struct rdma_cm_id *id, struct rdma_cm_event *event) |
| 222 | { |
| 223 | struct rpcrdma_xprt *xprt = id->context; |
| 224 | struct rpcrdma_ia *ia = &xprt->rx_ia; |
| 225 | struct rpcrdma_ep *ep = &xprt->rx_ep; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 226 | int connstate = 0; |
| 227 | |
Chuck Lever | b4744e0 | 2017-12-20 16:31:29 -0500 | [diff] [blame] | 228 | trace_xprtrdma_conn_upcall(xprt, event); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 229 | switch (event->event) { |
| 230 | case RDMA_CM_EVENT_ADDR_RESOLVED: |
| 231 | case RDMA_CM_EVENT_ROUTE_RESOLVED: |
Tom Talpey | 5675add | 2008-10-09 15:01:41 -0400 | [diff] [blame] | 232 | ia->ri_async_rc = 0; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 233 | complete(&ia->ri_done); |
| 234 | break; |
| 235 | case RDMA_CM_EVENT_ADDR_ERROR: |
Chuck Lever | 52d28fe | 2018-05-04 15:34:37 -0400 | [diff] [blame] | 236 | ia->ri_async_rc = -EPROTO; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 237 | complete(&ia->ri_done); |
| 238 | break; |
| 239 | case RDMA_CM_EVENT_ROUTE_ERROR: |
| 240 | ia->ri_async_rc = -ENETUNREACH; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 241 | complete(&ia->ri_done); |
| 242 | break; |
Chuck Lever | bebd031 | 2017-04-11 13:23:10 -0400 | [diff] [blame] | 243 | case RDMA_CM_EVENT_DEVICE_REMOVAL: |
| 244 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) |
Chuck Lever | d461f1f | 2017-12-14 20:56:50 -0500 | [diff] [blame] | 245 | pr_info("rpcrdma: removing device %s for %s:%s\n", |
Chuck Lever | 173b8f4 | 2017-06-08 11:53:00 -0400 | [diff] [blame] | 246 | ia->ri_device->name, |
Chuck Lever | d461f1f | 2017-12-14 20:56:50 -0500 | [diff] [blame] | 247 | rpcrdma_addrstr(xprt), rpcrdma_portstr(xprt)); |
Chuck Lever | bebd031 | 2017-04-11 13:23:10 -0400 | [diff] [blame] | 248 | #endif |
| 249 | set_bit(RPCRDMA_IAF_REMOVING, &ia->ri_flags); |
| 250 | ep->rep_connected = -ENODEV; |
| 251 | xprt_force_disconnect(&xprt->rx_xprt); |
| 252 | wait_for_completion(&ia->ri_remove_done); |
| 253 | |
| 254 | ia->ri_id = NULL; |
Chuck Lever | bebd031 | 2017-04-11 13:23:10 -0400 | [diff] [blame] | 255 | ia->ri_device = NULL; |
| 256 | /* Return 1 to ensure the core destroys the id. */ |
| 257 | return 1; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 258 | case RDMA_CM_EVENT_ESTABLISHED: |
Chuck Lever | 8a14793 | 2018-02-28 15:30:38 -0500 | [diff] [blame] | 259 | ++xprt->rx_xprt.connect_cookie; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 260 | connstate = 1; |
Chuck Lever | 87cfb9a | 2016-09-15 10:57:07 -0400 | [diff] [blame] | 261 | rpcrdma_update_connect_private(xprt, &event->param.conn); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 262 | goto connected; |
| 263 | case RDMA_CM_EVENT_CONNECT_ERROR: |
| 264 | connstate = -ENOTCONN; |
| 265 | goto connected; |
| 266 | case RDMA_CM_EVENT_UNREACHABLE: |
Chuck Lever | 52d28fe | 2018-05-04 15:34:37 -0400 | [diff] [blame] | 267 | connstate = -ENETUNREACH; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 268 | goto connected; |
| 269 | case RDMA_CM_EVENT_REJECTED: |
Chuck Lever | d461f1f | 2017-12-14 20:56:50 -0500 | [diff] [blame] | 270 | dprintk("rpcrdma: connection to %s:%s rejected: %s\n", |
| 271 | rpcrdma_addrstr(xprt), rpcrdma_portstr(xprt), |
Chuck Lever | 0a90487 | 2017-02-08 17:00:35 -0500 | [diff] [blame] | 272 | rdma_reject_msg(id, event->status)); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 273 | connstate = -ECONNREFUSED; |
Chuck Lever | 0a90487 | 2017-02-08 17:00:35 -0500 | [diff] [blame] | 274 | if (event->status == IB_CM_REJ_STALE_CONN) |
| 275 | connstate = -EAGAIN; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 276 | goto connected; |
| 277 | case RDMA_CM_EVENT_DISCONNECTED: |
Chuck Lever | 8a14793 | 2018-02-28 15:30:38 -0500 | [diff] [blame] | 278 | ++xprt->rx_xprt.connect_cookie; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 279 | connstate = -ECONNABORTED; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 280 | connected: |
Chuck Lever | be798f9 | 2017-10-16 15:01:39 -0400 | [diff] [blame] | 281 | xprt->rx_buf.rb_credits = 1; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 282 | ep->rep_connected = connstate; |
Chuck Lever | afadc46 | 2015-01-21 11:03:11 -0500 | [diff] [blame] | 283 | rpcrdma_conn_func(ep); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 284 | wake_up_all(&ep->rep_connect_wait); |
Chuck Lever | 8079fb7 | 2014-07-29 17:26:12 -0400 | [diff] [blame] | 285 | /*FALLTHROUGH*/ |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 286 | default: |
Chuck Lever | d461f1f | 2017-12-14 20:56:50 -0500 | [diff] [blame] | 287 | dprintk("RPC: %s: %s:%s on %s/%s (ep 0x%p): %s\n", |
| 288 | __func__, |
| 289 | rpcrdma_addrstr(xprt), rpcrdma_portstr(xprt), |
Chuck Lever | 173b8f4 | 2017-06-08 11:53:00 -0400 | [diff] [blame] | 290 | ia->ri_device->name, ia->ri_ops->ro_displayname, |
| 291 | ep, rdma_event_msg(event->event)); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 292 | break; |
| 293 | } |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static struct rdma_cm_id * |
Chuck Lever | dd229ce | 2017-12-14 20:56:58 -0500 | [diff] [blame] | 299 | rpcrdma_create_id(struct rpcrdma_xprt *xprt, struct rpcrdma_ia *ia) |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 300 | { |
Chuck Lever | 109b88a | 2016-11-29 10:52:40 -0500 | [diff] [blame] | 301 | unsigned long wtimeout = msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT) + 1; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 302 | struct rdma_cm_id *id; |
| 303 | int rc; |
| 304 | |
Chuck Lever | b4744e0 | 2017-12-20 16:31:29 -0500 | [diff] [blame] | 305 | trace_xprtrdma_conn_start(xprt); |
| 306 | |
Tom Talpey | 1a95405 | 2008-10-09 15:01:31 -0400 | [diff] [blame] | 307 | init_completion(&ia->ri_done); |
Chuck Lever | bebd031 | 2017-04-11 13:23:10 -0400 | [diff] [blame] | 308 | init_completion(&ia->ri_remove_done); |
Tom Talpey | 1a95405 | 2008-10-09 15:01:31 -0400 | [diff] [blame] | 309 | |
Chuck Lever | 107c4be | 2018-05-04 15:34:42 -0400 | [diff] [blame] | 310 | id = rdma_create_id(xprt->rx_xprt.xprt_net, rpcrdma_conn_upcall, |
| 311 | xprt, RDMA_PS_TCP, IB_QPT_RC); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 312 | if (IS_ERR(id)) { |
| 313 | rc = PTR_ERR(id); |
| 314 | dprintk("RPC: %s: rdma_create_id() failed %i\n", |
| 315 | __func__, rc); |
| 316 | return id; |
| 317 | } |
| 318 | |
Tom Talpey | 5675add | 2008-10-09 15:01:41 -0400 | [diff] [blame] | 319 | ia->ri_async_rc = -ETIMEDOUT; |
Chuck Lever | dd229ce | 2017-12-14 20:56:58 -0500 | [diff] [blame] | 320 | rc = rdma_resolve_addr(id, NULL, |
| 321 | (struct sockaddr *)&xprt->rx_xprt.addr, |
| 322 | RDMA_RESOLVE_TIMEOUT); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 323 | if (rc) { |
| 324 | dprintk("RPC: %s: rdma_resolve_addr() failed %i\n", |
| 325 | __func__, rc); |
| 326 | goto out; |
| 327 | } |
Chuck Lever | 109b88a | 2016-11-29 10:52:40 -0500 | [diff] [blame] | 328 | rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout); |
| 329 | if (rc < 0) { |
Chuck Lever | b4744e0 | 2017-12-20 16:31:29 -0500 | [diff] [blame] | 330 | trace_xprtrdma_conn_tout(xprt); |
Chuck Lever | 109b88a | 2016-11-29 10:52:40 -0500 | [diff] [blame] | 331 | goto out; |
| 332 | } |
Devesh Sharma | d0f36c4 | 2015-08-03 13:05:04 -0400 | [diff] [blame] | 333 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 334 | rc = ia->ri_async_rc; |
| 335 | if (rc) |
| 336 | goto out; |
| 337 | |
Tom Talpey | 5675add | 2008-10-09 15:01:41 -0400 | [diff] [blame] | 338 | ia->ri_async_rc = -ETIMEDOUT; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 339 | rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT); |
| 340 | if (rc) { |
| 341 | dprintk("RPC: %s: rdma_resolve_route() failed %i\n", |
| 342 | __func__, rc); |
Chuck Lever | 56a6bd1 | 2017-04-11 13:23:34 -0400 | [diff] [blame] | 343 | goto out; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 344 | } |
Chuck Lever | 109b88a | 2016-11-29 10:52:40 -0500 | [diff] [blame] | 345 | rc = wait_for_completion_interruptible_timeout(&ia->ri_done, wtimeout); |
| 346 | if (rc < 0) { |
Chuck Lever | b4744e0 | 2017-12-20 16:31:29 -0500 | [diff] [blame] | 347 | trace_xprtrdma_conn_tout(xprt); |
Chuck Lever | 56a6bd1 | 2017-04-11 13:23:34 -0400 | [diff] [blame] | 348 | goto out; |
Chuck Lever | 109b88a | 2016-11-29 10:52:40 -0500 | [diff] [blame] | 349 | } |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 350 | rc = ia->ri_async_rc; |
| 351 | if (rc) |
Chuck Lever | 56a6bd1 | 2017-04-11 13:23:34 -0400 | [diff] [blame] | 352 | goto out; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 353 | |
| 354 | return id; |
Chuck Lever | 56a6bd1 | 2017-04-11 13:23:34 -0400 | [diff] [blame] | 355 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 356 | out: |
| 357 | rdma_destroy_id(id); |
| 358 | return ERR_PTR(rc); |
| 359 | } |
| 360 | |
| 361 | /* |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 362 | * Exported functions. |
| 363 | */ |
| 364 | |
Chuck Lever | fff0959 | 2017-04-11 13:22:54 -0400 | [diff] [blame] | 365 | /** |
| 366 | * rpcrdma_ia_open - Open and initialize an Interface Adapter. |
Chuck Lever | dd229ce | 2017-12-14 20:56:58 -0500 | [diff] [blame] | 367 | * @xprt: transport with IA to (re)initialize |
Chuck Lever | fff0959 | 2017-04-11 13:22:54 -0400 | [diff] [blame] | 368 | * |
| 369 | * Returns 0 on success, negative errno if an appropriate |
| 370 | * Interface Adapter could not be found and opened. |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 371 | */ |
| 372 | int |
Chuck Lever | dd229ce | 2017-12-14 20:56:58 -0500 | [diff] [blame] | 373 | rpcrdma_ia_open(struct rpcrdma_xprt *xprt) |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 374 | { |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 375 | struct rpcrdma_ia *ia = &xprt->rx_ia; |
Chuck Lever | d1ed857 | 2015-08-03 13:03:30 -0400 | [diff] [blame] | 376 | int rc; |
| 377 | |
Chuck Lever | dd229ce | 2017-12-14 20:56:58 -0500 | [diff] [blame] | 378 | ia->ri_id = rpcrdma_create_id(xprt, ia); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 379 | if (IS_ERR(ia->ri_id)) { |
| 380 | rc = PTR_ERR(ia->ri_id); |
Chuck Lever | fff0959 | 2017-04-11 13:22:54 -0400 | [diff] [blame] | 381 | goto out_err; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 382 | } |
Chuck Lever | 89e0d112 | 2015-05-26 11:51:56 -0400 | [diff] [blame] | 383 | ia->ri_device = ia->ri_id->device; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 384 | |
Christoph Hellwig | ed082d3 | 2016-09-05 12:56:17 +0200 | [diff] [blame] | 385 | ia->ri_pd = ib_alloc_pd(ia->ri_device, 0); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 386 | if (IS_ERR(ia->ri_pd)) { |
| 387 | rc = PTR_ERR(ia->ri_pd); |
Chuck Lever | b54054c | 2016-06-29 13:53:27 -0400 | [diff] [blame] | 388 | pr_err("rpcrdma: ib_alloc_pd() returned %d\n", rc); |
Chuck Lever | fff0959 | 2017-04-11 13:22:54 -0400 | [diff] [blame] | 389 | goto out_err; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 390 | } |
| 391 | |
Chuck Lever | fff0959 | 2017-04-11 13:22:54 -0400 | [diff] [blame] | 392 | switch (xprt_rdma_memreg_strategy) { |
Chuck Lever | ce5b371 | 2017-12-14 20:57:47 -0500 | [diff] [blame] | 393 | case RPCRDMA_FRWR: |
Chuck Lever | b54054c | 2016-06-29 13:53:27 -0400 | [diff] [blame] | 394 | if (frwr_is_supported(ia)) { |
| 395 | ia->ri_ops = &rpcrdma_frwr_memreg_ops; |
| 396 | break; |
| 397 | } |
| 398 | /*FALLTHROUGH*/ |
Tom Talpey | bd7ed1d | 2008-10-09 15:00:09 -0400 | [diff] [blame] | 399 | case RPCRDMA_MTHCAFMR: |
Chuck Lever | b54054c | 2016-06-29 13:53:27 -0400 | [diff] [blame] | 400 | if (fmr_is_supported(ia)) { |
| 401 | ia->ri_ops = &rpcrdma_fmr_memreg_ops; |
| 402 | break; |
| 403 | } |
| 404 | /*FALLTHROUGH*/ |
Tom Talpey | bd7ed1d | 2008-10-09 15:00:09 -0400 | [diff] [blame] | 405 | default: |
Chuck Lever | fff0959 | 2017-04-11 13:22:54 -0400 | [diff] [blame] | 406 | pr_err("rpcrdma: Device %s does not support memreg mode %d\n", |
| 407 | ia->ri_device->name, xprt_rdma_memreg_strategy); |
Chuck Lever | b54054c | 2016-06-29 13:53:27 -0400 | [diff] [blame] | 408 | rc = -EINVAL; |
Chuck Lever | fff0959 | 2017-04-11 13:22:54 -0400 | [diff] [blame] | 409 | goto out_err; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 410 | } |
| 411 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 412 | return 0; |
Chuck Lever | 5ae711a | 2015-01-21 11:03:19 -0500 | [diff] [blame] | 413 | |
Chuck Lever | fff0959 | 2017-04-11 13:22:54 -0400 | [diff] [blame] | 414 | out_err: |
| 415 | rpcrdma_ia_close(ia); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 416 | return rc; |
| 417 | } |
| 418 | |
Chuck Lever | fff0959 | 2017-04-11 13:22:54 -0400 | [diff] [blame] | 419 | /** |
Chuck Lever | bebd031 | 2017-04-11 13:23:10 -0400 | [diff] [blame] | 420 | * rpcrdma_ia_remove - Handle device driver unload |
| 421 | * @ia: interface adapter being removed |
| 422 | * |
| 423 | * Divest transport H/W resources associated with this adapter, |
| 424 | * but allow it to be restored later. |
| 425 | */ |
| 426 | void |
| 427 | rpcrdma_ia_remove(struct rpcrdma_ia *ia) |
| 428 | { |
| 429 | struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt, |
| 430 | rx_ia); |
| 431 | struct rpcrdma_ep *ep = &r_xprt->rx_ep; |
| 432 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; |
| 433 | struct rpcrdma_req *req; |
| 434 | struct rpcrdma_rep *rep; |
| 435 | |
| 436 | cancel_delayed_work_sync(&buf->rb_refresh_worker); |
| 437 | |
| 438 | /* This is similar to rpcrdma_ep_destroy, but: |
| 439 | * - Don't cancel the connect worker. |
| 440 | * - Don't call rpcrdma_ep_disconnect, which waits |
| 441 | * for another conn upcall, which will deadlock. |
| 442 | * - rdma_disconnect is unneeded, the underlying |
| 443 | * connection is already gone. |
| 444 | */ |
| 445 | if (ia->ri_id->qp) { |
| 446 | ib_drain_qp(ia->ri_id->qp); |
| 447 | rdma_destroy_qp(ia->ri_id); |
| 448 | ia->ri_id->qp = NULL; |
| 449 | } |
| 450 | ib_free_cq(ep->rep_attr.recv_cq); |
Chuck Lever | 2552428 | 2018-03-19 14:23:16 -0400 | [diff] [blame] | 451 | ep->rep_attr.recv_cq = NULL; |
Chuck Lever | bebd031 | 2017-04-11 13:23:10 -0400 | [diff] [blame] | 452 | ib_free_cq(ep->rep_attr.send_cq); |
Chuck Lever | 2552428 | 2018-03-19 14:23:16 -0400 | [diff] [blame] | 453 | ep->rep_attr.send_cq = NULL; |
Chuck Lever | bebd031 | 2017-04-11 13:23:10 -0400 | [diff] [blame] | 454 | |
| 455 | /* The ULP is responsible for ensuring all DMA |
| 456 | * mappings and MRs are gone. |
| 457 | */ |
| 458 | list_for_each_entry(rep, &buf->rb_recv_bufs, rr_list) |
| 459 | rpcrdma_dma_unmap_regbuf(rep->rr_rdmabuf); |
| 460 | list_for_each_entry(req, &buf->rb_allreqs, rl_all) { |
| 461 | rpcrdma_dma_unmap_regbuf(req->rl_rdmabuf); |
| 462 | rpcrdma_dma_unmap_regbuf(req->rl_sendbuf); |
| 463 | rpcrdma_dma_unmap_regbuf(req->rl_recvbuf); |
| 464 | } |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 465 | rpcrdma_mrs_destroy(buf); |
Chuck Lever | 2552428 | 2018-03-19 14:23:16 -0400 | [diff] [blame] | 466 | ib_dealloc_pd(ia->ri_pd); |
| 467 | ia->ri_pd = NULL; |
Chuck Lever | bebd031 | 2017-04-11 13:23:10 -0400 | [diff] [blame] | 468 | |
| 469 | /* Allow waiters to continue */ |
| 470 | complete(&ia->ri_remove_done); |
Chuck Lever | b4744e0 | 2017-12-20 16:31:29 -0500 | [diff] [blame] | 471 | |
| 472 | trace_xprtrdma_remove(r_xprt); |
Chuck Lever | bebd031 | 2017-04-11 13:23:10 -0400 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /** |
Chuck Lever | fff0959 | 2017-04-11 13:22:54 -0400 | [diff] [blame] | 476 | * rpcrdma_ia_close - Clean up/close an IA. |
| 477 | * @ia: interface adapter to close |
| 478 | * |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 479 | */ |
| 480 | void |
| 481 | rpcrdma_ia_close(struct rpcrdma_ia *ia) |
| 482 | { |
Tom Talpey | fee08ca | 2008-10-09 15:01:00 -0400 | [diff] [blame] | 483 | if (ia->ri_id != NULL && !IS_ERR(ia->ri_id)) { |
| 484 | if (ia->ri_id->qp) |
| 485 | rdma_destroy_qp(ia->ri_id); |
Chuck Lever | 56a6bd1 | 2017-04-11 13:23:34 -0400 | [diff] [blame] | 486 | rdma_destroy_id(ia->ri_id); |
Tom Talpey | fee08ca | 2008-10-09 15:01:00 -0400 | [diff] [blame] | 487 | } |
Chuck Lever | fff0959 | 2017-04-11 13:22:54 -0400 | [diff] [blame] | 488 | ia->ri_id = NULL; |
| 489 | ia->ri_device = NULL; |
Chuck Lever | 6d44698 | 2015-05-26 11:51:27 -0400 | [diff] [blame] | 490 | |
| 491 | /* If the pd is still busy, xprtrdma missed freeing a resource */ |
| 492 | if (ia->ri_pd && !IS_ERR(ia->ri_pd)) |
Jason Gunthorpe | 7dd7864 | 2015-08-05 14:34:31 -0600 | [diff] [blame] | 493 | ib_dealloc_pd(ia->ri_pd); |
Chuck Lever | fff0959 | 2017-04-11 13:22:54 -0400 | [diff] [blame] | 494 | ia->ri_pd = NULL; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /* |
| 498 | * Create unconnected endpoint. |
| 499 | */ |
| 500 | int |
| 501 | rpcrdma_ep_create(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia, |
Chuck Lever | 16f906d | 2017-02-08 17:00:10 -0500 | [diff] [blame] | 502 | struct rpcrdma_create_data_internal *cdata) |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 503 | { |
Chuck Lever | 87cfb9a | 2016-09-15 10:57:07 -0400 | [diff] [blame] | 504 | struct rpcrdma_connect_private *pmsg = &ep->rep_cm_private; |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 505 | struct ib_cq *sendcq, *recvcq; |
Chuck Lever | 914fcad | 2018-05-04 15:34:48 -0400 | [diff] [blame] | 506 | unsigned int max_sge; |
Chuck Lever | 2fa8f88 | 2016-03-04 11:28:53 -0500 | [diff] [blame] | 507 | int rc; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 508 | |
Chuck Lever | eed5087 | 2017-03-11 15:52:47 -0500 | [diff] [blame] | 509 | max_sge = min_t(unsigned int, ia->ri_device->attrs.max_sge, |
| 510 | RPCRDMA_MAX_SEND_SGES); |
Chuck Lever | 16f906d | 2017-02-08 17:00:10 -0500 | [diff] [blame] | 511 | if (max_sge < RPCRDMA_MIN_SEND_SGES) { |
| 512 | pr_warn("rpcrdma: HCA provides only %d send SGEs\n", max_sge); |
Chuck Lever | b3221d6 | 2015-08-03 13:03:39 -0400 | [diff] [blame] | 513 | return -ENOMEM; |
| 514 | } |
Chuck Lever | 1179e2c | 2018-01-31 12:34:05 -0500 | [diff] [blame] | 515 | ia->ri_max_send_sges = max_sge; |
Chuck Lever | b3221d6 | 2015-08-03 13:03:39 -0400 | [diff] [blame] | 516 | |
Chuck Lever | 914fcad | 2018-05-04 15:34:48 -0400 | [diff] [blame] | 517 | rc = ia->ri_ops->ro_open(ia, ep, cdata); |
| 518 | if (rc) |
| 519 | return rc; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 520 | |
| 521 | ep->rep_attr.event_handler = rpcrdma_qp_async_error_upcall; |
| 522 | ep->rep_attr.qp_context = ep; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 523 | ep->rep_attr.srq = NULL; |
Chuck Lever | 16f906d | 2017-02-08 17:00:10 -0500 | [diff] [blame] | 524 | ep->rep_attr.cap.max_send_sge = max_sge; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 525 | ep->rep_attr.cap.max_recv_sge = 1; |
| 526 | ep->rep_attr.cap.max_inline_data = 0; |
| 527 | ep->rep_attr.sq_sig_type = IB_SIGNAL_REQ_WR; |
| 528 | ep->rep_attr.qp_type = IB_QPT_RC; |
| 529 | ep->rep_attr.port_num = ~0; |
| 530 | |
| 531 | dprintk("RPC: %s: requested max: dtos: send %d recv %d; " |
| 532 | "iovs: send %d recv %d\n", |
| 533 | __func__, |
| 534 | ep->rep_attr.cap.max_send_wr, |
| 535 | ep->rep_attr.cap.max_recv_wr, |
| 536 | ep->rep_attr.cap.max_send_sge, |
| 537 | ep->rep_attr.cap.max_recv_sge); |
| 538 | |
| 539 | /* set trigger for requesting send completion */ |
Chuck Lever | ae72950 | 2017-10-20 10:48:12 -0400 | [diff] [blame] | 540 | ep->rep_send_batch = min_t(unsigned int, RPCRDMA_MAX_SEND_BATCH, |
| 541 | cdata->max_requests >> 2); |
| 542 | ep->rep_send_count = ep->rep_send_batch; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 543 | init_waitqueue_head(&ep->rep_connect_wait); |
Chuck Lever | 254f91e | 2014-05-28 10:32:17 -0400 | [diff] [blame] | 544 | INIT_DELAYED_WORK(&ep->rep_connect_worker, rpcrdma_connect_worker); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 545 | |
Chuck Lever | 2fa8f88 | 2016-03-04 11:28:53 -0500 | [diff] [blame] | 546 | sendcq = ib_alloc_cq(ia->ri_device, NULL, |
| 547 | ep->rep_attr.cap.max_send_wr + 1, |
Chuck Lever | a4699f5 | 2017-10-30 16:21:49 -0400 | [diff] [blame] | 548 | 1, IB_POLL_WORKQUEUE); |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 549 | if (IS_ERR(sendcq)) { |
| 550 | rc = PTR_ERR(sendcq); |
| 551 | dprintk("RPC: %s: failed to create send CQ: %i\n", |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 552 | __func__, rc); |
| 553 | goto out1; |
| 554 | } |
| 555 | |
Chuck Lever | 552bf22 | 2016-03-04 11:28:36 -0500 | [diff] [blame] | 556 | recvcq = ib_alloc_cq(ia->ri_device, NULL, |
| 557 | ep->rep_attr.cap.max_recv_wr + 1, |
Chuck Lever | d8f532d | 2017-10-16 15:01:30 -0400 | [diff] [blame] | 558 | 0, IB_POLL_WORKQUEUE); |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 559 | if (IS_ERR(recvcq)) { |
| 560 | rc = PTR_ERR(recvcq); |
| 561 | dprintk("RPC: %s: failed to create recv CQ: %i\n", |
| 562 | __func__, rc); |
| 563 | goto out2; |
| 564 | } |
| 565 | |
Chuck Lever | fc66448 | 2014-05-28 10:33:25 -0400 | [diff] [blame] | 566 | ep->rep_attr.send_cq = sendcq; |
| 567 | ep->rep_attr.recv_cq = recvcq; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 568 | |
| 569 | /* Initialize cma parameters */ |
Chuck Lever | b2dde94 | 2016-05-02 14:43:03 -0400 | [diff] [blame] | 570 | memset(&ep->rep_remote_cma, 0, sizeof(ep->rep_remote_cma)); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 571 | |
Chuck Lever | 87cfb9a | 2016-09-15 10:57:07 -0400 | [diff] [blame] | 572 | /* Prepare RDMA-CM private message */ |
| 573 | pmsg->cp_magic = rpcrdma_cmp_magic; |
| 574 | pmsg->cp_version = RPCRDMA_CMP_VERSION; |
Chuck Lever | c8b920b | 2016-09-15 10:57:16 -0400 | [diff] [blame] | 575 | pmsg->cp_flags |= ia->ri_ops->ro_send_w_inv_ok; |
Chuck Lever | 87cfb9a | 2016-09-15 10:57:07 -0400 | [diff] [blame] | 576 | pmsg->cp_send_size = rpcrdma_encode_buffer_size(cdata->inline_wsize); |
| 577 | pmsg->cp_recv_size = rpcrdma_encode_buffer_size(cdata->inline_rsize); |
| 578 | ep->rep_remote_cma.private_data = pmsg; |
| 579 | ep->rep_remote_cma.private_data_len = sizeof(*pmsg); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 580 | |
| 581 | /* Client offers RDMA Read but does not initiate */ |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 582 | ep->rep_remote_cma.initiator_depth = 0; |
Chuck Lever | b7e85fff | 2018-02-28 15:30:33 -0500 | [diff] [blame] | 583 | ep->rep_remote_cma.responder_resources = |
| 584 | min_t(int, U8_MAX, ia->ri_device->attrs.max_qp_rd_atom); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 585 | |
Chuck Lever | b2dde94 | 2016-05-02 14:43:03 -0400 | [diff] [blame] | 586 | /* Limit transport retries so client can detect server |
| 587 | * GID changes quickly. RPC layer handles re-establishing |
| 588 | * transport connection and retransmission. |
| 589 | */ |
| 590 | ep->rep_remote_cma.retry_count = 6; |
| 591 | |
| 592 | /* RPC-over-RDMA handles its own flow control. In addition, |
| 593 | * make all RNR NAKs visible so we know that RPC-over-RDMA |
| 594 | * flow control is working correctly (no NAKs should be seen). |
| 595 | */ |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 596 | ep->rep_remote_cma.flow_control = 0; |
| 597 | ep->rep_remote_cma.rnr_retry_count = 0; |
| 598 | |
| 599 | return 0; |
| 600 | |
| 601 | out2: |
Chuck Lever | 2fa8f88 | 2016-03-04 11:28:53 -0500 | [diff] [blame] | 602 | ib_free_cq(sendcq); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 603 | out1: |
| 604 | return rc; |
| 605 | } |
| 606 | |
| 607 | /* |
| 608 | * rpcrdma_ep_destroy |
| 609 | * |
| 610 | * Disconnect and destroy endpoint. After this, the only |
| 611 | * valid operations on the ep are to free it (if dynamically |
| 612 | * allocated) or re-create it. |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 613 | */ |
Chuck Lever | 7f1d541 | 2014-05-28 10:33:16 -0400 | [diff] [blame] | 614 | void |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 615 | rpcrdma_ep_destroy(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia) |
| 616 | { |
Chuck Lever | 254f91e | 2014-05-28 10:32:17 -0400 | [diff] [blame] | 617 | cancel_delayed_work_sync(&ep->rep_connect_worker); |
| 618 | |
Chuck Lever | 2552428 | 2018-03-19 14:23:16 -0400 | [diff] [blame] | 619 | if (ia->ri_id && ia->ri_id->qp) { |
Chuck Lever | 550d750 | 2016-05-02 14:41:47 -0400 | [diff] [blame] | 620 | rpcrdma_ep_disconnect(ep, ia); |
Tom Talpey | fee08ca | 2008-10-09 15:01:00 -0400 | [diff] [blame] | 621 | rdma_destroy_qp(ia->ri_id); |
| 622 | ia->ri_id->qp = NULL; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 623 | } |
| 624 | |
Chuck Lever | 2552428 | 2018-03-19 14:23:16 -0400 | [diff] [blame] | 625 | if (ep->rep_attr.recv_cq) |
| 626 | ib_free_cq(ep->rep_attr.recv_cq); |
| 627 | if (ep->rep_attr.send_cq) |
| 628 | ib_free_cq(ep->rep_attr.send_cq); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 629 | } |
| 630 | |
Chuck Lever | a9b0e38 | 2017-04-11 13:23:26 -0400 | [diff] [blame] | 631 | /* Re-establish a connection after a device removal event. |
| 632 | * Unlike a normal reconnection, a fresh PD and a new set |
| 633 | * of MRs and buffers is needed. |
| 634 | */ |
| 635 | static int |
| 636 | rpcrdma_ep_recreate_xprt(struct rpcrdma_xprt *r_xprt, |
| 637 | struct rpcrdma_ep *ep, struct rpcrdma_ia *ia) |
| 638 | { |
Chuck Lever | a9b0e38 | 2017-04-11 13:23:26 -0400 | [diff] [blame] | 639 | int rc, err; |
| 640 | |
Chuck Lever | b4744e0 | 2017-12-20 16:31:29 -0500 | [diff] [blame] | 641 | trace_xprtrdma_reinsert(r_xprt); |
Chuck Lever | a9b0e38 | 2017-04-11 13:23:26 -0400 | [diff] [blame] | 642 | |
| 643 | rc = -EHOSTUNREACH; |
Chuck Lever | dd229ce | 2017-12-14 20:56:58 -0500 | [diff] [blame] | 644 | if (rpcrdma_ia_open(r_xprt)) |
Chuck Lever | a9b0e38 | 2017-04-11 13:23:26 -0400 | [diff] [blame] | 645 | goto out1; |
| 646 | |
| 647 | rc = -ENOMEM; |
| 648 | err = rpcrdma_ep_create(ep, ia, &r_xprt->rx_data); |
| 649 | if (err) { |
| 650 | pr_err("rpcrdma: rpcrdma_ep_create returned %d\n", err); |
| 651 | goto out2; |
| 652 | } |
| 653 | |
| 654 | rc = -ENETUNREACH; |
| 655 | err = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr); |
| 656 | if (err) { |
| 657 | pr_err("rpcrdma: rdma_create_qp returned %d\n", err); |
| 658 | goto out3; |
| 659 | } |
| 660 | |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 661 | rpcrdma_mrs_create(r_xprt); |
Chuck Lever | a9b0e38 | 2017-04-11 13:23:26 -0400 | [diff] [blame] | 662 | return 0; |
| 663 | |
| 664 | out3: |
| 665 | rpcrdma_ep_destroy(ep, ia); |
| 666 | out2: |
| 667 | rpcrdma_ia_close(ia); |
| 668 | out1: |
| 669 | return rc; |
| 670 | } |
| 671 | |
Chuck Lever | 1890896 | 2017-04-11 13:23:18 -0400 | [diff] [blame] | 672 | static int |
| 673 | rpcrdma_ep_reconnect(struct rpcrdma_xprt *r_xprt, struct rpcrdma_ep *ep, |
| 674 | struct rpcrdma_ia *ia) |
| 675 | { |
Chuck Lever | 1890896 | 2017-04-11 13:23:18 -0400 | [diff] [blame] | 676 | struct rdma_cm_id *id, *old; |
| 677 | int err, rc; |
| 678 | |
Chuck Lever | b4744e0 | 2017-12-20 16:31:29 -0500 | [diff] [blame] | 679 | trace_xprtrdma_reconnect(r_xprt); |
Chuck Lever | 1890896 | 2017-04-11 13:23:18 -0400 | [diff] [blame] | 680 | |
| 681 | rpcrdma_ep_disconnect(ep, ia); |
| 682 | |
| 683 | rc = -EHOSTUNREACH; |
Chuck Lever | dd229ce | 2017-12-14 20:56:58 -0500 | [diff] [blame] | 684 | id = rpcrdma_create_id(r_xprt, ia); |
Chuck Lever | 1890896 | 2017-04-11 13:23:18 -0400 | [diff] [blame] | 685 | if (IS_ERR(id)) |
| 686 | goto out; |
| 687 | |
| 688 | /* As long as the new ID points to the same device as the |
| 689 | * old ID, we can reuse the transport's existing PD and all |
| 690 | * previously allocated MRs. Also, the same device means |
| 691 | * the transport's previous DMA mappings are still valid. |
| 692 | * |
| 693 | * This is a sanity check only. There should be no way these |
| 694 | * point to two different devices here. |
| 695 | */ |
| 696 | old = id; |
| 697 | rc = -ENETUNREACH; |
| 698 | if (ia->ri_device != id->device) { |
| 699 | pr_err("rpcrdma: can't reconnect on different device!\n"); |
| 700 | goto out_destroy; |
| 701 | } |
| 702 | |
| 703 | err = rdma_create_qp(id, ia->ri_pd, &ep->rep_attr); |
| 704 | if (err) { |
| 705 | dprintk("RPC: %s: rdma_create_qp returned %d\n", |
| 706 | __func__, err); |
| 707 | goto out_destroy; |
| 708 | } |
| 709 | |
| 710 | /* Atomically replace the transport's ID and QP. */ |
| 711 | rc = 0; |
| 712 | old = ia->ri_id; |
| 713 | ia->ri_id = id; |
| 714 | rdma_destroy_qp(old); |
| 715 | |
| 716 | out_destroy: |
Chuck Lever | 56a6bd1 | 2017-04-11 13:23:34 -0400 | [diff] [blame] | 717 | rdma_destroy_id(old); |
Chuck Lever | 1890896 | 2017-04-11 13:23:18 -0400 | [diff] [blame] | 718 | out: |
| 719 | return rc; |
| 720 | } |
| 721 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 722 | /* |
| 723 | * Connect unconnected endpoint. |
| 724 | */ |
| 725 | int |
| 726 | rpcrdma_ep_connect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia) |
| 727 | { |
Chuck Lever | 0a90487 | 2017-02-08 17:00:35 -0500 | [diff] [blame] | 728 | struct rpcrdma_xprt *r_xprt = container_of(ia, struct rpcrdma_xprt, |
| 729 | rx_ia); |
Chuck Lever | 1890896 | 2017-04-11 13:23:18 -0400 | [diff] [blame] | 730 | int rc; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 731 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 732 | retry: |
Chuck Lever | 1890896 | 2017-04-11 13:23:18 -0400 | [diff] [blame] | 733 | switch (ep->rep_connected) { |
| 734 | case 0: |
Chuck Lever | ec62f40 | 2014-05-28 10:34:07 -0400 | [diff] [blame] | 735 | dprintk("RPC: %s: connecting...\n", __func__); |
| 736 | rc = rdma_create_qp(ia->ri_id, ia->ri_pd, &ep->rep_attr); |
| 737 | if (rc) { |
| 738 | dprintk("RPC: %s: rdma_create_qp failed %i\n", |
| 739 | __func__, rc); |
Chuck Lever | 1890896 | 2017-04-11 13:23:18 -0400 | [diff] [blame] | 740 | rc = -ENETUNREACH; |
| 741 | goto out_noupdate; |
Chuck Lever | ec62f40 | 2014-05-28 10:34:07 -0400 | [diff] [blame] | 742 | } |
Chuck Lever | 1890896 | 2017-04-11 13:23:18 -0400 | [diff] [blame] | 743 | break; |
Chuck Lever | a9b0e38 | 2017-04-11 13:23:26 -0400 | [diff] [blame] | 744 | case -ENODEV: |
| 745 | rc = rpcrdma_ep_recreate_xprt(r_xprt, ep, ia); |
| 746 | if (rc) |
| 747 | goto out_noupdate; |
| 748 | break; |
Chuck Lever | 1890896 | 2017-04-11 13:23:18 -0400 | [diff] [blame] | 749 | default: |
| 750 | rc = rpcrdma_ep_reconnect(r_xprt, ep, ia); |
| 751 | if (rc) |
| 752 | goto out; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 753 | } |
| 754 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 755 | ep->rep_connected = 0; |
| 756 | |
| 757 | rc = rdma_connect(ia->ri_id, &ep->rep_remote_cma); |
| 758 | if (rc) { |
| 759 | dprintk("RPC: %s: rdma_connect() failed with %i\n", |
| 760 | __func__, rc); |
| 761 | goto out; |
| 762 | } |
| 763 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 764 | wait_event_interruptible(ep->rep_connect_wait, ep->rep_connected != 0); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 765 | if (ep->rep_connected <= 0) { |
Chuck Lever | 0a90487 | 2017-02-08 17:00:35 -0500 | [diff] [blame] | 766 | if (ep->rep_connected == -EAGAIN) |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 767 | goto retry; |
| 768 | rc = ep->rep_connected; |
Chuck Lever | 0a90487 | 2017-02-08 17:00:35 -0500 | [diff] [blame] | 769 | goto out; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 770 | } |
| 771 | |
Chuck Lever | 0a90487 | 2017-02-08 17:00:35 -0500 | [diff] [blame] | 772 | dprintk("RPC: %s: connected\n", __func__); |
Chuck Lever | 7c8d9e7 | 2018-05-04 15:35:20 -0400 | [diff] [blame] | 773 | |
| 774 | rpcrdma_post_recvs(r_xprt, true); |
Chuck Lever | 0a90487 | 2017-02-08 17:00:35 -0500 | [diff] [blame] | 775 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 776 | out: |
| 777 | if (rc) |
| 778 | ep->rep_connected = rc; |
Chuck Lever | 1890896 | 2017-04-11 13:23:18 -0400 | [diff] [blame] | 779 | |
| 780 | out_noupdate: |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 781 | return rc; |
| 782 | } |
| 783 | |
| 784 | /* |
| 785 | * rpcrdma_ep_disconnect |
| 786 | * |
| 787 | * This is separate from destroy to facilitate the ability |
| 788 | * to reconnect without recreating the endpoint. |
| 789 | * |
| 790 | * This call is not reentrant, and must not be made in parallel |
| 791 | * on the same endpoint. |
| 792 | */ |
Chuck Lever | 282191c | 2014-07-29 17:25:55 -0400 | [diff] [blame] | 793 | void |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 794 | rpcrdma_ep_disconnect(struct rpcrdma_ep *ep, struct rpcrdma_ia *ia) |
| 795 | { |
| 796 | int rc; |
| 797 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 798 | rc = rdma_disconnect(ia->ri_id); |
Chuck Lever | b4744e0 | 2017-12-20 16:31:29 -0500 | [diff] [blame] | 799 | if (!rc) |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 800 | /* returns without wait if not connected */ |
| 801 | wait_event_interruptible(ep->rep_connect_wait, |
| 802 | ep->rep_connected != 1); |
Chuck Lever | b4744e0 | 2017-12-20 16:31:29 -0500 | [diff] [blame] | 803 | else |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 804 | ep->rep_connected = rc; |
Chuck Lever | b4744e0 | 2017-12-20 16:31:29 -0500 | [diff] [blame] | 805 | trace_xprtrdma_disconnect(container_of(ep, struct rpcrdma_xprt, |
| 806 | rx_ep), rc); |
Chuck Lever | 550d750 | 2016-05-02 14:41:47 -0400 | [diff] [blame] | 807 | |
| 808 | ib_drain_qp(ia->ri_id->qp); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 809 | } |
| 810 | |
Chuck Lever | ae72950 | 2017-10-20 10:48:12 -0400 | [diff] [blame] | 811 | /* Fixed-size circular FIFO queue. This implementation is wait-free and |
| 812 | * lock-free. |
| 813 | * |
| 814 | * Consumer is the code path that posts Sends. This path dequeues a |
| 815 | * sendctx for use by a Send operation. Multiple consumer threads |
| 816 | * are serialized by the RPC transport lock, which allows only one |
| 817 | * ->send_request call at a time. |
| 818 | * |
| 819 | * Producer is the code path that handles Send completions. This path |
| 820 | * enqueues a sendctx that has been completed. Multiple producer |
| 821 | * threads are serialized by the ib_poll_cq() function. |
| 822 | */ |
| 823 | |
| 824 | /* rpcrdma_sendctxs_destroy() assumes caller has already quiesced |
| 825 | * queue activity, and ib_drain_qp has flushed all remaining Send |
| 826 | * requests. |
| 827 | */ |
| 828 | static void rpcrdma_sendctxs_destroy(struct rpcrdma_buffer *buf) |
| 829 | { |
| 830 | unsigned long i; |
| 831 | |
| 832 | for (i = 0; i <= buf->rb_sc_last; i++) |
| 833 | kfree(buf->rb_sc_ctxs[i]); |
| 834 | kfree(buf->rb_sc_ctxs); |
| 835 | } |
| 836 | |
| 837 | static struct rpcrdma_sendctx *rpcrdma_sendctx_create(struct rpcrdma_ia *ia) |
| 838 | { |
| 839 | struct rpcrdma_sendctx *sc; |
| 840 | |
| 841 | sc = kzalloc(sizeof(*sc) + |
| 842 | ia->ri_max_send_sges * sizeof(struct ib_sge), |
| 843 | GFP_KERNEL); |
| 844 | if (!sc) |
| 845 | return NULL; |
| 846 | |
| 847 | sc->sc_wr.wr_cqe = &sc->sc_cqe; |
| 848 | sc->sc_wr.sg_list = sc->sc_sges; |
| 849 | sc->sc_wr.opcode = IB_WR_SEND; |
| 850 | sc->sc_cqe.done = rpcrdma_wc_send; |
| 851 | return sc; |
| 852 | } |
| 853 | |
| 854 | static int rpcrdma_sendctxs_create(struct rpcrdma_xprt *r_xprt) |
| 855 | { |
| 856 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; |
| 857 | struct rpcrdma_sendctx *sc; |
| 858 | unsigned long i; |
| 859 | |
| 860 | /* Maximum number of concurrent outstanding Send WRs. Capping |
| 861 | * the circular queue size stops Send Queue overflow by causing |
| 862 | * the ->send_request call to fail temporarily before too many |
| 863 | * Sends are posted. |
| 864 | */ |
| 865 | i = buf->rb_max_requests + RPCRDMA_MAX_BC_REQUESTS; |
| 866 | dprintk("RPC: %s: allocating %lu send_ctxs\n", __func__, i); |
| 867 | buf->rb_sc_ctxs = kcalloc(i, sizeof(sc), GFP_KERNEL); |
| 868 | if (!buf->rb_sc_ctxs) |
| 869 | return -ENOMEM; |
| 870 | |
| 871 | buf->rb_sc_last = i - 1; |
| 872 | for (i = 0; i <= buf->rb_sc_last; i++) { |
| 873 | sc = rpcrdma_sendctx_create(&r_xprt->rx_ia); |
| 874 | if (!sc) |
| 875 | goto out_destroy; |
| 876 | |
| 877 | sc->sc_xprt = r_xprt; |
| 878 | buf->rb_sc_ctxs[i] = sc; |
| 879 | } |
| 880 | |
| 881 | return 0; |
| 882 | |
| 883 | out_destroy: |
| 884 | rpcrdma_sendctxs_destroy(buf); |
| 885 | return -ENOMEM; |
| 886 | } |
| 887 | |
| 888 | /* The sendctx queue is not guaranteed to have a size that is a |
| 889 | * power of two, thus the helpers in circ_buf.h cannot be used. |
| 890 | * The other option is to use modulus (%), which can be expensive. |
| 891 | */ |
| 892 | static unsigned long rpcrdma_sendctx_next(struct rpcrdma_buffer *buf, |
| 893 | unsigned long item) |
| 894 | { |
| 895 | return likely(item < buf->rb_sc_last) ? item + 1 : 0; |
| 896 | } |
| 897 | |
| 898 | /** |
| 899 | * rpcrdma_sendctx_get_locked - Acquire a send context |
| 900 | * @buf: transport buffers from which to acquire an unused context |
| 901 | * |
| 902 | * Returns pointer to a free send completion context; or NULL if |
| 903 | * the queue is empty. |
| 904 | * |
| 905 | * Usage: Called to acquire an SGE array before preparing a Send WR. |
| 906 | * |
| 907 | * The caller serializes calls to this function (per rpcrdma_buffer), |
| 908 | * and provides an effective memory barrier that flushes the new value |
| 909 | * of rb_sc_head. |
| 910 | */ |
| 911 | struct rpcrdma_sendctx *rpcrdma_sendctx_get_locked(struct rpcrdma_buffer *buf) |
| 912 | { |
| 913 | struct rpcrdma_xprt *r_xprt; |
| 914 | struct rpcrdma_sendctx *sc; |
| 915 | unsigned long next_head; |
| 916 | |
| 917 | next_head = rpcrdma_sendctx_next(buf, buf->rb_sc_head); |
| 918 | |
| 919 | if (next_head == READ_ONCE(buf->rb_sc_tail)) |
| 920 | goto out_emptyq; |
| 921 | |
| 922 | /* ORDER: item must be accessed _before_ head is updated */ |
| 923 | sc = buf->rb_sc_ctxs[next_head]; |
| 924 | |
| 925 | /* Releasing the lock in the caller acts as a memory |
| 926 | * barrier that flushes rb_sc_head. |
| 927 | */ |
| 928 | buf->rb_sc_head = next_head; |
| 929 | |
| 930 | return sc; |
| 931 | |
| 932 | out_emptyq: |
| 933 | /* The queue is "empty" if there have not been enough Send |
| 934 | * completions recently. This is a sign the Send Queue is |
| 935 | * backing up. Cause the caller to pause and try again. |
| 936 | */ |
| 937 | dprintk("RPC: %s: empty sendctx queue\n", __func__); |
| 938 | r_xprt = container_of(buf, struct rpcrdma_xprt, rx_buf); |
| 939 | r_xprt->rx_stats.empty_sendctx_q++; |
| 940 | return NULL; |
| 941 | } |
| 942 | |
| 943 | /** |
| 944 | * rpcrdma_sendctx_put_locked - Release a send context |
| 945 | * @sc: send context to release |
| 946 | * |
| 947 | * Usage: Called from Send completion to return a sendctxt |
| 948 | * to the queue. |
| 949 | * |
| 950 | * The caller serializes calls to this function (per rpcrdma_buffer). |
| 951 | */ |
| 952 | void rpcrdma_sendctx_put_locked(struct rpcrdma_sendctx *sc) |
| 953 | { |
| 954 | struct rpcrdma_buffer *buf = &sc->sc_xprt->rx_buf; |
| 955 | unsigned long next_tail; |
| 956 | |
| 957 | /* Unmap SGEs of previously completed by unsignaled |
| 958 | * Sends by walking up the queue until @sc is found. |
| 959 | */ |
| 960 | next_tail = buf->rb_sc_tail; |
| 961 | do { |
| 962 | next_tail = rpcrdma_sendctx_next(buf, next_tail); |
| 963 | |
| 964 | /* ORDER: item must be accessed _before_ tail is updated */ |
| 965 | rpcrdma_unmap_sendctx(buf->rb_sc_ctxs[next_tail]); |
| 966 | |
| 967 | } while (buf->rb_sc_ctxs[next_tail] != sc); |
| 968 | |
| 969 | /* Paired with READ_ONCE */ |
| 970 | smp_store_release(&buf->rb_sc_tail, next_tail); |
| 971 | } |
| 972 | |
Chuck Lever | 505bbe6 | 2016-06-29 13:52:54 -0400 | [diff] [blame] | 973 | static void |
| 974 | rpcrdma_mr_recovery_worker(struct work_struct *work) |
| 975 | { |
| 976 | struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer, |
| 977 | rb_recovery_worker.work); |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 978 | struct rpcrdma_mr *mr; |
Chuck Lever | 505bbe6 | 2016-06-29 13:52:54 -0400 | [diff] [blame] | 979 | |
| 980 | spin_lock(&buf->rb_recovery_lock); |
| 981 | while (!list_empty(&buf->rb_stale_mrs)) { |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 982 | mr = rpcrdma_mr_pop(&buf->rb_stale_mrs); |
Chuck Lever | 505bbe6 | 2016-06-29 13:52:54 -0400 | [diff] [blame] | 983 | spin_unlock(&buf->rb_recovery_lock); |
| 984 | |
Chuck Lever | 1c443eff | 2017-12-20 16:31:21 -0500 | [diff] [blame] | 985 | trace_xprtrdma_recover_mr(mr); |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 986 | mr->mr_xprt->rx_ia.ri_ops->ro_recover_mr(mr); |
Chuck Lever | 505bbe6 | 2016-06-29 13:52:54 -0400 | [diff] [blame] | 987 | |
| 988 | spin_lock(&buf->rb_recovery_lock); |
kbuild test robot | 53d7852 | 2016-07-16 06:02:05 +0800 | [diff] [blame] | 989 | } |
Chuck Lever | 505bbe6 | 2016-06-29 13:52:54 -0400 | [diff] [blame] | 990 | spin_unlock(&buf->rb_recovery_lock); |
| 991 | } |
| 992 | |
| 993 | void |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 994 | rpcrdma_mr_defer_recovery(struct rpcrdma_mr *mr) |
Chuck Lever | 505bbe6 | 2016-06-29 13:52:54 -0400 | [diff] [blame] | 995 | { |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 996 | struct rpcrdma_xprt *r_xprt = mr->mr_xprt; |
Chuck Lever | 505bbe6 | 2016-06-29 13:52:54 -0400 | [diff] [blame] | 997 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; |
| 998 | |
| 999 | spin_lock(&buf->rb_recovery_lock); |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1000 | rpcrdma_mr_push(mr, &buf->rb_stale_mrs); |
Chuck Lever | 505bbe6 | 2016-06-29 13:52:54 -0400 | [diff] [blame] | 1001 | spin_unlock(&buf->rb_recovery_lock); |
| 1002 | |
| 1003 | schedule_delayed_work(&buf->rb_recovery_worker, 0); |
| 1004 | } |
| 1005 | |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 1006 | static void |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1007 | rpcrdma_mrs_create(struct rpcrdma_xprt *r_xprt) |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 1008 | { |
| 1009 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; |
| 1010 | struct rpcrdma_ia *ia = &r_xprt->rx_ia; |
| 1011 | unsigned int count; |
| 1012 | LIST_HEAD(free); |
| 1013 | LIST_HEAD(all); |
| 1014 | |
Chuck Lever | ae741a8 | 2018-02-28 15:30:49 -0500 | [diff] [blame] | 1015 | for (count = 0; count < 3; count++) { |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1016 | struct rpcrdma_mr *mr; |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 1017 | int rc; |
| 1018 | |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1019 | mr = kzalloc(sizeof(*mr), GFP_KERNEL); |
| 1020 | if (!mr) |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 1021 | break; |
| 1022 | |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1023 | rc = ia->ri_ops->ro_init_mr(ia, mr); |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 1024 | if (rc) { |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1025 | kfree(mr); |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 1026 | break; |
| 1027 | } |
| 1028 | |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1029 | mr->mr_xprt = r_xprt; |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 1030 | |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1031 | list_add(&mr->mr_list, &free); |
| 1032 | list_add(&mr->mr_all, &all); |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 1033 | } |
| 1034 | |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1035 | spin_lock(&buf->rb_mrlock); |
| 1036 | list_splice(&free, &buf->rb_mrs); |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 1037 | list_splice(&all, &buf->rb_all); |
| 1038 | r_xprt->rx_stats.mrs_allocated += count; |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1039 | spin_unlock(&buf->rb_mrlock); |
Chuck Lever | 1c443eff | 2017-12-20 16:31:21 -0500 | [diff] [blame] | 1040 | trace_xprtrdma_createmrs(r_xprt, count); |
Chuck Lever | 9e679d5 | 2018-02-28 15:30:44 -0500 | [diff] [blame] | 1041 | |
| 1042 | xprt_write_space(&r_xprt->rx_xprt); |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | static void |
| 1046 | rpcrdma_mr_refresh_worker(struct work_struct *work) |
| 1047 | { |
| 1048 | struct rpcrdma_buffer *buf = container_of(work, struct rpcrdma_buffer, |
| 1049 | rb_refresh_worker.work); |
| 1050 | struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt, |
| 1051 | rx_buf); |
| 1052 | |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1053 | rpcrdma_mrs_create(r_xprt); |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 1054 | } |
| 1055 | |
Chuck Lever | f531a5d | 2015-10-24 17:27:43 -0400 | [diff] [blame] | 1056 | struct rpcrdma_req * |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1057 | rpcrdma_create_req(struct rpcrdma_xprt *r_xprt) |
| 1058 | { |
Chuck Lever | f531a5d | 2015-10-24 17:27:43 -0400 | [diff] [blame] | 1059 | struct rpcrdma_buffer *buffer = &r_xprt->rx_buf; |
Chuck Lever | 2dd4a01 | 2018-02-28 15:31:05 -0500 | [diff] [blame] | 1060 | struct rpcrdma_regbuf *rb; |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1061 | struct rpcrdma_req *req; |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1062 | |
Chuck Lever | 85275c8 | 2015-01-21 11:04:16 -0500 | [diff] [blame] | 1063 | req = kzalloc(sizeof(*req), GFP_KERNEL); |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1064 | if (req == NULL) |
Chuck Lever | 85275c8 | 2015-01-21 11:04:16 -0500 | [diff] [blame] | 1065 | return ERR_PTR(-ENOMEM); |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1066 | |
Chuck Lever | 2dd4a01 | 2018-02-28 15:31:05 -0500 | [diff] [blame] | 1067 | rb = rpcrdma_alloc_regbuf(RPCRDMA_HDRBUF_SIZE, |
| 1068 | DMA_TO_DEVICE, GFP_KERNEL); |
| 1069 | if (IS_ERR(rb)) { |
| 1070 | kfree(req); |
| 1071 | return ERR_PTR(-ENOMEM); |
| 1072 | } |
| 1073 | req->rl_rdmabuf = rb; |
| 1074 | xdr_buf_init(&req->rl_hdrbuf, rb->rg_base, rdmab_length(rb)); |
| 1075 | req->rl_buffer = buffer; |
| 1076 | INIT_LIST_HEAD(&req->rl_registered); |
| 1077 | |
Chuck Lever | f531a5d | 2015-10-24 17:27:43 -0400 | [diff] [blame] | 1078 | spin_lock(&buffer->rb_reqslock); |
| 1079 | list_add(&req->rl_all, &buffer->rb_allreqs); |
| 1080 | spin_unlock(&buffer->rb_reqslock); |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1081 | return req; |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1082 | } |
| 1083 | |
Chuck Lever | 7c8d9e7 | 2018-05-04 15:35:20 -0400 | [diff] [blame] | 1084 | static int |
| 1085 | rpcrdma_create_rep(struct rpcrdma_xprt *r_xprt, bool temp) |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1086 | { |
| 1087 | struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data; |
Chuck Lever | d698c4a | 2017-12-14 20:56:09 -0500 | [diff] [blame] | 1088 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1089 | struct rpcrdma_rep *rep; |
| 1090 | int rc; |
| 1091 | |
| 1092 | rc = -ENOMEM; |
Chuck Lever | 6b1184c | 2015-01-21 11:04:25 -0500 | [diff] [blame] | 1093 | rep = kzalloc(sizeof(*rep), GFP_KERNEL); |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1094 | if (rep == NULL) |
| 1095 | goto out; |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1096 | |
Chuck Lever | 13650c2 | 2016-09-15 10:56:26 -0400 | [diff] [blame] | 1097 | rep->rr_rdmabuf = rpcrdma_alloc_regbuf(cdata->inline_rsize, |
Chuck Lever | 99ef4db | 2016-09-15 10:56:10 -0400 | [diff] [blame] | 1098 | DMA_FROM_DEVICE, GFP_KERNEL); |
Chuck Lever | 6b1184c | 2015-01-21 11:04:25 -0500 | [diff] [blame] | 1099 | if (IS_ERR(rep->rr_rdmabuf)) { |
| 1100 | rc = PTR_ERR(rep->rr_rdmabuf); |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1101 | goto out_free; |
Chuck Lever | 6b1184c | 2015-01-21 11:04:25 -0500 | [diff] [blame] | 1102 | } |
Chuck Lever | 96f8778 | 2017-08-03 14:30:03 -0400 | [diff] [blame] | 1103 | xdr_buf_init(&rep->rr_hdrbuf, rep->rr_rdmabuf->rg_base, |
| 1104 | rdmab_length(rep->rr_rdmabuf)); |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1105 | |
Chuck Lever | 1519e96 | 2016-09-15 10:57:49 -0400 | [diff] [blame] | 1106 | rep->rr_cqe.done = rpcrdma_wc_receive; |
Chuck Lever | fed171b | 2015-05-26 11:51:37 -0400 | [diff] [blame] | 1107 | rep->rr_rxprt = r_xprt; |
Chuck Lever | d8f532d | 2017-10-16 15:01:30 -0400 | [diff] [blame] | 1108 | INIT_WORK(&rep->rr_work, rpcrdma_deferred_completion); |
Chuck Lever | 6ea8e71 | 2016-09-15 10:56:51 -0400 | [diff] [blame] | 1109 | rep->rr_recv_wr.next = NULL; |
| 1110 | rep->rr_recv_wr.wr_cqe = &rep->rr_cqe; |
| 1111 | rep->rr_recv_wr.sg_list = &rep->rr_rdmabuf->rg_iov; |
| 1112 | rep->rr_recv_wr.num_sge = 1; |
Chuck Lever | 7c8d9e7 | 2018-05-04 15:35:20 -0400 | [diff] [blame] | 1113 | rep->rr_temp = temp; |
Chuck Lever | d698c4a | 2017-12-14 20:56:09 -0500 | [diff] [blame] | 1114 | |
| 1115 | spin_lock(&buf->rb_lock); |
| 1116 | list_add(&rep->rr_list, &buf->rb_recv_bufs); |
| 1117 | spin_unlock(&buf->rb_lock); |
| 1118 | return 0; |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1119 | |
| 1120 | out_free: |
| 1121 | kfree(rep); |
| 1122 | out: |
Chuck Lever | d698c4a | 2017-12-14 20:56:09 -0500 | [diff] [blame] | 1123 | dprintk("RPC: %s: reply buffer %d alloc failed\n", |
| 1124 | __func__, rc); |
| 1125 | return rc; |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1126 | } |
| 1127 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1128 | int |
Chuck Lever | ac920d0 | 2015-01-21 11:03:44 -0500 | [diff] [blame] | 1129 | rpcrdma_buffer_create(struct rpcrdma_xprt *r_xprt) |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1130 | { |
Chuck Lever | ac920d0 | 2015-01-21 11:03:44 -0500 | [diff] [blame] | 1131 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1132 | int i, rc; |
| 1133 | |
Chuck Lever | 1e465fd | 2015-10-24 17:27:02 -0400 | [diff] [blame] | 1134 | buf->rb_max_requests = r_xprt->rx_data.max_requests; |
Chuck Lever | f531a5d | 2015-10-24 17:27:43 -0400 | [diff] [blame] | 1135 | buf->rb_bc_srv_max_requests = 0; |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1136 | spin_lock_init(&buf->rb_mrlock); |
Chuck Lever | 505bbe6 | 2016-06-29 13:52:54 -0400 | [diff] [blame] | 1137 | spin_lock_init(&buf->rb_lock); |
| 1138 | spin_lock_init(&buf->rb_recovery_lock); |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1139 | INIT_LIST_HEAD(&buf->rb_mrs); |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 1140 | INIT_LIST_HEAD(&buf->rb_all); |
Chuck Lever | 505bbe6 | 2016-06-29 13:52:54 -0400 | [diff] [blame] | 1141 | INIT_LIST_HEAD(&buf->rb_stale_mrs); |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 1142 | INIT_DELAYED_WORK(&buf->rb_refresh_worker, |
| 1143 | rpcrdma_mr_refresh_worker); |
Chuck Lever | 505bbe6 | 2016-06-29 13:52:54 -0400 | [diff] [blame] | 1144 | INIT_DELAYED_WORK(&buf->rb_recovery_worker, |
| 1145 | rpcrdma_mr_recovery_worker); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1146 | |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1147 | rpcrdma_mrs_create(r_xprt); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1148 | |
Chuck Lever | 1e465fd | 2015-10-24 17:27:02 -0400 | [diff] [blame] | 1149 | INIT_LIST_HEAD(&buf->rb_send_bufs); |
Chuck Lever | f531a5d | 2015-10-24 17:27:43 -0400 | [diff] [blame] | 1150 | INIT_LIST_HEAD(&buf->rb_allreqs); |
| 1151 | spin_lock_init(&buf->rb_reqslock); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1152 | for (i = 0; i < buf->rb_max_requests; i++) { |
| 1153 | struct rpcrdma_req *req; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1154 | |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1155 | req = rpcrdma_create_req(r_xprt); |
| 1156 | if (IS_ERR(req)) { |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1157 | dprintk("RPC: %s: request buffer %d alloc" |
| 1158 | " failed\n", __func__, i); |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1159 | rc = PTR_ERR(req); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1160 | goto out; |
| 1161 | } |
Chuck Lever | a80d66c | 2017-06-08 11:52:12 -0400 | [diff] [blame] | 1162 | list_add(&req->rl_list, &buf->rb_send_bufs); |
Chuck Lever | 1e465fd | 2015-10-24 17:27:02 -0400 | [diff] [blame] | 1163 | } |
| 1164 | |
Chuck Lever | 7c8d9e7 | 2018-05-04 15:35:20 -0400 | [diff] [blame] | 1165 | buf->rb_posted_receives = 0; |
Chuck Lever | 1e465fd | 2015-10-24 17:27:02 -0400 | [diff] [blame] | 1166 | INIT_LIST_HEAD(&buf->rb_recv_bufs); |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1167 | |
Chuck Lever | ae72950 | 2017-10-20 10:48:12 -0400 | [diff] [blame] | 1168 | rc = rpcrdma_sendctxs_create(r_xprt); |
| 1169 | if (rc) |
| 1170 | goto out; |
| 1171 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1172 | return 0; |
| 1173 | out: |
| 1174 | rpcrdma_buffer_destroy(buf); |
| 1175 | return rc; |
| 1176 | } |
| 1177 | |
Chuck Lever | 1e465fd | 2015-10-24 17:27:02 -0400 | [diff] [blame] | 1178 | static struct rpcrdma_req * |
| 1179 | rpcrdma_buffer_get_req_locked(struct rpcrdma_buffer *buf) |
| 1180 | { |
| 1181 | struct rpcrdma_req *req; |
| 1182 | |
| 1183 | req = list_first_entry(&buf->rb_send_bufs, |
Chuck Lever | a80d66c | 2017-06-08 11:52:12 -0400 | [diff] [blame] | 1184 | struct rpcrdma_req, rl_list); |
Chuck Lever | 431af64 | 2017-06-08 11:52:20 -0400 | [diff] [blame] | 1185 | list_del_init(&req->rl_list); |
Chuck Lever | 1e465fd | 2015-10-24 17:27:02 -0400 | [diff] [blame] | 1186 | return req; |
| 1187 | } |
| 1188 | |
| 1189 | static struct rpcrdma_rep * |
| 1190 | rpcrdma_buffer_get_rep_locked(struct rpcrdma_buffer *buf) |
| 1191 | { |
| 1192 | struct rpcrdma_rep *rep; |
| 1193 | |
| 1194 | rep = list_first_entry(&buf->rb_recv_bufs, |
| 1195 | struct rpcrdma_rep, rr_list); |
| 1196 | list_del(&rep->rr_list); |
| 1197 | return rep; |
| 1198 | } |
| 1199 | |
Chuck Lever | 2e84522 | 2014-07-29 17:25:38 -0400 | [diff] [blame] | 1200 | static void |
Chuck Lever | 13650c2 | 2016-09-15 10:56:26 -0400 | [diff] [blame] | 1201 | rpcrdma_destroy_rep(struct rpcrdma_rep *rep) |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1202 | { |
Chuck Lever | 13650c2 | 2016-09-15 10:56:26 -0400 | [diff] [blame] | 1203 | rpcrdma_free_regbuf(rep->rr_rdmabuf); |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1204 | kfree(rep); |
| 1205 | } |
| 1206 | |
Chuck Lever | f531a5d | 2015-10-24 17:27:43 -0400 | [diff] [blame] | 1207 | void |
Chuck Lever | 13650c2 | 2016-09-15 10:56:26 -0400 | [diff] [blame] | 1208 | rpcrdma_destroy_req(struct rpcrdma_req *req) |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1209 | { |
Chuck Lever | 13650c2 | 2016-09-15 10:56:26 -0400 | [diff] [blame] | 1210 | rpcrdma_free_regbuf(req->rl_recvbuf); |
| 1211 | rpcrdma_free_regbuf(req->rl_sendbuf); |
| 1212 | rpcrdma_free_regbuf(req->rl_rdmabuf); |
Chuck Lever | 1392402 | 2015-01-21 11:03:52 -0500 | [diff] [blame] | 1213 | kfree(req); |
| 1214 | } |
| 1215 | |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 1216 | static void |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1217 | rpcrdma_mrs_destroy(struct rpcrdma_buffer *buf) |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 1218 | { |
| 1219 | struct rpcrdma_xprt *r_xprt = container_of(buf, struct rpcrdma_xprt, |
| 1220 | rx_buf); |
| 1221 | struct rpcrdma_ia *ia = rdmab_to_ia(buf); |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1222 | struct rpcrdma_mr *mr; |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 1223 | unsigned int count; |
| 1224 | |
| 1225 | count = 0; |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1226 | spin_lock(&buf->rb_mrlock); |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 1227 | while (!list_empty(&buf->rb_all)) { |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1228 | mr = list_entry(buf->rb_all.next, struct rpcrdma_mr, mr_all); |
| 1229 | list_del(&mr->mr_all); |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 1230 | |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1231 | spin_unlock(&buf->rb_mrlock); |
| 1232 | ia->ri_ops->ro_release_mr(mr); |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 1233 | count++; |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1234 | spin_lock(&buf->rb_mrlock); |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 1235 | } |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1236 | spin_unlock(&buf->rb_mrlock); |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 1237 | r_xprt->rx_stats.mrs_allocated = 0; |
| 1238 | |
| 1239 | dprintk("RPC: %s: released %u MRs\n", __func__, count); |
| 1240 | } |
| 1241 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1242 | void |
| 1243 | rpcrdma_buffer_destroy(struct rpcrdma_buffer *buf) |
| 1244 | { |
Chuck Lever | 505bbe6 | 2016-06-29 13:52:54 -0400 | [diff] [blame] | 1245 | cancel_delayed_work_sync(&buf->rb_recovery_worker); |
Chuck Lever | 9378b27 | 2017-04-11 13:22:29 -0400 | [diff] [blame] | 1246 | cancel_delayed_work_sync(&buf->rb_refresh_worker); |
Chuck Lever | 505bbe6 | 2016-06-29 13:52:54 -0400 | [diff] [blame] | 1247 | |
Chuck Lever | ae72950 | 2017-10-20 10:48:12 -0400 | [diff] [blame] | 1248 | rpcrdma_sendctxs_destroy(buf); |
| 1249 | |
Chuck Lever | 1e465fd | 2015-10-24 17:27:02 -0400 | [diff] [blame] | 1250 | while (!list_empty(&buf->rb_recv_bufs)) { |
| 1251 | struct rpcrdma_rep *rep; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1252 | |
Chuck Lever | 1e465fd | 2015-10-24 17:27:02 -0400 | [diff] [blame] | 1253 | rep = rpcrdma_buffer_get_rep_locked(buf); |
Chuck Lever | 13650c2 | 2016-09-15 10:56:26 -0400 | [diff] [blame] | 1254 | rpcrdma_destroy_rep(rep); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1255 | } |
| 1256 | |
Chuck Lever | f531a5d | 2015-10-24 17:27:43 -0400 | [diff] [blame] | 1257 | spin_lock(&buf->rb_reqslock); |
| 1258 | while (!list_empty(&buf->rb_allreqs)) { |
Chuck Lever | 1e465fd | 2015-10-24 17:27:02 -0400 | [diff] [blame] | 1259 | struct rpcrdma_req *req; |
Allen Andrews | 4034ba0 | 2014-05-28 10:32:09 -0400 | [diff] [blame] | 1260 | |
Chuck Lever | f531a5d | 2015-10-24 17:27:43 -0400 | [diff] [blame] | 1261 | req = list_first_entry(&buf->rb_allreqs, |
| 1262 | struct rpcrdma_req, rl_all); |
| 1263 | list_del(&req->rl_all); |
| 1264 | |
| 1265 | spin_unlock(&buf->rb_reqslock); |
Chuck Lever | 13650c2 | 2016-09-15 10:56:26 -0400 | [diff] [blame] | 1266 | rpcrdma_destroy_req(req); |
Chuck Lever | f531a5d | 2015-10-24 17:27:43 -0400 | [diff] [blame] | 1267 | spin_lock(&buf->rb_reqslock); |
Chuck Lever | 9f9d802 | 2014-07-29 17:24:45 -0400 | [diff] [blame] | 1268 | } |
Chuck Lever | f531a5d | 2015-10-24 17:27:43 -0400 | [diff] [blame] | 1269 | spin_unlock(&buf->rb_reqslock); |
Chuck Lever | 9f9d802 | 2014-07-29 17:24:45 -0400 | [diff] [blame] | 1270 | |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1271 | rpcrdma_mrs_destroy(buf); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1272 | } |
| 1273 | |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1274 | /** |
| 1275 | * rpcrdma_mr_get - Allocate an rpcrdma_mr object |
| 1276 | * @r_xprt: controlling transport |
| 1277 | * |
| 1278 | * Returns an initialized rpcrdma_mr or NULL if no free |
| 1279 | * rpcrdma_mr objects are available. |
| 1280 | */ |
| 1281 | struct rpcrdma_mr * |
| 1282 | rpcrdma_mr_get(struct rpcrdma_xprt *r_xprt) |
Chuck Lever | c2922c0 | 2014-07-29 17:24:36 -0400 | [diff] [blame] | 1283 | { |
Chuck Lever | 346aa66 | 2015-05-26 11:52:06 -0400 | [diff] [blame] | 1284 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1285 | struct rpcrdma_mr *mr = NULL; |
Chuck Lever | 346aa66 | 2015-05-26 11:52:06 -0400 | [diff] [blame] | 1286 | |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1287 | spin_lock(&buf->rb_mrlock); |
| 1288 | if (!list_empty(&buf->rb_mrs)) |
| 1289 | mr = rpcrdma_mr_pop(&buf->rb_mrs); |
| 1290 | spin_unlock(&buf->rb_mrlock); |
Chuck Lever | 346aa66 | 2015-05-26 11:52:06 -0400 | [diff] [blame] | 1291 | |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1292 | if (!mr) |
| 1293 | goto out_nomrs; |
| 1294 | return mr; |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 1295 | |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1296 | out_nomrs: |
Chuck Lever | 1c443eff | 2017-12-20 16:31:21 -0500 | [diff] [blame] | 1297 | trace_xprtrdma_nomrs(r_xprt); |
Chuck Lever | bebd031 | 2017-04-11 13:23:10 -0400 | [diff] [blame] | 1298 | if (r_xprt->rx_ep.rep_connected != -ENODEV) |
| 1299 | schedule_delayed_work(&buf->rb_refresh_worker, 0); |
Chuck Lever | e2ac236 | 2016-06-29 13:54:00 -0400 | [diff] [blame] | 1300 | |
| 1301 | /* Allow the reply handler and refresh worker to run */ |
| 1302 | cond_resched(); |
| 1303 | |
| 1304 | return NULL; |
Chuck Lever | c2922c0 | 2014-07-29 17:24:36 -0400 | [diff] [blame] | 1305 | } |
| 1306 | |
Chuck Lever | ec12e47 | 2017-12-14 20:58:04 -0500 | [diff] [blame] | 1307 | static void |
| 1308 | __rpcrdma_mr_put(struct rpcrdma_buffer *buf, struct rpcrdma_mr *mr) |
| 1309 | { |
| 1310 | spin_lock(&buf->rb_mrlock); |
| 1311 | rpcrdma_mr_push(mr, &buf->rb_mrs); |
| 1312 | spin_unlock(&buf->rb_mrlock); |
| 1313 | } |
| 1314 | |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1315 | /** |
| 1316 | * rpcrdma_mr_put - Release an rpcrdma_mr object |
| 1317 | * @mr: object to release |
| 1318 | * |
| 1319 | */ |
Chuck Lever | 346aa66 | 2015-05-26 11:52:06 -0400 | [diff] [blame] | 1320 | void |
Chuck Lever | 96cedde | 2017-12-14 20:57:55 -0500 | [diff] [blame] | 1321 | rpcrdma_mr_put(struct rpcrdma_mr *mr) |
Chuck Lever | c2922c0 | 2014-07-29 17:24:36 -0400 | [diff] [blame] | 1322 | { |
Chuck Lever | ec12e47 | 2017-12-14 20:58:04 -0500 | [diff] [blame] | 1323 | __rpcrdma_mr_put(&mr->mr_xprt->rx_buf, mr); |
| 1324 | } |
Chuck Lever | c2922c0 | 2014-07-29 17:24:36 -0400 | [diff] [blame] | 1325 | |
Chuck Lever | ec12e47 | 2017-12-14 20:58:04 -0500 | [diff] [blame] | 1326 | /** |
| 1327 | * rpcrdma_mr_unmap_and_put - DMA unmap an MR and release it |
| 1328 | * @mr: object to release |
| 1329 | * |
| 1330 | */ |
| 1331 | void |
| 1332 | rpcrdma_mr_unmap_and_put(struct rpcrdma_mr *mr) |
| 1333 | { |
| 1334 | struct rpcrdma_xprt *r_xprt = mr->mr_xprt; |
| 1335 | |
Chuck Lever | 2937fed | 2017-12-20 16:31:12 -0500 | [diff] [blame] | 1336 | trace_xprtrdma_dma_unmap(mr); |
Chuck Lever | ec12e47 | 2017-12-14 20:58:04 -0500 | [diff] [blame] | 1337 | ib_dma_unmap_sg(r_xprt->rx_ia.ri_device, |
| 1338 | mr->mr_sg, mr->mr_nents, mr->mr_dir); |
| 1339 | __rpcrdma_mr_put(&r_xprt->rx_buf, mr); |
Chuck Lever | c2922c0 | 2014-07-29 17:24:36 -0400 | [diff] [blame] | 1340 | } |
| 1341 | |
Chuck Lever | 7c8d9e7 | 2018-05-04 15:35:20 -0400 | [diff] [blame] | 1342 | /** |
| 1343 | * rpcrdma_buffer_get - Get a request buffer |
| 1344 | * @buffers: Buffer pool from which to obtain a buffer |
Chuck Lever | 78d506e | 2016-09-06 11:22:49 -0400 | [diff] [blame] | 1345 | * |
Chuck Lever | 7c8d9e7 | 2018-05-04 15:35:20 -0400 | [diff] [blame] | 1346 | * Returns a fresh rpcrdma_req, or NULL if none are available. |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1347 | */ |
| 1348 | struct rpcrdma_req * |
| 1349 | rpcrdma_buffer_get(struct rpcrdma_buffer *buffers) |
| 1350 | { |
| 1351 | struct rpcrdma_req *req; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1352 | |
Chuck Lever | a5b027e | 2015-10-24 17:27:27 -0400 | [diff] [blame] | 1353 | spin_lock(&buffers->rb_lock); |
Chuck Lever | 7c8d9e7 | 2018-05-04 15:35:20 -0400 | [diff] [blame] | 1354 | if (unlikely(list_empty(&buffers->rb_send_bufs))) |
| 1355 | goto out_noreqs; |
Chuck Lever | 1e465fd | 2015-10-24 17:27:02 -0400 | [diff] [blame] | 1356 | req = rpcrdma_buffer_get_req_locked(buffers); |
Chuck Lever | a5b027e | 2015-10-24 17:27:27 -0400 | [diff] [blame] | 1357 | spin_unlock(&buffers->rb_lock); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1358 | return req; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1359 | |
Chuck Lever | 7c8d9e7 | 2018-05-04 15:35:20 -0400 | [diff] [blame] | 1360 | out_noreqs: |
Chuck Lever | a5b027e | 2015-10-24 17:27:27 -0400 | [diff] [blame] | 1361 | spin_unlock(&buffers->rb_lock); |
Chuck Lever | 1e465fd | 2015-10-24 17:27:02 -0400 | [diff] [blame] | 1362 | return NULL; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1363 | } |
| 1364 | |
Chuck Lever | 7c8d9e7 | 2018-05-04 15:35:20 -0400 | [diff] [blame] | 1365 | /** |
| 1366 | * rpcrdma_buffer_put - Put request/reply buffers back into pool |
| 1367 | * @req: object to return |
| 1368 | * |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1369 | */ |
| 1370 | void |
| 1371 | rpcrdma_buffer_put(struct rpcrdma_req *req) |
| 1372 | { |
| 1373 | struct rpcrdma_buffer *buffers = req->rl_buffer; |
Chuck Lever | 1e465fd | 2015-10-24 17:27:02 -0400 | [diff] [blame] | 1374 | struct rpcrdma_rep *rep = req->rl_reply; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1375 | |
Chuck Lever | 1e465fd | 2015-10-24 17:27:02 -0400 | [diff] [blame] | 1376 | req->rl_reply = NULL; |
| 1377 | |
Chuck Lever | a5b027e | 2015-10-24 17:27:27 -0400 | [diff] [blame] | 1378 | spin_lock(&buffers->rb_lock); |
Chuck Lever | 7c8d9e7 | 2018-05-04 15:35:20 -0400 | [diff] [blame] | 1379 | list_add(&req->rl_list, &buffers->rb_send_bufs); |
Chuck Lever | 05c9746 | 2016-09-06 11:22:58 -0400 | [diff] [blame] | 1380 | if (rep) { |
Chuck Lever | 7c8d9e7 | 2018-05-04 15:35:20 -0400 | [diff] [blame] | 1381 | if (!rep->rr_temp) { |
| 1382 | list_add(&rep->rr_list, &buffers->rb_recv_bufs); |
| 1383 | rep = NULL; |
| 1384 | } |
Chuck Lever | 05c9746 | 2016-09-06 11:22:58 -0400 | [diff] [blame] | 1385 | } |
Chuck Lever | a5b027e | 2015-10-24 17:27:27 -0400 | [diff] [blame] | 1386 | spin_unlock(&buffers->rb_lock); |
Chuck Lever | 7c8d9e7 | 2018-05-04 15:35:20 -0400 | [diff] [blame] | 1387 | if (rep) |
| 1388 | rpcrdma_destroy_rep(rep); |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | /* |
| 1392 | * Put reply buffers back into pool when not attached to |
| 1393 | * request. This happens in error conditions. |
| 1394 | */ |
| 1395 | void |
| 1396 | rpcrdma_recv_buffer_put(struct rpcrdma_rep *rep) |
| 1397 | { |
Chuck Lever | fed171b | 2015-05-26 11:51:37 -0400 | [diff] [blame] | 1398 | struct rpcrdma_buffer *buffers = &rep->rr_rxprt->rx_buf; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1399 | |
Chuck Lever | 7c8d9e7 | 2018-05-04 15:35:20 -0400 | [diff] [blame] | 1400 | if (!rep->rr_temp) { |
| 1401 | spin_lock(&buffers->rb_lock); |
| 1402 | list_add(&rep->rr_list, &buffers->rb_recv_bufs); |
| 1403 | spin_unlock(&buffers->rb_lock); |
| 1404 | } else { |
| 1405 | rpcrdma_destroy_rep(rep); |
| 1406 | } |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1407 | } |
| 1408 | |
Chuck Lever | 9128c3e | 2015-01-21 11:04:00 -0500 | [diff] [blame] | 1409 | /** |
Chuck Lever | 99ef4db | 2016-09-15 10:56:10 -0400 | [diff] [blame] | 1410 | * rpcrdma_alloc_regbuf - allocate and DMA-map memory for SEND/RECV buffers |
Chuck Lever | 9128c3e | 2015-01-21 11:04:00 -0500 | [diff] [blame] | 1411 | * @size: size of buffer to be allocated, in bytes |
Chuck Lever | 99ef4db | 2016-09-15 10:56:10 -0400 | [diff] [blame] | 1412 | * @direction: direction of data movement |
Chuck Lever | 9128c3e | 2015-01-21 11:04:00 -0500 | [diff] [blame] | 1413 | * @flags: GFP flags |
| 1414 | * |
Chuck Lever | 54cbd6b | 2016-09-15 10:56:18 -0400 | [diff] [blame] | 1415 | * Returns an ERR_PTR, or a pointer to a regbuf, a buffer that |
| 1416 | * can be persistently DMA-mapped for I/O. |
Chuck Lever | 9128c3e | 2015-01-21 11:04:00 -0500 | [diff] [blame] | 1417 | * |
| 1418 | * xprtrdma uses a regbuf for posting an outgoing RDMA SEND, or for |
Chuck Lever | 99ef4db | 2016-09-15 10:56:10 -0400 | [diff] [blame] | 1419 | * receiving the payload of RDMA RECV operations. During Long Calls |
| 1420 | * or Replies they may be registered externally via ro_map. |
Chuck Lever | 9128c3e | 2015-01-21 11:04:00 -0500 | [diff] [blame] | 1421 | */ |
| 1422 | struct rpcrdma_regbuf * |
Chuck Lever | 13650c2 | 2016-09-15 10:56:26 -0400 | [diff] [blame] | 1423 | rpcrdma_alloc_regbuf(size_t size, enum dma_data_direction direction, |
| 1424 | gfp_t flags) |
Chuck Lever | 9128c3e | 2015-01-21 11:04:00 -0500 | [diff] [blame] | 1425 | { |
| 1426 | struct rpcrdma_regbuf *rb; |
Chuck Lever | 9128c3e | 2015-01-21 11:04:00 -0500 | [diff] [blame] | 1427 | |
Chuck Lever | 9128c3e | 2015-01-21 11:04:00 -0500 | [diff] [blame] | 1428 | rb = kmalloc(sizeof(*rb) + size, flags); |
| 1429 | if (rb == NULL) |
Chuck Lever | 54cbd6b | 2016-09-15 10:56:18 -0400 | [diff] [blame] | 1430 | return ERR_PTR(-ENOMEM); |
Chuck Lever | 9128c3e | 2015-01-21 11:04:00 -0500 | [diff] [blame] | 1431 | |
Chuck Lever | 54cbd6b | 2016-09-15 10:56:18 -0400 | [diff] [blame] | 1432 | rb->rg_device = NULL; |
Chuck Lever | 99ef4db | 2016-09-15 10:56:10 -0400 | [diff] [blame] | 1433 | rb->rg_direction = direction; |
Chuck Lever | 54cbd6b | 2016-09-15 10:56:18 -0400 | [diff] [blame] | 1434 | rb->rg_iov.length = size; |
Chuck Lever | 9128c3e | 2015-01-21 11:04:00 -0500 | [diff] [blame] | 1435 | |
| 1436 | return rb; |
Chuck Lever | 54cbd6b | 2016-09-15 10:56:18 -0400 | [diff] [blame] | 1437 | } |
Chuck Lever | 9128c3e | 2015-01-21 11:04:00 -0500 | [diff] [blame] | 1438 | |
Chuck Lever | 54cbd6b | 2016-09-15 10:56:18 -0400 | [diff] [blame] | 1439 | /** |
| 1440 | * __rpcrdma_map_regbuf - DMA-map a regbuf |
| 1441 | * @ia: controlling rpcrdma_ia |
| 1442 | * @rb: regbuf to be mapped |
| 1443 | */ |
| 1444 | bool |
| 1445 | __rpcrdma_dma_map_regbuf(struct rpcrdma_ia *ia, struct rpcrdma_regbuf *rb) |
| 1446 | { |
Chuck Lever | 91a10c5 | 2017-04-11 13:23:02 -0400 | [diff] [blame] | 1447 | struct ib_device *device = ia->ri_device; |
| 1448 | |
Chuck Lever | 54cbd6b | 2016-09-15 10:56:18 -0400 | [diff] [blame] | 1449 | if (rb->rg_direction == DMA_NONE) |
| 1450 | return false; |
| 1451 | |
Chuck Lever | 91a10c5 | 2017-04-11 13:23:02 -0400 | [diff] [blame] | 1452 | rb->rg_iov.addr = ib_dma_map_single(device, |
Chuck Lever | 54cbd6b | 2016-09-15 10:56:18 -0400 | [diff] [blame] | 1453 | (void *)rb->rg_base, |
| 1454 | rdmab_length(rb), |
| 1455 | rb->rg_direction); |
Chuck Lever | 91a10c5 | 2017-04-11 13:23:02 -0400 | [diff] [blame] | 1456 | if (ib_dma_mapping_error(device, rdmab_addr(rb))) |
Chuck Lever | 54cbd6b | 2016-09-15 10:56:18 -0400 | [diff] [blame] | 1457 | return false; |
| 1458 | |
Chuck Lever | 91a10c5 | 2017-04-11 13:23:02 -0400 | [diff] [blame] | 1459 | rb->rg_device = device; |
Chuck Lever | 54cbd6b | 2016-09-15 10:56:18 -0400 | [diff] [blame] | 1460 | rb->rg_iov.lkey = ia->ri_pd->local_dma_lkey; |
| 1461 | return true; |
| 1462 | } |
| 1463 | |
| 1464 | static void |
| 1465 | rpcrdma_dma_unmap_regbuf(struct rpcrdma_regbuf *rb) |
| 1466 | { |
Chuck Lever | e89e8d8f | 2018-01-31 12:34:13 -0500 | [diff] [blame] | 1467 | if (!rb) |
| 1468 | return; |
| 1469 | |
Chuck Lever | 54cbd6b | 2016-09-15 10:56:18 -0400 | [diff] [blame] | 1470 | if (!rpcrdma_regbuf_is_mapped(rb)) |
| 1471 | return; |
| 1472 | |
| 1473 | ib_dma_unmap_single(rb->rg_device, rdmab_addr(rb), |
| 1474 | rdmab_length(rb), rb->rg_direction); |
| 1475 | rb->rg_device = NULL; |
Chuck Lever | 9128c3e | 2015-01-21 11:04:00 -0500 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | /** |
| 1479 | * rpcrdma_free_regbuf - deregister and free registered buffer |
Chuck Lever | 9128c3e | 2015-01-21 11:04:00 -0500 | [diff] [blame] | 1480 | * @rb: regbuf to be deregistered and freed |
| 1481 | */ |
| 1482 | void |
Chuck Lever | 13650c2 | 2016-09-15 10:56:26 -0400 | [diff] [blame] | 1483 | rpcrdma_free_regbuf(struct rpcrdma_regbuf *rb) |
Chuck Lever | 9128c3e | 2015-01-21 11:04:00 -0500 | [diff] [blame] | 1484 | { |
Chuck Lever | 54cbd6b | 2016-09-15 10:56:18 -0400 | [diff] [blame] | 1485 | rpcrdma_dma_unmap_regbuf(rb); |
Chuck Lever | e531dca | 2015-08-03 13:03:20 -0400 | [diff] [blame] | 1486 | kfree(rb); |
Chuck Lever | 9128c3e | 2015-01-21 11:04:00 -0500 | [diff] [blame] | 1487 | } |
| 1488 | |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1489 | /* |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1490 | * Prepost any receive buffer, then post send. |
| 1491 | * |
| 1492 | * Receive buffer is donated to hardware, reclaimed upon recv completion. |
| 1493 | */ |
| 1494 | int |
| 1495 | rpcrdma_ep_post(struct rpcrdma_ia *ia, |
| 1496 | struct rpcrdma_ep *ep, |
| 1497 | struct rpcrdma_req *req) |
| 1498 | { |
Chuck Lever | ae72950 | 2017-10-20 10:48:12 -0400 | [diff] [blame] | 1499 | struct ib_send_wr *send_wr = &req->rl_sendctx->sc_wr; |
Chuck Lever | 655fec6 | 2016-09-15 10:57:24 -0400 | [diff] [blame] | 1500 | int rc; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1501 | |
Chuck Lever | 01bb35c | 2017-10-20 10:48:36 -0400 | [diff] [blame] | 1502 | if (!ep->rep_send_count || |
| 1503 | test_bit(RPCRDMA_REQ_F_TX_RESOURCES, &req->rl_flags)) { |
Chuck Lever | ae72950 | 2017-10-20 10:48:12 -0400 | [diff] [blame] | 1504 | send_wr->send_flags |= IB_SEND_SIGNALED; |
| 1505 | ep->rep_send_count = ep->rep_send_batch; |
| 1506 | } else { |
| 1507 | send_wr->send_flags &= ~IB_SEND_SIGNALED; |
| 1508 | --ep->rep_send_count; |
| 1509 | } |
Chuck Lever | 7a89f9c | 2016-06-29 13:53:43 -0400 | [diff] [blame] | 1510 | |
Chuck Lever | f287762 | 2018-02-28 15:30:59 -0500 | [diff] [blame] | 1511 | rc = ia->ri_ops->ro_send(ia, req); |
Chuck Lever | ab03eff | 2017-12-20 16:30:40 -0500 | [diff] [blame] | 1512 | trace_xprtrdma_post_send(req, rc); |
| 1513 | if (rc) |
| 1514 | return -ENOTCONN; |
| 1515 | return 0; |
\"Talpey, Thomas\ | c56c65f | 2007-09-10 13:51:18 -0400 | [diff] [blame] | 1516 | } |
| 1517 | |
Chuck Lever | 7c8d9e7 | 2018-05-04 15:35:20 -0400 | [diff] [blame] | 1518 | /** |
| 1519 | * rpcrdma_post_recvs - Maybe post some Receive buffers |
| 1520 | * @r_xprt: controlling transport |
| 1521 | * @temp: when true, allocate temp rpcrdma_rep objects |
| 1522 | * |
| 1523 | */ |
| 1524 | void |
| 1525 | rpcrdma_post_recvs(struct rpcrdma_xprt *r_xprt, bool temp) |
| 1526 | { |
| 1527 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; |
| 1528 | struct ib_recv_wr *wr, *bad_wr; |
| 1529 | int needed, count, rc; |
| 1530 | |
| 1531 | needed = buf->rb_credits + (buf->rb_bc_srv_max_requests << 1); |
| 1532 | if (buf->rb_posted_receives > needed) |
| 1533 | return; |
| 1534 | needed -= buf->rb_posted_receives; |
| 1535 | |
| 1536 | count = 0; |
| 1537 | wr = NULL; |
| 1538 | while (needed) { |
| 1539 | struct rpcrdma_regbuf *rb; |
| 1540 | struct rpcrdma_rep *rep; |
| 1541 | |
| 1542 | spin_lock(&buf->rb_lock); |
| 1543 | rep = list_first_entry_or_null(&buf->rb_recv_bufs, |
| 1544 | struct rpcrdma_rep, rr_list); |
| 1545 | if (likely(rep)) |
| 1546 | list_del(&rep->rr_list); |
| 1547 | spin_unlock(&buf->rb_lock); |
| 1548 | if (!rep) { |
| 1549 | if (rpcrdma_create_rep(r_xprt, temp)) |
| 1550 | break; |
| 1551 | continue; |
| 1552 | } |
| 1553 | |
| 1554 | rb = rep->rr_rdmabuf; |
| 1555 | if (!rpcrdma_regbuf_is_mapped(rb)) { |
| 1556 | if (!__rpcrdma_dma_map_regbuf(&r_xprt->rx_ia, rb)) { |
| 1557 | rpcrdma_recv_buffer_put(rep); |
| 1558 | break; |
| 1559 | } |
| 1560 | } |
| 1561 | |
| 1562 | trace_xprtrdma_post_recv(rep->rr_recv_wr.wr_cqe); |
| 1563 | rep->rr_recv_wr.next = wr; |
| 1564 | wr = &rep->rr_recv_wr; |
| 1565 | ++count; |
| 1566 | --needed; |
| 1567 | } |
| 1568 | if (!count) |
| 1569 | return; |
| 1570 | |
| 1571 | rc = ib_post_recv(r_xprt->rx_ia.ri_id->qp, wr, &bad_wr); |
| 1572 | if (rc) { |
| 1573 | for (wr = bad_wr; wr; wr = wr->next) { |
| 1574 | struct rpcrdma_rep *rep; |
| 1575 | |
| 1576 | rep = container_of(wr, struct rpcrdma_rep, rr_recv_wr); |
| 1577 | rpcrdma_recv_buffer_put(rep); |
| 1578 | --count; |
| 1579 | } |
| 1580 | } |
| 1581 | buf->rb_posted_receives += count; |
| 1582 | trace_xprtrdma_post_recvs(r_xprt, count, rc); |
| 1583 | } |