IB/umad: fix RMPP handling
[pandora-kernel.git] / drivers / infiniband / core / user_mad.c
index 97128e2..eb7f525 100644 (file)
@@ -31,7 +31,7 @@
  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  *
- * $Id: user_mad.c 2814 2005-07-06 19:14:09Z halr $
+ * $Id: user_mad.c 4010 2005-11-09 23:11:56Z roland $
  */
 
 #include <linux/module.h>
@@ -94,6 +94,9 @@ struct ib_umad_port {
        struct class_device   *sm_class_dev;
        struct semaphore       sm_sem;
 
+       struct rw_semaphore    mutex;
+       struct list_head       file_list;
+
        struct ib_device      *ib_dev;
        struct ib_umad_device *umad_dev;
        int                    dev_num;
@@ -107,13 +110,13 @@ struct ib_umad_device {
 };
 
 struct ib_umad_file {
-       struct ib_umad_port *port;
-       spinlock_t           recv_lock;
-       struct list_head     recv_list;
-       wait_queue_head_t    recv_wait;
-       struct rw_semaphore  agent_mutex;
-       struct ib_mad_agent *agent[IB_UMAD_MAX_AGENTS];
-       struct ib_mr        *mr[IB_UMAD_MAX_AGENTS];
+       struct ib_umad_port    *port;
+       struct list_head        recv_list;
+       struct list_head        port_list;
+       spinlock_t              recv_lock;
+       wait_queue_head_t       recv_wait;
+       struct ib_mad_agent    *agent[IB_UMAD_MAX_AGENTS];
+       int                     agents_dead;
 };
 
 struct ib_umad_packet {
@@ -142,17 +145,24 @@ static void ib_umad_release_dev(struct kref *ref)
        kfree(dev);
 }
 
+/* caller must hold port->mutex at least for reading */
+static struct ib_mad_agent *__get_agent(struct ib_umad_file *file, int id)
+{
+       return file->agents_dead ? NULL : file->agent[id];
+}
+
 static int queue_packet(struct ib_umad_file *file,
                        struct ib_mad_agent *agent,
                        struct ib_umad_packet *packet)
 {
        int ret = 1;
 
-       down_read(&file->agent_mutex);
+       down_read(&file->port->mutex);
+
        for (packet->mad.hdr.id = 0;
             packet->mad.hdr.id < IB_UMAD_MAX_AGENTS;
             packet->mad.hdr.id++)
-               if (agent == file->agent[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);
@@ -161,7 +171,7 @@ static int queue_packet(struct ib_umad_file *file,
                        break;
                }
 
-       up_read(&file->agent_mutex);
+       up_read(&file->port->mutex);
 
        return ret;
 }
@@ -300,9 +310,9 @@ static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
        u8 method;
        __be64 *tid;
        int ret, length, hdr_len, copy_offset;
-       int rmpp_active = 0;
+       int rmpp_active, has_rmpp_header;
 
-       if (count < sizeof (struct ib_user_mad))
+       if (count < sizeof (struct ib_user_mad) + IB_MGMT_RMPP_HDR)
                return -EINVAL;
 
        length = count - sizeof (struct ib_user_mad);
@@ -322,9 +332,9 @@ static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
                goto err;
        }
 
-       down_read(&file->agent_mutex);
+       down_read(&file->port->mutex);
 
-       agent = file->agent[packet->mad.hdr.id];
+       agent = __get_agent(file, packet->mad.hdr.id);
        if (!agent) {
                ret = -EINVAL;
                goto err_up;
@@ -350,28 +360,31 @@ static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
        }
 
        rmpp_mad = (struct ib_rmpp_mad *) packet->mad.data;
-       if (ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) & IB_MGMT_RMPP_FLAG_ACTIVE) {
-               /* RMPP active */
-               if (!agent->rmpp_version) {
-                       ret = -EINVAL;
-                       goto err_ah;
-               }
-
-               /* Validate that the management class can support RMPP */
-               if (rmpp_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_ADM) {
-                       hdr_len = IB_MGMT_SA_HDR;
-               } else if ((rmpp_mad->mad_hdr.mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
-                           (rmpp_mad->mad_hdr.mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)) {
-                               hdr_len = IB_MGMT_VENDOR_HDR;
-               } else {
-                       ret = -EINVAL;
-                       goto err_ah;
-               }
-               rmpp_active = 1;
+       if (rmpp_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_ADM) {
+               hdr_len = IB_MGMT_SA_HDR;
                copy_offset = IB_MGMT_RMPP_HDR;
+               has_rmpp_header = 1;
+       } else if (rmpp_mad->mad_hdr.mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START &&
+                  rmpp_mad->mad_hdr.mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END) {
+                       hdr_len = IB_MGMT_VENDOR_HDR;
+                       copy_offset = IB_MGMT_RMPP_HDR;
+                       has_rmpp_header = 1;
        } else {
                hdr_len = IB_MGMT_MAD_HDR;
                copy_offset = IB_MGMT_MAD_HDR;
+               has_rmpp_header = 0;
+       }
+
+       if (has_rmpp_header)
+               rmpp_active = ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
+                             IB_MGMT_RMPP_FLAG_ACTIVE;
+       else
+               rmpp_active = 0;
+
+       /* Validate that the management class can support RMPP */
+       if (rmpp_active && !agent->rmpp_version) {
+               ret = -EINVAL;
+               goto err_ah;
        }
 
        packet->msg = ib_create_send_mad(agent,
@@ -419,7 +432,7 @@ static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
        if (ret)
                goto err_msg;
 
-       up_read(&file->agent_mutex);
+       up_read(&file->port->mutex);
 
        return count;
 
@@ -430,7 +443,7 @@ err_ah:
        ib_destroy_ah(ah);
 
 err_up:
-       up_read(&file->agent_mutex);
+       up_read(&file->port->mutex);
 
 err:
        kfree(packet);
@@ -460,7 +473,12 @@ static int ib_umad_reg_agent(struct ib_umad_file *file, unsigned long arg)
        int agent_id;
        int ret;
 
-       down_write(&file->agent_mutex);
+       down_write(&file->port->mutex);
+
+       if (!file->port->ib_dev) {
+               ret = -EPIPE;
+               goto out;
+       }
 
        if (copy_from_user(&ureq, (void __user *) arg, sizeof ureq)) {
                ret = -EFAULT;
@@ -473,7 +491,7 @@ static int ib_umad_reg_agent(struct ib_umad_file *file, unsigned long arg)
        }
 
        for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)
-               if (!file->agent[agent_id])
+               if (!__get_agent(file, agent_id))
                        goto found;
 
        ret = -ENOMEM;
@@ -497,58 +515,46 @@ found:
                goto out;
        }
 
-       file->agent[agent_id] = agent;
-
-       file->mr[agent_id] = ib_get_dma_mr(agent->qp->pd, IB_ACCESS_LOCAL_WRITE);
-       if (IS_ERR(file->mr[agent_id])) {
-               ret = -ENOMEM;
-               goto err;
-       }
-
        if (put_user(agent_id,
                     (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) {
                ret = -EFAULT;
-               goto err_mr;
+               ib_unregister_mad_agent(agent);
+               goto out;
        }
 
+       file->agent[agent_id] = agent;
        ret = 0;
-       goto out;
-
-err_mr:
-       ib_dereg_mr(file->mr[agent_id]);
-
-err:
-       file->agent[agent_id] = NULL;
-       ib_unregister_mad_agent(agent);
 
 out:
-       up_write(&file->agent_mutex);
+       up_write(&file->port->mutex);
        return ret;
 }
 
 static int ib_umad_unreg_agent(struct ib_umad_file *file, unsigned long arg)
 {
+       struct ib_mad_agent *agent = NULL;
        u32 id;
        int ret = 0;
 
-       down_write(&file->agent_mutex);
+       if (get_user(id, (u32 __user *) arg))
+               return -EFAULT;
 
-       if (get_user(id, (u32 __user *) arg)) {
-               ret = -EFAULT;
-               goto out;
-       }
+       down_write(&file->port->mutex);
 
-       if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !file->agent[id]) {
+       if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !__get_agent(file, id)) {
                ret = -EINVAL;
                goto out;
        }
 
-       ib_dereg_mr(file->mr[id]);
-       ib_unregister_mad_agent(file->agent[id]);
+       agent = file->agent[id];
        file->agent[id] = NULL;
 
 out:
-       up_write(&file->agent_mutex);
+       up_write(&file->port->mutex);
+
+       if (agent)
+               ib_unregister_mad_agent(agent);
+
        return ret;
 }
 
@@ -569,6 +575,7 @@ static int ib_umad_open(struct inode *inode, struct file *filp)
 {
        struct ib_umad_port *port;
        struct ib_umad_file *file;
+       int ret = 0;
 
        spin_lock(&port_lock);
        port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE];
@@ -579,21 +586,32 @@ static int ib_umad_open(struct inode *inode, struct file *filp)
        if (!port)
                return -ENXIO;
 
+       down_write(&port->mutex);
+
+       if (!port->ib_dev) {
+               ret = -ENXIO;
+               goto out;
+       }
+
        file = kzalloc(sizeof *file, GFP_KERNEL);
        if (!file) {
                kref_put(&port->umad_dev->ref, ib_umad_release_dev);
-               return -ENOMEM;
+               ret = -ENOMEM;
+               goto out;
        }
 
        spin_lock_init(&file->recv_lock);
-       init_rwsem(&file->agent_mutex);
        INIT_LIST_HEAD(&file->recv_list);
        init_waitqueue_head(&file->recv_wait);
 
        file->port = port;
        filp->private_data = file;
 
-       return 0;
+       list_add_tail(&file->port_list, &port->file_list);
+
+out:
+       up_write(&port->mutex);
+       return ret;
 }
 
 static int ib_umad_close(struct inode *inode, struct file *filp)
@@ -601,19 +619,29 @@ static int ib_umad_close(struct inode *inode, struct file *filp)
        struct ib_umad_file *file = filp->private_data;
        struct ib_umad_device *dev = file->port->umad_dev;
        struct ib_umad_packet *packet, *tmp;
+       int already_dead;
        int i;
 
-       for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i)
-               if (file->agent[i]) {
-                       ib_dereg_mr(file->mr[i]);
-                       ib_unregister_mad_agent(file->agent[i]);
-               }
+       down_write(&file->port->mutex);
+
+       already_dead = file->agents_dead;
+       file->agents_dead = 1;
 
        list_for_each_entry_safe(packet, tmp, &file->recv_list, list)
                kfree(packet);
 
-       kfree(file);
+       list_del(&file->port_list);
 
+       downgrade_write(&file->port->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);
+
+       kfree(file);
        kref_put(&dev->ref, ib_umad_release_dev);
 
        return 0;
@@ -680,9 +708,13 @@ static int ib_umad_sm_close(struct inode *inode, struct file *filp)
        struct ib_port_modify props = {
                .clr_port_cap_mask = IB_PORT_SM
        };
-       int ret;
+       int ret = 0;
+
+       down_write(&port->mutex);
+       if (port->ib_dev)
+               ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
+       up_write(&port->mutex);
 
-       ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
        up(&port->sm_sem);
 
        kref_put(&port->umad_dev->ref, ib_umad_release_dev);
@@ -745,6 +777,8 @@ 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);
+       INIT_LIST_HEAD(&port->file_list);
 
        port->dev = cdev_alloc();
        if (!port->dev)
@@ -771,7 +805,7 @@ static int ib_umad_init_port(struct ib_device *device, int port_num,
                goto err_class;
        port->sm_dev->owner = THIS_MODULE;
        port->sm_dev->ops   = &umad_sm_fops;
-       kobject_set_name(&port->dev->kobj, "issm%d", port->dev_num);
+       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))
                goto err_sm_cdev;
 
@@ -813,6 +847,9 @@ err_cdev:
 
 static void ib_umad_kill_port(struct ib_umad_port *port)
 {
+       struct ib_umad_file *file;
+       int id;
+
        class_set_devdata(port->class_dev,    NULL);
        class_set_devdata(port->sm_class_dev, NULL);
 
@@ -826,6 +863,43 @@ 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);
+
+       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);
+
+               file->agents_dead = 1;
+               list_del_init(&file->port_list);
+
+               downgrade_write(&port->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);
+
        clear_bit(port->dev_num, dev_map);
 }
 
@@ -865,7 +939,7 @@ static void ib_umad_add_one(struct ib_device *device)
 
 err:
        while (--i >= s)
-               ib_umad_kill_port(&umad_dev->port[i]);
+               ib_umad_kill_port(&umad_dev->port[i - s]);
 
        kref_put(&umad_dev->ref, ib_umad_release_dev);
 }