trans_rdma.c 19 KB

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