trans_rdma.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/9p/trans_rdma.c
  4. *
  5. * RDMA transport layer based on the trans_fd.c implementation.
  6. *
  7. * Copyright (C) 2008 by Tom Tucker <tom@opengridcomputing.com>
  8. * Copyright (C) 2006 by Russ Cox <rsc@swtch.com>
  9. * Copyright (C) 2004-2005 by Latchesar Ionkov <lucho@ionkov.net>
  10. * Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com>
  11. * Copyright (C) 1997-2002 by Ron Minnich <rminnich@sarnoff.com>
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/in.h>
  15. #include <linux/module.h>
  16. #include <linux/net.h>
  17. #include <linux/ipv6.h>
  18. #include <linux/kthread.h>
  19. #include <linux/errno.h>
  20. #include <linux/kernel.h>
  21. #include <linux/un.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/inet.h>
  24. #include <linux/idr.h>
  25. #include <linux/file.h>
  26. #include <linux/parser.h>
  27. #include <linux/semaphore.h>
  28. #include <linux/slab.h>
  29. #include <linux/seq_file.h>
  30. #include <net/9p/9p.h>
  31. #include <net/9p/client.h>
  32. #include <net/9p/transport.h>
  33. #include <rdma/ib_verbs.h>
  34. #include <rdma/rdma_cm.h>
  35. #define P9_PORT 5640
  36. #define P9_RDMA_SQ_DEPTH 32
  37. #define P9_RDMA_RQ_DEPTH 32
  38. #define P9_RDMA_SEND_SGE 4
  39. #define P9_RDMA_RECV_SGE 4
  40. #define P9_RDMA_IRD 0
  41. #define P9_RDMA_ORD 0
  42. #define P9_RDMA_TIMEOUT 30000 /* 30 seconds */
  43. #define P9_RDMA_MAXSIZE (1024*1024) /* 1MB */
  44. /**
  45. * struct p9_trans_rdma - RDMA transport instance
  46. *
  47. * @state: tracks the transport state machine for connection setup and tear down
  48. * @cm_id: The RDMA CM ID
  49. * @pd: Protection Domain pointer
  50. * @qp: Queue Pair pointer
  51. * @cq: Completion Queue pointer
  52. * @timeout: Number of uSecs to wait for connection management events
  53. * @privport: Whether a privileged port may be used
  54. * @port: The port to use
  55. * @sq_depth: The depth of the Send Queue
  56. * @sq_sem: Semaphore for the SQ
  57. * @rq_depth: The depth of the Receive Queue.
  58. * @rq_sem: Semaphore for the RQ
  59. * @excess_rc : Amount of posted Receive Contexts without a pending request.
  60. * See rdma_request()
  61. * @addr: The remote peer's address
  62. * @req_lock: Protects the active request list
  63. * @cm_done: Completion event for connection management tracking
  64. */
  65. struct p9_trans_rdma {
  66. enum {
  67. P9_RDMA_INIT,
  68. P9_RDMA_ADDR_RESOLVED,
  69. P9_RDMA_ROUTE_RESOLVED,
  70. P9_RDMA_CONNECTED,
  71. P9_RDMA_FLUSHING,
  72. P9_RDMA_CLOSING,
  73. P9_RDMA_CLOSED,
  74. } state;
  75. struct rdma_cm_id *cm_id;
  76. struct ib_pd *pd;
  77. struct ib_qp *qp;
  78. struct ib_cq *cq;
  79. long timeout;
  80. bool privport;
  81. u16 port;
  82. int sq_depth;
  83. struct semaphore sq_sem;
  84. int rq_depth;
  85. struct semaphore rq_sem;
  86. atomic_t excess_rc;
  87. struct sockaddr_in addr;
  88. spinlock_t req_lock;
  89. struct completion cm_done;
  90. };
  91. struct p9_rdma_req;
  92. /**
  93. * struct p9_rdma_context - Keeps track of in-process WR
  94. *
  95. * @busa: Bus address to unmap when the WR completes
  96. * @req: Keeps track of requests (send)
  97. * @rc: Keepts track of replies (receive)
  98. */
  99. struct p9_rdma_context {
  100. struct ib_cqe cqe;
  101. dma_addr_t busa;
  102. union {
  103. struct p9_req_t *req;
  104. struct p9_fcall rc;
  105. };
  106. };
  107. /**
  108. * struct p9_rdma_opts - Collection of mount options
  109. * @port: port of connection
  110. * @sq_depth: The requested depth of the SQ. This really doesn't need
  111. * to be any deeper than the number of threads used in the client
  112. * @rq_depth: The depth of the RQ. Should be greater than or equal to SQ depth
  113. * @timeout: Time to wait in msecs for CM events
  114. */
  115. struct p9_rdma_opts {
  116. short port;
  117. bool privport;
  118. int sq_depth;
  119. int rq_depth;
  120. long timeout;
  121. };
  122. /*
  123. * Option Parsing (code inspired by NFS code)
  124. */
  125. enum {
  126. /* Options that take integer arguments */
  127. Opt_port, Opt_rq_depth, Opt_sq_depth, Opt_timeout,
  128. /* Options that take no argument */
  129. Opt_privport,
  130. Opt_err,
  131. };
  132. static match_table_t tokens = {
  133. {Opt_port, "port=%u"},
  134. {Opt_sq_depth, "sq=%u"},
  135. {Opt_rq_depth, "rq=%u"},
  136. {Opt_timeout, "timeout=%u"},
  137. {Opt_privport, "privport"},
  138. {Opt_err, NULL},
  139. };
  140. static int p9_rdma_show_options(struct seq_file *m, struct p9_client *clnt)
  141. {
  142. struct p9_trans_rdma *rdma = clnt->trans;
  143. if (rdma->port != P9_PORT)
  144. seq_printf(m, ",port=%u", rdma->port);
  145. if (rdma->sq_depth != P9_RDMA_SQ_DEPTH)
  146. seq_printf(m, ",sq=%u", rdma->sq_depth);
  147. if (rdma->rq_depth != P9_RDMA_RQ_DEPTH)
  148. seq_printf(m, ",rq=%u", rdma->rq_depth);
  149. if (rdma->timeout != P9_RDMA_TIMEOUT)
  150. seq_printf(m, ",timeout=%lu", rdma->timeout);
  151. if (rdma->privport)
  152. seq_puts(m, ",privport");
  153. return 0;
  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 = false;
  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 = true;
  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. c->status = Disconnected;
  243. break;
  244. case RDMA_CM_EVENT_TIMEWAIT_EXIT:
  245. break;
  246. case RDMA_CM_EVENT_ADDR_CHANGE:
  247. case RDMA_CM_EVENT_ROUTE_ERROR:
  248. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  249. case RDMA_CM_EVENT_MULTICAST_JOIN:
  250. case RDMA_CM_EVENT_MULTICAST_ERROR:
  251. case RDMA_CM_EVENT_REJECTED:
  252. case RDMA_CM_EVENT_CONNECT_REQUEST:
  253. case RDMA_CM_EVENT_CONNECT_RESPONSE:
  254. case RDMA_CM_EVENT_CONNECT_ERROR:
  255. case RDMA_CM_EVENT_ADDR_ERROR:
  256. case RDMA_CM_EVENT_UNREACHABLE:
  257. c->status = Disconnected;
  258. rdma_disconnect(rdma->cm_id);
  259. break;
  260. default:
  261. BUG();
  262. }
  263. complete(&rdma->cm_done);
  264. return 0;
  265. }
  266. static void
  267. recv_done(struct ib_cq *cq, struct ib_wc *wc)
  268. {
  269. struct p9_client *client = cq->cq_context;
  270. struct p9_trans_rdma *rdma = client->trans;
  271. struct p9_rdma_context *c =
  272. container_of(wc->wr_cqe, struct p9_rdma_context, cqe);
  273. struct p9_req_t *req;
  274. int err = 0;
  275. int16_t tag;
  276. req = NULL;
  277. ib_dma_unmap_single(rdma->cm_id->device, c->busa, client->msize,
  278. DMA_FROM_DEVICE);
  279. if (wc->status != IB_WC_SUCCESS)
  280. goto err_out;
  281. c->rc.size = wc->byte_len;
  282. err = p9_parse_header(&c->rc, NULL, NULL, &tag, 1);
  283. if (err)
  284. goto err_out;
  285. req = p9_tag_lookup(client, tag);
  286. if (!req)
  287. goto err_out;
  288. /* Check that we have not yet received a reply for this request.
  289. */
  290. if (unlikely(req->rc.sdata)) {
  291. pr_err("Duplicate reply for request %d", tag);
  292. goto err_out;
  293. }
  294. req->rc.size = c->rc.size;
  295. req->rc.sdata = c->rc.sdata;
  296. p9_client_cb(client, req, REQ_STATUS_RCVD);
  297. out:
  298. up(&rdma->rq_sem);
  299. kfree(c);
  300. return;
  301. err_out:
  302. p9_debug(P9_DEBUG_ERROR, "req %p err %d status %d\n",
  303. req, err, wc->status);
  304. rdma->state = P9_RDMA_FLUSHING;
  305. client->status = Disconnected;
  306. goto out;
  307. }
  308. static void
  309. send_done(struct ib_cq *cq, struct ib_wc *wc)
  310. {
  311. struct p9_client *client = cq->cq_context;
  312. struct p9_trans_rdma *rdma = client->trans;
  313. struct p9_rdma_context *c =
  314. container_of(wc->wr_cqe, struct p9_rdma_context, cqe);
  315. ib_dma_unmap_single(rdma->cm_id->device,
  316. c->busa, c->req->tc.size,
  317. DMA_TO_DEVICE);
  318. up(&rdma->sq_sem);
  319. p9_req_put(c->req);
  320. kfree(c);
  321. }
  322. static void qp_event_handler(struct ib_event *event, void *context)
  323. {
  324. p9_debug(P9_DEBUG_ERROR, "QP event %d context %p\n",
  325. event->event, context);
  326. }
  327. static void rdma_destroy_trans(struct p9_trans_rdma *rdma)
  328. {
  329. if (!rdma)
  330. return;
  331. if (rdma->qp && !IS_ERR(rdma->qp))
  332. ib_destroy_qp(rdma->qp);
  333. if (rdma->pd && !IS_ERR(rdma->pd))
  334. ib_dealloc_pd(rdma->pd);
  335. if (rdma->cq && !IS_ERR(rdma->cq))
  336. ib_free_cq(rdma->cq);
  337. if (rdma->cm_id && !IS_ERR(rdma->cm_id))
  338. rdma_destroy_id(rdma->cm_id);
  339. kfree(rdma);
  340. }
  341. static int
  342. post_recv(struct p9_client *client, struct p9_rdma_context *c)
  343. {
  344. struct p9_trans_rdma *rdma = client->trans;
  345. struct ib_recv_wr wr;
  346. struct ib_sge sge;
  347. c->busa = ib_dma_map_single(rdma->cm_id->device,
  348. c->rc.sdata, client->msize,
  349. DMA_FROM_DEVICE);
  350. if (ib_dma_mapping_error(rdma->cm_id->device, c->busa))
  351. goto error;
  352. c->cqe.done = recv_done;
  353. sge.addr = c->busa;
  354. sge.length = client->msize;
  355. sge.lkey = rdma->pd->local_dma_lkey;
  356. wr.next = NULL;
  357. wr.wr_cqe = &c->cqe;
  358. wr.sg_list = &sge;
  359. wr.num_sge = 1;
  360. return ib_post_recv(rdma->qp, &wr, NULL);
  361. error:
  362. p9_debug(P9_DEBUG_ERROR, "EIO\n");
  363. return -EIO;
  364. }
  365. static int rdma_request(struct p9_client *client, struct p9_req_t *req)
  366. {
  367. struct p9_trans_rdma *rdma = client->trans;
  368. struct ib_send_wr wr;
  369. struct ib_sge sge;
  370. int err = 0;
  371. unsigned long flags;
  372. struct p9_rdma_context *c = NULL;
  373. struct p9_rdma_context *rpl_context = NULL;
  374. /* When an error occurs between posting the recv and the send,
  375. * there will be a receive context posted without a pending request.
  376. * Since there is no way to "un-post" it, we remember it and skip
  377. * post_recv() for the next request.
  378. * So here,
  379. * see if we are this `next request' and need to absorb an excess rc.
  380. * If yes, then drop and free our own, and do not recv_post().
  381. **/
  382. if (unlikely(atomic_read(&rdma->excess_rc) > 0)) {
  383. if ((atomic_sub_return(1, &rdma->excess_rc) >= 0)) {
  384. /* Got one! */
  385. p9_fcall_fini(&req->rc);
  386. req->rc.sdata = NULL;
  387. goto dont_need_post_recv;
  388. } else {
  389. /* We raced and lost. */
  390. atomic_inc(&rdma->excess_rc);
  391. }
  392. }
  393. /* Allocate an fcall for the reply */
  394. rpl_context = kmalloc(sizeof *rpl_context, GFP_NOFS);
  395. if (!rpl_context) {
  396. err = -ENOMEM;
  397. goto recv_error;
  398. }
  399. rpl_context->rc.sdata = req->rc.sdata;
  400. /*
  401. * Post a receive buffer for this request. We need to ensure
  402. * there is a reply buffer available for every outstanding
  403. * request. A flushed request can result in no reply for an
  404. * outstanding request, so we must keep a count to avoid
  405. * overflowing the RQ.
  406. */
  407. if (down_interruptible(&rdma->rq_sem)) {
  408. err = -EINTR;
  409. goto recv_error;
  410. }
  411. err = post_recv(client, rpl_context);
  412. if (err) {
  413. p9_debug(P9_DEBUG_ERROR, "POST RECV failed: %d\n", err);
  414. goto recv_error;
  415. }
  416. /* remove posted receive buffer from request structure */
  417. req->rc.sdata = NULL;
  418. dont_need_post_recv:
  419. /* Post the request */
  420. c = kmalloc(sizeof *c, GFP_NOFS);
  421. if (!c) {
  422. err = -ENOMEM;
  423. goto send_error;
  424. }
  425. c->req = req;
  426. c->busa = ib_dma_map_single(rdma->cm_id->device,
  427. c->req->tc.sdata, c->req->tc.size,
  428. DMA_TO_DEVICE);
  429. if (ib_dma_mapping_error(rdma->cm_id->device, c->busa)) {
  430. err = -EIO;
  431. goto send_error;
  432. }
  433. c->cqe.done = send_done;
  434. sge.addr = c->busa;
  435. sge.length = c->req->tc.size;
  436. sge.lkey = rdma->pd->local_dma_lkey;
  437. wr.next = NULL;
  438. wr.wr_cqe = &c->cqe;
  439. wr.opcode = IB_WR_SEND;
  440. wr.send_flags = IB_SEND_SIGNALED;
  441. wr.sg_list = &sge;
  442. wr.num_sge = 1;
  443. if (down_interruptible(&rdma->sq_sem)) {
  444. err = -EINTR;
  445. goto send_error;
  446. }
  447. /* Mark request as `sent' *before* we actually send it,
  448. * because doing if after could erase the REQ_STATUS_RCVD
  449. * status in case of a very fast reply.
  450. */
  451. req->status = REQ_STATUS_SENT;
  452. err = ib_post_send(rdma->qp, &wr, NULL);
  453. if (err)
  454. goto send_error;
  455. /* Success */
  456. return 0;
  457. /* Handle errors that happened during or while preparing the send: */
  458. send_error:
  459. req->status = REQ_STATUS_ERROR;
  460. kfree(c);
  461. p9_debug(P9_DEBUG_ERROR, "Error %d in rdma_request()\n", err);
  462. /* Ach.
  463. * We did recv_post(), but not send. We have one recv_post in excess.
  464. */
  465. atomic_inc(&rdma->excess_rc);
  466. return err;
  467. /* Handle errors that happened during or while preparing post_recv(): */
  468. recv_error:
  469. kfree(rpl_context);
  470. spin_lock_irqsave(&rdma->req_lock, flags);
  471. if (err != -EINTR && rdma->state < P9_RDMA_CLOSING) {
  472. rdma->state = P9_RDMA_CLOSING;
  473. spin_unlock_irqrestore(&rdma->req_lock, flags);
  474. rdma_disconnect(rdma->cm_id);
  475. } else
  476. spin_unlock_irqrestore(&rdma->req_lock, flags);
  477. return err;
  478. }
  479. static void rdma_close(struct p9_client *client)
  480. {
  481. struct p9_trans_rdma *rdma;
  482. if (!client)
  483. return;
  484. rdma = client->trans;
  485. if (!rdma)
  486. return;
  487. client->status = Disconnected;
  488. rdma_disconnect(rdma->cm_id);
  489. rdma_destroy_trans(rdma);
  490. }
  491. /**
  492. * alloc_rdma - Allocate and initialize the rdma transport structure
  493. * @opts: Mount options structure
  494. */
  495. static struct p9_trans_rdma *alloc_rdma(struct p9_rdma_opts *opts)
  496. {
  497. struct p9_trans_rdma *rdma;
  498. rdma = kzalloc(sizeof(struct p9_trans_rdma), GFP_KERNEL);
  499. if (!rdma)
  500. return NULL;
  501. rdma->port = opts->port;
  502. rdma->privport = opts->privport;
  503. rdma->sq_depth = opts->sq_depth;
  504. rdma->rq_depth = opts->rq_depth;
  505. rdma->timeout = opts->timeout;
  506. spin_lock_init(&rdma->req_lock);
  507. init_completion(&rdma->cm_done);
  508. sema_init(&rdma->sq_sem, rdma->sq_depth);
  509. sema_init(&rdma->rq_sem, rdma->rq_depth);
  510. atomic_set(&rdma->excess_rc, 0);
  511. return rdma;
  512. }
  513. static int rdma_cancel(struct p9_client *client, struct p9_req_t *req)
  514. {
  515. /* Nothing to do here.
  516. * We will take care of it (if we have to) in rdma_cancelled()
  517. */
  518. return 1;
  519. }
  520. /* A request has been fully flushed without a reply.
  521. * That means we have posted one buffer in excess.
  522. */
  523. static int rdma_cancelled(struct p9_client *client, struct p9_req_t *req)
  524. {
  525. struct p9_trans_rdma *rdma = client->trans;
  526. atomic_inc(&rdma->excess_rc);
  527. return 0;
  528. }
  529. static int p9_rdma_bind_privport(struct p9_trans_rdma *rdma)
  530. {
  531. struct sockaddr_in cl = {
  532. .sin_family = AF_INET,
  533. .sin_addr.s_addr = htonl(INADDR_ANY),
  534. };
  535. int port, err = -EINVAL;
  536. for (port = P9_DEF_MAX_RESVPORT; port >= P9_DEF_MIN_RESVPORT; port--) {
  537. cl.sin_port = htons((ushort)port);
  538. err = rdma_bind_addr(rdma->cm_id, (struct sockaddr *)&cl);
  539. if (err != -EADDRINUSE)
  540. break;
  541. }
  542. return err;
  543. }
  544. /**
  545. * rdma_create_trans - Transport method for creating a transport instance
  546. * @client: client instance
  547. * @addr: IP address string
  548. * @args: Mount options string
  549. */
  550. static int
  551. rdma_create_trans(struct p9_client *client, const char *addr, char *args)
  552. {
  553. int err;
  554. struct p9_rdma_opts opts;
  555. struct p9_trans_rdma *rdma;
  556. struct rdma_conn_param conn_param;
  557. struct ib_qp_init_attr qp_attr;
  558. if (addr == NULL)
  559. return -EINVAL;
  560. /* Parse the transport specific mount options */
  561. err = parse_opts(args, &opts);
  562. if (err < 0)
  563. return err;
  564. /* Create and initialize the RDMA transport structure */
  565. rdma = alloc_rdma(&opts);
  566. if (!rdma)
  567. return -ENOMEM;
  568. /* Create the RDMA CM ID */
  569. rdma->cm_id = rdma_create_id(&init_net, p9_cm_event_handler, client,
  570. RDMA_PS_TCP, IB_QPT_RC);
  571. if (IS_ERR(rdma->cm_id))
  572. goto error;
  573. /* Associate the client with the transport */
  574. client->trans = rdma;
  575. /* Bind to a privileged port if we need to */
  576. if (opts.privport) {
  577. err = p9_rdma_bind_privport(rdma);
  578. if (err < 0) {
  579. pr_err("%s (%d): problem binding to privport: %d\n",
  580. __func__, task_pid_nr(current), -err);
  581. goto error;
  582. }
  583. }
  584. /* Resolve the server's address */
  585. rdma->addr.sin_family = AF_INET;
  586. rdma->addr.sin_addr.s_addr = in_aton(addr);
  587. rdma->addr.sin_port = htons(opts.port);
  588. err = rdma_resolve_addr(rdma->cm_id, NULL,
  589. (struct sockaddr *)&rdma->addr,
  590. rdma->timeout);
  591. if (err)
  592. goto error;
  593. err = wait_for_completion_interruptible(&rdma->cm_done);
  594. if (err || (rdma->state != P9_RDMA_ADDR_RESOLVED))
  595. goto error;
  596. /* Resolve the route to the server */
  597. err = rdma_resolve_route(rdma->cm_id, rdma->timeout);
  598. if (err)
  599. goto error;
  600. err = wait_for_completion_interruptible(&rdma->cm_done);
  601. if (err || (rdma->state != P9_RDMA_ROUTE_RESOLVED))
  602. goto error;
  603. /* Create the Completion Queue */
  604. rdma->cq = ib_alloc_cq_any(rdma->cm_id->device, client,
  605. opts.sq_depth + opts.rq_depth + 1,
  606. IB_POLL_SOFTIRQ);
  607. if (IS_ERR(rdma->cq))
  608. goto error;
  609. /* Create the Protection Domain */
  610. rdma->pd = ib_alloc_pd(rdma->cm_id->device, 0);
  611. if (IS_ERR(rdma->pd))
  612. goto error;
  613. /* Create the Queue Pair */
  614. memset(&qp_attr, 0, sizeof qp_attr);
  615. qp_attr.event_handler = qp_event_handler;
  616. qp_attr.qp_context = client;
  617. qp_attr.cap.max_send_wr = opts.sq_depth;
  618. qp_attr.cap.max_recv_wr = opts.rq_depth;
  619. qp_attr.cap.max_send_sge = P9_RDMA_SEND_SGE;
  620. qp_attr.cap.max_recv_sge = P9_RDMA_RECV_SGE;
  621. qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  622. qp_attr.qp_type = IB_QPT_RC;
  623. qp_attr.send_cq = rdma->cq;
  624. qp_attr.recv_cq = rdma->cq;
  625. err = rdma_create_qp(rdma->cm_id, rdma->pd, &qp_attr);
  626. if (err)
  627. goto error;
  628. rdma->qp = rdma->cm_id->qp;
  629. /* Request a connection */
  630. memset(&conn_param, 0, sizeof(conn_param));
  631. conn_param.private_data = NULL;
  632. conn_param.private_data_len = 0;
  633. conn_param.responder_resources = P9_RDMA_IRD;
  634. conn_param.initiator_depth = P9_RDMA_ORD;
  635. err = rdma_connect(rdma->cm_id, &conn_param);
  636. if (err)
  637. goto error;
  638. err = wait_for_completion_interruptible(&rdma->cm_done);
  639. if (err || (rdma->state != P9_RDMA_CONNECTED))
  640. goto error;
  641. client->status = Connected;
  642. return 0;
  643. error:
  644. rdma_destroy_trans(rdma);
  645. return -ENOTCONN;
  646. }
  647. static struct p9_trans_module p9_rdma_trans = {
  648. .name = "rdma",
  649. .maxsize = P9_RDMA_MAXSIZE,
  650. .def = 0,
  651. .owner = THIS_MODULE,
  652. .create = rdma_create_trans,
  653. .close = rdma_close,
  654. .request = rdma_request,
  655. .cancel = rdma_cancel,
  656. .cancelled = rdma_cancelled,
  657. .show_options = p9_rdma_show_options,
  658. };
  659. /**
  660. * p9_trans_rdma_init - Register the 9P RDMA transport driver
  661. */
  662. static int __init p9_trans_rdma_init(void)
  663. {
  664. v9fs_register_trans(&p9_rdma_trans);
  665. return 0;
  666. }
  667. static void __exit p9_trans_rdma_exit(void)
  668. {
  669. v9fs_unregister_trans(&p9_rdma_trans);
  670. }
  671. module_init(p9_trans_rdma_init);
  672. module_exit(p9_trans_rdma_exit);
  673. MODULE_AUTHOR("Tom Tucker <tom@opengridcomputing.com>");
  674. MODULE_DESCRIPTION("RDMA Transport for 9P");
  675. MODULE_LICENSE("Dual BSD/GPL");