ucm.c 33 KB

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