ucma.c 32 KB

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