ucm.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  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. #include <linux/completion.h>
  34. #include <linux/init.h>
  35. #include <linux/fs.h>
  36. #include <linux/module.h>
  37. #include <linux/device.h>
  38. #include <linux/err.h>
  39. #include <linux/poll.h>
  40. #include <linux/sched.h>
  41. #include <linux/file.h>
  42. #include <linux/mount.h>
  43. #include <linux/cdev.h>
  44. #include <linux/idr.h>
  45. #include <linux/mutex.h>
  46. #include <linux/slab.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 cdev;
  57. struct device 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. mutex_unlock(&ctx->file->file_mutex);
  140. /* clear incoming connections. */
  141. if (ib_ucm_new_cm_id(uevent->resp.event))
  142. ib_destroy_cm_id(uevent->cm_id);
  143. kfree(uevent);
  144. mutex_lock(&ctx->file->file_mutex);
  145. }
  146. mutex_unlock(&ctx->file->file_mutex);
  147. }
  148. static struct ib_ucm_context *ib_ucm_ctx_alloc(struct ib_ucm_file *file)
  149. {
  150. struct ib_ucm_context *ctx;
  151. int result;
  152. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  153. if (!ctx)
  154. return NULL;
  155. atomic_set(&ctx->ref, 1);
  156. init_completion(&ctx->comp);
  157. ctx->file = file;
  158. INIT_LIST_HEAD(&ctx->events);
  159. do {
  160. result = idr_pre_get(&ctx_id_table, GFP_KERNEL);
  161. if (!result)
  162. goto error;
  163. mutex_lock(&ctx_id_mutex);
  164. result = idr_get_new(&ctx_id_table, ctx, &ctx->id);
  165. mutex_unlock(&ctx_id_mutex);
  166. } while (result == -EAGAIN);
  167. if (result)
  168. goto error;
  169. list_add_tail(&ctx->file_list, &file->ctxs);
  170. return ctx;
  171. error:
  172. kfree(ctx);
  173. return NULL;
  174. }
  175. static void ib_ucm_event_req_get(struct ib_ucm_req_event_resp *ureq,
  176. struct ib_cm_req_event_param *kreq)
  177. {
  178. ureq->remote_ca_guid = kreq->remote_ca_guid;
  179. ureq->remote_qkey = kreq->remote_qkey;
  180. ureq->remote_qpn = kreq->remote_qpn;
  181. ureq->qp_type = kreq->qp_type;
  182. ureq->starting_psn = kreq->starting_psn;
  183. ureq->responder_resources = kreq->responder_resources;
  184. ureq->initiator_depth = kreq->initiator_depth;
  185. ureq->local_cm_response_timeout = kreq->local_cm_response_timeout;
  186. ureq->flow_control = kreq->flow_control;
  187. ureq->remote_cm_response_timeout = kreq->remote_cm_response_timeout;
  188. ureq->retry_count = kreq->retry_count;
  189. ureq->rnr_retry_count = kreq->rnr_retry_count;
  190. ureq->srq = kreq->srq;
  191. ureq->port = kreq->port;
  192. ib_copy_path_rec_to_user(&ureq->primary_path, kreq->primary_path);
  193. if (kreq->alternate_path)
  194. ib_copy_path_rec_to_user(&ureq->alternate_path,
  195. kreq->alternate_path);
  196. }
  197. static void ib_ucm_event_rep_get(struct ib_ucm_rep_event_resp *urep,
  198. struct ib_cm_rep_event_param *krep)
  199. {
  200. urep->remote_ca_guid = krep->remote_ca_guid;
  201. urep->remote_qkey = krep->remote_qkey;
  202. urep->remote_qpn = krep->remote_qpn;
  203. urep->starting_psn = krep->starting_psn;
  204. urep->responder_resources = krep->responder_resources;
  205. urep->initiator_depth = krep->initiator_depth;
  206. urep->target_ack_delay = krep->target_ack_delay;
  207. urep->failover_accepted = krep->failover_accepted;
  208. urep->flow_control = krep->flow_control;
  209. urep->rnr_retry_count = krep->rnr_retry_count;
  210. urep->srq = krep->srq;
  211. }
  212. static void ib_ucm_event_sidr_rep_get(struct ib_ucm_sidr_rep_event_resp *urep,
  213. struct ib_cm_sidr_rep_event_param *krep)
  214. {
  215. urep->status = krep->status;
  216. urep->qkey = krep->qkey;
  217. urep->qpn = krep->qpn;
  218. };
  219. static int ib_ucm_event_process(struct ib_cm_event *evt,
  220. struct ib_ucm_event *uvt)
  221. {
  222. void *info = NULL;
  223. switch (evt->event) {
  224. case IB_CM_REQ_RECEIVED:
  225. ib_ucm_event_req_get(&uvt->resp.u.req_resp,
  226. &evt->param.req_rcvd);
  227. uvt->data_len = IB_CM_REQ_PRIVATE_DATA_SIZE;
  228. uvt->resp.present = IB_UCM_PRES_PRIMARY;
  229. uvt->resp.present |= (evt->param.req_rcvd.alternate_path ?
  230. IB_UCM_PRES_ALTERNATE : 0);
  231. break;
  232. case IB_CM_REP_RECEIVED:
  233. ib_ucm_event_rep_get(&uvt->resp.u.rep_resp,
  234. &evt->param.rep_rcvd);
  235. uvt->data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
  236. break;
  237. case IB_CM_RTU_RECEIVED:
  238. uvt->data_len = IB_CM_RTU_PRIVATE_DATA_SIZE;
  239. uvt->resp.u.send_status = evt->param.send_status;
  240. break;
  241. case IB_CM_DREQ_RECEIVED:
  242. uvt->data_len = IB_CM_DREQ_PRIVATE_DATA_SIZE;
  243. uvt->resp.u.send_status = evt->param.send_status;
  244. break;
  245. case IB_CM_DREP_RECEIVED:
  246. uvt->data_len = IB_CM_DREP_PRIVATE_DATA_SIZE;
  247. uvt->resp.u.send_status = evt->param.send_status;
  248. break;
  249. case IB_CM_MRA_RECEIVED:
  250. uvt->resp.u.mra_resp.timeout =
  251. evt->param.mra_rcvd.service_timeout;
  252. uvt->data_len = IB_CM_MRA_PRIVATE_DATA_SIZE;
  253. break;
  254. case IB_CM_REJ_RECEIVED:
  255. uvt->resp.u.rej_resp.reason = evt->param.rej_rcvd.reason;
  256. uvt->data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
  257. uvt->info_len = evt->param.rej_rcvd.ari_length;
  258. info = evt->param.rej_rcvd.ari;
  259. break;
  260. case IB_CM_LAP_RECEIVED:
  261. ib_copy_path_rec_to_user(&uvt->resp.u.lap_resp.path,
  262. evt->param.lap_rcvd.alternate_path);
  263. uvt->data_len = IB_CM_LAP_PRIVATE_DATA_SIZE;
  264. uvt->resp.present = IB_UCM_PRES_ALTERNATE;
  265. break;
  266. case IB_CM_APR_RECEIVED:
  267. uvt->resp.u.apr_resp.status = evt->param.apr_rcvd.ap_status;
  268. uvt->data_len = IB_CM_APR_PRIVATE_DATA_SIZE;
  269. uvt->info_len = evt->param.apr_rcvd.info_len;
  270. info = evt->param.apr_rcvd.apr_info;
  271. break;
  272. case IB_CM_SIDR_REQ_RECEIVED:
  273. uvt->resp.u.sidr_req_resp.pkey =
  274. evt->param.sidr_req_rcvd.pkey;
  275. uvt->resp.u.sidr_req_resp.port =
  276. evt->param.sidr_req_rcvd.port;
  277. uvt->data_len = IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE;
  278. break;
  279. case IB_CM_SIDR_REP_RECEIVED:
  280. ib_ucm_event_sidr_rep_get(&uvt->resp.u.sidr_rep_resp,
  281. &evt->param.sidr_rep_rcvd);
  282. uvt->data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
  283. uvt->info_len = evt->param.sidr_rep_rcvd.info_len;
  284. info = evt->param.sidr_rep_rcvd.info;
  285. break;
  286. default:
  287. uvt->resp.u.send_status = evt->param.send_status;
  288. break;
  289. }
  290. if (uvt->data_len) {
  291. uvt->data = kmemdup(evt->private_data, uvt->data_len, GFP_KERNEL);
  292. if (!uvt->data)
  293. goto err1;
  294. uvt->resp.present |= IB_UCM_PRES_DATA;
  295. }
  296. if (uvt->info_len) {
  297. uvt->info = kmemdup(info, uvt->info_len, GFP_KERNEL);
  298. if (!uvt->info)
  299. goto err2;
  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. if (out_len < sizeof(struct ib_ucm_event_resp))
  347. return -ENOSPC;
  348. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  349. return -EFAULT;
  350. mutex_lock(&file->file_mutex);
  351. while (list_empty(&file->events)) {
  352. mutex_unlock(&file->file_mutex);
  353. if (file->filp->f_flags & O_NONBLOCK)
  354. return -EAGAIN;
  355. if (wait_event_interruptible(file->poll_wait,
  356. !list_empty(&file->events)))
  357. return -ERESTARTSYS;
  358. mutex_lock(&file->file_mutex);
  359. }
  360. uevent = list_entry(file->events.next, struct ib_ucm_event, file_list);
  361. if (ib_ucm_new_cm_id(uevent->resp.event)) {
  362. ctx = ib_ucm_ctx_alloc(file);
  363. if (!ctx) {
  364. result = -ENOMEM;
  365. goto done;
  366. }
  367. ctx->cm_id = uevent->cm_id;
  368. ctx->cm_id->context = ctx;
  369. uevent->resp.id = ctx->id;
  370. }
  371. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  372. &uevent->resp, sizeof(uevent->resp))) {
  373. result = -EFAULT;
  374. goto done;
  375. }
  376. if (uevent->data) {
  377. if (cmd.data_len < uevent->data_len) {
  378. result = -ENOMEM;
  379. goto done;
  380. }
  381. if (copy_to_user((void __user *)(unsigned long)cmd.data,
  382. uevent->data, uevent->data_len)) {
  383. result = -EFAULT;
  384. goto done;
  385. }
  386. }
  387. if (uevent->info) {
  388. if (cmd.info_len < uevent->info_len) {
  389. result = -ENOMEM;
  390. goto done;
  391. }
  392. if (copy_to_user((void __user *)(unsigned long)cmd.info,
  393. uevent->info, uevent->info_len)) {
  394. result = -EFAULT;
  395. goto done;
  396. }
  397. }
  398. list_del(&uevent->file_list);
  399. list_del(&uevent->ctx_list);
  400. uevent->ctx->events_reported++;
  401. kfree(uevent->data);
  402. kfree(uevent->info);
  403. kfree(uevent);
  404. done:
  405. mutex_unlock(&file->file_mutex);
  406. return result;
  407. }
  408. static ssize_t ib_ucm_create_id(struct ib_ucm_file *file,
  409. const char __user *inbuf,
  410. int in_len, int out_len)
  411. {
  412. struct ib_ucm_create_id cmd;
  413. struct ib_ucm_create_id_resp resp;
  414. struct ib_ucm_context *ctx;
  415. int result;
  416. if (out_len < sizeof(resp))
  417. return -ENOSPC;
  418. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  419. return -EFAULT;
  420. mutex_lock(&file->file_mutex);
  421. ctx = ib_ucm_ctx_alloc(file);
  422. mutex_unlock(&file->file_mutex);
  423. if (!ctx)
  424. return -ENOMEM;
  425. ctx->uid = cmd.uid;
  426. ctx->cm_id = ib_create_cm_id(file->device->ib_dev,
  427. ib_ucm_event_handler, ctx);
  428. if (IS_ERR(ctx->cm_id)) {
  429. result = PTR_ERR(ctx->cm_id);
  430. goto err1;
  431. }
  432. resp.id = ctx->id;
  433. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  434. &resp, sizeof(resp))) {
  435. result = -EFAULT;
  436. goto err2;
  437. }
  438. return 0;
  439. err2:
  440. ib_destroy_cm_id(ctx->cm_id);
  441. err1:
  442. mutex_lock(&ctx_id_mutex);
  443. idr_remove(&ctx_id_table, ctx->id);
  444. mutex_unlock(&ctx_id_mutex);
  445. kfree(ctx);
  446. return result;
  447. }
  448. static ssize_t ib_ucm_destroy_id(struct ib_ucm_file *file,
  449. const char __user *inbuf,
  450. int in_len, int out_len)
  451. {
  452. struct ib_ucm_destroy_id cmd;
  453. struct ib_ucm_destroy_id_resp resp;
  454. struct ib_ucm_context *ctx;
  455. int result = 0;
  456. if (out_len < sizeof(resp))
  457. return -ENOSPC;
  458. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  459. return -EFAULT;
  460. mutex_lock(&ctx_id_mutex);
  461. ctx = idr_find(&ctx_id_table, cmd.id);
  462. if (!ctx)
  463. ctx = ERR_PTR(-ENOENT);
  464. else if (ctx->file != file)
  465. ctx = ERR_PTR(-EINVAL);
  466. else
  467. idr_remove(&ctx_id_table, ctx->id);
  468. mutex_unlock(&ctx_id_mutex);
  469. if (IS_ERR(ctx))
  470. return PTR_ERR(ctx);
  471. ib_ucm_ctx_put(ctx);
  472. wait_for_completion(&ctx->comp);
  473. /* No new events will be generated after destroying the cm_id. */
  474. ib_destroy_cm_id(ctx->cm_id);
  475. /* Cleanup events not yet reported to the user. */
  476. ib_ucm_cleanup_events(ctx);
  477. resp.events_reported = ctx->events_reported;
  478. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  479. &resp, sizeof(resp)))
  480. result = -EFAULT;
  481. kfree(ctx);
  482. return result;
  483. }
  484. static ssize_t ib_ucm_attr_id(struct ib_ucm_file *file,
  485. const char __user *inbuf,
  486. int in_len, int out_len)
  487. {
  488. struct ib_ucm_attr_id_resp resp;
  489. struct ib_ucm_attr_id cmd;
  490. struct ib_ucm_context *ctx;
  491. int result = 0;
  492. if (out_len < sizeof(resp))
  493. return -ENOSPC;
  494. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  495. return -EFAULT;
  496. ctx = ib_ucm_ctx_get(file, cmd.id);
  497. if (IS_ERR(ctx))
  498. return PTR_ERR(ctx);
  499. resp.service_id = ctx->cm_id->service_id;
  500. resp.service_mask = ctx->cm_id->service_mask;
  501. resp.local_id = ctx->cm_id->local_id;
  502. resp.remote_id = ctx->cm_id->remote_id;
  503. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  504. &resp, sizeof(resp)))
  505. result = -EFAULT;
  506. ib_ucm_ctx_put(ctx);
  507. return result;
  508. }
  509. static ssize_t ib_ucm_init_qp_attr(struct ib_ucm_file *file,
  510. const char __user *inbuf,
  511. int in_len, int out_len)
  512. {
  513. struct ib_uverbs_qp_attr resp;
  514. struct ib_ucm_init_qp_attr cmd;
  515. struct ib_ucm_context *ctx;
  516. struct ib_qp_attr qp_attr;
  517. int result = 0;
  518. if (out_len < sizeof(resp))
  519. return -ENOSPC;
  520. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  521. return -EFAULT;
  522. ctx = ib_ucm_ctx_get(file, cmd.id);
  523. if (IS_ERR(ctx))
  524. return PTR_ERR(ctx);
  525. resp.qp_attr_mask = 0;
  526. memset(&qp_attr, 0, sizeof qp_attr);
  527. qp_attr.qp_state = cmd.qp_state;
  528. result = ib_cm_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
  529. if (result)
  530. goto out;
  531. ib_copy_qp_attr_to_user(&resp, &qp_attr);
  532. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  533. &resp, sizeof(resp)))
  534. result = -EFAULT;
  535. out:
  536. ib_ucm_ctx_put(ctx);
  537. return result;
  538. }
  539. static int ucm_validate_listen(__be64 service_id, __be64 service_mask)
  540. {
  541. service_id &= service_mask;
  542. if (((service_id & IB_CMA_SERVICE_ID_MASK) == IB_CMA_SERVICE_ID) ||
  543. ((service_id & IB_SDP_SERVICE_ID_MASK) == IB_SDP_SERVICE_ID))
  544. return -EINVAL;
  545. return 0;
  546. }
  547. static ssize_t ib_ucm_listen(struct ib_ucm_file *file,
  548. const char __user *inbuf,
  549. int in_len, int out_len)
  550. {
  551. struct ib_ucm_listen cmd;
  552. struct ib_ucm_context *ctx;
  553. int result;
  554. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  555. return -EFAULT;
  556. ctx = ib_ucm_ctx_get(file, cmd.id);
  557. if (IS_ERR(ctx))
  558. return PTR_ERR(ctx);
  559. result = ucm_validate_listen(cmd.service_id, cmd.service_mask);
  560. if (result)
  561. goto out;
  562. result = ib_cm_listen(ctx->cm_id, cmd.service_id, cmd.service_mask,
  563. NULL);
  564. out:
  565. ib_ucm_ctx_put(ctx);
  566. return result;
  567. }
  568. static ssize_t ib_ucm_notify(struct ib_ucm_file *file,
  569. const char __user *inbuf,
  570. int in_len, int out_len)
  571. {
  572. struct ib_ucm_notify cmd;
  573. struct ib_ucm_context *ctx;
  574. int result;
  575. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  576. return -EFAULT;
  577. ctx = ib_ucm_ctx_get(file, cmd.id);
  578. if (IS_ERR(ctx))
  579. return PTR_ERR(ctx);
  580. result = ib_cm_notify(ctx->cm_id, (enum ib_event_type) cmd.event);
  581. ib_ucm_ctx_put(ctx);
  582. return result;
  583. }
  584. static int ib_ucm_alloc_data(const void **dest, u64 src, u32 len)
  585. {
  586. void *data;
  587. *dest = NULL;
  588. if (!len)
  589. return 0;
  590. data = memdup_user((void __user *)(unsigned long)src, len);
  591. if (IS_ERR(data))
  592. return PTR_ERR(data);
  593. *dest = data;
  594. return 0;
  595. }
  596. static int ib_ucm_path_get(struct ib_sa_path_rec **path, u64 src)
  597. {
  598. struct ib_user_path_rec upath;
  599. struct ib_sa_path_rec *sa_path;
  600. *path = NULL;
  601. if (!src)
  602. return 0;
  603. sa_path = kmalloc(sizeof(*sa_path), GFP_KERNEL);
  604. if (!sa_path)
  605. return -ENOMEM;
  606. if (copy_from_user(&upath, (void __user *)(unsigned long)src,
  607. sizeof(upath))) {
  608. kfree(sa_path);
  609. return -EFAULT;
  610. }
  611. ib_copy_path_rec_from_user(sa_path, &upath);
  612. *path = sa_path;
  613. return 0;
  614. }
  615. static ssize_t ib_ucm_send_req(struct ib_ucm_file *file,
  616. const char __user *inbuf,
  617. int in_len, int out_len)
  618. {
  619. struct ib_cm_req_param param;
  620. struct ib_ucm_context *ctx;
  621. struct ib_ucm_req cmd;
  622. int result;
  623. param.private_data = NULL;
  624. param.primary_path = NULL;
  625. param.alternate_path = NULL;
  626. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  627. return -EFAULT;
  628. result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
  629. if (result)
  630. goto done;
  631. result = ib_ucm_path_get(&param.primary_path, cmd.primary_path);
  632. if (result)
  633. goto done;
  634. result = ib_ucm_path_get(&param.alternate_path, cmd.alternate_path);
  635. if (result)
  636. goto done;
  637. param.private_data_len = cmd.len;
  638. param.service_id = cmd.sid;
  639. param.qp_num = cmd.qpn;
  640. param.qp_type = cmd.qp_type;
  641. param.starting_psn = cmd.psn;
  642. param.peer_to_peer = cmd.peer_to_peer;
  643. param.responder_resources = cmd.responder_resources;
  644. param.initiator_depth = cmd.initiator_depth;
  645. param.remote_cm_response_timeout = cmd.remote_cm_response_timeout;
  646. param.flow_control = cmd.flow_control;
  647. param.local_cm_response_timeout = cmd.local_cm_response_timeout;
  648. param.retry_count = cmd.retry_count;
  649. param.rnr_retry_count = cmd.rnr_retry_count;
  650. param.max_cm_retries = cmd.max_cm_retries;
  651. param.srq = cmd.srq;
  652. ctx = ib_ucm_ctx_get(file, cmd.id);
  653. if (!IS_ERR(ctx)) {
  654. result = ib_send_cm_req(ctx->cm_id, &param);
  655. ib_ucm_ctx_put(ctx);
  656. } else
  657. result = PTR_ERR(ctx);
  658. done:
  659. kfree(param.private_data);
  660. kfree(param.primary_path);
  661. kfree(param.alternate_path);
  662. return result;
  663. }
  664. static ssize_t ib_ucm_send_rep(struct ib_ucm_file *file,
  665. const char __user *inbuf,
  666. int in_len, int out_len)
  667. {
  668. struct ib_cm_rep_param param;
  669. struct ib_ucm_context *ctx;
  670. struct ib_ucm_rep cmd;
  671. int result;
  672. param.private_data = NULL;
  673. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  674. return -EFAULT;
  675. result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
  676. if (result)
  677. return result;
  678. param.qp_num = cmd.qpn;
  679. param.starting_psn = cmd.psn;
  680. param.private_data_len = cmd.len;
  681. param.responder_resources = cmd.responder_resources;
  682. param.initiator_depth = cmd.initiator_depth;
  683. param.failover_accepted = cmd.failover_accepted;
  684. param.flow_control = cmd.flow_control;
  685. param.rnr_retry_count = cmd.rnr_retry_count;
  686. param.srq = cmd.srq;
  687. ctx = ib_ucm_ctx_get(file, cmd.id);
  688. if (!IS_ERR(ctx)) {
  689. ctx->uid = cmd.uid;
  690. result = ib_send_cm_rep(ctx->cm_id, &param);
  691. ib_ucm_ctx_put(ctx);
  692. } else
  693. result = PTR_ERR(ctx);
  694. kfree(param.private_data);
  695. return result;
  696. }
  697. static ssize_t ib_ucm_send_private_data(struct ib_ucm_file *file,
  698. const char __user *inbuf, int in_len,
  699. int (*func)(struct ib_cm_id *cm_id,
  700. const void *private_data,
  701. u8 private_data_len))
  702. {
  703. struct ib_ucm_private_data cmd;
  704. struct ib_ucm_context *ctx;
  705. const void *private_data = NULL;
  706. int result;
  707. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  708. return -EFAULT;
  709. result = ib_ucm_alloc_data(&private_data, cmd.data, cmd.len);
  710. if (result)
  711. return result;
  712. ctx = ib_ucm_ctx_get(file, cmd.id);
  713. if (!IS_ERR(ctx)) {
  714. result = func(ctx->cm_id, private_data, cmd.len);
  715. ib_ucm_ctx_put(ctx);
  716. } else
  717. result = PTR_ERR(ctx);
  718. kfree(private_data);
  719. return result;
  720. }
  721. static ssize_t ib_ucm_send_rtu(struct ib_ucm_file *file,
  722. const char __user *inbuf,
  723. int in_len, int out_len)
  724. {
  725. return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_rtu);
  726. }
  727. static ssize_t ib_ucm_send_dreq(struct ib_ucm_file *file,
  728. const char __user *inbuf,
  729. int in_len, int out_len)
  730. {
  731. return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_dreq);
  732. }
  733. static ssize_t ib_ucm_send_drep(struct ib_ucm_file *file,
  734. const char __user *inbuf,
  735. int in_len, int out_len)
  736. {
  737. return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_drep);
  738. }
  739. static ssize_t ib_ucm_send_info(struct ib_ucm_file *file,
  740. const char __user *inbuf, int in_len,
  741. int (*func)(struct ib_cm_id *cm_id,
  742. int status,
  743. const void *info,
  744. u8 info_len,
  745. const void *data,
  746. u8 data_len))
  747. {
  748. struct ib_ucm_context *ctx;
  749. struct ib_ucm_info cmd;
  750. const void *data = NULL;
  751. const void *info = NULL;
  752. int result;
  753. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  754. return -EFAULT;
  755. result = ib_ucm_alloc_data(&data, cmd.data, cmd.data_len);
  756. if (result)
  757. goto done;
  758. result = ib_ucm_alloc_data(&info, cmd.info, cmd.info_len);
  759. if (result)
  760. goto done;
  761. ctx = ib_ucm_ctx_get(file, cmd.id);
  762. if (!IS_ERR(ctx)) {
  763. result = func(ctx->cm_id, cmd.status, info, cmd.info_len,
  764. data, cmd.data_len);
  765. ib_ucm_ctx_put(ctx);
  766. } else
  767. result = PTR_ERR(ctx);
  768. done:
  769. kfree(data);
  770. kfree(info);
  771. return result;
  772. }
  773. static ssize_t ib_ucm_send_rej(struct ib_ucm_file *file,
  774. const char __user *inbuf,
  775. int in_len, int out_len)
  776. {
  777. return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_rej);
  778. }
  779. static ssize_t ib_ucm_send_apr(struct ib_ucm_file *file,
  780. const char __user *inbuf,
  781. int in_len, int out_len)
  782. {
  783. return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_apr);
  784. }
  785. static ssize_t ib_ucm_send_mra(struct ib_ucm_file *file,
  786. const char __user *inbuf,
  787. int in_len, int out_len)
  788. {
  789. struct ib_ucm_context *ctx;
  790. struct ib_ucm_mra cmd;
  791. const void *data = NULL;
  792. int result;
  793. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  794. return -EFAULT;
  795. result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
  796. if (result)
  797. return result;
  798. ctx = ib_ucm_ctx_get(file, cmd.id);
  799. if (!IS_ERR(ctx)) {
  800. result = ib_send_cm_mra(ctx->cm_id, cmd.timeout, data, cmd.len);
  801. ib_ucm_ctx_put(ctx);
  802. } else
  803. result = PTR_ERR(ctx);
  804. kfree(data);
  805. return result;
  806. }
  807. static ssize_t ib_ucm_send_lap(struct ib_ucm_file *file,
  808. const char __user *inbuf,
  809. int in_len, int out_len)
  810. {
  811. struct ib_ucm_context *ctx;
  812. struct ib_sa_path_rec *path = NULL;
  813. struct ib_ucm_lap cmd;
  814. const void *data = NULL;
  815. int result;
  816. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  817. return -EFAULT;
  818. result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
  819. if (result)
  820. goto done;
  821. result = ib_ucm_path_get(&path, cmd.path);
  822. if (result)
  823. goto done;
  824. ctx = ib_ucm_ctx_get(file, cmd.id);
  825. if (!IS_ERR(ctx)) {
  826. result = ib_send_cm_lap(ctx->cm_id, path, data, cmd.len);
  827. ib_ucm_ctx_put(ctx);
  828. } else
  829. result = PTR_ERR(ctx);
  830. done:
  831. kfree(data);
  832. kfree(path);
  833. return result;
  834. }
  835. static ssize_t ib_ucm_send_sidr_req(struct ib_ucm_file *file,
  836. const char __user *inbuf,
  837. int in_len, int out_len)
  838. {
  839. struct ib_cm_sidr_req_param param;
  840. struct ib_ucm_context *ctx;
  841. struct ib_ucm_sidr_req cmd;
  842. int result;
  843. param.private_data = NULL;
  844. param.path = NULL;
  845. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  846. return -EFAULT;
  847. result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
  848. if (result)
  849. goto done;
  850. result = ib_ucm_path_get(&param.path, cmd.path);
  851. if (result)
  852. goto done;
  853. param.private_data_len = cmd.len;
  854. param.service_id = cmd.sid;
  855. param.timeout_ms = cmd.timeout;
  856. param.max_cm_retries = cmd.max_cm_retries;
  857. ctx = ib_ucm_ctx_get(file, cmd.id);
  858. if (!IS_ERR(ctx)) {
  859. result = ib_send_cm_sidr_req(ctx->cm_id, &param);
  860. ib_ucm_ctx_put(ctx);
  861. } else
  862. result = PTR_ERR(ctx);
  863. done:
  864. kfree(param.private_data);
  865. kfree(param.path);
  866. return result;
  867. }
  868. static ssize_t ib_ucm_send_sidr_rep(struct ib_ucm_file *file,
  869. const char __user *inbuf,
  870. int in_len, int out_len)
  871. {
  872. struct ib_cm_sidr_rep_param param;
  873. struct ib_ucm_sidr_rep cmd;
  874. struct ib_ucm_context *ctx;
  875. int result;
  876. param.info = NULL;
  877. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  878. return -EFAULT;
  879. result = ib_ucm_alloc_data(&param.private_data,
  880. cmd.data, cmd.data_len);
  881. if (result)
  882. goto done;
  883. result = ib_ucm_alloc_data(&param.info, cmd.info, cmd.info_len);
  884. if (result)
  885. goto done;
  886. param.qp_num = cmd.qpn;
  887. param.qkey = cmd.qkey;
  888. param.status = cmd.status;
  889. param.info_length = cmd.info_len;
  890. param.private_data_len = cmd.data_len;
  891. ctx = ib_ucm_ctx_get(file, cmd.id);
  892. if (!IS_ERR(ctx)) {
  893. result = ib_send_cm_sidr_rep(ctx->cm_id, &param);
  894. ib_ucm_ctx_put(ctx);
  895. } else
  896. result = PTR_ERR(ctx);
  897. done:
  898. kfree(param.private_data);
  899. kfree(param.info);
  900. return result;
  901. }
  902. static ssize_t (*ucm_cmd_table[])(struct ib_ucm_file *file,
  903. const char __user *inbuf,
  904. int in_len, int out_len) = {
  905. [IB_USER_CM_CMD_CREATE_ID] = ib_ucm_create_id,
  906. [IB_USER_CM_CMD_DESTROY_ID] = ib_ucm_destroy_id,
  907. [IB_USER_CM_CMD_ATTR_ID] = ib_ucm_attr_id,
  908. [IB_USER_CM_CMD_LISTEN] = ib_ucm_listen,
  909. [IB_USER_CM_CMD_NOTIFY] = ib_ucm_notify,
  910. [IB_USER_CM_CMD_SEND_REQ] = ib_ucm_send_req,
  911. [IB_USER_CM_CMD_SEND_REP] = ib_ucm_send_rep,
  912. [IB_USER_CM_CMD_SEND_RTU] = ib_ucm_send_rtu,
  913. [IB_USER_CM_CMD_SEND_DREQ] = ib_ucm_send_dreq,
  914. [IB_USER_CM_CMD_SEND_DREP] = ib_ucm_send_drep,
  915. [IB_USER_CM_CMD_SEND_REJ] = ib_ucm_send_rej,
  916. [IB_USER_CM_CMD_SEND_MRA] = ib_ucm_send_mra,
  917. [IB_USER_CM_CMD_SEND_LAP] = ib_ucm_send_lap,
  918. [IB_USER_CM_CMD_SEND_APR] = ib_ucm_send_apr,
  919. [IB_USER_CM_CMD_SEND_SIDR_REQ] = ib_ucm_send_sidr_req,
  920. [IB_USER_CM_CMD_SEND_SIDR_REP] = ib_ucm_send_sidr_rep,
  921. [IB_USER_CM_CMD_EVENT] = ib_ucm_event,
  922. [IB_USER_CM_CMD_INIT_QP_ATTR] = ib_ucm_init_qp_attr,
  923. };
  924. static ssize_t ib_ucm_write(struct file *filp, const char __user *buf,
  925. size_t len, loff_t *pos)
  926. {
  927. struct ib_ucm_file *file = filp->private_data;
  928. struct ib_ucm_cmd_hdr hdr;
  929. ssize_t result;
  930. if (len < sizeof(hdr))
  931. return -EINVAL;
  932. if (copy_from_user(&hdr, buf, sizeof(hdr)))
  933. return -EFAULT;
  934. if (hdr.cmd >= ARRAY_SIZE(ucm_cmd_table))
  935. return -EINVAL;
  936. if (hdr.in + sizeof(hdr) > len)
  937. return -EINVAL;
  938. result = ucm_cmd_table[hdr.cmd](file, buf + sizeof(hdr),
  939. hdr.in, hdr.out);
  940. if (!result)
  941. result = len;
  942. return result;
  943. }
  944. static unsigned int ib_ucm_poll(struct file *filp,
  945. struct poll_table_struct *wait)
  946. {
  947. struct ib_ucm_file *file = filp->private_data;
  948. unsigned int mask = 0;
  949. poll_wait(filp, &file->poll_wait, wait);
  950. if (!list_empty(&file->events))
  951. mask = POLLIN | POLLRDNORM;
  952. return mask;
  953. }
  954. /*
  955. * ib_ucm_open() does not need the BKL:
  956. *
  957. * - no global state is referred to;
  958. * - there is no ioctl method to race against;
  959. * - no further module initialization is required for open to work
  960. * after the device is registered.
  961. */
  962. static int ib_ucm_open(struct inode *inode, struct file *filp)
  963. {
  964. struct ib_ucm_file *file;
  965. file = kmalloc(sizeof(*file), GFP_KERNEL);
  966. if (!file)
  967. return -ENOMEM;
  968. INIT_LIST_HEAD(&file->events);
  969. INIT_LIST_HEAD(&file->ctxs);
  970. init_waitqueue_head(&file->poll_wait);
  971. mutex_init(&file->file_mutex);
  972. filp->private_data = file;
  973. file->filp = filp;
  974. file->device = container_of(inode->i_cdev, struct ib_ucm_device, cdev);
  975. return nonseekable_open(inode, filp);
  976. }
  977. static int ib_ucm_close(struct inode *inode, struct file *filp)
  978. {
  979. struct ib_ucm_file *file = filp->private_data;
  980. struct ib_ucm_context *ctx;
  981. mutex_lock(&file->file_mutex);
  982. while (!list_empty(&file->ctxs)) {
  983. ctx = list_entry(file->ctxs.next,
  984. struct ib_ucm_context, file_list);
  985. mutex_unlock(&file->file_mutex);
  986. mutex_lock(&ctx_id_mutex);
  987. idr_remove(&ctx_id_table, ctx->id);
  988. mutex_unlock(&ctx_id_mutex);
  989. ib_destroy_cm_id(ctx->cm_id);
  990. ib_ucm_cleanup_events(ctx);
  991. kfree(ctx);
  992. mutex_lock(&file->file_mutex);
  993. }
  994. mutex_unlock(&file->file_mutex);
  995. kfree(file);
  996. return 0;
  997. }
  998. static void ib_ucm_release_dev(struct device *dev)
  999. {
  1000. struct ib_ucm_device *ucm_dev;
  1001. ucm_dev = container_of(dev, struct ib_ucm_device, dev);
  1002. cdev_del(&ucm_dev->cdev);
  1003. if (ucm_dev->devnum < IB_UCM_MAX_DEVICES)
  1004. clear_bit(ucm_dev->devnum, dev_map);
  1005. else
  1006. clear_bit(ucm_dev->devnum - IB_UCM_MAX_DEVICES, dev_map);
  1007. kfree(ucm_dev);
  1008. }
  1009. static const struct file_operations ucm_fops = {
  1010. .owner = THIS_MODULE,
  1011. .open = ib_ucm_open,
  1012. .release = ib_ucm_close,
  1013. .write = ib_ucm_write,
  1014. .poll = ib_ucm_poll,
  1015. .llseek = no_llseek,
  1016. };
  1017. static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
  1018. char *buf)
  1019. {
  1020. struct ib_ucm_device *ucm_dev;
  1021. ucm_dev = container_of(dev, struct ib_ucm_device, dev);
  1022. return sprintf(buf, "%s\n", ucm_dev->ib_dev->name);
  1023. }
  1024. static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  1025. static dev_t overflow_maj;
  1026. static DECLARE_BITMAP(overflow_map, IB_UCM_MAX_DEVICES);
  1027. static int find_overflow_devnum(void)
  1028. {
  1029. int ret;
  1030. if (!overflow_maj) {
  1031. ret = alloc_chrdev_region(&overflow_maj, 0, IB_UCM_MAX_DEVICES,
  1032. "infiniband_cm");
  1033. if (ret) {
  1034. printk(KERN_ERR "ucm: couldn't register dynamic device number\n");
  1035. return ret;
  1036. }
  1037. }
  1038. ret = find_first_zero_bit(overflow_map, IB_UCM_MAX_DEVICES);
  1039. if (ret >= IB_UCM_MAX_DEVICES)
  1040. return -1;
  1041. return ret;
  1042. }
  1043. static void ib_ucm_add_one(struct ib_device *device)
  1044. {
  1045. int devnum;
  1046. dev_t base;
  1047. struct ib_ucm_device *ucm_dev;
  1048. if (!device->alloc_ucontext ||
  1049. rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  1050. return;
  1051. ucm_dev = kzalloc(sizeof *ucm_dev, GFP_KERNEL);
  1052. if (!ucm_dev)
  1053. return;
  1054. ucm_dev->ib_dev = device;
  1055. devnum = find_first_zero_bit(dev_map, IB_UCM_MAX_DEVICES);
  1056. if (devnum >= IB_UCM_MAX_DEVICES) {
  1057. devnum = find_overflow_devnum();
  1058. if (devnum < 0)
  1059. goto err;
  1060. ucm_dev->devnum = devnum + IB_UCM_MAX_DEVICES;
  1061. base = devnum + overflow_maj;
  1062. set_bit(devnum, overflow_map);
  1063. } else {
  1064. ucm_dev->devnum = devnum;
  1065. base = devnum + IB_UCM_BASE_DEV;
  1066. set_bit(devnum, dev_map);
  1067. }
  1068. cdev_init(&ucm_dev->cdev, &ucm_fops);
  1069. ucm_dev->cdev.owner = THIS_MODULE;
  1070. kobject_set_name(&ucm_dev->cdev.kobj, "ucm%d", ucm_dev->devnum);
  1071. if (cdev_add(&ucm_dev->cdev, base, 1))
  1072. goto err;
  1073. ucm_dev->dev.class = &cm_class;
  1074. ucm_dev->dev.parent = device->dma_device;
  1075. ucm_dev->dev.devt = ucm_dev->cdev.dev;
  1076. ucm_dev->dev.release = ib_ucm_release_dev;
  1077. dev_set_name(&ucm_dev->dev, "ucm%d", ucm_dev->devnum);
  1078. if (device_register(&ucm_dev->dev))
  1079. goto err_cdev;
  1080. if (device_create_file(&ucm_dev->dev, &dev_attr_ibdev))
  1081. goto err_dev;
  1082. ib_set_client_data(device, &ucm_client, ucm_dev);
  1083. return;
  1084. err_dev:
  1085. device_unregister(&ucm_dev->dev);
  1086. err_cdev:
  1087. cdev_del(&ucm_dev->cdev);
  1088. if (ucm_dev->devnum < IB_UCM_MAX_DEVICES)
  1089. clear_bit(devnum, dev_map);
  1090. else
  1091. clear_bit(devnum, overflow_map);
  1092. err:
  1093. kfree(ucm_dev);
  1094. return;
  1095. }
  1096. static void ib_ucm_remove_one(struct ib_device *device)
  1097. {
  1098. struct ib_ucm_device *ucm_dev = ib_get_client_data(device, &ucm_client);
  1099. if (!ucm_dev)
  1100. return;
  1101. device_unregister(&ucm_dev->dev);
  1102. }
  1103. static CLASS_ATTR_STRING(abi_version, S_IRUGO,
  1104. __stringify(IB_USER_CM_ABI_VERSION));
  1105. static int __init ib_ucm_init(void)
  1106. {
  1107. int ret;
  1108. ret = register_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES,
  1109. "infiniband_cm");
  1110. if (ret) {
  1111. printk(KERN_ERR "ucm: couldn't register device number\n");
  1112. goto error1;
  1113. }
  1114. ret = class_create_file(&cm_class, &class_attr_abi_version.attr);
  1115. if (ret) {
  1116. printk(KERN_ERR "ucm: couldn't create abi_version attribute\n");
  1117. goto error2;
  1118. }
  1119. ret = ib_register_client(&ucm_client);
  1120. if (ret) {
  1121. printk(KERN_ERR "ucm: couldn't register client\n");
  1122. goto error3;
  1123. }
  1124. return 0;
  1125. error3:
  1126. class_remove_file(&cm_class, &class_attr_abi_version.attr);
  1127. error2:
  1128. unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
  1129. error1:
  1130. return ret;
  1131. }
  1132. static void __exit ib_ucm_cleanup(void)
  1133. {
  1134. ib_unregister_client(&ucm_client);
  1135. class_remove_file(&cm_class, &class_attr_abi_version.attr);
  1136. unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
  1137. if (overflow_maj)
  1138. unregister_chrdev_region(overflow_maj, IB_UCM_MAX_DEVICES);
  1139. idr_destroy(&ctx_id_table);
  1140. }
  1141. module_init(ib_ucm_init);
  1142. module_exit(ib_ucm_cleanup);