]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/infiniband/core/user_mad.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6-omap-h63xx.git] / drivers / infiniband / core / user_mad.c
index aee29139368c75b9b32b50141a1ab345126c3d60..840ede9ae965e2cfafd9766a91292e60910b2f1c 100644 (file)
@@ -2,6 +2,7 @@
  * Copyright (c) 2004 Topspin Communications.  All rights reserved.
  * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
+ * Copyright (c) 2008 Cisco. All rights reserved.
  *
  * This software is available to you under a choice of one of two
  * licenses.  You may choose to be licensed under the terms of the GNU
 #include <linux/cdev.h>
 #include <linux/dma-mapping.h>
 #include <linux/poll.h>
-#include <linux/rwsem.h>
+#include <linux/mutex.h>
 #include <linux/kref.h>
+#include <linux/compat.h>
+#include <linux/semaphore.h>
 
 #include <asm/uaccess.h>
-#include <asm/semaphore.h>
 
 #include <rdma/ib_mad.h>
 #include <rdma/ib_user_mad.h>
@@ -86,14 +88,14 @@ enum {
  */
 
 struct ib_umad_port {
-       struct cdev           *dev;
-       struct class_device   *class_dev;
+       struct cdev           *cdev;
+       struct device         *dev;
 
-       struct cdev           *sm_dev;
-       struct class_device   *sm_class_dev;
+       struct cdev           *sm_cdev;
+       struct device         *sm_dev;
        struct semaphore       sm_sem;
 
-       struct rw_semaphore    mutex;
+       struct mutex           file_mutex;
        struct list_head       file_list;
 
        struct ib_device      *ib_dev;
@@ -109,11 +111,11 @@ struct ib_umad_device {
 };
 
 struct ib_umad_file {
+       struct mutex            mutex;
        struct ib_umad_port    *port;
        struct list_head        recv_list;
        struct list_head        send_list;
        struct list_head        port_list;
-       spinlock_t              recv_lock;
        spinlock_t              send_lock;
        wait_queue_head_t       recv_wait;
        struct ib_mad_agent    *agent[IB_UMAD_MAX_AGENTS];
@@ -155,7 +157,7 @@ static int hdr_size(struct ib_umad_file *file)
                sizeof (struct ib_user_mad_hdr_old);
 }
 
-/* caller must hold port->mutex at least for reading */
+/* caller must hold file->mutex */
 static struct ib_mad_agent *__get_agent(struct ib_umad_file *file, int id)
 {
        return file->agents_dead ? NULL : file->agent[id];
@@ -167,32 +169,30 @@ static int queue_packet(struct ib_umad_file *file,
 {
        int ret = 1;
 
-       down_read(&file->port->mutex);
+       mutex_lock(&file->mutex);
 
        for (packet->mad.hdr.id = 0;
             packet->mad.hdr.id < IB_UMAD_MAX_AGENTS;
             packet->mad.hdr.id++)
                if (agent == __get_agent(file, packet->mad.hdr.id)) {
-                       spin_lock_irq(&file->recv_lock);
                        list_add_tail(&packet->list, &file->recv_list);
-                       spin_unlock_irq(&file->recv_lock);
                        wake_up_interruptible(&file->recv_wait);
                        ret = 0;
                        break;
                }
 
-       up_read(&file->port->mutex);
+       mutex_unlock(&file->mutex);
 
        return ret;
 }
 
 static void dequeue_send(struct ib_umad_file *file,
                         struct ib_umad_packet *packet)
- {
+{
        spin_lock_irq(&file->send_lock);
        list_del(&packet->list);
        spin_unlock_irq(&file->send_lock);
- }
+}
 
 static void send_handler(struct ib_mad_agent *agent,
                         struct ib_mad_send_wc *send_wc)
@@ -340,10 +340,10 @@ static ssize_t ib_umad_read(struct file *filp, char __user *buf,
        if (count < hdr_size(file))
                return -EINVAL;
 
-       spin_lock_irq(&file->recv_lock);
+       mutex_lock(&file->mutex);
 
        while (list_empty(&file->recv_list)) {
-               spin_unlock_irq(&file->recv_lock);
+               mutex_unlock(&file->mutex);
 
                if (filp->f_flags & O_NONBLOCK)
                        return -EAGAIN;
@@ -352,13 +352,13 @@ static ssize_t ib_umad_read(struct file *filp, char __user *buf,
                                             !list_empty(&file->recv_list)))
                        return -ERESTARTSYS;
 
-               spin_lock_irq(&file->recv_lock);
+               mutex_lock(&file->mutex);
        }
 
        packet = list_entry(file->recv_list.next, struct ib_umad_packet, list);
        list_del(&packet->list);
 
-       spin_unlock_irq(&file->recv_lock);
+       mutex_unlock(&file->mutex);
 
        if (packet->recv_wc)
                ret = copy_recv_mad(file, buf, packet, count);
@@ -367,9 +367,9 @@ static ssize_t ib_umad_read(struct file *filp, char __user *buf,
 
        if (ret < 0) {
                /* Requeue packet */
-               spin_lock_irq(&file->recv_lock);
+               mutex_lock(&file->mutex);
                list_add(&packet->list, &file->recv_list);
-               spin_unlock_irq(&file->recv_lock);
+               mutex_unlock(&file->mutex);
        } else {
                if (packet->recv_wc)
                        ib_free_recv_mad(packet->recv_wc);
@@ -480,7 +480,7 @@ static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
                goto err;
        }
 
-       down_read(&file->port->mutex);
+       mutex_lock(&file->mutex);
 
        agent = __get_agent(file, packet->mad.hdr.id);
        if (!agent) {
@@ -576,7 +576,7 @@ static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
        if (ret)
                goto err_send;
 
-       up_read(&file->port->mutex);
+       mutex_unlock(&file->mutex);
        return count;
 
 err_send:
@@ -586,7 +586,7 @@ err_msg:
 err_ah:
        ib_destroy_ah(ah);
 err_up:
-       up_read(&file->port->mutex);
+       mutex_unlock(&file->mutex);
 err:
        kfree(packet);
        return ret;
@@ -607,22 +607,24 @@ static unsigned int ib_umad_poll(struct file *filp, struct poll_table_struct *wa
        return mask;
 }
 
-static int ib_umad_reg_agent(struct ib_umad_file *file, unsigned long arg)
+static int ib_umad_reg_agent(struct ib_umad_file *file, void __user *arg,
+                            int compat_method_mask)
 {
        struct ib_user_mad_reg_req ureq;
        struct ib_mad_reg_req req;
-       struct ib_mad_agent *agent;
+       struct ib_mad_agent *agent = NULL;
        int agent_id;
        int ret;
 
-       down_write(&file->port->mutex);
+       mutex_lock(&file->port->file_mutex);
+       mutex_lock(&file->mutex);
 
        if (!file->port->ib_dev) {
                ret = -EPIPE;
                goto out;
        }
 
-       if (copy_from_user(&ureq, (void __user *) arg, sizeof ureq)) {
+       if (copy_from_user(&ureq, arg, sizeof ureq)) {
                ret = -EFAULT;
                goto out;
        }
@@ -643,8 +645,18 @@ found:
        if (ureq.mgmt_class) {
                req.mgmt_class         = ureq.mgmt_class;
                req.mgmt_class_version = ureq.mgmt_class_version;
-               memcpy(req.method_mask, ureq.method_mask, sizeof req.method_mask);
-               memcpy(req.oui,         ureq.oui,         sizeof req.oui);
+               memcpy(req.oui, ureq.oui, sizeof req.oui);
+
+               if (compat_method_mask) {
+                       u32 *umm = (u32 *) ureq.method_mask;
+                       int i;
+
+                       for (i = 0; i < BITS_TO_LONGS(IB_MGMT_MAX_METHODS); ++i)
+                               req.method_mask[i] =
+                                       umm[i * 2] | ((u64) umm[i * 2 + 1] << 32);
+               } else
+                       memcpy(req.method_mask, ureq.method_mask,
+                              sizeof req.method_mask);
        }
 
        agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num,
@@ -654,13 +666,13 @@ found:
                                      send_handler, recv_handler, file);
        if (IS_ERR(agent)) {
                ret = PTR_ERR(agent);
+               agent = NULL;
                goto out;
        }
 
        if (put_user(agent_id,
                     (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) {
                ret = -EFAULT;
-               ib_unregister_mad_agent(agent);
                goto out;
        }
 
@@ -678,20 +690,27 @@ found:
        ret = 0;
 
 out:
-       up_write(&file->port->mutex);
+       mutex_unlock(&file->mutex);
+
+       if (ret && agent)
+               ib_unregister_mad_agent(agent);
+
+       mutex_unlock(&file->port->file_mutex);
+
        return ret;
 }
 
-static int ib_umad_unreg_agent(struct ib_umad_file *file, unsigned long arg)
+static int ib_umad_unreg_agent(struct ib_umad_file *file, u32 __user *arg)
 {
        struct ib_mad_agent *agent = NULL;
        u32 id;
        int ret = 0;
 
-       if (get_user(id, (u32 __user *) arg))
+       if (get_user(id, arg))
                return -EFAULT;
 
-       down_write(&file->port->mutex);
+       mutex_lock(&file->port->file_mutex);
+       mutex_lock(&file->mutex);
 
        if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !__get_agent(file, id)) {
                ret = -EINVAL;
@@ -702,11 +721,13 @@ static int ib_umad_unreg_agent(struct ib_umad_file *file, unsigned long arg)
        file->agent[id] = NULL;
 
 out:
-       up_write(&file->port->mutex);
+       mutex_unlock(&file->mutex);
 
        if (agent)
                ib_unregister_mad_agent(agent);
 
+       mutex_unlock(&file->port->file_mutex);
+
        return ret;
 }
 
@@ -714,12 +735,12 @@ static long ib_umad_enable_pkey(struct ib_umad_file *file)
 {
        int ret = 0;
 
-       down_write(&file->port->mutex);
+       mutex_lock(&file->mutex);
        if (file->already_used)
                ret = -EINVAL;
        else
                file->use_pkey_index = 1;
-       up_write(&file->port->mutex);
+       mutex_unlock(&file->mutex);
 
        return ret;
 }
@@ -729,15 +750,32 @@ static long ib_umad_ioctl(struct file *filp, unsigned int cmd,
 {
        switch (cmd) {
        case IB_USER_MAD_REGISTER_AGENT:
-               return ib_umad_reg_agent(filp->private_data, arg);
+               return ib_umad_reg_agent(filp->private_data, (void __user *) arg, 0);
+       case IB_USER_MAD_UNREGISTER_AGENT:
+               return ib_umad_unreg_agent(filp->private_data, (__u32 __user *) arg);
+       case IB_USER_MAD_ENABLE_PKEY:
+               return ib_umad_enable_pkey(filp->private_data);
+       default:
+               return -ENOIOCTLCMD;
+       }
+}
+
+#ifdef CONFIG_COMPAT
+static long ib_umad_compat_ioctl(struct file *filp, unsigned int cmd,
+                                unsigned long arg)
+{
+       switch (cmd) {
+       case IB_USER_MAD_REGISTER_AGENT:
+               return ib_umad_reg_agent(filp->private_data, compat_ptr(arg), 1);
        case IB_USER_MAD_UNREGISTER_AGENT:
-               return ib_umad_unreg_agent(filp->private_data, arg);
+               return ib_umad_unreg_agent(filp->private_data, compat_ptr(arg));
        case IB_USER_MAD_ENABLE_PKEY:
                return ib_umad_enable_pkey(filp->private_data);
        default:
                return -ENOIOCTLCMD;
        }
 }
+#endif
 
 static int ib_umad_open(struct inode *inode, struct file *filp)
 {
@@ -754,7 +792,7 @@ static int ib_umad_open(struct inode *inode, struct file *filp)
        if (!port)
                return -ENXIO;
 
-       down_write(&port->mutex);
+       mutex_lock(&port->file_mutex);
 
        if (!port->ib_dev) {
                ret = -ENXIO;
@@ -768,7 +806,7 @@ static int ib_umad_open(struct inode *inode, struct file *filp)
                goto out;
        }
 
-       spin_lock_init(&file->recv_lock);
+       mutex_init(&file->mutex);
        spin_lock_init(&file->send_lock);
        INIT_LIST_HEAD(&file->recv_list);
        INIT_LIST_HEAD(&file->send_list);
@@ -780,7 +818,7 @@ static int ib_umad_open(struct inode *inode, struct file *filp)
        list_add_tail(&file->port_list, &port->file_list);
 
 out:
-       up_write(&port->mutex);
+       mutex_unlock(&port->file_mutex);
        return ret;
 }
 
@@ -792,7 +830,8 @@ static int ib_umad_close(struct inode *inode, struct file *filp)
        int already_dead;
        int i;
 
-       down_write(&file->port->mutex);
+       mutex_lock(&file->port->file_mutex);
+       mutex_lock(&file->mutex);
 
        already_dead = file->agents_dead;
        file->agents_dead = 1;
@@ -805,14 +844,14 @@ static int ib_umad_close(struct inode *inode, struct file *filp)
 
        list_del(&file->port_list);
 
-       downgrade_write(&file->port->mutex);
+       mutex_unlock(&file->mutex);
 
        if (!already_dead)
                for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i)
                        if (file->agent[i])
                                ib_unregister_mad_agent(file->agent[i]);
 
-       up_read(&file->port->mutex);
+       mutex_unlock(&file->port->file_mutex);
 
        kfree(file);
        kref_put(&dev->ref, ib_umad_release_dev);
@@ -826,7 +865,9 @@ static const struct file_operations umad_fops = {
        .write          = ib_umad_write,
        .poll           = ib_umad_poll,
        .unlocked_ioctl = ib_umad_ioctl,
-       .compat_ioctl   = ib_umad_ioctl,
+#ifdef CONFIG_COMPAT
+       .compat_ioctl   = ib_umad_compat_ioctl,
+#endif
        .open           = ib_umad_open,
        .release        = ib_umad_close
 };
@@ -883,10 +924,10 @@ static int ib_umad_sm_close(struct inode *inode, struct file *filp)
        };
        int ret = 0;
 
-       down_write(&port->mutex);
+       mutex_lock(&port->file_mutex);
        if (port->ib_dev)
                ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
-       up_write(&port->mutex);
+       mutex_unlock(&port->file_mutex);
 
        up(&port->sm_sem);
 
@@ -907,27 +948,29 @@ static struct ib_client umad_client = {
        .remove = ib_umad_remove_one
 };
 
-static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
+static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
+                         char *buf)
 {
-       struct ib_umad_port *port = class_get_devdata(class_dev);
+       struct ib_umad_port *port = dev_get_drvdata(dev);
 
        if (!port)
                return -ENODEV;
 
        return sprintf(buf, "%s\n", port->ib_dev->name);
 }
-static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
+static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
 
-static ssize_t show_port(struct class_device *class_dev, char *buf)
+static ssize_t show_port(struct device *dev, struct device_attribute *attr,
+                        char *buf)
 {
-       struct ib_umad_port *port = class_get_devdata(class_dev);
+       struct ib_umad_port *port = dev_get_drvdata(dev);
 
        if (!port)
                return -ENODEV;
 
        return sprintf(buf, "%d\n", port->port_num);
 }
-static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
+static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
 
 static ssize_t show_abi_version(struct class *class, char *buf)
 {
@@ -950,51 +993,48 @@ static int ib_umad_init_port(struct ib_device *device, int port_num,
        port->ib_dev   = device;
        port->port_num = port_num;
        init_MUTEX(&port->sm_sem);
-       init_rwsem(&port->mutex);
+       mutex_init(&port->file_mutex);
        INIT_LIST_HEAD(&port->file_list);
 
-       port->dev = cdev_alloc();
-       if (!port->dev)
+       port->cdev = cdev_alloc();
+       if (!port->cdev)
                return -1;
-       port->dev->owner = THIS_MODULE;
-       port->dev->ops   = &umad_fops;
-       kobject_set_name(&port->dev->kobj, "umad%d", port->dev_num);
-       if (cdev_add(port->dev, base_dev + port->dev_num, 1))
+       port->cdev->owner = THIS_MODULE;
+       port->cdev->ops   = &umad_fops;
+       kobject_set_name(&port->cdev->kobj, "umad%d", port->dev_num);
+       if (cdev_add(port->cdev, base_dev + port->dev_num, 1))
                goto err_cdev;
 
-       port->class_dev = class_device_create(umad_class, NULL, port->dev->dev,
-                                             device->dma_device,
-                                             "umad%d", port->dev_num);
-       if (IS_ERR(port->class_dev))
+       port->dev = device_create_drvdata(umad_class, device->dma_device,
+                                         port->cdev->dev, port,
+                                         "umad%d", port->dev_num);
+       if (IS_ERR(port->dev))
                goto err_cdev;
 
-       if (class_device_create_file(port->class_dev, &class_device_attr_ibdev))
-               goto err_class;
-       if (class_device_create_file(port->class_dev, &class_device_attr_port))
-               goto err_class;
-
-       port->sm_dev = cdev_alloc();
-       if (!port->sm_dev)
-               goto err_class;
-       port->sm_dev->owner = THIS_MODULE;
-       port->sm_dev->ops   = &umad_sm_fops;
-       kobject_set_name(&port->sm_dev->kobj, "issm%d", port->dev_num);
-       if (cdev_add(port->sm_dev, base_dev + port->dev_num + IB_UMAD_MAX_PORTS, 1))
+       if (device_create_file(port->dev, &dev_attr_ibdev))
+               goto err_dev;
+       if (device_create_file(port->dev, &dev_attr_port))
+               goto err_dev;
+
+       port->sm_cdev = cdev_alloc();
+       if (!port->sm_cdev)
+               goto err_dev;
+       port->sm_cdev->owner = THIS_MODULE;
+       port->sm_cdev->ops   = &umad_sm_fops;
+       kobject_set_name(&port->sm_cdev->kobj, "issm%d", port->dev_num);
+       if (cdev_add(port->sm_cdev, base_dev + port->dev_num + IB_UMAD_MAX_PORTS, 1))
                goto err_sm_cdev;
 
-       port->sm_class_dev = class_device_create(umad_class, NULL, port->sm_dev->dev,
-                                                device->dma_device,
-                                                "issm%d", port->dev_num);
-       if (IS_ERR(port->sm_class_dev))
+       port->sm_dev = device_create_drvdata(umad_class, device->dma_device,
+                                            port->sm_cdev->dev, port,
+                                            "issm%d", port->dev_num);
+       if (IS_ERR(port->sm_dev))
                goto err_sm_cdev;
 
-       class_set_devdata(port->class_dev,    port);
-       class_set_devdata(port->sm_class_dev, port);
-
-       if (class_device_create_file(port->sm_class_dev, &class_device_attr_ibdev))
-               goto err_sm_class;
-       if (class_device_create_file(port->sm_class_dev, &class_device_attr_port))
-               goto err_sm_class;
+       if (device_create_file(port->sm_dev, &dev_attr_ibdev))
+               goto err_sm_dev;
+       if (device_create_file(port->sm_dev, &dev_attr_port))
+               goto err_sm_dev;
 
        spin_lock(&port_lock);
        umad_port[port->dev_num] = port;
@@ -1002,17 +1042,17 @@ static int ib_umad_init_port(struct ib_device *device, int port_num,
 
        return 0;
 
-err_sm_class:
-       class_device_destroy(umad_class, port->sm_dev->dev);
+err_sm_dev:
+       device_destroy(umad_class, port->sm_cdev->dev);
 
 err_sm_cdev:
-       cdev_del(port->sm_dev);
+       cdev_del(port->sm_cdev);
 
-err_class:
-       class_device_destroy(umad_class, port->dev->dev);
+err_dev:
+       device_destroy(umad_class, port->cdev->dev);
 
 err_cdev:
-       cdev_del(port->dev);
+       cdev_del(port->cdev);
        clear_bit(port->dev_num, dev_map);
 
        return -1;
@@ -1021,57 +1061,38 @@ err_cdev:
 static void ib_umad_kill_port(struct ib_umad_port *port)
 {
        struct ib_umad_file *file;
+       int already_dead;
        int id;
 
-       class_set_devdata(port->class_dev,    NULL);
-       class_set_devdata(port->sm_class_dev, NULL);
+       dev_set_drvdata(port->dev,    NULL);
+       dev_set_drvdata(port->sm_dev, NULL);
 
-       class_device_destroy(umad_class, port->dev->dev);
-       class_device_destroy(umad_class, port->sm_dev->dev);
+       device_destroy(umad_class, port->cdev->dev);
+       device_destroy(umad_class, port->sm_cdev->dev);
 
-       cdev_del(port->dev);
-       cdev_del(port->sm_dev);
+       cdev_del(port->cdev);
+       cdev_del(port->sm_cdev);
 
        spin_lock(&port_lock);
        umad_port[port->dev_num] = NULL;
        spin_unlock(&port_lock);
 
-       down_write(&port->mutex);
+       mutex_lock(&port->file_mutex);
 
        port->ib_dev = NULL;
 
-       /*
-        * Now go through the list of files attached to this port and
-        * unregister all of their MAD agents.  We need to hold
-        * port->mutex while doing this to avoid racing with
-        * ib_umad_close(), but we can't hold the mutex for writing
-        * while calling ib_unregister_mad_agent(), since that might
-        * deadlock by calling back into queue_packet().  So we
-        * downgrade our lock to a read lock, and then drop and
-        * reacquire the write lock for the next iteration.
-        *
-        * We do list_del_init() on the file's list_head so that the
-        * list_del in ib_umad_close() is still OK, even after the
-        * file is removed from the list.
-        */
-       while (!list_empty(&port->file_list)) {
-               file = list_entry(port->file_list.next, struct ib_umad_file,
-                                 port_list);
-
+       list_for_each_entry(file, &port->file_list, port_list) {
+               mutex_lock(&file->mutex);
+               already_dead = file->agents_dead;
                file->agents_dead = 1;
-               list_del_init(&file->port_list);
-
-               downgrade_write(&port->mutex);
+               mutex_unlock(&file->mutex);
 
                for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id)
                        if (file->agent[id])
                                ib_unregister_mad_agent(file->agent[id]);
-
-               up_read(&port->mutex);
-               down_write(&port->mutex);
        }
 
-       up_write(&port->mutex);
+       mutex_unlock(&port->file_mutex);
 
        clear_bit(port->dev_num, dev_map);
 }