ucma.c 32 KB

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