ucma.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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. struct list_head mc_list;
  66. };
  67. struct ucma_multicast {
  68. struct ucma_context *ctx;
  69. int id;
  70. int events_reported;
  71. u64 uid;
  72. struct list_head list;
  73. struct sockaddr addr;
  74. u8 pad[sizeof(struct sockaddr_in6) -
  75. sizeof(struct sockaddr)];
  76. };
  77. struct ucma_event {
  78. struct ucma_context *ctx;
  79. struct ucma_multicast *mc;
  80. struct list_head list;
  81. struct rdma_cm_id *cm_id;
  82. struct rdma_ucm_event_resp resp;
  83. };
  84. static DEFINE_MUTEX(mut);
  85. static DEFINE_IDR(ctx_idr);
  86. static DEFINE_IDR(multicast_idr);
  87. static inline struct ucma_context *_ucma_find_context(int id,
  88. struct ucma_file *file)
  89. {
  90. struct ucma_context *ctx;
  91. ctx = idr_find(&ctx_idr, id);
  92. if (!ctx)
  93. ctx = ERR_PTR(-ENOENT);
  94. else if (ctx->file != file)
  95. ctx = ERR_PTR(-EINVAL);
  96. return ctx;
  97. }
  98. static struct ucma_context *ucma_get_ctx(struct ucma_file *file, int id)
  99. {
  100. struct ucma_context *ctx;
  101. mutex_lock(&mut);
  102. ctx = _ucma_find_context(id, file);
  103. if (!IS_ERR(ctx))
  104. atomic_inc(&ctx->ref);
  105. mutex_unlock(&mut);
  106. return ctx;
  107. }
  108. static void ucma_put_ctx(struct ucma_context *ctx)
  109. {
  110. if (atomic_dec_and_test(&ctx->ref))
  111. complete(&ctx->comp);
  112. }
  113. static struct ucma_context *ucma_alloc_ctx(struct ucma_file *file)
  114. {
  115. struct ucma_context *ctx;
  116. int ret;
  117. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  118. if (!ctx)
  119. return NULL;
  120. atomic_set(&ctx->ref, 1);
  121. init_completion(&ctx->comp);
  122. INIT_LIST_HEAD(&ctx->mc_list);
  123. ctx->file = file;
  124. do {
  125. ret = idr_pre_get(&ctx_idr, GFP_KERNEL);
  126. if (!ret)
  127. goto error;
  128. mutex_lock(&mut);
  129. ret = idr_get_new(&ctx_idr, ctx, &ctx->id);
  130. mutex_unlock(&mut);
  131. } while (ret == -EAGAIN);
  132. if (ret)
  133. goto error;
  134. list_add_tail(&ctx->list, &file->ctx_list);
  135. return ctx;
  136. error:
  137. kfree(ctx);
  138. return NULL;
  139. }
  140. static struct ucma_multicast* ucma_alloc_multicast(struct ucma_context *ctx)
  141. {
  142. struct ucma_multicast *mc;
  143. int ret;
  144. mc = kzalloc(sizeof(*mc), GFP_KERNEL);
  145. if (!mc)
  146. return NULL;
  147. do {
  148. ret = idr_pre_get(&multicast_idr, GFP_KERNEL);
  149. if (!ret)
  150. goto error;
  151. mutex_lock(&mut);
  152. ret = idr_get_new(&multicast_idr, mc, &mc->id);
  153. mutex_unlock(&mut);
  154. } while (ret == -EAGAIN);
  155. if (ret)
  156. goto error;
  157. mc->ctx = ctx;
  158. list_add_tail(&mc->list, &ctx->mc_list);
  159. return mc;
  160. error:
  161. kfree(mc);
  162. return NULL;
  163. }
  164. static void ucma_copy_conn_event(struct rdma_ucm_conn_param *dst,
  165. struct rdma_conn_param *src)
  166. {
  167. if (src->private_data_len)
  168. memcpy(dst->private_data, src->private_data,
  169. src->private_data_len);
  170. dst->private_data_len = src->private_data_len;
  171. dst->responder_resources =src->responder_resources;
  172. dst->initiator_depth = src->initiator_depth;
  173. dst->flow_control = src->flow_control;
  174. dst->retry_count = src->retry_count;
  175. dst->rnr_retry_count = src->rnr_retry_count;
  176. dst->srq = src->srq;
  177. dst->qp_num = src->qp_num;
  178. }
  179. static void ucma_copy_ud_event(struct rdma_ucm_ud_param *dst,
  180. struct rdma_ud_param *src)
  181. {
  182. if (src->private_data_len)
  183. memcpy(dst->private_data, src->private_data,
  184. src->private_data_len);
  185. dst->private_data_len = src->private_data_len;
  186. ib_copy_ah_attr_to_user(&dst->ah_attr, &src->ah_attr);
  187. dst->qp_num = src->qp_num;
  188. dst->qkey = src->qkey;
  189. }
  190. static void ucma_set_event_context(struct ucma_context *ctx,
  191. struct rdma_cm_event *event,
  192. struct ucma_event *uevent)
  193. {
  194. uevent->ctx = ctx;
  195. switch (event->event) {
  196. case RDMA_CM_EVENT_MULTICAST_JOIN:
  197. case RDMA_CM_EVENT_MULTICAST_ERROR:
  198. uevent->mc = (struct ucma_multicast *)
  199. event->param.ud.private_data;
  200. uevent->resp.uid = uevent->mc->uid;
  201. uevent->resp.id = uevent->mc->id;
  202. break;
  203. default:
  204. uevent->resp.uid = ctx->uid;
  205. uevent->resp.id = ctx->id;
  206. break;
  207. }
  208. }
  209. static int ucma_event_handler(struct rdma_cm_id *cm_id,
  210. struct rdma_cm_event *event)
  211. {
  212. struct ucma_event *uevent;
  213. struct ucma_context *ctx = cm_id->context;
  214. int ret = 0;
  215. uevent = kzalloc(sizeof(*uevent), GFP_KERNEL);
  216. if (!uevent)
  217. return event->event == RDMA_CM_EVENT_CONNECT_REQUEST;
  218. uevent->cm_id = cm_id;
  219. ucma_set_event_context(ctx, event, uevent);
  220. uevent->resp.event = event->event;
  221. uevent->resp.status = event->status;
  222. if (cm_id->ps == RDMA_PS_UDP || cm_id->ps == RDMA_PS_IPOIB)
  223. ucma_copy_ud_event(&uevent->resp.param.ud, &event->param.ud);
  224. else
  225. ucma_copy_conn_event(&uevent->resp.param.conn,
  226. &event->param.conn);
  227. mutex_lock(&ctx->file->mut);
  228. if (event->event == RDMA_CM_EVENT_CONNECT_REQUEST) {
  229. if (!ctx->backlog) {
  230. ret = -ENOMEM;
  231. kfree(uevent);
  232. goto out;
  233. }
  234. ctx->backlog--;
  235. } else if (!ctx->uid) {
  236. /*
  237. * We ignore events for new connections until userspace has set
  238. * their context. This can only happen if an error occurs on a
  239. * new connection before the user accepts it. This is okay,
  240. * since the accept will just fail later.
  241. */
  242. kfree(uevent);
  243. goto out;
  244. }
  245. list_add_tail(&uevent->list, &ctx->file->event_list);
  246. wake_up_interruptible(&ctx->file->poll_wait);
  247. out:
  248. mutex_unlock(&ctx->file->mut);
  249. return ret;
  250. }
  251. static ssize_t ucma_get_event(struct ucma_file *file, const char __user *inbuf,
  252. int in_len, int out_len)
  253. {
  254. struct ucma_context *ctx;
  255. struct rdma_ucm_get_event cmd;
  256. struct ucma_event *uevent;
  257. int ret = 0;
  258. DEFINE_WAIT(wait);
  259. if (out_len < sizeof uevent->resp)
  260. return -ENOSPC;
  261. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  262. return -EFAULT;
  263. mutex_lock(&file->mut);
  264. while (list_empty(&file->event_list)) {
  265. if (file->filp->f_flags & O_NONBLOCK) {
  266. ret = -EAGAIN;
  267. break;
  268. }
  269. if (signal_pending(current)) {
  270. ret = -ERESTARTSYS;
  271. break;
  272. }
  273. prepare_to_wait(&file->poll_wait, &wait, TASK_INTERRUPTIBLE);
  274. mutex_unlock(&file->mut);
  275. schedule();
  276. mutex_lock(&file->mut);
  277. finish_wait(&file->poll_wait, &wait);
  278. }
  279. if (ret)
  280. goto done;
  281. uevent = list_entry(file->event_list.next, struct ucma_event, list);
  282. if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST) {
  283. ctx = ucma_alloc_ctx(file);
  284. if (!ctx) {
  285. ret = -ENOMEM;
  286. goto done;
  287. }
  288. uevent->ctx->backlog++;
  289. ctx->cm_id = uevent->cm_id;
  290. ctx->cm_id->context = ctx;
  291. uevent->resp.id = ctx->id;
  292. }
  293. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  294. &uevent->resp, sizeof uevent->resp)) {
  295. ret = -EFAULT;
  296. goto done;
  297. }
  298. list_del(&uevent->list);
  299. uevent->ctx->events_reported++;
  300. if (uevent->mc)
  301. uevent->mc->events_reported++;
  302. kfree(uevent);
  303. done:
  304. mutex_unlock(&file->mut);
  305. return ret;
  306. }
  307. static ssize_t ucma_create_id(struct ucma_file *file,
  308. const char __user *inbuf,
  309. int in_len, int out_len)
  310. {
  311. struct rdma_ucm_create_id cmd;
  312. struct rdma_ucm_create_id_resp resp;
  313. struct ucma_context *ctx;
  314. int ret;
  315. if (out_len < sizeof(resp))
  316. return -ENOSPC;
  317. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  318. return -EFAULT;
  319. mutex_lock(&file->mut);
  320. ctx = ucma_alloc_ctx(file);
  321. mutex_unlock(&file->mut);
  322. if (!ctx)
  323. return -ENOMEM;
  324. ctx->uid = cmd.uid;
  325. ctx->cm_id = rdma_create_id(ucma_event_handler, ctx, cmd.ps);
  326. if (IS_ERR(ctx->cm_id)) {
  327. ret = PTR_ERR(ctx->cm_id);
  328. goto err1;
  329. }
  330. resp.id = ctx->id;
  331. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  332. &resp, sizeof(resp))) {
  333. ret = -EFAULT;
  334. goto err2;
  335. }
  336. return 0;
  337. err2:
  338. rdma_destroy_id(ctx->cm_id);
  339. err1:
  340. mutex_lock(&mut);
  341. idr_remove(&ctx_idr, ctx->id);
  342. mutex_unlock(&mut);
  343. kfree(ctx);
  344. return ret;
  345. }
  346. static void ucma_cleanup_multicast(struct ucma_context *ctx)
  347. {
  348. struct ucma_multicast *mc, *tmp;
  349. mutex_lock(&mut);
  350. list_for_each_entry_safe(mc, tmp, &ctx->mc_list, list) {
  351. list_del(&mc->list);
  352. idr_remove(&multicast_idr, mc->id);
  353. kfree(mc);
  354. }
  355. mutex_unlock(&mut);
  356. }
  357. static void ucma_cleanup_events(struct ucma_context *ctx)
  358. {
  359. struct ucma_event *uevent, *tmp;
  360. list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list) {
  361. if (uevent->ctx != ctx)
  362. continue;
  363. list_del(&uevent->list);
  364. /* clear incoming connections. */
  365. if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST)
  366. rdma_destroy_id(uevent->cm_id);
  367. kfree(uevent);
  368. }
  369. }
  370. static void ucma_cleanup_mc_events(struct ucma_multicast *mc)
  371. {
  372. struct ucma_event *uevent, *tmp;
  373. list_for_each_entry_safe(uevent, tmp, &mc->ctx->file->event_list, list) {
  374. if (uevent->mc != mc)
  375. continue;
  376. list_del(&uevent->list);
  377. kfree(uevent);
  378. }
  379. }
  380. static int ucma_free_ctx(struct ucma_context *ctx)
  381. {
  382. int events_reported;
  383. /* No new events will be generated after destroying the id. */
  384. rdma_destroy_id(ctx->cm_id);
  385. ucma_cleanup_multicast(ctx);
  386. /* Cleanup events not yet reported to the user. */
  387. mutex_lock(&ctx->file->mut);
  388. ucma_cleanup_events(ctx);
  389. list_del(&ctx->list);
  390. mutex_unlock(&ctx->file->mut);
  391. events_reported = ctx->events_reported;
  392. kfree(ctx);
  393. return events_reported;
  394. }
  395. static ssize_t ucma_destroy_id(struct ucma_file *file, const char __user *inbuf,
  396. int in_len, int out_len)
  397. {
  398. struct rdma_ucm_destroy_id cmd;
  399. struct rdma_ucm_destroy_id_resp resp;
  400. struct ucma_context *ctx;
  401. int ret = 0;
  402. if (out_len < sizeof(resp))
  403. return -ENOSPC;
  404. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  405. return -EFAULT;
  406. mutex_lock(&mut);
  407. ctx = _ucma_find_context(cmd.id, file);
  408. if (!IS_ERR(ctx))
  409. idr_remove(&ctx_idr, ctx->id);
  410. mutex_unlock(&mut);
  411. if (IS_ERR(ctx))
  412. return PTR_ERR(ctx);
  413. ucma_put_ctx(ctx);
  414. wait_for_completion(&ctx->comp);
  415. resp.events_reported = ucma_free_ctx(ctx);
  416. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  417. &resp, sizeof(resp)))
  418. ret = -EFAULT;
  419. return ret;
  420. }
  421. static ssize_t ucma_bind_addr(struct ucma_file *file, const char __user *inbuf,
  422. int in_len, int out_len)
  423. {
  424. struct rdma_ucm_bind_addr cmd;
  425. struct ucma_context *ctx;
  426. int ret;
  427. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  428. return -EFAULT;
  429. ctx = ucma_get_ctx(file, cmd.id);
  430. if (IS_ERR(ctx))
  431. return PTR_ERR(ctx);
  432. ret = rdma_bind_addr(ctx->cm_id, (struct sockaddr *) &cmd.addr);
  433. ucma_put_ctx(ctx);
  434. return ret;
  435. }
  436. static ssize_t ucma_resolve_addr(struct ucma_file *file,
  437. const char __user *inbuf,
  438. int in_len, int out_len)
  439. {
  440. struct rdma_ucm_resolve_addr cmd;
  441. struct ucma_context *ctx;
  442. int ret;
  443. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  444. return -EFAULT;
  445. ctx = ucma_get_ctx(file, cmd.id);
  446. if (IS_ERR(ctx))
  447. return PTR_ERR(ctx);
  448. ret = rdma_resolve_addr(ctx->cm_id, (struct sockaddr *) &cmd.src_addr,
  449. (struct sockaddr *) &cmd.dst_addr,
  450. cmd.timeout_ms);
  451. ucma_put_ctx(ctx);
  452. return ret;
  453. }
  454. static ssize_t ucma_resolve_route(struct ucma_file *file,
  455. const char __user *inbuf,
  456. int in_len, int out_len)
  457. {
  458. struct rdma_ucm_resolve_route cmd;
  459. struct ucma_context *ctx;
  460. int ret;
  461. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  462. return -EFAULT;
  463. ctx = ucma_get_ctx(file, cmd.id);
  464. if (IS_ERR(ctx))
  465. return PTR_ERR(ctx);
  466. ret = rdma_resolve_route(ctx->cm_id, cmd.timeout_ms);
  467. ucma_put_ctx(ctx);
  468. return ret;
  469. }
  470. static void ucma_copy_ib_route(struct rdma_ucm_query_route_resp *resp,
  471. struct rdma_route *route)
  472. {
  473. struct rdma_dev_addr *dev_addr;
  474. resp->num_paths = route->num_paths;
  475. switch (route->num_paths) {
  476. case 0:
  477. dev_addr = &route->addr.dev_addr;
  478. ib_addr_get_dgid(dev_addr,
  479. (union ib_gid *) &resp->ib_route[0].dgid);
  480. ib_addr_get_sgid(dev_addr,
  481. (union ib_gid *) &resp->ib_route[0].sgid);
  482. resp->ib_route[0].pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
  483. break;
  484. case 2:
  485. ib_copy_path_rec_to_user(&resp->ib_route[1],
  486. &route->path_rec[1]);
  487. /* fall through */
  488. case 1:
  489. ib_copy_path_rec_to_user(&resp->ib_route[0],
  490. &route->path_rec[0]);
  491. break;
  492. default:
  493. break;
  494. }
  495. }
  496. static ssize_t ucma_query_route(struct ucma_file *file,
  497. const char __user *inbuf,
  498. int in_len, int out_len)
  499. {
  500. struct rdma_ucm_query_route cmd;
  501. struct rdma_ucm_query_route_resp resp;
  502. struct ucma_context *ctx;
  503. struct sockaddr *addr;
  504. int ret = 0;
  505. if (out_len < sizeof(resp))
  506. return -ENOSPC;
  507. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  508. return -EFAULT;
  509. ctx = ucma_get_ctx(file, cmd.id);
  510. if (IS_ERR(ctx))
  511. return PTR_ERR(ctx);
  512. memset(&resp, 0, sizeof resp);
  513. addr = &ctx->cm_id->route.addr.src_addr;
  514. memcpy(&resp.src_addr, addr, addr->sa_family == AF_INET ?
  515. sizeof(struct sockaddr_in) :
  516. sizeof(struct sockaddr_in6));
  517. addr = &ctx->cm_id->route.addr.dst_addr;
  518. memcpy(&resp.dst_addr, addr, addr->sa_family == AF_INET ?
  519. sizeof(struct sockaddr_in) :
  520. sizeof(struct sockaddr_in6));
  521. if (!ctx->cm_id->device)
  522. goto out;
  523. resp.node_guid = ctx->cm_id->device->node_guid;
  524. resp.port_num = ctx->cm_id->port_num;
  525. switch (rdma_node_get_transport(ctx->cm_id->device->node_type)) {
  526. case RDMA_TRANSPORT_IB:
  527. ucma_copy_ib_route(&resp, &ctx->cm_id->route);
  528. break;
  529. default:
  530. break;
  531. }
  532. out:
  533. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  534. &resp, sizeof(resp)))
  535. ret = -EFAULT;
  536. ucma_put_ctx(ctx);
  537. return ret;
  538. }
  539. static void ucma_copy_conn_param(struct rdma_conn_param *dst,
  540. struct rdma_ucm_conn_param *src)
  541. {
  542. dst->private_data = src->private_data;
  543. dst->private_data_len = src->private_data_len;
  544. dst->responder_resources =src->responder_resources;
  545. dst->initiator_depth = src->initiator_depth;
  546. dst->flow_control = src->flow_control;
  547. dst->retry_count = src->retry_count;
  548. dst->rnr_retry_count = src->rnr_retry_count;
  549. dst->srq = src->srq;
  550. dst->qp_num = src->qp_num;
  551. }
  552. static ssize_t ucma_connect(struct ucma_file *file, const char __user *inbuf,
  553. int in_len, int out_len)
  554. {
  555. struct rdma_ucm_connect cmd;
  556. struct rdma_conn_param conn_param;
  557. struct ucma_context *ctx;
  558. int ret;
  559. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  560. return -EFAULT;
  561. if (!cmd.conn_param.valid)
  562. return -EINVAL;
  563. ctx = ucma_get_ctx(file, cmd.id);
  564. if (IS_ERR(ctx))
  565. return PTR_ERR(ctx);
  566. ucma_copy_conn_param(&conn_param, &cmd.conn_param);
  567. ret = rdma_connect(ctx->cm_id, &conn_param);
  568. ucma_put_ctx(ctx);
  569. return ret;
  570. }
  571. static ssize_t ucma_listen(struct ucma_file *file, const char __user *inbuf,
  572. int in_len, int out_len)
  573. {
  574. struct rdma_ucm_listen cmd;
  575. struct ucma_context *ctx;
  576. int ret;
  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. ctx->backlog = cmd.backlog > 0 && cmd.backlog < UCMA_MAX_BACKLOG ?
  583. cmd.backlog : UCMA_MAX_BACKLOG;
  584. ret = rdma_listen(ctx->cm_id, ctx->backlog);
  585. ucma_put_ctx(ctx);
  586. return ret;
  587. }
  588. static ssize_t ucma_accept(struct ucma_file *file, const char __user *inbuf,
  589. int in_len, int out_len)
  590. {
  591. struct rdma_ucm_accept cmd;
  592. struct rdma_conn_param conn_param;
  593. struct ucma_context *ctx;
  594. int ret;
  595. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  596. return -EFAULT;
  597. ctx = ucma_get_ctx(file, cmd.id);
  598. if (IS_ERR(ctx))
  599. return PTR_ERR(ctx);
  600. if (cmd.conn_param.valid) {
  601. ctx->uid = cmd.uid;
  602. ucma_copy_conn_param(&conn_param, &cmd.conn_param);
  603. ret = rdma_accept(ctx->cm_id, &conn_param);
  604. } else
  605. ret = rdma_accept(ctx->cm_id, NULL);
  606. ucma_put_ctx(ctx);
  607. return ret;
  608. }
  609. static ssize_t ucma_reject(struct ucma_file *file, const char __user *inbuf,
  610. int in_len, int out_len)
  611. {
  612. struct rdma_ucm_reject cmd;
  613. struct ucma_context *ctx;
  614. int ret;
  615. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  616. return -EFAULT;
  617. ctx = ucma_get_ctx(file, cmd.id);
  618. if (IS_ERR(ctx))
  619. return PTR_ERR(ctx);
  620. ret = rdma_reject(ctx->cm_id, cmd.private_data, cmd.private_data_len);
  621. ucma_put_ctx(ctx);
  622. return ret;
  623. }
  624. static ssize_t ucma_disconnect(struct ucma_file *file, const char __user *inbuf,
  625. int in_len, int out_len)
  626. {
  627. struct rdma_ucm_disconnect cmd;
  628. struct ucma_context *ctx;
  629. int ret;
  630. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  631. return -EFAULT;
  632. ctx = ucma_get_ctx(file, cmd.id);
  633. if (IS_ERR(ctx))
  634. return PTR_ERR(ctx);
  635. ret = rdma_disconnect(ctx->cm_id);
  636. ucma_put_ctx(ctx);
  637. return ret;
  638. }
  639. static ssize_t ucma_init_qp_attr(struct ucma_file *file,
  640. const char __user *inbuf,
  641. int in_len, int out_len)
  642. {
  643. struct rdma_ucm_init_qp_attr cmd;
  644. struct ib_uverbs_qp_attr resp;
  645. struct ucma_context *ctx;
  646. struct ib_qp_attr qp_attr;
  647. int ret;
  648. if (out_len < sizeof(resp))
  649. return -ENOSPC;
  650. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  651. return -EFAULT;
  652. ctx = ucma_get_ctx(file, cmd.id);
  653. if (IS_ERR(ctx))
  654. return PTR_ERR(ctx);
  655. resp.qp_attr_mask = 0;
  656. memset(&qp_attr, 0, sizeof qp_attr);
  657. qp_attr.qp_state = cmd.qp_state;
  658. ret = rdma_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
  659. if (ret)
  660. goto out;
  661. ib_copy_qp_attr_to_user(&resp, &qp_attr);
  662. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  663. &resp, sizeof(resp)))
  664. ret = -EFAULT;
  665. out:
  666. ucma_put_ctx(ctx);
  667. return ret;
  668. }
  669. static ssize_t ucma_notify(struct ucma_file *file, const char __user *inbuf,
  670. int in_len, int out_len)
  671. {
  672. struct rdma_ucm_notify cmd;
  673. struct ucma_context *ctx;
  674. int ret;
  675. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  676. return -EFAULT;
  677. ctx = ucma_get_ctx(file, cmd.id);
  678. if (IS_ERR(ctx))
  679. return PTR_ERR(ctx);
  680. ret = rdma_notify(ctx->cm_id, (enum ib_event_type) cmd.event);
  681. ucma_put_ctx(ctx);
  682. return ret;
  683. }
  684. static ssize_t ucma_join_multicast(struct ucma_file *file,
  685. const char __user *inbuf,
  686. int in_len, int out_len)
  687. {
  688. struct rdma_ucm_join_mcast cmd;
  689. struct rdma_ucm_create_id_resp resp;
  690. struct ucma_context *ctx;
  691. struct ucma_multicast *mc;
  692. int ret;
  693. if (out_len < sizeof(resp))
  694. return -ENOSPC;
  695. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  696. return -EFAULT;
  697. ctx = ucma_get_ctx(file, cmd.id);
  698. if (IS_ERR(ctx))
  699. return PTR_ERR(ctx);
  700. mutex_lock(&file->mut);
  701. mc = ucma_alloc_multicast(ctx);
  702. if (IS_ERR(mc)) {
  703. ret = PTR_ERR(mc);
  704. goto err1;
  705. }
  706. mc->uid = cmd.uid;
  707. memcpy(&mc->addr, &cmd.addr, sizeof cmd.addr);
  708. ret = rdma_join_multicast(ctx->cm_id, &mc->addr, mc);
  709. if (ret)
  710. goto err2;
  711. resp.id = mc->id;
  712. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  713. &resp, sizeof(resp))) {
  714. ret = -EFAULT;
  715. goto err3;
  716. }
  717. mutex_unlock(&file->mut);
  718. ucma_put_ctx(ctx);
  719. return 0;
  720. err3:
  721. rdma_leave_multicast(ctx->cm_id, &mc->addr);
  722. ucma_cleanup_mc_events(mc);
  723. err2:
  724. mutex_lock(&mut);
  725. idr_remove(&multicast_idr, mc->id);
  726. mutex_unlock(&mut);
  727. list_del(&mc->list);
  728. kfree(mc);
  729. err1:
  730. mutex_unlock(&file->mut);
  731. ucma_put_ctx(ctx);
  732. return ret;
  733. }
  734. static ssize_t ucma_leave_multicast(struct ucma_file *file,
  735. const char __user *inbuf,
  736. int in_len, int out_len)
  737. {
  738. struct rdma_ucm_destroy_id cmd;
  739. struct rdma_ucm_destroy_id_resp resp;
  740. struct ucma_multicast *mc;
  741. int ret = 0;
  742. if (out_len < sizeof(resp))
  743. return -ENOSPC;
  744. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  745. return -EFAULT;
  746. mutex_lock(&mut);
  747. mc = idr_find(&multicast_idr, cmd.id);
  748. if (!mc)
  749. mc = ERR_PTR(-ENOENT);
  750. else if (mc->ctx->file != file)
  751. mc = ERR_PTR(-EINVAL);
  752. else {
  753. idr_remove(&multicast_idr, mc->id);
  754. atomic_inc(&mc->ctx->ref);
  755. }
  756. mutex_unlock(&mut);
  757. if (IS_ERR(mc)) {
  758. ret = PTR_ERR(mc);
  759. goto out;
  760. }
  761. rdma_leave_multicast(mc->ctx->cm_id, &mc->addr);
  762. mutex_lock(&mc->ctx->file->mut);
  763. ucma_cleanup_mc_events(mc);
  764. list_del(&mc->list);
  765. mutex_unlock(&mc->ctx->file->mut);
  766. ucma_put_ctx(mc->ctx);
  767. resp.events_reported = mc->events_reported;
  768. kfree(mc);
  769. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  770. &resp, sizeof(resp)))
  771. ret = -EFAULT;
  772. out:
  773. return ret;
  774. }
  775. static ssize_t (*ucma_cmd_table[])(struct ucma_file *file,
  776. const char __user *inbuf,
  777. int in_len, int out_len) = {
  778. [RDMA_USER_CM_CMD_CREATE_ID] = ucma_create_id,
  779. [RDMA_USER_CM_CMD_DESTROY_ID] = ucma_destroy_id,
  780. [RDMA_USER_CM_CMD_BIND_ADDR] = ucma_bind_addr,
  781. [RDMA_USER_CM_CMD_RESOLVE_ADDR] = ucma_resolve_addr,
  782. [RDMA_USER_CM_CMD_RESOLVE_ROUTE]= ucma_resolve_route,
  783. [RDMA_USER_CM_CMD_QUERY_ROUTE] = ucma_query_route,
  784. [RDMA_USER_CM_CMD_CONNECT] = ucma_connect,
  785. [RDMA_USER_CM_CMD_LISTEN] = ucma_listen,
  786. [RDMA_USER_CM_CMD_ACCEPT] = ucma_accept,
  787. [RDMA_USER_CM_CMD_REJECT] = ucma_reject,
  788. [RDMA_USER_CM_CMD_DISCONNECT] = ucma_disconnect,
  789. [RDMA_USER_CM_CMD_INIT_QP_ATTR] = ucma_init_qp_attr,
  790. [RDMA_USER_CM_CMD_GET_EVENT] = ucma_get_event,
  791. [RDMA_USER_CM_CMD_GET_OPTION] = NULL,
  792. [RDMA_USER_CM_CMD_SET_OPTION] = NULL,
  793. [RDMA_USER_CM_CMD_NOTIFY] = ucma_notify,
  794. [RDMA_USER_CM_CMD_JOIN_MCAST] = ucma_join_multicast,
  795. [RDMA_USER_CM_CMD_LEAVE_MCAST] = ucma_leave_multicast,
  796. };
  797. static ssize_t ucma_write(struct file *filp, const char __user *buf,
  798. size_t len, loff_t *pos)
  799. {
  800. struct ucma_file *file = filp->private_data;
  801. struct rdma_ucm_cmd_hdr hdr;
  802. ssize_t ret;
  803. if (len < sizeof(hdr))
  804. return -EINVAL;
  805. if (copy_from_user(&hdr, buf, sizeof(hdr)))
  806. return -EFAULT;
  807. if (hdr.cmd < 0 || hdr.cmd >= ARRAY_SIZE(ucma_cmd_table))
  808. return -EINVAL;
  809. if (hdr.in + sizeof(hdr) > len)
  810. return -EINVAL;
  811. if (!ucma_cmd_table[hdr.cmd])
  812. return -ENOSYS;
  813. ret = ucma_cmd_table[hdr.cmd](file, buf + sizeof(hdr), hdr.in, hdr.out);
  814. if (!ret)
  815. ret = len;
  816. return ret;
  817. }
  818. static unsigned int ucma_poll(struct file *filp, struct poll_table_struct *wait)
  819. {
  820. struct ucma_file *file = filp->private_data;
  821. unsigned int mask = 0;
  822. poll_wait(filp, &file->poll_wait, wait);
  823. if (!list_empty(&file->event_list))
  824. mask = POLLIN | POLLRDNORM;
  825. return mask;
  826. }
  827. static int ucma_open(struct inode *inode, struct file *filp)
  828. {
  829. struct ucma_file *file;
  830. file = kmalloc(sizeof *file, GFP_KERNEL);
  831. if (!file)
  832. return -ENOMEM;
  833. INIT_LIST_HEAD(&file->event_list);
  834. INIT_LIST_HEAD(&file->ctx_list);
  835. init_waitqueue_head(&file->poll_wait);
  836. mutex_init(&file->mut);
  837. filp->private_data = file;
  838. file->filp = filp;
  839. return 0;
  840. }
  841. static int ucma_close(struct inode *inode, struct file *filp)
  842. {
  843. struct ucma_file *file = filp->private_data;
  844. struct ucma_context *ctx, *tmp;
  845. mutex_lock(&file->mut);
  846. list_for_each_entry_safe(ctx, tmp, &file->ctx_list, list) {
  847. mutex_unlock(&file->mut);
  848. mutex_lock(&mut);
  849. idr_remove(&ctx_idr, ctx->id);
  850. mutex_unlock(&mut);
  851. ucma_free_ctx(ctx);
  852. mutex_lock(&file->mut);
  853. }
  854. mutex_unlock(&file->mut);
  855. kfree(file);
  856. return 0;
  857. }
  858. static const struct file_operations ucma_fops = {
  859. .owner = THIS_MODULE,
  860. .open = ucma_open,
  861. .release = ucma_close,
  862. .write = ucma_write,
  863. .poll = ucma_poll,
  864. };
  865. static struct miscdevice ucma_misc = {
  866. .minor = MISC_DYNAMIC_MINOR,
  867. .name = "rdma_cm",
  868. .fops = &ucma_fops,
  869. };
  870. static ssize_t show_abi_version(struct device *dev,
  871. struct device_attribute *attr,
  872. char *buf)
  873. {
  874. return sprintf(buf, "%d\n", RDMA_USER_CM_ABI_VERSION);
  875. }
  876. static DEVICE_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  877. static int __init ucma_init(void)
  878. {
  879. int ret;
  880. ret = misc_register(&ucma_misc);
  881. if (ret)
  882. return ret;
  883. ret = device_create_file(ucma_misc.this_device, &dev_attr_abi_version);
  884. if (ret) {
  885. printk(KERN_ERR "rdma_ucm: couldn't create abi_version attr\n");
  886. goto err;
  887. }
  888. return 0;
  889. err:
  890. misc_deregister(&ucma_misc);
  891. return ret;
  892. }
  893. static void __exit ucma_cleanup(void)
  894. {
  895. device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
  896. misc_deregister(&ucma_misc);
  897. idr_destroy(&ctx_idr);
  898. }
  899. module_init(ucma_init);
  900. module_exit(ucma_cleanup);