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