blob: f504c9b00c1ba631cbe7f27b80ad0859ab9f3a08 [file] [log] [blame]
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001/*
2 * Copyright (c) 2005 Topspin Communications. All rights reserved.
Sean Heftyb9ef5202005-08-19 13:46:34 -07003 * Copyright (c) 2005 Intel Corporation. All rights reserved.
Hal Rosenstocka5b74542005-07-27 11:45:44 -07004 *
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.
Hal Rosenstocka5b74542005-07-27 11:45:44 -070032 */
Sean Hefty1b52fa982006-05-12 14:57:52 -070033
34#include <linux/completion.h>
Hal Rosenstocka5b74542005-07-27 11:45:44 -070035#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>
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040041#include <linux/sched.h>
Hal Rosenstocka5b74542005-07-27 11:45:44 -070042#include <linux/file.h>
43#include <linux/mount.h>
44#include <linux/cdev.h>
Sean Hefty595e7262005-10-17 15:33:47 -070045#include <linux/idr.h>
Ingo Molnar95ed6442006-01-13 14:51:39 -080046#include <linux/mutex.h>
Hal Rosenstocka5b74542005-07-27 11:45:44 -070047
48#include <asm/uaccess.h>
49
Sean Hefty595e7262005-10-17 15:33:47 -070050#include <rdma/ib_cm.h>
51#include <rdma/ib_user_cm.h>
Sean Hefty6a9af2e2006-06-17 20:37:27 -070052#include <rdma/ib_marshall.h>
Hal Rosenstocka5b74542005-07-27 11:45:44 -070053
54MODULE_AUTHOR("Libor Michalek");
55MODULE_DESCRIPTION("InfiniBand userspace Connection Manager access");
56MODULE_LICENSE("Dual BSD/GPL");
57
Sean Hefty07d357d2005-10-17 15:37:43 -070058struct ib_ucm_device {
59 int devnum;
Tony Jonesf4e91eb2008-02-22 00:13:36 +010060 struct cdev cdev;
61 struct device dev;
Sean Hefty07d357d2005-10-17 15:37:43 -070062 struct ib_device *ib_dev;
63};
64
Sean Hefty595e7262005-10-17 15:33:47 -070065struct ib_ucm_file {
Sean Hefty4be10c1e2006-05-25 10:03:23 -070066 struct mutex file_mutex;
Sean Hefty595e7262005-10-17 15:33:47 -070067 struct file *filp;
Sean Hefty07d357d2005-10-17 15:37:43 -070068 struct ib_ucm_device *device;
Hal Rosenstock79d81902005-07-27 20:38:56 -070069
Sean Hefty07d357d2005-10-17 15:37:43 -070070 struct list_head ctxs;
71 struct list_head events;
Sean Hefty595e7262005-10-17 15:33:47 -070072 wait_queue_head_t poll_wait;
73};
74
75struct ib_ucm_context {
76 int id;
Sean Hefty1b52fa982006-05-12 14:57:52 -070077 struct completion comp;
Sean Hefty595e7262005-10-17 15:33:47 -070078 atomic_t ref;
79 int events_reported;
80
81 struct ib_ucm_file *file;
82 struct ib_cm_id *cm_id;
83 __u64 uid;
84
85 struct list_head events; /* list of pending events. */
86 struct list_head file_list; /* member in file ctx list */
87};
88
89struct ib_ucm_event {
90 struct ib_ucm_context *ctx;
91 struct list_head file_list; /* member in file event list */
92 struct list_head ctx_list; /* member in ctx event list */
93
94 struct ib_cm_id *cm_id;
95 struct ib_ucm_event_resp resp;
96 void *data;
97 void *info;
98 int data_len;
99 int info_len;
100};
Hal Rosenstock79d81902005-07-27 20:38:56 -0700101
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700102enum {
103 IB_UCM_MAJOR = 231,
Sean Hefty07d357d2005-10-17 15:37:43 -0700104 IB_UCM_BASE_MINOR = 224,
105 IB_UCM_MAX_DEVICES = 32
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700106};
107
Sean Hefty9af57b72007-07-16 21:49:35 -0700108/* ib_cm and ib_user_cm modules share /sys/class/infiniband_cm */
109extern struct class cm_class;
110
Sean Hefty07d357d2005-10-17 15:37:43 -0700111#define IB_UCM_BASE_DEV MKDEV(IB_UCM_MAJOR, IB_UCM_BASE_MINOR)
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700112
Sean Hefty07d357d2005-10-17 15:37:43 -0700113static void ib_ucm_add_one(struct ib_device *device);
114static void ib_ucm_remove_one(struct ib_device *device);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700115
Sean Hefty07d357d2005-10-17 15:37:43 -0700116static struct ib_client ucm_client = {
117 .name = "ucm",
118 .add = ib_ucm_add_one,
119 .remove = ib_ucm_remove_one
120};
121
Ingo Molnar95ed6442006-01-13 14:51:39 -0800122static DEFINE_MUTEX(ctx_id_mutex);
Roland Dreier762a03e2005-10-17 15:38:50 -0700123static DEFINE_IDR(ctx_id_table);
Sean Hefty07d357d2005-10-17 15:37:43 -0700124static DECLARE_BITMAP(dev_map, IB_UCM_MAX_DEVICES);
Sean Hefty595e7262005-10-17 15:33:47 -0700125
Sean Heftyb9ef5202005-08-19 13:46:34 -0700126static struct ib_ucm_context *ib_ucm_ctx_get(struct ib_ucm_file *file, int id)
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700127{
128 struct ib_ucm_context *ctx;
129
Ingo Molnar95ed6442006-01-13 14:51:39 -0800130 mutex_lock(&ctx_id_mutex);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700131 ctx = idr_find(&ctx_id_table, id);
Sean Heftyb9ef5202005-08-19 13:46:34 -0700132 if (!ctx)
133 ctx = ERR_PTR(-ENOENT);
134 else if (ctx->file != file)
135 ctx = ERR_PTR(-EINVAL);
136 else
137 atomic_inc(&ctx->ref);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800138 mutex_unlock(&ctx_id_mutex);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700139
140 return ctx;
141}
142
143static void ib_ucm_ctx_put(struct ib_ucm_context *ctx)
144{
Sean Heftyb9ef5202005-08-19 13:46:34 -0700145 if (atomic_dec_and_test(&ctx->ref))
Sean Hefty1b52fa982006-05-12 14:57:52 -0700146 complete(&ctx->comp);
Sean Heftyb9ef5202005-08-19 13:46:34 -0700147}
148
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700149static inline int ib_ucm_new_cm_id(int event)
Sean Heftyb9ef5202005-08-19 13:46:34 -0700150{
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700151 return event == IB_CM_REQ_RECEIVED || event == IB_CM_SIDR_REQ_RECEIVED;
152}
153
154static void ib_ucm_cleanup_events(struct ib_ucm_context *ctx)
155{
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700156 struct ib_ucm_event *uevent;
157
Sean Hefty4be10c1e2006-05-25 10:03:23 -0700158 mutex_lock(&ctx->file->file_mutex);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700159 list_del(&ctx->file_list);
160 while (!list_empty(&ctx->events)) {
161
162 uevent = list_entry(ctx->events.next,
163 struct ib_ucm_event, ctx_list);
164 list_del(&uevent->file_list);
165 list_del(&uevent->ctx_list);
Michael S. Tsirkinf469b262006-11-29 15:33:10 -0800166 mutex_unlock(&ctx->file->file_mutex);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700167
168 /* clear incoming connections. */
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700169 if (ib_ucm_new_cm_id(uevent->resp.event))
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700170 ib_destroy_cm_id(uevent->cm_id);
171
172 kfree(uevent);
Michael S. Tsirkinf469b262006-11-29 15:33:10 -0800173 mutex_lock(&ctx->file->file_mutex);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700174 }
Sean Hefty4be10c1e2006-05-25 10:03:23 -0700175 mutex_unlock(&ctx->file->file_mutex);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700176}
177
178static struct ib_ucm_context *ib_ucm_ctx_alloc(struct ib_ucm_file *file)
179{
180 struct ib_ucm_context *ctx;
181 int result;
182
Roland Dreierde6eb662005-11-02 07:23:14 -0800183 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700184 if (!ctx)
185 return NULL;
186
Sean Heftyb9ef5202005-08-19 13:46:34 -0700187 atomic_set(&ctx->ref, 1);
Sean Hefty1b52fa982006-05-12 14:57:52 -0700188 init_completion(&ctx->comp);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700189 ctx->file = file;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700190 INIT_LIST_HEAD(&ctx->events);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700191
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700192 do {
193 result = idr_pre_get(&ctx_id_table, GFP_KERNEL);
194 if (!result)
195 goto error;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700196
Ingo Molnar95ed6442006-01-13 14:51:39 -0800197 mutex_lock(&ctx_id_mutex);
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700198 result = idr_get_new(&ctx_id_table, ctx, &ctx->id);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800199 mutex_unlock(&ctx_id_mutex);
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700200 } while (result == -EAGAIN);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700201
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700202 if (result)
203 goto error;
204
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700205 list_add_tail(&ctx->file_list, &file->ctxs);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700206 return ctx;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700207
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700208error:
209 kfree(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700210 return NULL;
211}
Sean Hefty07d357d2005-10-17 15:37:43 -0700212
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700213static void ib_ucm_event_req_get(struct ib_ucm_req_event_resp *ureq,
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700214 struct ib_cm_req_event_param *kreq)
215{
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700216 ureq->remote_ca_guid = kreq->remote_ca_guid;
217 ureq->remote_qkey = kreq->remote_qkey;
218 ureq->remote_qpn = kreq->remote_qpn;
219 ureq->qp_type = kreq->qp_type;
220 ureq->starting_psn = kreq->starting_psn;
221 ureq->responder_resources = kreq->responder_resources;
222 ureq->initiator_depth = kreq->initiator_depth;
223 ureq->local_cm_response_timeout = kreq->local_cm_response_timeout;
224 ureq->flow_control = kreq->flow_control;
225 ureq->remote_cm_response_timeout = kreq->remote_cm_response_timeout;
226 ureq->retry_count = kreq->retry_count;
227 ureq->rnr_retry_count = kreq->rnr_retry_count;
228 ureq->srq = kreq->srq;
Sean Hefty07d357d2005-10-17 15:37:43 -0700229 ureq->port = kreq->port;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700230
Sean Hefty6a9af2e2006-06-17 20:37:27 -0700231 ib_copy_path_rec_to_user(&ureq->primary_path, kreq->primary_path);
232 if (kreq->alternate_path)
233 ib_copy_path_rec_to_user(&ureq->alternate_path,
234 kreq->alternate_path);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700235}
236
237static void ib_ucm_event_rep_get(struct ib_ucm_rep_event_resp *urep,
238 struct ib_cm_rep_event_param *krep)
239{
240 urep->remote_ca_guid = krep->remote_ca_guid;
241 urep->remote_qkey = krep->remote_qkey;
242 urep->remote_qpn = krep->remote_qpn;
243 urep->starting_psn = krep->starting_psn;
244 urep->responder_resources = krep->responder_resources;
245 urep->initiator_depth = krep->initiator_depth;
246 urep->target_ack_delay = krep->target_ack_delay;
247 urep->failover_accepted = krep->failover_accepted;
248 urep->flow_control = krep->flow_control;
249 urep->rnr_retry_count = krep->rnr_retry_count;
250 urep->srq = krep->srq;
251}
252
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700253static void ib_ucm_event_sidr_rep_get(struct ib_ucm_sidr_rep_event_resp *urep,
254 struct ib_cm_sidr_rep_event_param *krep)
255{
256 urep->status = krep->status;
257 urep->qkey = krep->qkey;
258 urep->qpn = krep->qpn;
259};
260
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700261static int ib_ucm_event_process(struct ib_cm_event *evt,
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700262 struct ib_ucm_event *uvt)
263{
264 void *info = NULL;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700265
266 switch (evt->event) {
267 case IB_CM_REQ_RECEIVED:
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700268 ib_ucm_event_req_get(&uvt->resp.u.req_resp,
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700269 &evt->param.req_rcvd);
270 uvt->data_len = IB_CM_REQ_PRIVATE_DATA_SIZE;
Sean Heftyb9ef5202005-08-19 13:46:34 -0700271 uvt->resp.present = IB_UCM_PRES_PRIMARY;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700272 uvt->resp.present |= (evt->param.req_rcvd.alternate_path ?
273 IB_UCM_PRES_ALTERNATE : 0);
274 break;
275 case IB_CM_REP_RECEIVED:
276 ib_ucm_event_rep_get(&uvt->resp.u.rep_resp,
277 &evt->param.rep_rcvd);
278 uvt->data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700279 break;
280 case IB_CM_RTU_RECEIVED:
281 uvt->data_len = IB_CM_RTU_PRIVATE_DATA_SIZE;
282 uvt->resp.u.send_status = evt->param.send_status;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700283 break;
284 case IB_CM_DREQ_RECEIVED:
285 uvt->data_len = IB_CM_DREQ_PRIVATE_DATA_SIZE;
286 uvt->resp.u.send_status = evt->param.send_status;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700287 break;
288 case IB_CM_DREP_RECEIVED:
289 uvt->data_len = IB_CM_DREP_PRIVATE_DATA_SIZE;
290 uvt->resp.u.send_status = evt->param.send_status;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700291 break;
292 case IB_CM_MRA_RECEIVED:
Sean Heftyb9ef5202005-08-19 13:46:34 -0700293 uvt->resp.u.mra_resp.timeout =
294 evt->param.mra_rcvd.service_timeout;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700295 uvt->data_len = IB_CM_MRA_PRIVATE_DATA_SIZE;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700296 break;
297 case IB_CM_REJ_RECEIVED:
Sean Heftyb9ef5202005-08-19 13:46:34 -0700298 uvt->resp.u.rej_resp.reason = evt->param.rej_rcvd.reason;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700299 uvt->data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
300 uvt->info_len = evt->param.rej_rcvd.ari_length;
301 info = evt->param.rej_rcvd.ari;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700302 break;
303 case IB_CM_LAP_RECEIVED:
Sean Hefty6a9af2e2006-06-17 20:37:27 -0700304 ib_copy_path_rec_to_user(&uvt->resp.u.lap_resp.path,
305 evt->param.lap_rcvd.alternate_path);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700306 uvt->data_len = IB_CM_LAP_PRIVATE_DATA_SIZE;
Sean Heftyb9ef5202005-08-19 13:46:34 -0700307 uvt->resp.present = IB_UCM_PRES_ALTERNATE;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700308 break;
309 case IB_CM_APR_RECEIVED:
Sean Heftyb9ef5202005-08-19 13:46:34 -0700310 uvt->resp.u.apr_resp.status = evt->param.apr_rcvd.ap_status;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700311 uvt->data_len = IB_CM_APR_PRIVATE_DATA_SIZE;
312 uvt->info_len = evt->param.apr_rcvd.info_len;
313 info = evt->param.apr_rcvd.apr_info;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700314 break;
315 case IB_CM_SIDR_REQ_RECEIVED:
Roland Dreier3cd96562006-09-22 15:22:46 -0700316 uvt->resp.u.sidr_req_resp.pkey =
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700317 evt->param.sidr_req_rcvd.pkey;
Roland Dreier3cd96562006-09-22 15:22:46 -0700318 uvt->resp.u.sidr_req_resp.port =
Sean Hefty07d357d2005-10-17 15:37:43 -0700319 evt->param.sidr_req_rcvd.port;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700320 uvt->data_len = IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700321 break;
322 case IB_CM_SIDR_REP_RECEIVED:
323 ib_ucm_event_sidr_rep_get(&uvt->resp.u.sidr_rep_resp,
324 &evt->param.sidr_rep_rcvd);
325 uvt->data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
326 uvt->info_len = evt->param.sidr_rep_rcvd.info_len;
327 info = evt->param.sidr_rep_rcvd.info;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700328 break;
329 default:
330 uvt->resp.u.send_status = evt->param.send_status;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700331 break;
332 }
333
Sean Heftyb9ef5202005-08-19 13:46:34 -0700334 if (uvt->data_len) {
Eric Sesterhennbed8bdf2006-10-23 22:17:21 +0200335 uvt->data = kmemdup(evt->private_data, uvt->data_len, GFP_KERNEL);
Sean Heftyb9ef5202005-08-19 13:46:34 -0700336 if (!uvt->data)
337 goto err1;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700338
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700339 uvt->resp.present |= IB_UCM_PRES_DATA;
340 }
341
Sean Heftyb9ef5202005-08-19 13:46:34 -0700342 if (uvt->info_len) {
Eric Sesterhennbed8bdf2006-10-23 22:17:21 +0200343 uvt->info = kmemdup(info, uvt->info_len, GFP_KERNEL);
Sean Heftyb9ef5202005-08-19 13:46:34 -0700344 if (!uvt->info)
345 goto err2;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700346
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700347 uvt->resp.present |= IB_UCM_PRES_INFO;
348 }
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700349 return 0;
Sean Heftyb9ef5202005-08-19 13:46:34 -0700350
351err2:
Hal Rosenstock79d81902005-07-27 20:38:56 -0700352 kfree(uvt->data);
Sean Heftyb9ef5202005-08-19 13:46:34 -0700353err1:
354 return -ENOMEM;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700355}
356
357static int ib_ucm_event_handler(struct ib_cm_id *cm_id,
358 struct ib_cm_event *event)
359{
360 struct ib_ucm_event *uevent;
361 struct ib_ucm_context *ctx;
362 int result = 0;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700363
Sean Heftyb9ef5202005-08-19 13:46:34 -0700364 ctx = cm_id->context;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700365
Roland Dreierde6eb662005-11-02 07:23:14 -0800366 uevent = kzalloc(sizeof *uevent, GFP_KERNEL);
Sean Heftyb9ef5202005-08-19 13:46:34 -0700367 if (!uevent)
368 goto err1;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700369
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700370 uevent->ctx = ctx;
371 uevent->cm_id = cm_id;
372 uevent->resp.uid = ctx->uid;
373 uevent->resp.id = ctx->id;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700374 uevent->resp.event = event->event;
375
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700376 result = ib_ucm_event_process(event, uevent);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700377 if (result)
Sean Heftyb9ef5202005-08-19 13:46:34 -0700378 goto err2;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700379
Sean Hefty4be10c1e2006-05-25 10:03:23 -0700380 mutex_lock(&ctx->file->file_mutex);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700381 list_add_tail(&uevent->file_list, &ctx->file->events);
382 list_add_tail(&uevent->ctx_list, &ctx->events);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700383 wake_up_interruptible(&ctx->file->poll_wait);
Sean Hefty4be10c1e2006-05-25 10:03:23 -0700384 mutex_unlock(&ctx->file->file_mutex);
Sean Heftyb9ef5202005-08-19 13:46:34 -0700385 return 0;
386
387err2:
388 kfree(uevent);
389err1:
390 /* Destroy new cm_id's */
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700391 return ib_ucm_new_cm_id(event->event);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700392}
393
394static ssize_t ib_ucm_event(struct ib_ucm_file *file,
395 const char __user *inbuf,
396 int in_len, int out_len)
397{
398 struct ib_ucm_context *ctx;
399 struct ib_ucm_event_get cmd;
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700400 struct ib_ucm_event *uevent;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700401 int result = 0;
402 DEFINE_WAIT(wait);
403
404 if (out_len < sizeof(struct ib_ucm_event_resp))
405 return -ENOSPC;
406
407 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
408 return -EFAULT;
Sean Hefty07d357d2005-10-17 15:37:43 -0700409
Sean Hefty4be10c1e2006-05-25 10:03:23 -0700410 mutex_lock(&file->file_mutex);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700411 while (list_empty(&file->events)) {
Sean Hefty4be10c1e2006-05-25 10:03:23 -0700412 mutex_unlock(&file->file_mutex);
Sean Hefty9d41b7f2007-04-05 10:51:05 -0700413
414 if (file->filp->f_flags & O_NONBLOCK)
415 return -EAGAIN;
416
417 if (wait_event_interruptible(file->poll_wait,
418 !list_empty(&file->events)))
419 return -ERESTARTSYS;
420
Sean Hefty4be10c1e2006-05-25 10:03:23 -0700421 mutex_lock(&file->file_mutex);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700422 }
423
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700424 uevent = list_entry(file->events.next, struct ib_ucm_event, file_list);
425
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700426 if (ib_ucm_new_cm_id(uevent->resp.event)) {
427 ctx = ib_ucm_ctx_alloc(file);
428 if (!ctx) {
429 result = -ENOMEM;
430 goto done;
431 }
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700432
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700433 ctx->cm_id = uevent->cm_id;
434 ctx->cm_id->context = ctx;
435 uevent->resp.id = ctx->id;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700436 }
437
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700438 if (copy_to_user((void __user *)(unsigned long)cmd.response,
439 &uevent->resp, sizeof(uevent->resp))) {
440 result = -EFAULT;
441 goto done;
442 }
443
444 if (uevent->data) {
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700445 if (cmd.data_len < uevent->data_len) {
446 result = -ENOMEM;
447 goto done;
448 }
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700449 if (copy_to_user((void __user *)(unsigned long)cmd.data,
450 uevent->data, uevent->data_len)) {
451 result = -EFAULT;
452 goto done;
453 }
454 }
455
456 if (uevent->info) {
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700457 if (cmd.info_len < uevent->info_len) {
458 result = -ENOMEM;
459 goto done;
460 }
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700461 if (copy_to_user((void __user *)(unsigned long)cmd.info,
462 uevent->info, uevent->info_len)) {
463 result = -EFAULT;
464 goto done;
465 }
466 }
467
468 list_del(&uevent->file_list);
469 list_del(&uevent->ctx_list);
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700470 uevent->ctx->events_reported++;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700471
Hal Rosenstock79d81902005-07-27 20:38:56 -0700472 kfree(uevent->data);
473 kfree(uevent->info);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700474 kfree(uevent);
475done:
Sean Hefty4be10c1e2006-05-25 10:03:23 -0700476 mutex_unlock(&file->file_mutex);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700477 return result;
478}
479
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700480static ssize_t ib_ucm_create_id(struct ib_ucm_file *file,
481 const char __user *inbuf,
482 int in_len, int out_len)
483{
484 struct ib_ucm_create_id cmd;
485 struct ib_ucm_create_id_resp resp;
486 struct ib_ucm_context *ctx;
487 int result;
488
489 if (out_len < sizeof(resp))
490 return -ENOSPC;
491
492 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
493 return -EFAULT;
494
Sean Hefty4be10c1e2006-05-25 10:03:23 -0700495 mutex_lock(&file->file_mutex);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700496 ctx = ib_ucm_ctx_alloc(file);
Sean Hefty4be10c1e2006-05-25 10:03:23 -0700497 mutex_unlock(&file->file_mutex);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700498 if (!ctx)
499 return -ENOMEM;
500
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700501 ctx->uid = cmd.uid;
Sean Hefty07d357d2005-10-17 15:37:43 -0700502 ctx->cm_id = ib_create_cm_id(file->device->ib_dev,
503 ib_ucm_event_handler, ctx);
Sean Heftyb9ef5202005-08-19 13:46:34 -0700504 if (IS_ERR(ctx->cm_id)) {
505 result = PTR_ERR(ctx->cm_id);
Sean Hefty07d357d2005-10-17 15:37:43 -0700506 goto err1;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700507 }
508
509 resp.id = ctx->id;
510 if (copy_to_user((void __user *)(unsigned long)cmd.response,
511 &resp, sizeof(resp))) {
512 result = -EFAULT;
Sean Hefty07d357d2005-10-17 15:37:43 -0700513 goto err2;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700514 }
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700515 return 0;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700516
Sean Hefty07d357d2005-10-17 15:37:43 -0700517err2:
518 ib_destroy_cm_id(ctx->cm_id);
519err1:
Ingo Molnar95ed6442006-01-13 14:51:39 -0800520 mutex_lock(&ctx_id_mutex);
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700521 idr_remove(&ctx_id_table, ctx->id);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800522 mutex_unlock(&ctx_id_mutex);
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700523 kfree(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700524 return result;
525}
526
527static ssize_t ib_ucm_destroy_id(struct ib_ucm_file *file,
528 const char __user *inbuf,
529 int in_len, int out_len)
530{
531 struct ib_ucm_destroy_id cmd;
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700532 struct ib_ucm_destroy_id_resp resp;
533 struct ib_ucm_context *ctx;
534 int result = 0;
535
536 if (out_len < sizeof(resp))
537 return -ENOSPC;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700538
539 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
540 return -EFAULT;
541
Ingo Molnar95ed6442006-01-13 14:51:39 -0800542 mutex_lock(&ctx_id_mutex);
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700543 ctx = idr_find(&ctx_id_table, cmd.id);
544 if (!ctx)
545 ctx = ERR_PTR(-ENOENT);
546 else if (ctx->file != file)
547 ctx = ERR_PTR(-EINVAL);
548 else
549 idr_remove(&ctx_id_table, ctx->id);
Ingo Molnar95ed6442006-01-13 14:51:39 -0800550 mutex_unlock(&ctx_id_mutex);
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700551
552 if (IS_ERR(ctx))
553 return PTR_ERR(ctx);
554
Sean Hefty1b52fa982006-05-12 14:57:52 -0700555 ib_ucm_ctx_put(ctx);
556 wait_for_completion(&ctx->comp);
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700557
558 /* No new events will be generated after destroying the cm_id. */
559 ib_destroy_cm_id(ctx->cm_id);
560 /* Cleanup events not yet reported to the user. */
561 ib_ucm_cleanup_events(ctx);
562
563 resp.events_reported = ctx->events_reported;
564 if (copy_to_user((void __user *)(unsigned long)cmd.response,
565 &resp, sizeof(resp)))
566 result = -EFAULT;
567
568 kfree(ctx);
569 return result;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700570}
571
572static ssize_t ib_ucm_attr_id(struct ib_ucm_file *file,
573 const char __user *inbuf,
574 int in_len, int out_len)
575{
576 struct ib_ucm_attr_id_resp resp;
577 struct ib_ucm_attr_id cmd;
578 struct ib_ucm_context *ctx;
579 int result = 0;
580
581 if (out_len < sizeof(resp))
582 return -ENOSPC;
583
584 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
585 return -EFAULT;
586
Sean Heftyb9ef5202005-08-19 13:46:34 -0700587 ctx = ib_ucm_ctx_get(file, cmd.id);
588 if (IS_ERR(ctx))
589 return PTR_ERR(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700590
591 resp.service_id = ctx->cm_id->service_id;
592 resp.service_mask = ctx->cm_id->service_mask;
593 resp.local_id = ctx->cm_id->local_id;
594 resp.remote_id = ctx->cm_id->remote_id;
595
596 if (copy_to_user((void __user *)(unsigned long)cmd.response,
597 &resp, sizeof(resp)))
598 result = -EFAULT;
599
Sean Heftyb9ef5202005-08-19 13:46:34 -0700600 ib_ucm_ctx_put(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700601 return result;
602}
603
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700604static ssize_t ib_ucm_init_qp_attr(struct ib_ucm_file *file,
605 const char __user *inbuf,
606 int in_len, int out_len)
607{
Sean Hefty6a9af2e2006-06-17 20:37:27 -0700608 struct ib_uverbs_qp_attr resp;
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700609 struct ib_ucm_init_qp_attr cmd;
610 struct ib_ucm_context *ctx;
611 struct ib_qp_attr qp_attr;
612 int result = 0;
613
614 if (out_len < sizeof(resp))
615 return -ENOSPC;
616
617 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
618 return -EFAULT;
619
620 ctx = ib_ucm_ctx_get(file, cmd.id);
621 if (IS_ERR(ctx))
622 return PTR_ERR(ctx);
623
624 resp.qp_attr_mask = 0;
625 memset(&qp_attr, 0, sizeof qp_attr);
626 qp_attr.qp_state = cmd.qp_state;
627 result = ib_cm_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
628 if (result)
629 goto out;
630
Sean Hefty6a9af2e2006-06-17 20:37:27 -0700631 ib_copy_qp_attr_to_user(&resp, &qp_attr);
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700632
633 if (copy_to_user((void __user *)(unsigned long)cmd.response,
634 &resp, sizeof(resp)))
635 result = -EFAULT;
636
637out:
638 ib_ucm_ctx_put(ctx);
639 return result;
640}
641
Sean Hefty6e61d042006-06-17 20:37:28 -0700642static int ucm_validate_listen(__be64 service_id, __be64 service_mask)
643{
644 service_id &= service_mask;
645
646 if (((service_id & IB_CMA_SERVICE_ID_MASK) == IB_CMA_SERVICE_ID) ||
647 ((service_id & IB_SDP_SERVICE_ID_MASK) == IB_SDP_SERVICE_ID))
648 return -EINVAL;
649
650 return 0;
651}
652
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700653static ssize_t ib_ucm_listen(struct ib_ucm_file *file,
654 const char __user *inbuf,
655 int in_len, int out_len)
656{
657 struct ib_ucm_listen cmd;
658 struct ib_ucm_context *ctx;
659 int result;
660
661 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
662 return -EFAULT;
663
Sean Heftyb9ef5202005-08-19 13:46:34 -0700664 ctx = ib_ucm_ctx_get(file, cmd.id);
665 if (IS_ERR(ctx))
666 return PTR_ERR(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700667
Sean Hefty6e61d042006-06-17 20:37:28 -0700668 result = ucm_validate_listen(cmd.service_id, cmd.service_mask);
669 if (result)
670 goto out;
671
672 result = ib_cm_listen(ctx->cm_id, cmd.service_id, cmd.service_mask,
673 NULL);
674out:
Sean Heftyb9ef5202005-08-19 13:46:34 -0700675 ib_ucm_ctx_put(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700676 return result;
677}
678
Sean Heftye1444b52006-11-28 14:57:13 -0800679static ssize_t ib_ucm_notify(struct ib_ucm_file *file,
680 const char __user *inbuf,
681 int in_len, int out_len)
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700682{
Sean Heftye1444b52006-11-28 14:57:13 -0800683 struct ib_ucm_notify cmd;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700684 struct ib_ucm_context *ctx;
685 int result;
686
687 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
688 return -EFAULT;
689
Sean Heftyb9ef5202005-08-19 13:46:34 -0700690 ctx = ib_ucm_ctx_get(file, cmd.id);
691 if (IS_ERR(ctx))
692 return PTR_ERR(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700693
Sean Heftye1444b52006-11-28 14:57:13 -0800694 result = ib_cm_notify(ctx->cm_id, (enum ib_event_type) cmd.event);
Sean Heftyb9ef5202005-08-19 13:46:34 -0700695 ib_ucm_ctx_put(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700696 return result;
697}
698
699static int ib_ucm_alloc_data(const void **dest, u64 src, u32 len)
700{
701 void *data;
702
703 *dest = NULL;
704
705 if (!len)
706 return 0;
707
708 data = kmalloc(len, GFP_KERNEL);
709 if (!data)
710 return -ENOMEM;
711
712 if (copy_from_user(data, (void __user *)(unsigned long)src, len)) {
713 kfree(data);
714 return -EFAULT;
715 }
716
717 *dest = data;
718 return 0;
719}
720
721static int ib_ucm_path_get(struct ib_sa_path_rec **path, u64 src)
722{
Sean Hefty6a9af2e2006-06-17 20:37:27 -0700723 struct ib_user_path_rec upath;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700724 struct ib_sa_path_rec *sa_path;
725
726 *path = NULL;
727
728 if (!src)
729 return 0;
730
731 sa_path = kmalloc(sizeof(*sa_path), GFP_KERNEL);
732 if (!sa_path)
733 return -ENOMEM;
734
Sean Hefty6a9af2e2006-06-17 20:37:27 -0700735 if (copy_from_user(&upath, (void __user *)(unsigned long)src,
736 sizeof(upath))) {
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700737
738 kfree(sa_path);
739 return -EFAULT;
740 }
741
Sean Hefty6a9af2e2006-06-17 20:37:27 -0700742 ib_copy_path_rec_from_user(sa_path, &upath);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700743 *path = sa_path;
744 return 0;
745}
746
747static ssize_t ib_ucm_send_req(struct ib_ucm_file *file,
748 const char __user *inbuf,
749 int in_len, int out_len)
750{
751 struct ib_cm_req_param param;
752 struct ib_ucm_context *ctx;
753 struct ib_ucm_req cmd;
754 int result;
755
756 param.private_data = NULL;
757 param.primary_path = NULL;
758 param.alternate_path = NULL;
759
760 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
761 return -EFAULT;
762
763 result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
764 if (result)
765 goto done;
766
767 result = ib_ucm_path_get(&param.primary_path, cmd.primary_path);
768 if (result)
769 goto done;
770
771 result = ib_ucm_path_get(&param.alternate_path, cmd.alternate_path);
772 if (result)
773 goto done;
774
775 param.private_data_len = cmd.len;
776 param.service_id = cmd.sid;
777 param.qp_num = cmd.qpn;
778 param.qp_type = cmd.qp_type;
779 param.starting_psn = cmd.psn;
780 param.peer_to_peer = cmd.peer_to_peer;
781 param.responder_resources = cmd.responder_resources;
782 param.initiator_depth = cmd.initiator_depth;
783 param.remote_cm_response_timeout = cmd.remote_cm_response_timeout;
784 param.flow_control = cmd.flow_control;
785 param.local_cm_response_timeout = cmd.local_cm_response_timeout;
786 param.retry_count = cmd.retry_count;
787 param.rnr_retry_count = cmd.rnr_retry_count;
788 param.max_cm_retries = cmd.max_cm_retries;
789 param.srq = cmd.srq;
790
Sean Heftyb9ef5202005-08-19 13:46:34 -0700791 ctx = ib_ucm_ctx_get(file, cmd.id);
792 if (!IS_ERR(ctx)) {
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700793 result = ib_send_cm_req(ctx->cm_id, &param);
Sean Heftyb9ef5202005-08-19 13:46:34 -0700794 ib_ucm_ctx_put(ctx);
795 } else
796 result = PTR_ERR(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700797
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700798done:
Hal Rosenstock79d81902005-07-27 20:38:56 -0700799 kfree(param.private_data);
800 kfree(param.primary_path);
801 kfree(param.alternate_path);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700802 return result;
803}
804
805static ssize_t ib_ucm_send_rep(struct ib_ucm_file *file,
806 const char __user *inbuf,
807 int in_len, int out_len)
808{
809 struct ib_cm_rep_param param;
810 struct ib_ucm_context *ctx;
811 struct ib_ucm_rep cmd;
812 int result;
813
814 param.private_data = NULL;
815
816 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
817 return -EFAULT;
818
819 result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
820 if (result)
821 return result;
822
823 param.qp_num = cmd.qpn;
824 param.starting_psn = cmd.psn;
825 param.private_data_len = cmd.len;
826 param.responder_resources = cmd.responder_resources;
827 param.initiator_depth = cmd.initiator_depth;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700828 param.failover_accepted = cmd.failover_accepted;
829 param.flow_control = cmd.flow_control;
830 param.rnr_retry_count = cmd.rnr_retry_count;
831 param.srq = cmd.srq;
832
Sean Heftyb9ef5202005-08-19 13:46:34 -0700833 ctx = ib_ucm_ctx_get(file, cmd.id);
834 if (!IS_ERR(ctx)) {
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700835 ctx->uid = cmd.uid;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700836 result = ib_send_cm_rep(ctx->cm_id, &param);
Sean Heftyb9ef5202005-08-19 13:46:34 -0700837 ib_ucm_ctx_put(ctx);
838 } else
839 result = PTR_ERR(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700840
Hal Rosenstock79d81902005-07-27 20:38:56 -0700841 kfree(param.private_data);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700842 return result;
843}
844
845static ssize_t ib_ucm_send_private_data(struct ib_ucm_file *file,
846 const char __user *inbuf, int in_len,
847 int (*func)(struct ib_cm_id *cm_id,
848 const void *private_data,
849 u8 private_data_len))
850{
851 struct ib_ucm_private_data cmd;
852 struct ib_ucm_context *ctx;
853 const void *private_data = NULL;
854 int result;
855
856 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
857 return -EFAULT;
858
859 result = ib_ucm_alloc_data(&private_data, cmd.data, cmd.len);
860 if (result)
861 return result;
862
Sean Heftyb9ef5202005-08-19 13:46:34 -0700863 ctx = ib_ucm_ctx_get(file, cmd.id);
864 if (!IS_ERR(ctx)) {
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700865 result = func(ctx->cm_id, private_data, cmd.len);
Sean Heftyb9ef5202005-08-19 13:46:34 -0700866 ib_ucm_ctx_put(ctx);
867 } else
868 result = PTR_ERR(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700869
Hal Rosenstock79d81902005-07-27 20:38:56 -0700870 kfree(private_data);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700871 return result;
872}
873
874static ssize_t ib_ucm_send_rtu(struct ib_ucm_file *file,
875 const char __user *inbuf,
876 int in_len, int out_len)
877{
878 return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_rtu);
879}
880
881static ssize_t ib_ucm_send_dreq(struct ib_ucm_file *file,
882 const char __user *inbuf,
883 int in_len, int out_len)
884{
885 return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_dreq);
886}
887
888static ssize_t ib_ucm_send_drep(struct ib_ucm_file *file,
889 const char __user *inbuf,
890 int in_len, int out_len)
891{
892 return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_drep);
893}
894
895static ssize_t ib_ucm_send_info(struct ib_ucm_file *file,
896 const char __user *inbuf, int in_len,
897 int (*func)(struct ib_cm_id *cm_id,
898 int status,
899 const void *info,
900 u8 info_len,
901 const void *data,
902 u8 data_len))
903{
904 struct ib_ucm_context *ctx;
905 struct ib_ucm_info cmd;
906 const void *data = NULL;
907 const void *info = NULL;
908 int result;
909
910 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
911 return -EFAULT;
912
913 result = ib_ucm_alloc_data(&data, cmd.data, cmd.data_len);
914 if (result)
915 goto done;
916
917 result = ib_ucm_alloc_data(&info, cmd.info, cmd.info_len);
918 if (result)
919 goto done;
920
Sean Heftyb9ef5202005-08-19 13:46:34 -0700921 ctx = ib_ucm_ctx_get(file, cmd.id);
922 if (!IS_ERR(ctx)) {
923 result = func(ctx->cm_id, cmd.status, info, cmd.info_len,
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700924 data, cmd.data_len);
Sean Heftyb9ef5202005-08-19 13:46:34 -0700925 ib_ucm_ctx_put(ctx);
926 } else
927 result = PTR_ERR(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700928
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700929done:
Hal Rosenstock79d81902005-07-27 20:38:56 -0700930 kfree(data);
931 kfree(info);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700932 return result;
933}
934
935static ssize_t ib_ucm_send_rej(struct ib_ucm_file *file,
936 const char __user *inbuf,
937 int in_len, int out_len)
938{
939 return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_rej);
940}
941
942static ssize_t ib_ucm_send_apr(struct ib_ucm_file *file,
943 const char __user *inbuf,
944 int in_len, int out_len)
945{
946 return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_apr);
947}
948
949static ssize_t ib_ucm_send_mra(struct ib_ucm_file *file,
950 const char __user *inbuf,
951 int in_len, int out_len)
952{
953 struct ib_ucm_context *ctx;
954 struct ib_ucm_mra cmd;
955 const void *data = NULL;
956 int result;
957
958 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
959 return -EFAULT;
960
961 result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
962 if (result)
963 return result;
964
Sean Heftyb9ef5202005-08-19 13:46:34 -0700965 ctx = ib_ucm_ctx_get(file, cmd.id);
966 if (!IS_ERR(ctx)) {
967 result = ib_send_cm_mra(ctx->cm_id, cmd.timeout, data, cmd.len);
968 ib_ucm_ctx_put(ctx);
969 } else
970 result = PTR_ERR(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700971
Hal Rosenstock79d81902005-07-27 20:38:56 -0700972 kfree(data);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700973 return result;
974}
975
976static ssize_t ib_ucm_send_lap(struct ib_ucm_file *file,
977 const char __user *inbuf,
978 int in_len, int out_len)
979{
980 struct ib_ucm_context *ctx;
981 struct ib_sa_path_rec *path = NULL;
982 struct ib_ucm_lap cmd;
983 const void *data = NULL;
984 int result;
985
986 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
987 return -EFAULT;
988
989 result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
990 if (result)
991 goto done;
992
993 result = ib_ucm_path_get(&path, cmd.path);
994 if (result)
995 goto done;
996
Sean Heftyb9ef5202005-08-19 13:46:34 -0700997 ctx = ib_ucm_ctx_get(file, cmd.id);
998 if (!IS_ERR(ctx)) {
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700999 result = ib_send_cm_lap(ctx->cm_id, path, data, cmd.len);
Sean Heftyb9ef5202005-08-19 13:46:34 -07001000 ib_ucm_ctx_put(ctx);
1001 } else
1002 result = PTR_ERR(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001003
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001004done:
Hal Rosenstock79d81902005-07-27 20:38:56 -07001005 kfree(data);
1006 kfree(path);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001007 return result;
1008}
1009
1010static ssize_t ib_ucm_send_sidr_req(struct ib_ucm_file *file,
1011 const char __user *inbuf,
1012 int in_len, int out_len)
1013{
1014 struct ib_cm_sidr_req_param param;
1015 struct ib_ucm_context *ctx;
1016 struct ib_ucm_sidr_req cmd;
1017 int result;
1018
1019 param.private_data = NULL;
1020 param.path = NULL;
1021
1022 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1023 return -EFAULT;
1024
1025 result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
1026 if (result)
1027 goto done;
1028
1029 result = ib_ucm_path_get(&param.path, cmd.path);
1030 if (result)
1031 goto done;
1032
1033 param.private_data_len = cmd.len;
1034 param.service_id = cmd.sid;
1035 param.timeout_ms = cmd.timeout;
1036 param.max_cm_retries = cmd.max_cm_retries;
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001037
Sean Heftyb9ef5202005-08-19 13:46:34 -07001038 ctx = ib_ucm_ctx_get(file, cmd.id);
1039 if (!IS_ERR(ctx)) {
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001040 result = ib_send_cm_sidr_req(ctx->cm_id, &param);
Sean Heftyb9ef5202005-08-19 13:46:34 -07001041 ib_ucm_ctx_put(ctx);
1042 } else
1043 result = PTR_ERR(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001044
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001045done:
Hal Rosenstock79d81902005-07-27 20:38:56 -07001046 kfree(param.private_data);
1047 kfree(param.path);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001048 return result;
1049}
1050
1051static ssize_t ib_ucm_send_sidr_rep(struct ib_ucm_file *file,
1052 const char __user *inbuf,
1053 int in_len, int out_len)
1054{
1055 struct ib_cm_sidr_rep_param param;
1056 struct ib_ucm_sidr_rep cmd;
1057 struct ib_ucm_context *ctx;
1058 int result;
1059
1060 param.info = NULL;
1061
1062 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1063 return -EFAULT;
1064
1065 result = ib_ucm_alloc_data(&param.private_data,
1066 cmd.data, cmd.data_len);
1067 if (result)
1068 goto done;
1069
1070 result = ib_ucm_alloc_data(&param.info, cmd.info, cmd.info_len);
1071 if (result)
1072 goto done;
1073
Sean Heftyb9ef5202005-08-19 13:46:34 -07001074 param.qp_num = cmd.qpn;
1075 param.qkey = cmd.qkey;
1076 param.status = cmd.status;
1077 param.info_length = cmd.info_len;
1078 param.private_data_len = cmd.data_len;
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001079
Sean Heftyb9ef5202005-08-19 13:46:34 -07001080 ctx = ib_ucm_ctx_get(file, cmd.id);
1081 if (!IS_ERR(ctx)) {
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001082 result = ib_send_cm_sidr_rep(ctx->cm_id, &param);
Sean Heftyb9ef5202005-08-19 13:46:34 -07001083 ib_ucm_ctx_put(ctx);
1084 } else
1085 result = PTR_ERR(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001086
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001087done:
Hal Rosenstock79d81902005-07-27 20:38:56 -07001088 kfree(param.private_data);
1089 kfree(param.info);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001090 return result;
1091}
1092
1093static ssize_t (*ucm_cmd_table[])(struct ib_ucm_file *file,
1094 const char __user *inbuf,
1095 int in_len, int out_len) = {
1096 [IB_USER_CM_CMD_CREATE_ID] = ib_ucm_create_id,
1097 [IB_USER_CM_CMD_DESTROY_ID] = ib_ucm_destroy_id,
1098 [IB_USER_CM_CMD_ATTR_ID] = ib_ucm_attr_id,
1099 [IB_USER_CM_CMD_LISTEN] = ib_ucm_listen,
Sean Heftye1444b52006-11-28 14:57:13 -08001100 [IB_USER_CM_CMD_NOTIFY] = ib_ucm_notify,
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001101 [IB_USER_CM_CMD_SEND_REQ] = ib_ucm_send_req,
1102 [IB_USER_CM_CMD_SEND_REP] = ib_ucm_send_rep,
1103 [IB_USER_CM_CMD_SEND_RTU] = ib_ucm_send_rtu,
1104 [IB_USER_CM_CMD_SEND_DREQ] = ib_ucm_send_dreq,
1105 [IB_USER_CM_CMD_SEND_DREP] = ib_ucm_send_drep,
1106 [IB_USER_CM_CMD_SEND_REJ] = ib_ucm_send_rej,
1107 [IB_USER_CM_CMD_SEND_MRA] = ib_ucm_send_mra,
1108 [IB_USER_CM_CMD_SEND_LAP] = ib_ucm_send_lap,
1109 [IB_USER_CM_CMD_SEND_APR] = ib_ucm_send_apr,
1110 [IB_USER_CM_CMD_SEND_SIDR_REQ] = ib_ucm_send_sidr_req,
1111 [IB_USER_CM_CMD_SEND_SIDR_REP] = ib_ucm_send_sidr_rep,
1112 [IB_USER_CM_CMD_EVENT] = ib_ucm_event,
Sean Hefty0b2b35f2005-09-01 09:28:03 -07001113 [IB_USER_CM_CMD_INIT_QP_ATTR] = ib_ucm_init_qp_attr,
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001114};
1115
1116static ssize_t ib_ucm_write(struct file *filp, const char __user *buf,
1117 size_t len, loff_t *pos)
1118{
1119 struct ib_ucm_file *file = filp->private_data;
1120 struct ib_ucm_cmd_hdr hdr;
1121 ssize_t result;
1122
1123 if (len < sizeof(hdr))
1124 return -EINVAL;
1125
1126 if (copy_from_user(&hdr, buf, sizeof(hdr)))
1127 return -EFAULT;
1128
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001129 if (hdr.cmd < 0 || hdr.cmd >= ARRAY_SIZE(ucm_cmd_table))
1130 return -EINVAL;
1131
1132 if (hdr.in + sizeof(hdr) > len)
1133 return -EINVAL;
1134
1135 result = ucm_cmd_table[hdr.cmd](file, buf + sizeof(hdr),
1136 hdr.in, hdr.out);
1137 if (!result)
1138 result = len;
1139
1140 return result;
1141}
1142
1143static unsigned int ib_ucm_poll(struct file *filp,
1144 struct poll_table_struct *wait)
1145{
1146 struct ib_ucm_file *file = filp->private_data;
1147 unsigned int mask = 0;
1148
1149 poll_wait(filp, &file->poll_wait, wait);
1150
1151 if (!list_empty(&file->events))
1152 mask = POLLIN | POLLRDNORM;
1153
1154 return mask;
1155}
1156
Roland Dreier5ba18b12008-07-24 20:36:59 -07001157/*
1158 * ib_ucm_open() does not need the BKL:
1159 *
1160 * - no global state is referred to;
1161 * - there is no ioctl method to race against;
1162 * - no further module initialization is required for open to work
1163 * after the device is registered.
1164 */
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001165static int ib_ucm_open(struct inode *inode, struct file *filp)
1166{
1167 struct ib_ucm_file *file;
1168
1169 file = kmalloc(sizeof(*file), GFP_KERNEL);
1170 if (!file)
1171 return -ENOMEM;
1172
1173 INIT_LIST_HEAD(&file->events);
1174 INIT_LIST_HEAD(&file->ctxs);
1175 init_waitqueue_head(&file->poll_wait);
1176
Sean Hefty4be10c1e2006-05-25 10:03:23 -07001177 mutex_init(&file->file_mutex);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001178
1179 filp->private_data = file;
1180 file->filp = filp;
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001181 file->device = container_of(inode->i_cdev, struct ib_ucm_device, cdev);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001182
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001183 return 0;
1184}
1185
1186static int ib_ucm_close(struct inode *inode, struct file *filp)
1187{
1188 struct ib_ucm_file *file = filp->private_data;
1189 struct ib_ucm_context *ctx;
1190
Sean Hefty4be10c1e2006-05-25 10:03:23 -07001191 mutex_lock(&file->file_mutex);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001192 while (!list_empty(&file->ctxs)) {
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001193 ctx = list_entry(file->ctxs.next,
1194 struct ib_ucm_context, file_list);
Sean Hefty4be10c1e2006-05-25 10:03:23 -07001195 mutex_unlock(&file->file_mutex);
Sean Hefty0b2b35f2005-09-01 09:28:03 -07001196
Ingo Molnar95ed6442006-01-13 14:51:39 -08001197 mutex_lock(&ctx_id_mutex);
Sean Hefty0b2b35f2005-09-01 09:28:03 -07001198 idr_remove(&ctx_id_table, ctx->id);
Ingo Molnar95ed6442006-01-13 14:51:39 -08001199 mutex_unlock(&ctx_id_mutex);
Sean Hefty0b2b35f2005-09-01 09:28:03 -07001200
1201 ib_destroy_cm_id(ctx->cm_id);
1202 ib_ucm_cleanup_events(ctx);
1203 kfree(ctx);
1204
Sean Hefty4be10c1e2006-05-25 10:03:23 -07001205 mutex_lock(&file->file_mutex);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001206 }
Sean Hefty4be10c1e2006-05-25 10:03:23 -07001207 mutex_unlock(&file->file_mutex);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001208 kfree(file);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001209 return 0;
1210}
1211
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001212static void ib_ucm_release_dev(struct device *dev)
Sean Hefty07d357d2005-10-17 15:37:43 -07001213{
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001214 struct ib_ucm_device *ucm_dev;
Sean Hefty07d357d2005-10-17 15:37:43 -07001215
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001216 ucm_dev = container_of(dev, struct ib_ucm_device, dev);
1217 cdev_del(&ucm_dev->cdev);
1218 clear_bit(ucm_dev->devnum, dev_map);
1219 kfree(ucm_dev);
Sean Hefty07d357d2005-10-17 15:37:43 -07001220}
1221
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001222static const struct file_operations ucm_fops = {
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001223 .owner = THIS_MODULE,
1224 .open = ib_ucm_open,
1225 .release = ib_ucm_close,
1226 .write = ib_ucm_write,
1227 .poll = ib_ucm_poll,
1228};
1229
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001230static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
1231 char *buf)
Sean Hefty07d357d2005-10-17 15:37:43 -07001232{
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001233 struct ib_ucm_device *ucm_dev;
Roland Dreier3cd96562006-09-22 15:22:46 -07001234
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001235 ucm_dev = container_of(dev, struct ib_ucm_device, dev);
1236 return sprintf(buf, "%s\n", ucm_dev->ib_dev->name);
Sean Hefty07d357d2005-10-17 15:37:43 -07001237}
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001238static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
Sean Hefty07d357d2005-10-17 15:37:43 -07001239
1240static void ib_ucm_add_one(struct ib_device *device)
1241{
1242 struct ib_ucm_device *ucm_dev;
1243
Tom Tucker07ebafb2006-08-03 16:02:42 -05001244 if (!device->alloc_ucontext ||
1245 rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
Sean Hefty07d357d2005-10-17 15:37:43 -07001246 return;
1247
Roland Dreierde6eb662005-11-02 07:23:14 -08001248 ucm_dev = kzalloc(sizeof *ucm_dev, GFP_KERNEL);
Sean Hefty07d357d2005-10-17 15:37:43 -07001249 if (!ucm_dev)
1250 return;
1251
Sean Hefty07d357d2005-10-17 15:37:43 -07001252 ucm_dev->ib_dev = device;
1253
1254 ucm_dev->devnum = find_first_zero_bit(dev_map, IB_UCM_MAX_DEVICES);
1255 if (ucm_dev->devnum >= IB_UCM_MAX_DEVICES)
1256 goto err;
1257
1258 set_bit(ucm_dev->devnum, dev_map);
1259
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001260 cdev_init(&ucm_dev->cdev, &ucm_fops);
1261 ucm_dev->cdev.owner = THIS_MODULE;
1262 kobject_set_name(&ucm_dev->cdev.kobj, "ucm%d", ucm_dev->devnum);
1263 if (cdev_add(&ucm_dev->cdev, IB_UCM_BASE_DEV + ucm_dev->devnum, 1))
Sean Hefty07d357d2005-10-17 15:37:43 -07001264 goto err;
1265
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001266 ucm_dev->dev.class = &cm_class;
1267 ucm_dev->dev.parent = device->dma_device;
1268 ucm_dev->dev.devt = ucm_dev->cdev.dev;
1269 ucm_dev->dev.release = ib_ucm_release_dev;
Kay Sieversd927e382009-01-06 10:44:39 -08001270 dev_set_name(&ucm_dev->dev, "ucm%d", ucm_dev->devnum);
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001271 if (device_register(&ucm_dev->dev))
Sean Hefty07d357d2005-10-17 15:37:43 -07001272 goto err_cdev;
1273
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001274 if (device_create_file(&ucm_dev->dev, &dev_attr_ibdev))
1275 goto err_dev;
Sean Hefty07d357d2005-10-17 15:37:43 -07001276
1277 ib_set_client_data(device, &ucm_client, ucm_dev);
1278 return;
1279
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001280err_dev:
1281 device_unregister(&ucm_dev->dev);
Sean Hefty07d357d2005-10-17 15:37:43 -07001282err_cdev:
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001283 cdev_del(&ucm_dev->cdev);
Sean Hefty07d357d2005-10-17 15:37:43 -07001284 clear_bit(ucm_dev->devnum, dev_map);
1285err:
1286 kfree(ucm_dev);
1287 return;
1288}
1289
1290static void ib_ucm_remove_one(struct ib_device *device)
1291{
1292 struct ib_ucm_device *ucm_dev = ib_get_client_data(device, &ucm_client);
1293
1294 if (!ucm_dev)
1295 return;
1296
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001297 device_unregister(&ucm_dev->dev);
Sean Hefty07d357d2005-10-17 15:37:43 -07001298}
1299
1300static ssize_t show_abi_version(struct class *class, char *buf)
1301{
1302 return sprintf(buf, "%d\n", IB_USER_CM_ABI_VERSION);
1303}
1304static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001305
1306static int __init ib_ucm_init(void)
1307{
Sean Hefty07d357d2005-10-17 15:37:43 -07001308 int ret;
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001309
Sean Hefty07d357d2005-10-17 15:37:43 -07001310 ret = register_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES,
1311 "infiniband_cm");
1312 if (ret) {
1313 printk(KERN_ERR "ucm: couldn't register device number\n");
Sean Hefty9af57b72007-07-16 21:49:35 -07001314 goto error1;
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001315 }
1316
Sean Hefty9af57b72007-07-16 21:49:35 -07001317 ret = class_create_file(&cm_class, &class_attr_abi_version);
Sean Hefty07d357d2005-10-17 15:37:43 -07001318 if (ret) {
1319 printk(KERN_ERR "ucm: couldn't create abi_version attribute\n");
Sean Hefty9af57b72007-07-16 21:49:35 -07001320 goto error2;
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001321 }
1322
Sean Hefty07d357d2005-10-17 15:37:43 -07001323 ret = ib_register_client(&ucm_client);
1324 if (ret) {
1325 printk(KERN_ERR "ucm: couldn't register client\n");
Sean Hefty9af57b72007-07-16 21:49:35 -07001326 goto error3;
Sean Hefty07d357d2005-10-17 15:37:43 -07001327 }
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001328 return 0;
Sean Hefty07d357d2005-10-17 15:37:43 -07001329
Sean Hefty9af57b72007-07-16 21:49:35 -07001330error3:
1331 class_remove_file(&cm_class, &class_attr_abi_version);
1332error2:
Sean Hefty07d357d2005-10-17 15:37:43 -07001333 unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
Sean Hefty9af57b72007-07-16 21:49:35 -07001334error1:
Sean Hefty07d357d2005-10-17 15:37:43 -07001335 return ret;
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001336}
1337
1338static void __exit ib_ucm_cleanup(void)
1339{
Sean Hefty07d357d2005-10-17 15:37:43 -07001340 ib_unregister_client(&ucm_client);
Sean Hefty9af57b72007-07-16 21:49:35 -07001341 class_remove_file(&cm_class, &class_attr_abi_version);
Sean Hefty07d357d2005-10-17 15:37:43 -07001342 unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
Roland Dreier5d7edb32005-10-24 10:53:25 -07001343 idr_destroy(&ctx_id_table);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001344}
1345
1346module_init(ib_ucm_init);
1347module_exit(ib_ucm_cleanup);