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