ucma.c 42 KB

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