trans_rdma.c 18 KB

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