[openib-general] [PATCH] [CM] 1/6 core kernel changes to bind cm_id's to a device

Sean Hefty sean.hefty at intel.com
Tue Sep 6 16:41:34 PDT 2005


The following patch will bind communication identifiers to a
specific device.

Signed-off-by: Sean Hefty <sean.hefty at intel.com>

Index: core/cm.c
===================================================================
--- core/cm.c	(revision 3295)
+++ core/cm.c	(working copy)
@@ -365,9 +365,15 @@ static struct cm_id_private * cm_insert_
 		cur_cm_id_priv = rb_entry(parent, struct cm_id_private,
 					  service_node);
 		if ((cur_cm_id_priv->id.service_mask & service_id) ==
-		    (service_mask & cur_cm_id_priv->id.service_id))
-			return cm_id_priv;
-		if (service_id < cur_cm_id_priv->id.service_id)
+		    (service_mask & cur_cm_id_priv->id.service_id) &&
+		    (cm_id_priv->id.device == cur_cm_id_priv->id.device))
+			return cur_cm_id_priv;
+
+		if (cm_id_priv->id.device < cur_cm_id_priv->id.device)
+			link = &(*link)->rb_left;
+		else if (cm_id_priv->id.device > cur_cm_id_priv->id.device)
+			link = &(*link)->rb_right;
+		else if (service_id < cur_cm_id_priv->id.service_id)
 			link = &(*link)->rb_left;
 		else
 			link = &(*link)->rb_right;
@@ -377,7 +383,8 @@ static struct cm_id_private * cm_insert_
 	return NULL;
 }
 
-static struct cm_id_private * cm_find_listen(__be64 service_id)
+static struct cm_id_private * cm_find_listen(struct ib_device *device,
+					     __be64 service_id)
 {
 	struct rb_node *node = cm.listen_service_table.rb_node;
 	struct cm_id_private *cm_id_priv;
@@ -385,9 +392,15 @@ static struct cm_id_private * cm_find_li
 	while (node) {
 		cm_id_priv = rb_entry(node, struct cm_id_private, service_node);
 		if ((cm_id_priv->id.service_mask & service_id) ==
-		    (cm_id_priv->id.service_mask & cm_id_priv->id.service_id))
+		     cm_id_priv->id.service_id &&
+		    (cm_id_priv->id.device == device))
 			return cm_id_priv;
-		if (service_id < cm_id_priv->id.service_id)
+
+		if (device < cm_id_priv->id.device)
+			node = node->rb_left;
+		else if (device > cm_id_priv->id.device)
+			node = node->rb_right;
+		else if (service_id < cm_id_priv->id.service_id)
 			node = node->rb_left;
 		else
 			node = node->rb_right;
@@ -522,7 +535,8 @@ static void cm_reject_sidr_req(struct cm
 	ib_send_cm_sidr_rep(&cm_id_priv->id, &param);
 }
 
-struct ib_cm_id *ib_create_cm_id(ib_cm_handler cm_handler,
+struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
+				 ib_cm_handler cm_handler,
 				 void *context)
 {
 	struct cm_id_private *cm_id_priv;
@@ -534,6 +548,7 @@ struct ib_cm_id *ib_create_cm_id(ib_cm_h
 
 	memset(cm_id_priv, 0, sizeof *cm_id_priv);
 	cm_id_priv->id.state = IB_CM_IDLE;
+	cm_id_priv->id.device = device;
 	cm_id_priv->id.cm_handler = cm_handler;
 	cm_id_priv->id.context = context;
 	ret = cm_alloc_id(cm_id_priv);
@@ -1045,7 +1060,6 @@ static void cm_format_req_event(struct c
 	req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
 	param = &work->cm_event.param.req_rcvd;
 	param->listen_id = listen_id;
-	param->device = cm_id_priv->av.port->mad_agent->device;
 	param->port = cm_id_priv->av.port->port_num;
 	param->primary_path = &work->path[0];
 	if (req_msg->alt_local_lid)
@@ -1224,7 +1238,8 @@ static struct cm_id_private * cm_match_r
 	}
 
 	/* Find matching listen request. */
-	listen_cm_id_priv = cm_find_listen(req_msg->service_id);
+	listen_cm_id_priv = cm_find_listen(cm_id_priv->id.device,
+					   req_msg->service_id);
 	if (!listen_cm_id_priv) {
 		spin_unlock_irqrestore(&cm.lock, flags);
 		cm_issue_rej(work->port, work->mad_recv_wc,
@@ -1252,7 +1267,7 @@ static int cm_req_handler(struct cm_work
 
 	req_msg = (struct cm_req_msg *)work->mad_recv_wc->recv_buf.mad;
 
-	cm_id = ib_create_cm_id(NULL, NULL);
+	cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
 	if (IS_ERR(cm_id))
 		return PTR_ERR(cm_id);
 
@@ -2626,7 +2641,6 @@ static void cm_format_sidr_req_event(str
 	param = &work->cm_event.param.sidr_req_rcvd;
 	param->pkey = __be16_to_cpu(sidr_req_msg->pkey);
 	param->listen_id = listen_id;
-	param->device = work->port->mad_agent->device;
 	param->port = work->port->port_num;
 	work->cm_event.private_data = &sidr_req_msg->private_data;
 }
@@ -2639,7 +2653,7 @@ static int cm_sidr_req_handler(struct cm
 	struct ib_wc *wc;
 	unsigned long flags;
 
-	cm_id = ib_create_cm_id(NULL, NULL);
+	cm_id = ib_create_cm_id(work->port->cm_dev->device, NULL, NULL);
 	if (IS_ERR(cm_id))
 		return PTR_ERR(cm_id);
 	cm_id_priv = container_of(cm_id, struct cm_id_private, id);
@@ -2663,7 +2677,8 @@ static int cm_sidr_req_handler(struct cm
 		spin_unlock_irqrestore(&cm.lock, flags);
 		goto out; /* Duplicate message. */
 	}
-	cur_cm_id_priv = cm_find_listen(sidr_req_msg->service_id);
+	cur_cm_id_priv = cm_find_listen(cm_id->device,
+					sidr_req_msg->service_id);
 	if (!cur_cm_id_priv) {
 		rb_erase(&cm_id_priv->sidr_id_node, &cm.remote_sidr_table);
 		spin_unlock_irqrestore(&cm.lock, flags);
Index: core/ucm.c
===================================================================
--- core/ucm.c	(revision 3295)
+++ core/ucm.c	(working copy)
@@ -70,22 +70,40 @@ enum {
 			printk(KERN_DEBUG PFX format, ## arg); \
 	} while (0)
 
-static struct semaphore ctx_id_mutex;
-static struct idr       ctx_id_table;
+static void ib_ucm_add_one(struct ib_device *device);
+static void ib_ucm_remove_one(struct ib_device *device);
+
+static struct ib_client ucm_client = {
+	.name   = "ucm",
+	.add    = ib_ucm_add_one,
+	.remove = ib_ucm_remove_one
+};
+
+static struct ib_ucm {
+	struct semaphore mutex;
+	struct idr       ctx_id_table;
+	struct list_head device_list;
+} ucm;
+
+struct ucm_device {
+	struct list_head list;
+	struct ib_device *device;
+	__be64 guid;
+};
 
 static struct ib_ucm_context *ib_ucm_ctx_get(struct ib_ucm_file *file, int id)
 {
 	struct ib_ucm_context *ctx;
 
-	down(&ctx_id_mutex);
-	ctx = idr_find(&ctx_id_table, id);
+	down(&ucm.mutex);
+	ctx = idr_find(&ucm.ctx_id_table, id);
 	if (!ctx)
 		ctx = ERR_PTR(-ENOENT);
 	else if (ctx->file != file)
 		ctx = ERR_PTR(-EINVAL);
 	else
 		atomic_inc(&ctx->ref);
-	up(&ctx_id_mutex);
+	up(&ucm.mutex);
 
 	return ctx;
 }
@@ -139,13 +157,13 @@ static struct ib_ucm_context *ib_ucm_ctx
 	INIT_LIST_HEAD(&ctx->events);
 
 	do {
-		result = idr_pre_get(&ctx_id_table, GFP_KERNEL);
+		result = idr_pre_get(&ucm.ctx_id_table, GFP_KERNEL);
 		if (!result)
 			goto error;
 
-		down(&ctx_id_mutex);
-		result = idr_get_new(&ctx_id_table, ctx, &ctx->id);
-		up(&ctx_id_mutex);
+		down(&ucm.mutex);
+		result = idr_get_new(&ucm.ctx_id_table, ctx, &ctx->id);
+		up(&ucm.mutex);
 	} while (result == -EAGAIN);
 
 	if (result)
@@ -209,6 +227,7 @@ static void ib_ucm_event_req_get(struct 
 	ureq->retry_count                = kreq->retry_count;
 	ureq->rnr_retry_count            = kreq->rnr_retry_count;
 	ureq->srq                        = kreq->srq;
+	ureq->port			 = kreq->port;
 
 	ib_ucm_event_path_get(&ureq->primary_path, kreq->primary_path);
 	ib_ucm_event_path_get(&ureq->alternate_path, kreq->alternate_path);
@@ -295,6 +314,8 @@ static int ib_ucm_event_process(struct i
 	case IB_CM_SIDR_REQ_RECEIVED:
 		uvt->resp.u.sidr_req_resp.pkey = 
 					evt->param.sidr_req_rcvd.pkey;
+		uvt->resp.u.sidr_req_resp.port = 
+					evt->param.sidr_req_rcvd.port;
 		uvt->data_len = IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE;
 		break;
 	case IB_CM_SIDR_REP_RECEIVED:
@@ -471,6 +492,16 @@ done:
 	return result;
 }
 
+static struct ib_device *ib_ucm_get_device(__be64 guid)
+{
+	struct ucm_device *ucm_dev;
+
+	list_for_each_entry(ucm_dev, &ucm.device_list, list) {
+		if (ucm_dev->guid == guid)
+			return ucm_dev->device;
+	}
+	return NULL;
+}
 
 static ssize_t ib_ucm_create_id(struct ib_ucm_file *file,
 				const char __user *inbuf,
@@ -479,6 +510,7 @@ static ssize_t ib_ucm_create_id(struct i
 	struct ib_ucm_create_id cmd;
 	struct ib_ucm_create_id_resp resp;
 	struct ib_ucm_context *ctx;
+	struct ib_device *device;
 	int result;
 
 	if (out_len < sizeof(resp))
@@ -489,12 +521,19 @@ static ssize_t ib_ucm_create_id(struct i
 
 	down(&file->mutex);
 	ctx = ib_ucm_ctx_alloc(file);
-	up(&file->mutex);
-	if (!ctx)
+	if (!ctx) {
+		up(&file->mutex);
 		return -ENOMEM;
+	}
+	device = ib_ucm_get_device(cmd.device_guid);
+	up(&file->mutex);
+	if (!device) {
+		result = -EINVAL;
+		goto err;
+	}
 
 	ctx->uid = cmd.uid;
-	ctx->cm_id = ib_create_cm_id(ib_ucm_event_handler, ctx);
+	ctx->cm_id = ib_create_cm_id(device, ib_ucm_event_handler, ctx);
 	if (IS_ERR(ctx->cm_id)) {
 		result = PTR_ERR(ctx->cm_id);
 		goto err;
@@ -510,13 +549,15 @@ static ssize_t ib_ucm_create_id(struct i
 	return 0;
 
 err:
-	down(&ctx_id_mutex);
-	idr_remove(&ctx_id_table, ctx->id);
-	up(&ctx_id_mutex);
+	down(&ucm.mutex);
+	idr_remove(&ucm.ctx_id_table, ctx->id);
+	up(&ucm.mutex);
 
-	if (!IS_ERR(ctx->cm_id))
+	if (ctx->cm_id && !IS_ERR(ctx->cm_id))
 		ib_destroy_cm_id(ctx->cm_id);
 
+	ib_ucm_cleanup_events(ctx);
+
 	kfree(ctx);
 	return result;
 }
@@ -536,15 +577,15 @@ static ssize_t ib_ucm_destroy_id(struct 
 	if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
 		return -EFAULT;
 
-	down(&ctx_id_mutex);
-	ctx = idr_find(&ctx_id_table, cmd.id);
+	down(&ucm.mutex);
+	ctx = idr_find(&ucm.ctx_id_table, cmd.id);
 	if (!ctx)
 		ctx = ERR_PTR(-ENOENT);
 	else if (ctx->file != file)
 		ctx = ERR_PTR(-EINVAL);
 	else
-		idr_remove(&ctx_id_table, ctx->id);
-	up(&ctx_id_mutex);
+		idr_remove(&ucm.ctx_id_table, ctx->id);
+	up(&ucm.mutex);
 
 	if (IS_ERR(ctx))
 		return PTR_ERR(ctx);
@@ -1248,9 +1289,9 @@ static int ib_ucm_close(struct inode *in
 				 struct ib_ucm_context, file_list);
 		up(&file->mutex);
 
-		down(&ctx_id_mutex);
-		idr_remove(&ctx_id_table, ctx->id);
-		up(&ctx_id_mutex);
+		down(&ucm.mutex);
+		idr_remove(&ucm.ctx_id_table, ctx->id);
+		up(&ucm.mutex);
 
 		ib_destroy_cm_id(ctx->cm_id);
 		ib_ucm_cleanup_events(ctx);
@@ -1263,6 +1304,60 @@ static int ib_ucm_close(struct inode *in
 	return 0;
 }
 
+static __be64 ib_ucm_get_ca_guid(struct ib_device *device)
+{
+	struct ib_device_attr *device_attr;
+	__be64 guid;
+	int ret;
+
+	device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
+	if (!device_attr)
+		return 0;
+
+	ret = ib_query_device(device, device_attr);
+	guid = ret ? 0 : device_attr->node_guid;
+	kfree(device_attr);
+	return guid;
+}
+
+static void ib_ucm_add_one(struct ib_device *device)
+{
+	struct ucm_device *ucm_dev;
+
+	ucm_dev = kmalloc(sizeof(*ucm_dev), GFP_KERNEL);
+	if (!ucm_dev)
+		return;
+
+	ucm_dev->device = device;
+	ucm_dev->guid = ib_ucm_get_ca_guid(device);
+	if (!ucm_dev->guid)
+		goto error;
+
+	ib_set_client_data(device, &ucm_client, ucm_dev);
+
+	down(&ucm.mutex);
+	list_add_tail(&ucm_dev->list, &ucm.device_list);
+	up(&ucm.mutex);
+	return;
+
+error:
+	kfree(ucm_dev);
+}
+
+static void ib_ucm_remove_one(struct ib_device *device)
+{
+	struct ucm_device *ucm_dev;
+
+	ucm_dev = ib_get_client_data(device, &ucm_client);
+	if (!ucm_dev)
+		return;
+
+	down(&ucm.mutex);
+	list_del(&ucm_dev->list);
+	up(&ucm.mutex);
+	kfree(ucm_dev);
+}
+
 static struct file_operations ib_ucm_fops = {
 	.owner 	 = THIS_MODULE,
 	.open 	 = ib_ucm_open,
@@ -1271,14 +1366,22 @@ static struct file_operations ib_ucm_fop
 	.poll    = ib_ucm_poll,
 };
 
-
 static struct class *ib_ucm_class;
-static struct cdev	  ib_ucm_cdev;
+static struct cdev  ib_ucm_cdev;
 
 static int __init ib_ucm_init(void)
 {
 	int result;
 
+	memset(&ucm, 0, sizeof ucm);
+	INIT_LIST_HEAD(&ucm.device_list);
+	idr_init(&ucm.ctx_id_table);
+	init_MUTEX(&ucm.mutex);
+
+	result = ib_register_client(&ucm_client);
+	if (result)
+		goto err_reg;
+
 	result = register_chrdev_region(IB_UCM_DEV, 1, "infiniband_cm");
 	if (result) {
 		ucm_dbg("Error <%d> registering dev\n", result);
@@ -1302,15 +1405,14 @@ static int __init ib_ucm_init(void)
 
 	class_device_create(ib_ucm_class, IB_UCM_DEV, NULL, "ucm");
 
-	idr_init(&ctx_id_table);
-	init_MUTEX(&ctx_id_mutex);
-
 	return 0;
 err_class:
 	cdev_del(&ib_ucm_cdev);
 err_cdev:
 	unregister_chrdev_region(IB_UCM_DEV, 1);
 err_chr:
+	ib_unregister_client(&ucm_client);
+err_reg:
 	return result;
 }
 
@@ -1320,6 +1422,7 @@ static void __exit ib_ucm_cleanup(void)
 	class_destroy(ib_ucm_class);
 	cdev_del(&ib_ucm_cdev);
 	unregister_chrdev_region(IB_UCM_DEV, 1);
+	ib_unregister_client(&ucm_client);
 }
 
 module_init(ib_ucm_init);
Index: include/rdma/ib_cm.h
===================================================================
--- include/rdma/ib_cm.h	(revision 3295)
+++ include/rdma/ib_cm.h	(working copy)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2004 Intel Corporation.  All rights reserved.
+ * Copyright (c) 2004, 2005 Intel Corporation.  All rights reserved.
  * Copyright (c) 2004 Topspin Corporation.  All rights reserved.
  * Copyright (c) 2004 Voltaire Corporation.  All rights reserved.
  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
@@ -109,7 +109,6 @@ struct ib_cm_id;
 
 struct ib_cm_req_event_param {
 	struct ib_cm_id		*listen_id;
-	struct ib_device	*device;
 	u8			port;
 
 	struct ib_sa_path_rec	*primary_path;
@@ -220,7 +219,6 @@ struct ib_cm_apr_event_param {
 
 struct ib_cm_sidr_req_event_param {
 	struct ib_cm_id		*listen_id;
-	struct ib_device	*device;
 	u8			port;
 	u16			pkey;
 };
@@ -284,6 +282,7 @@ typedef int (*ib_cm_handler)(struct ib_c
 struct ib_cm_id {
 	ib_cm_handler		cm_handler;
 	void			*context;
+	struct ib_device	*device;
 	__be64			service_id;
 	__be64			service_mask;
 	enum ib_cm_state	state;		/* internal CM/debug use */
@@ -294,6 +293,8 @@ struct ib_cm_id {
 
 /**
  * ib_create_cm_id - Allocate a communication identifier.
+ * @device: Device associated with the cm_id.  All related communication will
+ * be associated with the specified device.
  * @cm_handler: Callback invoked to notify the user of CM events.
  * @context: User specified context associated with the communication
  *   identifier.
@@ -301,7 +302,8 @@ struct ib_cm_id {
  * Communication identifiers are used to track connection states, service
  * ID resolution requests, and listen requests.
  */
-struct ib_cm_id *ib_create_cm_id(ib_cm_handler cm_handler,
+struct ib_cm_id *ib_create_cm_id(struct ib_device *device,
+				 ib_cm_handler cm_handler,
 				 void *context);
 
 /**
Index: include/rdma/ib_user_cm.h
===================================================================
--- include/rdma/ib_user_cm.h	(revision 3295)
+++ include/rdma/ib_user_cm.h	(working copy)
@@ -38,7 +38,7 @@
 
 #include <linux/types.h>
 
-#define IB_USER_CM_ABI_VERSION 2
+#define IB_USER_CM_ABI_VERSION 3
 
 enum {
 	IB_USER_CM_CMD_CREATE_ID,
@@ -74,6 +74,7 @@ struct ib_ucm_cmd_hdr {
 
 struct ib_ucm_create_id {
 	__u64 uid;
+	__be64 device_guid;
 	__u64 response;
 };
 
@@ -299,8 +300,6 @@ struct ib_ucm_event_get {
 };
 
 struct ib_ucm_req_event_resp {
-	/* device */
-	/* port */
 	struct ib_ucm_path_rec primary_path;
 	struct ib_ucm_path_rec alternate_path;
 	__be64                 remote_ca_guid;
@@ -316,6 +315,7 @@ struct ib_ucm_req_event_resp {
 	__u8  retry_count;
 	__u8  rnr_retry_count;
 	__u8  srq;
+	__u8  port;
 };
 
 struct ib_ucm_rep_event_resp {
@@ -353,10 +353,9 @@ struct ib_ucm_apr_event_resp {
 };
 
 struct ib_ucm_sidr_req_event_resp {
-	/* device */
-	/* port */
 	__u16 pkey;
-	__u8  reserved[2];
+	__u8  port;
+	__u8  reserved;
 };
 
 struct ib_ucm_sidr_rep_event_resp {






More information about the general mailing list