ucm.c 36 KB

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