Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/9p/trans_rdma.c |
| 3 | * |
| 4 | * RDMA transport layer based on the trans_fd.c implementation. |
| 5 | * |
| 6 | * Copyright (C) 2008 by Tom Tucker <tom@opengridcomputing.com> |
| 7 | * Copyright (C) 2006 by Russ Cox <rsc@swtch.com> |
| 8 | * Copyright (C) 2004-2005 by Latchesar Ionkov <lucho@ionkov.net> |
| 9 | * Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com> |
| 10 | * Copyright (C) 1997-2002 by Ron Minnich <rminnich@sarnoff.com> |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License version 2 |
| 14 | * as published by the Free Software Foundation. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to: |
| 23 | * Free Software Foundation |
| 24 | * 51 Franklin Street, Fifth Floor |
| 25 | * Boston, MA 02111-1301 USA |
| 26 | * |
| 27 | */ |
| 28 | |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 29 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 30 | |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 31 | #include <linux/in.h> |
| 32 | #include <linux/module.h> |
| 33 | #include <linux/net.h> |
| 34 | #include <linux/ipv6.h> |
| 35 | #include <linux/kthread.h> |
| 36 | #include <linux/errno.h> |
| 37 | #include <linux/kernel.h> |
| 38 | #include <linux/un.h> |
| 39 | #include <linux/uaccess.h> |
| 40 | #include <linux/inet.h> |
| 41 | #include <linux/idr.h> |
| 42 | #include <linux/file.h> |
| 43 | #include <linux/parser.h> |
| 44 | #include <linux/semaphore.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 45 | #include <linux/slab.h> |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 46 | #include <net/9p/9p.h> |
| 47 | #include <net/9p/client.h> |
| 48 | #include <net/9p/transport.h> |
| 49 | #include <rdma/ib_verbs.h> |
| 50 | #include <rdma/rdma_cm.h> |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 51 | |
| 52 | #define P9_PORT 5640 |
| 53 | #define P9_RDMA_SQ_DEPTH 32 |
| 54 | #define P9_RDMA_RQ_DEPTH 32 |
| 55 | #define P9_RDMA_SEND_SGE 4 |
| 56 | #define P9_RDMA_RECV_SGE 4 |
| 57 | #define P9_RDMA_IRD 0 |
| 58 | #define P9_RDMA_ORD 0 |
| 59 | #define P9_RDMA_TIMEOUT 30000 /* 30 seconds */ |
Simon Derr | 3fcc62f | 2013-06-21 15:32:37 +0200 | [diff] [blame] | 60 | #define P9_RDMA_MAXSIZE (1024*1024) /* 1MB */ |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 61 | |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 62 | /** |
| 63 | * struct p9_trans_rdma - RDMA transport instance |
| 64 | * |
| 65 | * @state: tracks the transport state machine for connection setup and tear down |
| 66 | * @cm_id: The RDMA CM ID |
| 67 | * @pd: Protection Domain pointer |
| 68 | * @qp: Queue Pair pointer |
| 69 | * @cq: Completion Queue pointer |
Abhishek Kulkarni | 0e15597 | 2009-07-19 13:41:55 -0600 | [diff] [blame] | 70 | * @dm_mr: DMA Memory Region pointer |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 71 | * @lkey: The local access only memory region key |
| 72 | * @timeout: Number of uSecs to wait for connection management events |
| 73 | * @sq_depth: The depth of the Send Queue |
| 74 | * @sq_sem: Semaphore for the SQ |
| 75 | * @rq_depth: The depth of the Receive Queue. |
Simon Derr | fd453d0 | 2013-06-21 15:32:39 +0200 | [diff] [blame] | 76 | * @rq_sem: Semaphore for the RQ |
Simon Derr | 1cff330 | 2013-06-21 15:32:42 +0200 | [diff] [blame] | 77 | * @excess_rc : Amount of posted Receive Contexts without a pending request. |
| 78 | * See rdma_request() |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 79 | * @addr: The remote peer's address |
| 80 | * @req_lock: Protects the active request list |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 81 | * @cm_done: Completion event for connection management tracking |
| 82 | */ |
| 83 | struct p9_trans_rdma { |
| 84 | enum { |
| 85 | P9_RDMA_INIT, |
| 86 | P9_RDMA_ADDR_RESOLVED, |
| 87 | P9_RDMA_ROUTE_RESOLVED, |
| 88 | P9_RDMA_CONNECTED, |
| 89 | P9_RDMA_FLUSHING, |
| 90 | P9_RDMA_CLOSING, |
| 91 | P9_RDMA_CLOSED, |
| 92 | } state; |
| 93 | struct rdma_cm_id *cm_id; |
| 94 | struct ib_pd *pd; |
| 95 | struct ib_qp *qp; |
| 96 | struct ib_cq *cq; |
| 97 | struct ib_mr *dma_mr; |
| 98 | u32 lkey; |
| 99 | long timeout; |
| 100 | int sq_depth; |
| 101 | struct semaphore sq_sem; |
| 102 | int rq_depth; |
Simon Derr | fd453d0 | 2013-06-21 15:32:39 +0200 | [diff] [blame] | 103 | struct semaphore rq_sem; |
Simon Derr | 1cff330 | 2013-06-21 15:32:42 +0200 | [diff] [blame] | 104 | atomic_t excess_rc; |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 105 | struct sockaddr_in addr; |
| 106 | spinlock_t req_lock; |
| 107 | |
| 108 | struct completion cm_done; |
| 109 | }; |
| 110 | |
| 111 | /** |
| 112 | * p9_rdma_context - Keeps track of in-process WR |
| 113 | * |
| 114 | * @wc_op: The original WR op for when the CQE completes in error. |
| 115 | * @busa: Bus address to unmap when the WR completes |
| 116 | * @req: Keeps track of requests (send) |
| 117 | * @rc: Keepts track of replies (receive) |
| 118 | */ |
| 119 | struct p9_rdma_req; |
| 120 | struct p9_rdma_context { |
| 121 | enum ib_wc_opcode wc_op; |
| 122 | dma_addr_t busa; |
| 123 | union { |
| 124 | struct p9_req_t *req; |
| 125 | struct p9_fcall *rc; |
| 126 | }; |
| 127 | }; |
| 128 | |
| 129 | /** |
| 130 | * p9_rdma_opts - Collection of mount options |
| 131 | * @port: port of connection |
| 132 | * @sq_depth: The requested depth of the SQ. This really doesn't need |
| 133 | * to be any deeper than the number of threads used in the client |
| 134 | * @rq_depth: The depth of the RQ. Should be greater than or equal to SQ depth |
| 135 | * @timeout: Time to wait in msecs for CM events |
| 136 | */ |
| 137 | struct p9_rdma_opts { |
| 138 | short port; |
| 139 | int sq_depth; |
| 140 | int rq_depth; |
| 141 | long timeout; |
Dominique Martinet | f569d3e | 2015-01-09 13:07:00 +0100 | [diff] [blame^] | 142 | int privport; |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | /* |
| 146 | * Option Parsing (code inspired by NFS code) |
| 147 | */ |
| 148 | enum { |
| 149 | /* Options that take integer arguments */ |
Dominique Martinet | f569d3e | 2015-01-09 13:07:00 +0100 | [diff] [blame^] | 150 | Opt_port, Opt_rq_depth, Opt_sq_depth, Opt_timeout, |
| 151 | /* Options that take no argument */ |
| 152 | Opt_privport, |
| 153 | Opt_err, |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | static match_table_t tokens = { |
| 157 | {Opt_port, "port=%u"}, |
| 158 | {Opt_sq_depth, "sq=%u"}, |
| 159 | {Opt_rq_depth, "rq=%u"}, |
| 160 | {Opt_timeout, "timeout=%u"}, |
Dominique Martinet | f569d3e | 2015-01-09 13:07:00 +0100 | [diff] [blame^] | 161 | {Opt_privport, "privport"}, |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 162 | {Opt_err, NULL}, |
| 163 | }; |
| 164 | |
| 165 | /** |
Abhishek Kulkarni | 0e15597 | 2009-07-19 13:41:55 -0600 | [diff] [blame] | 166 | * parse_opts - parse mount options into rdma options structure |
| 167 | * @params: options string passed from mount |
| 168 | * @opts: rdma transport-specific structure to parse options into |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 169 | * |
| 170 | * Returns 0 upon success, -ERRNO upon failure |
| 171 | */ |
| 172 | static int parse_opts(char *params, struct p9_rdma_opts *opts) |
| 173 | { |
| 174 | char *p; |
| 175 | substring_t args[MAX_OPT_ARGS]; |
| 176 | int option; |
Eric Van Hensbergen | d8c8a9e | 2010-02-08 16:23:23 -0600 | [diff] [blame] | 177 | char *options, *tmp_options; |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 178 | |
| 179 | opts->port = P9_PORT; |
| 180 | opts->sq_depth = P9_RDMA_SQ_DEPTH; |
| 181 | opts->rq_depth = P9_RDMA_RQ_DEPTH; |
| 182 | opts->timeout = P9_RDMA_TIMEOUT; |
Dominique Martinet | f569d3e | 2015-01-09 13:07:00 +0100 | [diff] [blame^] | 183 | opts->privport = 0; |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 184 | |
| 185 | if (!params) |
| 186 | return 0; |
| 187 | |
Eric Van Hensbergen | d8c8a9e | 2010-02-08 16:23:23 -0600 | [diff] [blame] | 188 | tmp_options = kstrdup(params, GFP_KERNEL); |
| 189 | if (!tmp_options) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 190 | p9_debug(P9_DEBUG_ERROR, |
| 191 | "failed to allocate copy of option string\n"); |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 192 | return -ENOMEM; |
| 193 | } |
Eric Van Hensbergen | d8c8a9e | 2010-02-08 16:23:23 -0600 | [diff] [blame] | 194 | options = tmp_options; |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 195 | |
| 196 | while ((p = strsep(&options, ",")) != NULL) { |
| 197 | int token; |
| 198 | int r; |
| 199 | if (!*p) |
| 200 | continue; |
| 201 | token = match_token(p, tokens, args); |
Dominique Martinet | f569d3e | 2015-01-09 13:07:00 +0100 | [diff] [blame^] | 202 | if ((token != Opt_err) && (token != Opt_privport)) { |
| 203 | r = match_int(&args[0], &option); |
| 204 | if (r < 0) { |
| 205 | p9_debug(P9_DEBUG_ERROR, |
| 206 | "integer field, but no integer?\n"); |
| 207 | continue; |
| 208 | } |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 209 | } |
| 210 | switch (token) { |
| 211 | case Opt_port: |
| 212 | opts->port = option; |
| 213 | break; |
| 214 | case Opt_sq_depth: |
| 215 | opts->sq_depth = option; |
| 216 | break; |
| 217 | case Opt_rq_depth: |
| 218 | opts->rq_depth = option; |
| 219 | break; |
| 220 | case Opt_timeout: |
| 221 | opts->timeout = option; |
| 222 | break; |
Dominique Martinet | f569d3e | 2015-01-09 13:07:00 +0100 | [diff] [blame^] | 223 | case Opt_privport: |
| 224 | opts->privport = 1; |
| 225 | break; |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 226 | default: |
| 227 | continue; |
| 228 | } |
| 229 | } |
| 230 | /* RQ must be at least as large as the SQ */ |
| 231 | opts->rq_depth = max(opts->rq_depth, opts->sq_depth); |
Eric Van Hensbergen | d8c8a9e | 2010-02-08 16:23:23 -0600 | [diff] [blame] | 232 | kfree(tmp_options); |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | static int |
| 237 | p9_cm_event_handler(struct rdma_cm_id *id, struct rdma_cm_event *event) |
| 238 | { |
| 239 | struct p9_client *c = id->context; |
| 240 | struct p9_trans_rdma *rdma = c->trans; |
| 241 | switch (event->event) { |
| 242 | case RDMA_CM_EVENT_ADDR_RESOLVED: |
| 243 | BUG_ON(rdma->state != P9_RDMA_INIT); |
| 244 | rdma->state = P9_RDMA_ADDR_RESOLVED; |
| 245 | break; |
| 246 | |
| 247 | case RDMA_CM_EVENT_ROUTE_RESOLVED: |
| 248 | BUG_ON(rdma->state != P9_RDMA_ADDR_RESOLVED); |
| 249 | rdma->state = P9_RDMA_ROUTE_RESOLVED; |
| 250 | break; |
| 251 | |
| 252 | case RDMA_CM_EVENT_ESTABLISHED: |
| 253 | BUG_ON(rdma->state != P9_RDMA_ROUTE_RESOLVED); |
| 254 | rdma->state = P9_RDMA_CONNECTED; |
| 255 | break; |
| 256 | |
| 257 | case RDMA_CM_EVENT_DISCONNECTED: |
| 258 | if (rdma) |
| 259 | rdma->state = P9_RDMA_CLOSED; |
| 260 | if (c) |
| 261 | c->status = Disconnected; |
| 262 | break; |
| 263 | |
| 264 | case RDMA_CM_EVENT_TIMEWAIT_EXIT: |
| 265 | break; |
| 266 | |
| 267 | case RDMA_CM_EVENT_ADDR_CHANGE: |
| 268 | case RDMA_CM_EVENT_ROUTE_ERROR: |
| 269 | case RDMA_CM_EVENT_DEVICE_REMOVAL: |
| 270 | case RDMA_CM_EVENT_MULTICAST_JOIN: |
| 271 | case RDMA_CM_EVENT_MULTICAST_ERROR: |
| 272 | case RDMA_CM_EVENT_REJECTED: |
| 273 | case RDMA_CM_EVENT_CONNECT_REQUEST: |
| 274 | case RDMA_CM_EVENT_CONNECT_RESPONSE: |
| 275 | case RDMA_CM_EVENT_CONNECT_ERROR: |
| 276 | case RDMA_CM_EVENT_ADDR_ERROR: |
| 277 | case RDMA_CM_EVENT_UNREACHABLE: |
| 278 | c->status = Disconnected; |
| 279 | rdma_disconnect(rdma->cm_id); |
| 280 | break; |
| 281 | default: |
| 282 | BUG(); |
| 283 | } |
| 284 | complete(&rdma->cm_done); |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | static void |
| 289 | handle_recv(struct p9_client *client, struct p9_trans_rdma *rdma, |
| 290 | struct p9_rdma_context *c, enum ib_wc_status status, u32 byte_len) |
| 291 | { |
| 292 | struct p9_req_t *req; |
| 293 | int err = 0; |
| 294 | int16_t tag; |
| 295 | |
| 296 | req = NULL; |
| 297 | ib_dma_unmap_single(rdma->cm_id->device, c->busa, client->msize, |
| 298 | DMA_FROM_DEVICE); |
| 299 | |
| 300 | if (status != IB_WC_SUCCESS) |
| 301 | goto err_out; |
| 302 | |
| 303 | err = p9_parse_header(c->rc, NULL, NULL, &tag, 1); |
| 304 | if (err) |
| 305 | goto err_out; |
| 306 | |
| 307 | req = p9_tag_lookup(client, tag); |
| 308 | if (!req) |
| 309 | goto err_out; |
| 310 | |
Simon Derr | 47229ff | 2013-06-21 15:32:38 +0200 | [diff] [blame] | 311 | /* Check that we have not yet received a reply for this request. |
| 312 | */ |
| 313 | if (unlikely(req->rc)) { |
| 314 | pr_err("Duplicate reply for request %d", tag); |
| 315 | goto err_out; |
| 316 | } |
| 317 | |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 318 | req->rc = c->rc; |
Dominique Martinet | 2b6e72e | 2014-01-17 18:31:00 +0100 | [diff] [blame] | 319 | p9_client_cb(client, req, REQ_STATUS_RCVD); |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 320 | |
| 321 | return; |
| 322 | |
| 323 | err_out: |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 324 | p9_debug(P9_DEBUG_ERROR, "req %p err %d status %d\n", req, err, status); |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 325 | rdma->state = P9_RDMA_FLUSHING; |
| 326 | client->status = Disconnected; |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | static void |
| 330 | handle_send(struct p9_client *client, struct p9_trans_rdma *rdma, |
| 331 | struct p9_rdma_context *c, enum ib_wc_status status, u32 byte_len) |
| 332 | { |
| 333 | ib_dma_unmap_single(rdma->cm_id->device, |
| 334 | c->busa, c->req->tc->size, |
| 335 | DMA_TO_DEVICE); |
| 336 | } |
| 337 | |
| 338 | static void qp_event_handler(struct ib_event *event, void *context) |
| 339 | { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 340 | p9_debug(P9_DEBUG_ERROR, "QP event %d context %p\n", |
| 341 | event->event, context); |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | static void cq_comp_handler(struct ib_cq *cq, void *cq_context) |
| 345 | { |
| 346 | struct p9_client *client = cq_context; |
| 347 | struct p9_trans_rdma *rdma = client->trans; |
| 348 | int ret; |
| 349 | struct ib_wc wc; |
| 350 | |
| 351 | ib_req_notify_cq(rdma->cq, IB_CQ_NEXT_COMP); |
| 352 | while ((ret = ib_poll_cq(cq, 1, &wc)) > 0) { |
| 353 | struct p9_rdma_context *c = (void *) (unsigned long) wc.wr_id; |
| 354 | |
| 355 | switch (c->wc_op) { |
| 356 | case IB_WC_RECV: |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 357 | handle_recv(client, rdma, c, wc.status, wc.byte_len); |
Simon Derr | fd453d0 | 2013-06-21 15:32:39 +0200 | [diff] [blame] | 358 | up(&rdma->rq_sem); |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 359 | break; |
| 360 | |
| 361 | case IB_WC_SEND: |
| 362 | handle_send(client, rdma, c, wc.status, wc.byte_len); |
| 363 | up(&rdma->sq_sem); |
| 364 | break; |
| 365 | |
| 366 | default: |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 367 | pr_err("unexpected completion type, c->wc_op=%d, wc.opcode=%d, status=%d\n", |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 368 | c->wc_op, wc.opcode, wc.status); |
| 369 | break; |
| 370 | } |
| 371 | kfree(c); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | static void cq_event_handler(struct ib_event *e, void *v) |
| 376 | { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 377 | p9_debug(P9_DEBUG_ERROR, "CQ event %d context %p\n", e->event, v); |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | static void rdma_destroy_trans(struct p9_trans_rdma *rdma) |
| 381 | { |
| 382 | if (!rdma) |
| 383 | return; |
| 384 | |
| 385 | if (rdma->dma_mr && !IS_ERR(rdma->dma_mr)) |
| 386 | ib_dereg_mr(rdma->dma_mr); |
| 387 | |
| 388 | if (rdma->qp && !IS_ERR(rdma->qp)) |
| 389 | ib_destroy_qp(rdma->qp); |
| 390 | |
| 391 | if (rdma->pd && !IS_ERR(rdma->pd)) |
| 392 | ib_dealloc_pd(rdma->pd); |
| 393 | |
| 394 | if (rdma->cq && !IS_ERR(rdma->cq)) |
| 395 | ib_destroy_cq(rdma->cq); |
| 396 | |
| 397 | if (rdma->cm_id && !IS_ERR(rdma->cm_id)) |
| 398 | rdma_destroy_id(rdma->cm_id); |
| 399 | |
| 400 | kfree(rdma); |
| 401 | } |
| 402 | |
| 403 | static int |
| 404 | post_recv(struct p9_client *client, struct p9_rdma_context *c) |
| 405 | { |
| 406 | struct p9_trans_rdma *rdma = client->trans; |
| 407 | struct ib_recv_wr wr, *bad_wr; |
| 408 | struct ib_sge sge; |
| 409 | |
| 410 | c->busa = ib_dma_map_single(rdma->cm_id->device, |
| 411 | c->rc->sdata, client->msize, |
| 412 | DMA_FROM_DEVICE); |
| 413 | if (ib_dma_mapping_error(rdma->cm_id->device, c->busa)) |
| 414 | goto error; |
| 415 | |
| 416 | sge.addr = c->busa; |
| 417 | sge.length = client->msize; |
| 418 | sge.lkey = rdma->lkey; |
| 419 | |
| 420 | wr.next = NULL; |
| 421 | c->wc_op = IB_WC_RECV; |
| 422 | wr.wr_id = (unsigned long) c; |
| 423 | wr.sg_list = &sge; |
| 424 | wr.num_sge = 1; |
| 425 | return ib_post_recv(rdma->qp, &wr, &bad_wr); |
| 426 | |
| 427 | error: |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 428 | p9_debug(P9_DEBUG_ERROR, "EIO\n"); |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 429 | return -EIO; |
| 430 | } |
| 431 | |
| 432 | static int rdma_request(struct p9_client *client, struct p9_req_t *req) |
| 433 | { |
| 434 | struct p9_trans_rdma *rdma = client->trans; |
| 435 | struct ib_send_wr wr, *bad_wr; |
| 436 | struct ib_sge sge; |
| 437 | int err = 0; |
| 438 | unsigned long flags; |
| 439 | struct p9_rdma_context *c = NULL; |
| 440 | struct p9_rdma_context *rpl_context = NULL; |
| 441 | |
Simon Derr | 1cff330 | 2013-06-21 15:32:42 +0200 | [diff] [blame] | 442 | /* When an error occurs between posting the recv and the send, |
| 443 | * there will be a receive context posted without a pending request. |
| 444 | * Since there is no way to "un-post" it, we remember it and skip |
| 445 | * post_recv() for the next request. |
| 446 | * So here, |
| 447 | * see if we are this `next request' and need to absorb an excess rc. |
| 448 | * If yes, then drop and free our own, and do not recv_post(). |
| 449 | **/ |
| 450 | if (unlikely(atomic_read(&rdma->excess_rc) > 0)) { |
| 451 | if ((atomic_sub_return(1, &rdma->excess_rc) >= 0)) { |
| 452 | /* Got one ! */ |
| 453 | kfree(req->rc); |
| 454 | req->rc = NULL; |
| 455 | goto dont_need_post_recv; |
| 456 | } else { |
| 457 | /* We raced and lost. */ |
| 458 | atomic_inc(&rdma->excess_rc); |
| 459 | } |
| 460 | } |
| 461 | |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 462 | /* Allocate an fcall for the reply */ |
Aneesh Kumar K.V | eeff66e | 2011-03-08 16:39:47 +0530 | [diff] [blame] | 463 | rpl_context = kmalloc(sizeof *rpl_context, GFP_NOFS); |
Davidlohr Bueso | 1d6400c | 2010-09-13 15:53:18 +0000 | [diff] [blame] | 464 | if (!rpl_context) { |
| 465 | err = -ENOMEM; |
Simon Derr | 2f52d07 | 2013-06-21 15:32:41 +0200 | [diff] [blame] | 466 | goto recv_error; |
Davidlohr Bueso | 1d6400c | 2010-09-13 15:53:18 +0000 | [diff] [blame] | 467 | } |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 468 | rpl_context->rc = req->rc; |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 469 | |
| 470 | /* |
| 471 | * Post a receive buffer for this request. We need to ensure |
| 472 | * there is a reply buffer available for every outstanding |
| 473 | * request. A flushed request can result in no reply for an |
| 474 | * outstanding request, so we must keep a count to avoid |
| 475 | * overflowing the RQ. |
| 476 | */ |
Simon Derr | 2f52d07 | 2013-06-21 15:32:41 +0200 | [diff] [blame] | 477 | if (down_interruptible(&rdma->rq_sem)) { |
| 478 | err = -EINTR; |
| 479 | goto recv_error; |
| 480 | } |
Simon Derr | fd453d0 | 2013-06-21 15:32:39 +0200 | [diff] [blame] | 481 | |
| 482 | err = post_recv(client, rpl_context); |
| 483 | if (err) { |
| 484 | p9_debug(P9_DEBUG_FCALL, "POST RECV failed\n"); |
Simon Derr | 2f52d07 | 2013-06-21 15:32:41 +0200 | [diff] [blame] | 485 | goto recv_error; |
Simon Derr | fd453d0 | 2013-06-21 15:32:39 +0200 | [diff] [blame] | 486 | } |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 487 | /* remove posted receive buffer from request structure */ |
| 488 | req->rc = NULL; |
| 489 | |
Simon Derr | 1cff330 | 2013-06-21 15:32:42 +0200 | [diff] [blame] | 490 | dont_need_post_recv: |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 491 | /* Post the request */ |
Aneesh Kumar K.V | eeff66e | 2011-03-08 16:39:47 +0530 | [diff] [blame] | 492 | c = kmalloc(sizeof *c, GFP_NOFS); |
Davidlohr Bueso | 1d6400c | 2010-09-13 15:53:18 +0000 | [diff] [blame] | 493 | if (!c) { |
| 494 | err = -ENOMEM; |
Simon Derr | 2f52d07 | 2013-06-21 15:32:41 +0200 | [diff] [blame] | 495 | goto send_error; |
Davidlohr Bueso | 1d6400c | 2010-09-13 15:53:18 +0000 | [diff] [blame] | 496 | } |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 497 | c->req = req; |
| 498 | |
| 499 | c->busa = ib_dma_map_single(rdma->cm_id->device, |
| 500 | c->req->tc->sdata, c->req->tc->size, |
| 501 | DMA_TO_DEVICE); |
Simon Derr | 2f52d07 | 2013-06-21 15:32:41 +0200 | [diff] [blame] | 502 | if (ib_dma_mapping_error(rdma->cm_id->device, c->busa)) { |
| 503 | err = -EIO; |
| 504 | goto send_error; |
| 505 | } |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 506 | |
| 507 | sge.addr = c->busa; |
| 508 | sge.length = c->req->tc->size; |
| 509 | sge.lkey = rdma->lkey; |
| 510 | |
| 511 | wr.next = NULL; |
| 512 | c->wc_op = IB_WC_SEND; |
| 513 | wr.wr_id = (unsigned long) c; |
| 514 | wr.opcode = IB_WR_SEND; |
| 515 | wr.send_flags = IB_SEND_SIGNALED; |
| 516 | wr.sg_list = &sge; |
| 517 | wr.num_sge = 1; |
| 518 | |
Simon Derr | 2f52d07 | 2013-06-21 15:32:41 +0200 | [diff] [blame] | 519 | if (down_interruptible(&rdma->sq_sem)) { |
| 520 | err = -EINTR; |
| 521 | goto send_error; |
| 522 | } |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 523 | |
Simon Derr | 3f9d5b8 | 2014-03-10 16:38:50 +0100 | [diff] [blame] | 524 | /* Mark request as `sent' *before* we actually send it, |
| 525 | * because doing if after could erase the REQ_STATUS_RCVD |
| 526 | * status in case of a very fast reply. |
| 527 | */ |
| 528 | req->status = REQ_STATUS_SENT; |
Simon Derr | 2f52d07 | 2013-06-21 15:32:41 +0200 | [diff] [blame] | 529 | err = ib_post_send(rdma->qp, &wr, &bad_wr); |
| 530 | if (err) |
| 531 | goto send_error; |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 532 | |
Simon Derr | 2f52d07 | 2013-06-21 15:32:41 +0200 | [diff] [blame] | 533 | /* Success */ |
| 534 | return 0; |
| 535 | |
| 536 | /* Handle errors that happened during or while preparing the send: */ |
| 537 | send_error: |
Simon Derr | 3f9d5b8 | 2014-03-10 16:38:50 +0100 | [diff] [blame] | 538 | req->status = REQ_STATUS_ERROR; |
Davidlohr Bueso | 1d6400c | 2010-09-13 15:53:18 +0000 | [diff] [blame] | 539 | kfree(c); |
Simon Derr | 2f52d07 | 2013-06-21 15:32:41 +0200 | [diff] [blame] | 540 | p9_debug(P9_DEBUG_ERROR, "Error %d in rdma_request()\n", err); |
Simon Derr | 1cff330 | 2013-06-21 15:32:42 +0200 | [diff] [blame] | 541 | |
| 542 | /* Ach. |
| 543 | * We did recv_post(), but not send. We have one recv_post in excess. |
| 544 | */ |
| 545 | atomic_inc(&rdma->excess_rc); |
Simon Derr | 2f52d07 | 2013-06-21 15:32:41 +0200 | [diff] [blame] | 546 | return err; |
| 547 | |
| 548 | /* Handle errors that happened during or while preparing post_recv(): */ |
| 549 | recv_error: |
Davidlohr Bueso | 1d6400c | 2010-09-13 15:53:18 +0000 | [diff] [blame] | 550 | kfree(rpl_context); |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 551 | spin_lock_irqsave(&rdma->req_lock, flags); |
| 552 | if (rdma->state < P9_RDMA_CLOSING) { |
| 553 | rdma->state = P9_RDMA_CLOSING; |
| 554 | spin_unlock_irqrestore(&rdma->req_lock, flags); |
| 555 | rdma_disconnect(rdma->cm_id); |
| 556 | } else |
| 557 | spin_unlock_irqrestore(&rdma->req_lock, flags); |
| 558 | return err; |
| 559 | } |
| 560 | |
| 561 | static void rdma_close(struct p9_client *client) |
| 562 | { |
| 563 | struct p9_trans_rdma *rdma; |
| 564 | |
| 565 | if (!client) |
| 566 | return; |
| 567 | |
| 568 | rdma = client->trans; |
| 569 | if (!rdma) |
| 570 | return; |
| 571 | |
| 572 | client->status = Disconnected; |
| 573 | rdma_disconnect(rdma->cm_id); |
| 574 | rdma_destroy_trans(rdma); |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * alloc_rdma - Allocate and initialize the rdma transport structure |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 579 | * @opts: Mount options structure |
| 580 | */ |
| 581 | static struct p9_trans_rdma *alloc_rdma(struct p9_rdma_opts *opts) |
| 582 | { |
| 583 | struct p9_trans_rdma *rdma; |
| 584 | |
| 585 | rdma = kzalloc(sizeof(struct p9_trans_rdma), GFP_KERNEL); |
| 586 | if (!rdma) |
| 587 | return NULL; |
| 588 | |
| 589 | rdma->sq_depth = opts->sq_depth; |
| 590 | rdma->rq_depth = opts->rq_depth; |
| 591 | rdma->timeout = opts->timeout; |
| 592 | spin_lock_init(&rdma->req_lock); |
| 593 | init_completion(&rdma->cm_done); |
| 594 | sema_init(&rdma->sq_sem, rdma->sq_depth); |
Simon Derr | fd453d0 | 2013-06-21 15:32:39 +0200 | [diff] [blame] | 595 | sema_init(&rdma->rq_sem, rdma->rq_depth); |
Simon Derr | 1cff330 | 2013-06-21 15:32:42 +0200 | [diff] [blame] | 596 | atomic_set(&rdma->excess_rc, 0); |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 597 | |
| 598 | return rdma; |
| 599 | } |
| 600 | |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 601 | static int rdma_cancel(struct p9_client *client, struct p9_req_t *req) |
| 602 | { |
Simon Derr | 931700d | 2014-03-10 16:38:51 +0100 | [diff] [blame] | 603 | /* Nothing to do here. |
| 604 | * We will take care of it (if we have to) in rdma_cancelled() |
| 605 | */ |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 606 | return 1; |
| 607 | } |
| 608 | |
Simon Derr | 931700d | 2014-03-10 16:38:51 +0100 | [diff] [blame] | 609 | /* A request has been fully flushed without a reply. |
| 610 | * That means we have posted one buffer in excess. |
| 611 | */ |
| 612 | static int rdma_cancelled(struct p9_client *client, struct p9_req_t *req) |
| 613 | { |
| 614 | struct p9_trans_rdma *rdma = client->trans; |
| 615 | atomic_inc(&rdma->excess_rc); |
| 616 | return 0; |
| 617 | } |
| 618 | |
Dominique Martinet | f569d3e | 2015-01-09 13:07:00 +0100 | [diff] [blame^] | 619 | static int p9_rdma_bind_privport(struct p9_trans_rdma *rdma) |
| 620 | { |
| 621 | struct sockaddr_in cl = { |
| 622 | .sin_family = AF_INET, |
| 623 | .sin_addr.s_addr = htonl(INADDR_ANY), |
| 624 | }; |
| 625 | int port, err = -EINVAL; |
| 626 | |
| 627 | for (port = P9_DEF_MAX_RESVPORT; port >= P9_DEF_MIN_RESVPORT; port--) { |
| 628 | cl.sin_port = htons((ushort)port); |
| 629 | err = rdma_bind_addr(rdma->cm_id, (struct sockaddr *)&cl); |
| 630 | if (err != -EADDRINUSE) |
| 631 | break; |
| 632 | } |
| 633 | return err; |
| 634 | } |
| 635 | |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 636 | /** |
| 637 | * trans_create_rdma - Transport method for creating atransport instance |
| 638 | * @client: client instance |
| 639 | * @addr: IP address string |
| 640 | * @args: Mount options string |
| 641 | */ |
| 642 | static int |
| 643 | rdma_create_trans(struct p9_client *client, const char *addr, char *args) |
| 644 | { |
| 645 | int err; |
| 646 | struct p9_rdma_opts opts; |
| 647 | struct p9_trans_rdma *rdma; |
| 648 | struct rdma_conn_param conn_param; |
| 649 | struct ib_qp_init_attr qp_attr; |
| 650 | struct ib_device_attr devattr; |
| 651 | |
| 652 | /* Parse the transport specific mount options */ |
| 653 | err = parse_opts(args, &opts); |
| 654 | if (err < 0) |
| 655 | return err; |
| 656 | |
| 657 | /* Create and initialize the RDMA transport structure */ |
| 658 | rdma = alloc_rdma(&opts); |
| 659 | if (!rdma) |
| 660 | return -ENOMEM; |
| 661 | |
| 662 | /* Create the RDMA CM ID */ |
Sean Hefty | b26f9b9 | 2010-04-01 17:08:41 +0000 | [diff] [blame] | 663 | rdma->cm_id = rdma_create_id(p9_cm_event_handler, client, RDMA_PS_TCP, |
| 664 | IB_QPT_RC); |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 665 | if (IS_ERR(rdma->cm_id)) |
| 666 | goto error; |
| 667 | |
Tom Tucker | 517ac45 | 2008-10-23 16:30:13 -0500 | [diff] [blame] | 668 | /* Associate the client with the transport */ |
| 669 | client->trans = rdma; |
| 670 | |
Dominique Martinet | f569d3e | 2015-01-09 13:07:00 +0100 | [diff] [blame^] | 671 | /* Bind to a privileged port if we need to */ |
| 672 | if (opts.privport) { |
| 673 | err = p9_rdma_bind_privport(rdma); |
| 674 | if (err < 0) { |
| 675 | pr_err("%s (%d): problem binding to privport: %d\n", |
| 676 | __func__, task_pid_nr(current), -err); |
| 677 | goto error; |
| 678 | } |
| 679 | } |
| 680 | |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 681 | /* Resolve the server's address */ |
| 682 | rdma->addr.sin_family = AF_INET; |
| 683 | rdma->addr.sin_addr.s_addr = in_aton(addr); |
| 684 | rdma->addr.sin_port = htons(opts.port); |
| 685 | err = rdma_resolve_addr(rdma->cm_id, NULL, |
| 686 | (struct sockaddr *)&rdma->addr, |
| 687 | rdma->timeout); |
| 688 | if (err) |
| 689 | goto error; |
| 690 | err = wait_for_completion_interruptible(&rdma->cm_done); |
| 691 | if (err || (rdma->state != P9_RDMA_ADDR_RESOLVED)) |
| 692 | goto error; |
| 693 | |
| 694 | /* Resolve the route to the server */ |
| 695 | err = rdma_resolve_route(rdma->cm_id, rdma->timeout); |
| 696 | if (err) |
| 697 | goto error; |
| 698 | err = wait_for_completion_interruptible(&rdma->cm_done); |
| 699 | if (err || (rdma->state != P9_RDMA_ROUTE_RESOLVED)) |
| 700 | goto error; |
| 701 | |
| 702 | /* Query the device attributes */ |
| 703 | err = ib_query_device(rdma->cm_id->device, &devattr); |
| 704 | if (err) |
| 705 | goto error; |
| 706 | |
| 707 | /* Create the Completion Queue */ |
| 708 | rdma->cq = ib_create_cq(rdma->cm_id->device, cq_comp_handler, |
| 709 | cq_event_handler, client, |
| 710 | opts.sq_depth + opts.rq_depth + 1, 0); |
| 711 | if (IS_ERR(rdma->cq)) |
| 712 | goto error; |
| 713 | ib_req_notify_cq(rdma->cq, IB_CQ_NEXT_COMP); |
| 714 | |
| 715 | /* Create the Protection Domain */ |
| 716 | rdma->pd = ib_alloc_pd(rdma->cm_id->device); |
| 717 | if (IS_ERR(rdma->pd)) |
| 718 | goto error; |
| 719 | |
| 720 | /* Cache the DMA lkey in the transport */ |
| 721 | rdma->dma_mr = NULL; |
| 722 | if (devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY) |
| 723 | rdma->lkey = rdma->cm_id->device->local_dma_lkey; |
| 724 | else { |
| 725 | rdma->dma_mr = ib_get_dma_mr(rdma->pd, IB_ACCESS_LOCAL_WRITE); |
| 726 | if (IS_ERR(rdma->dma_mr)) |
| 727 | goto error; |
| 728 | rdma->lkey = rdma->dma_mr->lkey; |
| 729 | } |
| 730 | |
| 731 | /* Create the Queue Pair */ |
| 732 | memset(&qp_attr, 0, sizeof qp_attr); |
| 733 | qp_attr.event_handler = qp_event_handler; |
| 734 | qp_attr.qp_context = client; |
| 735 | qp_attr.cap.max_send_wr = opts.sq_depth; |
| 736 | qp_attr.cap.max_recv_wr = opts.rq_depth; |
| 737 | qp_attr.cap.max_send_sge = P9_RDMA_SEND_SGE; |
| 738 | qp_attr.cap.max_recv_sge = P9_RDMA_RECV_SGE; |
| 739 | qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR; |
| 740 | qp_attr.qp_type = IB_QPT_RC; |
| 741 | qp_attr.send_cq = rdma->cq; |
| 742 | qp_attr.recv_cq = rdma->cq; |
| 743 | err = rdma_create_qp(rdma->cm_id, rdma->pd, &qp_attr); |
| 744 | if (err) |
| 745 | goto error; |
| 746 | rdma->qp = rdma->cm_id->qp; |
| 747 | |
| 748 | /* Request a connection */ |
| 749 | memset(&conn_param, 0, sizeof(conn_param)); |
| 750 | conn_param.private_data = NULL; |
| 751 | conn_param.private_data_len = 0; |
| 752 | conn_param.responder_resources = P9_RDMA_IRD; |
| 753 | conn_param.initiator_depth = P9_RDMA_ORD; |
| 754 | err = rdma_connect(rdma->cm_id, &conn_param); |
| 755 | if (err) |
| 756 | goto error; |
| 757 | err = wait_for_completion_interruptible(&rdma->cm_done); |
| 758 | if (err || (rdma->state != P9_RDMA_CONNECTED)) |
| 759 | goto error; |
| 760 | |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 761 | client->status = Connected; |
| 762 | |
| 763 | return 0; |
| 764 | |
| 765 | error: |
| 766 | rdma_destroy_trans(rdma); |
| 767 | return -ENOTCONN; |
| 768 | } |
| 769 | |
| 770 | static struct p9_trans_module p9_rdma_trans = { |
| 771 | .name = "rdma", |
| 772 | .maxsize = P9_RDMA_MAXSIZE, |
| 773 | .def = 0, |
| 774 | .owner = THIS_MODULE, |
| 775 | .create = rdma_create_trans, |
| 776 | .close = rdma_close, |
| 777 | .request = rdma_request, |
| 778 | .cancel = rdma_cancel, |
Simon Derr | 931700d | 2014-03-10 16:38:51 +0100 | [diff] [blame] | 779 | .cancelled = rdma_cancelled, |
Tom Tucker | fc79d4b | 2008-10-22 18:47:39 -0500 | [diff] [blame] | 780 | }; |
| 781 | |
| 782 | /** |
| 783 | * p9_trans_rdma_init - Register the 9P RDMA transport driver |
| 784 | */ |
| 785 | static int __init p9_trans_rdma_init(void) |
| 786 | { |
| 787 | v9fs_register_trans(&p9_rdma_trans); |
| 788 | return 0; |
| 789 | } |
| 790 | |
| 791 | static void __exit p9_trans_rdma_exit(void) |
| 792 | { |
| 793 | v9fs_unregister_trans(&p9_rdma_trans); |
| 794 | } |
| 795 | |
| 796 | module_init(p9_trans_rdma_init); |
| 797 | module_exit(p9_trans_rdma_exit); |
| 798 | |
| 799 | MODULE_AUTHOR("Tom Tucker <tom@opengridcomputing.com>"); |
| 800 | MODULE_DESCRIPTION("RDMA Transport for 9P"); |
| 801 | MODULE_LICENSE("Dual BSD/GPL"); |