trans_rdma.c 20 KB

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