ucm.c 34 KB

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