ucm.c 33 KB

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