ucma.c 20 KB

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