ucma.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  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/sched.h>
  37. #include <linux/idr.h>
  38. #include <linux/in.h>
  39. #include <linux/in6.h>
  40. #include <linux/miscdevice.h>
  41. #include <linux/slab.h>
  42. #include <linux/sysctl.h>
  43. #include <linux/module.h>
  44. #include <rdma/rdma_user_cm.h>
  45. #include <rdma/ib_marshall.h>
  46. #include <rdma/rdma_cm.h>
  47. #include <rdma/rdma_cm_ib.h>
  48. #include <rdma/ib_addr.h>
  49. MODULE_AUTHOR("Sean Hefty");
  50. MODULE_DESCRIPTION("RDMA Userspace Connection Manager Access");
  51. MODULE_LICENSE("Dual BSD/GPL");
  52. static unsigned int max_backlog = 1024;
  53. static struct ctl_table_header *ucma_ctl_table_hdr;
  54. static ctl_table ucma_ctl_table[] = {
  55. {
  56. .procname = "max_backlog",
  57. .data = &max_backlog,
  58. .maxlen = sizeof max_backlog,
  59. .mode = 0644,
  60. .proc_handler = proc_dointvec,
  61. },
  62. { }
  63. };
  64. struct ucma_file {
  65. struct mutex mut;
  66. struct file *filp;
  67. struct list_head ctx_list;
  68. struct list_head event_list;
  69. wait_queue_head_t poll_wait;
  70. };
  71. struct ucma_context {
  72. int id;
  73. struct completion comp;
  74. atomic_t ref;
  75. int events_reported;
  76. int backlog;
  77. struct ucma_file *file;
  78. struct rdma_cm_id *cm_id;
  79. u64 uid;
  80. struct list_head list;
  81. struct list_head mc_list;
  82. };
  83. struct ucma_multicast {
  84. struct ucma_context *ctx;
  85. int id;
  86. int events_reported;
  87. u64 uid;
  88. struct list_head list;
  89. struct sockaddr_storage addr;
  90. };
  91. struct ucma_event {
  92. struct ucma_context *ctx;
  93. struct ucma_multicast *mc;
  94. struct list_head list;
  95. struct rdma_cm_id *cm_id;
  96. struct rdma_ucm_event_resp resp;
  97. };
  98. static DEFINE_MUTEX(mut);
  99. static DEFINE_IDR(ctx_idr);
  100. static DEFINE_IDR(multicast_idr);
  101. static inline struct ucma_context *_ucma_find_context(int id,
  102. struct ucma_file *file)
  103. {
  104. struct ucma_context *ctx;
  105. ctx = idr_find(&ctx_idr, id);
  106. if (!ctx)
  107. ctx = ERR_PTR(-ENOENT);
  108. else if (ctx->file != file)
  109. ctx = ERR_PTR(-EINVAL);
  110. return ctx;
  111. }
  112. static struct ucma_context *ucma_get_ctx(struct ucma_file *file, int id)
  113. {
  114. struct ucma_context *ctx;
  115. mutex_lock(&mut);
  116. ctx = _ucma_find_context(id, file);
  117. if (!IS_ERR(ctx))
  118. atomic_inc(&ctx->ref);
  119. mutex_unlock(&mut);
  120. return ctx;
  121. }
  122. static void ucma_put_ctx(struct ucma_context *ctx)
  123. {
  124. if (atomic_dec_and_test(&ctx->ref))
  125. complete(&ctx->comp);
  126. }
  127. static struct ucma_context *ucma_alloc_ctx(struct ucma_file *file)
  128. {
  129. struct ucma_context *ctx;
  130. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  131. if (!ctx)
  132. return NULL;
  133. atomic_set(&ctx->ref, 1);
  134. init_completion(&ctx->comp);
  135. INIT_LIST_HEAD(&ctx->mc_list);
  136. ctx->file = file;
  137. mutex_lock(&mut);
  138. ctx->id = idr_alloc(&ctx_idr, ctx, 0, 0, GFP_KERNEL);
  139. mutex_unlock(&mut);
  140. if (ctx->id < 0)
  141. goto error;
  142. list_add_tail(&ctx->list, &file->ctx_list);
  143. return ctx;
  144. error:
  145. kfree(ctx);
  146. return NULL;
  147. }
  148. static struct ucma_multicast* ucma_alloc_multicast(struct ucma_context *ctx)
  149. {
  150. struct ucma_multicast *mc;
  151. mc = kzalloc(sizeof(*mc), GFP_KERNEL);
  152. if (!mc)
  153. return NULL;
  154. mutex_lock(&mut);
  155. mc->id = idr_alloc(&multicast_idr, mc, 0, 0, GFP_KERNEL);
  156. mutex_unlock(&mut);
  157. if (mc->id < 0)
  158. goto error;
  159. mc->ctx = ctx;
  160. list_add_tail(&mc->list, &ctx->mc_list);
  161. return mc;
  162. error:
  163. kfree(mc);
  164. return NULL;
  165. }
  166. static void ucma_copy_conn_event(struct rdma_ucm_conn_param *dst,
  167. struct rdma_conn_param *src)
  168. {
  169. if (src->private_data_len)
  170. memcpy(dst->private_data, src->private_data,
  171. src->private_data_len);
  172. dst->private_data_len = src->private_data_len;
  173. dst->responder_resources =src->responder_resources;
  174. dst->initiator_depth = src->initiator_depth;
  175. dst->flow_control = src->flow_control;
  176. dst->retry_count = src->retry_count;
  177. dst->rnr_retry_count = src->rnr_retry_count;
  178. dst->srq = src->srq;
  179. dst->qp_num = src->qp_num;
  180. }
  181. static void ucma_copy_ud_event(struct rdma_ucm_ud_param *dst,
  182. struct rdma_ud_param *src)
  183. {
  184. if (src->private_data_len)
  185. memcpy(dst->private_data, src->private_data,
  186. src->private_data_len);
  187. dst->private_data_len = src->private_data_len;
  188. ib_copy_ah_attr_to_user(&dst->ah_attr, &src->ah_attr);
  189. dst->qp_num = src->qp_num;
  190. dst->qkey = src->qkey;
  191. }
  192. static void ucma_set_event_context(struct ucma_context *ctx,
  193. struct rdma_cm_event *event,
  194. struct ucma_event *uevent)
  195. {
  196. uevent->ctx = ctx;
  197. switch (event->event) {
  198. case RDMA_CM_EVENT_MULTICAST_JOIN:
  199. case RDMA_CM_EVENT_MULTICAST_ERROR:
  200. uevent->mc = (struct ucma_multicast *)
  201. event->param.ud.private_data;
  202. uevent->resp.uid = uevent->mc->uid;
  203. uevent->resp.id = uevent->mc->id;
  204. break;
  205. default:
  206. uevent->resp.uid = ctx->uid;
  207. uevent->resp.id = ctx->id;
  208. break;
  209. }
  210. }
  211. static int ucma_event_handler(struct rdma_cm_id *cm_id,
  212. struct rdma_cm_event *event)
  213. {
  214. struct ucma_event *uevent;
  215. struct ucma_context *ctx = cm_id->context;
  216. int ret = 0;
  217. uevent = kzalloc(sizeof(*uevent), GFP_KERNEL);
  218. if (!uevent)
  219. return event->event == RDMA_CM_EVENT_CONNECT_REQUEST;
  220. mutex_lock(&ctx->file->mut);
  221. uevent->cm_id = cm_id;
  222. ucma_set_event_context(ctx, event, uevent);
  223. uevent->resp.event = event->event;
  224. uevent->resp.status = event->status;
  225. if (cm_id->qp_type == IB_QPT_UD)
  226. ucma_copy_ud_event(&uevent->resp.param.ud, &event->param.ud);
  227. else
  228. ucma_copy_conn_event(&uevent->resp.param.conn,
  229. &event->param.conn);
  230. if (event->event == RDMA_CM_EVENT_CONNECT_REQUEST) {
  231. if (!ctx->backlog) {
  232. ret = -ENOMEM;
  233. kfree(uevent);
  234. goto out;
  235. }
  236. ctx->backlog--;
  237. } else if (!ctx->uid) {
  238. /*
  239. * We ignore events for new connections until userspace has set
  240. * their context. This can only happen if an error occurs on a
  241. * new connection before the user accepts it. This is okay,
  242. * since the accept will just fail later.
  243. */
  244. kfree(uevent);
  245. goto out;
  246. }
  247. list_add_tail(&uevent->list, &ctx->file->event_list);
  248. wake_up_interruptible(&ctx->file->poll_wait);
  249. out:
  250. mutex_unlock(&ctx->file->mut);
  251. return ret;
  252. }
  253. static ssize_t ucma_get_event(struct ucma_file *file, const char __user *inbuf,
  254. int in_len, int out_len)
  255. {
  256. struct ucma_context *ctx;
  257. struct rdma_ucm_get_event cmd;
  258. struct ucma_event *uevent;
  259. int ret = 0;
  260. if (out_len < sizeof uevent->resp)
  261. return -ENOSPC;
  262. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  263. return -EFAULT;
  264. mutex_lock(&file->mut);
  265. while (list_empty(&file->event_list)) {
  266. mutex_unlock(&file->mut);
  267. if (file->filp->f_flags & O_NONBLOCK)
  268. return -EAGAIN;
  269. if (wait_event_interruptible(file->poll_wait,
  270. !list_empty(&file->event_list)))
  271. return -ERESTARTSYS;
  272. mutex_lock(&file->mut);
  273. }
  274. uevent = list_entry(file->event_list.next, struct ucma_event, list);
  275. if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST) {
  276. ctx = ucma_alloc_ctx(file);
  277. if (!ctx) {
  278. ret = -ENOMEM;
  279. goto done;
  280. }
  281. uevent->ctx->backlog++;
  282. ctx->cm_id = uevent->cm_id;
  283. ctx->cm_id->context = ctx;
  284. uevent->resp.id = ctx->id;
  285. }
  286. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  287. &uevent->resp, sizeof uevent->resp)) {
  288. ret = -EFAULT;
  289. goto done;
  290. }
  291. list_del(&uevent->list);
  292. uevent->ctx->events_reported++;
  293. if (uevent->mc)
  294. uevent->mc->events_reported++;
  295. kfree(uevent);
  296. done:
  297. mutex_unlock(&file->mut);
  298. return ret;
  299. }
  300. static int ucma_get_qp_type(struct rdma_ucm_create_id *cmd, enum ib_qp_type *qp_type)
  301. {
  302. switch (cmd->ps) {
  303. case RDMA_PS_TCP:
  304. *qp_type = IB_QPT_RC;
  305. return 0;
  306. case RDMA_PS_UDP:
  307. case RDMA_PS_IPOIB:
  308. *qp_type = IB_QPT_UD;
  309. return 0;
  310. case RDMA_PS_IB:
  311. *qp_type = cmd->qp_type;
  312. return 0;
  313. default:
  314. return -EINVAL;
  315. }
  316. }
  317. static ssize_t ucma_create_id(struct ucma_file *file, const char __user *inbuf,
  318. int in_len, int out_len)
  319. {
  320. struct rdma_ucm_create_id cmd;
  321. struct rdma_ucm_create_id_resp resp;
  322. struct ucma_context *ctx;
  323. enum ib_qp_type qp_type;
  324. int ret;
  325. if (out_len < sizeof(resp))
  326. return -ENOSPC;
  327. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  328. return -EFAULT;
  329. ret = ucma_get_qp_type(&cmd, &qp_type);
  330. if (ret)
  331. return ret;
  332. mutex_lock(&file->mut);
  333. ctx = ucma_alloc_ctx(file);
  334. mutex_unlock(&file->mut);
  335. if (!ctx)
  336. return -ENOMEM;
  337. ctx->uid = cmd.uid;
  338. ctx->cm_id = rdma_create_id(ucma_event_handler, ctx, cmd.ps, qp_type);
  339. if (IS_ERR(ctx->cm_id)) {
  340. ret = PTR_ERR(ctx->cm_id);
  341. goto err1;
  342. }
  343. resp.id = ctx->id;
  344. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  345. &resp, sizeof(resp))) {
  346. ret = -EFAULT;
  347. goto err2;
  348. }
  349. return 0;
  350. err2:
  351. rdma_destroy_id(ctx->cm_id);
  352. err1:
  353. mutex_lock(&mut);
  354. idr_remove(&ctx_idr, ctx->id);
  355. mutex_unlock(&mut);
  356. kfree(ctx);
  357. return ret;
  358. }
  359. static void ucma_cleanup_multicast(struct ucma_context *ctx)
  360. {
  361. struct ucma_multicast *mc, *tmp;
  362. mutex_lock(&mut);
  363. list_for_each_entry_safe(mc, tmp, &ctx->mc_list, list) {
  364. list_del(&mc->list);
  365. idr_remove(&multicast_idr, mc->id);
  366. kfree(mc);
  367. }
  368. mutex_unlock(&mut);
  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. /*
  381. * We cannot hold file->mut when calling rdma_destroy_id() or we can
  382. * deadlock. We also acquire file->mut in ucma_event_handler(), and
  383. * rdma_destroy_id() will wait until all callbacks have completed.
  384. */
  385. static int ucma_free_ctx(struct ucma_context *ctx)
  386. {
  387. int events_reported;
  388. struct ucma_event *uevent, *tmp;
  389. LIST_HEAD(list);
  390. /* No new events will be generated after destroying the id. */
  391. rdma_destroy_id(ctx->cm_id);
  392. ucma_cleanup_multicast(ctx);
  393. /* Cleanup events not yet reported to the user. */
  394. mutex_lock(&ctx->file->mut);
  395. list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list) {
  396. if (uevent->ctx == ctx)
  397. list_move_tail(&uevent->list, &list);
  398. }
  399. list_del(&ctx->list);
  400. mutex_unlock(&ctx->file->mut);
  401. list_for_each_entry_safe(uevent, tmp, &list, list) {
  402. list_del(&uevent->list);
  403. if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST)
  404. rdma_destroy_id(uevent->cm_id);
  405. kfree(uevent);
  406. }
  407. events_reported = ctx->events_reported;
  408. kfree(ctx);
  409. return events_reported;
  410. }
  411. static ssize_t ucma_destroy_id(struct ucma_file *file, const char __user *inbuf,
  412. int in_len, int out_len)
  413. {
  414. struct rdma_ucm_destroy_id cmd;
  415. struct rdma_ucm_destroy_id_resp resp;
  416. struct ucma_context *ctx;
  417. int ret = 0;
  418. if (out_len < sizeof(resp))
  419. return -ENOSPC;
  420. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  421. return -EFAULT;
  422. mutex_lock(&mut);
  423. ctx = _ucma_find_context(cmd.id, file);
  424. if (!IS_ERR(ctx))
  425. idr_remove(&ctx_idr, ctx->id);
  426. mutex_unlock(&mut);
  427. if (IS_ERR(ctx))
  428. return PTR_ERR(ctx);
  429. ucma_put_ctx(ctx);
  430. wait_for_completion(&ctx->comp);
  431. resp.events_reported = ucma_free_ctx(ctx);
  432. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  433. &resp, sizeof(resp)))
  434. ret = -EFAULT;
  435. return ret;
  436. }
  437. static ssize_t ucma_bind_addr(struct ucma_file *file, const char __user *inbuf,
  438. int in_len, int out_len)
  439. {
  440. struct rdma_ucm_bind_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_bind_addr(ctx->cm_id, (struct sockaddr *) &cmd.addr);
  449. ucma_put_ctx(ctx);
  450. return ret;
  451. }
  452. static ssize_t ucma_resolve_addr(struct ucma_file *file,
  453. const char __user *inbuf,
  454. int in_len, int out_len)
  455. {
  456. struct rdma_ucm_resolve_addr cmd;
  457. struct ucma_context *ctx;
  458. int ret;
  459. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  460. return -EFAULT;
  461. ctx = ucma_get_ctx(file, cmd.id);
  462. if (IS_ERR(ctx))
  463. return PTR_ERR(ctx);
  464. ret = rdma_resolve_addr(ctx->cm_id, (struct sockaddr *) &cmd.src_addr,
  465. (struct sockaddr *) &cmd.dst_addr,
  466. cmd.timeout_ms);
  467. ucma_put_ctx(ctx);
  468. return ret;
  469. }
  470. static ssize_t ucma_resolve_route(struct ucma_file *file,
  471. const char __user *inbuf,
  472. int in_len, int out_len)
  473. {
  474. struct rdma_ucm_resolve_route cmd;
  475. struct ucma_context *ctx;
  476. int ret;
  477. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  478. return -EFAULT;
  479. ctx = ucma_get_ctx(file, cmd.id);
  480. if (IS_ERR(ctx))
  481. return PTR_ERR(ctx);
  482. ret = rdma_resolve_route(ctx->cm_id, cmd.timeout_ms);
  483. ucma_put_ctx(ctx);
  484. return ret;
  485. }
  486. static void ucma_copy_ib_route(struct rdma_ucm_query_route_resp *resp,
  487. struct rdma_route *route)
  488. {
  489. struct rdma_dev_addr *dev_addr;
  490. resp->num_paths = route->num_paths;
  491. switch (route->num_paths) {
  492. case 0:
  493. dev_addr = &route->addr.dev_addr;
  494. rdma_addr_get_dgid(dev_addr,
  495. (union ib_gid *) &resp->ib_route[0].dgid);
  496. rdma_addr_get_sgid(dev_addr,
  497. (union ib_gid *) &resp->ib_route[0].sgid);
  498. resp->ib_route[0].pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
  499. break;
  500. case 2:
  501. ib_copy_path_rec_to_user(&resp->ib_route[1],
  502. &route->path_rec[1]);
  503. /* fall through */
  504. case 1:
  505. ib_copy_path_rec_to_user(&resp->ib_route[0],
  506. &route->path_rec[0]);
  507. break;
  508. default:
  509. break;
  510. }
  511. }
  512. static void ucma_copy_iboe_route(struct rdma_ucm_query_route_resp *resp,
  513. struct rdma_route *route)
  514. {
  515. struct rdma_dev_addr *dev_addr;
  516. struct net_device *dev;
  517. u16 vid = 0;
  518. resp->num_paths = route->num_paths;
  519. switch (route->num_paths) {
  520. case 0:
  521. dev_addr = &route->addr.dev_addr;
  522. dev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
  523. if (dev) {
  524. vid = rdma_vlan_dev_vlan_id(dev);
  525. dev_put(dev);
  526. }
  527. iboe_mac_vlan_to_ll((union ib_gid *) &resp->ib_route[0].dgid,
  528. dev_addr->dst_dev_addr, vid);
  529. iboe_addr_get_sgid(dev_addr,
  530. (union ib_gid *) &resp->ib_route[0].sgid);
  531. resp->ib_route[0].pkey = cpu_to_be16(0xffff);
  532. break;
  533. case 2:
  534. ib_copy_path_rec_to_user(&resp->ib_route[1],
  535. &route->path_rec[1]);
  536. /* fall through */
  537. case 1:
  538. ib_copy_path_rec_to_user(&resp->ib_route[0],
  539. &route->path_rec[0]);
  540. break;
  541. default:
  542. break;
  543. }
  544. }
  545. static void ucma_copy_iw_route(struct rdma_ucm_query_route_resp *resp,
  546. struct rdma_route *route)
  547. {
  548. struct rdma_dev_addr *dev_addr;
  549. dev_addr = &route->addr.dev_addr;
  550. rdma_addr_get_dgid(dev_addr, (union ib_gid *) &resp->ib_route[0].dgid);
  551. rdma_addr_get_sgid(dev_addr, (union ib_gid *) &resp->ib_route[0].sgid);
  552. }
  553. static ssize_t ucma_query_route(struct ucma_file *file,
  554. const char __user *inbuf,
  555. int in_len, int out_len)
  556. {
  557. struct rdma_ucm_query cmd;
  558. struct rdma_ucm_query_route_resp resp;
  559. struct ucma_context *ctx;
  560. struct sockaddr *addr;
  561. int ret = 0;
  562. if (out_len < sizeof(resp))
  563. return -ENOSPC;
  564. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  565. return -EFAULT;
  566. ctx = ucma_get_ctx(file, cmd.id);
  567. if (IS_ERR(ctx))
  568. return PTR_ERR(ctx);
  569. memset(&resp, 0, sizeof resp);
  570. addr = (struct sockaddr *) &ctx->cm_id->route.addr.src_addr;
  571. memcpy(&resp.src_addr, addr, addr->sa_family == AF_INET ?
  572. sizeof(struct sockaddr_in) :
  573. sizeof(struct sockaddr_in6));
  574. addr = (struct sockaddr *) &ctx->cm_id->route.addr.dst_addr;
  575. memcpy(&resp.dst_addr, addr, addr->sa_family == AF_INET ?
  576. sizeof(struct sockaddr_in) :
  577. sizeof(struct sockaddr_in6));
  578. if (!ctx->cm_id->device)
  579. goto out;
  580. resp.node_guid = (__force __u64) ctx->cm_id->device->node_guid;
  581. resp.port_num = ctx->cm_id->port_num;
  582. switch (rdma_node_get_transport(ctx->cm_id->device->node_type)) {
  583. case RDMA_TRANSPORT_IB:
  584. switch (rdma_port_get_link_layer(ctx->cm_id->device,
  585. ctx->cm_id->port_num)) {
  586. case IB_LINK_LAYER_INFINIBAND:
  587. ucma_copy_ib_route(&resp, &ctx->cm_id->route);
  588. break;
  589. case IB_LINK_LAYER_ETHERNET:
  590. ucma_copy_iboe_route(&resp, &ctx->cm_id->route);
  591. break;
  592. default:
  593. break;
  594. }
  595. break;
  596. case RDMA_TRANSPORT_IWARP:
  597. ucma_copy_iw_route(&resp, &ctx->cm_id->route);
  598. break;
  599. default:
  600. break;
  601. }
  602. out:
  603. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  604. &resp, sizeof(resp)))
  605. ret = -EFAULT;
  606. ucma_put_ctx(ctx);
  607. return ret;
  608. }
  609. static void ucma_query_device_addr(struct rdma_cm_id *cm_id,
  610. struct rdma_ucm_query_addr_resp *resp)
  611. {
  612. if (!cm_id->device)
  613. return;
  614. resp->node_guid = (__force __u64) cm_id->device->node_guid;
  615. resp->port_num = cm_id->port_num;
  616. resp->pkey = (__force __u16) cpu_to_be16(
  617. ib_addr_get_pkey(&cm_id->route.addr.dev_addr));
  618. }
  619. static ssize_t ucma_query_addr(struct ucma_context *ctx,
  620. void __user *response, int out_len)
  621. {
  622. struct rdma_ucm_query_addr_resp resp;
  623. struct sockaddr *addr;
  624. int ret = 0;
  625. if (out_len < sizeof(resp))
  626. return -ENOSPC;
  627. memset(&resp, 0, sizeof resp);
  628. addr = (struct sockaddr *) &ctx->cm_id->route.addr.src_addr;
  629. resp.src_size = rdma_addr_size(addr);
  630. memcpy(&resp.src_addr, addr, resp.src_size);
  631. addr = (struct sockaddr *) &ctx->cm_id->route.addr.dst_addr;
  632. resp.dst_size = rdma_addr_size(addr);
  633. memcpy(&resp.dst_addr, addr, resp.dst_size);
  634. ucma_query_device_addr(ctx->cm_id, &resp);
  635. if (copy_to_user(response, &resp, sizeof(resp)))
  636. ret = -EFAULT;
  637. return ret;
  638. }
  639. static ssize_t ucma_query_path(struct ucma_context *ctx,
  640. void __user *response, int out_len)
  641. {
  642. struct rdma_ucm_query_path_resp *resp;
  643. int i, ret = 0;
  644. if (out_len < sizeof(*resp))
  645. return -ENOSPC;
  646. resp = kzalloc(out_len, GFP_KERNEL);
  647. if (!resp)
  648. return -ENOMEM;
  649. resp->num_paths = ctx->cm_id->route.num_paths;
  650. for (i = 0, out_len -= sizeof(*resp);
  651. i < resp->num_paths && out_len > sizeof(struct ib_path_rec_data);
  652. i++, out_len -= sizeof(struct ib_path_rec_data)) {
  653. resp->path_data[i].flags = IB_PATH_GMP | IB_PATH_PRIMARY |
  654. IB_PATH_BIDIRECTIONAL;
  655. ib_sa_pack_path(&ctx->cm_id->route.path_rec[i],
  656. &resp->path_data[i].path_rec);
  657. }
  658. if (copy_to_user(response, resp,
  659. sizeof(*resp) + (i * sizeof(struct ib_path_rec_data))))
  660. ret = -EFAULT;
  661. kfree(resp);
  662. return ret;
  663. }
  664. static ssize_t ucma_query(struct ucma_file *file,
  665. const char __user *inbuf,
  666. int in_len, int out_len)
  667. {
  668. struct rdma_ucm_query cmd;
  669. struct ucma_context *ctx;
  670. void __user *response;
  671. int ret;
  672. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  673. return -EFAULT;
  674. response = (void __user *)(unsigned long) cmd.response;
  675. ctx = ucma_get_ctx(file, cmd.id);
  676. if (IS_ERR(ctx))
  677. return PTR_ERR(ctx);
  678. switch (cmd.option) {
  679. case RDMA_USER_CM_QUERY_ADDR:
  680. ret = ucma_query_addr(ctx, response, out_len);
  681. break;
  682. case RDMA_USER_CM_QUERY_PATH:
  683. ret = ucma_query_path(ctx, response, out_len);
  684. break;
  685. default:
  686. ret = -ENOSYS;
  687. break;
  688. }
  689. ucma_put_ctx(ctx);
  690. return ret;
  691. }
  692. static void ucma_copy_conn_param(struct rdma_cm_id *id,
  693. struct rdma_conn_param *dst,
  694. struct rdma_ucm_conn_param *src)
  695. {
  696. dst->private_data = src->private_data;
  697. dst->private_data_len = src->private_data_len;
  698. dst->responder_resources =src->responder_resources;
  699. dst->initiator_depth = src->initiator_depth;
  700. dst->flow_control = src->flow_control;
  701. dst->retry_count = src->retry_count;
  702. dst->rnr_retry_count = src->rnr_retry_count;
  703. dst->srq = src->srq;
  704. dst->qp_num = src->qp_num;
  705. dst->qkey = (id->route.addr.src_addr.ss_family == AF_IB) ? src->qkey : 0;
  706. }
  707. static ssize_t ucma_connect(struct ucma_file *file, const char __user *inbuf,
  708. int in_len, int out_len)
  709. {
  710. struct rdma_ucm_connect cmd;
  711. struct rdma_conn_param conn_param;
  712. struct ucma_context *ctx;
  713. int ret;
  714. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  715. return -EFAULT;
  716. if (!cmd.conn_param.valid)
  717. return -EINVAL;
  718. ctx = ucma_get_ctx(file, cmd.id);
  719. if (IS_ERR(ctx))
  720. return PTR_ERR(ctx);
  721. ucma_copy_conn_param(ctx->cm_id, &conn_param, &cmd.conn_param);
  722. ret = rdma_connect(ctx->cm_id, &conn_param);
  723. ucma_put_ctx(ctx);
  724. return ret;
  725. }
  726. static ssize_t ucma_listen(struct ucma_file *file, const char __user *inbuf,
  727. int in_len, int out_len)
  728. {
  729. struct rdma_ucm_listen cmd;
  730. struct ucma_context *ctx;
  731. int ret;
  732. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  733. return -EFAULT;
  734. ctx = ucma_get_ctx(file, cmd.id);
  735. if (IS_ERR(ctx))
  736. return PTR_ERR(ctx);
  737. ctx->backlog = cmd.backlog > 0 && cmd.backlog < max_backlog ?
  738. cmd.backlog : max_backlog;
  739. ret = rdma_listen(ctx->cm_id, ctx->backlog);
  740. ucma_put_ctx(ctx);
  741. return ret;
  742. }
  743. static ssize_t ucma_accept(struct ucma_file *file, const char __user *inbuf,
  744. int in_len, int out_len)
  745. {
  746. struct rdma_ucm_accept cmd;
  747. struct rdma_conn_param conn_param;
  748. struct ucma_context *ctx;
  749. int ret;
  750. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  751. return -EFAULT;
  752. ctx = ucma_get_ctx(file, cmd.id);
  753. if (IS_ERR(ctx))
  754. return PTR_ERR(ctx);
  755. if (cmd.conn_param.valid) {
  756. ucma_copy_conn_param(ctx->cm_id, &conn_param, &cmd.conn_param);
  757. mutex_lock(&file->mut);
  758. ret = rdma_accept(ctx->cm_id, &conn_param);
  759. if (!ret)
  760. ctx->uid = cmd.uid;
  761. mutex_unlock(&file->mut);
  762. } else
  763. ret = rdma_accept(ctx->cm_id, NULL);
  764. ucma_put_ctx(ctx);
  765. return ret;
  766. }
  767. static ssize_t ucma_reject(struct ucma_file *file, const char __user *inbuf,
  768. int in_len, int out_len)
  769. {
  770. struct rdma_ucm_reject cmd;
  771. struct ucma_context *ctx;
  772. int ret;
  773. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  774. return -EFAULT;
  775. ctx = ucma_get_ctx(file, cmd.id);
  776. if (IS_ERR(ctx))
  777. return PTR_ERR(ctx);
  778. ret = rdma_reject(ctx->cm_id, cmd.private_data, cmd.private_data_len);
  779. ucma_put_ctx(ctx);
  780. return ret;
  781. }
  782. static ssize_t ucma_disconnect(struct ucma_file *file, const char __user *inbuf,
  783. int in_len, int out_len)
  784. {
  785. struct rdma_ucm_disconnect cmd;
  786. struct ucma_context *ctx;
  787. int ret;
  788. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  789. return -EFAULT;
  790. ctx = ucma_get_ctx(file, cmd.id);
  791. if (IS_ERR(ctx))
  792. return PTR_ERR(ctx);
  793. ret = rdma_disconnect(ctx->cm_id);
  794. ucma_put_ctx(ctx);
  795. return ret;
  796. }
  797. static ssize_t ucma_init_qp_attr(struct ucma_file *file,
  798. const char __user *inbuf,
  799. int in_len, int out_len)
  800. {
  801. struct rdma_ucm_init_qp_attr cmd;
  802. struct ib_uverbs_qp_attr resp;
  803. struct ucma_context *ctx;
  804. struct ib_qp_attr qp_attr;
  805. int ret;
  806. if (out_len < sizeof(resp))
  807. return -ENOSPC;
  808. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  809. return -EFAULT;
  810. ctx = ucma_get_ctx(file, cmd.id);
  811. if (IS_ERR(ctx))
  812. return PTR_ERR(ctx);
  813. resp.qp_attr_mask = 0;
  814. memset(&qp_attr, 0, sizeof qp_attr);
  815. qp_attr.qp_state = cmd.qp_state;
  816. ret = rdma_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
  817. if (ret)
  818. goto out;
  819. ib_copy_qp_attr_to_user(&resp, &qp_attr);
  820. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  821. &resp, sizeof(resp)))
  822. ret = -EFAULT;
  823. out:
  824. ucma_put_ctx(ctx);
  825. return ret;
  826. }
  827. static int ucma_set_option_id(struct ucma_context *ctx, int optname,
  828. void *optval, size_t optlen)
  829. {
  830. int ret = 0;
  831. switch (optname) {
  832. case RDMA_OPTION_ID_TOS:
  833. if (optlen != sizeof(u8)) {
  834. ret = -EINVAL;
  835. break;
  836. }
  837. rdma_set_service_type(ctx->cm_id, *((u8 *) optval));
  838. break;
  839. case RDMA_OPTION_ID_REUSEADDR:
  840. if (optlen != sizeof(int)) {
  841. ret = -EINVAL;
  842. break;
  843. }
  844. ret = rdma_set_reuseaddr(ctx->cm_id, *((int *) optval) ? 1 : 0);
  845. break;
  846. case RDMA_OPTION_ID_AFONLY:
  847. if (optlen != sizeof(int)) {
  848. ret = -EINVAL;
  849. break;
  850. }
  851. ret = rdma_set_afonly(ctx->cm_id, *((int *) optval) ? 1 : 0);
  852. break;
  853. default:
  854. ret = -ENOSYS;
  855. }
  856. return ret;
  857. }
  858. static int ucma_set_ib_path(struct ucma_context *ctx,
  859. struct ib_path_rec_data *path_data, size_t optlen)
  860. {
  861. struct ib_sa_path_rec sa_path;
  862. struct rdma_cm_event event;
  863. int ret;
  864. if (optlen % sizeof(*path_data))
  865. return -EINVAL;
  866. for (; optlen; optlen -= sizeof(*path_data), path_data++) {
  867. if (path_data->flags == (IB_PATH_GMP | IB_PATH_PRIMARY |
  868. IB_PATH_BIDIRECTIONAL))
  869. break;
  870. }
  871. if (!optlen)
  872. return -EINVAL;
  873. ib_sa_unpack_path(path_data->path_rec, &sa_path);
  874. ret = rdma_set_ib_paths(ctx->cm_id, &sa_path, 1);
  875. if (ret)
  876. return ret;
  877. memset(&event, 0, sizeof event);
  878. event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
  879. return ucma_event_handler(ctx->cm_id, &event);
  880. }
  881. static int ucma_set_option_ib(struct ucma_context *ctx, int optname,
  882. void *optval, size_t optlen)
  883. {
  884. int ret;
  885. switch (optname) {
  886. case RDMA_OPTION_IB_PATH:
  887. ret = ucma_set_ib_path(ctx, optval, optlen);
  888. break;
  889. default:
  890. ret = -ENOSYS;
  891. }
  892. return ret;
  893. }
  894. static int ucma_set_option_level(struct ucma_context *ctx, int level,
  895. int optname, void *optval, size_t optlen)
  896. {
  897. int ret;
  898. switch (level) {
  899. case RDMA_OPTION_ID:
  900. ret = ucma_set_option_id(ctx, optname, optval, optlen);
  901. break;
  902. case RDMA_OPTION_IB:
  903. ret = ucma_set_option_ib(ctx, optname, optval, optlen);
  904. break;
  905. default:
  906. ret = -ENOSYS;
  907. }
  908. return ret;
  909. }
  910. static ssize_t ucma_set_option(struct ucma_file *file, const char __user *inbuf,
  911. int in_len, int out_len)
  912. {
  913. struct rdma_ucm_set_option cmd;
  914. struct ucma_context *ctx;
  915. void *optval;
  916. int ret;
  917. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  918. return -EFAULT;
  919. ctx = ucma_get_ctx(file, cmd.id);
  920. if (IS_ERR(ctx))
  921. return PTR_ERR(ctx);
  922. optval = memdup_user((void __user *) (unsigned long) cmd.optval,
  923. cmd.optlen);
  924. if (IS_ERR(optval)) {
  925. ret = PTR_ERR(optval);
  926. goto out;
  927. }
  928. ret = ucma_set_option_level(ctx, cmd.level, cmd.optname, optval,
  929. cmd.optlen);
  930. kfree(optval);
  931. out:
  932. ucma_put_ctx(ctx);
  933. return ret;
  934. }
  935. static ssize_t ucma_notify(struct ucma_file *file, const char __user *inbuf,
  936. int in_len, int out_len)
  937. {
  938. struct rdma_ucm_notify cmd;
  939. struct ucma_context *ctx;
  940. int ret;
  941. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  942. return -EFAULT;
  943. ctx = ucma_get_ctx(file, cmd.id);
  944. if (IS_ERR(ctx))
  945. return PTR_ERR(ctx);
  946. ret = rdma_notify(ctx->cm_id, (enum ib_event_type) cmd.event);
  947. ucma_put_ctx(ctx);
  948. return ret;
  949. }
  950. static ssize_t ucma_join_multicast(struct ucma_file *file,
  951. const char __user *inbuf,
  952. int in_len, int out_len)
  953. {
  954. struct rdma_ucm_join_mcast cmd;
  955. struct rdma_ucm_create_id_resp resp;
  956. struct ucma_context *ctx;
  957. struct ucma_multicast *mc;
  958. int ret;
  959. if (out_len < sizeof(resp))
  960. return -ENOSPC;
  961. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  962. return -EFAULT;
  963. ctx = ucma_get_ctx(file, cmd.id);
  964. if (IS_ERR(ctx))
  965. return PTR_ERR(ctx);
  966. mutex_lock(&file->mut);
  967. mc = ucma_alloc_multicast(ctx);
  968. if (!mc) {
  969. ret = -ENOMEM;
  970. goto err1;
  971. }
  972. mc->uid = cmd.uid;
  973. memcpy(&mc->addr, &cmd.addr, sizeof cmd.addr);
  974. ret = rdma_join_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr, mc);
  975. if (ret)
  976. goto err2;
  977. resp.id = mc->id;
  978. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  979. &resp, sizeof(resp))) {
  980. ret = -EFAULT;
  981. goto err3;
  982. }
  983. mutex_unlock(&file->mut);
  984. ucma_put_ctx(ctx);
  985. return 0;
  986. err3:
  987. rdma_leave_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr);
  988. ucma_cleanup_mc_events(mc);
  989. err2:
  990. mutex_lock(&mut);
  991. idr_remove(&multicast_idr, mc->id);
  992. mutex_unlock(&mut);
  993. list_del(&mc->list);
  994. kfree(mc);
  995. err1:
  996. mutex_unlock(&file->mut);
  997. ucma_put_ctx(ctx);
  998. return ret;
  999. }
  1000. static ssize_t ucma_leave_multicast(struct ucma_file *file,
  1001. const char __user *inbuf,
  1002. int in_len, int out_len)
  1003. {
  1004. struct rdma_ucm_destroy_id cmd;
  1005. struct rdma_ucm_destroy_id_resp resp;
  1006. struct ucma_multicast *mc;
  1007. int ret = 0;
  1008. if (out_len < sizeof(resp))
  1009. return -ENOSPC;
  1010. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  1011. return -EFAULT;
  1012. mutex_lock(&mut);
  1013. mc = idr_find(&multicast_idr, cmd.id);
  1014. if (!mc)
  1015. mc = ERR_PTR(-ENOENT);
  1016. else if (mc->ctx->file != file)
  1017. mc = ERR_PTR(-EINVAL);
  1018. else {
  1019. idr_remove(&multicast_idr, mc->id);
  1020. atomic_inc(&mc->ctx->ref);
  1021. }
  1022. mutex_unlock(&mut);
  1023. if (IS_ERR(mc)) {
  1024. ret = PTR_ERR(mc);
  1025. goto out;
  1026. }
  1027. rdma_leave_multicast(mc->ctx->cm_id, (struct sockaddr *) &mc->addr);
  1028. mutex_lock(&mc->ctx->file->mut);
  1029. ucma_cleanup_mc_events(mc);
  1030. list_del(&mc->list);
  1031. mutex_unlock(&mc->ctx->file->mut);
  1032. ucma_put_ctx(mc->ctx);
  1033. resp.events_reported = mc->events_reported;
  1034. kfree(mc);
  1035. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  1036. &resp, sizeof(resp)))
  1037. ret = -EFAULT;
  1038. out:
  1039. return ret;
  1040. }
  1041. static void ucma_lock_files(struct ucma_file *file1, struct ucma_file *file2)
  1042. {
  1043. /* Acquire mutex's based on pointer comparison to prevent deadlock. */
  1044. if (file1 < file2) {
  1045. mutex_lock(&file1->mut);
  1046. mutex_lock(&file2->mut);
  1047. } else {
  1048. mutex_lock(&file2->mut);
  1049. mutex_lock(&file1->mut);
  1050. }
  1051. }
  1052. static void ucma_unlock_files(struct ucma_file *file1, struct ucma_file *file2)
  1053. {
  1054. if (file1 < file2) {
  1055. mutex_unlock(&file2->mut);
  1056. mutex_unlock(&file1->mut);
  1057. } else {
  1058. mutex_unlock(&file1->mut);
  1059. mutex_unlock(&file2->mut);
  1060. }
  1061. }
  1062. static void ucma_move_events(struct ucma_context *ctx, struct ucma_file *file)
  1063. {
  1064. struct ucma_event *uevent, *tmp;
  1065. list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list)
  1066. if (uevent->ctx == ctx)
  1067. list_move_tail(&uevent->list, &file->event_list);
  1068. }
  1069. static ssize_t ucma_migrate_id(struct ucma_file *new_file,
  1070. const char __user *inbuf,
  1071. int in_len, int out_len)
  1072. {
  1073. struct rdma_ucm_migrate_id cmd;
  1074. struct rdma_ucm_migrate_resp resp;
  1075. struct ucma_context *ctx;
  1076. struct fd f;
  1077. struct ucma_file *cur_file;
  1078. int ret = 0;
  1079. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  1080. return -EFAULT;
  1081. /* Get current fd to protect against it being closed */
  1082. f = fdget(cmd.fd);
  1083. if (!f.file)
  1084. return -ENOENT;
  1085. /* Validate current fd and prevent destruction of id. */
  1086. ctx = ucma_get_ctx(f.file->private_data, cmd.id);
  1087. if (IS_ERR(ctx)) {
  1088. ret = PTR_ERR(ctx);
  1089. goto file_put;
  1090. }
  1091. cur_file = ctx->file;
  1092. if (cur_file == new_file) {
  1093. resp.events_reported = ctx->events_reported;
  1094. goto response;
  1095. }
  1096. /*
  1097. * Migrate events between fd's, maintaining order, and avoiding new
  1098. * events being added before existing events.
  1099. */
  1100. ucma_lock_files(cur_file, new_file);
  1101. mutex_lock(&mut);
  1102. list_move_tail(&ctx->list, &new_file->ctx_list);
  1103. ucma_move_events(ctx, new_file);
  1104. ctx->file = new_file;
  1105. resp.events_reported = ctx->events_reported;
  1106. mutex_unlock(&mut);
  1107. ucma_unlock_files(cur_file, new_file);
  1108. response:
  1109. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  1110. &resp, sizeof(resp)))
  1111. ret = -EFAULT;
  1112. ucma_put_ctx(ctx);
  1113. file_put:
  1114. fdput(f);
  1115. return ret;
  1116. }
  1117. static ssize_t (*ucma_cmd_table[])(struct ucma_file *file,
  1118. const char __user *inbuf,
  1119. int in_len, int out_len) = {
  1120. [RDMA_USER_CM_CMD_CREATE_ID] = ucma_create_id,
  1121. [RDMA_USER_CM_CMD_DESTROY_ID] = ucma_destroy_id,
  1122. [RDMA_USER_CM_CMD_BIND_ADDR] = ucma_bind_addr,
  1123. [RDMA_USER_CM_CMD_RESOLVE_ADDR] = ucma_resolve_addr,
  1124. [RDMA_USER_CM_CMD_RESOLVE_ROUTE]= ucma_resolve_route,
  1125. [RDMA_USER_CM_CMD_QUERY_ROUTE] = ucma_query_route,
  1126. [RDMA_USER_CM_CMD_CONNECT] = ucma_connect,
  1127. [RDMA_USER_CM_CMD_LISTEN] = ucma_listen,
  1128. [RDMA_USER_CM_CMD_ACCEPT] = ucma_accept,
  1129. [RDMA_USER_CM_CMD_REJECT] = ucma_reject,
  1130. [RDMA_USER_CM_CMD_DISCONNECT] = ucma_disconnect,
  1131. [RDMA_USER_CM_CMD_INIT_QP_ATTR] = ucma_init_qp_attr,
  1132. [RDMA_USER_CM_CMD_GET_EVENT] = ucma_get_event,
  1133. [RDMA_USER_CM_CMD_GET_OPTION] = NULL,
  1134. [RDMA_USER_CM_CMD_SET_OPTION] = ucma_set_option,
  1135. [RDMA_USER_CM_CMD_NOTIFY] = ucma_notify,
  1136. [RDMA_USER_CM_CMD_JOIN_MCAST] = ucma_join_multicast,
  1137. [RDMA_USER_CM_CMD_LEAVE_MCAST] = ucma_leave_multicast,
  1138. [RDMA_USER_CM_CMD_MIGRATE_ID] = ucma_migrate_id,
  1139. [RDMA_USER_CM_CMD_QUERY] = ucma_query
  1140. };
  1141. static ssize_t ucma_write(struct file *filp, const char __user *buf,
  1142. size_t len, loff_t *pos)
  1143. {
  1144. struct ucma_file *file = filp->private_data;
  1145. struct rdma_ucm_cmd_hdr hdr;
  1146. ssize_t ret;
  1147. if (len < sizeof(hdr))
  1148. return -EINVAL;
  1149. if (copy_from_user(&hdr, buf, sizeof(hdr)))
  1150. return -EFAULT;
  1151. if (hdr.cmd >= ARRAY_SIZE(ucma_cmd_table))
  1152. return -EINVAL;
  1153. if (hdr.in + sizeof(hdr) > len)
  1154. return -EINVAL;
  1155. if (!ucma_cmd_table[hdr.cmd])
  1156. return -ENOSYS;
  1157. ret = ucma_cmd_table[hdr.cmd](file, buf + sizeof(hdr), hdr.in, hdr.out);
  1158. if (!ret)
  1159. ret = len;
  1160. return ret;
  1161. }
  1162. static unsigned int ucma_poll(struct file *filp, struct poll_table_struct *wait)
  1163. {
  1164. struct ucma_file *file = filp->private_data;
  1165. unsigned int mask = 0;
  1166. poll_wait(filp, &file->poll_wait, wait);
  1167. if (!list_empty(&file->event_list))
  1168. mask = POLLIN | POLLRDNORM;
  1169. return mask;
  1170. }
  1171. /*
  1172. * ucma_open() does not need the BKL:
  1173. *
  1174. * - no global state is referred to;
  1175. * - there is no ioctl method to race against;
  1176. * - no further module initialization is required for open to work
  1177. * after the device is registered.
  1178. */
  1179. static int ucma_open(struct inode *inode, struct file *filp)
  1180. {
  1181. struct ucma_file *file;
  1182. file = kmalloc(sizeof *file, GFP_KERNEL);
  1183. if (!file)
  1184. return -ENOMEM;
  1185. INIT_LIST_HEAD(&file->event_list);
  1186. INIT_LIST_HEAD(&file->ctx_list);
  1187. init_waitqueue_head(&file->poll_wait);
  1188. mutex_init(&file->mut);
  1189. filp->private_data = file;
  1190. file->filp = filp;
  1191. return nonseekable_open(inode, filp);
  1192. }
  1193. static int ucma_close(struct inode *inode, struct file *filp)
  1194. {
  1195. struct ucma_file *file = filp->private_data;
  1196. struct ucma_context *ctx, *tmp;
  1197. mutex_lock(&file->mut);
  1198. list_for_each_entry_safe(ctx, tmp, &file->ctx_list, list) {
  1199. mutex_unlock(&file->mut);
  1200. mutex_lock(&mut);
  1201. idr_remove(&ctx_idr, ctx->id);
  1202. mutex_unlock(&mut);
  1203. ucma_free_ctx(ctx);
  1204. mutex_lock(&file->mut);
  1205. }
  1206. mutex_unlock(&file->mut);
  1207. kfree(file);
  1208. return 0;
  1209. }
  1210. static const struct file_operations ucma_fops = {
  1211. .owner = THIS_MODULE,
  1212. .open = ucma_open,
  1213. .release = ucma_close,
  1214. .write = ucma_write,
  1215. .poll = ucma_poll,
  1216. .llseek = no_llseek,
  1217. };
  1218. static struct miscdevice ucma_misc = {
  1219. .minor = MISC_DYNAMIC_MINOR,
  1220. .name = "rdma_cm",
  1221. .nodename = "infiniband/rdma_cm",
  1222. .mode = 0666,
  1223. .fops = &ucma_fops,
  1224. };
  1225. static ssize_t show_abi_version(struct device *dev,
  1226. struct device_attribute *attr,
  1227. char *buf)
  1228. {
  1229. return sprintf(buf, "%d\n", RDMA_USER_CM_ABI_VERSION);
  1230. }
  1231. static DEVICE_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  1232. static int __init ucma_init(void)
  1233. {
  1234. int ret;
  1235. ret = misc_register(&ucma_misc);
  1236. if (ret)
  1237. return ret;
  1238. ret = device_create_file(ucma_misc.this_device, &dev_attr_abi_version);
  1239. if (ret) {
  1240. printk(KERN_ERR "rdma_ucm: couldn't create abi_version attr\n");
  1241. goto err1;
  1242. }
  1243. ucma_ctl_table_hdr = register_net_sysctl(&init_net, "net/rdma_ucm", ucma_ctl_table);
  1244. if (!ucma_ctl_table_hdr) {
  1245. printk(KERN_ERR "rdma_ucm: couldn't register sysctl paths\n");
  1246. ret = -ENOMEM;
  1247. goto err2;
  1248. }
  1249. return 0;
  1250. err2:
  1251. device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
  1252. err1:
  1253. misc_deregister(&ucma_misc);
  1254. return ret;
  1255. }
  1256. static void __exit ucma_cleanup(void)
  1257. {
  1258. unregister_net_sysctl_table(ucma_ctl_table_hdr);
  1259. device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
  1260. misc_deregister(&ucma_misc);
  1261. idr_destroy(&ctx_idr);
  1262. }
  1263. module_init(ucma_init);
  1264. module_exit(ucma_cleanup);