]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/infiniband/core/user_mad.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/jk/spufs into...
[linux-2.6-omap-h63xx.git] / drivers / infiniband / core / user_mad.c
index d97ded25c4ff98bb7c8e68fa299f569bcfec1eb3..4e915104ac4ca38e70569896a746c5686fba28ac 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
@@ -42,8 +43,9 @@
 #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 <asm/uaccess.h>
 #include <asm/semaphore.h>
@@ -93,7 +95,7 @@ struct ib_umad_port {
        struct class_device   *sm_class_dev;
        struct semaphore       sm_sem;
 
-       struct rw_semaphore    mutex;
+       struct mutex           file_mutex;
        struct list_head       file_list;
 
        struct ib_device      *ib_dev;
@@ -109,15 +111,17 @@ 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];
        int                     agents_dead;
+       u8                      use_pkey_index;
+       u8                      already_used;
 };
 
 struct ib_umad_packet {
@@ -147,7 +151,13 @@ static void ib_umad_release_dev(struct kref *ref)
        kfree(dev);
 }
 
-/* caller must hold port->mutex at least for reading */
+static int hdr_size(struct ib_umad_file *file)
+{
+       return file->use_pkey_index ? sizeof (struct ib_user_mad_hdr) :
+               sizeof (struct ib_user_mad_hdr_old);
+}
+
+/* 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];
@@ -159,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)
@@ -221,13 +229,13 @@ static void recv_handler(struct ib_mad_agent *agent,
        packet->length = mad_recv_wc->mad_len;
        packet->recv_wc = mad_recv_wc;
 
-       packet->mad.hdr.status    = 0;
-       packet->mad.hdr.length    = sizeof (struct ib_user_mad) +
-                                   mad_recv_wc->mad_len;
-       packet->mad.hdr.qpn       = cpu_to_be32(mad_recv_wc->wc->src_qp);
-       packet->mad.hdr.lid       = cpu_to_be16(mad_recv_wc->wc->slid);
-       packet->mad.hdr.sl        = mad_recv_wc->wc->sl;
-       packet->mad.hdr.path_bits = mad_recv_wc->wc->dlid_path_bits;
+       packet->mad.hdr.status     = 0;
+       packet->mad.hdr.length     = hdr_size(file) + mad_recv_wc->mad_len;
+       packet->mad.hdr.qpn        = cpu_to_be32(mad_recv_wc->wc->src_qp);
+       packet->mad.hdr.lid        = cpu_to_be16(mad_recv_wc->wc->slid);
+       packet->mad.hdr.sl         = mad_recv_wc->wc->sl;
+       packet->mad.hdr.path_bits  = mad_recv_wc->wc->dlid_path_bits;
+       packet->mad.hdr.pkey_index = mad_recv_wc->wc->pkey_index;
        packet->mad.hdr.grh_present = !!(mad_recv_wc->wc->wc_flags & IB_WC_GRH);
        if (packet->mad.hdr.grh_present) {
                struct ib_ah_attr ah_attr;
@@ -253,8 +261,8 @@ err1:
        ib_free_recv_mad(mad_recv_wc);
 }
 
-static ssize_t copy_recv_mad(char __user *buf, struct ib_umad_packet *packet,
-                            size_t count)
+static ssize_t copy_recv_mad(struct ib_umad_file *file, char __user *buf,
+                            struct ib_umad_packet *packet, size_t count)
 {
        struct ib_mad_recv_buf *recv_buf;
        int left, seg_payload, offset, max_seg_payload;
@@ -262,15 +270,15 @@ static ssize_t copy_recv_mad(char __user *buf, struct ib_umad_packet *packet,
        /* We need enough room to copy the first (or only) MAD segment. */
        recv_buf = &packet->recv_wc->recv_buf;
        if ((packet->length <= sizeof (*recv_buf->mad) &&
-            count < sizeof (packet->mad) + packet->length) ||
+            count < hdr_size(file) + packet->length) ||
            (packet->length > sizeof (*recv_buf->mad) &&
-            count < sizeof (packet->mad) + sizeof (*recv_buf->mad)))
+            count < hdr_size(file) + sizeof (*recv_buf->mad)))
                return -EINVAL;
 
-       if (copy_to_user(buf, &packet->mad, sizeof (packet->mad)))
+       if (copy_to_user(buf, &packet->mad, hdr_size(file)))
                return -EFAULT;
 
-       buf += sizeof (packet->mad);
+       buf += hdr_size(file);
        seg_payload = min_t(int, packet->length, sizeof (*recv_buf->mad));
        if (copy_to_user(buf, recv_buf->mad, seg_payload))
                return -EFAULT;
@@ -280,7 +288,7 @@ static ssize_t copy_recv_mad(char __user *buf, struct ib_umad_packet *packet,
                 * Multipacket RMPP MAD message. Copy remainder of message.
                 * Note that last segment may have a shorter payload.
                 */
-               if (count < sizeof (packet->mad) + packet->length) {
+               if (count < hdr_size(file) + packet->length) {
                        /*
                         * The buffer is too small, return the first RMPP segment,
                         * which includes the RMPP message length.
@@ -300,18 +308,23 @@ static ssize_t copy_recv_mad(char __user *buf, struct ib_umad_packet *packet,
                                return -EFAULT;
                }
        }
-       return sizeof (packet->mad) + packet->length;
+       return hdr_size(file) + packet->length;
 }
 
-static ssize_t copy_send_mad(char __user *buf, struct ib_umad_packet *packet,
-                            size_t count)
+static ssize_t copy_send_mad(struct ib_umad_file *file, char __user *buf,
+                            struct ib_umad_packet *packet, size_t count)
 {
-       ssize_t size = sizeof (packet->mad) + packet->length;
+       ssize_t size = hdr_size(file) + packet->length;
 
        if (count < size)
                return -EINVAL;
 
-       if (copy_to_user(buf, &packet->mad, size))
+       if (copy_to_user(buf, &packet->mad, hdr_size(file)))
+               return -EFAULT;
+
+       buf += hdr_size(file);
+
+       if (copy_to_user(buf, packet->mad.data, packet->length))
                return -EFAULT;
 
        return size;
@@ -324,13 +337,13 @@ static ssize_t ib_umad_read(struct file *filp, char __user *buf,
        struct ib_umad_packet *packet;
        ssize_t ret;
 
-       if (count < sizeof (struct ib_user_mad))
+       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;
@@ -339,24 +352,24 @@ 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(buf, packet, count);
+               ret = copy_recv_mad(file, buf, packet, count);
        else
-               ret = copy_send_mad(buf, packet, count);
+               ret = copy_send_mad(file, buf, packet, count);
 
        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);
@@ -442,15 +455,14 @@ static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
        __be64 *tid;
        int ret, data_len, hdr_len, copy_offset, rmpp_active;
 
-       if (count < sizeof (struct ib_user_mad) + IB_MGMT_RMPP_HDR)
+       if (count < hdr_size(file) + IB_MGMT_RMPP_HDR)
                return -EINVAL;
 
        packet = kzalloc(sizeof *packet + IB_MGMT_RMPP_HDR, GFP_KERNEL);
        if (!packet)
                return -ENOMEM;
 
-       if (copy_from_user(&packet->mad, buf,
-                           sizeof (struct ib_user_mad) + IB_MGMT_RMPP_HDR)) {
+       if (copy_from_user(&packet->mad, buf, hdr_size(file))) {
                ret = -EFAULT;
                goto err;
        }
@@ -461,7 +473,14 @@ static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
                goto err;
        }
 
-       down_read(&file->port->mutex);
+       buf += hdr_size(file);
+
+       if (copy_from_user(packet->mad.data, buf, IB_MGMT_RMPP_HDR)) {
+               ret = -EFAULT;
+               goto err;
+       }
+
+       mutex_lock(&file->mutex);
 
        agent = __get_agent(file, packet->mad.hdr.id);
        if (!agent) {
@@ -500,11 +519,11 @@ static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
                              IB_MGMT_RMPP_FLAG_ACTIVE;
        }
 
-       data_len = count - sizeof (struct ib_user_mad) - hdr_len;
+       data_len = count - hdr_size(file) - hdr_len;
        packet->msg = ib_create_send_mad(agent,
                                         be32_to_cpu(packet->mad.hdr.qpn),
-                                        0, rmpp_active, hdr_len,
-                                        data_len, GFP_KERNEL);
+                                        packet->mad.hdr.pkey_index, rmpp_active,
+                                        hdr_len, data_len, GFP_KERNEL);
        if (IS_ERR(packet->msg)) {
                ret = PTR_ERR(packet->msg);
                goto err_ah;
@@ -517,7 +536,6 @@ static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
 
        /* Copy MAD header.  Any RMPP header is already in place. */
        memcpy(packet->msg->mad, packet->mad.data, IB_MGMT_MAD_HDR);
-       buf += sizeof (struct ib_user_mad);
 
        if (!rmpp_active) {
                if (copy_from_user(packet->msg->mad + copy_offset,
@@ -558,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:
@@ -568,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;
@@ -589,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;
        }
@@ -625,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,
@@ -636,34 +666,51 @@ 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;
        }
 
+       if (!file->already_used) {
+               file->already_used = 1;
+               if (!file->use_pkey_index) {
+                       printk(KERN_WARNING "user_mad: process %s did not enable "
+                              "P_Key index support.\n", current->comm);
+                       printk(KERN_WARNING "user_mad:   Documentation/infiniband/user_mad.txt "
+                              "has info on the new ABI.\n");
+               }
+       }
+
        file->agent[agent_id] = agent;
        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;
@@ -674,11 +721,27 @@ 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;
+}
+
+static long ib_umad_enable_pkey(struct ib_umad_file *file)
+{
+       int ret = 0;
+
+       mutex_lock(&file->mutex);
+       if (file->already_used)
+               ret = -EINVAL;
+       else
+               file->use_pkey_index = 1;
+       mutex_unlock(&file->mutex);
+
        return ret;
 }
 
@@ -687,13 +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)
 {
@@ -710,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;
@@ -724,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);
@@ -736,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;
 }
 
@@ -748,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;
@@ -761,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);
@@ -782,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
 };
@@ -839,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);
 
@@ -906,7 +991,7 @@ 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();
@@ -977,6 +1062,7 @@ 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);
@@ -992,42 +1078,22 @@ static void ib_umad_kill_port(struct ib_umad_port *port)
        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);
 }