ucm.c 33 KB

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