ucm.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  1. /*
  2. * Copyright (c) 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Intel Corporation. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. *
  33. * $Id: ucm.c 4311 2005-12-05 18:42:01Z sean.hefty $
  34. */
  35. #include <linux/completion.h>
  36. #include <linux/init.h>
  37. #include <linux/fs.h>
  38. #include <linux/module.h>
  39. #include <linux/device.h>
  40. #include <linux/err.h>
  41. #include <linux/poll.h>
  42. #include <linux/file.h>
  43. #include <linux/mount.h>
  44. #include <linux/cdev.h>
  45. #include <linux/idr.h>
  46. #include <linux/mutex.h>
  47. #include <asm/uaccess.h>
  48. #include <rdma/ib_cm.h>
  49. #include <rdma/ib_user_cm.h>
  50. #include <rdma/ib_marshall.h>
  51. MODULE_AUTHOR("Libor Michalek");
  52. MODULE_DESCRIPTION("InfiniBand userspace Connection Manager access");
  53. MODULE_LICENSE("Dual BSD/GPL");
  54. struct ib_ucm_device {
  55. int devnum;
  56. struct cdev dev;
  57. struct class_device class_dev;
  58. struct ib_device *ib_dev;
  59. };
  60. struct ib_ucm_file {
  61. struct mutex file_mutex;
  62. struct file *filp;
  63. struct ib_ucm_device *device;
  64. struct list_head ctxs;
  65. struct list_head events;
  66. wait_queue_head_t poll_wait;
  67. };
  68. struct ib_ucm_context {
  69. int id;
  70. struct completion comp;
  71. atomic_t ref;
  72. int events_reported;
  73. struct ib_ucm_file *file;
  74. struct ib_cm_id *cm_id;
  75. __u64 uid;
  76. struct list_head events; /* list of pending events. */
  77. struct list_head file_list; /* member in file ctx list */
  78. };
  79. struct ib_ucm_event {
  80. struct ib_ucm_context *ctx;
  81. struct list_head file_list; /* member in file event list */
  82. struct list_head ctx_list; /* member in ctx event list */
  83. struct ib_cm_id *cm_id;
  84. struct ib_ucm_event_resp resp;
  85. void *data;
  86. void *info;
  87. int data_len;
  88. int info_len;
  89. };
  90. enum {
  91. IB_UCM_MAJOR = 231,
  92. IB_UCM_BASE_MINOR = 224,
  93. IB_UCM_MAX_DEVICES = 32
  94. };
  95. #define IB_UCM_BASE_DEV MKDEV(IB_UCM_MAJOR, IB_UCM_BASE_MINOR)
  96. static void ib_ucm_add_one(struct ib_device *device);
  97. static void ib_ucm_remove_one(struct ib_device *device);
  98. static struct ib_client ucm_client = {
  99. .name = "ucm",
  100. .add = ib_ucm_add_one,
  101. .remove = ib_ucm_remove_one
  102. };
  103. static DEFINE_MUTEX(ctx_id_mutex);
  104. static DEFINE_IDR(ctx_id_table);
  105. static DECLARE_BITMAP(dev_map, IB_UCM_MAX_DEVICES);
  106. static struct ib_ucm_context *ib_ucm_ctx_get(struct ib_ucm_file *file, int id)
  107. {
  108. struct ib_ucm_context *ctx;
  109. mutex_lock(&ctx_id_mutex);
  110. ctx = idr_find(&ctx_id_table, id);
  111. if (!ctx)
  112. ctx = ERR_PTR(-ENOENT);
  113. else if (ctx->file != file)
  114. ctx = ERR_PTR(-EINVAL);
  115. else
  116. atomic_inc(&ctx->ref);
  117. mutex_unlock(&ctx_id_mutex);
  118. return ctx;
  119. }
  120. static void ib_ucm_ctx_put(struct ib_ucm_context *ctx)
  121. {
  122. if (atomic_dec_and_test(&ctx->ref))
  123. complete(&ctx->comp);
  124. }
  125. static inline int ib_ucm_new_cm_id(int event)
  126. {
  127. return event == IB_CM_REQ_RECEIVED || event == IB_CM_SIDR_REQ_RECEIVED;
  128. }
  129. static void ib_ucm_cleanup_events(struct ib_ucm_context *ctx)
  130. {
  131. struct ib_ucm_event *uevent;
  132. mutex_lock(&ctx->file->file_mutex);
  133. list_del(&ctx->file_list);
  134. while (!list_empty(&ctx->events)) {
  135. uevent = list_entry(ctx->events.next,
  136. struct ib_ucm_event, ctx_list);
  137. list_del(&uevent->file_list);
  138. list_del(&uevent->ctx_list);
  139. /* clear incoming connections. */
  140. if (ib_ucm_new_cm_id(uevent->resp.event))
  141. ib_destroy_cm_id(uevent->cm_id);
  142. kfree(uevent);
  143. }
  144. mutex_unlock(&ctx->file->file_mutex);
  145. }
  146. static struct ib_ucm_context *ib_ucm_ctx_alloc(struct ib_ucm_file *file)
  147. {
  148. struct ib_ucm_context *ctx;
  149. int result;
  150. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  151. if (!ctx)
  152. return NULL;
  153. atomic_set(&ctx->ref, 1);
  154. init_completion(&ctx->comp);
  155. ctx->file = file;
  156. INIT_LIST_HEAD(&ctx->events);
  157. do {
  158. result = idr_pre_get(&ctx_id_table, GFP_KERNEL);
  159. if (!result)
  160. goto error;
  161. mutex_lock(&ctx_id_mutex);
  162. result = idr_get_new(&ctx_id_table, ctx, &ctx->id);
  163. mutex_unlock(&ctx_id_mutex);
  164. } while (result == -EAGAIN);
  165. if (result)
  166. goto error;
  167. list_add_tail(&ctx->file_list, &file->ctxs);
  168. return ctx;
  169. error:
  170. kfree(ctx);
  171. return NULL;
  172. }
  173. static void ib_ucm_event_req_get(struct ib_ucm_req_event_resp *ureq,
  174. struct ib_cm_req_event_param *kreq)
  175. {
  176. ureq->remote_ca_guid = kreq->remote_ca_guid;
  177. ureq->remote_qkey = kreq->remote_qkey;
  178. ureq->remote_qpn = kreq->remote_qpn;
  179. ureq->qp_type = kreq->qp_type;
  180. ureq->starting_psn = kreq->starting_psn;
  181. ureq->responder_resources = kreq->responder_resources;
  182. ureq->initiator_depth = kreq->initiator_depth;
  183. ureq->local_cm_response_timeout = kreq->local_cm_response_timeout;
  184. ureq->flow_control = kreq->flow_control;
  185. ureq->remote_cm_response_timeout = kreq->remote_cm_response_timeout;
  186. ureq->retry_count = kreq->retry_count;
  187. ureq->rnr_retry_count = kreq->rnr_retry_count;
  188. ureq->srq = kreq->srq;
  189. ureq->port = kreq->port;
  190. ib_copy_path_rec_to_user(&ureq->primary_path, kreq->primary_path);
  191. if (kreq->alternate_path)
  192. ib_copy_path_rec_to_user(&ureq->alternate_path,
  193. kreq->alternate_path);
  194. }
  195. static void ib_ucm_event_rep_get(struct ib_ucm_rep_event_resp *urep,
  196. struct ib_cm_rep_event_param *krep)
  197. {
  198. urep->remote_ca_guid = krep->remote_ca_guid;
  199. urep->remote_qkey = krep->remote_qkey;
  200. urep->remote_qpn = krep->remote_qpn;
  201. urep->starting_psn = krep->starting_psn;
  202. urep->responder_resources = krep->responder_resources;
  203. urep->initiator_depth = krep->initiator_depth;
  204. urep->target_ack_delay = krep->target_ack_delay;
  205. urep->failover_accepted = krep->failover_accepted;
  206. urep->flow_control = krep->flow_control;
  207. urep->rnr_retry_count = krep->rnr_retry_count;
  208. urep->srq = krep->srq;
  209. }
  210. static void ib_ucm_event_sidr_rep_get(struct ib_ucm_sidr_rep_event_resp *urep,
  211. struct ib_cm_sidr_rep_event_param *krep)
  212. {
  213. urep->status = krep->status;
  214. urep->qkey = krep->qkey;
  215. urep->qpn = krep->qpn;
  216. };
  217. static int ib_ucm_event_process(struct ib_cm_event *evt,
  218. struct ib_ucm_event *uvt)
  219. {
  220. void *info = NULL;
  221. switch (evt->event) {
  222. case IB_CM_REQ_RECEIVED:
  223. ib_ucm_event_req_get(&uvt->resp.u.req_resp,
  224. &evt->param.req_rcvd);
  225. uvt->data_len = IB_CM_REQ_PRIVATE_DATA_SIZE;
  226. uvt->resp.present = IB_UCM_PRES_PRIMARY;
  227. uvt->resp.present |= (evt->param.req_rcvd.alternate_path ?
  228. IB_UCM_PRES_ALTERNATE : 0);
  229. break;
  230. case IB_CM_REP_RECEIVED:
  231. ib_ucm_event_rep_get(&uvt->resp.u.rep_resp,
  232. &evt->param.rep_rcvd);
  233. uvt->data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
  234. break;
  235. case IB_CM_RTU_RECEIVED:
  236. uvt->data_len = IB_CM_RTU_PRIVATE_DATA_SIZE;
  237. uvt->resp.u.send_status = evt->param.send_status;
  238. break;
  239. case IB_CM_DREQ_RECEIVED:
  240. uvt->data_len = IB_CM_DREQ_PRIVATE_DATA_SIZE;
  241. uvt->resp.u.send_status = evt->param.send_status;
  242. break;
  243. case IB_CM_DREP_RECEIVED:
  244. uvt->data_len = IB_CM_DREP_PRIVATE_DATA_SIZE;
  245. uvt->resp.u.send_status = evt->param.send_status;
  246. break;
  247. case IB_CM_MRA_RECEIVED:
  248. uvt->resp.u.mra_resp.timeout =
  249. evt->param.mra_rcvd.service_timeout;
  250. uvt->data_len = IB_CM_MRA_PRIVATE_DATA_SIZE;
  251. break;
  252. case IB_CM_REJ_RECEIVED:
  253. uvt->resp.u.rej_resp.reason = evt->param.rej_rcvd.reason;
  254. uvt->data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
  255. uvt->info_len = evt->param.rej_rcvd.ari_length;
  256. info = evt->param.rej_rcvd.ari;
  257. break;
  258. case IB_CM_LAP_RECEIVED:
  259. ib_copy_path_rec_to_user(&uvt->resp.u.lap_resp.path,
  260. evt->param.lap_rcvd.alternate_path);
  261. uvt->data_len = IB_CM_LAP_PRIVATE_DATA_SIZE;
  262. uvt->resp.present = IB_UCM_PRES_ALTERNATE;
  263. break;
  264. case IB_CM_APR_RECEIVED:
  265. uvt->resp.u.apr_resp.status = evt->param.apr_rcvd.ap_status;
  266. uvt->data_len = IB_CM_APR_PRIVATE_DATA_SIZE;
  267. uvt->info_len = evt->param.apr_rcvd.info_len;
  268. info = evt->param.apr_rcvd.apr_info;
  269. break;
  270. case IB_CM_SIDR_REQ_RECEIVED:
  271. uvt->resp.u.sidr_req_resp.pkey =
  272. evt->param.sidr_req_rcvd.pkey;
  273. uvt->resp.u.sidr_req_resp.port =
  274. evt->param.sidr_req_rcvd.port;
  275. uvt->data_len = IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE;
  276. break;
  277. case IB_CM_SIDR_REP_RECEIVED:
  278. ib_ucm_event_sidr_rep_get(&uvt->resp.u.sidr_rep_resp,
  279. &evt->param.sidr_rep_rcvd);
  280. uvt->data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
  281. uvt->info_len = evt->param.sidr_rep_rcvd.info_len;
  282. info = evt->param.sidr_rep_rcvd.info;
  283. break;
  284. default:
  285. uvt->resp.u.send_status = evt->param.send_status;
  286. break;
  287. }
  288. if (uvt->data_len) {
  289. uvt->data = kmalloc(uvt->data_len, GFP_KERNEL);
  290. if (!uvt->data)
  291. goto err1;
  292. memcpy(uvt->data, evt->private_data, uvt->data_len);
  293. uvt->resp.present |= IB_UCM_PRES_DATA;
  294. }
  295. if (uvt->info_len) {
  296. uvt->info = kmalloc(uvt->info_len, GFP_KERNEL);
  297. if (!uvt->info)
  298. goto err2;
  299. memcpy(uvt->info, info, uvt->info_len);
  300. uvt->resp.present |= IB_UCM_PRES_INFO;
  301. }
  302. return 0;
  303. err2:
  304. kfree(uvt->data);
  305. err1:
  306. return -ENOMEM;
  307. }
  308. static int ib_ucm_event_handler(struct ib_cm_id *cm_id,
  309. struct ib_cm_event *event)
  310. {
  311. struct ib_ucm_event *uevent;
  312. struct ib_ucm_context *ctx;
  313. int result = 0;
  314. ctx = cm_id->context;
  315. uevent = kzalloc(sizeof *uevent, GFP_KERNEL);
  316. if (!uevent)
  317. goto err1;
  318. uevent->ctx = ctx;
  319. uevent->cm_id = cm_id;
  320. uevent->resp.uid = ctx->uid;
  321. uevent->resp.id = ctx->id;
  322. uevent->resp.event = event->event;
  323. result = ib_ucm_event_process(event, uevent);
  324. if (result)
  325. goto err2;
  326. mutex_lock(&ctx->file->file_mutex);
  327. list_add_tail(&uevent->file_list, &ctx->file->events);
  328. list_add_tail(&uevent->ctx_list, &ctx->events);
  329. wake_up_interruptible(&ctx->file->poll_wait);
  330. mutex_unlock(&ctx->file->file_mutex);
  331. return 0;
  332. err2:
  333. kfree(uevent);
  334. err1:
  335. /* Destroy new cm_id's */
  336. return ib_ucm_new_cm_id(event->event);
  337. }
  338. static ssize_t ib_ucm_event(struct ib_ucm_file *file,
  339. const char __user *inbuf,
  340. int in_len, int out_len)
  341. {
  342. struct ib_ucm_context *ctx;
  343. struct ib_ucm_event_get cmd;
  344. struct ib_ucm_event *uevent;
  345. int result = 0;
  346. DEFINE_WAIT(wait);
  347. if (out_len < sizeof(struct ib_ucm_event_resp))
  348. return -ENOSPC;
  349. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  350. return -EFAULT;
  351. mutex_lock(&file->file_mutex);
  352. while (list_empty(&file->events)) {
  353. if (file->filp->f_flags & O_NONBLOCK) {
  354. result = -EAGAIN;
  355. break;
  356. }
  357. if (signal_pending(current)) {
  358. result = -ERESTARTSYS;
  359. break;
  360. }
  361. prepare_to_wait(&file->poll_wait, &wait, TASK_INTERRUPTIBLE);
  362. mutex_unlock(&file->file_mutex);
  363. schedule();
  364. mutex_lock(&file->file_mutex);
  365. finish_wait(&file->poll_wait, &wait);
  366. }
  367. if (result)
  368. goto done;
  369. uevent = list_entry(file->events.next, struct ib_ucm_event, file_list);
  370. if (ib_ucm_new_cm_id(uevent->resp.event)) {
  371. ctx = ib_ucm_ctx_alloc(file);
  372. if (!ctx) {
  373. result = -ENOMEM;
  374. goto done;
  375. }
  376. ctx->cm_id = uevent->cm_id;
  377. ctx->cm_id->context = ctx;
  378. uevent->resp.id = ctx->id;
  379. }
  380. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  381. &uevent->resp, sizeof(uevent->resp))) {
  382. result = -EFAULT;
  383. goto done;
  384. }
  385. if (uevent->data) {
  386. if (cmd.data_len < uevent->data_len) {
  387. result = -ENOMEM;
  388. goto done;
  389. }
  390. if (copy_to_user((void __user *)(unsigned long)cmd.data,
  391. uevent->data, uevent->data_len)) {
  392. result = -EFAULT;
  393. goto done;
  394. }
  395. }
  396. if (uevent->info) {
  397. if (cmd.info_len < uevent->info_len) {
  398. result = -ENOMEM;
  399. goto done;
  400. }
  401. if (copy_to_user((void __user *)(unsigned long)cmd.info,
  402. uevent->info, uevent->info_len)) {
  403. result = -EFAULT;
  404. goto done;
  405. }
  406. }
  407. list_del(&uevent->file_list);
  408. list_del(&uevent->ctx_list);
  409. uevent->ctx->events_reported++;
  410. kfree(uevent->data);
  411. kfree(uevent->info);
  412. kfree(uevent);
  413. done:
  414. mutex_unlock(&file->file_mutex);
  415. return result;
  416. }
  417. static ssize_t ib_ucm_create_id(struct ib_ucm_file *file,
  418. const char __user *inbuf,
  419. int in_len, int out_len)
  420. {
  421. struct ib_ucm_create_id cmd;
  422. struct ib_ucm_create_id_resp resp;
  423. struct ib_ucm_context *ctx;
  424. int result;
  425. if (out_len < sizeof(resp))
  426. return -ENOSPC;
  427. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  428. return -EFAULT;
  429. mutex_lock(&file->file_mutex);
  430. ctx = ib_ucm_ctx_alloc(file);
  431. mutex_unlock(&file->file_mutex);
  432. if (!ctx)
  433. return -ENOMEM;
  434. ctx->uid = cmd.uid;
  435. ctx->cm_id = ib_create_cm_id(file->device->ib_dev,
  436. ib_ucm_event_handler, ctx);
  437. if (IS_ERR(ctx->cm_id)) {
  438. result = PTR_ERR(ctx->cm_id);
  439. goto err1;
  440. }
  441. resp.id = ctx->id;
  442. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  443. &resp, sizeof(resp))) {
  444. result = -EFAULT;
  445. goto err2;
  446. }
  447. return 0;
  448. err2:
  449. ib_destroy_cm_id(ctx->cm_id);
  450. err1:
  451. mutex_lock(&ctx_id_mutex);
  452. idr_remove(&ctx_id_table, ctx->id);
  453. mutex_unlock(&ctx_id_mutex);
  454. kfree(ctx);
  455. return result;
  456. }
  457. static ssize_t ib_ucm_destroy_id(struct ib_ucm_file *file,
  458. const char __user *inbuf,
  459. int in_len, int out_len)
  460. {
  461. struct ib_ucm_destroy_id cmd;
  462. struct ib_ucm_destroy_id_resp resp;
  463. struct ib_ucm_context *ctx;
  464. int result = 0;
  465. if (out_len < sizeof(resp))
  466. return -ENOSPC;
  467. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  468. return -EFAULT;
  469. mutex_lock(&ctx_id_mutex);
  470. ctx = idr_find(&ctx_id_table, cmd.id);
  471. if (!ctx)
  472. ctx = ERR_PTR(-ENOENT);
  473. else if (ctx->file != file)
  474. ctx = ERR_PTR(-EINVAL);
  475. else
  476. idr_remove(&ctx_id_table, ctx->id);
  477. mutex_unlock(&ctx_id_mutex);
  478. if (IS_ERR(ctx))
  479. return PTR_ERR(ctx);
  480. ib_ucm_ctx_put(ctx);
  481. wait_for_completion(&ctx->comp);
  482. /* No new events will be generated after destroying the cm_id. */
  483. ib_destroy_cm_id(ctx->cm_id);
  484. /* Cleanup events not yet reported to the user. */
  485. ib_ucm_cleanup_events(ctx);
  486. resp.events_reported = ctx->events_reported;
  487. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  488. &resp, sizeof(resp)))
  489. result = -EFAULT;
  490. kfree(ctx);
  491. return result;
  492. }
  493. static ssize_t ib_ucm_attr_id(struct ib_ucm_file *file,
  494. const char __user *inbuf,
  495. int in_len, int out_len)
  496. {
  497. struct ib_ucm_attr_id_resp resp;
  498. struct ib_ucm_attr_id cmd;
  499. struct ib_ucm_context *ctx;
  500. int result = 0;
  501. if (out_len < sizeof(resp))
  502. return -ENOSPC;
  503. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  504. return -EFAULT;
  505. ctx = ib_ucm_ctx_get(file, cmd.id);
  506. if (IS_ERR(ctx))
  507. return PTR_ERR(ctx);
  508. resp.service_id = ctx->cm_id->service_id;
  509. resp.service_mask = ctx->cm_id->service_mask;
  510. resp.local_id = ctx->cm_id->local_id;
  511. resp.remote_id = ctx->cm_id->remote_id;
  512. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  513. &resp, sizeof(resp)))
  514. result = -EFAULT;
  515. ib_ucm_ctx_put(ctx);
  516. return result;
  517. }
  518. static ssize_t ib_ucm_init_qp_attr(struct ib_ucm_file *file,
  519. const char __user *inbuf,
  520. int in_len, int out_len)
  521. {
  522. struct ib_uverbs_qp_attr resp;
  523. struct ib_ucm_init_qp_attr cmd;
  524. struct ib_ucm_context *ctx;
  525. struct ib_qp_attr qp_attr;
  526. int result = 0;
  527. if (out_len < sizeof(resp))
  528. return -ENOSPC;
  529. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  530. return -EFAULT;
  531. ctx = ib_ucm_ctx_get(file, cmd.id);
  532. if (IS_ERR(ctx))
  533. return PTR_ERR(ctx);
  534. resp.qp_attr_mask = 0;
  535. memset(&qp_attr, 0, sizeof qp_attr);
  536. qp_attr.qp_state = cmd.qp_state;
  537. result = ib_cm_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
  538. if (result)
  539. goto out;
  540. ib_copy_qp_attr_to_user(&resp, &qp_attr);
  541. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  542. &resp, sizeof(resp)))
  543. result = -EFAULT;
  544. out:
  545. ib_ucm_ctx_put(ctx);
  546. return result;
  547. }
  548. static int ucm_validate_listen(__be64 service_id, __be64 service_mask)
  549. {
  550. service_id &= service_mask;
  551. if (((service_id & IB_CMA_SERVICE_ID_MASK) == IB_CMA_SERVICE_ID) ||
  552. ((service_id & IB_SDP_SERVICE_ID_MASK) == IB_SDP_SERVICE_ID))
  553. return -EINVAL;
  554. return 0;
  555. }
  556. static ssize_t ib_ucm_listen(struct ib_ucm_file *file,
  557. const char __user *inbuf,
  558. int in_len, int out_len)
  559. {
  560. struct ib_ucm_listen cmd;
  561. struct ib_ucm_context *ctx;
  562. int result;
  563. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  564. return -EFAULT;
  565. ctx = ib_ucm_ctx_get(file, cmd.id);
  566. if (IS_ERR(ctx))
  567. return PTR_ERR(ctx);
  568. result = ucm_validate_listen(cmd.service_id, cmd.service_mask);
  569. if (result)
  570. goto out;
  571. result = ib_cm_listen(ctx->cm_id, cmd.service_id, cmd.service_mask,
  572. NULL);
  573. out:
  574. ib_ucm_ctx_put(ctx);
  575. return result;
  576. }
  577. static ssize_t ib_ucm_establish(struct ib_ucm_file *file,
  578. const char __user *inbuf,
  579. int in_len, int out_len)
  580. {
  581. struct ib_ucm_establish cmd;
  582. struct ib_ucm_context *ctx;
  583. int result;
  584. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  585. return -EFAULT;
  586. ctx = ib_ucm_ctx_get(file, cmd.id);
  587. if (IS_ERR(ctx))
  588. return PTR_ERR(ctx);
  589. result = ib_cm_establish(ctx->cm_id);
  590. ib_ucm_ctx_put(ctx);
  591. return result;
  592. }
  593. static int ib_ucm_alloc_data(const void **dest, u64 src, u32 len)
  594. {
  595. void *data;
  596. *dest = NULL;
  597. if (!len)
  598. return 0;
  599. data = kmalloc(len, GFP_KERNEL);
  600. if (!data)
  601. return -ENOMEM;
  602. if (copy_from_user(data, (void __user *)(unsigned long)src, len)) {
  603. kfree(data);
  604. return -EFAULT;
  605. }
  606. *dest = data;
  607. return 0;
  608. }
  609. static int ib_ucm_path_get(struct ib_sa_path_rec **path, u64 src)
  610. {
  611. struct ib_user_path_rec upath;
  612. struct ib_sa_path_rec *sa_path;
  613. *path = NULL;
  614. if (!src)
  615. return 0;
  616. sa_path = kmalloc(sizeof(*sa_path), GFP_KERNEL);
  617. if (!sa_path)
  618. return -ENOMEM;
  619. if (copy_from_user(&upath, (void __user *)(unsigned long)src,
  620. sizeof(upath))) {
  621. kfree(sa_path);
  622. return -EFAULT;
  623. }
  624. ib_copy_path_rec_from_user(sa_path, &upath);
  625. *path = sa_path;
  626. return 0;
  627. }
  628. static ssize_t ib_ucm_send_req(struct ib_ucm_file *file,
  629. const char __user *inbuf,
  630. int in_len, int out_len)
  631. {
  632. struct ib_cm_req_param param;
  633. struct ib_ucm_context *ctx;
  634. struct ib_ucm_req cmd;
  635. int result;
  636. param.private_data = NULL;
  637. param.primary_path = NULL;
  638. param.alternate_path = NULL;
  639. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  640. return -EFAULT;
  641. result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
  642. if (result)
  643. goto done;
  644. result = ib_ucm_path_get(&param.primary_path, cmd.primary_path);
  645. if (result)
  646. goto done;
  647. result = ib_ucm_path_get(&param.alternate_path, cmd.alternate_path);
  648. if (result)
  649. goto done;
  650. param.private_data_len = cmd.len;
  651. param.service_id = cmd.sid;
  652. param.qp_num = cmd.qpn;
  653. param.qp_type = cmd.qp_type;
  654. param.starting_psn = cmd.psn;
  655. param.peer_to_peer = cmd.peer_to_peer;
  656. param.responder_resources = cmd.responder_resources;
  657. param.initiator_depth = cmd.initiator_depth;
  658. param.remote_cm_response_timeout = cmd.remote_cm_response_timeout;
  659. param.flow_control = cmd.flow_control;
  660. param.local_cm_response_timeout = cmd.local_cm_response_timeout;
  661. param.retry_count = cmd.retry_count;
  662. param.rnr_retry_count = cmd.rnr_retry_count;
  663. param.max_cm_retries = cmd.max_cm_retries;
  664. param.srq = cmd.srq;
  665. ctx = ib_ucm_ctx_get(file, cmd.id);
  666. if (!IS_ERR(ctx)) {
  667. result = ib_send_cm_req(ctx->cm_id, &param);
  668. ib_ucm_ctx_put(ctx);
  669. } else
  670. result = PTR_ERR(ctx);
  671. done:
  672. kfree(param.private_data);
  673. kfree(param.primary_path);
  674. kfree(param.alternate_path);
  675. return result;
  676. }
  677. static ssize_t ib_ucm_send_rep(struct ib_ucm_file *file,
  678. const char __user *inbuf,
  679. int in_len, int out_len)
  680. {
  681. struct ib_cm_rep_param param;
  682. struct ib_ucm_context *ctx;
  683. struct ib_ucm_rep cmd;
  684. int result;
  685. param.private_data = NULL;
  686. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  687. return -EFAULT;
  688. result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
  689. if (result)
  690. return result;
  691. param.qp_num = cmd.qpn;
  692. param.starting_psn = cmd.psn;
  693. param.private_data_len = cmd.len;
  694. param.responder_resources = cmd.responder_resources;
  695. param.initiator_depth = cmd.initiator_depth;
  696. param.target_ack_delay = cmd.target_ack_delay;
  697. param.failover_accepted = cmd.failover_accepted;
  698. param.flow_control = cmd.flow_control;
  699. param.rnr_retry_count = cmd.rnr_retry_count;
  700. param.srq = cmd.srq;
  701. ctx = ib_ucm_ctx_get(file, cmd.id);
  702. if (!IS_ERR(ctx)) {
  703. ctx->uid = cmd.uid;
  704. result = ib_send_cm_rep(ctx->cm_id, &param);
  705. ib_ucm_ctx_put(ctx);
  706. } else
  707. result = PTR_ERR(ctx);
  708. kfree(param.private_data);
  709. return result;
  710. }
  711. static ssize_t ib_ucm_send_private_data(struct ib_ucm_file *file,
  712. const char __user *inbuf, int in_len,
  713. int (*func)(struct ib_cm_id *cm_id,
  714. const void *private_data,
  715. u8 private_data_len))
  716. {
  717. struct ib_ucm_private_data cmd;
  718. struct ib_ucm_context *ctx;
  719. const void *private_data = NULL;
  720. int result;
  721. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  722. return -EFAULT;
  723. result = ib_ucm_alloc_data(&private_data, cmd.data, cmd.len);
  724. if (result)
  725. return result;
  726. ctx = ib_ucm_ctx_get(file, cmd.id);
  727. if (!IS_ERR(ctx)) {
  728. result = func(ctx->cm_id, private_data, cmd.len);
  729. ib_ucm_ctx_put(ctx);
  730. } else
  731. result = PTR_ERR(ctx);
  732. kfree(private_data);
  733. return result;
  734. }
  735. static ssize_t ib_ucm_send_rtu(struct ib_ucm_file *file,
  736. const char __user *inbuf,
  737. int in_len, int out_len)
  738. {
  739. return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_rtu);
  740. }
  741. static ssize_t ib_ucm_send_dreq(struct ib_ucm_file *file,
  742. const char __user *inbuf,
  743. int in_len, int out_len)
  744. {
  745. return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_dreq);
  746. }
  747. static ssize_t ib_ucm_send_drep(struct ib_ucm_file *file,
  748. const char __user *inbuf,
  749. int in_len, int out_len)
  750. {
  751. return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_drep);
  752. }
  753. static ssize_t ib_ucm_send_info(struct ib_ucm_file *file,
  754. const char __user *inbuf, int in_len,
  755. int (*func)(struct ib_cm_id *cm_id,
  756. int status,
  757. const void *info,
  758. u8 info_len,
  759. const void *data,
  760. u8 data_len))
  761. {
  762. struct ib_ucm_context *ctx;
  763. struct ib_ucm_info cmd;
  764. const void *data = NULL;
  765. const void *info = NULL;
  766. int result;
  767. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  768. return -EFAULT;
  769. result = ib_ucm_alloc_data(&data, cmd.data, cmd.data_len);
  770. if (result)
  771. goto done;
  772. result = ib_ucm_alloc_data(&info, cmd.info, cmd.info_len);
  773. if (result)
  774. goto done;
  775. ctx = ib_ucm_ctx_get(file, cmd.id);
  776. if (!IS_ERR(ctx)) {
  777. result = func(ctx->cm_id, cmd.status, info, cmd.info_len,
  778. data, cmd.data_len);
  779. ib_ucm_ctx_put(ctx);
  780. } else
  781. result = PTR_ERR(ctx);
  782. done:
  783. kfree(data);
  784. kfree(info);
  785. return result;
  786. }
  787. static ssize_t ib_ucm_send_rej(struct ib_ucm_file *file,
  788. const char __user *inbuf,
  789. int in_len, int out_len)
  790. {
  791. return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_rej);
  792. }
  793. static ssize_t ib_ucm_send_apr(struct ib_ucm_file *file,
  794. const char __user *inbuf,
  795. int in_len, int out_len)
  796. {
  797. return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_apr);
  798. }
  799. static ssize_t ib_ucm_send_mra(struct ib_ucm_file *file,
  800. const char __user *inbuf,
  801. int in_len, int out_len)
  802. {
  803. struct ib_ucm_context *ctx;
  804. struct ib_ucm_mra cmd;
  805. const void *data = NULL;
  806. int result;
  807. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  808. return -EFAULT;
  809. result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
  810. if (result)
  811. return result;
  812. ctx = ib_ucm_ctx_get(file, cmd.id);
  813. if (!IS_ERR(ctx)) {
  814. result = ib_send_cm_mra(ctx->cm_id, cmd.timeout, data, cmd.len);
  815. ib_ucm_ctx_put(ctx);
  816. } else
  817. result = PTR_ERR(ctx);
  818. kfree(data);
  819. return result;
  820. }
  821. static ssize_t ib_ucm_send_lap(struct ib_ucm_file *file,
  822. const char __user *inbuf,
  823. int in_len, int out_len)
  824. {
  825. struct ib_ucm_context *ctx;
  826. struct ib_sa_path_rec *path = NULL;
  827. struct ib_ucm_lap cmd;
  828. const void *data = NULL;
  829. int result;
  830. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  831. return -EFAULT;
  832. result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
  833. if (result)
  834. goto done;
  835. result = ib_ucm_path_get(&path, cmd.path);
  836. if (result)
  837. goto done;
  838. ctx = ib_ucm_ctx_get(file, cmd.id);
  839. if (!IS_ERR(ctx)) {
  840. result = ib_send_cm_lap(ctx->cm_id, path, data, cmd.len);
  841. ib_ucm_ctx_put(ctx);
  842. } else
  843. result = PTR_ERR(ctx);
  844. done:
  845. kfree(data);
  846. kfree(path);
  847. return result;
  848. }
  849. static ssize_t ib_ucm_send_sidr_req(struct ib_ucm_file *file,
  850. const char __user *inbuf,
  851. int in_len, int out_len)
  852. {
  853. struct ib_cm_sidr_req_param param;
  854. struct ib_ucm_context *ctx;
  855. struct ib_ucm_sidr_req cmd;
  856. int result;
  857. param.private_data = NULL;
  858. param.path = NULL;
  859. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  860. return -EFAULT;
  861. result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
  862. if (result)
  863. goto done;
  864. result = ib_ucm_path_get(&param.path, cmd.path);
  865. if (result)
  866. goto done;
  867. param.private_data_len = cmd.len;
  868. param.service_id = cmd.sid;
  869. param.timeout_ms = cmd.timeout;
  870. param.max_cm_retries = cmd.max_cm_retries;
  871. ctx = ib_ucm_ctx_get(file, cmd.id);
  872. if (!IS_ERR(ctx)) {
  873. result = ib_send_cm_sidr_req(ctx->cm_id, &param);
  874. ib_ucm_ctx_put(ctx);
  875. } else
  876. result = PTR_ERR(ctx);
  877. done:
  878. kfree(param.private_data);
  879. kfree(param.path);
  880. return result;
  881. }
  882. static ssize_t ib_ucm_send_sidr_rep(struct ib_ucm_file *file,
  883. const char __user *inbuf,
  884. int in_len, int out_len)
  885. {
  886. struct ib_cm_sidr_rep_param param;
  887. struct ib_ucm_sidr_rep cmd;
  888. struct ib_ucm_context *ctx;
  889. int result;
  890. param.info = NULL;
  891. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  892. return -EFAULT;
  893. result = ib_ucm_alloc_data(&param.private_data,
  894. cmd.data, cmd.data_len);
  895. if (result)
  896. goto done;
  897. result = ib_ucm_alloc_data(&param.info, cmd.info, cmd.info_len);
  898. if (result)
  899. goto done;
  900. param.qp_num = cmd.qpn;
  901. param.qkey = cmd.qkey;
  902. param.status = cmd.status;
  903. param.info_length = cmd.info_len;
  904. param.private_data_len = cmd.data_len;
  905. ctx = ib_ucm_ctx_get(file, cmd.id);
  906. if (!IS_ERR(ctx)) {
  907. result = ib_send_cm_sidr_rep(ctx->cm_id, &param);
  908. ib_ucm_ctx_put(ctx);
  909. } else
  910. result = PTR_ERR(ctx);
  911. done:
  912. kfree(param.private_data);
  913. kfree(param.info);
  914. return result;
  915. }
  916. static ssize_t (*ucm_cmd_table[])(struct ib_ucm_file *file,
  917. const char __user *inbuf,
  918. int in_len, int out_len) = {
  919. [IB_USER_CM_CMD_CREATE_ID] = ib_ucm_create_id,
  920. [IB_USER_CM_CMD_DESTROY_ID] = ib_ucm_destroy_id,
  921. [IB_USER_CM_CMD_ATTR_ID] = ib_ucm_attr_id,
  922. [IB_USER_CM_CMD_LISTEN] = ib_ucm_listen,
  923. [IB_USER_CM_CMD_ESTABLISH] = ib_ucm_establish,
  924. [IB_USER_CM_CMD_SEND_REQ] = ib_ucm_send_req,
  925. [IB_USER_CM_CMD_SEND_REP] = ib_ucm_send_rep,
  926. [IB_USER_CM_CMD_SEND_RTU] = ib_ucm_send_rtu,
  927. [IB_USER_CM_CMD_SEND_DREQ] = ib_ucm_send_dreq,
  928. [IB_USER_CM_CMD_SEND_DREP] = ib_ucm_send_drep,
  929. [IB_USER_CM_CMD_SEND_REJ] = ib_ucm_send_rej,
  930. [IB_USER_CM_CMD_SEND_MRA] = ib_ucm_send_mra,
  931. [IB_USER_CM_CMD_SEND_LAP] = ib_ucm_send_lap,
  932. [IB_USER_CM_CMD_SEND_APR] = ib_ucm_send_apr,
  933. [IB_USER_CM_CMD_SEND_SIDR_REQ] = ib_ucm_send_sidr_req,
  934. [IB_USER_CM_CMD_SEND_SIDR_REP] = ib_ucm_send_sidr_rep,
  935. [IB_USER_CM_CMD_EVENT] = ib_ucm_event,
  936. [IB_USER_CM_CMD_INIT_QP_ATTR] = ib_ucm_init_qp_attr,
  937. };
  938. static ssize_t ib_ucm_write(struct file *filp, const char __user *buf,
  939. size_t len, loff_t *pos)
  940. {
  941. struct ib_ucm_file *file = filp->private_data;
  942. struct ib_ucm_cmd_hdr hdr;
  943. ssize_t result;
  944. if (len < sizeof(hdr))
  945. return -EINVAL;
  946. if (copy_from_user(&hdr, buf, sizeof(hdr)))
  947. return -EFAULT;
  948. if (hdr.cmd < 0 || hdr.cmd >= ARRAY_SIZE(ucm_cmd_table))
  949. return -EINVAL;
  950. if (hdr.in + sizeof(hdr) > len)
  951. return -EINVAL;
  952. result = ucm_cmd_table[hdr.cmd](file, buf + sizeof(hdr),
  953. hdr.in, hdr.out);
  954. if (!result)
  955. result = len;
  956. return result;
  957. }
  958. static unsigned int ib_ucm_poll(struct file *filp,
  959. struct poll_table_struct *wait)
  960. {
  961. struct ib_ucm_file *file = filp->private_data;
  962. unsigned int mask = 0;
  963. poll_wait(filp, &file->poll_wait, wait);
  964. if (!list_empty(&file->events))
  965. mask = POLLIN | POLLRDNORM;
  966. return mask;
  967. }
  968. static int ib_ucm_open(struct inode *inode, struct file *filp)
  969. {
  970. struct ib_ucm_file *file;
  971. file = kmalloc(sizeof(*file), GFP_KERNEL);
  972. if (!file)
  973. return -ENOMEM;
  974. INIT_LIST_HEAD(&file->events);
  975. INIT_LIST_HEAD(&file->ctxs);
  976. init_waitqueue_head(&file->poll_wait);
  977. mutex_init(&file->file_mutex);
  978. filp->private_data = file;
  979. file->filp = filp;
  980. file->device = container_of(inode->i_cdev, struct ib_ucm_device, dev);
  981. return 0;
  982. }
  983. static int ib_ucm_close(struct inode *inode, struct file *filp)
  984. {
  985. struct ib_ucm_file *file = filp->private_data;
  986. struct ib_ucm_context *ctx;
  987. mutex_lock(&file->file_mutex);
  988. while (!list_empty(&file->ctxs)) {
  989. ctx = list_entry(file->ctxs.next,
  990. struct ib_ucm_context, file_list);
  991. mutex_unlock(&file->file_mutex);
  992. mutex_lock(&ctx_id_mutex);
  993. idr_remove(&ctx_id_table, ctx->id);
  994. mutex_unlock(&ctx_id_mutex);
  995. ib_destroy_cm_id(ctx->cm_id);
  996. ib_ucm_cleanup_events(ctx);
  997. kfree(ctx);
  998. mutex_lock(&file->file_mutex);
  999. }
  1000. mutex_unlock(&file->file_mutex);
  1001. kfree(file);
  1002. return 0;
  1003. }
  1004. static void ib_ucm_release_class_dev(struct class_device *class_dev)
  1005. {
  1006. struct ib_ucm_device *dev;
  1007. dev = container_of(class_dev, struct ib_ucm_device, class_dev);
  1008. cdev_del(&dev->dev);
  1009. clear_bit(dev->devnum, dev_map);
  1010. kfree(dev);
  1011. }
  1012. static struct file_operations ucm_fops = {
  1013. .owner = THIS_MODULE,
  1014. .open = ib_ucm_open,
  1015. .release = ib_ucm_close,
  1016. .write = ib_ucm_write,
  1017. .poll = ib_ucm_poll,
  1018. };
  1019. static struct class ucm_class = {
  1020. .name = "infiniband_cm",
  1021. .release = ib_ucm_release_class_dev
  1022. };
  1023. static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
  1024. {
  1025. struct ib_ucm_device *dev;
  1026. dev = container_of(class_dev, struct ib_ucm_device, class_dev);
  1027. return sprintf(buf, "%s\n", dev->ib_dev->name);
  1028. }
  1029. static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  1030. static void ib_ucm_add_one(struct ib_device *device)
  1031. {
  1032. struct ib_ucm_device *ucm_dev;
  1033. if (!device->alloc_ucontext ||
  1034. rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  1035. return;
  1036. ucm_dev = kzalloc(sizeof *ucm_dev, GFP_KERNEL);
  1037. if (!ucm_dev)
  1038. return;
  1039. ucm_dev->ib_dev = device;
  1040. ucm_dev->devnum = find_first_zero_bit(dev_map, IB_UCM_MAX_DEVICES);
  1041. if (ucm_dev->devnum >= IB_UCM_MAX_DEVICES)
  1042. goto err;
  1043. set_bit(ucm_dev->devnum, dev_map);
  1044. cdev_init(&ucm_dev->dev, &ucm_fops);
  1045. ucm_dev->dev.owner = THIS_MODULE;
  1046. kobject_set_name(&ucm_dev->dev.kobj, "ucm%d", ucm_dev->devnum);
  1047. if (cdev_add(&ucm_dev->dev, IB_UCM_BASE_DEV + ucm_dev->devnum, 1))
  1048. goto err;
  1049. ucm_dev->class_dev.class = &ucm_class;
  1050. ucm_dev->class_dev.dev = device->dma_device;
  1051. ucm_dev->class_dev.devt = ucm_dev->dev.dev;
  1052. snprintf(ucm_dev->class_dev.class_id, BUS_ID_SIZE, "ucm%d",
  1053. ucm_dev->devnum);
  1054. if (class_device_register(&ucm_dev->class_dev))
  1055. goto err_cdev;
  1056. if (class_device_create_file(&ucm_dev->class_dev,
  1057. &class_device_attr_ibdev))
  1058. goto err_class;
  1059. ib_set_client_data(device, &ucm_client, ucm_dev);
  1060. return;
  1061. err_class:
  1062. class_device_unregister(&ucm_dev->class_dev);
  1063. err_cdev:
  1064. cdev_del(&ucm_dev->dev);
  1065. clear_bit(ucm_dev->devnum, dev_map);
  1066. err:
  1067. kfree(ucm_dev);
  1068. return;
  1069. }
  1070. static void ib_ucm_remove_one(struct ib_device *device)
  1071. {
  1072. struct ib_ucm_device *ucm_dev = ib_get_client_data(device, &ucm_client);
  1073. if (!ucm_dev)
  1074. return;
  1075. class_device_unregister(&ucm_dev->class_dev);
  1076. }
  1077. static ssize_t show_abi_version(struct class *class, char *buf)
  1078. {
  1079. return sprintf(buf, "%d\n", IB_USER_CM_ABI_VERSION);
  1080. }
  1081. static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  1082. static int __init ib_ucm_init(void)
  1083. {
  1084. int ret;
  1085. ret = register_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES,
  1086. "infiniband_cm");
  1087. if (ret) {
  1088. printk(KERN_ERR "ucm: couldn't register device number\n");
  1089. goto err;
  1090. }
  1091. ret = class_register(&ucm_class);
  1092. if (ret) {
  1093. printk(KERN_ERR "ucm: couldn't create class infiniband_cm\n");
  1094. goto err_chrdev;
  1095. }
  1096. ret = class_create_file(&ucm_class, &class_attr_abi_version);
  1097. if (ret) {
  1098. printk(KERN_ERR "ucm: couldn't create abi_version attribute\n");
  1099. goto err_class;
  1100. }
  1101. ret = ib_register_client(&ucm_client);
  1102. if (ret) {
  1103. printk(KERN_ERR "ucm: couldn't register client\n");
  1104. goto err_class;
  1105. }
  1106. return 0;
  1107. err_class:
  1108. class_unregister(&ucm_class);
  1109. err_chrdev:
  1110. unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
  1111. err:
  1112. return ret;
  1113. }
  1114. static void __exit ib_ucm_cleanup(void)
  1115. {
  1116. ib_unregister_client(&ucm_client);
  1117. class_unregister(&ucm_class);
  1118. unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
  1119. idr_destroy(&ctx_id_table);
  1120. }
  1121. module_init(ib_ucm_init);
  1122. module_exit(ib_ucm_cleanup);