ucm.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  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_dev(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 print_dev_t(buf, dev->dev.dev);
  1105. }
  1106. static CLASS_DEVICE_ATTR(dev, S_IRUGO, show_dev, NULL);
  1107. static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
  1108. {
  1109. struct ib_ucm_device *dev;
  1110. dev = container_of(class_dev, struct ib_ucm_device, class_dev);
  1111. return sprintf(buf, "%s\n", dev->ib_dev->name);
  1112. }
  1113. static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  1114. static void ib_ucm_add_one(struct ib_device *device)
  1115. {
  1116. struct ib_ucm_device *ucm_dev;
  1117. if (!device->alloc_ucontext)
  1118. return;
  1119. ucm_dev = kzalloc(sizeof *ucm_dev, GFP_KERNEL);
  1120. if (!ucm_dev)
  1121. return;
  1122. ucm_dev->ib_dev = device;
  1123. ucm_dev->devnum = find_first_zero_bit(dev_map, IB_UCM_MAX_DEVICES);
  1124. if (ucm_dev->devnum >= IB_UCM_MAX_DEVICES)
  1125. goto err;
  1126. set_bit(ucm_dev->devnum, dev_map);
  1127. cdev_init(&ucm_dev->dev, &ucm_fops);
  1128. ucm_dev->dev.owner = THIS_MODULE;
  1129. kobject_set_name(&ucm_dev->dev.kobj, "ucm%d", ucm_dev->devnum);
  1130. if (cdev_add(&ucm_dev->dev, IB_UCM_BASE_DEV + ucm_dev->devnum, 1))
  1131. goto err;
  1132. ucm_dev->class_dev.class = &ucm_class;
  1133. ucm_dev->class_dev.dev = device->dma_device;
  1134. snprintf(ucm_dev->class_dev.class_id, BUS_ID_SIZE, "ucm%d",
  1135. ucm_dev->devnum);
  1136. if (class_device_register(&ucm_dev->class_dev))
  1137. goto err_cdev;
  1138. if (class_device_create_file(&ucm_dev->class_dev,
  1139. &class_device_attr_dev))
  1140. goto err_class;
  1141. if (class_device_create_file(&ucm_dev->class_dev,
  1142. &class_device_attr_ibdev))
  1143. goto err_class;
  1144. ib_set_client_data(device, &ucm_client, ucm_dev);
  1145. return;
  1146. err_class:
  1147. class_device_unregister(&ucm_dev->class_dev);
  1148. err_cdev:
  1149. cdev_del(&ucm_dev->dev);
  1150. clear_bit(ucm_dev->devnum, dev_map);
  1151. err:
  1152. kfree(ucm_dev);
  1153. return;
  1154. }
  1155. static void ib_ucm_remove_one(struct ib_device *device)
  1156. {
  1157. struct ib_ucm_device *ucm_dev = ib_get_client_data(device, &ucm_client);
  1158. if (!ucm_dev)
  1159. return;
  1160. class_device_unregister(&ucm_dev->class_dev);
  1161. }
  1162. static ssize_t show_abi_version(struct class *class, char *buf)
  1163. {
  1164. return sprintf(buf, "%d\n", IB_USER_CM_ABI_VERSION);
  1165. }
  1166. static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  1167. static int __init ib_ucm_init(void)
  1168. {
  1169. int ret;
  1170. ret = register_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES,
  1171. "infiniband_cm");
  1172. if (ret) {
  1173. printk(KERN_ERR "ucm: couldn't register device number\n");
  1174. goto err;
  1175. }
  1176. ret = class_register(&ucm_class);
  1177. if (ret) {
  1178. printk(KERN_ERR "ucm: couldn't create class infiniband_cm\n");
  1179. goto err_chrdev;
  1180. }
  1181. ret = class_create_file(&ucm_class, &class_attr_abi_version);
  1182. if (ret) {
  1183. printk(KERN_ERR "ucm: couldn't create abi_version attribute\n");
  1184. goto err_class;
  1185. }
  1186. ret = ib_register_client(&ucm_client);
  1187. if (ret) {
  1188. printk(KERN_ERR "ucm: couldn't register client\n");
  1189. goto err_class;
  1190. }
  1191. return 0;
  1192. err_class:
  1193. class_unregister(&ucm_class);
  1194. err_chrdev:
  1195. unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
  1196. err:
  1197. return ret;
  1198. }
  1199. static void __exit ib_ucm_cleanup(void)
  1200. {
  1201. ib_unregister_client(&ucm_client);
  1202. class_unregister(&ucm_class);
  1203. unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
  1204. idr_destroy(&ctx_id_table);
  1205. }
  1206. module_init(ib_ucm_init);
  1207. module_exit(ib_ucm_cleanup);