blob: 6e15787d1de1a1d32e6f0df8a2e95c02e79a12f4 [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.
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>
Sean Hefty595e7262005-10-17 15:33:47 -070044#include <linux/idr.h>
Hal Rosenstocka5b74542005-07-27 11:45:44 -070045
46#include <asm/uaccess.h>
47
Sean Hefty595e7262005-10-17 15:33:47 -070048#include <rdma/ib_cm.h>
49#include <rdma/ib_user_cm.h>
Hal Rosenstocka5b74542005-07-27 11:45:44 -070050
51MODULE_AUTHOR("Libor Michalek");
52MODULE_DESCRIPTION("InfiniBand userspace Connection Manager access");
53MODULE_LICENSE("Dual BSD/GPL");
54
Sean Hefty07d357d2005-10-17 15:37:43 -070055struct ib_ucm_device {
56 int devnum;
57 struct cdev dev;
58 struct class_device class_dev;
59 struct ib_device *ib_dev;
60};
61
Sean Hefty595e7262005-10-17 15:33:47 -070062struct ib_ucm_file {
63 struct semaphore mutex;
64 struct file *filp;
Sean Hefty07d357d2005-10-17 15:37:43 -070065 struct ib_ucm_device *device;
Hal Rosenstock79d81902005-07-27 20:38:56 -070066
Sean Hefty07d357d2005-10-17 15:37:43 -070067 struct list_head ctxs;
68 struct list_head events;
Sean Hefty595e7262005-10-17 15:33:47 -070069 wait_queue_head_t poll_wait;
70};
71
72struct ib_ucm_context {
73 int id;
74 wait_queue_head_t wait;
75 atomic_t ref;
76 int events_reported;
77
78 struct ib_ucm_file *file;
79 struct ib_cm_id *cm_id;
80 __u64 uid;
81
82 struct list_head events; /* list of pending events. */
83 struct list_head file_list; /* member in file ctx list */
84};
85
86struct ib_ucm_event {
87 struct ib_ucm_context *ctx;
88 struct list_head file_list; /* member in file event list */
89 struct list_head ctx_list; /* member in ctx event list */
90
91 struct ib_cm_id *cm_id;
92 struct ib_ucm_event_resp resp;
93 void *data;
94 void *info;
95 int data_len;
96 int info_len;
97};
Hal Rosenstock79d81902005-07-27 20:38:56 -070098
Hal Rosenstocka5b74542005-07-27 11:45:44 -070099enum {
100 IB_UCM_MAJOR = 231,
Sean Hefty07d357d2005-10-17 15:37:43 -0700101 IB_UCM_BASE_MINOR = 224,
102 IB_UCM_MAX_DEVICES = 32
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700103};
104
Sean Hefty07d357d2005-10-17 15:37:43 -0700105#define IB_UCM_BASE_DEV MKDEV(IB_UCM_MAJOR, IB_UCM_BASE_MINOR)
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700106
Sean Hefty07d357d2005-10-17 15:37:43 -0700107static void ib_ucm_add_one(struct ib_device *device);
108static void ib_ucm_remove_one(struct ib_device *device);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700109
Sean Hefty07d357d2005-10-17 15:37:43 -0700110static struct ib_client ucm_client = {
111 .name = "ucm",
112 .add = ib_ucm_add_one,
113 .remove = ib_ucm_remove_one
114};
115
Roland Dreier762a03e2005-10-17 15:38:50 -0700116static DECLARE_MUTEX(ctx_id_mutex);
117static DEFINE_IDR(ctx_id_table);
Sean Hefty07d357d2005-10-17 15:37:43 -0700118static DECLARE_BITMAP(dev_map, IB_UCM_MAX_DEVICES);
Sean Hefty595e7262005-10-17 15:33:47 -0700119
Sean Heftyb9ef5202005-08-19 13:46:34 -0700120static struct ib_ucm_context *ib_ucm_ctx_get(struct ib_ucm_file *file, int id)
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700121{
122 struct ib_ucm_context *ctx;
123
124 down(&ctx_id_mutex);
125 ctx = idr_find(&ctx_id_table, id);
Sean Heftyb9ef5202005-08-19 13:46:34 -0700126 if (!ctx)
127 ctx = ERR_PTR(-ENOENT);
128 else if (ctx->file != file)
129 ctx = ERR_PTR(-EINVAL);
130 else
131 atomic_inc(&ctx->ref);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700132 up(&ctx_id_mutex);
133
134 return ctx;
135}
136
137static void ib_ucm_ctx_put(struct ib_ucm_context *ctx)
138{
Sean Heftyb9ef5202005-08-19 13:46:34 -0700139 if (atomic_dec_and_test(&ctx->ref))
140 wake_up(&ctx->wait);
141}
142
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700143static inline int ib_ucm_new_cm_id(int event)
Sean Heftyb9ef5202005-08-19 13:46:34 -0700144{
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700145 return event == IB_CM_REQ_RECEIVED || event == IB_CM_SIDR_REQ_RECEIVED;
146}
147
148static void ib_ucm_cleanup_events(struct ib_ucm_context *ctx)
149{
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700150 struct ib_ucm_event *uevent;
151
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700152 down(&ctx->file->mutex);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700153 list_del(&ctx->file_list);
154 while (!list_empty(&ctx->events)) {
155
156 uevent = list_entry(ctx->events.next,
157 struct ib_ucm_event, ctx_list);
158 list_del(&uevent->file_list);
159 list_del(&uevent->ctx_list);
160
161 /* clear incoming connections. */
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700162 if (ib_ucm_new_cm_id(uevent->resp.event))
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700163 ib_destroy_cm_id(uevent->cm_id);
164
165 kfree(uevent);
166 }
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700167 up(&ctx->file->mutex);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700168}
169
170static struct ib_ucm_context *ib_ucm_ctx_alloc(struct ib_ucm_file *file)
171{
172 struct ib_ucm_context *ctx;
173 int result;
174
Roland Dreierde6eb662005-11-02 07:23:14 -0800175 ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700176 if (!ctx)
177 return NULL;
178
Sean Heftyb9ef5202005-08-19 13:46:34 -0700179 atomic_set(&ctx->ref, 1);
180 init_waitqueue_head(&ctx->wait);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700181 ctx->file = file;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700182 INIT_LIST_HEAD(&ctx->events);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700183
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700184 do {
185 result = idr_pre_get(&ctx_id_table, GFP_KERNEL);
186 if (!result)
187 goto error;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700188
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700189 down(&ctx_id_mutex);
190 result = idr_get_new(&ctx_id_table, ctx, &ctx->id);
191 up(&ctx_id_mutex);
192 } while (result == -EAGAIN);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700193
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700194 if (result)
195 goto error;
196
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700197 list_add_tail(&ctx->file_list, &file->ctxs);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700198 return ctx;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700199
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700200error:
201 kfree(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700202 return NULL;
203}
Sean Hefty07d357d2005-10-17 15:37:43 -0700204
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700205static void ib_ucm_event_path_get(struct ib_ucm_path_rec *upath,
206 struct ib_sa_path_rec *kpath)
207{
208 if (!kpath || !upath)
209 return;
210
Sean Heftyb9ef5202005-08-19 13:46:34 -0700211 memcpy(upath->dgid, kpath->dgid.raw, sizeof *upath->dgid);
212 memcpy(upath->sgid, kpath->sgid.raw, sizeof *upath->sgid);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700213
214 upath->dlid = kpath->dlid;
215 upath->slid = kpath->slid;
216 upath->raw_traffic = kpath->raw_traffic;
217 upath->flow_label = kpath->flow_label;
218 upath->hop_limit = kpath->hop_limit;
219 upath->traffic_class = kpath->traffic_class;
220 upath->reversible = kpath->reversible;
221 upath->numb_path = kpath->numb_path;
222 upath->pkey = kpath->pkey;
223 upath->sl = kpath->sl;
224 upath->mtu_selector = kpath->mtu_selector;
225 upath->mtu = kpath->mtu;
226 upath->rate_selector = kpath->rate_selector;
227 upath->rate = kpath->rate;
228 upath->packet_life_time = kpath->packet_life_time;
229 upath->preference = kpath->preference;
230
231 upath->packet_life_time_selector =
232 kpath->packet_life_time_selector;
233}
234
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700235static void ib_ucm_event_req_get(struct ib_ucm_req_event_resp *ureq,
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700236 struct ib_cm_req_event_param *kreq)
237{
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700238 ureq->remote_ca_guid = kreq->remote_ca_guid;
239 ureq->remote_qkey = kreq->remote_qkey;
240 ureq->remote_qpn = kreq->remote_qpn;
241 ureq->qp_type = kreq->qp_type;
242 ureq->starting_psn = kreq->starting_psn;
243 ureq->responder_resources = kreq->responder_resources;
244 ureq->initiator_depth = kreq->initiator_depth;
245 ureq->local_cm_response_timeout = kreq->local_cm_response_timeout;
246 ureq->flow_control = kreq->flow_control;
247 ureq->remote_cm_response_timeout = kreq->remote_cm_response_timeout;
248 ureq->retry_count = kreq->retry_count;
249 ureq->rnr_retry_count = kreq->rnr_retry_count;
250 ureq->srq = kreq->srq;
Sean Hefty07d357d2005-10-17 15:37:43 -0700251 ureq->port = kreq->port;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700252
253 ib_ucm_event_path_get(&ureq->primary_path, kreq->primary_path);
254 ib_ucm_event_path_get(&ureq->alternate_path, kreq->alternate_path);
255}
256
257static void ib_ucm_event_rep_get(struct ib_ucm_rep_event_resp *urep,
258 struct ib_cm_rep_event_param *krep)
259{
260 urep->remote_ca_guid = krep->remote_ca_guid;
261 urep->remote_qkey = krep->remote_qkey;
262 urep->remote_qpn = krep->remote_qpn;
263 urep->starting_psn = krep->starting_psn;
264 urep->responder_resources = krep->responder_resources;
265 urep->initiator_depth = krep->initiator_depth;
266 urep->target_ack_delay = krep->target_ack_delay;
267 urep->failover_accepted = krep->failover_accepted;
268 urep->flow_control = krep->flow_control;
269 urep->rnr_retry_count = krep->rnr_retry_count;
270 urep->srq = krep->srq;
271}
272
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700273static void ib_ucm_event_sidr_rep_get(struct ib_ucm_sidr_rep_event_resp *urep,
274 struct ib_cm_sidr_rep_event_param *krep)
275{
276 urep->status = krep->status;
277 urep->qkey = krep->qkey;
278 urep->qpn = krep->qpn;
279};
280
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700281static int ib_ucm_event_process(struct ib_cm_event *evt,
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700282 struct ib_ucm_event *uvt)
283{
284 void *info = NULL;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700285
286 switch (evt->event) {
287 case IB_CM_REQ_RECEIVED:
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700288 ib_ucm_event_req_get(&uvt->resp.u.req_resp,
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700289 &evt->param.req_rcvd);
290 uvt->data_len = IB_CM_REQ_PRIVATE_DATA_SIZE;
Sean Heftyb9ef5202005-08-19 13:46:34 -0700291 uvt->resp.present = IB_UCM_PRES_PRIMARY;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700292 uvt->resp.present |= (evt->param.req_rcvd.alternate_path ?
293 IB_UCM_PRES_ALTERNATE : 0);
294 break;
295 case IB_CM_REP_RECEIVED:
296 ib_ucm_event_rep_get(&uvt->resp.u.rep_resp,
297 &evt->param.rep_rcvd);
298 uvt->data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700299 break;
300 case IB_CM_RTU_RECEIVED:
301 uvt->data_len = IB_CM_RTU_PRIVATE_DATA_SIZE;
302 uvt->resp.u.send_status = evt->param.send_status;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700303 break;
304 case IB_CM_DREQ_RECEIVED:
305 uvt->data_len = IB_CM_DREQ_PRIVATE_DATA_SIZE;
306 uvt->resp.u.send_status = evt->param.send_status;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700307 break;
308 case IB_CM_DREP_RECEIVED:
309 uvt->data_len = IB_CM_DREP_PRIVATE_DATA_SIZE;
310 uvt->resp.u.send_status = evt->param.send_status;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700311 break;
312 case IB_CM_MRA_RECEIVED:
Sean Heftyb9ef5202005-08-19 13:46:34 -0700313 uvt->resp.u.mra_resp.timeout =
314 evt->param.mra_rcvd.service_timeout;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700315 uvt->data_len = IB_CM_MRA_PRIVATE_DATA_SIZE;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700316 break;
317 case IB_CM_REJ_RECEIVED:
Sean Heftyb9ef5202005-08-19 13:46:34 -0700318 uvt->resp.u.rej_resp.reason = evt->param.rej_rcvd.reason;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700319 uvt->data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
320 uvt->info_len = evt->param.rej_rcvd.ari_length;
321 info = evt->param.rej_rcvd.ari;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700322 break;
323 case IB_CM_LAP_RECEIVED:
Sean Heftyb9ef5202005-08-19 13:46:34 -0700324 ib_ucm_event_path_get(&uvt->resp.u.lap_resp.path,
325 evt->param.lap_rcvd.alternate_path);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700326 uvt->data_len = IB_CM_LAP_PRIVATE_DATA_SIZE;
Sean Heftyb9ef5202005-08-19 13:46:34 -0700327 uvt->resp.present = IB_UCM_PRES_ALTERNATE;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700328 break;
329 case IB_CM_APR_RECEIVED:
Sean Heftyb9ef5202005-08-19 13:46:34 -0700330 uvt->resp.u.apr_resp.status = evt->param.apr_rcvd.ap_status;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700331 uvt->data_len = IB_CM_APR_PRIVATE_DATA_SIZE;
332 uvt->info_len = evt->param.apr_rcvd.info_len;
333 info = evt->param.apr_rcvd.apr_info;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700334 break;
335 case IB_CM_SIDR_REQ_RECEIVED:
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700336 uvt->resp.u.sidr_req_resp.pkey =
337 evt->param.sidr_req_rcvd.pkey;
Sean Hefty07d357d2005-10-17 15:37:43 -0700338 uvt->resp.u.sidr_req_resp.port =
339 evt->param.sidr_req_rcvd.port;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700340 uvt->data_len = IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700341 break;
342 case IB_CM_SIDR_REP_RECEIVED:
343 ib_ucm_event_sidr_rep_get(&uvt->resp.u.sidr_rep_resp,
344 &evt->param.sidr_rep_rcvd);
345 uvt->data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
346 uvt->info_len = evt->param.sidr_rep_rcvd.info_len;
347 info = evt->param.sidr_rep_rcvd.info;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700348 break;
349 default:
350 uvt->resp.u.send_status = evt->param.send_status;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700351 break;
352 }
353
Sean Heftyb9ef5202005-08-19 13:46:34 -0700354 if (uvt->data_len) {
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700355 uvt->data = kmalloc(uvt->data_len, GFP_KERNEL);
Sean Heftyb9ef5202005-08-19 13:46:34 -0700356 if (!uvt->data)
357 goto err1;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700358
359 memcpy(uvt->data, evt->private_data, uvt->data_len);
360 uvt->resp.present |= IB_UCM_PRES_DATA;
361 }
362
Sean Heftyb9ef5202005-08-19 13:46:34 -0700363 if (uvt->info_len) {
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700364 uvt->info = kmalloc(uvt->info_len, GFP_KERNEL);
Sean Heftyb9ef5202005-08-19 13:46:34 -0700365 if (!uvt->info)
366 goto err2;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700367
368 memcpy(uvt->info, info, uvt->info_len);
369 uvt->resp.present |= IB_UCM_PRES_INFO;
370 }
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700371 return 0;
Sean Heftyb9ef5202005-08-19 13:46:34 -0700372
373err2:
Hal Rosenstock79d81902005-07-27 20:38:56 -0700374 kfree(uvt->data);
Sean Heftyb9ef5202005-08-19 13:46:34 -0700375err1:
376 return -ENOMEM;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700377}
378
379static int ib_ucm_event_handler(struct ib_cm_id *cm_id,
380 struct ib_cm_event *event)
381{
382 struct ib_ucm_event *uevent;
383 struct ib_ucm_context *ctx;
384 int result = 0;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700385
Sean Heftyb9ef5202005-08-19 13:46:34 -0700386 ctx = cm_id->context;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700387
Roland Dreierde6eb662005-11-02 07:23:14 -0800388 uevent = kzalloc(sizeof *uevent, GFP_KERNEL);
Sean Heftyb9ef5202005-08-19 13:46:34 -0700389 if (!uevent)
390 goto err1;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700391
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700392 uevent->ctx = ctx;
393 uevent->cm_id = cm_id;
394 uevent->resp.uid = ctx->uid;
395 uevent->resp.id = ctx->id;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700396 uevent->resp.event = event->event;
397
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700398 result = ib_ucm_event_process(event, uevent);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700399 if (result)
Sean Heftyb9ef5202005-08-19 13:46:34 -0700400 goto err2;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700401
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700402 down(&ctx->file->mutex);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700403 list_add_tail(&uevent->file_list, &ctx->file->events);
404 list_add_tail(&uevent->ctx_list, &ctx->events);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700405 wake_up_interruptible(&ctx->file->poll_wait);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700406 up(&ctx->file->mutex);
Sean Heftyb9ef5202005-08-19 13:46:34 -0700407 return 0;
408
409err2:
410 kfree(uevent);
411err1:
412 /* Destroy new cm_id's */
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700413 return ib_ucm_new_cm_id(event->event);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700414}
415
416static ssize_t ib_ucm_event(struct ib_ucm_file *file,
417 const char __user *inbuf,
418 int in_len, int out_len)
419{
420 struct ib_ucm_context *ctx;
421 struct ib_ucm_event_get cmd;
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700422 struct ib_ucm_event *uevent;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700423 int result = 0;
424 DEFINE_WAIT(wait);
425
426 if (out_len < sizeof(struct ib_ucm_event_resp))
427 return -ENOSPC;
428
429 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
430 return -EFAULT;
Sean Hefty07d357d2005-10-17 15:37:43 -0700431
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700432 down(&file->mutex);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700433 while (list_empty(&file->events)) {
434
435 if (file->filp->f_flags & O_NONBLOCK) {
436 result = -EAGAIN;
437 break;
438 }
439
440 if (signal_pending(current)) {
441 result = -ERESTARTSYS;
442 break;
443 }
444
445 prepare_to_wait(&file->poll_wait, &wait, TASK_INTERRUPTIBLE);
446
447 up(&file->mutex);
448 schedule();
449 down(&file->mutex);
450
451 finish_wait(&file->poll_wait, &wait);
452 }
453
454 if (result)
455 goto done;
456
457 uevent = list_entry(file->events.next, struct ib_ucm_event, file_list);
458
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700459 if (ib_ucm_new_cm_id(uevent->resp.event)) {
460 ctx = ib_ucm_ctx_alloc(file);
461 if (!ctx) {
462 result = -ENOMEM;
463 goto done;
464 }
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700465
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700466 ctx->cm_id = uevent->cm_id;
467 ctx->cm_id->context = ctx;
468 uevent->resp.id = ctx->id;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700469 }
470
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700471 if (copy_to_user((void __user *)(unsigned long)cmd.response,
472 &uevent->resp, sizeof(uevent->resp))) {
473 result = -EFAULT;
474 goto done;
475 }
476
477 if (uevent->data) {
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700478 if (cmd.data_len < uevent->data_len) {
479 result = -ENOMEM;
480 goto done;
481 }
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700482 if (copy_to_user((void __user *)(unsigned long)cmd.data,
483 uevent->data, uevent->data_len)) {
484 result = -EFAULT;
485 goto done;
486 }
487 }
488
489 if (uevent->info) {
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700490 if (cmd.info_len < uevent->info_len) {
491 result = -ENOMEM;
492 goto done;
493 }
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700494 if (copy_to_user((void __user *)(unsigned long)cmd.info,
495 uevent->info, uevent->info_len)) {
496 result = -EFAULT;
497 goto done;
498 }
499 }
500
501 list_del(&uevent->file_list);
502 list_del(&uevent->ctx_list);
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700503 uevent->ctx->events_reported++;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700504
Hal Rosenstock79d81902005-07-27 20:38:56 -0700505 kfree(uevent->data);
506 kfree(uevent->info);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700507 kfree(uevent);
508done:
509 up(&file->mutex);
510 return result;
511}
512
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700513static ssize_t ib_ucm_create_id(struct ib_ucm_file *file,
514 const char __user *inbuf,
515 int in_len, int out_len)
516{
517 struct ib_ucm_create_id cmd;
518 struct ib_ucm_create_id_resp resp;
519 struct ib_ucm_context *ctx;
520 int result;
521
522 if (out_len < sizeof(resp))
523 return -ENOSPC;
524
525 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
526 return -EFAULT;
527
Sean Heftyb9ef5202005-08-19 13:46:34 -0700528 down(&file->mutex);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700529 ctx = ib_ucm_ctx_alloc(file);
Sean Heftyb9ef5202005-08-19 13:46:34 -0700530 up(&file->mutex);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700531 if (!ctx)
532 return -ENOMEM;
533
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700534 ctx->uid = cmd.uid;
Sean Hefty07d357d2005-10-17 15:37:43 -0700535 ctx->cm_id = ib_create_cm_id(file->device->ib_dev,
536 ib_ucm_event_handler, ctx);
Sean Heftyb9ef5202005-08-19 13:46:34 -0700537 if (IS_ERR(ctx->cm_id)) {
538 result = PTR_ERR(ctx->cm_id);
Sean Hefty07d357d2005-10-17 15:37:43 -0700539 goto err1;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700540 }
541
542 resp.id = ctx->id;
543 if (copy_to_user((void __user *)(unsigned long)cmd.response,
544 &resp, sizeof(resp))) {
545 result = -EFAULT;
Sean Hefty07d357d2005-10-17 15:37:43 -0700546 goto err2;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700547 }
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700548 return 0;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700549
Sean Hefty07d357d2005-10-17 15:37:43 -0700550err2:
551 ib_destroy_cm_id(ctx->cm_id);
552err1:
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700553 down(&ctx_id_mutex);
554 idr_remove(&ctx_id_table, ctx->id);
555 up(&ctx_id_mutex);
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700556 kfree(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700557 return result;
558}
559
560static ssize_t ib_ucm_destroy_id(struct ib_ucm_file *file,
561 const char __user *inbuf,
562 int in_len, int out_len)
563{
564 struct ib_ucm_destroy_id cmd;
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700565 struct ib_ucm_destroy_id_resp resp;
566 struct ib_ucm_context *ctx;
567 int result = 0;
568
569 if (out_len < sizeof(resp))
570 return -ENOSPC;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700571
572 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
573 return -EFAULT;
574
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700575 down(&ctx_id_mutex);
576 ctx = idr_find(&ctx_id_table, cmd.id);
577 if (!ctx)
578 ctx = ERR_PTR(-ENOENT);
579 else if (ctx->file != file)
580 ctx = ERR_PTR(-EINVAL);
581 else
582 idr_remove(&ctx_id_table, ctx->id);
583 up(&ctx_id_mutex);
584
585 if (IS_ERR(ctx))
586 return PTR_ERR(ctx);
587
588 atomic_dec(&ctx->ref);
589 wait_event(ctx->wait, !atomic_read(&ctx->ref));
590
591 /* No new events will be generated after destroying the cm_id. */
592 ib_destroy_cm_id(ctx->cm_id);
593 /* Cleanup events not yet reported to the user. */
594 ib_ucm_cleanup_events(ctx);
595
596 resp.events_reported = ctx->events_reported;
597 if (copy_to_user((void __user *)(unsigned long)cmd.response,
598 &resp, sizeof(resp)))
599 result = -EFAULT;
600
601 kfree(ctx);
602 return result;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700603}
604
605static ssize_t ib_ucm_attr_id(struct ib_ucm_file *file,
606 const char __user *inbuf,
607 int in_len, int out_len)
608{
609 struct ib_ucm_attr_id_resp resp;
610 struct ib_ucm_attr_id cmd;
611 struct ib_ucm_context *ctx;
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
Sean Heftyb9ef5202005-08-19 13:46:34 -0700620 ctx = ib_ucm_ctx_get(file, cmd.id);
621 if (IS_ERR(ctx))
622 return PTR_ERR(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700623
624 resp.service_id = ctx->cm_id->service_id;
625 resp.service_mask = ctx->cm_id->service_mask;
626 resp.local_id = ctx->cm_id->local_id;
627 resp.remote_id = ctx->cm_id->remote_id;
628
629 if (copy_to_user((void __user *)(unsigned long)cmd.response,
630 &resp, sizeof(resp)))
631 result = -EFAULT;
632
Sean Heftyb9ef5202005-08-19 13:46:34 -0700633 ib_ucm_ctx_put(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700634 return result;
635}
636
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700637static void ib_ucm_copy_ah_attr(struct ib_ucm_ah_attr *dest_attr,
638 struct ib_ah_attr *src_attr)
639{
640 memcpy(dest_attr->grh_dgid, src_attr->grh.dgid.raw,
641 sizeof src_attr->grh.dgid);
642 dest_attr->grh_flow_label = src_attr->grh.flow_label;
643 dest_attr->grh_sgid_index = src_attr->grh.sgid_index;
644 dest_attr->grh_hop_limit = src_attr->grh.hop_limit;
645 dest_attr->grh_traffic_class = src_attr->grh.traffic_class;
646
647 dest_attr->dlid = src_attr->dlid;
648 dest_attr->sl = src_attr->sl;
649 dest_attr->src_path_bits = src_attr->src_path_bits;
650 dest_attr->static_rate = src_attr->static_rate;
651 dest_attr->is_global = (src_attr->ah_flags & IB_AH_GRH);
652 dest_attr->port_num = src_attr->port_num;
653}
654
655static void ib_ucm_copy_qp_attr(struct ib_ucm_init_qp_attr_resp *dest_attr,
656 struct ib_qp_attr *src_attr)
657{
658 dest_attr->cur_qp_state = src_attr->cur_qp_state;
659 dest_attr->path_mtu = src_attr->path_mtu;
660 dest_attr->path_mig_state = src_attr->path_mig_state;
661 dest_attr->qkey = src_attr->qkey;
662 dest_attr->rq_psn = src_attr->rq_psn;
663 dest_attr->sq_psn = src_attr->sq_psn;
664 dest_attr->dest_qp_num = src_attr->dest_qp_num;
665 dest_attr->qp_access_flags = src_attr->qp_access_flags;
666
667 dest_attr->max_send_wr = src_attr->cap.max_send_wr;
668 dest_attr->max_recv_wr = src_attr->cap.max_recv_wr;
669 dest_attr->max_send_sge = src_attr->cap.max_send_sge;
670 dest_attr->max_recv_sge = src_attr->cap.max_recv_sge;
671 dest_attr->max_inline_data = src_attr->cap.max_inline_data;
672
673 ib_ucm_copy_ah_attr(&dest_attr->ah_attr, &src_attr->ah_attr);
674 ib_ucm_copy_ah_attr(&dest_attr->alt_ah_attr, &src_attr->alt_ah_attr);
675
676 dest_attr->pkey_index = src_attr->pkey_index;
677 dest_attr->alt_pkey_index = src_attr->alt_pkey_index;
678 dest_attr->en_sqd_async_notify = src_attr->en_sqd_async_notify;
679 dest_attr->sq_draining = src_attr->sq_draining;
680 dest_attr->max_rd_atomic = src_attr->max_rd_atomic;
681 dest_attr->max_dest_rd_atomic = src_attr->max_dest_rd_atomic;
682 dest_attr->min_rnr_timer = src_attr->min_rnr_timer;
683 dest_attr->port_num = src_attr->port_num;
684 dest_attr->timeout = src_attr->timeout;
685 dest_attr->retry_cnt = src_attr->retry_cnt;
686 dest_attr->rnr_retry = src_attr->rnr_retry;
687 dest_attr->alt_port_num = src_attr->alt_port_num;
688 dest_attr->alt_timeout = src_attr->alt_timeout;
689}
690
691static ssize_t ib_ucm_init_qp_attr(struct ib_ucm_file *file,
692 const char __user *inbuf,
693 int in_len, int out_len)
694{
695 struct ib_ucm_init_qp_attr_resp resp;
696 struct ib_ucm_init_qp_attr cmd;
697 struct ib_ucm_context *ctx;
698 struct ib_qp_attr qp_attr;
699 int result = 0;
700
701 if (out_len < sizeof(resp))
702 return -ENOSPC;
703
704 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
705 return -EFAULT;
706
707 ctx = ib_ucm_ctx_get(file, cmd.id);
708 if (IS_ERR(ctx))
709 return PTR_ERR(ctx);
710
711 resp.qp_attr_mask = 0;
712 memset(&qp_attr, 0, sizeof qp_attr);
713 qp_attr.qp_state = cmd.qp_state;
714 result = ib_cm_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
715 if (result)
716 goto out;
717
718 ib_ucm_copy_qp_attr(&resp, &qp_attr);
719
720 if (copy_to_user((void __user *)(unsigned long)cmd.response,
721 &resp, sizeof(resp)))
722 result = -EFAULT;
723
724out:
725 ib_ucm_ctx_put(ctx);
726 return result;
727}
728
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700729static ssize_t ib_ucm_listen(struct ib_ucm_file *file,
730 const char __user *inbuf,
731 int in_len, int out_len)
732{
733 struct ib_ucm_listen cmd;
734 struct ib_ucm_context *ctx;
735 int result;
736
737 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
738 return -EFAULT;
739
Sean Heftyb9ef5202005-08-19 13:46:34 -0700740 ctx = ib_ucm_ctx_get(file, cmd.id);
741 if (IS_ERR(ctx))
742 return PTR_ERR(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700743
Sean Heftyb9ef5202005-08-19 13:46:34 -0700744 result = ib_cm_listen(ctx->cm_id, cmd.service_id, cmd.service_mask);
745 ib_ucm_ctx_put(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700746 return result;
747}
748
749static ssize_t ib_ucm_establish(struct ib_ucm_file *file,
750 const char __user *inbuf,
751 int in_len, int out_len)
752{
753 struct ib_ucm_establish cmd;
754 struct ib_ucm_context *ctx;
755 int result;
756
757 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
758 return -EFAULT;
759
Sean Heftyb9ef5202005-08-19 13:46:34 -0700760 ctx = ib_ucm_ctx_get(file, cmd.id);
761 if (IS_ERR(ctx))
762 return PTR_ERR(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700763
Sean Heftyb9ef5202005-08-19 13:46:34 -0700764 result = ib_cm_establish(ctx->cm_id);
765 ib_ucm_ctx_put(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700766 return result;
767}
768
769static int ib_ucm_alloc_data(const void **dest, u64 src, u32 len)
770{
771 void *data;
772
773 *dest = NULL;
774
775 if (!len)
776 return 0;
777
778 data = kmalloc(len, GFP_KERNEL);
779 if (!data)
780 return -ENOMEM;
781
782 if (copy_from_user(data, (void __user *)(unsigned long)src, len)) {
783 kfree(data);
784 return -EFAULT;
785 }
786
787 *dest = data;
788 return 0;
789}
790
791static int ib_ucm_path_get(struct ib_sa_path_rec **path, u64 src)
792{
793 struct ib_ucm_path_rec ucm_path;
794 struct ib_sa_path_rec *sa_path;
795
796 *path = NULL;
797
798 if (!src)
799 return 0;
800
801 sa_path = kmalloc(sizeof(*sa_path), GFP_KERNEL);
802 if (!sa_path)
803 return -ENOMEM;
804
805 if (copy_from_user(&ucm_path, (void __user *)(unsigned long)src,
806 sizeof(ucm_path))) {
807
808 kfree(sa_path);
809 return -EFAULT;
810 }
811
Sean Heftyb9ef5202005-08-19 13:46:34 -0700812 memcpy(sa_path->dgid.raw, ucm_path.dgid, sizeof sa_path->dgid);
813 memcpy(sa_path->sgid.raw, ucm_path.sgid, sizeof sa_path->sgid);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700814
815 sa_path->dlid = ucm_path.dlid;
816 sa_path->slid = ucm_path.slid;
817 sa_path->raw_traffic = ucm_path.raw_traffic;
818 sa_path->flow_label = ucm_path.flow_label;
819 sa_path->hop_limit = ucm_path.hop_limit;
820 sa_path->traffic_class = ucm_path.traffic_class;
821 sa_path->reversible = ucm_path.reversible;
822 sa_path->numb_path = ucm_path.numb_path;
823 sa_path->pkey = ucm_path.pkey;
824 sa_path->sl = ucm_path.sl;
825 sa_path->mtu_selector = ucm_path.mtu_selector;
826 sa_path->mtu = ucm_path.mtu;
827 sa_path->rate_selector = ucm_path.rate_selector;
828 sa_path->rate = ucm_path.rate;
829 sa_path->packet_life_time = ucm_path.packet_life_time;
830 sa_path->preference = ucm_path.preference;
831
832 sa_path->packet_life_time_selector =
833 ucm_path.packet_life_time_selector;
834
835 *path = sa_path;
836 return 0;
837}
838
839static ssize_t ib_ucm_send_req(struct ib_ucm_file *file,
840 const char __user *inbuf,
841 int in_len, int out_len)
842{
843 struct ib_cm_req_param param;
844 struct ib_ucm_context *ctx;
845 struct ib_ucm_req cmd;
846 int result;
847
848 param.private_data = NULL;
849 param.primary_path = NULL;
850 param.alternate_path = NULL;
851
852 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
853 return -EFAULT;
854
855 result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
856 if (result)
857 goto done;
858
859 result = ib_ucm_path_get(&param.primary_path, cmd.primary_path);
860 if (result)
861 goto done;
862
863 result = ib_ucm_path_get(&param.alternate_path, cmd.alternate_path);
864 if (result)
865 goto done;
866
867 param.private_data_len = cmd.len;
868 param.service_id = cmd.sid;
869 param.qp_num = cmd.qpn;
870 param.qp_type = cmd.qp_type;
871 param.starting_psn = cmd.psn;
872 param.peer_to_peer = cmd.peer_to_peer;
873 param.responder_resources = cmd.responder_resources;
874 param.initiator_depth = cmd.initiator_depth;
875 param.remote_cm_response_timeout = cmd.remote_cm_response_timeout;
876 param.flow_control = cmd.flow_control;
877 param.local_cm_response_timeout = cmd.local_cm_response_timeout;
878 param.retry_count = cmd.retry_count;
879 param.rnr_retry_count = cmd.rnr_retry_count;
880 param.max_cm_retries = cmd.max_cm_retries;
881 param.srq = cmd.srq;
882
Sean Heftyb9ef5202005-08-19 13:46:34 -0700883 ctx = ib_ucm_ctx_get(file, cmd.id);
884 if (!IS_ERR(ctx)) {
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700885 result = ib_send_cm_req(ctx->cm_id, &param);
Sean Heftyb9ef5202005-08-19 13:46:34 -0700886 ib_ucm_ctx_put(ctx);
887 } else
888 result = PTR_ERR(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700889
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700890done:
Hal Rosenstock79d81902005-07-27 20:38:56 -0700891 kfree(param.private_data);
892 kfree(param.primary_path);
893 kfree(param.alternate_path);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700894 return result;
895}
896
897static ssize_t ib_ucm_send_rep(struct ib_ucm_file *file,
898 const char __user *inbuf,
899 int in_len, int out_len)
900{
901 struct ib_cm_rep_param param;
902 struct ib_ucm_context *ctx;
903 struct ib_ucm_rep cmd;
904 int result;
905
906 param.private_data = NULL;
907
908 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
909 return -EFAULT;
910
911 result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
912 if (result)
913 return result;
914
915 param.qp_num = cmd.qpn;
916 param.starting_psn = cmd.psn;
917 param.private_data_len = cmd.len;
918 param.responder_resources = cmd.responder_resources;
919 param.initiator_depth = cmd.initiator_depth;
920 param.target_ack_delay = cmd.target_ack_delay;
921 param.failover_accepted = cmd.failover_accepted;
922 param.flow_control = cmd.flow_control;
923 param.rnr_retry_count = cmd.rnr_retry_count;
924 param.srq = cmd.srq;
925
Sean Heftyb9ef5202005-08-19 13:46:34 -0700926 ctx = ib_ucm_ctx_get(file, cmd.id);
927 if (!IS_ERR(ctx)) {
Sean Hefty0b2b35f2005-09-01 09:28:03 -0700928 ctx->uid = cmd.uid;
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700929 result = ib_send_cm_rep(ctx->cm_id, &param);
Sean Heftyb9ef5202005-08-19 13:46:34 -0700930 ib_ucm_ctx_put(ctx);
931 } else
932 result = PTR_ERR(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700933
Hal Rosenstock79d81902005-07-27 20:38:56 -0700934 kfree(param.private_data);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700935 return result;
936}
937
938static ssize_t ib_ucm_send_private_data(struct ib_ucm_file *file,
939 const char __user *inbuf, int in_len,
940 int (*func)(struct ib_cm_id *cm_id,
941 const void *private_data,
942 u8 private_data_len))
943{
944 struct ib_ucm_private_data cmd;
945 struct ib_ucm_context *ctx;
946 const void *private_data = NULL;
947 int result;
948
949 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
950 return -EFAULT;
951
952 result = ib_ucm_alloc_data(&private_data, cmd.data, cmd.len);
953 if (result)
954 return result;
955
Sean Heftyb9ef5202005-08-19 13:46:34 -0700956 ctx = ib_ucm_ctx_get(file, cmd.id);
957 if (!IS_ERR(ctx)) {
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700958 result = func(ctx->cm_id, private_data, cmd.len);
Sean Heftyb9ef5202005-08-19 13:46:34 -0700959 ib_ucm_ctx_put(ctx);
960 } else
961 result = PTR_ERR(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700962
Hal Rosenstock79d81902005-07-27 20:38:56 -0700963 kfree(private_data);
Hal Rosenstocka5b74542005-07-27 11:45:44 -0700964 return result;
965}
966
967static ssize_t ib_ucm_send_rtu(struct ib_ucm_file *file,
968 const char __user *inbuf,
969 int in_len, int out_len)
970{
971 return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_rtu);
972}
973
974static ssize_t ib_ucm_send_dreq(struct ib_ucm_file *file,
975 const char __user *inbuf,
976 int in_len, int out_len)
977{
978 return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_dreq);
979}
980
981static ssize_t ib_ucm_send_drep(struct ib_ucm_file *file,
982 const char __user *inbuf,
983 int in_len, int out_len)
984{
985 return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_drep);
986}
987
988static ssize_t ib_ucm_send_info(struct ib_ucm_file *file,
989 const char __user *inbuf, int in_len,
990 int (*func)(struct ib_cm_id *cm_id,
991 int status,
992 const void *info,
993 u8 info_len,
994 const void *data,
995 u8 data_len))
996{
997 struct ib_ucm_context *ctx;
998 struct ib_ucm_info cmd;
999 const void *data = NULL;
1000 const void *info = NULL;
1001 int result;
1002
1003 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1004 return -EFAULT;
1005
1006 result = ib_ucm_alloc_data(&data, cmd.data, cmd.data_len);
1007 if (result)
1008 goto done;
1009
1010 result = ib_ucm_alloc_data(&info, cmd.info, cmd.info_len);
1011 if (result)
1012 goto done;
1013
Sean Heftyb9ef5202005-08-19 13:46:34 -07001014 ctx = ib_ucm_ctx_get(file, cmd.id);
1015 if (!IS_ERR(ctx)) {
1016 result = func(ctx->cm_id, cmd.status, info, cmd.info_len,
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001017 data, cmd.data_len);
Sean Heftyb9ef5202005-08-19 13:46:34 -07001018 ib_ucm_ctx_put(ctx);
1019 } else
1020 result = PTR_ERR(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001021
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001022done:
Hal Rosenstock79d81902005-07-27 20:38:56 -07001023 kfree(data);
1024 kfree(info);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001025 return result;
1026}
1027
1028static ssize_t ib_ucm_send_rej(struct ib_ucm_file *file,
1029 const char __user *inbuf,
1030 int in_len, int out_len)
1031{
1032 return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_rej);
1033}
1034
1035static ssize_t ib_ucm_send_apr(struct ib_ucm_file *file,
1036 const char __user *inbuf,
1037 int in_len, int out_len)
1038{
1039 return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_apr);
1040}
1041
1042static ssize_t ib_ucm_send_mra(struct ib_ucm_file *file,
1043 const char __user *inbuf,
1044 int in_len, int out_len)
1045{
1046 struct ib_ucm_context *ctx;
1047 struct ib_ucm_mra cmd;
1048 const void *data = NULL;
1049 int result;
1050
1051 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1052 return -EFAULT;
1053
1054 result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
1055 if (result)
1056 return result;
1057
Sean Heftyb9ef5202005-08-19 13:46:34 -07001058 ctx = ib_ucm_ctx_get(file, cmd.id);
1059 if (!IS_ERR(ctx)) {
1060 result = ib_send_cm_mra(ctx->cm_id, cmd.timeout, data, cmd.len);
1061 ib_ucm_ctx_put(ctx);
1062 } else
1063 result = PTR_ERR(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001064
Hal Rosenstock79d81902005-07-27 20:38:56 -07001065 kfree(data);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001066 return result;
1067}
1068
1069static ssize_t ib_ucm_send_lap(struct ib_ucm_file *file,
1070 const char __user *inbuf,
1071 int in_len, int out_len)
1072{
1073 struct ib_ucm_context *ctx;
1074 struct ib_sa_path_rec *path = NULL;
1075 struct ib_ucm_lap cmd;
1076 const void *data = NULL;
1077 int result;
1078
1079 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1080 return -EFAULT;
1081
1082 result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
1083 if (result)
1084 goto done;
1085
1086 result = ib_ucm_path_get(&path, cmd.path);
1087 if (result)
1088 goto done;
1089
Sean Heftyb9ef5202005-08-19 13:46:34 -07001090 ctx = ib_ucm_ctx_get(file, cmd.id);
1091 if (!IS_ERR(ctx)) {
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001092 result = ib_send_cm_lap(ctx->cm_id, path, data, cmd.len);
Sean Heftyb9ef5202005-08-19 13:46:34 -07001093 ib_ucm_ctx_put(ctx);
1094 } else
1095 result = PTR_ERR(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001096
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001097done:
Hal Rosenstock79d81902005-07-27 20:38:56 -07001098 kfree(data);
1099 kfree(path);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001100 return result;
1101}
1102
1103static ssize_t ib_ucm_send_sidr_req(struct ib_ucm_file *file,
1104 const char __user *inbuf,
1105 int in_len, int out_len)
1106{
1107 struct ib_cm_sidr_req_param param;
1108 struct ib_ucm_context *ctx;
1109 struct ib_ucm_sidr_req cmd;
1110 int result;
1111
1112 param.private_data = NULL;
1113 param.path = NULL;
1114
1115 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1116 return -EFAULT;
1117
1118 result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
1119 if (result)
1120 goto done;
1121
1122 result = ib_ucm_path_get(&param.path, cmd.path);
1123 if (result)
1124 goto done;
1125
1126 param.private_data_len = cmd.len;
1127 param.service_id = cmd.sid;
1128 param.timeout_ms = cmd.timeout;
1129 param.max_cm_retries = cmd.max_cm_retries;
1130 param.pkey = cmd.pkey;
1131
Sean Heftyb9ef5202005-08-19 13:46:34 -07001132 ctx = ib_ucm_ctx_get(file, cmd.id);
1133 if (!IS_ERR(ctx)) {
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001134 result = ib_send_cm_sidr_req(ctx->cm_id, &param);
Sean Heftyb9ef5202005-08-19 13:46:34 -07001135 ib_ucm_ctx_put(ctx);
1136 } else
1137 result = PTR_ERR(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001138
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001139done:
Hal Rosenstock79d81902005-07-27 20:38:56 -07001140 kfree(param.private_data);
1141 kfree(param.path);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001142 return result;
1143}
1144
1145static ssize_t ib_ucm_send_sidr_rep(struct ib_ucm_file *file,
1146 const char __user *inbuf,
1147 int in_len, int out_len)
1148{
1149 struct ib_cm_sidr_rep_param param;
1150 struct ib_ucm_sidr_rep cmd;
1151 struct ib_ucm_context *ctx;
1152 int result;
1153
1154 param.info = NULL;
1155
1156 if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
1157 return -EFAULT;
1158
1159 result = ib_ucm_alloc_data(&param.private_data,
1160 cmd.data, cmd.data_len);
1161 if (result)
1162 goto done;
1163
1164 result = ib_ucm_alloc_data(&param.info, cmd.info, cmd.info_len);
1165 if (result)
1166 goto done;
1167
Sean Heftyb9ef5202005-08-19 13:46:34 -07001168 param.qp_num = cmd.qpn;
1169 param.qkey = cmd.qkey;
1170 param.status = cmd.status;
1171 param.info_length = cmd.info_len;
1172 param.private_data_len = cmd.data_len;
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001173
Sean Heftyb9ef5202005-08-19 13:46:34 -07001174 ctx = ib_ucm_ctx_get(file, cmd.id);
1175 if (!IS_ERR(ctx)) {
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001176 result = ib_send_cm_sidr_rep(ctx->cm_id, &param);
Sean Heftyb9ef5202005-08-19 13:46:34 -07001177 ib_ucm_ctx_put(ctx);
1178 } else
1179 result = PTR_ERR(ctx);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001180
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001181done:
Hal Rosenstock79d81902005-07-27 20:38:56 -07001182 kfree(param.private_data);
1183 kfree(param.info);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001184 return result;
1185}
1186
1187static ssize_t (*ucm_cmd_table[])(struct ib_ucm_file *file,
1188 const char __user *inbuf,
1189 int in_len, int out_len) = {
1190 [IB_USER_CM_CMD_CREATE_ID] = ib_ucm_create_id,
1191 [IB_USER_CM_CMD_DESTROY_ID] = ib_ucm_destroy_id,
1192 [IB_USER_CM_CMD_ATTR_ID] = ib_ucm_attr_id,
1193 [IB_USER_CM_CMD_LISTEN] = ib_ucm_listen,
1194 [IB_USER_CM_CMD_ESTABLISH] = ib_ucm_establish,
1195 [IB_USER_CM_CMD_SEND_REQ] = ib_ucm_send_req,
1196 [IB_USER_CM_CMD_SEND_REP] = ib_ucm_send_rep,
1197 [IB_USER_CM_CMD_SEND_RTU] = ib_ucm_send_rtu,
1198 [IB_USER_CM_CMD_SEND_DREQ] = ib_ucm_send_dreq,
1199 [IB_USER_CM_CMD_SEND_DREP] = ib_ucm_send_drep,
1200 [IB_USER_CM_CMD_SEND_REJ] = ib_ucm_send_rej,
1201 [IB_USER_CM_CMD_SEND_MRA] = ib_ucm_send_mra,
1202 [IB_USER_CM_CMD_SEND_LAP] = ib_ucm_send_lap,
1203 [IB_USER_CM_CMD_SEND_APR] = ib_ucm_send_apr,
1204 [IB_USER_CM_CMD_SEND_SIDR_REQ] = ib_ucm_send_sidr_req,
1205 [IB_USER_CM_CMD_SEND_SIDR_REP] = ib_ucm_send_sidr_rep,
1206 [IB_USER_CM_CMD_EVENT] = ib_ucm_event,
Sean Hefty0b2b35f2005-09-01 09:28:03 -07001207 [IB_USER_CM_CMD_INIT_QP_ATTR] = ib_ucm_init_qp_attr,
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001208};
1209
1210static ssize_t ib_ucm_write(struct file *filp, const char __user *buf,
1211 size_t len, loff_t *pos)
1212{
1213 struct ib_ucm_file *file = filp->private_data;
1214 struct ib_ucm_cmd_hdr hdr;
1215 ssize_t result;
1216
1217 if (len < sizeof(hdr))
1218 return -EINVAL;
1219
1220 if (copy_from_user(&hdr, buf, sizeof(hdr)))
1221 return -EFAULT;
1222
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001223 if (hdr.cmd < 0 || hdr.cmd >= ARRAY_SIZE(ucm_cmd_table))
1224 return -EINVAL;
1225
1226 if (hdr.in + sizeof(hdr) > len)
1227 return -EINVAL;
1228
1229 result = ucm_cmd_table[hdr.cmd](file, buf + sizeof(hdr),
1230 hdr.in, hdr.out);
1231 if (!result)
1232 result = len;
1233
1234 return result;
1235}
1236
1237static unsigned int ib_ucm_poll(struct file *filp,
1238 struct poll_table_struct *wait)
1239{
1240 struct ib_ucm_file *file = filp->private_data;
1241 unsigned int mask = 0;
1242
1243 poll_wait(filp, &file->poll_wait, wait);
1244
1245 if (!list_empty(&file->events))
1246 mask = POLLIN | POLLRDNORM;
1247
1248 return mask;
1249}
1250
1251static int ib_ucm_open(struct inode *inode, struct file *filp)
1252{
1253 struct ib_ucm_file *file;
1254
1255 file = kmalloc(sizeof(*file), GFP_KERNEL);
1256 if (!file)
1257 return -ENOMEM;
1258
1259 INIT_LIST_HEAD(&file->events);
1260 INIT_LIST_HEAD(&file->ctxs);
1261 init_waitqueue_head(&file->poll_wait);
1262
1263 init_MUTEX(&file->mutex);
1264
1265 filp->private_data = file;
1266 file->filp = filp;
Sean Hefty07d357d2005-10-17 15:37:43 -07001267 file->device = container_of(inode->i_cdev, struct ib_ucm_device, dev);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001268
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001269 return 0;
1270}
1271
1272static int ib_ucm_close(struct inode *inode, struct file *filp)
1273{
1274 struct ib_ucm_file *file = filp->private_data;
1275 struct ib_ucm_context *ctx;
1276
1277 down(&file->mutex);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001278 while (!list_empty(&file->ctxs)) {
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001279 ctx = list_entry(file->ctxs.next,
1280 struct ib_ucm_context, file_list);
Sean Heftyb9ef5202005-08-19 13:46:34 -07001281 up(&file->mutex);
Sean Hefty0b2b35f2005-09-01 09:28:03 -07001282
1283 down(&ctx_id_mutex);
1284 idr_remove(&ctx_id_table, ctx->id);
1285 up(&ctx_id_mutex);
1286
1287 ib_destroy_cm_id(ctx->cm_id);
1288 ib_ucm_cleanup_events(ctx);
1289 kfree(ctx);
1290
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001291 down(&file->mutex);
1292 }
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001293 up(&file->mutex);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001294 kfree(file);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001295 return 0;
1296}
1297
Sean Hefty07d357d2005-10-17 15:37:43 -07001298static void ib_ucm_release_class_dev(struct class_device *class_dev)
1299{
1300 struct ib_ucm_device *dev;
1301
1302 dev = container_of(class_dev, struct ib_ucm_device, class_dev);
1303 cdev_del(&dev->dev);
1304 clear_bit(dev->devnum, dev_map);
1305 kfree(dev);
1306}
1307
1308static struct file_operations ucm_fops = {
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001309 .owner = THIS_MODULE,
1310 .open = ib_ucm_open,
1311 .release = ib_ucm_close,
1312 .write = ib_ucm_write,
1313 .poll = ib_ucm_poll,
1314};
1315
Sean Hefty07d357d2005-10-17 15:37:43 -07001316static struct class ucm_class = {
1317 .name = "infiniband_cm",
1318 .release = ib_ucm_release_class_dev
1319};
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001320
Sean Hefty07d357d2005-10-17 15:37:43 -07001321static ssize_t show_dev(struct class_device *class_dev, char *buf)
1322{
1323 struct ib_ucm_device *dev;
1324
1325 dev = container_of(class_dev, struct ib_ucm_device, class_dev);
1326 return print_dev_t(buf, dev->dev.dev);
1327}
1328static CLASS_DEVICE_ATTR(dev, S_IRUGO, show_dev, NULL);
1329
1330static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
1331{
1332 struct ib_ucm_device *dev;
1333
1334 dev = container_of(class_dev, struct ib_ucm_device, class_dev);
1335 return sprintf(buf, "%s\n", dev->ib_dev->name);
1336}
1337static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1338
1339static void ib_ucm_add_one(struct ib_device *device)
1340{
1341 struct ib_ucm_device *ucm_dev;
1342
1343 if (!device->alloc_ucontext)
1344 return;
1345
Roland Dreierde6eb662005-11-02 07:23:14 -08001346 ucm_dev = kzalloc(sizeof *ucm_dev, GFP_KERNEL);
Sean Hefty07d357d2005-10-17 15:37:43 -07001347 if (!ucm_dev)
1348 return;
1349
Sean Hefty07d357d2005-10-17 15:37:43 -07001350 ucm_dev->ib_dev = device;
1351
1352 ucm_dev->devnum = find_first_zero_bit(dev_map, IB_UCM_MAX_DEVICES);
1353 if (ucm_dev->devnum >= IB_UCM_MAX_DEVICES)
1354 goto err;
1355
1356 set_bit(ucm_dev->devnum, dev_map);
1357
1358 cdev_init(&ucm_dev->dev, &ucm_fops);
1359 ucm_dev->dev.owner = THIS_MODULE;
1360 kobject_set_name(&ucm_dev->dev.kobj, "ucm%d", ucm_dev->devnum);
1361 if (cdev_add(&ucm_dev->dev, IB_UCM_BASE_DEV + ucm_dev->devnum, 1))
1362 goto err;
1363
1364 ucm_dev->class_dev.class = &ucm_class;
1365 ucm_dev->class_dev.dev = device->dma_device;
1366 snprintf(ucm_dev->class_dev.class_id, BUS_ID_SIZE, "ucm%d",
1367 ucm_dev->devnum);
1368 if (class_device_register(&ucm_dev->class_dev))
1369 goto err_cdev;
1370
1371 if (class_device_create_file(&ucm_dev->class_dev,
1372 &class_device_attr_dev))
1373 goto err_class;
1374 if (class_device_create_file(&ucm_dev->class_dev,
1375 &class_device_attr_ibdev))
1376 goto err_class;
1377
1378 ib_set_client_data(device, &ucm_client, ucm_dev);
1379 return;
1380
1381err_class:
1382 class_device_unregister(&ucm_dev->class_dev);
1383err_cdev:
1384 cdev_del(&ucm_dev->dev);
1385 clear_bit(ucm_dev->devnum, dev_map);
1386err:
1387 kfree(ucm_dev);
1388 return;
1389}
1390
1391static void ib_ucm_remove_one(struct ib_device *device)
1392{
1393 struct ib_ucm_device *ucm_dev = ib_get_client_data(device, &ucm_client);
1394
1395 if (!ucm_dev)
1396 return;
1397
1398 class_device_unregister(&ucm_dev->class_dev);
1399}
1400
1401static ssize_t show_abi_version(struct class *class, char *buf)
1402{
1403 return sprintf(buf, "%d\n", IB_USER_CM_ABI_VERSION);
1404}
1405static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001406
1407static int __init ib_ucm_init(void)
1408{
Sean Hefty07d357d2005-10-17 15:37:43 -07001409 int ret;
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001410
Sean Hefty07d357d2005-10-17 15:37:43 -07001411 ret = register_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES,
1412 "infiniband_cm");
1413 if (ret) {
1414 printk(KERN_ERR "ucm: couldn't register device number\n");
1415 goto err;
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001416 }
1417
Sean Hefty07d357d2005-10-17 15:37:43 -07001418 ret = class_register(&ucm_class);
1419 if (ret) {
1420 printk(KERN_ERR "ucm: couldn't create class infiniband_cm\n");
1421 goto err_chrdev;
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001422 }
1423
Sean Hefty07d357d2005-10-17 15:37:43 -07001424 ret = class_create_file(&ucm_class, &class_attr_abi_version);
1425 if (ret) {
1426 printk(KERN_ERR "ucm: couldn't create abi_version attribute\n");
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001427 goto err_class;
1428 }
1429
Sean Hefty07d357d2005-10-17 15:37:43 -07001430 ret = ib_register_client(&ucm_client);
1431 if (ret) {
1432 printk(KERN_ERR "ucm: couldn't register client\n");
1433 goto err_class;
1434 }
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001435 return 0;
Sean Hefty07d357d2005-10-17 15:37:43 -07001436
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001437err_class:
Sean Hefty07d357d2005-10-17 15:37:43 -07001438 class_unregister(&ucm_class);
1439err_chrdev:
1440 unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
1441err:
1442 return ret;
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001443}
1444
1445static void __exit ib_ucm_cleanup(void)
1446{
Sean Hefty07d357d2005-10-17 15:37:43 -07001447 ib_unregister_client(&ucm_client);
1448 class_unregister(&ucm_class);
1449 unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
Roland Dreier5d7edb32005-10-24 10:53:25 -07001450 idr_destroy(&ctx_id_table);
Hal Rosenstocka5b74542005-07-27 11:45:44 -07001451}
1452
1453module_init(ib_ucm_init);
1454module_exit(ib_ucm_cleanup);