trans_rdma.c 17 KB

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