ucm.c 33 KB

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