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. struct ucma_file {
  64. struct mutex mut;
  65. struct file *filp;
  66. struct list_head ctx_list;
  67. struct list_head event_list;
  68. wait_queue_head_t poll_wait;
  69. };
  70. struct ucma_context {
  71. int id;
  72. struct completion comp;
  73. atomic_t ref;
  74. int events_reported;
  75. int backlog;
  76. struct ucma_file *file;
  77. struct rdma_cm_id *cm_id;
  78. u64 uid;
  79. struct list_head list;
  80. struct list_head mc_list;
  81. };
  82. struct ucma_multicast {
  83. struct ucma_context *ctx;
  84. int id;
  85. int events_reported;
  86. u64 uid;
  87. struct list_head list;
  88. struct sockaddr_storage addr;
  89. };
  90. struct ucma_event {
  91. struct ucma_context *ctx;
  92. struct ucma_multicast *mc;
  93. struct list_head list;
  94. struct rdma_cm_id *cm_id;
  95. struct rdma_ucm_event_resp resp;
  96. };
  97. static DEFINE_MUTEX(mut);
  98. static DEFINE_IDR(ctx_idr);
  99. static DEFINE_IDR(multicast_idr);
  100. static inline struct ucma_context *_ucma_find_context(int id,
  101. struct ucma_file *file)
  102. {
  103. struct ucma_context *ctx;
  104. ctx = idr_find(&ctx_idr, id);
  105. if (!ctx)
  106. ctx = ERR_PTR(-ENOENT);
  107. else if (ctx->file != file)
  108. ctx = ERR_PTR(-EINVAL);
  109. return ctx;
  110. }
  111. static struct ucma_context *ucma_get_ctx(struct ucma_file *file, int id)
  112. {
  113. struct ucma_context *ctx;
  114. mutex_lock(&mut);
  115. ctx = _ucma_find_context(id, file);
  116. if (!IS_ERR(ctx))
  117. atomic_inc(&ctx->ref);
  118. mutex_unlock(&mut);
  119. return ctx;
  120. }
  121. static void ucma_put_ctx(struct ucma_context *ctx)
  122. {
  123. if (atomic_dec_and_test(&ctx->ref))
  124. complete(&ctx->comp);
  125. }
  126. static struct ucma_context *ucma_alloc_ctx(struct ucma_file *file)
  127. {
  128. struct ucma_context *ctx;
  129. int ret;
  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. do {
  138. ret = idr_pre_get(&ctx_idr, GFP_KERNEL);
  139. if (!ret)
  140. goto error;
  141. mutex_lock(&mut);
  142. ret = idr_get_new(&ctx_idr, ctx, &ctx->id);
  143. mutex_unlock(&mut);
  144. } while (ret == -EAGAIN);
  145. if (ret)
  146. goto error;
  147. list_add_tail(&ctx->list, &file->ctx_list);
  148. return ctx;
  149. error:
  150. kfree(ctx);
  151. return NULL;
  152. }
  153. static struct ucma_multicast* ucma_alloc_multicast(struct ucma_context *ctx)
  154. {
  155. struct ucma_multicast *mc;
  156. int ret;
  157. mc = kzalloc(sizeof(*mc), GFP_KERNEL);
  158. if (!mc)
  159. return NULL;
  160. do {
  161. ret = idr_pre_get(&multicast_idr, GFP_KERNEL);
  162. if (!ret)
  163. goto error;
  164. mutex_lock(&mut);
  165. ret = idr_get_new(&multicast_idr, mc, &mc->id);
  166. mutex_unlock(&mut);
  167. } while (ret == -EAGAIN);
  168. if (ret)
  169. goto error;
  170. mc->ctx = ctx;
  171. list_add_tail(&mc->list, &ctx->mc_list);
  172. return mc;
  173. error:
  174. kfree(mc);
  175. return NULL;
  176. }
  177. static void ucma_copy_conn_event(struct rdma_ucm_conn_param *dst,
  178. struct rdma_conn_param *src)
  179. {
  180. if (src->private_data_len)
  181. memcpy(dst->private_data, src->private_data,
  182. src->private_data_len);
  183. dst->private_data_len = src->private_data_len;
  184. dst->responder_resources =src->responder_resources;
  185. dst->initiator_depth = src->initiator_depth;
  186. dst->flow_control = src->flow_control;
  187. dst->retry_count = src->retry_count;
  188. dst->rnr_retry_count = src->rnr_retry_count;
  189. dst->srq = src->srq;
  190. dst->qp_num = src->qp_num;
  191. }
  192. static void ucma_copy_ud_event(struct rdma_ucm_ud_param *dst,
  193. struct rdma_ud_param *src)
  194. {
  195. if (src->private_data_len)
  196. memcpy(dst->private_data, src->private_data,
  197. src->private_data_len);
  198. dst->private_data_len = src->private_data_len;
  199. ib_copy_ah_attr_to_user(&dst->ah_attr, &src->ah_attr);
  200. dst->qp_num = src->qp_num;
  201. dst->qkey = src->qkey;
  202. }
  203. static void ucma_set_event_context(struct ucma_context *ctx,
  204. struct rdma_cm_event *event,
  205. struct ucma_event *uevent)
  206. {
  207. uevent->ctx = ctx;
  208. switch (event->event) {
  209. case RDMA_CM_EVENT_MULTICAST_JOIN:
  210. case RDMA_CM_EVENT_MULTICAST_ERROR:
  211. uevent->mc = (struct ucma_multicast *)
  212. event->param.ud.private_data;
  213. uevent->resp.uid = uevent->mc->uid;
  214. uevent->resp.id = uevent->mc->id;
  215. break;
  216. default:
  217. uevent->resp.uid = ctx->uid;
  218. uevent->resp.id = ctx->id;
  219. break;
  220. }
  221. }
  222. static int ucma_event_handler(struct rdma_cm_id *cm_id,
  223. struct rdma_cm_event *event)
  224. {
  225. struct ucma_event *uevent;
  226. struct ucma_context *ctx = cm_id->context;
  227. int ret = 0;
  228. uevent = kzalloc(sizeof(*uevent), GFP_KERNEL);
  229. if (!uevent)
  230. return event->event == RDMA_CM_EVENT_CONNECT_REQUEST;
  231. mutex_lock(&ctx->file->mut);
  232. uevent->cm_id = cm_id;
  233. ucma_set_event_context(ctx, event, uevent);
  234. uevent->resp.event = event->event;
  235. uevent->resp.status = event->status;
  236. if (cm_id->qp_type == IB_QPT_UD)
  237. ucma_copy_ud_event(&uevent->resp.param.ud, &event->param.ud);
  238. else
  239. ucma_copy_conn_event(&uevent->resp.param.conn,
  240. &event->param.conn);
  241. if (event->event == RDMA_CM_EVENT_CONNECT_REQUEST) {
  242. if (!ctx->backlog) {
  243. ret = -ENOMEM;
  244. kfree(uevent);
  245. goto out;
  246. }
  247. ctx->backlog--;
  248. } else if (!ctx->uid) {
  249. /*
  250. * We ignore events for new connections until userspace has set
  251. * their context. This can only happen if an error occurs on a
  252. * new connection before the user accepts it. This is okay,
  253. * since the accept will just fail later.
  254. */
  255. kfree(uevent);
  256. goto out;
  257. }
  258. list_add_tail(&uevent->list, &ctx->file->event_list);
  259. wake_up_interruptible(&ctx->file->poll_wait);
  260. out:
  261. mutex_unlock(&ctx->file->mut);
  262. return ret;
  263. }
  264. static ssize_t ucma_get_event(struct ucma_file *file, const char __user *inbuf,
  265. int in_len, int out_len)
  266. {
  267. struct ucma_context *ctx;
  268. struct rdma_ucm_get_event cmd;
  269. struct ucma_event *uevent;
  270. int ret = 0;
  271. if (out_len < sizeof uevent->resp)
  272. return -ENOSPC;
  273. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  274. return -EFAULT;
  275. mutex_lock(&file->mut);
  276. while (list_empty(&file->event_list)) {
  277. mutex_unlock(&file->mut);
  278. if (file->filp->f_flags & O_NONBLOCK)
  279. return -EAGAIN;
  280. if (wait_event_interruptible(file->poll_wait,
  281. !list_empty(&file->event_list)))
  282. return -ERESTARTSYS;
  283. mutex_lock(&file->mut);
  284. }
  285. uevent = list_entry(file->event_list.next, struct ucma_event, list);
  286. if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST) {
  287. ctx = ucma_alloc_ctx(file);
  288. if (!ctx) {
  289. ret = -ENOMEM;
  290. goto done;
  291. }
  292. uevent->ctx->backlog++;
  293. ctx->cm_id = uevent->cm_id;
  294. ctx->cm_id->context = ctx;
  295. uevent->resp.id = ctx->id;
  296. }
  297. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  298. &uevent->resp, sizeof uevent->resp)) {
  299. ret = -EFAULT;
  300. goto done;
  301. }
  302. list_del(&uevent->list);
  303. uevent->ctx->events_reported++;
  304. if (uevent->mc)
  305. uevent->mc->events_reported++;
  306. kfree(uevent);
  307. done:
  308. mutex_unlock(&file->mut);
  309. return ret;
  310. }
  311. static int ucma_get_qp_type(struct rdma_ucm_create_id *cmd, enum ib_qp_type *qp_type)
  312. {
  313. switch (cmd->ps) {
  314. case RDMA_PS_TCP:
  315. *qp_type = IB_QPT_RC;
  316. return 0;
  317. case RDMA_PS_UDP:
  318. case RDMA_PS_IPOIB:
  319. *qp_type = IB_QPT_UD;
  320. return 0;
  321. case RDMA_PS_IB:
  322. *qp_type = cmd->qp_type;
  323. return 0;
  324. default:
  325. return -EINVAL;
  326. }
  327. }
  328. static ssize_t ucma_create_id(struct ucma_file *file, const char __user *inbuf,
  329. int in_len, int out_len)
  330. {
  331. struct rdma_ucm_create_id cmd;
  332. struct rdma_ucm_create_id_resp resp;
  333. struct ucma_context *ctx;
  334. enum ib_qp_type qp_type;
  335. int ret;
  336. if (out_len < sizeof(resp))
  337. return -ENOSPC;
  338. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  339. return -EFAULT;
  340. ret = ucma_get_qp_type(&cmd, &qp_type);
  341. if (ret)
  342. return ret;
  343. mutex_lock(&file->mut);
  344. ctx = ucma_alloc_ctx(file);
  345. mutex_unlock(&file->mut);
  346. if (!ctx)
  347. return -ENOMEM;
  348. ctx->uid = cmd.uid;
  349. ctx->cm_id = rdma_create_id(ucma_event_handler, ctx, cmd.ps, qp_type);
  350. if (IS_ERR(ctx->cm_id)) {
  351. ret = PTR_ERR(ctx->cm_id);
  352. goto err1;
  353. }
  354. resp.id = ctx->id;
  355. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  356. &resp, sizeof(resp))) {
  357. ret = -EFAULT;
  358. goto err2;
  359. }
  360. return 0;
  361. err2:
  362. rdma_destroy_id(ctx->cm_id);
  363. err1:
  364. mutex_lock(&mut);
  365. idr_remove(&ctx_idr, ctx->id);
  366. mutex_unlock(&mut);
  367. kfree(ctx);
  368. return ret;
  369. }
  370. static void ucma_cleanup_multicast(struct ucma_context *ctx)
  371. {
  372. struct ucma_multicast *mc, *tmp;
  373. mutex_lock(&mut);
  374. list_for_each_entry_safe(mc, tmp, &ctx->mc_list, list) {
  375. list_del(&mc->list);
  376. idr_remove(&multicast_idr, mc->id);
  377. kfree(mc);
  378. }
  379. mutex_unlock(&mut);
  380. }
  381. static void ucma_cleanup_mc_events(struct ucma_multicast *mc)
  382. {
  383. struct ucma_event *uevent, *tmp;
  384. list_for_each_entry_safe(uevent, tmp, &mc->ctx->file->event_list, list) {
  385. if (uevent->mc != mc)
  386. continue;
  387. list_del(&uevent->list);
  388. kfree(uevent);
  389. }
  390. }
  391. /*
  392. * We cannot hold file->mut when calling rdma_destroy_id() or we can
  393. * deadlock. We also acquire file->mut in ucma_event_handler(), and
  394. * rdma_destroy_id() will wait until all callbacks have completed.
  395. */
  396. static int ucma_free_ctx(struct ucma_context *ctx)
  397. {
  398. int events_reported;
  399. struct ucma_event *uevent, *tmp;
  400. LIST_HEAD(list);
  401. /* No new events will be generated after destroying the id. */
  402. rdma_destroy_id(ctx->cm_id);
  403. ucma_cleanup_multicast(ctx);
  404. /* Cleanup events not yet reported to the user. */
  405. mutex_lock(&ctx->file->mut);
  406. list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list) {
  407. if (uevent->ctx == ctx)
  408. list_move_tail(&uevent->list, &list);
  409. }
  410. list_del(&ctx->list);
  411. mutex_unlock(&ctx->file->mut);
  412. list_for_each_entry_safe(uevent, tmp, &list, list) {
  413. list_del(&uevent->list);
  414. if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST)
  415. rdma_destroy_id(uevent->cm_id);
  416. kfree(uevent);
  417. }
  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. ucma_copy_conn_param(&conn_param, &cmd.conn_param);
  683. mutex_lock(&file->mut);
  684. ret = rdma_accept(ctx->cm_id, &conn_param);
  685. if (!ret)
  686. ctx->uid = cmd.uid;
  687. mutex_unlock(&file->mut);
  688. } else
  689. ret = rdma_accept(ctx->cm_id, NULL);
  690. ucma_put_ctx(ctx);
  691. return ret;
  692. }
  693. static ssize_t ucma_reject(struct ucma_file *file, const char __user *inbuf,
  694. int in_len, int out_len)
  695. {
  696. struct rdma_ucm_reject cmd;
  697. struct ucma_context *ctx;
  698. int ret;
  699. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  700. return -EFAULT;
  701. ctx = ucma_get_ctx(file, cmd.id);
  702. if (IS_ERR(ctx))
  703. return PTR_ERR(ctx);
  704. ret = rdma_reject(ctx->cm_id, cmd.private_data, cmd.private_data_len);
  705. ucma_put_ctx(ctx);
  706. return ret;
  707. }
  708. static ssize_t ucma_disconnect(struct ucma_file *file, const char __user *inbuf,
  709. int in_len, int out_len)
  710. {
  711. struct rdma_ucm_disconnect cmd;
  712. struct ucma_context *ctx;
  713. int ret;
  714. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  715. return -EFAULT;
  716. ctx = ucma_get_ctx(file, cmd.id);
  717. if (IS_ERR(ctx))
  718. return PTR_ERR(ctx);
  719. ret = rdma_disconnect(ctx->cm_id);
  720. ucma_put_ctx(ctx);
  721. return ret;
  722. }
  723. static ssize_t ucma_init_qp_attr(struct ucma_file *file,
  724. const char __user *inbuf,
  725. int in_len, int out_len)
  726. {
  727. struct rdma_ucm_init_qp_attr cmd;
  728. struct ib_uverbs_qp_attr resp;
  729. struct ucma_context *ctx;
  730. struct ib_qp_attr qp_attr;
  731. int ret;
  732. if (out_len < sizeof(resp))
  733. return -ENOSPC;
  734. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  735. return -EFAULT;
  736. ctx = ucma_get_ctx(file, cmd.id);
  737. if (IS_ERR(ctx))
  738. return PTR_ERR(ctx);
  739. resp.qp_attr_mask = 0;
  740. memset(&qp_attr, 0, sizeof qp_attr);
  741. qp_attr.qp_state = cmd.qp_state;
  742. ret = rdma_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
  743. if (ret)
  744. goto out;
  745. ib_copy_qp_attr_to_user(&resp, &qp_attr);
  746. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  747. &resp, sizeof(resp)))
  748. ret = -EFAULT;
  749. out:
  750. ucma_put_ctx(ctx);
  751. return ret;
  752. }
  753. static int ucma_set_option_id(struct ucma_context *ctx, int optname,
  754. void *optval, size_t optlen)
  755. {
  756. int ret = 0;
  757. switch (optname) {
  758. case RDMA_OPTION_ID_TOS:
  759. if (optlen != sizeof(u8)) {
  760. ret = -EINVAL;
  761. break;
  762. }
  763. rdma_set_service_type(ctx->cm_id, *((u8 *) optval));
  764. break;
  765. case RDMA_OPTION_ID_REUSEADDR:
  766. if (optlen != sizeof(int)) {
  767. ret = -EINVAL;
  768. break;
  769. }
  770. ret = rdma_set_reuseaddr(ctx->cm_id, *((int *) optval) ? 1 : 0);
  771. break;
  772. case RDMA_OPTION_ID_AFONLY:
  773. if (optlen != sizeof(int)) {
  774. ret = -EINVAL;
  775. break;
  776. }
  777. ret = rdma_set_afonly(ctx->cm_id, *((int *) optval) ? 1 : 0);
  778. break;
  779. default:
  780. ret = -ENOSYS;
  781. }
  782. return ret;
  783. }
  784. static int ucma_set_ib_path(struct ucma_context *ctx,
  785. struct ib_path_rec_data *path_data, size_t optlen)
  786. {
  787. struct ib_sa_path_rec sa_path;
  788. struct rdma_cm_event event;
  789. int ret;
  790. if (optlen % sizeof(*path_data))
  791. return -EINVAL;
  792. for (; optlen; optlen -= sizeof(*path_data), path_data++) {
  793. if (path_data->flags == (IB_PATH_GMP | IB_PATH_PRIMARY |
  794. IB_PATH_BIDIRECTIONAL))
  795. break;
  796. }
  797. if (!optlen)
  798. return -EINVAL;
  799. ib_sa_unpack_path(path_data->path_rec, &sa_path);
  800. ret = rdma_set_ib_paths(ctx->cm_id, &sa_path, 1);
  801. if (ret)
  802. return ret;
  803. memset(&event, 0, sizeof event);
  804. event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
  805. return ucma_event_handler(ctx->cm_id, &event);
  806. }
  807. static int ucma_set_option_ib(struct ucma_context *ctx, int optname,
  808. void *optval, size_t optlen)
  809. {
  810. int ret;
  811. switch (optname) {
  812. case RDMA_OPTION_IB_PATH:
  813. ret = ucma_set_ib_path(ctx, optval, optlen);
  814. break;
  815. default:
  816. ret = -ENOSYS;
  817. }
  818. return ret;
  819. }
  820. static int ucma_set_option_level(struct ucma_context *ctx, int level,
  821. int optname, void *optval, size_t optlen)
  822. {
  823. int ret;
  824. switch (level) {
  825. case RDMA_OPTION_ID:
  826. ret = ucma_set_option_id(ctx, optname, optval, optlen);
  827. break;
  828. case RDMA_OPTION_IB:
  829. ret = ucma_set_option_ib(ctx, optname, optval, optlen);
  830. break;
  831. default:
  832. ret = -ENOSYS;
  833. }
  834. return ret;
  835. }
  836. static ssize_t ucma_set_option(struct ucma_file *file, const char __user *inbuf,
  837. int in_len, int out_len)
  838. {
  839. struct rdma_ucm_set_option cmd;
  840. struct ucma_context *ctx;
  841. void *optval;
  842. int ret;
  843. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  844. return -EFAULT;
  845. ctx = ucma_get_ctx(file, cmd.id);
  846. if (IS_ERR(ctx))
  847. return PTR_ERR(ctx);
  848. optval = memdup_user((void __user *) (unsigned long) cmd.optval,
  849. cmd.optlen);
  850. if (IS_ERR(optval)) {
  851. ret = PTR_ERR(optval);
  852. goto out;
  853. }
  854. ret = ucma_set_option_level(ctx, cmd.level, cmd.optname, optval,
  855. cmd.optlen);
  856. kfree(optval);
  857. out:
  858. ucma_put_ctx(ctx);
  859. return ret;
  860. }
  861. static ssize_t ucma_notify(struct ucma_file *file, const char __user *inbuf,
  862. int in_len, int out_len)
  863. {
  864. struct rdma_ucm_notify cmd;
  865. struct ucma_context *ctx;
  866. int ret;
  867. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  868. return -EFAULT;
  869. ctx = ucma_get_ctx(file, cmd.id);
  870. if (IS_ERR(ctx))
  871. return PTR_ERR(ctx);
  872. ret = rdma_notify(ctx->cm_id, (enum ib_event_type) cmd.event);
  873. ucma_put_ctx(ctx);
  874. return ret;
  875. }
  876. static ssize_t ucma_join_multicast(struct ucma_file *file,
  877. const char __user *inbuf,
  878. int in_len, int out_len)
  879. {
  880. struct rdma_ucm_join_mcast cmd;
  881. struct rdma_ucm_create_id_resp resp;
  882. struct ucma_context *ctx;
  883. struct ucma_multicast *mc;
  884. int ret;
  885. if (out_len < sizeof(resp))
  886. return -ENOSPC;
  887. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  888. return -EFAULT;
  889. ctx = ucma_get_ctx(file, cmd.id);
  890. if (IS_ERR(ctx))
  891. return PTR_ERR(ctx);
  892. mutex_lock(&file->mut);
  893. mc = ucma_alloc_multicast(ctx);
  894. if (!mc) {
  895. ret = -ENOMEM;
  896. goto err1;
  897. }
  898. mc->uid = cmd.uid;
  899. memcpy(&mc->addr, &cmd.addr, sizeof cmd.addr);
  900. ret = rdma_join_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr, mc);
  901. if (ret)
  902. goto err2;
  903. resp.id = mc->id;
  904. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  905. &resp, sizeof(resp))) {
  906. ret = -EFAULT;
  907. goto err3;
  908. }
  909. mutex_unlock(&file->mut);
  910. ucma_put_ctx(ctx);
  911. return 0;
  912. err3:
  913. rdma_leave_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr);
  914. ucma_cleanup_mc_events(mc);
  915. err2:
  916. mutex_lock(&mut);
  917. idr_remove(&multicast_idr, mc->id);
  918. mutex_unlock(&mut);
  919. list_del(&mc->list);
  920. kfree(mc);
  921. err1:
  922. mutex_unlock(&file->mut);
  923. ucma_put_ctx(ctx);
  924. return ret;
  925. }
  926. static ssize_t ucma_leave_multicast(struct ucma_file *file,
  927. const char __user *inbuf,
  928. int in_len, int out_len)
  929. {
  930. struct rdma_ucm_destroy_id cmd;
  931. struct rdma_ucm_destroy_id_resp resp;
  932. struct ucma_multicast *mc;
  933. int ret = 0;
  934. if (out_len < sizeof(resp))
  935. return -ENOSPC;
  936. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  937. return -EFAULT;
  938. mutex_lock(&mut);
  939. mc = idr_find(&multicast_idr, cmd.id);
  940. if (!mc)
  941. mc = ERR_PTR(-ENOENT);
  942. else if (mc->ctx->file != file)
  943. mc = ERR_PTR(-EINVAL);
  944. else {
  945. idr_remove(&multicast_idr, mc->id);
  946. atomic_inc(&mc->ctx->ref);
  947. }
  948. mutex_unlock(&mut);
  949. if (IS_ERR(mc)) {
  950. ret = PTR_ERR(mc);
  951. goto out;
  952. }
  953. rdma_leave_multicast(mc->ctx->cm_id, (struct sockaddr *) &mc->addr);
  954. mutex_lock(&mc->ctx->file->mut);
  955. ucma_cleanup_mc_events(mc);
  956. list_del(&mc->list);
  957. mutex_unlock(&mc->ctx->file->mut);
  958. ucma_put_ctx(mc->ctx);
  959. resp.events_reported = mc->events_reported;
  960. kfree(mc);
  961. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  962. &resp, sizeof(resp)))
  963. ret = -EFAULT;
  964. out:
  965. return ret;
  966. }
  967. static void ucma_lock_files(struct ucma_file *file1, struct ucma_file *file2)
  968. {
  969. /* Acquire mutex's based on pointer comparison to prevent deadlock. */
  970. if (file1 < file2) {
  971. mutex_lock(&file1->mut);
  972. mutex_lock(&file2->mut);
  973. } else {
  974. mutex_lock(&file2->mut);
  975. mutex_lock(&file1->mut);
  976. }
  977. }
  978. static void ucma_unlock_files(struct ucma_file *file1, struct ucma_file *file2)
  979. {
  980. if (file1 < file2) {
  981. mutex_unlock(&file2->mut);
  982. mutex_unlock(&file1->mut);
  983. } else {
  984. mutex_unlock(&file1->mut);
  985. mutex_unlock(&file2->mut);
  986. }
  987. }
  988. static void ucma_move_events(struct ucma_context *ctx, struct ucma_file *file)
  989. {
  990. struct ucma_event *uevent, *tmp;
  991. list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list)
  992. if (uevent->ctx == ctx)
  993. list_move_tail(&uevent->list, &file->event_list);
  994. }
  995. static ssize_t ucma_migrate_id(struct ucma_file *new_file,
  996. const char __user *inbuf,
  997. int in_len, int out_len)
  998. {
  999. struct rdma_ucm_migrate_id cmd;
  1000. struct rdma_ucm_migrate_resp resp;
  1001. struct ucma_context *ctx;
  1002. struct fd f;
  1003. struct ucma_file *cur_file;
  1004. int ret = 0;
  1005. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  1006. return -EFAULT;
  1007. /* Get current fd to protect against it being closed */
  1008. f = fdget(cmd.fd);
  1009. if (!f.file)
  1010. return -ENOENT;
  1011. /* Validate current fd and prevent destruction of id. */
  1012. ctx = ucma_get_ctx(f.file->private_data, cmd.id);
  1013. if (IS_ERR(ctx)) {
  1014. ret = PTR_ERR(ctx);
  1015. goto file_put;
  1016. }
  1017. cur_file = ctx->file;
  1018. if (cur_file == new_file) {
  1019. resp.events_reported = ctx->events_reported;
  1020. goto response;
  1021. }
  1022. /*
  1023. * Migrate events between fd's, maintaining order, and avoiding new
  1024. * events being added before existing events.
  1025. */
  1026. ucma_lock_files(cur_file, new_file);
  1027. mutex_lock(&mut);
  1028. list_move_tail(&ctx->list, &new_file->ctx_list);
  1029. ucma_move_events(ctx, new_file);
  1030. ctx->file = new_file;
  1031. resp.events_reported = ctx->events_reported;
  1032. mutex_unlock(&mut);
  1033. ucma_unlock_files(cur_file, new_file);
  1034. response:
  1035. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  1036. &resp, sizeof(resp)))
  1037. ret = -EFAULT;
  1038. ucma_put_ctx(ctx);
  1039. file_put:
  1040. fdput(f);
  1041. return ret;
  1042. }
  1043. static ssize_t (*ucma_cmd_table[])(struct ucma_file *file,
  1044. const char __user *inbuf,
  1045. int in_len, int out_len) = {
  1046. [RDMA_USER_CM_CMD_CREATE_ID] = ucma_create_id,
  1047. [RDMA_USER_CM_CMD_DESTROY_ID] = ucma_destroy_id,
  1048. [RDMA_USER_CM_CMD_BIND_ADDR] = ucma_bind_addr,
  1049. [RDMA_USER_CM_CMD_RESOLVE_ADDR] = ucma_resolve_addr,
  1050. [RDMA_USER_CM_CMD_RESOLVE_ROUTE]= ucma_resolve_route,
  1051. [RDMA_USER_CM_CMD_QUERY_ROUTE] = ucma_query_route,
  1052. [RDMA_USER_CM_CMD_CONNECT] = ucma_connect,
  1053. [RDMA_USER_CM_CMD_LISTEN] = ucma_listen,
  1054. [RDMA_USER_CM_CMD_ACCEPT] = ucma_accept,
  1055. [RDMA_USER_CM_CMD_REJECT] = ucma_reject,
  1056. [RDMA_USER_CM_CMD_DISCONNECT] = ucma_disconnect,
  1057. [RDMA_USER_CM_CMD_INIT_QP_ATTR] = ucma_init_qp_attr,
  1058. [RDMA_USER_CM_CMD_GET_EVENT] = ucma_get_event,
  1059. [RDMA_USER_CM_CMD_GET_OPTION] = NULL,
  1060. [RDMA_USER_CM_CMD_SET_OPTION] = ucma_set_option,
  1061. [RDMA_USER_CM_CMD_NOTIFY] = ucma_notify,
  1062. [RDMA_USER_CM_CMD_JOIN_MCAST] = ucma_join_multicast,
  1063. [RDMA_USER_CM_CMD_LEAVE_MCAST] = ucma_leave_multicast,
  1064. [RDMA_USER_CM_CMD_MIGRATE_ID] = ucma_migrate_id
  1065. };
  1066. static ssize_t ucma_write(struct file *filp, const char __user *buf,
  1067. size_t len, loff_t *pos)
  1068. {
  1069. struct ucma_file *file = filp->private_data;
  1070. struct rdma_ucm_cmd_hdr hdr;
  1071. ssize_t ret;
  1072. if (len < sizeof(hdr))
  1073. return -EINVAL;
  1074. if (copy_from_user(&hdr, buf, sizeof(hdr)))
  1075. return -EFAULT;
  1076. if (hdr.cmd >= ARRAY_SIZE(ucma_cmd_table))
  1077. return -EINVAL;
  1078. if (hdr.in + sizeof(hdr) > len)
  1079. return -EINVAL;
  1080. if (!ucma_cmd_table[hdr.cmd])
  1081. return -ENOSYS;
  1082. ret = ucma_cmd_table[hdr.cmd](file, buf + sizeof(hdr), hdr.in, hdr.out);
  1083. if (!ret)
  1084. ret = len;
  1085. return ret;
  1086. }
  1087. static unsigned int ucma_poll(struct file *filp, struct poll_table_struct *wait)
  1088. {
  1089. struct ucma_file *file = filp->private_data;
  1090. unsigned int mask = 0;
  1091. poll_wait(filp, &file->poll_wait, wait);
  1092. if (!list_empty(&file->event_list))
  1093. mask = POLLIN | POLLRDNORM;
  1094. return mask;
  1095. }
  1096. /*
  1097. * ucma_open() does not need the BKL:
  1098. *
  1099. * - no global state is referred to;
  1100. * - there is no ioctl method to race against;
  1101. * - no further module initialization is required for open to work
  1102. * after the device is registered.
  1103. */
  1104. static int ucma_open(struct inode *inode, struct file *filp)
  1105. {
  1106. struct ucma_file *file;
  1107. file = kmalloc(sizeof *file, GFP_KERNEL);
  1108. if (!file)
  1109. return -ENOMEM;
  1110. INIT_LIST_HEAD(&file->event_list);
  1111. INIT_LIST_HEAD(&file->ctx_list);
  1112. init_waitqueue_head(&file->poll_wait);
  1113. mutex_init(&file->mut);
  1114. filp->private_data = file;
  1115. file->filp = filp;
  1116. return nonseekable_open(inode, filp);
  1117. }
  1118. static int ucma_close(struct inode *inode, struct file *filp)
  1119. {
  1120. struct ucma_file *file = filp->private_data;
  1121. struct ucma_context *ctx, *tmp;
  1122. mutex_lock(&file->mut);
  1123. list_for_each_entry_safe(ctx, tmp, &file->ctx_list, list) {
  1124. mutex_unlock(&file->mut);
  1125. mutex_lock(&mut);
  1126. idr_remove(&ctx_idr, ctx->id);
  1127. mutex_unlock(&mut);
  1128. ucma_free_ctx(ctx);
  1129. mutex_lock(&file->mut);
  1130. }
  1131. mutex_unlock(&file->mut);
  1132. kfree(file);
  1133. return 0;
  1134. }
  1135. static const struct file_operations ucma_fops = {
  1136. .owner = THIS_MODULE,
  1137. .open = ucma_open,
  1138. .release = ucma_close,
  1139. .write = ucma_write,
  1140. .poll = ucma_poll,
  1141. .llseek = no_llseek,
  1142. };
  1143. static struct miscdevice ucma_misc = {
  1144. .minor = MISC_DYNAMIC_MINOR,
  1145. .name = "rdma_cm",
  1146. .nodename = "infiniband/rdma_cm",
  1147. .mode = 0666,
  1148. .fops = &ucma_fops,
  1149. };
  1150. static ssize_t show_abi_version(struct device *dev,
  1151. struct device_attribute *attr,
  1152. char *buf)
  1153. {
  1154. return sprintf(buf, "%d\n", RDMA_USER_CM_ABI_VERSION);
  1155. }
  1156. static DEVICE_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  1157. static int __init ucma_init(void)
  1158. {
  1159. int ret;
  1160. ret = misc_register(&ucma_misc);
  1161. if (ret)
  1162. return ret;
  1163. ret = device_create_file(ucma_misc.this_device, &dev_attr_abi_version);
  1164. if (ret) {
  1165. printk(KERN_ERR "rdma_ucm: couldn't create abi_version attr\n");
  1166. goto err1;
  1167. }
  1168. ucma_ctl_table_hdr = register_net_sysctl(&init_net, "net/rdma_ucm", ucma_ctl_table);
  1169. if (!ucma_ctl_table_hdr) {
  1170. printk(KERN_ERR "rdma_ucm: couldn't register sysctl paths\n");
  1171. ret = -ENOMEM;
  1172. goto err2;
  1173. }
  1174. return 0;
  1175. err2:
  1176. device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
  1177. err1:
  1178. misc_deregister(&ucma_misc);
  1179. return ret;
  1180. }
  1181. static void __exit ucma_cleanup(void)
  1182. {
  1183. unregister_net_sysctl_table(ucma_ctl_table_hdr);
  1184. device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
  1185. misc_deregister(&ucma_misc);
  1186. idr_destroy(&ctx_idr);
  1187. }
  1188. module_init(ucma_init);
  1189. module_exit(ucma_cleanup);