[openib-general] [PATCH] Start creating "user context" infrastructure

Roland Dreier roland at topspin.com
Thu Jan 20 16:58:17 PST 2005


This patch is one tiny step towards allowing userspace to create
various resources.  It doesn't really do anything useful but I'd like
to get feedback on these API changes early.

Any suggestions/objections?

Thanks,
  Roland

Index: infiniband/include/ib_verbs.h
===================================================================
--- infiniband/include/ib_verbs.h	(revision 1572)
+++ infiniband/include/ib_verbs.h	(working copy)
@@ -82,6 +82,8 @@ enum ib_atomic_cap {
 	IB_ATOMIC_GLOB
 };
 
+struct ib_ucontext;
+
 struct ib_device_attr {
 	u64			fw_ver;
 	u64			node_guid;
@@ -626,8 +628,9 @@ struct ib_fmr_attr {
 };
 
 struct ib_pd {
-	struct ib_device *device;
-	atomic_t          usecnt; /* count all resources */
+	struct ib_device       *device;
+	struct ib_ucontext     *context;
+	atomic_t          	usecnt; /* count all resources */
 };
 
 struct ib_ah {
@@ -638,12 +641,13 @@ struct ib_ah {
 typedef void (*ib_comp_handler)(struct ib_cq *cq, void *cq_context);
 
 struct ib_cq {
-	struct ib_device *device;
-	ib_comp_handler   comp_handler;
-	void             (*event_handler)(struct ib_event *, void *);
-	void *            cq_context;
-	int               cqe;
-	atomic_t          usecnt; /* count number of work queues */
+	struct ib_device       *device;
+	struct ib_ucontext     *context;
+	ib_comp_handler   	comp_handler;
+	void                  (*event_handler)(struct ib_event *, void *);
+	void *            	cq_context;
+	int               	cqe;
+	atomic_t          	usecnt; /* count number of work queues */
 };
 
 struct ib_srq {
@@ -744,7 +748,10 @@ struct ib_device {
 	int		           (*modify_port)(struct ib_device *device,
 						  u8 port_num, int port_modify_mask,
 						  struct ib_port_modify *port_modify);
-	struct ib_pd *             (*alloc_pd)(struct ib_device *device);
+	struct ib_ucontext *       (*alloc_ucontext)(struct ib_device *device);
+	int                        (*dealloc_ucontext)(struct ib_ucontext *context);
+	struct ib_pd *             (*alloc_pd)(struct ib_device *device,
+					       struct ib_ucontext *context);
 	int                        (*dealloc_pd)(struct ib_pd *pd);
 	struct ib_ah *             (*create_ah)(struct ib_pd *pd,
 						struct ib_ah_attr *ah_attr);
@@ -770,7 +777,8 @@ struct ib_device {
 						struct ib_recv_wr *recv_wr,
 						struct ib_recv_wr **bad_recv_wr);
 	struct ib_cq *             (*create_cq)(struct ib_device *device,
-						int cqe);
+						int cqe,
+						struct ib_ucontext *context);
 	int                        (*destroy_cq)(struct ib_cq *cq);
 	int                        (*resize_cq)(struct ib_cq *cq, int *cqe);
 	int                        (*poll_cq)(struct ib_cq *cq, int num_entries,
@@ -823,6 +831,8 @@ struct ib_device {
 						  struct ib_grh *in_grh,
 						  struct ib_mad *in_mad,
 						  struct ib_mad *out_mad);
+	int                        (*mmap)(struct ib_ucontext *context,
+					   struct vm_area_struct *vma);
 
 	struct class_device          class_dev;
 	struct kobject               ports_parent;
Index: infiniband/core/uverbs_main.c
===================================================================
--- infiniband/core/uverbs_main.c	(revision 1573)
+++ infiniband/core/uverbs_main.c	(working copy)
@@ -271,13 +271,11 @@ static ssize_t ib_uverbs_write(struct fi
 	}
 }
 
-static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct * vma)
+static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
 {
-	/*
-	 * Pass through to the low-level driver if it supports mmap --
-	 * otherwise fail as if we have no mmap method.
-	 */
-	return -ENODEV;
+	struct ib_uverbs_file *file = filp->private_data;
+
+	return file->device->ib_dev->mmap(file->ucontext, vma);
 }
 
 static int ib_uverbs_open(struct inode *inode, struct file *filp)
@@ -297,9 +295,15 @@ static int ib_uverbs_open(struct inode *
 	file->device = dev;
 	kref_init(&file->ref);
 
+	file->ucontext = dev->ib_dev->alloc_ucontext(dev->ib_dev);
+	if (IS_ERR(file->ucontext)) {
+		ret = PTR_ERR(file->ucontext);
+		goto err;
+	}
+
 	ret = ib_uverbs_event_init(&file->async_file, file);
 	if (ret)
-		goto err;
+		goto err_context;
 
 	file->async_file.is_async = 1;
 
@@ -329,6 +333,9 @@ err_async:
 
 	ib_uverbs_event_release(&file->async_file);
 
+err_context:
+	dev->ib_dev->dealloc_ucontext(file->ucontext);
+
 err:
 	kref_put(&file->ref, ib_uverbs_release_file);
 
@@ -355,6 +362,13 @@ static int ib_uverbs_close(struct inode 
 static struct file_operations uverbs_fops = {
 	.owner 	 = THIS_MODULE,
 	.write 	 = ib_uverbs_write,
+	.open 	 = ib_uverbs_open,
+	.release = ib_uverbs_close
+};
+
+static struct file_operations uverbs_mmap_fops = {
+	.owner 	 = THIS_MODULE,
+	.write 	 = ib_uverbs_write,
 	.mmap    = ib_uverbs_mmap,
 	.open 	 = ib_uverbs_open,
 	.release = ib_uverbs_close
@@ -409,6 +423,9 @@ static void ib_uverbs_add_one(struct ib_
 {
 	struct ib_uverbs_device *uverbs_dev;
 
+	if (!device->alloc_ucontext)
+		return;
+
 	uverbs_dev = kmalloc(sizeof *uverbs_dev, GFP_KERNEL);
 	if (!uverbs_dev)
 		return;
@@ -427,7 +444,10 @@ static void ib_uverbs_add_one(struct ib_
 	uverbs_dev->ib_dev   = device;
 	uverbs_dev->num_comp = 1;
 
-	cdev_init(&uverbs_dev->dev, &uverbs_fops);
+	if (device->mmap)
+		cdev_init(&uverbs_dev->dev, &uverbs_mmap_fops);
+	else
+		cdev_init(&uverbs_dev->dev, &uverbs_fops);
 	uverbs_dev->dev.owner = THIS_MODULE;
 	kobject_set_name(&uverbs_dev->dev.kobj, "uverbs%d", uverbs_dev->devnum);
 	if (cdev_add(&uverbs_dev->dev, base_dev + uverbs_dev->devnum, 1))
Index: infiniband/core/uverbs.h
===================================================================
--- infiniband/core/uverbs.h	(revision 1573)
+++ infiniband/core/uverbs.h	(working copy)
@@ -64,6 +64,7 @@ struct ib_uverbs_event_file {
 struct ib_uverbs_file {
 	struct kref                 ref;
 	struct ib_uverbs_device    *device;
+	struct ib_ucontext         *ucontext;
 	struct ib_event_handler     event_handler;
 	struct ib_uverbs_event_file async_file; 
 	struct ib_uverbs_event_file comp_file[1]; 
Index: infiniband/core/verbs.c
===================================================================
--- infiniband/core/verbs.c	(revision 1572)
+++ infiniband/core/verbs.c	(working copy)
@@ -47,7 +47,7 @@ struct ib_pd *ib_alloc_pd(struct ib_devi
 {
 	struct ib_pd *pd;
 
-	pd = device->alloc_pd(device);
+	pd = device->alloc_pd(device, NULL);
 
 	if (!IS_ERR(pd)) {
 		pd->device = device;
@@ -197,7 +197,7 @@ struct ib_cq *ib_create_cq(struct ib_dev
 {
 	struct ib_cq *cq;
 
-	cq = device->create_cq(device, cqe);
+	cq = device->create_cq(device, cqe, NULL);
 
 	if (!IS_ERR(cq)) {
 		cq->device        = device;
Index: infiniband/hw/mthca/mthca_provider.c
===================================================================
--- infiniband/hw/mthca/mthca_provider.c	(revision 1572)
+++ infiniband/hw/mthca/mthca_provider.c	(working copy)
@@ -266,7 +266,8 @@ static int mthca_query_gid(struct ib_dev
 	return err;
 }
 
-static struct ib_pd *mthca_alloc_pd(struct ib_device *ibdev)
+static struct ib_pd *mthca_alloc_pd(struct ib_device *ibdev,
+				    struct ib_ucontext *context)
 {
 	struct mthca_pd *pd;
 	int err;
@@ -391,7 +392,8 @@ static int mthca_destroy_qp(struct ib_qp
 	return 0;
 }
 
-static struct ib_cq *mthca_create_cq(struct ib_device *ibdev, int entries)
+static struct ib_cq *mthca_create_cq(struct ib_device *ibdev, int entries,
+				     struct ib_ucontext *context)
 {
 	struct mthca_cq *cq;
 	int nent;



More information about the general mailing list