trans_rdma.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #include <linux/in.h>
  30. #include <linux/module.h>
  31. #include <linux/net.h>
  32. #include <linux/ipv6.h>
  33. #include <linux/kthread.h>
  34. #include <linux/errno.h>
  35. #include <linux/kernel.h>
  36. #include <linux/un.h>
  37. #include <linux/uaccess.h>
  38. #include <linux/inet.h>
  39. #include <linux/idr.h>
  40. #include <linux/file.h>
  41. #include <linux/parser.h>
  42. #include <linux/semaphore.h>
  43. #include <linux/slab.h>
  44. #include <net/9p/9p.h>
  45. #include <net/9p/client.h>
  46. #include <net/9p/transport.h>
  47. #include <rdma/ib_verbs.h>
  48. #include <rdma/rdma_cm.h>
  49. #define P9_PORT 5640
  50. #define P9_RDMA_SQ_DEPTH 32
  51. #define P9_RDMA_RQ_DEPTH 32
  52. #define P9_RDMA_SEND_SGE 4
  53. #define P9_RDMA_RECV_SGE 4
  54. #define P9_RDMA_IRD 0
  55. #define P9_RDMA_ORD 0
  56. #define P9_RDMA_TIMEOUT 30000 /* 30 seconds */
  57. #define P9_RDMA_MAXSIZE (1024*1024) /* 1MB */
  58. /**
  59. * struct p9_trans_rdma - RDMA transport instance
  60. *
  61. * @state: tracks the transport state machine for connection setup and tear down
  62. * @cm_id: The RDMA CM ID
  63. * @pd: Protection Domain pointer
  64. * @qp: Queue Pair pointer
  65. * @cq: Completion Queue pointer
  66. * @dm_mr: DMA Memory Region pointer
  67. * @lkey: The local access only memory region key
  68. * @timeout: Number of uSecs to wait for connection management events
  69. * @sq_depth: The depth of the Send Queue
  70. * @sq_sem: Semaphore for the SQ
  71. * @rq_depth: The depth of the Receive Queue.
  72. * @rq_sem: Semaphore for the RQ
  73. * @excess_rc : Amount of posted Receive Contexts without a pending request.
  74. * See rdma_request()
  75. * @addr: The remote peer's address
  76. * @req_lock: Protects the active request list
  77. * @cm_done: Completion event for connection management tracking
  78. */
  79. struct p9_trans_rdma {
  80. enum {
  81. P9_RDMA_INIT,
  82. P9_RDMA_ADDR_RESOLVED,
  83. P9_RDMA_ROUTE_RESOLVED,
  84. P9_RDMA_CONNECTED,
  85. P9_RDMA_FLUSHING,
  86. P9_RDMA_CLOSING,
  87. P9_RDMA_CLOSED,
  88. } state;
  89. struct rdma_cm_id *cm_id;
  90. struct ib_pd *pd;
  91. struct ib_qp *qp;
  92. struct ib_cq *cq;
  93. long timeout;
  94. int sq_depth;
  95. struct semaphore sq_sem;
  96. int rq_depth;
  97. struct semaphore rq_sem;
  98. atomic_t excess_rc;
  99. struct sockaddr_in addr;
  100. spinlock_t req_lock;
  101. struct completion cm_done;
  102. };
  103. /**
  104. * p9_rdma_context - Keeps track of in-process WR
  105. *
  106. * @busa: Bus address to unmap when the WR completes
  107. * @req: Keeps track of requests (send)
  108. * @rc: Keepts track of replies (receive)
  109. */
  110. struct p9_rdma_req;
  111. struct p9_rdma_context {
  112. struct ib_cqe cqe;
  113. dma_addr_t busa;
  114. union {
  115. struct p9_req_t *req;
  116. struct p9_fcall *rc;
  117. };
  118. };
  119. /**
  120. * p9_rdma_opts - Collection of mount options
  121. * @port: port of connection
  122. * @sq_depth: The requested depth of the SQ. This really doesn't need
  123. * to be any deeper than the number of threads used in the client
  124. * @rq_depth: The depth of the RQ. Should be greater than or equal to SQ depth
  125. * @timeout: Time to wait in msecs for CM events
  126. */
  127. struct p9_rdma_opts {
  128. short port;
  129. int sq_depth;
  130. int rq_depth;
  131. long timeout;
  132. int privport;
  133. };
  134. /*
  135. * Option Parsing (code inspired by NFS code)
  136. */
  137. enum {
  138. /* Options that take integer arguments */
  139. Opt_port, Opt_rq_depth, Opt_sq_depth, Opt_timeout,
  140. /* Options that take no argument */
  141. Opt_privport,
  142. Opt_err,
  143. };
  144. static match_table_t tokens = {
  145. {Opt_port, "port=%u"},
  146. {Opt_sq_depth, "sq=%u"},
  147. {Opt_rq_depth, "rq=%u"},
  148. {Opt_timeout, "timeout=%u"},
  149. {Opt_privport, "privport"},
  150. {Opt_err, NULL},
  151. };
  152. /**
  153. * parse_opts - parse mount options into rdma options structure
  154. * @params: options string passed from mount
  155. * @opts: rdma transport-specific structure to parse options into
  156. *
  157. * Returns 0 upon success, -ERRNO upon failure
  158. */
  159. static int parse_opts(char *params, struct p9_rdma_opts *opts)
  160. {
  161. char *p;
  162. substring_t args[MAX_OPT_ARGS];
  163. int option;
  164. char *options, *tmp_options;
  165. opts->port = P9_PORT;
  166. opts->sq_depth = P9_RDMA_SQ_DEPTH;
  167. opts->rq_depth = P9_RDMA_RQ_DEPTH;
  168. opts->timeout = P9_RDMA_TIMEOUT;
  169. opts->privport = 0;
  170. if (!params)
  171. return 0;
  172. tmp_options = kstrdup(params, GFP_KERNEL);
  173. if (!tmp_options) {
  174. p9_debug(P9_DEBUG_ERROR,
  175. "failed to allocate copy of option string\n");
  176. return -ENOMEM;
  177. }
  178. options = tmp_options;
  179. while ((p = strsep(&options, ",")) != NULL) {
  180. int token;
  181. int r;
  182. if (!*p)
  183. continue;
  184. token = match_token(p, tokens, args);
  185. if ((token != Opt_err) && (token != Opt_privport)) {
  186. r = match_int(&args[0], &option);
  187. if (r < 0) {
  188. p9_debug(P9_DEBUG_ERROR,
  189. "integer field, but no integer?\n");
  190. continue;
  191. }
  192. }
  193. switch (token) {
  194. case Opt_port:
  195. opts->port = option;
  196. break;
  197. case Opt_sq_depth:
  198. opts->sq_depth = option;
  199. break;
  200. case Opt_rq_depth:
  201. opts->rq_depth = option;
  202. break;
  203. case Opt_timeout:
  204. opts->timeout = option;
  205. break;
  206. case Opt_privport:
  207. opts->privport = 1;
  208. break;
  209. default:
  210. continue;
  211. }
  212. }
  213. /* RQ must be at least as large as the SQ */
  214. opts->rq_depth = max(opts->rq_depth, opts->sq_depth);
  215. kfree(tmp_options);
  216. return 0;
  217. }
  218. static int
  219. p9_cm_event_handler(struct rdma_cm_id *id, struct rdma_cm_event *event)
  220. {
  221. struct p9_client *c = id->context;
  222. struct p9_trans_rdma *rdma = c->trans;
  223. switch (event->event) {
  224. case RDMA_CM_EVENT_ADDR_RESOLVED:
  225. BUG_ON(rdma->state != P9_RDMA_INIT);
  226. rdma->state = P9_RDMA_ADDR_RESOLVED;
  227. break;
  228. case RDMA_CM_EVENT_ROUTE_RESOLVED:
  229. BUG_ON(rdma->state != P9_RDMA_ADDR_RESOLVED);
  230. rdma->state = P9_RDMA_ROUTE_RESOLVED;
  231. break;
  232. case RDMA_CM_EVENT_ESTABLISHED:
  233. BUG_ON(rdma->state != P9_RDMA_ROUTE_RESOLVED);
  234. rdma->state = P9_RDMA_CONNECTED;
  235. break;
  236. case RDMA_CM_EVENT_DISCONNECTED:
  237. if (rdma)
  238. rdma->state = P9_RDMA_CLOSED;
  239. if (c)
  240. c->status = Disconnected;
  241. break;
  242. case RDMA_CM_EVENT_TIMEWAIT_EXIT:
  243. break;
  244. case RDMA_CM_EVENT_ADDR_CHANGE:
  245. case RDMA_CM_EVENT_ROUTE_ERROR:
  246. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  247. case RDMA_CM_EVENT_MULTICAST_JOIN:
  248. case RDMA_CM_EVENT_MULTICAST_ERROR:
  249. case RDMA_CM_EVENT_REJECTED:
  250. case RDMA_CM_EVENT_CONNECT_REQUEST:
  251. case RDMA_CM_EVENT_CONNECT_RESPONSE:
  252. case RDMA_CM_EVENT_CONNECT_ERROR:
  253. case RDMA_CM_EVENT_ADDR_ERROR:
  254. case RDMA_CM_EVENT_UNREACHABLE:
  255. c->status = Disconnected;
  256. rdma_disconnect(rdma->cm_id);
  257. break;
  258. default:
  259. BUG();
  260. }
  261. complete(&rdma->cm_done);
  262. return 0;
  263. }
  264. static void
  265. recv_done(struct ib_cq *cq, struct ib_wc *wc)
  266. {
  267. struct p9_client *client = cq->cq_context;
  268. struct p9_trans_rdma *rdma = client->trans;
  269. struct p9_rdma_context *c =
  270. container_of(wc->wr_cqe, struct p9_rdma_context, cqe);
  271. struct p9_req_t *req;
  272. int err = 0;
  273. int16_t tag;
  274. req = NULL;
  275. ib_dma_unmap_single(rdma->cm_id->device, c->busa, client->msize,
  276. DMA_FROM_DEVICE);
  277. if (wc->status != IB_WC_SUCCESS)
  278. goto err_out;
  279. err = p9_parse_header(c->rc, NULL, NULL, &tag, 1);
  280. if (err)
  281. goto err_out;
  282. req = p9_tag_lookup(client, tag);
  283. if (!req)
  284. goto err_out;
  285. /* Check that we have not yet received a reply for this request.
  286. */
  287. if (unlikely(req->rc)) {
  288. pr_err("Duplicate reply for request %d", tag);
  289. goto err_out;
  290. }
  291. req->rc = c->rc;
  292. p9_client_cb(client, req, REQ_STATUS_RCVD);
  293. out:
  294. up(&rdma->rq_sem);
  295. kfree(c);
  296. return;
  297. err_out:
  298. p9_debug(P9_DEBUG_ERROR, "req %p err %d status %d\n",
  299. req, err, wc->status);
  300. rdma->state = P9_RDMA_FLUSHING;
  301. client->status = Disconnected;
  302. goto out;
  303. }
  304. static void
  305. send_done(struct ib_cq *cq, struct ib_wc *wc)
  306. {
  307. struct p9_client *client = cq->cq_context;
  308. struct p9_trans_rdma *rdma = client->trans;
  309. struct p9_rdma_context *c =
  310. container_of(wc->wr_cqe, struct p9_rdma_context, cqe);
  311. ib_dma_unmap_single(rdma->cm_id->device,
  312. c->busa, c->req->tc->size,
  313. DMA_TO_DEVICE);
  314. up(&rdma->sq_sem);
  315. kfree(c);
  316. }
  317. static void qp_event_handler(struct ib_event *event, void *context)
  318. {
  319. p9_debug(P9_DEBUG_ERROR, "QP event %d context %p\n",
  320. event->event, context);
  321. }
  322. static void rdma_destroy_trans(struct p9_trans_rdma *rdma)
  323. {
  324. if (!rdma)
  325. return;
  326. if (rdma->qp && !IS_ERR(rdma->qp))
  327. ib_destroy_qp(rdma->qp);
  328. if (rdma->pd && !IS_ERR(rdma->pd))
  329. ib_dealloc_pd(rdma->pd);
  330. if (rdma->cq && !IS_ERR(rdma->cq))
  331. ib_free_cq(rdma->cq);
  332. if (rdma->cm_id && !IS_ERR(rdma->cm_id))
  333. rdma_destroy_id(rdma->cm_id);
  334. kfree(rdma);
  335. }
  336. static int
  337. post_recv(struct p9_client *client, struct p9_rdma_context *c)
  338. {
  339. struct p9_trans_rdma *rdma = client->trans;
  340. struct ib_recv_wr wr, *bad_wr;
  341. struct ib_sge sge;
  342. c->busa = ib_dma_map_single(rdma->cm_id->device,
  343. c->rc->sdata, client->msize,
  344. DMA_FROM_DEVICE);
  345. if (ib_dma_mapping_error(rdma->cm_id->device, c->busa))
  346. goto error;
  347. c->cqe.done = recv_done;
  348. sge.addr = c->busa;
  349. sge.length = client->msize;
  350. sge.lkey = rdma->pd->local_dma_lkey;
  351. wr.next = NULL;
  352. wr.wr_cqe = &c->cqe;
  353. wr.sg_list = &sge;
  354. wr.num_sge = 1;
  355. return ib_post_recv(rdma->qp, &wr, &bad_wr);
  356. error:
  357. p9_debug(P9_DEBUG_ERROR, "EIO\n");
  358. return -EIO;
  359. }
  360. static int rdma_request(struct p9_client *client, struct p9_req_t *req)
  361. {
  362. struct p9_trans_rdma *rdma = client->trans;
  363. struct ib_send_wr wr, *bad_wr;
  364. struct ib_sge sge;
  365. int err = 0;
  366. unsigned long flags;
  367. struct p9_rdma_context *c = NULL;
  368. struct p9_rdma_context *rpl_context = NULL;
  369. /* When an error occurs between posting the recv and the send,
  370. * there will be a receive context posted without a pending request.
  371. * Since there is no way to "un-post" it, we remember it and skip
  372. * post_recv() for the next request.
  373. * So here,
  374. * see if we are this `next request' and need to absorb an excess rc.
  375. * If yes, then drop and free our own, and do not recv_post().
  376. **/
  377. if (unlikely(atomic_read(&rdma->excess_rc) > 0)) {
  378. if ((atomic_sub_return(1, &rdma->excess_rc) >= 0)) {
  379. /* Got one ! */
  380. kfree(req->rc);
  381. req->rc = NULL;
  382. goto dont_need_post_recv;
  383. } else {
  384. /* We raced and lost. */
  385. atomic_inc(&rdma->excess_rc);
  386. }
  387. }
  388. /* Allocate an fcall for the reply */
  389. rpl_context = kmalloc(sizeof *rpl_context, GFP_NOFS);
  390. if (!rpl_context) {
  391. err = -ENOMEM;
  392. goto recv_error;
  393. }
  394. rpl_context->rc = req->rc;
  395. /*
  396. * Post a receive buffer for this request. We need to ensure
  397. * there is a reply buffer available for every outstanding
  398. * request. A flushed request can result in no reply for an
  399. * outstanding request, so we must keep a count to avoid
  400. * overflowing the RQ.
  401. */
  402. if (down_interruptible(&rdma->rq_sem)) {
  403. err = -EINTR;
  404. goto recv_error;
  405. }
  406. err = post_recv(client, rpl_context);
  407. if (err) {
  408. p9_debug(P9_DEBUG_FCALL, "POST RECV failed\n");
  409. goto recv_error;
  410. }
  411. /* remove posted receive buffer from request structure */
  412. req->rc = NULL;
  413. dont_need_post_recv:
  414. /* Post the request */
  415. c = kmalloc(sizeof *c, GFP_NOFS);
  416. if (!c) {
  417. err = -ENOMEM;
  418. goto send_error;
  419. }
  420. c->req = req;
  421. c->busa = ib_dma_map_single(rdma->cm_id->device,
  422. c->req->tc->sdata, c->req->tc->size,
  423. DMA_TO_DEVICE);
  424. if (ib_dma_mapping_error(rdma->cm_id->device, c->busa)) {
  425. err = -EIO;
  426. goto send_error;
  427. }
  428. c->cqe.done = send_done;
  429. sge.addr = c->busa;
  430. sge.length = c->req->tc->size;
  431. sge.lkey = rdma->pd->local_dma_lkey;
  432. wr.next = NULL;
  433. wr.wr_cqe = &c->cqe;
  434. wr.opcode = IB_WR_SEND;
  435. wr.send_flags = IB_SEND_SIGNALED;
  436. wr.sg_list = &sge;
  437. wr.num_sge = 1;
  438. if (down_interruptible(&rdma->sq_sem)) {
  439. err = -EINTR;
  440. goto send_error;
  441. }
  442. /* Mark request as `sent' *before* we actually send it,
  443. * because doing if after could erase the REQ_STATUS_RCVD
  444. * status in case of a very fast reply.
  445. */
  446. req->status = REQ_STATUS_SENT;
  447. err = ib_post_send(rdma->qp, &wr, &bad_wr);
  448. if (err)
  449. goto send_error;
  450. /* Success */
  451. return 0;
  452. /* Handle errors that happened during or while preparing the send: */
  453. send_error:
  454. req->status = REQ_STATUS_ERROR;
  455. kfree(c);
  456. p9_debug(P9_DEBUG_ERROR, "Error %d in rdma_request()\n", err);
  457. /* Ach.
  458. * We did recv_post(), but not send. We have one recv_post in excess.
  459. */
  460. atomic_inc(&rdma->excess_rc);
  461. return err;
  462. /* Handle errors that happened during or while preparing post_recv(): */
  463. recv_error:
  464. kfree(rpl_context);
  465. spin_lock_irqsave(&rdma->req_lock, flags);
  466. if (rdma->state < P9_RDMA_CLOSING) {
  467. rdma->state = P9_RDMA_CLOSING;
  468. spin_unlock_irqrestore(&rdma->req_lock, flags);
  469. rdma_disconnect(rdma->cm_id);
  470. } else
  471. spin_unlock_irqrestore(&rdma->req_lock, flags);
  472. return err;
  473. }
  474. static void rdma_close(struct p9_client *client)
  475. {
  476. struct p9_trans_rdma *rdma;
  477. if (!client)
  478. return;
  479. rdma = client->trans;
  480. if (!rdma)
  481. return;
  482. client->status = Disconnected;
  483. rdma_disconnect(rdma->cm_id);
  484. rdma_destroy_trans(rdma);
  485. }
  486. /**
  487. * alloc_rdma - Allocate and initialize the rdma transport structure
  488. * @opts: Mount options structure
  489. */
  490. static struct p9_trans_rdma *alloc_rdma(struct p9_rdma_opts *opts)
  491. {
  492. struct p9_trans_rdma *rdma;
  493. rdma = kzalloc(sizeof(struct p9_trans_rdma), GFP_KERNEL);
  494. if (!rdma)
  495. return NULL;
  496. rdma->sq_depth = opts->sq_depth;
  497. rdma->rq_depth = opts->rq_depth;
  498. rdma->timeout = opts->timeout;
  499. spin_lock_init(&rdma->req_lock);
  500. init_completion(&rdma->cm_done);
  501. sema_init(&rdma->sq_sem, rdma->sq_depth);
  502. sema_init(&rdma->rq_sem, rdma->rq_depth);
  503. atomic_set(&rdma->excess_rc, 0);
  504. return rdma;
  505. }
  506. static int rdma_cancel(struct p9_client *client, struct p9_req_t *req)
  507. {
  508. /* Nothing to do here.
  509. * We will take care of it (if we have to) in rdma_cancelled()
  510. */
  511. return 1;
  512. }
  513. /* A request has been fully flushed without a reply.
  514. * That means we have posted one buffer in excess.
  515. */
  516. static int rdma_cancelled(struct p9_client *client, struct p9_req_t *req)
  517. {
  518. struct p9_trans_rdma *rdma = client->trans;
  519. atomic_inc(&rdma->excess_rc);
  520. return 0;
  521. }
  522. static int p9_rdma_bind_privport(struct p9_trans_rdma *rdma)
  523. {
  524. struct sockaddr_in cl = {
  525. .sin_family = AF_INET,
  526. .sin_addr.s_addr = htonl(INADDR_ANY),
  527. };
  528. int port, err = -EINVAL;
  529. for (port = P9_DEF_MAX_RESVPORT; port >= P9_DEF_MIN_RESVPORT; port--) {
  530. cl.sin_port = htons((ushort)port);
  531. err = rdma_bind_addr(rdma->cm_id, (struct sockaddr *)&cl);
  532. if (err != -EADDRINUSE)
  533. break;
  534. }
  535. return err;
  536. }
  537. /**
  538. * trans_create_rdma - Transport method for creating atransport instance
  539. * @client: client instance
  540. * @addr: IP address string
  541. * @args: Mount options string
  542. */
  543. static int
  544. rdma_create_trans(struct p9_client *client, const char *addr, char *args)
  545. {
  546. int err;
  547. struct p9_rdma_opts opts;
  548. struct p9_trans_rdma *rdma;
  549. struct rdma_conn_param conn_param;
  550. struct ib_qp_init_attr qp_attr;
  551. /* Parse the transport specific mount options */
  552. err = parse_opts(args, &opts);
  553. if (err < 0)
  554. return err;
  555. /* Create and initialize the RDMA transport structure */
  556. rdma = alloc_rdma(&opts);
  557. if (!rdma)
  558. return -ENOMEM;
  559. /* Create the RDMA CM ID */
  560. rdma->cm_id = rdma_create_id(&init_net, p9_cm_event_handler, client,
  561. RDMA_PS_TCP, IB_QPT_RC);
  562. if (IS_ERR(rdma->cm_id))
  563. goto error;
  564. /* Associate the client with the transport */
  565. client->trans = rdma;
  566. /* Bind to a privileged port if we need to */
  567. if (opts.privport) {
  568. err = p9_rdma_bind_privport(rdma);
  569. if (err < 0) {
  570. pr_err("%s (%d): problem binding to privport: %d\n",
  571. __func__, task_pid_nr(current), -err);
  572. goto error;
  573. }
  574. }
  575. /* Resolve the server's address */
  576. rdma->addr.sin_family = AF_INET;
  577. rdma->addr.sin_addr.s_addr = in_aton(addr);
  578. rdma->addr.sin_port = htons(opts.port);
  579. err = rdma_resolve_addr(rdma->cm_id, NULL,
  580. (struct sockaddr *)&rdma->addr,
  581. rdma->timeout);
  582. if (err)
  583. goto error;
  584. err = wait_for_completion_interruptible(&rdma->cm_done);
  585. if (err || (rdma->state != P9_RDMA_ADDR_RESOLVED))
  586. goto error;
  587. /* Resolve the route to the server */
  588. err = rdma_resolve_route(rdma->cm_id, rdma->timeout);
  589. if (err)
  590. goto error;
  591. err = wait_for_completion_interruptible(&rdma->cm_done);
  592. if (err || (rdma->state != P9_RDMA_ROUTE_RESOLVED))
  593. goto error;
  594. /* Create the Completion Queue */
  595. rdma->cq = ib_alloc_cq(rdma->cm_id->device, client,
  596. opts.sq_depth + opts.rq_depth + 1,
  597. 0, IB_POLL_SOFTIRQ);
  598. if (IS_ERR(rdma->cq))
  599. goto error;
  600. /* Create the Protection Domain */
  601. rdma->pd = ib_alloc_pd(rdma->cm_id->device, 0);
  602. if (IS_ERR(rdma->pd))
  603. goto error;
  604. /* Create the Queue Pair */
  605. memset(&qp_attr, 0, sizeof qp_attr);
  606. qp_attr.event_handler = qp_event_handler;
  607. qp_attr.qp_context = client;
  608. qp_attr.cap.max_send_wr = opts.sq_depth;
  609. qp_attr.cap.max_recv_wr = opts.rq_depth;
  610. qp_attr.cap.max_send_sge = P9_RDMA_SEND_SGE;
  611. qp_attr.cap.max_recv_sge = P9_RDMA_RECV_SGE;
  612. qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  613. qp_attr.qp_type = IB_QPT_RC;
  614. qp_attr.send_cq = rdma->cq;
  615. qp_attr.recv_cq = rdma->cq;
  616. err = rdma_create_qp(rdma->cm_id, rdma->pd, &qp_attr);
  617. if (err)
  618. goto error;
  619. rdma->qp = rdma->cm_id->qp;
  620. /* Request a connection */
  621. memset(&conn_param, 0, sizeof(conn_param));
  622. conn_param.private_data = NULL;
  623. conn_param.private_data_len = 0;
  624. conn_param.responder_resources = P9_RDMA_IRD;
  625. conn_param.initiator_depth = P9_RDMA_ORD;
  626. err = rdma_connect(rdma->cm_id, &conn_param);
  627. if (err)
  628. goto error;
  629. err = wait_for_completion_interruptible(&rdma->cm_done);
  630. if (err || (rdma->state != P9_RDMA_CONNECTED))
  631. goto error;
  632. client->status = Connected;
  633. return 0;
  634. error:
  635. rdma_destroy_trans(rdma);
  636. return -ENOTCONN;
  637. }
  638. static struct p9_trans_module p9_rdma_trans = {
  639. .name = "rdma",
  640. .maxsize = P9_RDMA_MAXSIZE,
  641. .def = 0,
  642. .owner = THIS_MODULE,
  643. .create = rdma_create_trans,
  644. .close = rdma_close,
  645. .request = rdma_request,
  646. .cancel = rdma_cancel,
  647. .cancelled = rdma_cancelled,
  648. };
  649. /**
  650. * p9_trans_rdma_init - Register the 9P RDMA transport driver
  651. */
  652. static int __init p9_trans_rdma_init(void)
  653. {
  654. v9fs_register_trans(&p9_rdma_trans);
  655. return 0;
  656. }
  657. static void __exit p9_trans_rdma_exit(void)
  658. {
  659. v9fs_unregister_trans(&p9_rdma_trans);
  660. }
  661. module_init(p9_trans_rdma_init);
  662. module_exit(p9_trans_rdma_exit);
  663. MODULE_AUTHOR("Tom Tucker <tom@opengridcomputing.com>");
  664. MODULE_DESCRIPTION("RDMA Transport for 9P");
  665. MODULE_LICENSE("Dual BSD/GPL");