ucma.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. /*
  2. * Copyright (c) 2005-2006 Intel Corporation. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/completion.h>
  33. #include <linux/mutex.h>
  34. #include <linux/poll.h>
  35. #include <linux/idr.h>
  36. #include <linux/in.h>
  37. #include <linux/in6.h>
  38. #include <linux/miscdevice.h>
  39. #include <rdma/rdma_user_cm.h>
  40. #include <rdma/ib_marshall.h>
  41. #include <rdma/rdma_cm.h>
  42. MODULE_AUTHOR("Sean Hefty");
  43. MODULE_DESCRIPTION("RDMA Userspace Connection Manager Access");
  44. MODULE_LICENSE("Dual BSD/GPL");
  45. enum {
  46. UCMA_MAX_BACKLOG = 128
  47. };
  48. struct ucma_file {
  49. struct mutex mut;
  50. struct file *filp;
  51. struct list_head ctx_list;
  52. struct list_head event_list;
  53. wait_queue_head_t poll_wait;
  54. };
  55. struct ucma_context {
  56. int id;
  57. struct completion comp;
  58. atomic_t ref;
  59. int events_reported;
  60. int backlog;
  61. struct ucma_file *file;
  62. struct rdma_cm_id *cm_id;
  63. u64 uid;
  64. struct list_head list;
  65. };
  66. struct ucma_event {
  67. struct ucma_context *ctx;
  68. struct list_head list;
  69. struct rdma_cm_id *cm_id;
  70. struct rdma_ucm_event_resp resp;
  71. };
  72. static DEFINE_MUTEX(mut);
  73. static DEFINE_IDR(ctx_idr);
  74. static inline struct ucma_context *_ucma_find_context(int id,
  75. struct ucma_file *file)
  76. {
  77. struct ucma_context *ctx;
  78. ctx = idr_find(&ctx_idr, id);
  79. if (!ctx)
  80. ctx = ERR_PTR(-ENOENT);
  81. else if (ctx->file != file)
  82. ctx = ERR_PTR(-EINVAL);
  83. return ctx;
  84. }
  85. static struct ucma_context *ucma_get_ctx(struct ucma_file *file, int id)
  86. {
  87. struct ucma_context *ctx;
  88. mutex_lock(&mut);
  89. ctx = _ucma_find_context(id, file);
  90. if (!IS_ERR(ctx))
  91. atomic_inc(&ctx->ref);
  92. mutex_unlock(&mut);
  93. return ctx;
  94. }
  95. static void ucma_put_ctx(struct ucma_context *ctx)
  96. {
  97. if (atomic_dec_and_test(&ctx->ref))
  98. complete(&ctx->comp);
  99. }
  100. static struct ucma_context *ucma_alloc_ctx(struct ucma_file *file)
  101. {
  102. struct ucma_context *ctx;
  103. int ret;
  104. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  105. if (!ctx)
  106. return NULL;
  107. atomic_set(&ctx->ref, 1);
  108. init_completion(&ctx->comp);
  109. ctx->file = file;
  110. do {
  111. ret = idr_pre_get(&ctx_idr, GFP_KERNEL);
  112. if (!ret)
  113. goto error;
  114. mutex_lock(&mut);
  115. ret = idr_get_new(&ctx_idr, ctx, &ctx->id);
  116. mutex_unlock(&mut);
  117. } while (ret == -EAGAIN);
  118. if (ret)
  119. goto error;
  120. list_add_tail(&ctx->list, &file->ctx_list);
  121. return ctx;
  122. error:
  123. kfree(ctx);
  124. return NULL;
  125. }
  126. static void ucma_copy_conn_event(struct rdma_ucm_conn_param *dst,
  127. struct rdma_conn_param *src)
  128. {
  129. if (src->private_data_len)
  130. memcpy(dst->private_data, src->private_data,
  131. src->private_data_len);
  132. dst->private_data_len = src->private_data_len;
  133. dst->responder_resources =src->responder_resources;
  134. dst->initiator_depth = src->initiator_depth;
  135. dst->flow_control = src->flow_control;
  136. dst->retry_count = src->retry_count;
  137. dst->rnr_retry_count = src->rnr_retry_count;
  138. dst->srq = src->srq;
  139. dst->qp_num = src->qp_num;
  140. }
  141. static void ucma_copy_ud_event(struct rdma_ucm_ud_param *dst,
  142. struct rdma_ud_param *src)
  143. {
  144. if (src->private_data_len)
  145. memcpy(dst->private_data, src->private_data,
  146. src->private_data_len);
  147. dst->private_data_len = src->private_data_len;
  148. ib_copy_ah_attr_to_user(&dst->ah_attr, &src->ah_attr);
  149. dst->qp_num = src->qp_num;
  150. dst->qkey = src->qkey;
  151. }
  152. static void ucma_set_event_context(struct ucma_context *ctx,
  153. struct rdma_cm_event *event,
  154. struct ucma_event *uevent)
  155. {
  156. uevent->ctx = ctx;
  157. uevent->resp.uid = ctx->uid;
  158. uevent->resp.id = ctx->id;
  159. }
  160. static int ucma_event_handler(struct rdma_cm_id *cm_id,
  161. struct rdma_cm_event *event)
  162. {
  163. struct ucma_event *uevent;
  164. struct ucma_context *ctx = cm_id->context;
  165. int ret = 0;
  166. uevent = kzalloc(sizeof(*uevent), GFP_KERNEL);
  167. if (!uevent)
  168. return event->event == RDMA_CM_EVENT_CONNECT_REQUEST;
  169. uevent->cm_id = cm_id;
  170. ucma_set_event_context(ctx, event, uevent);
  171. uevent->resp.event = event->event;
  172. uevent->resp.status = event->status;
  173. if (cm_id->ps == RDMA_PS_UDP)
  174. ucma_copy_ud_event(&uevent->resp.param.ud, &event->param.ud);
  175. else
  176. ucma_copy_conn_event(&uevent->resp.param.conn,
  177. &event->param.conn);
  178. mutex_lock(&ctx->file->mut);
  179. if (event->event == RDMA_CM_EVENT_CONNECT_REQUEST) {
  180. if (!ctx->backlog) {
  181. ret = -EDQUOT;
  182. kfree(uevent);
  183. goto out;
  184. }
  185. ctx->backlog--;
  186. } else if (!ctx->uid) {
  187. /*
  188. * We ignore events for new connections until userspace has set
  189. * their context. This can only happen if an error occurs on a
  190. * new connection before the user accepts it. This is okay,
  191. * since the accept will just fail later.
  192. */
  193. kfree(uevent);
  194. goto out;
  195. }
  196. list_add_tail(&uevent->list, &ctx->file->event_list);
  197. wake_up_interruptible(&ctx->file->poll_wait);
  198. out:
  199. mutex_unlock(&ctx->file->mut);
  200. return ret;
  201. }
  202. static ssize_t ucma_get_event(struct ucma_file *file, const char __user *inbuf,
  203. int in_len, int out_len)
  204. {
  205. struct ucma_context *ctx;
  206. struct rdma_ucm_get_event cmd;
  207. struct ucma_event *uevent;
  208. int ret = 0;
  209. DEFINE_WAIT(wait);
  210. if (out_len < sizeof uevent->resp)
  211. return -ENOSPC;
  212. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  213. return -EFAULT;
  214. mutex_lock(&file->mut);
  215. while (list_empty(&file->event_list)) {
  216. if (file->filp->f_flags & O_NONBLOCK) {
  217. ret = -EAGAIN;
  218. break;
  219. }
  220. if (signal_pending(current)) {
  221. ret = -ERESTARTSYS;
  222. break;
  223. }
  224. prepare_to_wait(&file->poll_wait, &wait, TASK_INTERRUPTIBLE);
  225. mutex_unlock(&file->mut);
  226. schedule();
  227. mutex_lock(&file->mut);
  228. finish_wait(&file->poll_wait, &wait);
  229. }
  230. if (ret)
  231. goto done;
  232. uevent = list_entry(file->event_list.next, struct ucma_event, list);
  233. if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST) {
  234. ctx = ucma_alloc_ctx(file);
  235. if (!ctx) {
  236. ret = -ENOMEM;
  237. goto done;
  238. }
  239. uevent->ctx->backlog++;
  240. ctx->cm_id = uevent->cm_id;
  241. ctx->cm_id->context = ctx;
  242. uevent->resp.id = ctx->id;
  243. }
  244. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  245. &uevent->resp, sizeof uevent->resp)) {
  246. ret = -EFAULT;
  247. goto done;
  248. }
  249. list_del(&uevent->list);
  250. uevent->ctx->events_reported++;
  251. kfree(uevent);
  252. done:
  253. mutex_unlock(&file->mut);
  254. return ret;
  255. }
  256. static ssize_t ucma_create_id(struct ucma_file *file,
  257. const char __user *inbuf,
  258. int in_len, int out_len)
  259. {
  260. struct rdma_ucm_create_id cmd;
  261. struct rdma_ucm_create_id_resp resp;
  262. struct ucma_context *ctx;
  263. int ret;
  264. if (out_len < sizeof(resp))
  265. return -ENOSPC;
  266. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  267. return -EFAULT;
  268. mutex_lock(&file->mut);
  269. ctx = ucma_alloc_ctx(file);
  270. mutex_unlock(&file->mut);
  271. if (!ctx)
  272. return -ENOMEM;
  273. ctx->uid = cmd.uid;
  274. ctx->cm_id = rdma_create_id(ucma_event_handler, ctx, cmd.ps);
  275. if (IS_ERR(ctx->cm_id)) {
  276. ret = PTR_ERR(ctx->cm_id);
  277. goto err1;
  278. }
  279. resp.id = ctx->id;
  280. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  281. &resp, sizeof(resp))) {
  282. ret = -EFAULT;
  283. goto err2;
  284. }
  285. return 0;
  286. err2:
  287. rdma_destroy_id(ctx->cm_id);
  288. err1:
  289. mutex_lock(&mut);
  290. idr_remove(&ctx_idr, ctx->id);
  291. mutex_unlock(&mut);
  292. kfree(ctx);
  293. return ret;
  294. }
  295. static void ucma_cleanup_events(struct ucma_context *ctx)
  296. {
  297. struct ucma_event *uevent, *tmp;
  298. list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list) {
  299. if (uevent->ctx != ctx)
  300. continue;
  301. list_del(&uevent->list);
  302. /* clear incoming connections. */
  303. if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST)
  304. rdma_destroy_id(uevent->cm_id);
  305. kfree(uevent);
  306. }
  307. }
  308. static int ucma_free_ctx(struct ucma_context *ctx)
  309. {
  310. int events_reported;
  311. /* No new events will be generated after destroying the id. */
  312. rdma_destroy_id(ctx->cm_id);
  313. /* Cleanup events not yet reported to the user. */
  314. mutex_lock(&ctx->file->mut);
  315. ucma_cleanup_events(ctx);
  316. list_del(&ctx->list);
  317. mutex_unlock(&ctx->file->mut);
  318. events_reported = ctx->events_reported;
  319. kfree(ctx);
  320. return events_reported;
  321. }
  322. static ssize_t ucma_destroy_id(struct ucma_file *file, const char __user *inbuf,
  323. int in_len, int out_len)
  324. {
  325. struct rdma_ucm_destroy_id cmd;
  326. struct rdma_ucm_destroy_id_resp resp;
  327. struct ucma_context *ctx;
  328. int ret = 0;
  329. if (out_len < sizeof(resp))
  330. return -ENOSPC;
  331. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  332. return -EFAULT;
  333. mutex_lock(&mut);
  334. ctx = _ucma_find_context(cmd.id, file);
  335. if (!IS_ERR(ctx))
  336. idr_remove(&ctx_idr, ctx->id);
  337. mutex_unlock(&mut);
  338. if (IS_ERR(ctx))
  339. return PTR_ERR(ctx);
  340. ucma_put_ctx(ctx);
  341. wait_for_completion(&ctx->comp);
  342. resp.events_reported = ucma_free_ctx(ctx);
  343. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  344. &resp, sizeof(resp)))
  345. ret = -EFAULT;
  346. return ret;
  347. }
  348. static ssize_t ucma_bind_addr(struct ucma_file *file, const char __user *inbuf,
  349. int in_len, int out_len)
  350. {
  351. struct rdma_ucm_bind_addr cmd;
  352. struct ucma_context *ctx;
  353. int ret;
  354. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  355. return -EFAULT;
  356. ctx = ucma_get_ctx(file, cmd.id);
  357. if (IS_ERR(ctx))
  358. return PTR_ERR(ctx);
  359. ret = rdma_bind_addr(ctx->cm_id, (struct sockaddr *) &cmd.addr);
  360. ucma_put_ctx(ctx);
  361. return ret;
  362. }
  363. static ssize_t ucma_resolve_addr(struct ucma_file *file,
  364. const char __user *inbuf,
  365. int in_len, int out_len)
  366. {
  367. struct rdma_ucm_resolve_addr cmd;
  368. struct ucma_context *ctx;
  369. int ret;
  370. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  371. return -EFAULT;
  372. ctx = ucma_get_ctx(file, cmd.id);
  373. if (IS_ERR(ctx))
  374. return PTR_ERR(ctx);
  375. ret = rdma_resolve_addr(ctx->cm_id, (struct sockaddr *) &cmd.src_addr,
  376. (struct sockaddr *) &cmd.dst_addr,
  377. cmd.timeout_ms);
  378. ucma_put_ctx(ctx);
  379. return ret;
  380. }
  381. static ssize_t ucma_resolve_route(struct ucma_file *file,
  382. const char __user *inbuf,
  383. int in_len, int out_len)
  384. {
  385. struct rdma_ucm_resolve_route cmd;
  386. struct ucma_context *ctx;
  387. int ret;
  388. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  389. return -EFAULT;
  390. ctx = ucma_get_ctx(file, cmd.id);
  391. if (IS_ERR(ctx))
  392. return PTR_ERR(ctx);
  393. ret = rdma_resolve_route(ctx->cm_id, cmd.timeout_ms);
  394. ucma_put_ctx(ctx);
  395. return ret;
  396. }
  397. static void ucma_copy_ib_route(struct rdma_ucm_query_route_resp *resp,
  398. struct rdma_route *route)
  399. {
  400. struct rdma_dev_addr *dev_addr;
  401. resp->num_paths = route->num_paths;
  402. switch (route->num_paths) {
  403. case 0:
  404. dev_addr = &route->addr.dev_addr;
  405. ib_addr_get_dgid(dev_addr,
  406. (union ib_gid *) &resp->ib_route[0].dgid);
  407. ib_addr_get_sgid(dev_addr,
  408. (union ib_gid *) &resp->ib_route[0].sgid);
  409. resp->ib_route[0].pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
  410. break;
  411. case 2:
  412. ib_copy_path_rec_to_user(&resp->ib_route[1],
  413. &route->path_rec[1]);
  414. /* fall through */
  415. case 1:
  416. ib_copy_path_rec_to_user(&resp->ib_route[0],
  417. &route->path_rec[0]);
  418. break;
  419. default:
  420. break;
  421. }
  422. }
  423. static ssize_t ucma_query_route(struct ucma_file *file,
  424. const char __user *inbuf,
  425. int in_len, int out_len)
  426. {
  427. struct rdma_ucm_query_route cmd;
  428. struct rdma_ucm_query_route_resp resp;
  429. struct ucma_context *ctx;
  430. struct sockaddr *addr;
  431. int ret = 0;
  432. if (out_len < sizeof(resp))
  433. return -ENOSPC;
  434. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  435. return -EFAULT;
  436. ctx = ucma_get_ctx(file, cmd.id);
  437. if (IS_ERR(ctx))
  438. return PTR_ERR(ctx);
  439. memset(&resp, 0, sizeof resp);
  440. addr = &ctx->cm_id->route.addr.src_addr;
  441. memcpy(&resp.src_addr, addr, addr->sa_family == AF_INET ?
  442. sizeof(struct sockaddr_in) :
  443. sizeof(struct sockaddr_in6));
  444. addr = &ctx->cm_id->route.addr.dst_addr;
  445. memcpy(&resp.dst_addr, addr, addr->sa_family == AF_INET ?
  446. sizeof(struct sockaddr_in) :
  447. sizeof(struct sockaddr_in6));
  448. if (!ctx->cm_id->device)
  449. goto out;
  450. resp.node_guid = ctx->cm_id->device->node_guid;
  451. resp.port_num = ctx->cm_id->port_num;
  452. switch (rdma_node_get_transport(ctx->cm_id->device->node_type)) {
  453. case RDMA_TRANSPORT_IB:
  454. ucma_copy_ib_route(&resp, &ctx->cm_id->route);
  455. break;
  456. default:
  457. break;
  458. }
  459. out:
  460. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  461. &resp, sizeof(resp)))
  462. ret = -EFAULT;
  463. ucma_put_ctx(ctx);
  464. return ret;
  465. }
  466. static void ucma_copy_conn_param(struct rdma_conn_param *dst,
  467. struct rdma_ucm_conn_param *src)
  468. {
  469. dst->private_data = src->private_data;
  470. dst->private_data_len = src->private_data_len;
  471. dst->responder_resources =src->responder_resources;
  472. dst->initiator_depth = src->initiator_depth;
  473. dst->flow_control = src->flow_control;
  474. dst->retry_count = src->retry_count;
  475. dst->rnr_retry_count = src->rnr_retry_count;
  476. dst->srq = src->srq;
  477. dst->qp_num = src->qp_num;
  478. }
  479. static ssize_t ucma_connect(struct ucma_file *file, const char __user *inbuf,
  480. int in_len, int out_len)
  481. {
  482. struct rdma_ucm_connect cmd;
  483. struct rdma_conn_param conn_param;
  484. struct ucma_context *ctx;
  485. int ret;
  486. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  487. return -EFAULT;
  488. if (!cmd.conn_param.valid)
  489. return -EINVAL;
  490. ctx = ucma_get_ctx(file, cmd.id);
  491. if (IS_ERR(ctx))
  492. return PTR_ERR(ctx);
  493. ucma_copy_conn_param(&conn_param, &cmd.conn_param);
  494. ret = rdma_connect(ctx->cm_id, &conn_param);
  495. ucma_put_ctx(ctx);
  496. return ret;
  497. }
  498. static ssize_t ucma_listen(struct ucma_file *file, const char __user *inbuf,
  499. int in_len, int out_len)
  500. {
  501. struct rdma_ucm_listen cmd;
  502. struct ucma_context *ctx;
  503. int ret;
  504. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  505. return -EFAULT;
  506. ctx = ucma_get_ctx(file, cmd.id);
  507. if (IS_ERR(ctx))
  508. return PTR_ERR(ctx);
  509. ctx->backlog = cmd.backlog > 0 && cmd.backlog < UCMA_MAX_BACKLOG ?
  510. cmd.backlog : UCMA_MAX_BACKLOG;
  511. ret = rdma_listen(ctx->cm_id, ctx->backlog);
  512. ucma_put_ctx(ctx);
  513. return ret;
  514. }
  515. static ssize_t ucma_accept(struct ucma_file *file, const char __user *inbuf,
  516. int in_len, int out_len)
  517. {
  518. struct rdma_ucm_accept cmd;
  519. struct rdma_conn_param conn_param;
  520. struct ucma_context *ctx;
  521. int ret;
  522. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  523. return -EFAULT;
  524. ctx = ucma_get_ctx(file, cmd.id);
  525. if (IS_ERR(ctx))
  526. return PTR_ERR(ctx);
  527. if (cmd.conn_param.valid) {
  528. ctx->uid = cmd.uid;
  529. ucma_copy_conn_param(&conn_param, &cmd.conn_param);
  530. ret = rdma_accept(ctx->cm_id, &conn_param);
  531. } else
  532. ret = rdma_accept(ctx->cm_id, NULL);
  533. ucma_put_ctx(ctx);
  534. return ret;
  535. }
  536. static ssize_t ucma_reject(struct ucma_file *file, const char __user *inbuf,
  537. int in_len, int out_len)
  538. {
  539. struct rdma_ucm_reject cmd;
  540. struct ucma_context *ctx;
  541. int ret;
  542. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  543. return -EFAULT;
  544. ctx = ucma_get_ctx(file, cmd.id);
  545. if (IS_ERR(ctx))
  546. return PTR_ERR(ctx);
  547. ret = rdma_reject(ctx->cm_id, cmd.private_data, cmd.private_data_len);
  548. ucma_put_ctx(ctx);
  549. return ret;
  550. }
  551. static ssize_t ucma_disconnect(struct ucma_file *file, const char __user *inbuf,
  552. int in_len, int out_len)
  553. {
  554. struct rdma_ucm_disconnect cmd;
  555. struct ucma_context *ctx;
  556. int ret;
  557. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  558. return -EFAULT;
  559. ctx = ucma_get_ctx(file, cmd.id);
  560. if (IS_ERR(ctx))
  561. return PTR_ERR(ctx);
  562. ret = rdma_disconnect(ctx->cm_id);
  563. ucma_put_ctx(ctx);
  564. return ret;
  565. }
  566. static ssize_t ucma_init_qp_attr(struct ucma_file *file,
  567. const char __user *inbuf,
  568. int in_len, int out_len)
  569. {
  570. struct rdma_ucm_init_qp_attr cmd;
  571. struct ib_uverbs_qp_attr resp;
  572. struct ucma_context *ctx;
  573. struct ib_qp_attr qp_attr;
  574. int ret;
  575. if (out_len < sizeof(resp))
  576. return -ENOSPC;
  577. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  578. return -EFAULT;
  579. ctx = ucma_get_ctx(file, cmd.id);
  580. if (IS_ERR(ctx))
  581. return PTR_ERR(ctx);
  582. resp.qp_attr_mask = 0;
  583. memset(&qp_attr, 0, sizeof qp_attr);
  584. qp_attr.qp_state = cmd.qp_state;
  585. ret = rdma_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
  586. if (ret)
  587. goto out;
  588. ib_copy_qp_attr_to_user(&resp, &qp_attr);
  589. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  590. &resp, sizeof(resp)))
  591. ret = -EFAULT;
  592. out:
  593. ucma_put_ctx(ctx);
  594. return ret;
  595. }
  596. static ssize_t ucma_notify(struct ucma_file *file, const char __user *inbuf,
  597. int in_len, int out_len)
  598. {
  599. struct rdma_ucm_notify cmd;
  600. struct ucma_context *ctx;
  601. int ret;
  602. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  603. return -EFAULT;
  604. ctx = ucma_get_ctx(file, cmd.id);
  605. if (IS_ERR(ctx))
  606. return PTR_ERR(ctx);
  607. ret = rdma_notify(ctx->cm_id, (enum ib_event_type) cmd.event);
  608. ucma_put_ctx(ctx);
  609. return ret;
  610. }
  611. static ssize_t (*ucma_cmd_table[])(struct ucma_file *file,
  612. const char __user *inbuf,
  613. int in_len, int out_len) = {
  614. [RDMA_USER_CM_CMD_CREATE_ID] = ucma_create_id,
  615. [RDMA_USER_CM_CMD_DESTROY_ID] = ucma_destroy_id,
  616. [RDMA_USER_CM_CMD_BIND_ADDR] = ucma_bind_addr,
  617. [RDMA_USER_CM_CMD_RESOLVE_ADDR] = ucma_resolve_addr,
  618. [RDMA_USER_CM_CMD_RESOLVE_ROUTE]= ucma_resolve_route,
  619. [RDMA_USER_CM_CMD_QUERY_ROUTE] = ucma_query_route,
  620. [RDMA_USER_CM_CMD_CONNECT] = ucma_connect,
  621. [RDMA_USER_CM_CMD_LISTEN] = ucma_listen,
  622. [RDMA_USER_CM_CMD_ACCEPT] = ucma_accept,
  623. [RDMA_USER_CM_CMD_REJECT] = ucma_reject,
  624. [RDMA_USER_CM_CMD_DISCONNECT] = ucma_disconnect,
  625. [RDMA_USER_CM_CMD_INIT_QP_ATTR] = ucma_init_qp_attr,
  626. [RDMA_USER_CM_CMD_GET_EVENT] = ucma_get_event,
  627. [RDMA_USER_CM_CMD_GET_OPTION] = NULL,
  628. [RDMA_USER_CM_CMD_SET_OPTION] = NULL,
  629. [RDMA_USER_CM_CMD_NOTIFY] = ucma_notify,
  630. };
  631. static ssize_t ucma_write(struct file *filp, const char __user *buf,
  632. size_t len, loff_t *pos)
  633. {
  634. struct ucma_file *file = filp->private_data;
  635. struct rdma_ucm_cmd_hdr hdr;
  636. ssize_t ret;
  637. if (len < sizeof(hdr))
  638. return -EINVAL;
  639. if (copy_from_user(&hdr, buf, sizeof(hdr)))
  640. return -EFAULT;
  641. if (hdr.cmd < 0 || hdr.cmd >= ARRAY_SIZE(ucma_cmd_table))
  642. return -EINVAL;
  643. if (hdr.in + sizeof(hdr) > len)
  644. return -EINVAL;
  645. if (!ucma_cmd_table[hdr.cmd])
  646. return -ENOSYS;
  647. ret = ucma_cmd_table[hdr.cmd](file, buf + sizeof(hdr), hdr.in, hdr.out);
  648. if (!ret)
  649. ret = len;
  650. return ret;
  651. }
  652. static unsigned int ucma_poll(struct file *filp, struct poll_table_struct *wait)
  653. {
  654. struct ucma_file *file = filp->private_data;
  655. unsigned int mask = 0;
  656. poll_wait(filp, &file->poll_wait, wait);
  657. if (!list_empty(&file->event_list))
  658. mask = POLLIN | POLLRDNORM;
  659. return mask;
  660. }
  661. static int ucma_open(struct inode *inode, struct file *filp)
  662. {
  663. struct ucma_file *file;
  664. file = kmalloc(sizeof *file, GFP_KERNEL);
  665. if (!file)
  666. return -ENOMEM;
  667. INIT_LIST_HEAD(&file->event_list);
  668. INIT_LIST_HEAD(&file->ctx_list);
  669. init_waitqueue_head(&file->poll_wait);
  670. mutex_init(&file->mut);
  671. filp->private_data = file;
  672. file->filp = filp;
  673. return 0;
  674. }
  675. static int ucma_close(struct inode *inode, struct file *filp)
  676. {
  677. struct ucma_file *file = filp->private_data;
  678. struct ucma_context *ctx, *tmp;
  679. mutex_lock(&file->mut);
  680. list_for_each_entry_safe(ctx, tmp, &file->ctx_list, list) {
  681. mutex_unlock(&file->mut);
  682. mutex_lock(&mut);
  683. idr_remove(&ctx_idr, ctx->id);
  684. mutex_unlock(&mut);
  685. ucma_free_ctx(ctx);
  686. mutex_lock(&file->mut);
  687. }
  688. mutex_unlock(&file->mut);
  689. kfree(file);
  690. return 0;
  691. }
  692. static struct file_operations ucma_fops = {
  693. .owner = THIS_MODULE,
  694. .open = ucma_open,
  695. .release = ucma_close,
  696. .write = ucma_write,
  697. .poll = ucma_poll,
  698. };
  699. static struct miscdevice ucma_misc = {
  700. .minor = MISC_DYNAMIC_MINOR,
  701. .name = "rdma_cm",
  702. .fops = &ucma_fops,
  703. };
  704. static ssize_t show_abi_version(struct device *dev,
  705. struct device_attribute *attr,
  706. char *buf)
  707. {
  708. return sprintf(buf, "%d\n", RDMA_USER_CM_ABI_VERSION);
  709. }
  710. static DEVICE_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  711. static int __init ucma_init(void)
  712. {
  713. int ret;
  714. ret = misc_register(&ucma_misc);
  715. if (ret)
  716. return ret;
  717. ret = device_create_file(ucma_misc.this_device, &dev_attr_abi_version);
  718. if (ret) {
  719. printk(KERN_ERR "rdma_ucm: couldn't create abi_version attr\n");
  720. goto err;
  721. }
  722. return 0;
  723. err:
  724. misc_deregister(&ucma_misc);
  725. return ret;
  726. }
  727. static void __exit ucma_cleanup(void)
  728. {
  729. device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
  730. misc_deregister(&ucma_misc);
  731. idr_destroy(&ctx_idr);
  732. }
  733. module_init(ucma_init);
  734. module_exit(ucma_cleanup);