ucm.c 37 KB

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