ucm.c 34 KB

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