ucma.c 28 KB

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