trans_rdma.c 17 KB

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