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