ucma.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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. goto out;
  183. }
  184. ctx->backlog--;
  185. }
  186. list_add_tail(&uevent->list, &ctx->file->event_list);
  187. wake_up_interruptible(&ctx->file->poll_wait);
  188. out:
  189. mutex_unlock(&ctx->file->mut);
  190. return ret;
  191. }
  192. static ssize_t ucma_get_event(struct ucma_file *file, const char __user *inbuf,
  193. int in_len, int out_len)
  194. {
  195. struct ucma_context *ctx;
  196. struct rdma_ucm_get_event cmd;
  197. struct ucma_event *uevent;
  198. int ret = 0;
  199. DEFINE_WAIT(wait);
  200. if (out_len < sizeof uevent->resp)
  201. return -ENOSPC;
  202. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  203. return -EFAULT;
  204. mutex_lock(&file->mut);
  205. while (list_empty(&file->event_list)) {
  206. if (file->filp->f_flags & O_NONBLOCK) {
  207. ret = -EAGAIN;
  208. break;
  209. }
  210. if (signal_pending(current)) {
  211. ret = -ERESTARTSYS;
  212. break;
  213. }
  214. prepare_to_wait(&file->poll_wait, &wait, TASK_INTERRUPTIBLE);
  215. mutex_unlock(&file->mut);
  216. schedule();
  217. mutex_lock(&file->mut);
  218. finish_wait(&file->poll_wait, &wait);
  219. }
  220. if (ret)
  221. goto done;
  222. uevent = list_entry(file->event_list.next, struct ucma_event, list);
  223. if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST) {
  224. ctx = ucma_alloc_ctx(file);
  225. if (!ctx) {
  226. ret = -ENOMEM;
  227. goto done;
  228. }
  229. uevent->ctx->backlog++;
  230. ctx->cm_id = uevent->cm_id;
  231. ctx->cm_id->context = ctx;
  232. uevent->resp.id = ctx->id;
  233. }
  234. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  235. &uevent->resp, sizeof uevent->resp)) {
  236. ret = -EFAULT;
  237. goto done;
  238. }
  239. list_del(&uevent->list);
  240. uevent->ctx->events_reported++;
  241. kfree(uevent);
  242. done:
  243. mutex_unlock(&file->mut);
  244. return ret;
  245. }
  246. static ssize_t ucma_create_id(struct ucma_file *file,
  247. const char __user *inbuf,
  248. int in_len, int out_len)
  249. {
  250. struct rdma_ucm_create_id cmd;
  251. struct rdma_ucm_create_id_resp resp;
  252. struct ucma_context *ctx;
  253. int ret;
  254. if (out_len < sizeof(resp))
  255. return -ENOSPC;
  256. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  257. return -EFAULT;
  258. mutex_lock(&file->mut);
  259. ctx = ucma_alloc_ctx(file);
  260. mutex_unlock(&file->mut);
  261. if (!ctx)
  262. return -ENOMEM;
  263. ctx->uid = cmd.uid;
  264. ctx->cm_id = rdma_create_id(ucma_event_handler, ctx, cmd.ps);
  265. if (IS_ERR(ctx->cm_id)) {
  266. ret = PTR_ERR(ctx->cm_id);
  267. goto err1;
  268. }
  269. resp.id = ctx->id;
  270. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  271. &resp, sizeof(resp))) {
  272. ret = -EFAULT;
  273. goto err2;
  274. }
  275. return 0;
  276. err2:
  277. rdma_destroy_id(ctx->cm_id);
  278. err1:
  279. mutex_lock(&mut);
  280. idr_remove(&ctx_idr, ctx->id);
  281. mutex_unlock(&mut);
  282. kfree(ctx);
  283. return ret;
  284. }
  285. static void ucma_cleanup_events(struct ucma_context *ctx)
  286. {
  287. struct ucma_event *uevent, *tmp;
  288. list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list) {
  289. if (uevent->ctx != ctx)
  290. continue;
  291. list_del(&uevent->list);
  292. /* clear incoming connections. */
  293. if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST)
  294. rdma_destroy_id(uevent->cm_id);
  295. kfree(uevent);
  296. }
  297. }
  298. static int ucma_free_ctx(struct ucma_context *ctx)
  299. {
  300. int events_reported;
  301. /* No new events will be generated after destroying the id. */
  302. rdma_destroy_id(ctx->cm_id);
  303. /* Cleanup events not yet reported to the user. */
  304. mutex_lock(&ctx->file->mut);
  305. ucma_cleanup_events(ctx);
  306. list_del(&ctx->list);
  307. mutex_unlock(&ctx->file->mut);
  308. events_reported = ctx->events_reported;
  309. kfree(ctx);
  310. return events_reported;
  311. }
  312. static ssize_t ucma_destroy_id(struct ucma_file *file, const char __user *inbuf,
  313. int in_len, int out_len)
  314. {
  315. struct rdma_ucm_destroy_id cmd;
  316. struct rdma_ucm_destroy_id_resp resp;
  317. struct ucma_context *ctx;
  318. int ret = 0;
  319. if (out_len < sizeof(resp))
  320. return -ENOSPC;
  321. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  322. return -EFAULT;
  323. mutex_lock(&mut);
  324. ctx = _ucma_find_context(cmd.id, file);
  325. if (!IS_ERR(ctx))
  326. idr_remove(&ctx_idr, ctx->id);
  327. mutex_unlock(&mut);
  328. if (IS_ERR(ctx))
  329. return PTR_ERR(ctx);
  330. ucma_put_ctx(ctx);
  331. wait_for_completion(&ctx->comp);
  332. resp.events_reported = ucma_free_ctx(ctx);
  333. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  334. &resp, sizeof(resp)))
  335. ret = -EFAULT;
  336. return ret;
  337. }
  338. static ssize_t ucma_bind_addr(struct ucma_file *file, const char __user *inbuf,
  339. int in_len, int out_len)
  340. {
  341. struct rdma_ucm_bind_addr cmd;
  342. struct ucma_context *ctx;
  343. int ret;
  344. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  345. return -EFAULT;
  346. ctx = ucma_get_ctx(file, cmd.id);
  347. if (IS_ERR(ctx))
  348. return PTR_ERR(ctx);
  349. ret = rdma_bind_addr(ctx->cm_id, (struct sockaddr *) &cmd.addr);
  350. ucma_put_ctx(ctx);
  351. return ret;
  352. }
  353. static ssize_t ucma_resolve_addr(struct ucma_file *file,
  354. const char __user *inbuf,
  355. int in_len, int out_len)
  356. {
  357. struct rdma_ucm_resolve_addr cmd;
  358. struct ucma_context *ctx;
  359. int ret;
  360. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  361. return -EFAULT;
  362. ctx = ucma_get_ctx(file, cmd.id);
  363. if (IS_ERR(ctx))
  364. return PTR_ERR(ctx);
  365. ret = rdma_resolve_addr(ctx->cm_id, (struct sockaddr *) &cmd.src_addr,
  366. (struct sockaddr *) &cmd.dst_addr,
  367. cmd.timeout_ms);
  368. ucma_put_ctx(ctx);
  369. return ret;
  370. }
  371. static ssize_t ucma_resolve_route(struct ucma_file *file,
  372. const char __user *inbuf,
  373. int in_len, int out_len)
  374. {
  375. struct rdma_ucm_resolve_route cmd;
  376. struct ucma_context *ctx;
  377. int ret;
  378. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  379. return -EFAULT;
  380. ctx = ucma_get_ctx(file, cmd.id);
  381. if (IS_ERR(ctx))
  382. return PTR_ERR(ctx);
  383. ret = rdma_resolve_route(ctx->cm_id, cmd.timeout_ms);
  384. ucma_put_ctx(ctx);
  385. return ret;
  386. }
  387. static void ucma_copy_ib_route(struct rdma_ucm_query_route_resp *resp,
  388. struct rdma_route *route)
  389. {
  390. struct rdma_dev_addr *dev_addr;
  391. resp->num_paths = route->num_paths;
  392. switch (route->num_paths) {
  393. case 0:
  394. dev_addr = &route->addr.dev_addr;
  395. ib_addr_get_dgid(dev_addr,
  396. (union ib_gid *) &resp->ib_route[0].dgid);
  397. ib_addr_get_sgid(dev_addr,
  398. (union ib_gid *) &resp->ib_route[0].sgid);
  399. resp->ib_route[0].pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
  400. break;
  401. case 2:
  402. ib_copy_path_rec_to_user(&resp->ib_route[1],
  403. &route->path_rec[1]);
  404. /* fall through */
  405. case 1:
  406. ib_copy_path_rec_to_user(&resp->ib_route[0],
  407. &route->path_rec[0]);
  408. break;
  409. default:
  410. break;
  411. }
  412. }
  413. static ssize_t ucma_query_route(struct ucma_file *file,
  414. const char __user *inbuf,
  415. int in_len, int out_len)
  416. {
  417. struct rdma_ucm_query_route cmd;
  418. struct rdma_ucm_query_route_resp resp;
  419. struct ucma_context *ctx;
  420. struct sockaddr *addr;
  421. int ret = 0;
  422. if (out_len < sizeof(resp))
  423. return -ENOSPC;
  424. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  425. return -EFAULT;
  426. ctx = ucma_get_ctx(file, cmd.id);
  427. if (IS_ERR(ctx))
  428. return PTR_ERR(ctx);
  429. memset(&resp, 0, sizeof resp);
  430. addr = &ctx->cm_id->route.addr.src_addr;
  431. memcpy(&resp.src_addr, addr, addr->sa_family == AF_INET ?
  432. sizeof(struct sockaddr_in) :
  433. sizeof(struct sockaddr_in6));
  434. addr = &ctx->cm_id->route.addr.dst_addr;
  435. memcpy(&resp.dst_addr, addr, addr->sa_family == AF_INET ?
  436. sizeof(struct sockaddr_in) :
  437. sizeof(struct sockaddr_in6));
  438. if (!ctx->cm_id->device)
  439. goto out;
  440. resp.node_guid = ctx->cm_id->device->node_guid;
  441. resp.port_num = ctx->cm_id->port_num;
  442. switch (rdma_node_get_transport(ctx->cm_id->device->node_type)) {
  443. case RDMA_TRANSPORT_IB:
  444. ucma_copy_ib_route(&resp, &ctx->cm_id->route);
  445. break;
  446. default:
  447. break;
  448. }
  449. out:
  450. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  451. &resp, sizeof(resp)))
  452. ret = -EFAULT;
  453. ucma_put_ctx(ctx);
  454. return ret;
  455. }
  456. static void ucma_copy_conn_param(struct rdma_conn_param *dst,
  457. struct rdma_ucm_conn_param *src)
  458. {
  459. dst->private_data = src->private_data;
  460. dst->private_data_len = src->private_data_len;
  461. dst->responder_resources =src->responder_resources;
  462. dst->initiator_depth = src->initiator_depth;
  463. dst->flow_control = src->flow_control;
  464. dst->retry_count = src->retry_count;
  465. dst->rnr_retry_count = src->rnr_retry_count;
  466. dst->srq = src->srq;
  467. dst->qp_num = src->qp_num;
  468. }
  469. static ssize_t ucma_connect(struct ucma_file *file, const char __user *inbuf,
  470. int in_len, int out_len)
  471. {
  472. struct rdma_ucm_connect cmd;
  473. struct rdma_conn_param conn_param;
  474. struct ucma_context *ctx;
  475. int ret;
  476. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  477. return -EFAULT;
  478. if (!cmd.conn_param.valid)
  479. return -EINVAL;
  480. ctx = ucma_get_ctx(file, cmd.id);
  481. if (IS_ERR(ctx))
  482. return PTR_ERR(ctx);
  483. ucma_copy_conn_param(&conn_param, &cmd.conn_param);
  484. ret = rdma_connect(ctx->cm_id, &conn_param);
  485. ucma_put_ctx(ctx);
  486. return ret;
  487. }
  488. static ssize_t ucma_listen(struct ucma_file *file, const char __user *inbuf,
  489. int in_len, int out_len)
  490. {
  491. struct rdma_ucm_listen cmd;
  492. struct ucma_context *ctx;
  493. int ret;
  494. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  495. return -EFAULT;
  496. ctx = ucma_get_ctx(file, cmd.id);
  497. if (IS_ERR(ctx))
  498. return PTR_ERR(ctx);
  499. ctx->backlog = cmd.backlog > 0 && cmd.backlog < UCMA_MAX_BACKLOG ?
  500. cmd.backlog : UCMA_MAX_BACKLOG;
  501. ret = rdma_listen(ctx->cm_id, ctx->backlog);
  502. ucma_put_ctx(ctx);
  503. return ret;
  504. }
  505. static ssize_t ucma_accept(struct ucma_file *file, const char __user *inbuf,
  506. int in_len, int out_len)
  507. {
  508. struct rdma_ucm_accept cmd;
  509. struct rdma_conn_param conn_param;
  510. struct ucma_context *ctx;
  511. int ret;
  512. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  513. return -EFAULT;
  514. ctx = ucma_get_ctx(file, cmd.id);
  515. if (IS_ERR(ctx))
  516. return PTR_ERR(ctx);
  517. if (cmd.conn_param.valid) {
  518. ctx->uid = cmd.uid;
  519. ucma_copy_conn_param(&conn_param, &cmd.conn_param);
  520. ret = rdma_accept(ctx->cm_id, &conn_param);
  521. } else
  522. ret = rdma_accept(ctx->cm_id, NULL);
  523. ucma_put_ctx(ctx);
  524. return ret;
  525. }
  526. static ssize_t ucma_reject(struct ucma_file *file, const char __user *inbuf,
  527. int in_len, int out_len)
  528. {
  529. struct rdma_ucm_reject cmd;
  530. struct ucma_context *ctx;
  531. int ret;
  532. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  533. return -EFAULT;
  534. ctx = ucma_get_ctx(file, cmd.id);
  535. if (IS_ERR(ctx))
  536. return PTR_ERR(ctx);
  537. ret = rdma_reject(ctx->cm_id, cmd.private_data, cmd.private_data_len);
  538. ucma_put_ctx(ctx);
  539. return ret;
  540. }
  541. static ssize_t ucma_disconnect(struct ucma_file *file, const char __user *inbuf,
  542. int in_len, int out_len)
  543. {
  544. struct rdma_ucm_disconnect cmd;
  545. struct ucma_context *ctx;
  546. int ret;
  547. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  548. return -EFAULT;
  549. ctx = ucma_get_ctx(file, cmd.id);
  550. if (IS_ERR(ctx))
  551. return PTR_ERR(ctx);
  552. ret = rdma_disconnect(ctx->cm_id);
  553. ucma_put_ctx(ctx);
  554. return ret;
  555. }
  556. static ssize_t ucma_init_qp_attr(struct ucma_file *file,
  557. const char __user *inbuf,
  558. int in_len, int out_len)
  559. {
  560. struct rdma_ucm_init_qp_attr cmd;
  561. struct ib_uverbs_qp_attr resp;
  562. struct ucma_context *ctx;
  563. struct ib_qp_attr qp_attr;
  564. int ret;
  565. if (out_len < sizeof(resp))
  566. return -ENOSPC;
  567. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  568. return -EFAULT;
  569. ctx = ucma_get_ctx(file, cmd.id);
  570. if (IS_ERR(ctx))
  571. return PTR_ERR(ctx);
  572. resp.qp_attr_mask = 0;
  573. memset(&qp_attr, 0, sizeof qp_attr);
  574. qp_attr.qp_state = cmd.qp_state;
  575. ret = rdma_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
  576. if (ret)
  577. goto out;
  578. ib_copy_qp_attr_to_user(&resp, &qp_attr);
  579. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  580. &resp, sizeof(resp)))
  581. ret = -EFAULT;
  582. out:
  583. ucma_put_ctx(ctx);
  584. return ret;
  585. }
  586. static ssize_t ucma_notify(struct ucma_file *file, const char __user *inbuf,
  587. int in_len, int out_len)
  588. {
  589. struct rdma_ucm_notify cmd;
  590. struct ucma_context *ctx;
  591. int ret;
  592. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  593. return -EFAULT;
  594. ctx = ucma_get_ctx(file, cmd.id);
  595. if (IS_ERR(ctx))
  596. return PTR_ERR(ctx);
  597. ret = rdma_notify(ctx->cm_id, (enum ib_event_type) cmd.event);
  598. ucma_put_ctx(ctx);
  599. return ret;
  600. }
  601. static ssize_t (*ucma_cmd_table[])(struct ucma_file *file,
  602. const char __user *inbuf,
  603. int in_len, int out_len) = {
  604. [RDMA_USER_CM_CMD_CREATE_ID] = ucma_create_id,
  605. [RDMA_USER_CM_CMD_DESTROY_ID] = ucma_destroy_id,
  606. [RDMA_USER_CM_CMD_BIND_ADDR] = ucma_bind_addr,
  607. [RDMA_USER_CM_CMD_RESOLVE_ADDR] = ucma_resolve_addr,
  608. [RDMA_USER_CM_CMD_RESOLVE_ROUTE]= ucma_resolve_route,
  609. [RDMA_USER_CM_CMD_QUERY_ROUTE] = ucma_query_route,
  610. [RDMA_USER_CM_CMD_CONNECT] = ucma_connect,
  611. [RDMA_USER_CM_CMD_LISTEN] = ucma_listen,
  612. [RDMA_USER_CM_CMD_ACCEPT] = ucma_accept,
  613. [RDMA_USER_CM_CMD_REJECT] = ucma_reject,
  614. [RDMA_USER_CM_CMD_DISCONNECT] = ucma_disconnect,
  615. [RDMA_USER_CM_CMD_INIT_QP_ATTR] = ucma_init_qp_attr,
  616. [RDMA_USER_CM_CMD_GET_EVENT] = ucma_get_event,
  617. [RDMA_USER_CM_CMD_GET_OPTION] = NULL,
  618. [RDMA_USER_CM_CMD_SET_OPTION] = NULL,
  619. [RDMA_USER_CM_CMD_NOTIFY] = ucma_notify,
  620. };
  621. static ssize_t ucma_write(struct file *filp, const char __user *buf,
  622. size_t len, loff_t *pos)
  623. {
  624. struct ucma_file *file = filp->private_data;
  625. struct rdma_ucm_cmd_hdr hdr;
  626. ssize_t ret;
  627. if (len < sizeof(hdr))
  628. return -EINVAL;
  629. if (copy_from_user(&hdr, buf, sizeof(hdr)))
  630. return -EFAULT;
  631. if (hdr.cmd < 0 || hdr.cmd >= ARRAY_SIZE(ucma_cmd_table))
  632. return -EINVAL;
  633. if (hdr.in + sizeof(hdr) > len)
  634. return -EINVAL;
  635. if (!ucma_cmd_table[hdr.cmd])
  636. return -ENOSYS;
  637. ret = ucma_cmd_table[hdr.cmd](file, buf + sizeof(hdr), hdr.in, hdr.out);
  638. if (!ret)
  639. ret = len;
  640. return ret;
  641. }
  642. static unsigned int ucma_poll(struct file *filp, struct poll_table_struct *wait)
  643. {
  644. struct ucma_file *file = filp->private_data;
  645. unsigned int mask = 0;
  646. poll_wait(filp, &file->poll_wait, wait);
  647. if (!list_empty(&file->event_list))
  648. mask = POLLIN | POLLRDNORM;
  649. return mask;
  650. }
  651. static int ucma_open(struct inode *inode, struct file *filp)
  652. {
  653. struct ucma_file *file;
  654. file = kmalloc(sizeof *file, GFP_KERNEL);
  655. if (!file)
  656. return -ENOMEM;
  657. INIT_LIST_HEAD(&file->event_list);
  658. INIT_LIST_HEAD(&file->ctx_list);
  659. init_waitqueue_head(&file->poll_wait);
  660. mutex_init(&file->mut);
  661. filp->private_data = file;
  662. file->filp = filp;
  663. return 0;
  664. }
  665. static int ucma_close(struct inode *inode, struct file *filp)
  666. {
  667. struct ucma_file *file = filp->private_data;
  668. struct ucma_context *ctx, *tmp;
  669. mutex_lock(&file->mut);
  670. list_for_each_entry_safe(ctx, tmp, &file->ctx_list, list) {
  671. mutex_unlock(&file->mut);
  672. mutex_lock(&mut);
  673. idr_remove(&ctx_idr, ctx->id);
  674. mutex_unlock(&mut);
  675. ucma_free_ctx(ctx);
  676. mutex_lock(&file->mut);
  677. }
  678. mutex_unlock(&file->mut);
  679. kfree(file);
  680. return 0;
  681. }
  682. static struct file_operations ucma_fops = {
  683. .owner = THIS_MODULE,
  684. .open = ucma_open,
  685. .release = ucma_close,
  686. .write = ucma_write,
  687. .poll = ucma_poll,
  688. };
  689. static struct miscdevice ucma_misc = {
  690. .minor = MISC_DYNAMIC_MINOR,
  691. .name = "rdma_cm",
  692. .fops = &ucma_fops,
  693. };
  694. static ssize_t show_abi_version(struct device *dev,
  695. struct device_attribute *attr,
  696. char *buf)
  697. {
  698. return sprintf(buf, "%d\n", RDMA_USER_CM_ABI_VERSION);
  699. }
  700. static DEVICE_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  701. static int __init ucma_init(void)
  702. {
  703. int ret;
  704. ret = misc_register(&ucma_misc);
  705. if (ret)
  706. return ret;
  707. ret = device_create_file(ucma_misc.this_device, &dev_attr_abi_version);
  708. if (ret) {
  709. printk(KERN_ERR "rdma_ucm: couldn't create abi_version attr\n");
  710. goto err;
  711. }
  712. return 0;
  713. err:
  714. misc_deregister(&ucma_misc);
  715. return ret;
  716. }
  717. static void __exit ucma_cleanup(void)
  718. {
  719. device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
  720. misc_deregister(&ucma_misc);
  721. idr_destroy(&ctx_idr);
  722. }
  723. module_init(ucma_init);
  724. module_exit(ucma_cleanup);