trans_rdma.c 19 KB

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