ucm.c 33 KB

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