blob: 37a78d20c0f647a976ec58dbad1a06a957cc1771 [file] [log] [blame]
Tom Tuckerfc79d4b2008-10-22 18:47:39 -05001/*
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 Perches5d385152011-11-28 10:40:46 -080029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Tom Tuckerfc79d4b2008-10-22 18:47:39 -050031#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 Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Tom Tuckerfc79d4b2008-10-22 18:47:39 -050046#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 Tuckerfc79d4b2008-10-22 18:47:39 -050051
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 Derr3fcc62f2013-06-21 15:32:37 +020060#define P9_RDMA_MAXSIZE (1024*1024) /* 1MB */
Tom Tuckerfc79d4b2008-10-22 18:47:39 -050061
Tom Tuckerfc79d4b2008-10-22 18:47:39 -050062/**
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 Kulkarni0e155972009-07-19 13:41:55 -060070 * @dm_mr: DMA Memory Region pointer
Tom Tuckerfc79d4b2008-10-22 18:47:39 -050071 * @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 Derrfd453d02013-06-21 15:32:39 +020076 * @rq_sem: Semaphore for the RQ
Simon Derr1cff3302013-06-21 15:32:42 +020077 * @excess_rc : Amount of posted Receive Contexts without a pending request.
78 * See rdma_request()
Tom Tuckerfc79d4b2008-10-22 18:47:39 -050079 * @addr: The remote peer's address
80 * @req_lock: Protects the active request list
Tom Tuckerfc79d4b2008-10-22 18:47:39 -050081 * @cm_done: Completion event for connection management tracking
82 */
83struct 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 Derrfd453d02013-06-21 15:32:39 +0200103 struct semaphore rq_sem;
Simon Derr1cff3302013-06-21 15:32:42 +0200104 atomic_t excess_rc;
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500105 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 */
119struct p9_rdma_req;
120struct 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 */
137struct p9_rdma_opts {
138 short port;
139 int sq_depth;
140 int rq_depth;
141 long timeout;
Dominique Martinetf569d3e2015-01-09 13:07:00 +0100142 int privport;
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500143};
144
145/*
146 * Option Parsing (code inspired by NFS code)
147 */
148enum {
149 /* Options that take integer arguments */
Dominique Martinetf569d3e2015-01-09 13:07:00 +0100150 Opt_port, Opt_rq_depth, Opt_sq_depth, Opt_timeout,
151 /* Options that take no argument */
152 Opt_privport,
153 Opt_err,
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500154};
155
156static 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 Martinetf569d3e2015-01-09 13:07:00 +0100161 {Opt_privport, "privport"},
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500162 {Opt_err, NULL},
163};
164
165/**
Abhishek Kulkarni0e155972009-07-19 13:41:55 -0600166 * 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 Tuckerfc79d4b2008-10-22 18:47:39 -0500169 *
170 * Returns 0 upon success, -ERRNO upon failure
171 */
172static 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 Hensbergend8c8a9e2010-02-08 16:23:23 -0600177 char *options, *tmp_options;
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500178
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 Martinetf569d3e2015-01-09 13:07:00 +0100183 opts->privport = 0;
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500184
185 if (!params)
186 return 0;
187
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600188 tmp_options = kstrdup(params, GFP_KERNEL);
189 if (!tmp_options) {
Joe Perches5d385152011-11-28 10:40:46 -0800190 p9_debug(P9_DEBUG_ERROR,
191 "failed to allocate copy of option string\n");
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500192 return -ENOMEM;
193 }
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600194 options = tmp_options;
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500195
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 Martinetf569d3e2015-01-09 13:07:00 +0100202 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 Tuckerfc79d4b2008-10-22 18:47:39 -0500209 }
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 Martinetf569d3e2015-01-09 13:07:00 +0100223 case Opt_privport:
224 opts->privport = 1;
225 break;
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500226 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 Hensbergend8c8a9e2010-02-08 16:23:23 -0600232 kfree(tmp_options);
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500233 return 0;
234}
235
236static int
237p9_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
288static void
289handle_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 Derr47229ff2013-06-21 15:32:38 +0200311 /* 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 Tuckerfc79d4b2008-10-22 18:47:39 -0500318 req->rc = c->rc;
Dominique Martinet2b6e72e2014-01-17 18:31:00 +0100319 p9_client_cb(client, req, REQ_STATUS_RCVD);
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500320
321 return;
322
323 err_out:
Joe Perches5d385152011-11-28 10:40:46 -0800324 p9_debug(P9_DEBUG_ERROR, "req %p err %d status %d\n", req, err, status);
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500325 rdma->state = P9_RDMA_FLUSHING;
326 client->status = Disconnected;
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500327}
328
329static void
330handle_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
338static void qp_event_handler(struct ib_event *event, void *context)
339{
Joe Perches5d385152011-11-28 10:40:46 -0800340 p9_debug(P9_DEBUG_ERROR, "QP event %d context %p\n",
341 event->event, context);
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500342}
343
344static 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 Tuckerfc79d4b2008-10-22 18:47:39 -0500357 handle_recv(client, rdma, c, wc.status, wc.byte_len);
Simon Derrfd453d02013-06-21 15:32:39 +0200358 up(&rdma->rq_sem);
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500359 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 Perches5d385152011-11-28 10:40:46 -0800367 pr_err("unexpected completion type, c->wc_op=%d, wc.opcode=%d, status=%d\n",
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500368 c->wc_op, wc.opcode, wc.status);
369 break;
370 }
371 kfree(c);
372 }
373}
374
375static void cq_event_handler(struct ib_event *e, void *v)
376{
Joe Perches5d385152011-11-28 10:40:46 -0800377 p9_debug(P9_DEBUG_ERROR, "CQ event %d context %p\n", e->event, v);
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500378}
379
380static 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
403static int
404post_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 Perches5d385152011-11-28 10:40:46 -0800428 p9_debug(P9_DEBUG_ERROR, "EIO\n");
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500429 return -EIO;
430}
431
432static 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 Derr1cff3302013-06-21 15:32:42 +0200442 /* 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 Tuckerfc79d4b2008-10-22 18:47:39 -0500462 /* Allocate an fcall for the reply */
Aneesh Kumar K.Veeff66e2011-03-08 16:39:47 +0530463 rpl_context = kmalloc(sizeof *rpl_context, GFP_NOFS);
Davidlohr Bueso1d6400c2010-09-13 15:53:18 +0000464 if (!rpl_context) {
465 err = -ENOMEM;
Simon Derr2f52d072013-06-21 15:32:41 +0200466 goto recv_error;
Davidlohr Bueso1d6400c2010-09-13 15:53:18 +0000467 }
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500468 rpl_context->rc = req->rc;
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500469
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 Derr2f52d072013-06-21 15:32:41 +0200477 if (down_interruptible(&rdma->rq_sem)) {
478 err = -EINTR;
479 goto recv_error;
480 }
Simon Derrfd453d02013-06-21 15:32:39 +0200481
482 err = post_recv(client, rpl_context);
483 if (err) {
484 p9_debug(P9_DEBUG_FCALL, "POST RECV failed\n");
Simon Derr2f52d072013-06-21 15:32:41 +0200485 goto recv_error;
Simon Derrfd453d02013-06-21 15:32:39 +0200486 }
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500487 /* remove posted receive buffer from request structure */
488 req->rc = NULL;
489
Simon Derr1cff3302013-06-21 15:32:42 +0200490dont_need_post_recv:
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500491 /* Post the request */
Aneesh Kumar K.Veeff66e2011-03-08 16:39:47 +0530492 c = kmalloc(sizeof *c, GFP_NOFS);
Davidlohr Bueso1d6400c2010-09-13 15:53:18 +0000493 if (!c) {
494 err = -ENOMEM;
Simon Derr2f52d072013-06-21 15:32:41 +0200495 goto send_error;
Davidlohr Bueso1d6400c2010-09-13 15:53:18 +0000496 }
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500497 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 Derr2f52d072013-06-21 15:32:41 +0200502 if (ib_dma_mapping_error(rdma->cm_id->device, c->busa)) {
503 err = -EIO;
504 goto send_error;
505 }
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500506
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 Derr2f52d072013-06-21 15:32:41 +0200519 if (down_interruptible(&rdma->sq_sem)) {
520 err = -EINTR;
521 goto send_error;
522 }
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500523
Simon Derr3f9d5b82014-03-10 16:38:50 +0100524 /* 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 Derr2f52d072013-06-21 15:32:41 +0200529 err = ib_post_send(rdma->qp, &wr, &bad_wr);
530 if (err)
531 goto send_error;
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500532
Simon Derr2f52d072013-06-21 15:32:41 +0200533 /* Success */
534 return 0;
535
536 /* Handle errors that happened during or while preparing the send: */
537 send_error:
Simon Derr3f9d5b82014-03-10 16:38:50 +0100538 req->status = REQ_STATUS_ERROR;
Davidlohr Bueso1d6400c2010-09-13 15:53:18 +0000539 kfree(c);
Simon Derr2f52d072013-06-21 15:32:41 +0200540 p9_debug(P9_DEBUG_ERROR, "Error %d in rdma_request()\n", err);
Simon Derr1cff3302013-06-21 15:32:42 +0200541
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 Derr2f52d072013-06-21 15:32:41 +0200546 return err;
547
548 /* Handle errors that happened during or while preparing post_recv(): */
549 recv_error:
Davidlohr Bueso1d6400c2010-09-13 15:53:18 +0000550 kfree(rpl_context);
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500551 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
561static 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 Tuckerfc79d4b2008-10-22 18:47:39 -0500579 * @opts: Mount options structure
580 */
581static 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 Derrfd453d02013-06-21 15:32:39 +0200595 sema_init(&rdma->rq_sem, rdma->rq_depth);
Simon Derr1cff3302013-06-21 15:32:42 +0200596 atomic_set(&rdma->excess_rc, 0);
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500597
598 return rdma;
599}
600
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500601static int rdma_cancel(struct p9_client *client, struct p9_req_t *req)
602{
Simon Derr931700d2014-03-10 16:38:51 +0100603 /* Nothing to do here.
604 * We will take care of it (if we have to) in rdma_cancelled()
605 */
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500606 return 1;
607}
608
Simon Derr931700d2014-03-10 16:38:51 +0100609/* A request has been fully flushed without a reply.
610 * That means we have posted one buffer in excess.
611 */
612static 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 Martinetf569d3e2015-01-09 13:07:00 +0100619static 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 Tuckerfc79d4b2008-10-22 18:47:39 -0500636/**
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 */
642static int
643rdma_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;
Matan Barak8e372102015-06-11 16:35:21 +0300651 struct ib_cq_init_attr cq_attr = {};
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500652
653 /* Parse the transport specific mount options */
654 err = parse_opts(args, &opts);
655 if (err < 0)
656 return err;
657
658 /* Create and initialize the RDMA transport structure */
659 rdma = alloc_rdma(&opts);
660 if (!rdma)
661 return -ENOMEM;
662
663 /* Create the RDMA CM ID */
Sean Heftyb26f9b92010-04-01 17:08:41 +0000664 rdma->cm_id = rdma_create_id(p9_cm_event_handler, client, RDMA_PS_TCP,
665 IB_QPT_RC);
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500666 if (IS_ERR(rdma->cm_id))
667 goto error;
668
Tom Tucker517ac452008-10-23 16:30:13 -0500669 /* Associate the client with the transport */
670 client->trans = rdma;
671
Dominique Martinetf569d3e2015-01-09 13:07:00 +0100672 /* Bind to a privileged port if we need to */
673 if (opts.privport) {
674 err = p9_rdma_bind_privport(rdma);
675 if (err < 0) {
676 pr_err("%s (%d): problem binding to privport: %d\n",
677 __func__, task_pid_nr(current), -err);
678 goto error;
679 }
680 }
681
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500682 /* Resolve the server's address */
683 rdma->addr.sin_family = AF_INET;
684 rdma->addr.sin_addr.s_addr = in_aton(addr);
685 rdma->addr.sin_port = htons(opts.port);
686 err = rdma_resolve_addr(rdma->cm_id, NULL,
687 (struct sockaddr *)&rdma->addr,
688 rdma->timeout);
689 if (err)
690 goto error;
691 err = wait_for_completion_interruptible(&rdma->cm_done);
692 if (err || (rdma->state != P9_RDMA_ADDR_RESOLVED))
693 goto error;
694
695 /* Resolve the route to the server */
696 err = rdma_resolve_route(rdma->cm_id, rdma->timeout);
697 if (err)
698 goto error;
699 err = wait_for_completion_interruptible(&rdma->cm_done);
700 if (err || (rdma->state != P9_RDMA_ROUTE_RESOLVED))
701 goto error;
702
703 /* Query the device attributes */
704 err = ib_query_device(rdma->cm_id->device, &devattr);
705 if (err)
706 goto error;
707
708 /* Create the Completion Queue */
Matan Barak8e372102015-06-11 16:35:21 +0300709 cq_attr.cqe = opts.sq_depth + opts.rq_depth + 1;
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500710 rdma->cq = ib_create_cq(rdma->cm_id->device, cq_comp_handler,
711 cq_event_handler, client,
Matan Barak8e372102015-06-11 16:35:21 +0300712 &cq_attr);
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500713 if (IS_ERR(rdma->cq))
714 goto error;
715 ib_req_notify_cq(rdma->cq, IB_CQ_NEXT_COMP);
716
717 /* Create the Protection Domain */
718 rdma->pd = ib_alloc_pd(rdma->cm_id->device);
719 if (IS_ERR(rdma->pd))
720 goto error;
721
722 /* Cache the DMA lkey in the transport */
723 rdma->dma_mr = NULL;
724 if (devattr.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)
725 rdma->lkey = rdma->cm_id->device->local_dma_lkey;
726 else {
727 rdma->dma_mr = ib_get_dma_mr(rdma->pd, IB_ACCESS_LOCAL_WRITE);
728 if (IS_ERR(rdma->dma_mr))
729 goto error;
730 rdma->lkey = rdma->dma_mr->lkey;
731 }
732
733 /* Create the Queue Pair */
734 memset(&qp_attr, 0, sizeof qp_attr);
735 qp_attr.event_handler = qp_event_handler;
736 qp_attr.qp_context = client;
737 qp_attr.cap.max_send_wr = opts.sq_depth;
738 qp_attr.cap.max_recv_wr = opts.rq_depth;
739 qp_attr.cap.max_send_sge = P9_RDMA_SEND_SGE;
740 qp_attr.cap.max_recv_sge = P9_RDMA_RECV_SGE;
741 qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
742 qp_attr.qp_type = IB_QPT_RC;
743 qp_attr.send_cq = rdma->cq;
744 qp_attr.recv_cq = rdma->cq;
745 err = rdma_create_qp(rdma->cm_id, rdma->pd, &qp_attr);
746 if (err)
747 goto error;
748 rdma->qp = rdma->cm_id->qp;
749
750 /* Request a connection */
751 memset(&conn_param, 0, sizeof(conn_param));
752 conn_param.private_data = NULL;
753 conn_param.private_data_len = 0;
754 conn_param.responder_resources = P9_RDMA_IRD;
755 conn_param.initiator_depth = P9_RDMA_ORD;
756 err = rdma_connect(rdma->cm_id, &conn_param);
757 if (err)
758 goto error;
759 err = wait_for_completion_interruptible(&rdma->cm_done);
760 if (err || (rdma->state != P9_RDMA_CONNECTED))
761 goto error;
762
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500763 client->status = Connected;
764
765 return 0;
766
767error:
768 rdma_destroy_trans(rdma);
769 return -ENOTCONN;
770}
771
772static struct p9_trans_module p9_rdma_trans = {
773 .name = "rdma",
774 .maxsize = P9_RDMA_MAXSIZE,
775 .def = 0,
776 .owner = THIS_MODULE,
777 .create = rdma_create_trans,
778 .close = rdma_close,
779 .request = rdma_request,
780 .cancel = rdma_cancel,
Simon Derr931700d2014-03-10 16:38:51 +0100781 .cancelled = rdma_cancelled,
Tom Tuckerfc79d4b2008-10-22 18:47:39 -0500782};
783
784/**
785 * p9_trans_rdma_init - Register the 9P RDMA transport driver
786 */
787static int __init p9_trans_rdma_init(void)
788{
789 v9fs_register_trans(&p9_rdma_trans);
790 return 0;
791}
792
793static void __exit p9_trans_rdma_exit(void)
794{
795 v9fs_unregister_trans(&p9_rdma_trans);
796}
797
798module_init(p9_trans_rdma_init);
799module_exit(p9_trans_rdma_exit);
800
801MODULE_AUTHOR("Tom Tucker <tom@opengridcomputing.com>");
802MODULE_DESCRIPTION("RDMA Transport for 9P");
803MODULE_LICENSE("Dual BSD/GPL");