Merge master.kernel.org:/pub/scm/linux/kernel/git/herbert/crypto-2.6
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / infiniband / core / device.c
index e169e798354b6283174a66adfe0f0027b9412ca5..3ada17c0f23921b87094cdacf638dbdc33a927fc 100644 (file)
 #include <linux/module.h>
 #include <linux/string.h>
 #include <linux/errno.h>
+#include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/init.h>
-
-#include <asm/semaphore.h>
+#include <linux/mutex.h>
+#include <linux/workqueue.h>
 
 #include "core_priv.h"
 
@@ -57,13 +58,13 @@ static LIST_HEAD(device_list);
 static LIST_HEAD(client_list);
 
 /*
- * device_sem protects access to both device_list and client_list.
+ * device_mutex protects access to both device_list and client_list.
  * There's no real point to using multiple locks or something fancier
  * like an rwsem: we always access both lists, and we're always
  * modifying one list or the other list.  In any case this is not a
  * hot path so there's no point in trying to optimize.
  */
-static DECLARE_MUTEX(device_sem);
+static DEFINE_MUTEX(device_mutex);
 
 static int ib_device_check_mandatory(struct ib_device *device)
 {
@@ -94,7 +95,7 @@ static int ib_device_check_mandatory(struct ib_device *device)
        };
        int i;
 
-       for (i = 0; i < sizeof mandatory_table / sizeof mandatory_table[0]; ++i) {
+       for (i = 0; i < ARRAY_SIZE(mandatory_table); ++i) {
                if (!*(void **) ((void *) device + mandatory_table[i].offset)) {
                        printk(KERN_WARNING "Device %s is missing mandatory function %s\n",
                               device->name, mandatory_table[i].name);
@@ -149,6 +150,18 @@ static int alloc_name(char *name)
        return 0;
 }
 
+static int start_port(struct ib_device *device)
+{
+       return (device->node_type == RDMA_NODE_IB_SWITCH) ? 0 : 1;
+}
+
+
+static int end_port(struct ib_device *device)
+{
+       return (device->node_type == RDMA_NODE_IB_SWITCH) ?
+               0 : device->phys_port_cnt;
+}
+
 /**
  * ib_alloc_device - allocate an IB device struct
  * @size:size of structure to allocate
@@ -208,6 +221,45 @@ static int add_client_context(struct ib_device *device, struct ib_client *client
        return 0;
 }
 
+static int read_port_table_lengths(struct ib_device *device)
+{
+       struct ib_port_attr *tprops = NULL;
+       int num_ports, ret = -ENOMEM;
+       u8 port_index;
+
+       tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
+       if (!tprops)
+               goto out;
+
+       num_ports = end_port(device) - start_port(device) + 1;
+
+       device->pkey_tbl_len = kmalloc(sizeof *device->pkey_tbl_len * num_ports,
+                                      GFP_KERNEL);
+       device->gid_tbl_len = kmalloc(sizeof *device->gid_tbl_len * num_ports,
+                                     GFP_KERNEL);
+       if (!device->pkey_tbl_len || !device->gid_tbl_len)
+               goto err;
+
+       for (port_index = 0; port_index < num_ports; ++port_index) {
+               ret = ib_query_port(device, port_index + start_port(device),
+                                       tprops);
+               if (ret)
+                       goto err;
+               device->pkey_tbl_len[port_index] = tprops->pkey_tbl_len;
+               device->gid_tbl_len[port_index]  = tprops->gid_tbl_len;
+       }
+
+       ret = 0;
+       goto out;
+
+err:
+       kfree(device->gid_tbl_len);
+       kfree(device->pkey_tbl_len);
+out:
+       kfree(tprops);
+       return ret;
+}
+
 /**
  * ib_register_device - Register an IB device with IB core
  * @device:Device to register
@@ -221,7 +273,7 @@ int ib_register_device(struct ib_device *device)
 {
        int ret;
 
-       down(&device_sem);
+       mutex_lock(&device_mutex);
 
        if (strchr(device->name, '%')) {
                ret = alloc_name(device->name);
@@ -239,10 +291,19 @@ int ib_register_device(struct ib_device *device)
        spin_lock_init(&device->event_handler_lock);
        spin_lock_init(&device->client_data_lock);
 
+       ret = read_port_table_lengths(device);
+       if (ret) {
+               printk(KERN_WARNING "Couldn't create table lengths cache for device %s\n",
+                      device->name);
+               goto out;
+       }
+
        ret = ib_device_register_sysfs(device);
        if (ret) {
                printk(KERN_WARNING "Couldn't register device %s with driver model\n",
                       device->name);
+               kfree(device->gid_tbl_len);
+               kfree(device->pkey_tbl_len);
                goto out;
        }
 
@@ -259,7 +320,7 @@ int ib_register_device(struct ib_device *device)
        }
 
  out:
-       up(&device_sem);
+       mutex_unlock(&device_mutex);
        return ret;
 }
 EXPORT_SYMBOL(ib_register_device);
@@ -276,7 +337,7 @@ void ib_unregister_device(struct ib_device *device)
        struct ib_client_data *context, *tmp;
        unsigned long flags;
 
-       down(&device_sem);
+       mutex_lock(&device_mutex);
 
        list_for_each_entry_reverse(client, &client_list, list)
                if (client->remove)
@@ -284,7 +345,10 @@ void ib_unregister_device(struct ib_device *device)
 
        list_del(&device->core_list);
 
-       up(&device_sem);
+       kfree(device->gid_tbl_len);
+       kfree(device->pkey_tbl_len);
+
+       mutex_unlock(&device_mutex);
 
        spin_lock_irqsave(&device->client_data_lock, flags);
        list_for_each_entry_safe(context, tmp, &device->client_data_list, list)
@@ -312,14 +376,14 @@ int ib_register_client(struct ib_client *client)
 {
        struct ib_device *device;
 
-       down(&device_sem);
+       mutex_lock(&device_mutex);
 
        list_add_tail(&client->list, &client_list);
        list_for_each_entry(device, &device_list, core_list)
                if (client->add && !add_client_context(device, client))
                        client->add(device);
 
-       up(&device_sem);
+       mutex_unlock(&device_mutex);
 
        return 0;
 }
@@ -339,7 +403,7 @@ void ib_unregister_client(struct ib_client *client)
        struct ib_device *device;
        unsigned long flags;
 
-       down(&device_sem);
+       mutex_lock(&device_mutex);
 
        list_for_each_entry(device, &device_list, core_list) {
                if (client->remove)
@@ -355,7 +419,7 @@ void ib_unregister_client(struct ib_client *client)
        }
        list_del(&client->list);
 
-       up(&device_sem);
+       mutex_unlock(&device_mutex);
 }
 EXPORT_SYMBOL(ib_unregister_client);
 
@@ -386,7 +450,7 @@ void *ib_get_client_data(struct ib_device *device, struct ib_client *client)
 EXPORT_SYMBOL(ib_get_client_data);
 
 /**
- * ib_set_client_data - Get IB client context
+ * ib_set_client_data - Set IB client context
  * @device:Device to set context for
  * @client:Client to set context for
  * @data:Context to set
@@ -506,10 +570,7 @@ int ib_query_port(struct ib_device *device,
                  u8 port_num,
                  struct ib_port_attr *port_attr)
 {
-       if (device->node_type == IB_NODE_SWITCH) {
-               if (port_num)
-                       return -EINVAL;
-       } else if (port_num < 1 || port_num > device->phys_port_cnt)
+       if (port_num < start_port(device) || port_num > end_port(device))
                return -EINVAL;
 
        return device->query_port(device, port_num, port_attr);
@@ -581,10 +642,7 @@ int ib_modify_port(struct ib_device *device,
                   u8 port_num, int port_modify_mask,
                   struct ib_port_modify *port_modify)
 {
-       if (device->node_type == IB_NODE_SWITCH) {
-               if (port_num)
-                       return -EINVAL;
-       } else if (port_num < 1 || port_num > device->phys_port_cnt)
+       if (port_num < start_port(device) || port_num > end_port(device))
                return -EINVAL;
 
        return device->modify_port(device, port_num, port_modify_mask,
@@ -592,6 +650,68 @@ int ib_modify_port(struct ib_device *device,
 }
 EXPORT_SYMBOL(ib_modify_port);
 
+/**
+ * ib_find_gid - Returns the port number and GID table index where
+ *   a specified GID value occurs.
+ * @device: The device to query.
+ * @gid: The GID value to search for.
+ * @port_num: The port number of the device where the GID value was found.
+ * @index: The index into the GID table where the GID was found.  This
+ *   parameter may be NULL.
+ */
+int ib_find_gid(struct ib_device *device, union ib_gid *gid,
+               u8 *port_num, u16 *index)
+{
+       union ib_gid tmp_gid;
+       int ret, port, i;
+
+       for (port = start_port(device); port <= end_port(device); ++port) {
+               for (i = 0; i < device->gid_tbl_len[port - start_port(device)]; ++i) {
+                       ret = ib_query_gid(device, port, i, &tmp_gid);
+                       if (ret)
+                               return ret;
+                       if (!memcmp(&tmp_gid, gid, sizeof *gid)) {
+                               *port_num = port;
+                               if (index)
+                                       *index = i;
+                               return 0;
+                       }
+               }
+       }
+
+       return -ENOENT;
+}
+EXPORT_SYMBOL(ib_find_gid);
+
+/**
+ * ib_find_pkey - Returns the PKey table index where a specified
+ *   PKey value occurs.
+ * @device: The device to query.
+ * @port_num: The port number of the device to search for the PKey.
+ * @pkey: The PKey value to search for.
+ * @index: The index into the PKey table where the PKey was found.
+ */
+int ib_find_pkey(struct ib_device *device,
+                u8 port_num, u16 pkey, u16 *index)
+{
+       int ret, i;
+       u16 tmp_pkey;
+
+       for (i = 0; i < device->pkey_tbl_len[port_num - start_port(device)]; ++i) {
+               ret = ib_query_pkey(device, port_num, i, &tmp_pkey);
+               if (ret)
+                       return ret;
+
+               if (pkey == tmp_pkey) {
+                       *index = i;
+                       return 0;
+               }
+       }
+
+       return -ENOENT;
+}
+EXPORT_SYMBOL(ib_find_pkey);
+
 static int __init ib_core_init(void)
 {
        int ret;
@@ -613,6 +733,8 @@ static void __exit ib_core_cleanup(void)
 {
        ib_cache_cleanup();
        ib_sysfs_cleanup();
+       /* Make sure that any pending umem accounting work is done. */
+       flush_scheduled_work();
 }
 
 module_init(ib_core_init);