ucma.c 32 KB

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